alpha equivalence
[fp.git] / src / Lambda.hs
index f828f4d..07d1f4b 100644 (file)
@@ -20,10 +20,12 @@ module Lambda
   , tRead
     -- * Reduction
   , reduce
   , tRead
     -- * Reduction
   , reduce
+  , toNormalForm
+  , Strategy(..)
   ) where
   
 
   ) where
   
 
-import Data.Text as T
+import Data.Text as T hiding (map)
 import Data.Attoparsec.Text
 import Control.Applicative
 import Control.Monad.State 
 import Data.Attoparsec.Text
 import Control.Applicative
 import Control.Monad.State 
@@ -31,9 +33,17 @@ import Control.Monad.State
 -- $setup
 -- >>> import Test.QuickCheck
 -- >>> import Control.Applicative
 -- $setup
 -- >>> import Test.QuickCheck
 -- >>> import Control.Applicative
--- >>> let aTerm 0 = liftA (Var . ("x" ++) . show) (arbitrary :: Gen Int)
--- >>> let aTerm n = oneof [aTerm 0, liftA (Lambda "x") $ aTerm (n - 1), liftA2 App (aTerm (n `div` 2)) (aTerm (n `div` 2))] 
+-- >>> 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
 -- >>> instance Arbitrary Term where arbitrary = sized aTerm
+--
+-- TODO: shrink Terms
+
+cP :: Term
+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 :: Term
 cY = tRead "λf.(λx.f (x x)) (λx.f (x x))"
 
 cY :: Term
 cY = tRead "λf.(λx.f (x x)) (λx.f (x x))"
@@ -44,13 +54,26 @@ cI = tRead "λx.x"
 cK :: Term
 cK = tRead "λx y.x"
 
 cK :: Term
 cK = tRead "λx y.x"
 
+cS :: Term
+cS = tRead "λx y z.x z (y z)"
+
 type VarName = String
 
 -- | 
 -- >>> print $ Lambda "x" (Var "x")
 -- (λx.x)
 
 type VarName = String
 
 -- | 
 -- >>> print $ Lambda "x" (Var "x")
 -- (λx.x)
 
-data Term = EmptyT | Var VarName | Lambda VarName Term | App Term Term deriving (Eq)
+data Term = Var VarName | Lambda VarName Term | App Term Term deriving (Eq)
+
+varnames :: [VarName]
+varnames = map (:[]) ['a'..'z'] ++ [c : s | s <- varnames, c <- ['a'..'z']]
+
+alphaNorm :: Term -> Term
+alphaNorm t = alpha varnames t
+  where
+    alpha (v:vs) (Lambda x t) = Lambda v . alpha vs $ substitute x (Var v) t
+    alpha vs (App u v) = App (alpha vs u) (alpha vs v)
+    alpha vs (Var x) = Var x
 
 pattern RedEx x t s = App (Lambda x t) s
 pattern AppApp a b c = App a (App b c)
 
 pattern RedEx x t s = App (Lambda x t) s
 pattern AppApp a b c = App a (App b c)
@@ -155,20 +178,11 @@ reduceStep :: (Monad m) => Term -> m Term
 reduceStep (RedEx x s t) = return $ substitute x t s
 reduceStep t = return $ t
 
 reduceStep (RedEx x s t) = return $ substitute x t s
 reduceStep t = return $ t
 
-traversPost :: (Monad m) => (Term -> m Term) -> Term -> m Term
-traversPost f (App t u) = do
-  nt <- traversPost f t
-  nu <- traversPost f u
-  case App nt nu of
-    l@(RedEx _ _ _) -> traversPost f =<< f l
-    r -> return r
-traversPost f (Lambda x t) = return . Lambda x =<< traversPost f t
-traversPost f (Var x) = return $ (Var x)
-
 data Z = R Term Z | L Z Term | ZL VarName Z | E
 data D = Up | Down
 data Z = R Term Z | L Z Term | ZL VarName Z | E
 data D = Up | Down
-data Zip = Zip Z Term
+type TermZipper = (Term, Z, D)
 
 
+move :: TermZipper -> TermZipper
 move (App l r, c, Down) = (l, L c r, Down)
 move (Lambda x t, c, Down) = (t, ZL x c, Down)
 move (Var x, c, Down) = (Var x, c, Up)
 move (App l r, c, Down) = (l, L c r, Down)
 move (Lambda x t, c, Down) = (t, ZL x c, Down)
 move (Var x, c, Down) = (Var x, c, Up)
@@ -177,18 +191,17 @@ move (t, R l c, Up) = (App l t, c, Up)
 move (t, ZL x c, Up) = (Lambda x t, c, Up)
 move (t, E, Up) = (t, E, Up)
 
 move (t, ZL x c, Up) = (Lambda x t, c, Up)
 move (t, E, Up) = (t, E, Up)
 
+unmove :: TermZipper -> TermZipper
 unmove (t, L c r, Down) = (App t r, c, Down)
 unmove x = x
 
 unmove (t, L c r, Down) = (App t r, c, Down)
 unmove x = x
 
-getF (t, _, _) = t
-
 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
       nt <- f t
       tr f $ (nt, c, Down)
 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
       nt <- f t
       tr f $ (nt, c, Down)
-    tr f (t, E, Up) = return t
+    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)
 
     tr f (t, c, Up) = tr f $ move (t, c, Up)
     tr f (t, c, Down) = tr f $ move (t, c, Down)
 
@@ -198,7 +211,7 @@ travPre fnc term = tr fnc (term, E, Down)
     tr f (t@(RedEx _ _ _), c, Down) = do
       nt <- f t
       tr f $ unmove (nt, c, Down)
     tr f (t@(RedEx _ _ _), c, Down) = do
       nt <- f t
       tr f $ unmove (nt, c, Down)
-    tr f (t, E, Up) = return t
+    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)
 
     tr f (t, c, Up) = tr f $ move (t, c, Up)
     tr f (t, c, Down) = tr f $ move (t, c, Down)
 
@@ -215,13 +228,13 @@ printT t = do
 -- >>> toNormalForm Eager 100 $ App cI cI
 -- Just (λx.x)
 --
 -- >>> toNormalForm Eager 100 $ App cI cI
 -- Just (λx.x)
 --
--- >>> toNormalForm Eager 1000 $ (App (App cK cI) cY)
+-- >>> toNormalForm Eager 100 $ (App (App cK cI) cY)
 -- Nothing
 --
 -- Nothing
 --
--- >>> toNormalForm Lazy 1000 $ (App (App cK cI) cY)
+-- >>> toNormalForm Lazy 100 $ (App (App cK cI) cY)
 -- Just (λx.x)
 --
 -- Just (λx.x)
 --
--- prop> (\ t u -> t == u || t == Nothing || u == Nothing) (toNormalForm Lazy 1000 x) (toNormalForm Eager 1000 x)
+-- prop> (\ t u -> t == u || t == Nothing || u == Nothing) (alphaNorm <$> toNormalForm Lazy 1000 x) (alphaNorm <$> toNormalForm Eager 1000 x)
 
 
 toNormalForm :: Strategy -> Int -> Term -> Maybe Term
 
 
 toNormalForm :: Strategy -> Int -> Term -> Maybe Term
@@ -235,8 +248,8 @@ cnt t@(RedEx _ _ _) = do
 cnt t = return t
 
 short :: Int -> Term -> StateT Int Maybe Term
 cnt t = return t
 
 short :: Int -> Term -> StateT Int Maybe Term
-short max t = do
+short maxN t = do
   n <- get
   n <- get
-  if n > max
+  if n > maxN
     then lift Nothing
     else return t
     then lift Nothing
     else return t