-
Notifications
You must be signed in to change notification settings - Fork 4
/
LambdaCalculus.hs
55 lines (47 loc) · 1.32 KB
/
LambdaCalculus.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 LambdaCalculus where
-- Lambda calculus terms with explicit names
type NamedTermVariable = String
data NamedTerm
= NVar NamedTermVariable
| NApp NamedTerm NamedTerm
| NLam NamedTermVariable NamedTerm
| NNum Integer
deriving (Show, Eq)
-- Examples
-- * \x -> x
nIdentity = NLam "x" (NVar "x")
-- * (\x -> x) 3
nExample = NApp nIdentity (NNum 3)
-- Lambda calculus using de Bruijn indices
type DeBruijnIndex = Integer
data DeBruijnTerm
= BVar DeBruijnIndex
| BApp DeBruijnTerm DeBruijnTerm
| BLam DeBruijnTerm
| BNum Integer
deriving (Show, Eq)
-- Examples
-- * \x -> x
bIdentity = BLam (BVar 0)
-- * (\x -> x) 3
bExample = BApp bIdentity (BNum 3)
-- For each representation of lambda calculus,
-- where "Var" and "Term" refer to the concrete types:
-- * write an alpha-equivalence function
-- (\x. x) alpha-equiv to (\y. y) and so on
-- (\x y. x) is not alpha-equiv to (\x y. y)
--
-- alphaEquiv :: Term -> Term -> Bool
-- * write a substitution function
--
-- subst :: (Var, Term) -> Term -> Term
--
-- note: to ensure that variables are not accidentally captured
-- you have to implement a function to "freshen" all the
-- variables from lambdas
--
-- freshen :: Term -> Term
-- * write an evaluator which implements the beta-reduction rule
-- (\x. e) v --> subst x by v in e
--
-- eval :: Term -> Term