-
Notifications
You must be signed in to change notification settings - Fork 0
/
Beta.hs
54 lines (42 loc) · 1.72 KB
/
Beta.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
{-
Module : Compute
Description : Proc for beta reduction.
Maintainer : [email protected]
Stability : experimental
This module contains the code used for
computing on lambda terms.
Of course, computations may never normalise.
User discretion is advised. For example:
ghci> compute (App omega omega)
You'll be waiting awhile!
(!) At this stage there is no care taken
to avoid variable clashes. Next step
is to implement alpha reduction so
as to ensure no variables clash prior
to beginning computation.
-}
module Beta where
import Lambda
import Church
substitute :: LambdaTerm -> String -> LambdaTerm -> LambdaTerm
substitute (Var x) v t = if (x == v)
then t
else (Var x)
substitute (App x y) v t = App (substitute x v t)
(substitute y v t)
substitute (Abs x b) v t = Abs x (substitute b v t)
betaReduction :: LambdaTerm -> LambdaTerm
betaReduction (App (Abs x b) t) = substitute b x t
betaReduction (Var x) = Var x
betaReduction (App x y) = App (betaReduction x)
(betaReduction y)
betaReduction (Abs x b) = Abs x (betaReduction b)
normalized :: LambdaTerm -> Bool
normalized (App (Abs x b) t) = False
normalized (Var x) = True
normalized (App x y) = (normalized x) && (normalized y)
normalized (Abs x b) = (normalized b)
compute :: LambdaTerm -> LambdaTerm
compute t
| normalized t = t
| otherwise = compute (betaReduction t)