Eval optimization! Tests for demos
All checks were successful
Test, Build, and Release / test (push) Successful in 1m30s
Test, Build, and Release / build (push) Successful in 1m26s

This commit is contained in:
James Eversole
2025-01-25 09:18:13 -06:00
parent 1f5a910fb2
commit 2bd388c871
6 changed files with 70 additions and 45 deletions

View File

@ -24,7 +24,7 @@ evalSingle env term
| SVar name <- term =
case Map.lookup name env of
Just v -> Map.insert "__result" v env
Nothing -> errorWithoutStackTrace $ "Variable " ++ name ++ " not defined"
Nothing -> errorWithoutStackTrace $ "Variable `" ++ name ++ "` not defined"
| otherwise =
Map.insert "__result" (evalAST env term) env
@ -59,11 +59,13 @@ evalAST env term
elimLambda :: TricuAST -> TricuAST
elimLambda = go
where
go (SLambda [v] (SApp f (SVar x)))
| v == x && not (isFree v f) = elimLambda f
go (SLambda (v:vs) body)
| null vs = toSKI v (elimLambda body)
| otherwise = elimLambda (SLambda [v] (SLambda vs body))
go (SApp f g) = SApp (elimLambda f) (elimLambda g)
go x = x
| null vs = toSKI v (elimLambda body)
| otherwise = elimLambda (SLambda [v] (SLambda vs body))
go (SApp f g) = SApp (elimLambda f) (elimLambda g)
go x = x
toSKI x (SVar y)
| x == y = _I
@ -73,7 +75,6 @@ elimLambda = go
| otherwise = SApp (SApp _S (toSKI x n)) (toSKI x u)
toSKI x t
| not (isFree x t) = SApp _K t
| otherwise = SApp (SApp _S (toSKI x t)) TLeaf
_S = parseSingle "t (t (t t t)) t"
_K = parseSingle "t t"