Eager and Lazy
[fp.git] / src / Lambda.hs
index f54a100..f828f4d 100644 (file)
@@ -1,32 +1,61 @@
 {-# OPTIONS_GHC -fno-warn-unused-do-bind #-}
 {-# LANGUAGE PatternSynonyms #-}
 
-module Lambda where
+-- |
+-- Module      :  Lambda
+-- Copyright   :  Tomáš Musil 2014
+-- License     :  BSD-3
+--
+-- Maintainer  :  tomik.musil@gmail.com
+-- Stability   :  experimental
+--
+-- This is a toy λ-calculus implementation.
+
+module Lambda 
+  ( -- * Types
+    VarName
+  , Term(..)
+    -- * Parsing terms
+  , parseTerm
+  , tRead
+    -- * Reduction
+  , reduce
+  ) where
+  
 
 import Data.Text as T
 import Data.Attoparsec.Text
 import Control.Applicative
+import Control.Monad.State 
+
+-- $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))] 
+-- >>> instance Arbitrary Term where arbitrary = sized aTerm
+
+cY :: Term
+cY = tRead "λf.(λx.f (x x)) (λx.f (x x))"
+
+cI :: Term
+cI = tRead "λx.x"
+
+cK :: Term
+cK = tRead "λx y.x"
 
 type VarName = String
-data Term = Var VarName | Lambda VarName Term | App Term Term deriving (Eq)
+
+-- | 
+-- >>> print $ Lambda "x" (Var "x")
+-- (λx.x)
+
+data Term = EmptyT | Var VarName | Lambda VarName Term | App Term Term deriving (Eq)
 
 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)
 
--- $setup
--- >>> import Test.QuickCheck
--- >>> import Control.Applicative
--- >>> let aTerm 0 = pure $ Var "x"
--- >>> let aTerm n = oneof [pure (Var "x"), liftA (Lambda "x") $ aTerm (n - 1), liftA2 App (aTerm (n `div` 2)) (aTerm (n `div` 2))] 
--- >>> instance Arbitrary Term where arbitrary = sized aTerm
-
--- | Read and show λ-terms
---
---   >>> print $ Lambda "x" (Var "x")
---   (λx.x)
---
---   prop> t == tRead (show (t :: Term))
 
 instance Show Term where
   show (Var x) = x
@@ -38,7 +67,9 @@ instance Show Term where
 braced :: Term -> String
 braced t = "(" ++ show t ++ ")"
 
---instance Read Term where
+-- |
+-- prop> t == tRead (show (t :: Term))
+
 tRead :: String -> Term
 tRead s = case parseOnly (parseTerm <* endOfInput) (T.pack s) of
     (Right t) -> t
@@ -46,7 +77,7 @@ tRead s = case parseOnly (parseTerm <* endOfInput) (T.pack s) of
 
 parseVar :: Parser Term
 parseVar = do
-  x <- many1 letter
+  x <- many1 (letter <|> digit)
   return $! Var x
 
 parseLambda :: Parser Term
@@ -106,9 +137,106 @@ substitute a b (Lambda x t)
   | otherwise = Lambda x (substitute a b t)
 substitute a b (App t u) = App (substitute a b t) (substitute a b u)
 
+-- | Reduce λ-term
+--
+-- >>> reduce $ tRead "(\\x.x x) (g f)"
+-- g f (g f)
+
 reduce :: Term -> Term
 reduce (Var x) = Var x
 reduce (Lambda x t) = Lambda x (reduce t)
 reduce (App t u) = app (reduce t) u
   where app (Lambda x v) w = reduce $ substitute x w v
         app a b = App a (reduce b)
+
+data Strategy = Eager | Lazy
+
+reduceStep :: (Monad m) => Term -> m Term
+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 Zip = Zip Z Term
+
+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 (t, L c r, Up) = (r, R t c, Down)
+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)
+
+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)
+    tr f (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
+      nt <- f t
+      tr f $ unmove (nt, c, Down)
+    tr f (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
+-- Just (λx.x)
+--
+-- >>> toNormalForm Eager 100 $ App cI cI
+-- Just (λx.x)
+--
+-- >>> toNormalForm Eager 1000 $ (App (App cK cI) cY)
+-- Nothing
+--
+-- >>> toNormalForm Lazy 1000 $ (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)
+
+
+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)
+
+cnt :: (Monad m) => Term -> StateT Int m Term
+cnt t@(RedEx _ _ _) = do
+  modify (+ 1)
+  return t
+cnt t = return t
+
+short :: Int -> Term -> StateT Int Maybe Term
+short max t = do
+  n <- get
+  if n > max
+    then lift Nothing
+    else return t