Fix Show and Eq instances for Expression
Show now outputs according to usual lambda calculus notation. Eq now tests for alpha equivalency
This commit is contained in:
parent
705df975b0
commit
577a4e5a71
1 changed files with 16 additions and 9 deletions
25
lambda.hs
25
lambda.hs
|
|
@ -34,15 +34,18 @@ instance (Show s) => Show (Expression s) where
|
|||
-- show_ :: Expression String -> String
|
||||
where show_ c (EReference n)
|
||||
| n >= 0 && n < length c = c !! n
|
||||
| otherwise = "ERR" -- TODO: Do this better?
|
||||
show_ c (ESymbol s) = s
|
||||
show_ c (EExpr a b@(EExpr _ _)) = show_ c a ++ " (" ++ show_ c b ++ ")"
|
||||
show_ c (EExpr a b) = show_ c a ++ " " ++ show_ c b
|
||||
show_ c (ELambda s e@(EExpr _ _)) = "\\" ++ s ++ ".(" ++ show_ (s : c) e ++ ")"
|
||||
show_ c (ELambda s e) = "\\" ++ s ++ "." ++ show_ (s : c) e
|
||||
| otherwise = "ERR" -- TODO: Deal with errors properly?
|
||||
show_ c (ESymbol s) = s
|
||||
show_ c (ELambda s e) = "\\" ++ s ++ "." ++ show_ (s : c) e
|
||||
show_ c (EExpr a@(ELambda _ _) b@(ELambda _ _)) = "(" ++ show_ c a ++ ") (" ++ show_ c b ++ ")"
|
||||
show_ c (EExpr a@(ELambda _ _) b@(EExpr _ _)) = "(" ++ show_ c a ++ ") (" ++ show_ c b ++ ")"
|
||||
show_ c (EExpr a@(ELambda _ _) b ) = "(" ++ show_ c a ++ ") " ++ show_ c b
|
||||
show_ c (EExpr a b@(ELambda _ _)) = show_ c a ++ " (" ++ show_ c b ++ ")"
|
||||
show_ c (EExpr a b@(EExpr _ _)) = show_ c a ++ " (" ++ show_ c b ++ ")"
|
||||
show_ c (EExpr a b) = show_ c a ++ " " ++ show_ c b
|
||||
|
||||
instance Eq (Expression s) where
|
||||
(ESymbol _) == (ESymbol _) = True
|
||||
instance (Eq s) => Eq (Expression s) where
|
||||
(ESymbol a) == (ESymbol b) = a == b
|
||||
(EReference a) == (EReference b) = a == b
|
||||
(EExpr a b) == (EExpr c d) = a == c && b == d
|
||||
(ELambda _ a) == (ELambda _ b) = a == b
|
||||
|
|
@ -73,7 +76,7 @@ takeWhileUnique l = map fst
|
|||
$ takeWhile (\a -> not $ fst a `elem` snd a)
|
||||
$ zip l (inits l)
|
||||
|
||||
evaluate :: Expression s -> [Expression s]
|
||||
evaluate :: (Eq s) => Expression s -> [Expression s]
|
||||
evaluate = takeWhileUnique . iterate apply
|
||||
|
||||
{-
|
||||
|
|
@ -90,6 +93,9 @@ newtype StrSymbol = StrSymbol String
|
|||
instance Show StrSymbol where
|
||||
show (StrSymbol s) = s
|
||||
|
||||
instance Eq StrSymbol where
|
||||
(StrSymbol a) == (StrSymbol b) = a == b
|
||||
|
||||
--instance Read StrSymbol where
|
||||
-- read s = StrSymbol s
|
||||
|
||||
|
|
@ -103,6 +109,7 @@ main = do
|
|||
putStrLn "Test nested expressions and parentheses"
|
||||
print (_e (_e (_s 1) (_s 2)) (_e (_s 3) (_s 4)))
|
||||
print (_e (_e (_s 1) (_e (_s 2) (_s 3))) (_s 4))
|
||||
print (_e (_e (_l 1 (_r 0)) (_e (_l 2 (_r 0)) (_l 3 (_r 0)))) (_l 4 (_r 0)))
|
||||
putStrLn "Test references and symbols in lambda expressions"
|
||||
print (_l 5 (_l 2 (_e (_s 3) (_r 0))))
|
||||
print (_l 5 (_l 2 (_e (_s 3) (_r 1))))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue