Small change to make it easier to read

This commit is contained in:
2025-11-25 19:16:00 +01:00
parent 8312f289c8
commit f1cf75fced

View File

@ -10,14 +10,14 @@ import Control.Monad.Identity ( Identity( runIdentity ) )
subst :: Variable -> Exp -> Exp -> Exp
subst var e = \case
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
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
where
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
eval :: Exp -> Exp
@ -39,7 +39,7 @@ eval = runIdentity . eval'
x -> pure x
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) =
if c == c'
then pure (bs,e)