Small change to make it easier to read
This commit is contained in:
@ -10,14 +10,14 @@ import Control.Monad.Identity ( Identity( runIdentity ) )
|
|||||||
subst :: Variable -> Exp -> Exp -> Exp
|
subst :: Variable -> Exp -> Exp -> Exp
|
||||||
subst var e = \case
|
subst var e = \case
|
||||||
Apply e1 e2 -> Apply (subst var e e1) (subst var e e2)
|
Apply e1 e2 -> Apply (subst var e e1) (subst var e e2)
|
||||||
Lambda x e' -> Lambda x $ if var /= x then subst var e e' else e'
|
Lambda x e' -> Lambda x $ if var == x then e' else subst var e e'
|
||||||
Var x -> if var == x then e else Var x
|
Var x -> if var == x then e else Var x
|
||||||
Const c es -> Const c $ map (subst var e) es
|
Const c es -> Const c $ map (subst var e) es
|
||||||
Rec x e' -> Rec x $ if var /= x then subst var e e' else e'
|
Rec x e' -> Rec x $ if var == x then e' else subst var e e'
|
||||||
Case e' bs -> Case (subst var e e') $ map substBr bs
|
Case e' bs -> Case (subst var e e') $ map substBr bs
|
||||||
where
|
where
|
||||||
substBr :: Br -> Br
|
substBr :: Br -> Br
|
||||||
substBr (Branch c vs e') = Branch c vs $ if var `notElem` vs then subst var e e' else e'
|
substBr (Branch c vs e') = Branch c vs $ if var `elem` vs then e' else subst var e e'
|
||||||
|
|
||||||
-- Task 5
|
-- Task 5
|
||||||
eval :: Exp -> Exp
|
eval :: Exp -> Exp
|
||||||
@ -39,7 +39,7 @@ eval = runIdentity . eval'
|
|||||||
x -> pure x
|
x -> pure x
|
||||||
|
|
||||||
lookupBranch :: Constructor -> [Br] -> Identity ([Variable], Exp)
|
lookupBranch :: Constructor -> [Br] -> Identity ([Variable], Exp)
|
||||||
lookupBranch c [] = error "No matching branch"
|
lookupBranch c [] = error $ "No matching branch, c was: " <> show c
|
||||||
lookupBranch c ((Branch c' bs e):brs) =
|
lookupBranch c ((Branch c' bs e):brs) =
|
||||||
if c == c'
|
if c == c'
|
||||||
then pure (bs,e)
|
then pure (bs,e)
|
||||||
|
|||||||
Reference in New Issue
Block a user