-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLambda.hs
55 lines (42 loc) · 1.39 KB
/
Lambda.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{-
Module : Lambda
Description : Basic type definition for untyped lambda calculus.
Maintainer : [email protected]
Stability : experimental
This module defines a type for working with lambda expressions.
Some example combinators are provided.
LambdaTerm is also made into an instance of Show for printing.
-}
module Lambda where
data LambdaTerm =
Var String
| Abs String LambdaTerm
| App LambdaTerm LambdaTerm
deriving Eq
-- Example combinators.
omega = Abs "x" (App (Var "x") (Var "x"))
i :: LambdaTerm
i = Abs "x" (Var "x")
k :: LambdaTerm
k = Abs "u" (Abs "v" (Var "u"))
s :: LambdaTerm
s = Abs "a"
(Abs "b"
(Abs "c"
(App (App (Var "a") (Var "c"))
(App (Var "b") (Var "c")))))
-- SKI(KIS) -> I [Correct!]
testTerm :: LambdaTerm
testTerm = App (App (App s k) i)
(App (App k i) s)
-- SII (SII) -> SII [Correct! Does not normalise]
hangTerm :: LambdaTerm
hangTerm = App (App (App s i) i)
(App (App s i) i)
-- String representation.
printTerm :: LambdaTerm -> String
printTerm (Var x) = x
printTerm (Abs x y) = "(lam " ++ x ++ ". " ++ (printTerm y) ++ ")"
printTerm (App x y) = "(" ++ (printTerm x) ++ " " ++ (printTerm y) ++ ")"
instance Show LambdaTerm where
show = printTerm