alpha equivalence
authorTomáš Musil <tomik.musil@gmail.com>
Sat, 13 Dec 2014 06:50:35 +0000 (07:50 +0100)
committerTomáš Musil <tomik.musil@gmail.com>
Sat, 13 Dec 2014 06:50:35 +0000 (07:50 +0100)
src/Lambda.hs

index 199d7a8..07d1f4b 100644 (file)
@@ -35,9 +35,12 @@ import Control.Monad.State
 -- >>> import Control.Applicative
 -- >>> 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, liftA2 Lambda aVarName $ aTerm (n - 1), liftA2 App (aTerm (n `div` 2)) (aTerm (n `div` 2))] 
+-- >>> 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
 
 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)"
@@ -51,6 +54,9 @@ cI = tRead "λx.x"
 cK :: Term
 cK = tRead "λx y.x"
 
+cS :: Term
+cS = tRead "λx y z.x z (y z)"
+
 type VarName = String
 
 -- | 
@@ -59,6 +65,16 @@ type VarName = String
 
 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 EmLambda x y t = Lambda x (Lambda y t)
@@ -218,7 +234,7 @@ printT t = do
 -- >>> toNormalForm Lazy 100 $ (App (App cK cI) cY)
 -- 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