Skip to content
Snippets Groups Projects
Commit 5966762c authored by ik1g19's avatar ik1g19
Browse files

attempted fixing 6. it didnt work.

parent cd38cc6f
No related branches found
No related tags found
No related merge requests found
...@@ -41,6 +41,11 @@ data LamExpr = LamMacro String | LamApp LamExpr LamExpr | ...@@ -41,6 +41,11 @@ data LamExpr = LamMacro String | LamApp LamExpr LamExpr |
-- ADD YOUR OWN CODE HERE -- ADD YOUR OWN CODE HERE
{--| The following code is copyright of the University of Southampton |--
--| The author of this code is Isaac Klugman |--}
------------------------------------------Challenge 1------------------------------------------------- -- | inline comments ------------------------------------------Challenge 1------------------------------------------------- -- | inline comments
...@@ -271,13 +276,23 @@ macroDef [] = "" ...@@ -271,13 +276,23 @@ macroDef [] = ""
macroDef ( (name,expr):ms ) = foldl1 (++) ["def ",name," = ",exprToStr [] expr," in ",macroDef ms] macroDef ( (name,expr):ms ) = foldl1 (++) ["def ",name," = ",exprToStr [] expr," in ",macroDef ms]
-- {--| replaces a macro with its definition |--}
-- catchMacroStr :: [(String,LamExpr)] -> LamExpr -> String
-- catchMacro ms e | macros == [] = exprToStr ms e
-- | otherwise = fst $ head macros
-- where
-- macros = filter ( (e==) . snd ) ms
{--| replaces a macro with its definition |--} {--| replaces a macro with its definition |--}
catchMacro :: [(String,LamExpr)] -> LamExpr -> String catchMacroExpr :: [(String,LamExpr)] -> LamExpr -> LamExpr
catchMacro ms e | macros == [] = exprToStr ms e catchMacroExpr ms e | macros == [] = e
| otherwise = fst $ head macros | otherwise = LamMacro $ (fst . head) macros
where where
macros = filter ( (e==) . snd ) ms macros = filter ( (e==) . snd ) ms
catchAndConvert :: [(String,LamExpr)] -> LamExpr -> String
catchAndConvert ms e = exprToStr ms $ catchMacroExpr ms e
{--| converts expr to str |-- {--| converts expr to str |--
--| params: |-- --| params: |--
...@@ -285,22 +300,26 @@ catchMacro ms e | macros == [] = exprToStr ms e ...@@ -285,22 +300,26 @@ catchMacro ms e | macros == [] = exprToStr ms e
--| -expr to convert |-- --| -expr to convert |--
--| returns expr in string form |--} --| returns expr in string form |--}
exprToStr :: [(String,LamExpr)] -> LamExpr -> String exprToStr :: [(String,LamExpr)] -> LamExpr -> String
exprToStr ms e@(LamApp e1 e2) | e == eNone = none exprToStr ms e@(LamApp e1 e2) | eMacros == eNone = none
| e == eLeft = left | eMacros == eLeft = left
| e == eRight = right | eMacros == eRight = right
| e == eBoth = both | eMacros == eBoth = both
| otherwise = "fuck"
where where
none = foldl1 (++) [ catchMacro ms e1, " ", catchMacro ms e2 ] -- | applying different uses of parenthese to eMacros = LamApp (catchMacroExpr ms e1) (catchMacroExpr ms e2) -- | when testing for parentheses, macros will be replaced
left = foldl1 (++) ["(",catchMacro ms e1,") ", catchMacro ms e2 ] -- | determine when they are necessary -- | so macros must be replaced in the original expr
right = foldl1 (++) [ catchMacro ms e1, " (",catchMacro ms e2,")"]
both = foldl1 (++) ["(",catchMacro ms e1,") (",catchMacro ms e2,")"] none = foldl1 (++) [ catchAndConvert ms e1, " ", catchAndConvert ms e2 ] -- | applying different uses of parenthese to
left = foldl1 (++) ["(",catchAndConvert ms e1,") ", catchAndConvert ms e2 ] -- | determine when they are necessary
right = foldl1 (++) [ catchAndConvert ms e1, " (",catchAndConvert ms e2,")"]
both = foldl1 (++) ["(",catchAndConvert ms e1,") (",catchAndConvert ms e2,")"]
Just (LamDef _ eNone) = parseLamMacro none Just (LamDef _ eNone) = parseLamMacro none
Just (LamDef _ eLeft) = parseLamMacro left Just (LamDef _ eLeft) = parseLamMacro left
Just (LamDef _ eRight) = parseLamMacro right Just (LamDef _ eRight) = parseLamMacro right
Just (LamDef _ eBoth) = parseLamMacro both Just (LamDef _ eBoth) = parseLamMacro both
exprToStr ms (LamAbs x e ) = "\\x" ++ show x ++ " -> " ++ catchMacro ms e exprToStr ms (LamAbs x e ) = "\\x" ++ show x ++ " -> " ++ catchAndConvert ms e
exprToStr ms (LamVar x ) = "x" ++ show x exprToStr ms (LamVar x ) = "x" ++ show x
exprToStr ms (LamMacro m ) = m exprToStr ms (LamMacro m ) = m
...@@ -569,10 +588,11 @@ redex (LamApp e1 e2) = redex e1 || redex e2 ...@@ -569,10 +588,11 @@ redex (LamApp e1 e2) = redex e1 || redex e2
eval1cbv :: [ (String,LamExpr) ] -> LamExpr -> LamExpr eval1cbv :: [ (String,LamExpr) ] -> LamExpr -> LamExpr
eval1cbv ms (LamAbs x e) = LamAbs x e eval1cbv ms (LamAbs x e) = LamAbs x e
eval1cbv ms (LamMacro name) = (snd . head) $ filter ((name==) . fst) ms
eval1cbv ms (LamApp (LamAbs x e1) e@(LamAbs y e2)) = subst e1 x e eval1cbv ms (LamApp (LamAbs x e1) e@(LamAbs y e2)) = subst e1 x e
eval1cbv ms (LamApp e1@(LamAbs x e) e2) = LamApp e1 $ eval1cbv ms e2 eval1cbv ms (LamApp e1@(LamAbs x e) e2) = LamApp e1 (eval1cbv ms e2)
eval1cbv ms (LamApp e1 e2) = LamApp (eval1cbv ms e1) e2 eval1cbv ms (LamApp e1 e2) = LamApp (eval1cbv ms e1) e2
eval1cbv ms (LamMacro name) = (snd . head) $ filter ((name==) . fst) ms
eval1cbv ms (LamVar x) = LamVar x
eval1cbn :: [ (String,LamExpr) ] -> LamExpr -> LamExpr eval1cbn :: [ (String,LamExpr) ] -> LamExpr -> LamExpr
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment