Reduce duplication of elimLambda calls

This commit is contained in:
James Eversole 2025-01-20 15:16:27 -06:00 committed by James Eversole
parent e6e05b607a
commit b385349197
2 changed files with 7 additions and 7 deletions

View File

@ -2,7 +2,7 @@
## Introduction ## Introduction
tricu (pronounced like "tree-shoe" in English) is a purely functional interpreted language implemented in Haskell. [I'm](https://eversole.co) developing tricu to further research the possibilities offered by the various forms of [Tree Calculi](https://github.com/barry-jay-personal/typed_tree_calculus/blob/main/typed_program_analysis.pdf). tricu (pronounced "tree-shoe") is a purely functional interpreted language implemented in Haskell. [I'm](https://eversole.co) developing tricu to further research the possibilities offered by the various forms of [Tree Calculi](https://github.com/barry-jay-personal/typed_tree_calculus/blob/main/typed_program_analysis.pdf).
tricu offers minimal syntax sugar yet manages to provide a complete, intuitive, and familiar programming environment. There is great power in simplicity. tricu offers: tricu offers minimal syntax sugar yet manages to provide a complete, intuitive, and familiar programming environment. There is great power in simplicity. tricu offers:

View File

@ -11,11 +11,10 @@ import qualified Data.Set as Set
evalSingle :: Env -> TricuAST -> Env evalSingle :: Env -> TricuAST -> Env
evalSingle env term evalSingle env term
| SFunc name [] body <- term = | SFunc name [] body <- term =
let res = evalAST env $ elimLambda body let res = evalAST env body
in Map.insert "__result" res (Map.insert name res env) in Map.insert "__result" res (Map.insert name res env)
| SLambda _ body <- term = Map.insert "__result" (evalAST env body) env
| SApp func arg <- term = Map.insert "__result" | SApp func arg <- term = Map.insert "__result"
(apply (evalAST env $ elimLambda func) (evalAST env $ elimLambda arg)) env (apply (evalAST env func) (evalAST env arg)) env
| SVar name <- term = case Map.lookup name env of | SVar name <- term = case Map.lookup name env of
Just v -> Map.insert "__result" v env Just v -> Map.insert "__result" v env
Nothing -> errorWithoutStackTrace $ "Variable " ++ name ++ " not defined" Nothing -> errorWithoutStackTrace $ "Variable " ++ name ++ " not defined"
@ -27,12 +26,13 @@ evalTricu env list = evalTricu' env (filter (/= SEmpty) list)
evalTricu' :: Env -> [TricuAST] -> Env evalTricu' :: Env -> [TricuAST] -> Env
evalTricu' env [] = env evalTricu' env [] = env
evalTricu' env [s] = evalTricu' env [s] =
let updatedEnv = evalSingle env $ elimLambda s let updatedEnv = evalSingle env s
in Map.insert "__result" (result updatedEnv) updatedEnv in Map.insert "__result" (result updatedEnv) updatedEnv
evalTricu' env (x:xs) = evalTricu (evalSingle env $ elimLambda x) xs evalTricu' env (x:xs) = evalTricu (evalSingle env x) xs
evalAST :: Env -> TricuAST -> T evalAST :: Env -> TricuAST -> T
evalAST env term evalAST env term
| SLambda _ _ <- term = evalAST env (elimLambda term)
| SVar name <- term = evalVar name | SVar name <- term = evalVar name
| TLeaf <- term = Leaf | TLeaf <- term = Leaf
| TStem t <- term = Stem (evalAST env t) | TStem t <- term = Stem (evalAST env t)