Fakultas Ilmu Komputer UI

Commit 435251b3 authored by Sean Gillespie's avatar Sean Gillespie
Browse files

Refactor based on static analysis

parent 6cc049ac
......@@ -5,8 +5,7 @@ import Test.Hspec
import Language.Lambda.HspecUtils
spec :: Spec
spec = do
describe "Bool" $ do
spec = describe "Bool" $ do
-- Bool is the definition of Booleans. We represent bools
-- using Church Encodings:
--
......@@ -23,22 +22,22 @@ spec = do
--
-- and is defined by
-- and = \x y. x y x
it "true and true = true" $ do
it "true and true = true" $
"(\\x y. x y x) (\\t f. t) (\\t f. t)" `shouldEvalTo` "\\t f. t"
it "true and false = false" $ do
it "true and false = false" $
"(\\x y. x y x) (\\t f. t) (\\t f. f)" `shouldEvalTo` "\\t f. f"
it "false and true = false" $ do
it "false and true = false" $
"(\\x y. x y x) (\\t f. f) (\\t f. t)" `shouldEvalTo` "\\t f. f"
it "false and false = false" $ do
it "false and false = false" $
"(\\x y. x y x) (\\t f. f) (\\t f. f)" `shouldEvalTo` "\\t f. f"
it "false and p = false" $ do
it "false and p = false" $
"(\\x y. x y x) (\\t f. f) p" `shouldEvalTo` "\\t f. f"
it "true and p = false" $ do
it "true and p = false" $
"(\\x y. x y x) (\\t f. t) p" `shouldEvalTo` "p"
describe "or" $ do
......@@ -51,22 +50,22 @@ spec = do
--
-- or is defined by
-- or = \x y. x x y
it "true or true = true" $ do
it "true or true = true" $
"(\\x y. x x y) (\\t f. t) (\\t f. t)" `shouldEvalTo` "\\t f. t"
it "true or false = true" $ do
it "true or false = true" $
"(\\x y. x x y) (\\t f. t) (\\t f. f)" `shouldEvalTo` "\\t f. t"
it "false or true = true" $ do
it "false or true = true" $
"(\\x y. x x y) (\\t f. f) (\\t f. t)" `shouldEvalTo` "\\t f. t"
it "false or false = false" $ do
it "false or false = false" $
"(\\x y. x x y) (\\t f. f) (\\t f. f)" `shouldEvalTo` "\\t f. f"
it "true or p = true" $ do
it "true or p = true" $
"(\\x y. x x y) (\\t f. t) p" `shouldEvalTo` "\\t f. t"
it "false or p = p" $ do
it "false or p = p" $
"(\\x y. x x y) (\\t f. f) p" `shouldEvalTo` "p"
......@@ -78,10 +77,10 @@ spec = do
--
-- not is defined by
-- not = \x. x (\t f. f) (\t f. t)
it "not true = false" $ do
it "not true = false" $
"(\\x. x (\\t f. f) (\\t f. t)) \\t f. t" `shouldEvalTo` "\\t f. f"
it "not false = true"$ do
it "not false = true" $
"(\\x. x (\\t f. f) (\\t f. t)) \\t f. f" `shouldEvalTo` "\\t f. t"
describe "if" $ do
......@@ -94,16 +93,16 @@ spec = do
--
-- if is defined by
-- if = \p x y. p x y
it "if true 0 1 = 0" $ do
it "if true 0 1 = 0" $
"(\\p x y. p x y) (\\t f. t) (\\f x. x) (\\f x. f x)"
`shouldEvalTo` "\\f x. x"
it "if false 0 1 = 1" $ do
it "if false 0 1 = 1" $
"(\\p x y. p x y) (\\t f. f) (\\f x. x) (\\f x. f x)"
`shouldEvalTo` "\\f x. f x"
it "it true p q = p" $ do
it "it true p q = p" $
"(\\p x y. p x y) (\\t f. t) p q" `shouldEvalTo` "p"
it "it false p q = q" $ do
it "it false p q = q" $
"(\\p x y. p x y) (\\t f. f) p q" `shouldEvalTo` "q"
......@@ -5,8 +5,7 @@ import Test.Hspec
import Language.Lambda.HspecUtils
spec :: Spec
spec = do
describe "Nat" $ do
spec = describe "Nat" $ do
-- Nat is the definition of natural numbers. More precisely, Nat
-- is the set of nonnegative integers. We represent nats using
-- Church Encodings:
......@@ -24,10 +23,10 @@ spec = do
--
-- successor is defined by
-- succ = \n f x. f (n f x)
it "succ 0 = 1" $ do
it "succ 0 = 1" $
"(\\n f x. f (n f x)) (\\f x. x)" `shouldEvalTo` "\\f x. f x"
it "succ 1 = 2" $ do
it "succ 1 = 2" $
"(\\n f x. f (n f x)) (\\f x. f x)" `shouldEvalTo` "\\f x. f (f x)"
describe "add" $ do
......@@ -35,17 +34,17 @@ spec = do
--
-- It is defined by applying successor m times on n:
-- add = \m n f x. m f (n f x)
it "add 0 2 = 2" $ do
it "add 0 2 = 2" $
"(\\m n f x. m f (n f x)) (\\f x. x) (\\f x. f (f x))"
`shouldEvalTo` "\\f x. f (f x)"
it "add 3 2 = 5" $ do
it "add 3 2 = 5" $
"(\\m n f x. m f (n f x)) (\\f x. f (f (f x))) (\\f x. f (f x))"
`shouldEvalTo` "\\f x. f (f (f (f (f x))))"
-- Here, we use `\f x. n f x` instead of `n`. This is because
-- I haven't implemented eta conversion
it "add 0 n = n" $ do
it "add 0 n = n" $
"(\\m n f x. m f (n f x)) (\\f x. x) n"
`shouldEvalTo` "\\f x. n f x"
......@@ -57,19 +56,19 @@ spec = do
--
-- Using eta conversion, we can omit the parameter x
-- multiply = \m n f. m (n f)
it "multiply 0 2 = 0" $ do
it "multiply 0 2 = 0" $
"(\\m n f. m (n f)) (\\f x. x) (\\f x. f (f x))"
`shouldEvalTo` "\\f x. x"
it "multiply 2 3 = 6" $ do
it "multiply 2 3 = 6" $
"(\\m n f. m (n f)) (\\f x. f (f x)) (\\f x. f (f (f x)))"
`shouldEvalTo` "\\f x. f (f (f (f (f (f x)))))"
it "multiply 0 n = 0" $ do
it "multiply 0 n = 0" $
"(\\m n f. m (n f)) (\\f x. x) n"
`shouldEvalTo` "\\f x. x"
it "multiply 1 n = n" $ do
it "multiply 1 n = n" $
"(\\m n f. m (n f)) (\\f x. f x) n"
`shouldEvalTo` "\\f x. n f x"
......@@ -86,18 +85,18 @@ spec = do
-- NOTE: Here we use the first form to get more predictable
-- variable names. Otherwise, alpha conversion will choose a random
-- unique variable.
it "power 0 1 = 0" $ do
it "power 0 1 = 0" $
"(\\m n f x. (n m) f x) (\\f x. x) (\\f x. f x)"
`shouldEvalTo` "\\f x. x"
it "power 2 3 = 8" $ do
it "power 2 3 = 8" $
"(\\m n f x. (n m) f x) (\\f x. f (f x)) (\\f x. f (f (f x)))"
`shouldEvalTo` "\\f x. f (f (f (f (f (f (f (f x)))))))"
it "power n 0 = 1" $ do
it "power n 0 = 1" $
"(\\m n f x. (n m) f x) n (\\f x. x)"
`shouldEvalTo` "\\f x. f x"
it "power n 1 = n" $ do
it "power n 1 = n" $
"(\\m n f x. (n m) f x) n (\\f x. f x)"
`shouldEvalTo` "\\f x. n f x"
......@@ -5,8 +5,7 @@ import Language.Lambda.HspecUtils
import Test.Hspec
spec :: Spec
spec = do
describe "Pair" $ do
spec = describe "Pair" $ do
-- Pair is the definition of tuples with two items. Pairs,
-- again are represented using Church Encodings:
--
......@@ -17,11 +16,11 @@ spec = do
--
-- first is defined by
-- first = \p. p (\t f. t)
it "first 0 1 = 0" $ do
it "first 0 1 = 0" $
"(\\p. p (\\t f. t)) ((\\x y f. f x y) (\\f x. x) (\\f x. f x))"
`shouldEvalTo` "\\f x. x"
it "first x y = x" $ do
it "first x y = x" $
"(\\p. p (\\t f. t)) ((\\x y f. f x y) x y)" `shouldEvalTo` "x"
describe "second" $ do
......@@ -30,7 +29,7 @@ spec = do
--
-- second is defined by
-- second = \p. p (\t f. f)
it "second 0 1 = 1" $ do
it "second 0 1 = 1" $
"(\\p. p (\\t f. f)) ((\\x y f. f x y) (\\f x. x) (\\f x. f x))"
`shouldEvalTo` "\\f x. f x"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment