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 Test.QuickCheck
31 -- >>> import Control.Applicative
32 -- >>> import Lambda.Parser.Fancy
33 -- >>> import Lambda.Term
34 -- >>> let cP = tRead "(λa d c.(λa.e) b (λc.d)) ((λa.(λd.a) (λd c.b ((λa.a) a)) (a ((λa.(λd.e) ((λe.(λd b.a) (λa c.(λa a d.(λd.b (λa d.c) e) (λb b.c a (a d (λb d d e a.d (λb b.d))))) ((λb.a) c)) (d ((λc.(λd.a (λe.e)) (c d)) ((λe.b) a))) c (λa.d (e (λe.(λd c.b) a))) (c (b a)) a (λe.(λa b e b a.d) b)) ((λe.b) (λa.b)) ((λe d.b) b) e) b) ((λc c.a e) (λb.(λb.e) a)))) (λe.e) b (λd c e e c a.c)) a)"
35 -- >>> cY = tRead "λf.(λx.f (x x)) (λx.f (x x))"
36 -- >>> cI = tRead "λx.x"
37 -- >>> cK = tRead "λx y.x"
38 -- >>> cS = tRead "λx y z.x z (y z)"
39 -- >>> let aVarName = oneof . map (pure . (:[])) $ ['a'..'e']
40 -- >>> let aVar = liftA Var aVarName
41 -- >>> let aComb = oneof . map pure $ [cS, cK, cI, cY]
42 -- >>> let aTerm 0 = aVar
43 -- >>> let aTerm n = oneof [aVar, aComb, liftA2 Lambda aVarName $ aTerm (n - 1), liftA2 App (aTerm (n `div` 2)) (aTerm (n `div` 2))]
44 -- >>> instance Arbitrary Term where arbitrary = sized aTerm
49 varnames = map (:[]) ['a'..'z'] ++ [c : s | s <- varnames, c <- ['a'..'z']]
51 alphaNorm :: Term -> Term
52 alphaNorm t = alpha varnames t
54 alpha (v:vs) (Lambda x r) = Lambda v . alpha vs $ substitute x (Var v) r
55 alpha vs (App u v) = App (alpha vs u) (alpha vs v)
56 alpha _ (Var x) = Var x
57 alpha [] _ = undefined
59 isFreeIn :: VarName -> Term -> Bool
60 isFreeIn x (Var v) = x == v
61 isFreeIn x (App t u) = x `isFreeIn` t || x `isFreeIn` u
62 isFreeIn x (Lambda v t) = x /= v && x `isFreeIn` t
64 rename :: Term -> Term
65 rename (Lambda x t) = Lambda n (substitute x (Var n) t)
67 rnm v = if (v ++ "r") `isFreeIn` t then rnm (v ++ "r") else v ++ "r"
68 rename _ = error "TODO vymyslet reprezentaci, kde pujde udelat fce, ktera bere jen Lambdy"
70 substitute :: VarName -> Term -> Term -> Term
71 substitute a b (Var x) = if x == a then b else Var x
72 substitute a b (Lambda x t)
74 | x `isFreeIn` b = substitute a b $ rename (Lambda x t)
75 | otherwise = Lambda x (substitute a b t)
76 substitute a b (App t u) = App (substitute a b t) (substitute a b u)
80 -- >>> reduce $ tRead "(\\x.x x) (g f)"
83 reduce :: Term -> Term
84 reduce (Var x) = Var x
85 reduce (Lambda x t) = Lambda x (reduce t)
86 reduce (App t u) = app (reduce t) u
87 where app (Lambda x v) w = reduce $ substitute x w v
88 app a b = App a (reduce b)
90 data Strategy = Eager | Lazy
92 reduceStep :: (Monad m) => Term -> m Term
93 reduceStep (RedEx x s t) = return $ substitute x t s
94 reduceStep t = return $ t
96 data Z = R Term Z | L Z Term | ZL VarName Z | E
98 type TermZipper = (Term, Z, D)
100 move :: TermZipper -> TermZipper
101 move (App l r, c, Down) = (l, L c r, Down)
102 move (Lambda x t, c, Down) = (t, ZL x c, Down)
103 move (Var x, c, Down) = (Var x, c, Up)
104 move (t, L c r, Up) = (r, R t c, Down)
105 move (t, R l c, Up) = (App l t, c, Up)
106 move (t, ZL x c, Up) = (Lambda x t, c, Up)
107 move (t, E, Up) = (t, E, Up)
109 unmove :: TermZipper -> TermZipper
110 unmove (t, L c r, Down) = (App t r, c, Down)
113 travPost :: (Monad m) => (Term -> m Term) -> Term -> m Term
114 travPost fnc term = tr fnc (term, E, Down)
116 tr f (t@(RedEx _ _ _), c, Up) = do
119 tr _ (t, E, Up) = return t
120 tr f (t, c, Up) = tr f $ move (t, c, Up)
121 tr f (t, c, Down) = tr f $ move (t, c, Down)
123 travPre :: (Monad m) => (Term -> m Term) -> Term -> m Term
124 travPre fnc term = tr fnc (term, E, Down)
126 tr f (t@(RedEx _ _ _), c, Down) = do
128 tr f $ unmove (nt, c, Down)
129 tr _ (t, E, Up) = return t
130 tr f (t, c, Up) = tr f $ move (t, c, Up)
131 tr f (t, c, Down) = tr f $ move (t, c, Down)
134 printT :: Term -> IO Term
142 -- >>> toNormalForm Eager 100 cI
145 -- >>> toNormalForm Eager 100 $ App cI cI
148 -- >>> toNormalForm Eager 100 $ (App (App cK cI) cY)
151 -- >>> toNormalForm Lazy 100 $ (App (App cK cI) cY)
154 -- prop> (\ t u -> t == u || t == Nothing || u == Nothing) (alphaNorm <$> toNormalForm Lazy 1000 x) (alphaNorm <$> toNormalForm Eager 1000 x)
157 toNormalForm :: Strategy -> Int -> Term -> Maybe Term
158 toNormalForm Eager n = flip evalStateT 0 . travPost (cnt >=> short n >=> reduceStep)
159 toNormalForm Lazy n = flip evalStateT 0 . travPre (cnt >=> short n >=> reduceStep)
161 cnt :: (Monad m) => Term -> StateT Int m Term
162 cnt t@(RedEx _ _ _) = do
167 short :: Int -> Term -> StateT Int Maybe Term