Skip to content

Commit 7c91e47

Browse files
committed
is a termreducer really worse than graph-reduction?
1 parent 8189214 commit 7c91e47

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

app/Main.hs

+9-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import System.TimeIt
1818
import Text.RawString.QQ
1919
import qualified Data.Bifunctor
2020
import LambdaToSKI (compileBracket)
21+
import TermReducer
2122

2223

2324
printGraph :: ST s (STRef s (Graph s)) -> ST s String
@@ -36,13 +37,16 @@ main = do
3637
hSetEncoding stdout utf8 -- this is required to handle UTF-8 characters like λ
3738

3839
--let testSource = "main = (\\x y -> + x x) 3 4"
39-
mapM_ showCompilations [prod, factorial, fibonacci, ackermann, tak]
40+
mapM_ showCompilations [factorial, fibonacci, ackermann, tak]
4041
--demo
4142

4243
type SourceCode = String
4344

4445
prod :: SourceCode
45-
prod = "main = λx y. * x y"
46+
prod = [r|
47+
mult = λx y. * y x
48+
main = mult 3 (+ 5 7)
49+
|]
4650

4751
tak :: SourceCode
4852
tak = [r|
@@ -110,6 +114,9 @@ showCompilations source = do
110114
print expr'
111115
printCS expr'
112116
putStrLn ""
117+
--putStr "reduced: "
118+
--x <- red expr'
119+
--print x
113120

114121
let expr'' = compileBulk env
115122
putStrLn "The main expression compiled to SICKBY combinator expressions with bulk combinators:"

lambda-ski.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ library
3434
Kiselyov
3535
LambdaToSKI
3636
Parser
37+
TermReducer
3738
other-modules:
3839
Paths_lambda_ski
3940
hs-source-dirs:

src/CLTerm.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module CLTerm
1313

1414
import Parser (Expr(..))
1515

16-
data CL = Com Combinator | INT Integer | CL :@ CL
16+
data CL = Com Combinator | INT Integer | CL :@ CL deriving (Eq)
1717

1818
instance Show CL where
1919
showsPrec :: Int -> CL -> ShowS

src/TermReducer.hs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module TermReducer where
2+
3+
import CLTerm
4+
5+
6+
-- data CL = Com Combinator | INT Integer | CL :@ CL
7+
8+
reduce :: CL -> IO CL
9+
reduce (Com c) = pure $ Com c
10+
reduce (INT i) = pure $ INT i
11+
reduce (Com I :@ t) = pure t
12+
reduce (Com K :@ t :@ _) = pure t
13+
reduce (Com S :@ t :@ u :@ v) = pure $ (t :@ v) :@ (u :@ v)
14+
reduce (Com B :@ f :@ g :@ x) = pure $ f :@ (g :@ x) -- B F G X = F (G X)
15+
reduce (Com C :@ t :@ u :@ v) = pure $ t :@ v :@ u
16+
reduce (Com Y :@ t) = pure $ t :@ (Com Y :@ t)
17+
reduce (Com P :@ t :@ u) = pure $ Com P :@ t :@ u
18+
reduce (Com R :@ t :@ u) = pure $ Com R :@ t :@ u
19+
reduce (Com ADD :@ INT i :@ INT j) = pure $ INT (i + j)
20+
reduce (Com ADD :@ i :@ j) = do ri <- red i; rj <- red j; reduce (Com ADD :@ ri :@ rj)
21+
reduce (Com SUB :@ INT i :@ INT j) = pure $ INT (i - j)
22+
reduce (Com SUB :@ i :@ j) = do ri <- red i; rj <- red j; reduce (Com SUB :@ ri :@ rj)
23+
reduce (Com MUL :@ INT i :@ INT j) = pure $ INT (i * j)
24+
reduce (Com MUL :@ i :@ j) = do ri <- red i; rj <- red j; reduce (Com MUL :@ ri :@ rj)
25+
reduce (Com DIV :@ INT i :@ INT j) = pure $ INT (i `div` j)
26+
reduce (Com DIV :@ i :@ j) = do ri <- red i; rj <- red j; reduce (Com DIV :@ ri :@ rj)
27+
reduce (Com REM :@ INT i :@ INT j) = pure $ INT (i `rem` j)
28+
reduce (Com REM :@ i :@ j) = do ri <- red i; rj <- red j; reduce (Com REM :@ ri :@ rj)
29+
reduce (Com SUB1 :@ INT i) = pure $ INT (i - 1)
30+
reduce (Com SUB1 :@ i) = do ri <- red i; reduce (Com SUB1 :@ ri)
31+
reduce (Com EQL :@ INT i :@ INT j) = if i == j then pure $ INT 1 else pure $ INT 0
32+
reduce (Com EQL :@ i :@ j) = do ri <- red i; rj <- red j; reduce (Com EQL :@ ri :@ rj)
33+
reduce (Com GEQ :@ INT i :@ INT j) = if i >= j then pure $ INT 1 else pure $ INT 0
34+
reduce (Com GEQ :@ i :@ j) = do ri <- red i; rj <- red j; reduce (Com GEQ :@ ri :@ rj)
35+
reduce (Com ZEROP :@ INT i) = if i == 0 then pure $ INT 1 else pure $ INT 0
36+
reduce (Com ZEROP :@ i) = do ri <- red i; reduce (Com ZEROP :@ ri)
37+
reduce (Com IF :@ (INT t) :@ u :@ v) = if t == 1 then red u else red v
38+
reduce (Com IF :@ t :@ u :@ v) = do rt <- red t; if rt == INT 1 then red u else red v
39+
reduce (Com B' :@ t :@ u :@ v) = pure $ t :@ (u :@ v)
40+
reduce (Com C' :@ t :@ u :@ v) = pure $ t :@ v :@ u
41+
reduce (Com S' :@ t :@ u :@ v) = pure $ (t :@ v) :@ (u :@ v)
42+
reduce (Com T :@ t) = reduce t
43+
reduce (t :@ u) = do rt <- red t; ru <- red u; reduce $ rt :@ ru
44+
45+
red :: CL -> IO CL
46+
red x@(INT i) = do print x; pure x
47+
red x@(Com c) = do print x; pure x
48+
red x = do print x; red =<< reduce x

0 commit comments

Comments
 (0)