X-Git-Url: http://git.tomasm.cz/fp.git/blobdiff_plain/801f0c270025ca872a488690aca42b59eeee89aa..0ab32fcedd87264abc88474b62f4c2e22ddf200b:/src/Lambda.hs diff --git a/src/Lambda.hs b/src/Lambda.hs index f54a100..f89cc7e 100644 --- a/src/Lambda.hs +++ b/src/Lambda.hs @@ -1,18 +1,32 @@ {-# 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 - -type VarName = String -data Term = 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) +import Control.Monad.State -- $setup -- >>> import Test.QuickCheck @@ -21,12 +35,18 @@ pattern EmLambda x y t = Lambda x (Lambda y t) -- >>> 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)) +type VarName = String + +-- | +-- >>> print $ Lambda "x" (Var "x") +-- (λx.x) + +data Term = 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) + instance Show Term where show (Var x) = x @@ -38,7 +58,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 @@ -106,9 +128,49 @@ 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 + f (App nt nu) +traversPost f (Lambda x t) = f . Lambda x =<< traversPost f t +traversPost f (Var x) = f (Var x) + +printT :: Term -> IO Term +printT t = do + print t + return t + +toNormalForm :: Strategy -> Int -> Term -> Maybe Term +toNormalForm Eager n = flip evalStateT 0 . traversPost (short n >=> cnt >=> 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