Skip to content

Commit a2c84b9

Browse files
author
Thomas Mahler
committed
prepare usage of MicroHs backend
1 parent 22ff6ff commit a2c84b9

File tree

5 files changed

+106
-3
lines changed

5 files changed

+106
-3
lines changed

app/Main.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import qualified Data.Bifunctor
2020
import LambdaToSKI (compileBracket)
2121
import TermReducer
2222
import IonAssembly (toIon)
23+
import MicroHsExp (toMhsPrg)
2324

2425

2526
printGraph :: ST s (STRef s (Graph s)) -> ST s String
@@ -63,7 +64,7 @@ ackermann = [r|
6364

6465
factorial :: SourceCode
6566
factorial = [r|
66-
fact = y(λf n. if (is0 n) 1 (* n (f (sub1 n))))
67+
fact = y(λf n. if (eql n 0) 1 (* n (f (- n 1))))
6768
main = fact 100
6869
|]
6970

@@ -73,6 +74,10 @@ fibonacci = [r|
7374
main = fib 10
7475
|]
7576

77+
printMhs :: CL -> IO ()
78+
printMhs cl = do
79+
--let (n, exps, prg) = toStringCMdl ([], toMhsExp cl)
80+
putStrLn ("MicroHs expression: " ++ toMhsPrg cl)
7681

7782
printCS :: CL -> IO ()
7883
printCS cl = do
@@ -98,17 +103,20 @@ showCompilations source = do
98103
putStrLn "The main expression compiled to SICKBY combinator expressions by recursice bracket abstraction:"
99104
print expr
100105
printCS expr
106+
printMhs expr
101107
putStrLn ""
102108

103109
putStrLn "applying plain Kiselyov compilation:"
104110
print $ compilePlain env
105111
printCS $ compilePlain env
112+
printMhs $ compilePlain env
106113
putStrLn ""
107114

108115
let exprK = compileK env
109116
putStrLn "The main expression compiled to SICKBY combinator expressions with K-optimization:"
110117
print exprK
111118
printCS exprK
119+
printMhs exprK
112120
putStrLn ""
113121

114122

cabal.project

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
packages: .
2+
3+
source-repository-package
4+
type: git
5+
location: https://github.com/augustss/MicroHs.git
6+
tag: 38cbb10b8f1bd5bee13d9afb2b15239d8c897000

lambda-ski.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ library
3434
IonAssembly
3535
Kiselyov
3636
LambdaToSKI
37+
MicroHsExp
3738
Parser
3839
TermReducer
3940
other-modules:

src/MicroHsExp.hs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
module MicroHsExp where
3+
4+
import CLTerm
5+
import MicroHs.Exp
6+
import MicroHs.Expr
7+
import MicroHs.ExpPrint (toStringCMdl)
8+
import MicroHs.Desugar (LDef)
9+
import MicroHs.Ident
10+
11+
{--
12+
13+
data Exp
14+
= Var Ident
15+
| App Exp Exp
16+
| Lam Ident Exp
17+
| Lit Lit
18+
deriving (Eq)
19+
20+
type LDef = (Ident, Exp)
21+
22+
data SLoc = SLoc FilePath Line Col
23+
type Line = Int
24+
type Col = Int
25+
26+
data Ident = Ident SLoc Text
27+
28+
29+
-- The argument is all definitions and the main expression.
30+
-- The result is the number of definitions, all foreign export identifiers, and the program as a string.
31+
toStringCMdl :: ([LDef], Exp) -> (Int, [Ident], String)
32+
33+
34+
--}
35+
36+
example :: Exp
37+
example = Lit (LInt 42)
38+
--Var sampleIdent
39+
--`App` Lam sampleIdent (Var sampleIdent)
40+
--`App` Lit (LInt 42)
41+
42+
sampleIdent :: Ident
43+
sampleIdent = mkIdent "sample"
44+
45+
samplePrg :: ([LDef], Exp)
46+
samplePrg = ([(sampleIdent, example)], example)
47+
48+
addExp :: Exp
49+
addExp = App (Lit (LPrim "IO.print")) (App (App (Lit (LPrim "+")) (Lit (LInt 42))) (Lit (LInt 23)))
50+
51+
addPrg :: ([LDef], Exp)
52+
addPrg = ([], addExp)
53+
54+
55+
toMhsExp :: CL -> Exp
56+
toMhsExp (Com c) = Lit (LPrim (show c))
57+
toMhsExp (INT i) = Lit (LInt (fromIntegral i))
58+
toMhsExp (t :@ u) = App (toMhsExp t) (toMhsExp u)
59+
60+
toMhsPrg :: CL -> String
61+
toMhsPrg cl = let (n, exps, prg) = toStringCMdl ([], toMhsExp cl)
62+
in prg
63+
64+
toIon :: CL -> String
65+
toIon (Com c) = show c
66+
toIon (INT i) = "(" ++ show i ++ ")"
67+
toIon (t :@ u) = "`" ++ toIon t ++ toIon u
68+
69+
test :: CL
70+
test = Com B :@ Com S :@ (Com B :@ Com B)
71+
72+
main :: IO ()
73+
main = do
74+
75+
let (n, exps, prg) = toStringCMdl addPrg
76+
putStrLn $ "Number of definitions: " ++ show n
77+
putStrLn $ "Foreign exports: " ++ show exps
78+
putStrLn $ "Program: " ++ prg
79+
80+
-- let (n, exps, prg) = toStringCMdl samplePrg
81+
-- putStrLn $ "Number of definitions: " ++ show n
82+
-- putStrLn $ "Foreign exports: " ++ show exps
83+
-- putStrLn $ "Program: " ++ prg
84+
85+
-- let mhsExp = toMhsExp test
86+
-- putStrLn $ "MicroHs expression: " ++ show mhsExp
87+
88+
-- putStrLn $ "Test expression: " ++ (show $ toStringCMdl ([], mhsExp))

stack.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ packages:
3737
#
3838
extra-deps:
3939
#- MicroHs-0.10.5.0
40-
- git: https://github.com/thma/MicroHs
41-
commit: a0426a5bc5e8461bd7937dd2ae1db9dea98724c2
40+
- git: https://github.com/augustss/MicroHs.git
41+
commit: 38cbb10b8f1bd5bee13d9afb2b15239d8c897000
4242

4343

4444
# - acme-missiles-0.3

0 commit comments

Comments
 (0)