-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerm.hs
More file actions
75 lines (56 loc) · 2.28 KB
/
Term.hs
File metadata and controls
75 lines (56 loc) · 2.28 KB
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
65
66
67
68
69
70
71
72
73
74
--- Named and nameless terms for the untyped lambda calculus as in TAPL,
--- Ch. 6
module Term where
import Data.List (elemIndex, sort, elem)
import Data.Maybe (fromJust)
--- Types for terms
-- TODO add boolean and numerical types/values; spread their evaluation
-- throughout the interpreter.
-- Nameless terms
type Index = Integer
data NamelessTerm = NLAbs NamelessTerm
| NLApp NamelessTerm NamelessTerm
| NLVar Index
deriving Eq
instance Show NamelessTerm where
show (NLAbs t) = "(\\. " ++ show t ++ ")"
show (NLApp t1 t2) = "(" ++ show t1 ++ " " ++ show t2 ++ ")"
show (NLVar i) = show i
-- Named terms
type VarName = String
data Term = Abs VarName Term
| App Term Term
| Var VarName
instance Show Term where
show (Abs var t) = "(\\" ++ var ++ ". " ++ show t ++ ")"
show (App t1 t2) = "(" ++ show t1 ++ " " ++ show t2 ++ ")"
show (Var v) = v
instance Eq Term where
t1 == t2 = (fst $ removeNames t1) == (fst $ removeNames t2)
-- Naming contexts. For convenience, our variable->index conversion is the
-- _OPPOSITE_ of that in TAPL; e.g., the variable with index zero is at the
-- _HEAD_ of the list.
newtype Context = Context [VarName] deriving Show
emptyContext = Context []
nameIndex :: VarName -> Context -> Index
nameIndex name (Context ctx) = fromIntegral $ fromJust $ elemIndex name ctx
pushName :: VarName -> Context -> Context
pushName name (Context ctx) = Context $ name:ctx
--- Operations on terms
freeVariables :: Term -> [VarName]
freeVariables t = helper t [] -- (i know i should be using sets)
where helper (Abs v b) bindings = helper b $ v:bindings
helper (App t1 t2) bindings =
(helper t1 bindings) ++ (helper t2 bindings)
helper (Var v) bindings = if v `elem` bindings then [] else [v]
canonicalContext :: Term -> Context
canonicalContext t = Context $ sort $ freeVariables t
removeNamesWithCtx t ctx =
case t of
(Abs v b) -> NLAbs $ removeNamesWithCtx b (pushName v ctx)
(App t1 t2) -> NLApp (removeNamesWithCtx t1 ctx) $
(removeNamesWithCtx t2 ctx)
(Var v) -> NLVar $ nameIndex v ctx
removeNames :: Term -> (NamelessTerm, Context)
removeNames t = (removeNamesWithCtx t ctx, ctx)
where ctx = canonicalContext t