Skip to content

Commit 1c648cd

Browse files
author
Thomas Mahler
committed
use latest version of MicroHs
1 parent 938d209 commit 1c648cd

File tree

12 files changed

+444
-396
lines changed

12 files changed

+444
-396
lines changed

Example.hs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
module Example(fac, main) where
1+
module Example where
22

3-
fac :: Int -> Int
4-
fac 0 = 1
5-
fac n = n * fac(n - 1)
3+
--fix :: (Int -> Int) -> Int
4+
fix f = f $ fix f
65

7-
main :: IO ()
8-
main = do
9-
putStrLn "computing some factorials"
10-
print $ map fac [0..10]
6+
fib :: Int -> Int
7+
fib = fix (\f n -> if n <= 2 then 1 else f (n-1) + f (n - 2))
8+
9+
main = print (fib 10)

app/Main.hs

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@ import LambdaToSKI (compileBracket)
2222
import TermReducer
2323
import IonAssembly (toIon)
2424
import MicroHsExp (toMhsPrg)
25-
import MicroHs.MhsEval (withMhsContext, eval, run)
26-
import MicroHs.Main (main)
25+
import MhsEval (withMhsContext, eval, run)
26+
import qualified MicroHs.Main as MHS (main)
27+
import System.Process (readProcess)
2728

2829

30+
31+
microHsevalTest :: CL -> IO String
32+
microHsevalTest expr = do
33+
let prg = toMhsPrg expr
34+
readProcess "mhseval" [] prg
35+
2936
printGraph :: ST s (STRef s (Graph s)) -> ST s String
3037
printGraph graph = do
3138
gP <- graph
@@ -38,26 +45,37 @@ reduceGraph graph = do
3845

3946
main :: IO ()
4047
main = do
41-
hSetEncoding stdin utf8 -- this is required to handle UTF-8 characters like λ
42-
hSetEncoding stdout utf8 -- this is required to handle UTF-8 characters like λ
43-
let source = ackermann
48+
--hSetEncoding stdin utf8 -- this is required to handle UTF-8 characters like λ
49+
--hSetEncoding stdout utf8 -- this is required to handle UTF-8 characters like λ
50+
let source = factorial
4451
let env = parseEnvironment source
4552
let expr' = compileEta env
53+
putStrLn $ "Factorial compiled to combinator expression:\n" ++ show expr'
54+
4655
let prg = toMhsPrg expr'
47-
48-
-- use MicroHs to compile AND execute the Example.hs program
49-
withArgs ["-r", "Example"] MicroHs.Main.main
50-
--prg <- readFile "out.comb"
56+
putStrLn $ "The resulting MicroHs program: \n" ++ prg
57+
58+
-- use microHs to compile 'Example.hs' to 'out.comb'
59+
withArgs ["Example.hs"] MHS.main
60+
-- read the program 'out.comb' into a string
61+
prg' <- readFile "out.comb"
62+
-- use the MicroHs runtime to execute the program
63+
withMhsContext $ \ctx ->
64+
run ctx prg'
65+
66+
-- -- use MicroHs to compile AND execute the Example.hs program
67+
withArgs ["-r", "Example.hs"] MHS.main
68+
-- --prg <- readFile "out.comb"
5169

5270
result <- withMhsContext $ \ctx ->
53-
eval ctx prg
71+
eval ctx prg
5472
putStrLn $ "Result: " ++ result
55-
return ()
73+
-- return ()
5674

57-
combCode <- readFile "out.comb"
58-
withMhsContext $ \ctx -> do
59-
putStrLn "Running the compiled program:"
60-
run ctx combCode
75+
-- combCode <- readFile "out.comb"
76+
-- withMhsContext $ \ctx -> do
77+
-- putStrLn "Running the compiled program:"
78+
-- run ctx combCode
6179

6280

6381
--let testSource = "main = (\\x y -> + x x) 3 4"
@@ -66,6 +84,12 @@ main = do
6684

6785
type SourceCode = String
6886

