Clean up some function logic
This commit is contained in:
parent
cf0ed21144
commit
8a23c2e875
1 changed files with 22 additions and 26 deletions
46
lambda.hs
46
lambda.hs
|
|
@ -35,13 +35,13 @@ instance Symbol SymLetter where
|
||||||
let bases = map symLetBase other
|
let bases = map symLetBase other
|
||||||
freeBases = names \\ (nub bases)
|
freeBases = names \\ (nub bases)
|
||||||
in SymLetter (head freeBases)
|
in SymLetter (head freeBases)
|
||||||
where letters = ['a'..'z']
|
where namesN 0 = [""]
|
||||||
namesN 0 = [""]
|
namesN n = [n ++ [l] | n <- namesN (n - 1), l <- ['a'..'z']]
|
||||||
namesN n = [n ++ [l] | n <- namesN (n - 1), l <- letters]
|
|
||||||
names = concatMap namesN [1..]
|
names = concatMap namesN [1..]
|
||||||
|
|
||||||
|
|
||||||
-- An expression. Can be a mere symbol, a lambda application, or a lambda abstraction.
|
-- An expression. Can be a mere symbol, a lambda application, or a lambda abstraction.
|
||||||
|
-- re. "a lambda application": (a b) is also a valid expression, even though nothing is applied here ~G
|
||||||
data Expression s = ESymbol s
|
data Expression s = ESymbol s
|
||||||
| EExpr (Expression s) (Expression s)
|
| EExpr (Expression s) (Expression s)
|
||||||
| ELambda s (Expression s)
|
| ELambda s (Expression s)
|
||||||
|
|
@ -60,37 +60,33 @@ showTopLevel e = show e
|
||||||
printTopLevel :: (Show s) => Expression s -> IO ()
|
printTopLevel :: (Show s) => Expression s -> IO ()
|
||||||
printTopLevel = putStrLn . showTopLevel
|
printTopLevel = putStrLn . showTopLevel
|
||||||
|
|
||||||
{-
|
contextMap :: (Symbol s, Symbol t) => ([t] -> s -> t) -> [t] -> Expression s -> Expression t
|
||||||
Apply a function
|
contextMap f context e = helper f [] context e
|
||||||
Insert an expression into another expression:
|
where helper f mapping context (ESymbol s) = ESymbol $ fromMaybe (f context s) $ lookup s mapping
|
||||||
replace all symbols s with an expression
|
helper f mapping context (EExpr a b) = EExpr (helper f mapping context a) (helper f mapping context b)
|
||||||
change all symbols within that expression to be unique in the expression's new context
|
helper f mapping context (ELambda s e) =
|
||||||
simplify all symbols
|
let news = f context s
|
||||||
-}
|
newmapping = (s, news) : mapping
|
||||||
simplifyExpr :: (Symbol s) => [(s, s)] -> [s] -> Expression s -> Expression s
|
newcontext = news : context
|
||||||
simplifyExpr mapping context (ESymbol s) = ESymbol $ fromMaybe (findName context s) $ lookup s mapping
|
in ELambda news (helper f newmapping newcontext e)
|
||||||
simplifyExpr mapping context (EExpr a b) = EExpr (simplifyExpr mapping context a) (simplifyExpr mapping context b)
|
|
||||||
simplifyExpr mapping context (ELambda l e) =
|
|
||||||
let newl = findName context l
|
|
||||||
newmapping = (l, newl) : mapping
|
|
||||||
newcontext = newl : context
|
|
||||||
in ELambda newl (simplifyExpr newmapping newcontext e)
|
|
||||||
|
|
||||||
makeUnique :: (Symbol s) => [s] -> Expression s -> Expression s
|
makeUnique :: (Symbol s) => [s] -> Expression s -> Expression s
|
||||||
makeUnique = simplifyExpr []
|
makeUnique = contextMap findName
|
||||||
|
|
||||||
simplify :: (Symbol s) => Expression s -> Expression s
|
simplify :: (Symbol s) => Expression s -> Expression s
|
||||||
simplify = makeUnique []
|
simplify = makeUnique []
|
||||||
|
|
||||||
insertExpr :: (Symbol s) => s -> Expression s -> [s] -> Expression s -> Expression s
|
-- symbol to replace -> expression to replace symbol with -> expression to replace in -> resulting epression
|
||||||
insertExpr r new context old@(ESymbol s)
|
replaceIn :: (Symbol s) => s -> Expression s -> Expression s -> Expression s
|
||||||
| r == s = makeUnique context new
|
replaceIn = helper []
|
||||||
|
where helper context replace new old@(ESymbol s)
|
||||||
|
| replace == s = makeUnique context new
|
||||||
| otherwise = old
|
| otherwise = old
|
||||||
insertExpr r new context (EExpr a b) = EExpr (insertExpr r new context a) (insertExpr r new context b)
|
helper context replace new (EExpr a b) = EExpr (helper context replace new a) (helper context replace new b)
|
||||||
insertExpr r new context (ELambda l e) = ELambda l (insertExpr r new (l : context) e)
|
helper context replace new (ELambda s e) = ELambda s (helper (s : context) replace new e)
|
||||||
|
|
||||||
apply :: (Symbol s) => Expression s -> Expression s
|
apply :: (Symbol s) => Expression s -> Expression s
|
||||||
apply (EExpr (ELambda s l) b) = insertExpr s b [] l -- [], not [s]
|
apply (EExpr (ELambda s a) b) = replaceIn s b a -- replace s with b in a
|
||||||
apply (EExpr a@(EExpr _ _) b) = EExpr (apply a) b
|
apply (EExpr a@(EExpr _ _) b) = EExpr (apply a) b
|
||||||
apply e = e
|
apply e = e
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue