-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlpha.hs
64 lines (53 loc) · 2.5 KB
/
Alpha.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
56
57
58
59
60
61
62
63
64
{-
Module : Alpha
Description : Implement alpha reduction.
Maintainer : [email protected]
Stability : experimental
This module implements alpha reduction.
Given a LambdaTerm to normalise, this module
first changes all bound variables to unique
integers. After computation, it returns the
bound variables to a more familiar set of
variables: x y z etc.
This does assume at most 10 bound variables
in the normalised expression.
-}
module Alpha where
import Lambda
-- Variable rename, towards alpha reduction.
renameVar :: LambdaTerm -> String -> String -> LambdaTerm
renameVar (App x y) old new = App (renameVar x old new) (renameVar y old new)
renameVar (Abs x b) old new = if (x == old)
then Abs new (renameVar b old new)
else Abs x (renameVar b old new)
renameVar (Var x) old new = if (x == old)
then Var new
else Var x
alphaHelper :: LambdaTerm -> [Int] -> LambdaTerm
alphaHelper (Abs x b) vars = Abs (show (head vars))
(alphaHelper (renameVar b x (show(head vars)))
(tail vars))
alphaHelper (App x y) vars = App (alphaHelper x evenVars)
(alphaHelper y oddVars)
where
evenVars = [x | (x, i) <- zip vars [0..], even i]
oddVars = [x | (x, i) <- zip vars [0..], odd i]
alphaHelper (Var x) vars = Var x
alphaReduction :: LambdaTerm -> LambdaTerm
alphaReduction t = alphaHelper t variables
where
variables = [1,2 ..]
unalphaHelper :: LambdaTerm -> [String] -> LambdaTerm
unalphaHelper (Abs x b) vars = Abs (head vars)
(unalphaHelper (renameVar b x (head vars))
(tail vars))
unalphaHelper (App x y) vars = App (unalphaHelper x evenVars)
(unalphaHelper y oddVars)
where
evenVars = [x | (x, i) <- zip vars [0..], even i]
oddVars = [x | (x, i) <- zip vars [0..], odd i]
unalphaHelper (Var x) vars = Var x
unAlphaReduction :: LambdaTerm -> LambdaTerm
unAlphaReduction t = unalphaHelper t prettyVars
where
prettyVars = ["x","y","z","u","v","p","q","r","a","b","c"]