Skip to content

Commit a54f863

Browse files
author
Thomas Mahler
committed
show that term reducer can handle explicit fix point combinator
1 parent ffc7470 commit a54f863

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

‎test/TermReducerSpec.hs‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ spec = do
3232
verify Simple.basicSubtraction
3333
it "computes basic multiplication" $
3434
verify Simple.basicMultiplication
35-
35+
3636
describe "TermReducer - Complex Recursive Tests" $ do
3737
it "computes simple constant" $
3838
verify simpleConstant
@@ -48,6 +48,8 @@ spec = do
4848
verify smallFactorial
4949
it "computes simple fib (recursive)" $
5050
verify originalSmallFib
51+
it "computes fac with explicit Y combinator" $
52+
verify fixFactorial
5153

5254
-- Very simple non-recursive tests for TermReducer
5355
simpleConstant :: String
@@ -104,7 +106,14 @@ smallFactorial :: String
104106
smallFactorial = [r|
105107
expected = 120
106108
fact = y(λf n. if (is0 n) 1 (* n (f (sub1 n))))
107-
--fact = \n. if (is0 n) (+ 2 21) (* 6 7)
109+
main = fact 5
110+
|]
111+
112+
fixFactorial :: String
113+
fixFactorial = [r|
114+
expected = 120
115+
fix = λf . (λx . x x)(λx . f(x x))
116+
fact = fix(λf n. if (is0 n) 1 (* n (f (sub1 n))))
108117
main = fact 5
109118
|]
110119

@@ -120,22 +129,20 @@ verify :: String -> IO ()
120129
verify src = do
121130
result <- runTest src
122131
result `shouldBe` True
123-
132+
124133
runTest :: String -> IO Bool
125134
runTest src = do
126135
let pEnv = parseEnvironment src
127136
aExp = compile pEnv abstractToSKI
128137
expected = toCL $ fromJust (lookup "expected" pEnv)
129-
138+
130139
-- Capture result with timeout handling
131-
result <- catch
140+
catch
132141
(let actual = reduce aExp
133-
in do
142+
in do
134143
putStrLn $ "Expected: " ++ show expected
135-
putStrLn $ "Actual: " ++ show actual
144+
putStrLn $ "Actual: " ++ show actual
136145
return $ show expected == show actual)
137-
(\e -> do
146+
(\e -> do
138147
putStrLn $ "Error during reduction: " ++ show (e :: SomeException)
139148
return False)
140-
141-
return result

0 commit comments

Comments
 (0)