From 8312f289c8aaeb5e1692f7213b7730611c32300e Mon Sep 17 00:00:00 2001 From: pingu Date: Tue, 25 Nov 2025 18:18:19 +0100 Subject: [PATCH] Tehee --- .../Main.hs => Interpreter/Haskell.hs} | 5 +-- 3or4/Interpreter/Self.hs | 42 +++++++++++++++++++ 3or4/app/Main.hs | 8 ++++ 3or4/chi.cabal | 5 ++- 3or4/interpreter/Self.hs | 8 ---- 5 files changed, 55 insertions(+), 13 deletions(-) rename 3or4/{interpreter/Main.hs => Interpreter/Haskell.hs} (95%) create mode 100644 3or4/Interpreter/Self.hs create mode 100644 3or4/app/Main.hs delete mode 100644 3or4/interpreter/Self.hs diff --git a/3or4/interpreter/Main.hs b/3or4/Interpreter/Haskell.hs similarity index 95% rename from 3or4/interpreter/Main.hs rename to 3or4/Interpreter/Haskell.hs index f2e4733..4ac06a5 100644 --- a/3or4/interpreter/Main.hs +++ b/3or4/Interpreter/Haskell.hs @@ -1,5 +1,6 @@ {-# Language LambdaCase, Strict #-} -module Main where + +module Interpreter.Haskell where import Chi import Data.Functor ( (<&>) ) @@ -45,5 +46,3 @@ eval = runIdentity . eval' else lookupBranch c brs -main :: IO () -main = getLine >>= print . eval . parse diff --git a/3or4/Interpreter/Self.hs b/3or4/Interpreter/Self.hs new file mode 100644 index 0000000..2626b17 --- /dev/null +++ b/3or4/Interpreter/Self.hs @@ -0,0 +1,42 @@ +{-# Language LambdaCase, Strict #-} + +module Interpreter.Self where + +import Chi +import Interpreter.Haskell + +mapExp :: Exp +mapExp = parse + "\\f. rec map = \\xs. case xs of \ + \ { Nil() -> Nil() \ + \ ; Cons(x,xs) -> Cons(f x, map xs)\ + \ }" + +substBrExp :: Exp +substBrExp = parse + "rec substBr = \\x. " + +ifExp :: Exp +ifExp = parse + "\\b. \\t. \\f. case b of \ + \ { True() -> t \ + \ ; False() -> f \ + \ }" + +equalExp :: Exp +equalExp = parse "" + +substExp :: Exp +substExp = + subst (Variable "if") ifExp . + subst (Variable "equal") equalExp . + subst (Variable "substBr") substBrExp . + subst (Variable "map") mapExp $ parse + "\\var. \\e. rec subst = \\x. case x of\ + \ { Apply(e1,e2) -> Apply(subst e1, subst e2) \ + \ ; Lambda(x,y) -> if (equal x var) Lambda(x,y) Lambda(x,subst y) \ + \ ; Var(x) -> if (equal x var) e Var(x) \ + \ ; Const(c,es) -> Const(c, map subst es) \ + \ ; Rec(x, e') -> if (equal x var) Rec(x,e') Rec(x,subst e')\ + \ ; Case(e',bs) -> _ \ + \ }" diff --git a/3or4/app/Main.hs b/3or4/app/Main.hs new file mode 100644 index 0000000..de5a658 --- /dev/null +++ b/3or4/app/Main.hs @@ -0,0 +1,8 @@ +{-# Language LambdaCase, Strict #-} +module Main where + +import Chi +import Interpreter.Haskell + +main :: IO () +main = getLine >>= print . eval . parse diff --git a/3or4/chi.cabal b/3or4/chi.cabal index 6de4f5e..42807f4 100644 --- a/3or4/chi.cabal +++ b/3or4/chi.cabal @@ -24,14 +24,15 @@ library LexChi ParChi PrintChi + Interpreter.Haskell + Interpreter.Self hs-source-dirs: . executable interpreter main-is: Main.hs hs-source-dirs: - interpreter - other-modules: Self + app build-depends: base , chi , mtl diff --git a/3or4/interpreter/Self.hs b/3or4/interpreter/Self.hs deleted file mode 100644 index 127f209..0000000 --- a/3or4/interpreter/Self.hs +++ /dev/null @@ -1,8 +0,0 @@ --- | - -module Self where - -import Chi - -substExp :: Exp -substExp = parse ""