🚀 Functions
This commit is contained in:
@ -7,28 +7,28 @@ import Test.Tasty.HUnit (testCase, (@?=))
|
||||
|
||||
-- -- Consider this example when you have added the necessary constructors.
|
||||
-- -- The Y combinator in a form suitable for strict evaluation.
|
||||
-- yComb :: Exp
|
||||
-- yComb =
|
||||
-- Lambda "f" $
|
||||
-- Apply
|
||||
-- (Lambda "g" (Apply (Var "g") (Var "g")))
|
||||
-- ( Lambda
|
||||
-- "g"
|
||||
-- ( Apply
|
||||
-- (Var "f")
|
||||
-- (Lambda "a" (Apply (Apply (Var "g") (Var "g")) (Var "a")))
|
||||
-- )
|
||||
-- )
|
||||
yComb :: Exp
|
||||
yComb =
|
||||
Lambda "f" $
|
||||
Apply
|
||||
(Lambda "g" (Apply (Var "g") (Var "g")))
|
||||
( Lambda
|
||||
"g"
|
||||
( Apply
|
||||
(Var "f")
|
||||
(Lambda "a" (Apply (Apply (Var "g") (Var "g")) (Var "a")))
|
||||
)
|
||||
)
|
||||
|
||||
-- fact :: Exp
|
||||
-- fact =
|
||||
-- Apply yComb $
|
||||
-- Lambda "rec" $
|
||||
-- Lambda "n" $
|
||||
-- If
|
||||
-- (Eql (Var "n") (CstInt 0))
|
||||
-- (CstInt 1)
|
||||
-- (Mul (Var "n") (Apply (Var "rec") (Sub (Var "n") (CstInt 1))))
|
||||
fact :: Exp
|
||||
fact =
|
||||
Apply yComb $
|
||||
Lambda "rec" $
|
||||
Lambda "n" $
|
||||
If
|
||||
(Eql (Var "n") (CstInt 0))
|
||||
(CstInt 1)
|
||||
(Mul (Var "n") (Apply (Var "rec") (Sub (Var "n") (CstInt 1))))
|
||||
|
||||
tests :: TestTree
|
||||
tests =
|
||||
@ -90,7 +90,17 @@ tests =
|
||||
(Add (CstInt 2) (CstInt 3))
|
||||
(Let "x" (CstBool True) (Var "x"))
|
||||
)
|
||||
@?= Right (ValBool True)
|
||||
--
|
||||
-- TODO - add more
|
||||
@?= Right (ValBool True),
|
||||
testCase "Lambda" $
|
||||
eval
|
||||
envEmpty
|
||||
(
|
||||
Lambda "y" (Var "y")
|
||||
)
|
||||
@?= Right (ValFun [] "y" (Var "y")),
|
||||
testCase "Apply" $
|
||||
eval [] (Apply (Let "x" (CstInt 2) (Lambda "y" (Add (Var "x") (Var "y")))) (CstInt 3))
|
||||
@?= Right (ValInt 5),
|
||||
testCase "Apply (fact)" $
|
||||
eval envEmpty (Apply fact (CstInt 5)) @?= Right (ValInt 120)
|
||||
]
|
||||
|
Reference in New Issue
Block a user