From 5a911ff0009d353cd278cc3371d50f54f5742a5c Mon Sep 17 00:00:00 2001 From: pingu Date: Tue, 25 Nov 2025 19:47:14 +0100 Subject: [PATCH] Baba --- 3or4/Interpreter/Self.hs | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/3or4/Interpreter/Self.hs b/3or4/Interpreter/Self.hs index 474da97..b09ea02 100644 --- a/3or4/Interpreter/Self.hs +++ b/3or4/Interpreter/Self.hs @@ -5,6 +5,8 @@ module Interpreter.Self where import Chi import Interpreter.Haskell +-- Task 3 + mapExp :: Exp mapExp = parse "\\f. rec map = \\xs. case xs of \ @@ -57,3 +59,43 @@ substExp = \ ; Rec(x, e') -> if (equal x var) Rec(x,e') Rec(x,subst e')\ \ ; Case(e',bs) -> Case(subst e', map (substBr subst var) bs) \ \ }" + +-- Task 4 + +lookupExp :: Exp +lookupExp = + subst (Variable "if") ifExp . + subst (Variable "equal") equalExp $ parse + "\\c. rec lookup = \\xs. case xs of \ + \ -- TODO: Fix for Nil() \ + \ { Cons(b,bs) -> \ + \ case b of \ + \ { Branch(c', bs, e') -> if (equal c c') Pair(bs,e') (lookup bs) } \ + \ }" + +substsExp :: Exp +substsExp = + subst (Variable "subst") substExp $ parse + "rec substs = \\xs. \\vs. \\e'. case xs of \ + \ { Nil() -> case vs of { Nil() -> e' } \ + \ ; Cons(x,xs) -> case vs of { Cons(v,vs) -> subst x v (substs xs vs e') }\ + \ }" + +evalExp :: Exp +evalExp = + subst (Variable "map") mapExp . + subst (Variable "lookup") lookupExp . + subst (Variable "substs") substsExp . + subst (Variable "subst") substExp $ parse + "rec eval = \\e. case e of \ + \ { Lambda(x,e) -> Lambda(x,e) \ + \ ; Apply(e1,e2) -> case eval e1 of \ + \ { Lambda(x,e) -> eval (subst x (eval e2) e) } \ + \ ; Rec(x,e) -> eval (subst x Rec(x,e) e) \ + \ ; Const(c,es) -> Const(c, map eval es) \ + \ ; Case(e,bs) -> case eval e of \ + \ { Const(c,vs) -> case lookup c bs of \ + \ { Pair(xs,e') -> eval (substs xs vs e') } \ + \ } \ + \ ; Var(x) -> Var(x) \ + \ }"