X-Git-Url: http://git.tomasm.cz/fp.git/blobdiff_plain/2d2af9eded0d0e4968cf21746f3772f61caecd73..refs/heads/master:/src/Lambda.hs?ds=inline diff --git a/src/Lambda.hs b/src/Lambda.hs index b3a25ce..428ac99 100644 --- a/src/Lambda.hs +++ b/src/Lambda.hs @@ -14,6 +14,7 @@ module Lambda ( -- * Types VarName , Term(..) + , pattern RedEx -- * Reduction , alphaNorm , reduce @@ -25,6 +26,9 @@ import Control.Monad.State import Lambda.Term +import Debug.Trace +import Lambda.Parser.Fancy () + -- $setup -- >>> import Control.Applicative -- >>> import Lambda.Parser.Fancy @@ -75,9 +79,9 @@ reduce (App t u) = app (reduce t) u 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 @@ -96,6 +100,8 @@ unmove :: TermZipper -> TermZipper 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 @@ -130,12 +136,21 @@ travPre fnc term = tr fnc (term, E, Down) -- >>> 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