87+
ex :: SourceCode
88+
ex = [r|
89+
90+
main = if (eql 0 1) 23 42
91+
|]
92+
6993
prod :: SourceCode
7094
prod = [r|
7195
mult = λx y. * y x
@@ -86,7 +110,7 @@ ackermann = [r|
86110

87111
factorial :: SourceCode
88112
factorial = [r|
89-
fact = y(λf n. if (eql n 0) 1 (* n (f (- n 1))))
113+
fact = y(\f n. if (eql n 0) 1 (* n (f (- n 1))))
90114
main = fact 10
91115
|]
92116

benchmark/BenchmarkSources.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ type SourceCode = String
88
ackermann :: SourceCode
99
ackermann = [r|
1010
expected = 7
11-
ack = y(λf n m. if (is0 n) (+ m 1) (if (is0 m) (f (sub1 n) 1) (f (sub1 n) (f n (sub1 m)))))
12-
main = ack 2 2
11+
ack = y(λf n m. if (eql n 0) (+ m 1) (if (eql m 0) (f (- n 1) 1) (f (- n 1) (f n (- m 1)))))
12+
main = ack 3 9
1313
|]
1414

1515
factorial :: SourceCode
@@ -22,8 +22,8 @@ factorial = [r|
2222
fibonacci :: SourceCode
2323
fibonacci = [r|
2424
expected = 89
25-
fib = y(λf n. if (is0 n) 1 (if (eql n 1) 1 (+ (f (sub1 n)) (f (sub n 2)))))
26-
main = fib 10
25+
fib = y(λf n. if (leq n 1) 1 (+ (f (- n 1)) (f (- n 2))))
26+
main = fib 37
2727
|]
2828

2929
gaussian :: SourceCode

benchmark/ReductionBenchmarks.hs

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import HhiReducer
1313
import Control.Monad.Fix ( fix )
1414
import BenchmarkSources
1515
import MicroHsExp ( toMhsPrg )
16-
import MicroHs.MhsEval
16+
import MhsEval
1717

1818
loadTestCase :: SourceCode -> IO CL
1919
loadTestCase src = do
@@ -69,12 +69,10 @@ reducerTest expr = show $ transLink primitives expr
6969
reducerTestLog :: CL -> String
7070
reducerTestLog expr = show $ transLinkLog primitives expr
7171

72-
microHsTest :: MhsContext -> CL -> IO String
73-
microHsTest ctx expr = do
74-
let prg = toMhsPrg expr
75-
result <- eval ctx prg
76-
return result
72+
microHsTest :: MhsContext -> String -> IO ()
73+
microHsTest ctx prg = run ctx prg
7774

75+
7876
benchmarks :: IO ()
7977
benchmarks = do
8078
fac <- loadTestCase factorial
@@ -104,6 +102,15 @@ benchmarks = do
104102
takBulkLog <- loadTestCaseBreakBulkLog BenchmarkSources.tak
105103

106104
mhsContext <- createMhsContext
105+
let mhsFacEta = toMhsPrg facEta
106+
mhsTakEta = toMhsPrg takEta
107+
mhsFibEta = toMhsPrg fibEta
108+
mhsAckEta = toMhsPrg akkEta
109+
110+
print facEta
111+
print takEta
112+
print fibEta
113+
print akkEta
107114

108115
-- sanity checks
109116
print $ graphTest fac
@@ -114,44 +121,46 @@ benchmarks = do
114121
print $ reducerTestLog facBulk
115122
print $ show $ fact 10
116123
do
117-
microHsResult <- microHsTest mhsContext facEta
124+
microHsResult <- microHsTest mhsContext mhsFacEta
118125
print microHsResult
119126

120127
defaultMain [
121-
bench "factorial Graph-Reduce" $ nf graphTest fac
122-
, bench "factorial Graph-Reduce-Eta" $ nf graphTest facEta
128+
-- bench "factorial Graph-Reduce" $ nf graphTest fac
129+
-- , bench "factorial Graph-Reduce-Eta" $ nf graphTest facEta
123130
-- , bench "factorial Graph-Reduce-Lin" $ nf graphTest facBulkLinear
124131
-- , bench "factorial Graph-Reduce-Log" $ nf graphTest facBulkLog
125132
-- , bench "factorial HHI-Reduce" $ nf reducerTest fac
126-
, bench "factorial HHI-Eta" $ nf reducerTest facEta
133+
bench "factorial HHI-Eta" $ nf reducerTest facEta
127134
-- , bench "factorial HHI-Bulk" $ nf reducerTest facBulk
128135
-- , bench "factorial HHI-Bulk-Log" $ nf reducerTestLog facBulk
129136
-- , bench "factorial HHI-Break-Bulk" $ nf reducerTest facBulkLinear
130137
-- , bench "factorial HHI-Break-Log" $ nf reducerTestLog facBulkLog
131-
, bench "factorial MicroHs" $ nfIO (microHsTest mhsContext facEta)
138+
, bench "factorial MicroHs" $ nfIO (microHsTest mhsContext mhsFacEta)
132139
, bench "factorial Native" $ nf fact 10
133-
-- , bench "fibonacci Graph-Reduce" $ nf graphTest fib
134-
-- , bench "fibonacci Graph-Reduce-Eta" $ nf graphTest fibEta
135-
-- , bench "fibonacci Graph-Reduce-Lin" $ nf graphTest fibBulkLinear
136-
-- , bench "fibonacci Graph-Reduce-Log" $ nf graphTest fibBulkLog
137-
-- , bench "fibonacci HHI-Reduce" $ nf reducerTest fib
138-
-- , bench "fibonacci HHI-Eta" $ nf reducerTest fibEta
140+
-- bench "fibonacci Graph-Reduce" $ nf graphTest fib
141+
--, bench "fibonacci Graph-Reduce-Eta" $ nf graphTest fibEta
142+
--, bench "fibonacci Graph-Reduce-Lin" $ nf graphTest fibBulkLinear
143+
--, bench "fibonacci Graph-Reduce-Log" $ nf graphTest fibBulkLog
144+
--, bench "fibonacci HHI-Reduce" $ nf reducerTest fib
145+
, bench "fibonacci HHI-Eta" $ nf reducerTest fibEta
139146
-- , bench "fibonacci HHi-Bulk" $ nf reducerTest fibBulk
140147
-- , bench "fibonacci HHI-Bulk-Log" $ nf reducerTestLog fibBulk
141148
-- , bench "fibonacci HHI-Break-Bulk" $ nf reducerTest fibBulkLinear
142149
-- , bench "fibonacci HHI-Break-Log" $ nf reducerTestLog fibBulkLog
143-
-- , bench "fibonacci Native" $ nf fibo 10
150+
, bench "fibonacci MicroHs" $ nfIO (microHsTest mhsContext mhsFibEta)
151+
, bench "fibonacci Native" $ nf fibo 37
144152
-- , bench "ackermann Graph-Reduce" $ nf graphTest akk
145-
-- , bench "ackermann Graph-Reduce-Eta" $ nf graphTest akkEta
153+
--, bench "ackermann Graph-Reduce-Eta" $ nf graphTest akkEta
146154
-- , bench "ackermann Graph-Reduce-Lin" $ nf graphTest akkBulkLinear
147155
-- , bench "ackermann Graph-Reduce-Log" $ nf graphTest akkBulkLog
148-
-- , bench "ackermann HHI-Reduce" $ nf reducerTest akk
156+
, bench "ackermann HHI-Reduce" $ nf reducerTest akkEta
149157
-- , bench "ackermann HHI-Eta" $ nf reducerTest akkEta
150158
-- , bench "ackermann HHI-Bulk" $ nf reducerTest akkBulk
151159
-- , bench "ackermann HHI-Bulk-Log" $ nf reducerTestLog akkBulk
152160
-- , bench "ackermann HHI-Break-Bulk" $ nf reducerTest akkBulkLinear
153161
-- , bench "ackermann HHI-Break-Log" $ nf reducerTestLog akkBulkLog
154-
-- , bench "ackermann Native" $ nf ack_2 2
162+
, bench "ackermann MicroHs" $ nfIO (microHsTest mhsContext mhsAckEta)
163+
, bench "ackermann Native" $ nf ack_3 9
155164
-- -- , bench "gaussian Graph-Reduce" $ nf graphTest gau
156165
-- -- , bench "gaussian Graph-Reduce-Eta" $ nf graphTest gauEta
157166
-- -- , bench "gaussian HHI-Reduce" $ nf reducerTest gau
@@ -160,7 +169,7 @@ benchmarks = do
160169
-- -- , bench "gaussian HHI-Bulk-Log" $ nf reducerTestLog gauBulk
161170
-- -- , bench "gaussian Native" $ nf gaussianSum 100
162171
-- , bench "tak Graph-Reduce" $ nf graphTest tak
163-
, bench "tak Graph-Reduce-Eta" $ nf graphTest takEta
172+
--, bench "tak Graph-Reduce-Eta" $ nf graphTest takEta
164173
-- , bench "tak Graph-Reduce-Lin" $ nf graphTest takBulkLinear
165174
-- , bench "tak Graph-Reduce-Log" $ nf graphTest takBulkLog
166175
-- , bench "tak HHI-Reduce" $ nf reducerTest tak
@@ -169,37 +178,42 @@ benchmarks = do
169178
-- , bench "tak HHI-Bulk-Log" $ nf reducerTestLog takBulk
170179
-- , bench "tak HHI-Break-Bulk" $ nf reducerTest takBulkLinear
171180
-- , bench "tak HHI-Break-Log" $ nf reducerTestLog takBulkLog
172-
, bench "tak MicroHs" $ nfIO (microHsTest mhsContext takEta)
173-
, bench "tak Native" $ nf tak1 (7,4,2)
181+
, bench "tak MicroHs" $ nfIO (run mhsContext mhsTakEta)
182+
, bench "tak Native" $ nf tak1 (18,6,3)
174183
]
184+
closeMhsContext mhsContext
185+
putStrLn "Benchmarks completed."
175186
return ()
176187

177188

178-
fact :: Integer -> Integer
189+
fact :: Int -> Int
179190
fact = fix (\f n -> if n == 0 then 1 else n * f (n-1))
180191

181-
fibo :: Integer -> Integer
182-
fibo = fix (\f n -> if n == 0 || n == 1 then 1 else f (n-1) + f (n - 2))
192+
fibo :: Int -> Int
193+
fibo = fix (\f n -> if n <= 1 then 1 else f (n-1) + f (n - 2))
183194

184-
ack_2 :: Integer -> Integer
195+
ack_2 :: Int -> Int
185196
ack_2 = ack 2
186197

187-
ack :: Integer -> Integer -> Integer
198+
ack_3 :: Int -> Int
199+
ack_3 = ack 3
200+
201+
ack :: Int -> Int -> Int
188202
ack = fix (\f n m ->
189203
if n == 0
190204
then m + 1
191205
else (if m == 0
192206
then f (n-1) 1
193207
else f (n-1) (f n (m-1))))
194208

195-
gaussianSum :: Integer -> Integer
209+
gaussianSum :: Int -> Int
196210
gaussianSum = fix (\f n -> if n == 0 then 0 else n + f (n-1))
197211

198212

199-
tak_18_6 :: Integer -> Integer
213+
tak_18_6 :: Int -> Int
200214
tak_18_6 = takN 18 6
201215

202-
takN :: Integer -> Integer -> Integer -> Integer
216+
takN :: Int -> Int -> Int -> Int
203217
takN = fix (\f x y z -> (if y >= x then z else f (f (x-1) y z) (f (y-1) z x) (f (z-1) x y )))
204218

205219
tak1 (x,y,z) = takN x y z

cabal.project

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
packages: .
2-
2+
--packages: ., ../MicroHs
33
source-repository-package
44
type: git
5-
location: https://github.com/thma/MicroHs.git
6-
tag: fca862b31e7cedbf7695028b6cbefa65c9def19b
5+
location: https://github.com/augustss/MicroHs.git
6+
tag: aa8d6e39cc9ce7f3a23f369c5b255c1b237376f3

lambda-ski.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ executable lambda-ski-exe
8686
, lambda-ski
8787
, mtl
8888
, parsec
89+
, process
8990
, raw-strings-qq
9091
, timeit
9192
, uniplate

0 commit comments

Comments
 (0)