From a68a8519ca458c3d2ca91e8a51dc367ee677c3aa Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 5 Nov 2017 17:28:44 +0000 Subject: [PATCH] Apply expressions --- lambda.hs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lambda.hs b/lambda.hs index f87cd41..34423f4 100644 --- a/lambda.hs +++ b/lambda.hs @@ -5,6 +5,7 @@ import Data.List data Symbol = Symbol { symBase :: String -- lowercase a to z , symLen :: Int -- nonnegative } + deriving (Eq) instance Show Symbol where show (Symbol s n) = s ++ (replicate n '\'') @@ -30,6 +31,28 @@ instance (Show s) => Show (Expression s) where show (EExpr a b) = "(" ++ show a ++ " " ++ show b ++ ")" show (ELambda s e) = "\\" ++ show s ++ "." ++ show e +{- +Apply a function +Insert an expression into another expression: + replace all symbols s with an expression + change all symbols within that expression to be unique in the expression's new context +-} +makeUnique :: [Symbol] -> Expression Symbol -> Expression Symbol +makeUnique context (ESymbol s) = ESymbol (findName context s) +makeUnique context (EExpr a b) = EExpr (makeUnique context a) (makeUnique context b) +makeUnique context (ELambda l e) = ELambda (findName context l) (makeUnique context e) + +insertExpr :: Symbol -> Expression Symbol -> [Symbol] -> Expression Symbol -> Expression Symbol +insertExpr r new context old@(ESymbol s) + | r == s = new + | otherwise = old +insertExpr r new context (EExpr a b) = EExpr (insertExpr r new context a) (insertExpr r new context b) +insertExpr r new context (ELambda l e) = ELambda l (insertExpr r new (l : context) e) + +apply :: Expression Symbol -> Expression Symbol +apply (EExpr (ELambda s l) b) = insertExpr s b [] l -- [], not [s] +apply e = e + _s :: String -> Expression Symbol _s s = ESymbol $ Symbol s 0 @@ -47,4 +70,3 @@ main = do print $ findName [(Symbol "a" 0), (Symbol "b" 0), (Symbol "a" 1)] (Symbol "b" 3) print $ findName [(Symbol "a" 0), (Symbol "b" 0), (Symbol "a" 1)] (Symbol "c" 2) print $ findName [(Symbol "a" 1), (Symbol "a" 3), (Symbol "a" 0)] (Symbol "a" 1) -