-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplest.hs
More file actions
29 lines (23 loc) · 947 Bytes
/
Copy pathSimplest.hs
File metadata and controls
29 lines (23 loc) · 947 Bytes
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
module Simplest where
data Term = Var Int | Lambda Term | App Term Term
instance Show Term where
show (Var n) = "x" <> show n
show (Lambda p) = "λ." <> show p
show (App f arg) = "(" <> show f <> ") " <> show arg
-- subst variable id n, with term sub, in a term
subst' :: Int -> Term -> Term -> Term
subst' n sub (Var n') = if n == n' then sub else Var n'
subst' n sub (Lambda p) = Lambda $ subst' (n+1) sub p
subst' n sub (App f arg) = App (subst' n sub f) (subst' n sub arg)
-- Commonly used when wishing to erase a lambda
subst = subst' 0
reduce :: Term -> Term
reduce (Var n) = Var n
reduce (Lambda p) = Lambda (reduce p)
reduce (App f arg) = let rf = reduce f in case rf of
-- After substition, more reduction options may appear
Lambda p -> reduce $ subst arg p
-- The function argument could not be reduced
_ -> App rf (reduce arg)
test :: Term
test = Lambda (Lambda (Var 0)) `App` Var 1 `App` Var 2