1 {-# LANGUAGE PatternSynonyms #-}
5 -- Copyright : Tomáš Musil 2014
8 -- Maintainer : tomik.musil@gmail.com
9 -- Stability : experimental
11 -- This is a toy λ-calculus implementation.
25 import Control.Monad.State
30 import Lambda.Parser.Fancy ()
33 -- >>> import Control.Applicative
34 -- >>> import Lambda.Parser.Fancy
35 -- >>> import Test.Term
36 -- >>> import Test.QuickCheck
39 varnames = map (:[]) ['a'..'z'] ++ [c : s | s <- varnames, c <- ['a'..'z']]
41 alphaNorm :: Term -> Term
42 alphaNorm = alpha varnames
44 alpha (v:vs) (Lambda x r) = Lambda v . alpha vs $ substitute x (Var v) r
45 alpha vs (App u v) = App (alpha vs u) (alpha vs v)
46 alpha _ (Var x) = Var x
47 alpha [] _ = undefined
49 isFreeIn :: VarName -> Term -> Bool
50 isFreeIn x (Var v) = x == v
51 isFreeIn x (App t u) = x `isFreeIn` t || x `isFreeIn` u
52 isFreeIn x (Lambda v t) = x /= v && x `isFreeIn` t
54 rename :: Term -> Term
55 rename (Lambda x t) = Lambda n (substitute x (Var n) t)
57 rnm v = if (v ++ "r") `isFreeIn` t then rnm (v ++ "r") else v ++ "r"
58 rename _ = error "TODO vymyslet reprezentaci, kde pujde udelat fce, ktera bere jen Lambdy"
60 substitute :: VarName -> Term -> Term -> Term
61 substitute a b (Var x) = if x == a then b else Var x
62 substitute a b (Lambda x t)
64 | x `isFreeIn` b = substitute a b $ rename (Lambda x t)
65 | otherwise = Lambda x (substitute a b t)
66 substitute a b (App t u) = App (substitute a b t) (substitute a b u)
70 -- >>> reduce $ tRead "(\\x.x x) (g f)"
73 reduce :: Term -> Term
74 reduce (Var x) = Var x
75 reduce (Lambda x t) = Lambda x (reduce t)
76 reduce (App t u) = app (reduce t) u
77 where app (Lambda x v) w = reduce $ substitute x w v
78 app a b = App a (reduce b)
80 data Strategy = Eager | Lazy
82 reduceStep :: Term -> Term
83 reduceStep (RedEx x s t) = substitute x t s
86 data Z = R Term Z | L Z Term | ZL VarName Z | E
88 type TermZipper = (Term, Z, D)
90 move :: TermZipper -> TermZipper
91 move (App l r, c, Down) = (l, L c r, Down)
92 move (Lambda x t, c, Down) = (t, ZL x c, Down)
93 move (Var x, c, Down) = (Var x, c, Up)
94 move (t, L c r, Up) = (r, R t c, Down)
95 move (t, R l c, Up) = (App l t, c, Up)
96 move (t, ZL x c, Up) = (Lambda x t, c, Up)
97 move (t, E, Up) = (t, E, Up)
99 unmove :: TermZipper -> TermZipper
100 unmove (t, L c r, Down) = (App t r, c, Down)
103 -- getTerm :: TermZipper -> Term
105 travPost :: (Monad m) => (Term -> m Term) -> Term -> m Term
106 travPost fnc term = tr fnc (term, E, Down)
108 tr f (t@RedEx{}, c, Up) = do
111 tr _ (t, E, Up) = return t
112 tr f (t, c, Up) = tr f $ move (t, c, Up)
113 tr f (t, c, Down) = tr f $ move (t, c, Down)
115 travPre :: (Monad m) => (Term -> m Term) -> Term -> m Term
116 travPre fnc term = tr fnc (term, E, Down)
118 tr f (t@RedEx{}, c, Down) = do
120 tr f $ unmove (nt, c, Down)
121 tr _ (t, E, Up) = return t
122 tr f (t, c, Up) = tr f $ move (t, c, Up)
123 tr f (t, c, Down) = tr f $ move (t, c, Down)
127 -- >>> toNormalForm Eager 100 cI
130 -- >>> toNormalForm Eager 100 $ App cI cI
133 -- >>> toNormalForm Eager 100 $ (App (App cK cI) cY)
136 -- >>> toNormalForm Lazy 100 $ (App (App cK cI) cY)
139 -- prop> within 10000000 $ (\ t u -> t == u || t == Nothing || u == Nothing) (alphaNorm <$> toNormalForm Lazy 100 x) (alphaNorm <$> toNormalForm Eager 100 x)
141 -- inf = tRead "(\\d.a ((\\d c.c d c) (\\x y z.x z (y z)) (\\f.(\\x.f (x x)) (\\x.f (x x))) e))"
144 toNormalFormDebug :: Strategy -> Int -> Term -> Maybe Term
145 toNormalFormDebug Eager n = flip evalStateT 0 . travPost (prnt >=> cnt >=> short n >=> return . reduceStep)
146 toNormalFormDebug Lazy n = flip evalStateT 0 . travPre (prnt >=> cnt >=> short n >=> return . reduceStep)
148 toNormalForm :: Strategy -> Int -> Term -> Maybe Term
149 toNormalForm Eager n = flip evalStateT 0 . travPost (cnt >=> short n >=> return . reduceStep)
150 toNormalForm Lazy n = flip evalStateT 0 . travPre (cnt >=> short n >=> return . reduceStep)
152 prnt :: (Monad m) => Term -> StateT Int m Term
153 prnt t = traceShow t $ return t
155 cnt :: (Monad m) => Term -> StateT Int m Term
161 short :: Int -> Term -> StateT Int Maybe Term