( -- * Types
VarName
, Term(..)
+ , pattern RedEx
-- * Reduction
, alphaNorm
, reduce
, toNormalForm
, Strategy(..)
) where
-
import Control.Monad.State
import Lambda.Term
+import Debug.Trace
+import Lambda.Parser.Fancy ()
+
-- $setup
--- >>> import Test.QuickCheck
-- >>> import Control.Applicative
-- >>> import Lambda.Parser.Fancy
--- >>> import Lambda.Term
--- >>> 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)"
--- >>> cY = tRead "λf.(λx.f (x x)) (λx.f (x x))"
--- >>> cI = tRead "λx.x"
--- >>> cK = tRead "λx y.x"
--- >>> cS = tRead "λx y z.x z (y z)"
--- >>> let aVarName = oneof . map (pure . (:[])) $ ['a'..'e']
--- >>> let aVar = liftA Var aVarName
--- >>> let aComb = oneof . map pure $ [cS, cK, cI, cY]
--- >>> let aTerm 0 = aVar
--- >>> let aTerm n = oneof [aVar, aComb, liftA2 Lambda aVarName $ aTerm (n - 1), liftA2 App (aTerm (n `div` 2)) (aTerm (n `div` 2))]
--- >>> instance Arbitrary Term where arbitrary = sized aTerm
---
--- TODO: shrink Terms
+-- >>> import Test.Term
+-- >>> import Test.QuickCheck
varnames :: [VarName]
varnames = map (:[]) ['a'..'z'] ++ [c : s | s <- varnames, c <- ['a'..'z']]
alphaNorm :: Term -> Term
-alphaNorm t = alpha varnames t
+alphaNorm = alpha varnames
where
alpha (v:vs) (Lambda x r) = Lambda v . alpha vs $ substitute x (Var v) r
alpha vs (App u v) = App (alpha vs u) (alpha vs v)
data Strategy = Eager | Lazy
-reduceStep :: (Monad m) => Term -> m Term
-reduceStep (RedEx x s t) = return $ substitute x t s
-reduceStep t = return $ t
+reduceStep :: Term -> Term
+reduceStep (RedEx x s t) = substitute x t s
+reduceStep t = t
data Z = R Term Z | L Z Term | ZL VarName Z | E
data D = Up | Down
unmove (t, L c r, Down) = (App t r, c, Down)
unmove x = x
+-- getTerm :: TermZipper -> Term
+
travPost :: (Monad m) => (Term -> m Term) -> Term -> m Term
travPost fnc term = tr fnc (term, E, Down)
where
- tr f (t@(RedEx _ _ _), c, Up) = do
+ tr f (t@RedEx{}, c, Up) = do
nt <- f t
- tr f $ (nt, c, Down)
+ tr f (nt, c, Down)
tr _ (t, E, Up) = return t
tr f (t, c, Up) = tr f $ move (t, c, Up)
tr f (t, c, Down) = tr f $ move (t, c, Down)
travPre :: (Monad m) => (Term -> m Term) -> Term -> m Term
travPre fnc term = tr fnc (term, E, Down)
where
- tr f (t@(RedEx _ _ _), c, Down) = do
+ tr f (t@RedEx{}, c, Down) = do
nt <- f t
tr f $ unmove (nt, c, Down)
tr _ (t, E, Up) = return t
tr f (t, c, Up) = tr f $ move (t, c, Up)
tr f (t, c, Down) = tr f $ move (t, c, Down)
-{-
-printT :: Term -> IO Term
-printT t = do
- print t
- return t
--}
-
-- |
--
-- >>> toNormalForm Eager 100 cI
-- >>> toNormalForm Lazy 100 $ (App (App cK cI) cY)
-- Just (λx.x)
--
--- prop> (\ t u -> t == u || t == Nothing || u == Nothing) (alphaNorm <$> toNormalForm Lazy 1000 x) (alphaNorm <$> toNormalForm Eager 1000 x)
+-- prop> within 10000000 $ (\ t u -> t == u || t == Nothing || u == Nothing) (alphaNorm <$> toNormalForm Lazy 100 x) (alphaNorm <$> toNormalForm Eager 100 x)
+
+-- 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))"
+toNormalFormDebug :: Strategy -> Int -> Term -> Maybe Term
+toNormalFormDebug Eager n = flip evalStateT 0 . travPost (prnt >=> cnt >=> short n >=> return . reduceStep)
+toNormalFormDebug Lazy n = flip evalStateT 0 . travPre (prnt >=> cnt >=> short n >=> return . reduceStep)
+
toNormalForm :: Strategy -> Int -> Term -> Maybe Term
-toNormalForm Eager n = flip evalStateT 0 . travPost (cnt >=> short n >=> reduceStep)
-toNormalForm Lazy n = flip evalStateT 0 . travPre (cnt >=> short n >=> reduceStep)
+toNormalForm Eager n = flip evalStateT 0 . travPost (cnt >=> short n >=> return . reduceStep)
+toNormalForm Lazy n = flip evalStateT 0 . travPre (cnt >=> short n >=> return . reduceStep)
+
+prnt :: (Monad m) => Term -> StateT Int m Term
+prnt t = traceShow t $ return t
cnt :: (Monad m) => Term -> StateT Int m Term
-cnt t@(RedEx _ _ _) = do
+cnt t@RedEx{} = do
modify (+ 1)
return t
cnt t = return t