aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Reducer
diff options
context:
space:
mode:
authorMarvin Borner2024-03-02 11:21:44 +0100
committerMarvin Borner2024-03-02 11:21:44 +0100
commiteff903fc61b060b6333cb60bfced95e44da000ba (patch)
treeb99fb2070843ed33f4449c0c9d7777d2db337e79 /src/Reducer
parentfe951a51daa805f1abb9a973bbe749888d8f9a83 (diff)
Started non-outsourced optimizer
Diffstat (limited to 'src/Reducer')
-rw-r--r--src/Reducer/HigherOrder.hs13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/Reducer/HigherOrder.hs b/src/Reducer/HigherOrder.hs
index 91378b7..1699c87 100644
--- a/src/Reducer/HigherOrder.hs
+++ b/src/Reducer/HigherOrder.hs
@@ -9,6 +9,11 @@ import Helper
data HigherOrder = HigherOrderBruijn Int | HigherOrderAbstraction (HigherOrder -> HigherOrder) | HigherOrderApplication HigherOrder HigherOrder
data NamedTerm = NamedVariable Int | NamedAbstraction Int NamedTerm | NamedApplication NamedTerm NamedTerm
+(!?) :: [a] -> Int -> Maybe a
+(!?) [] _ = Nothing
+(!?) (x : _ ) 0 = Just x
+(!?) (_ : xs) i = xs !? (i - 1)
+
app :: HigherOrder -> HigherOrder -> HigherOrder
app (HigherOrderAbstraction f) = f
app f = HigherOrderApplication f
@@ -16,7 +21,9 @@ app f = HigherOrderApplication f
eval :: Expression -> HigherOrder
eval = go []
where
- go env (Bruijn x ) = env !! x
+ go env (Bruijn x) = case env !? x of
+ Just v -> v
+ _ -> HigherOrderBruijn x
go env (Abstraction e ) = HigherOrderAbstraction $ \x -> go (x : env) e
go env (Application e1 e2) = app (go env e1) (go env e2)
go _ _ = invalidProgramState
@@ -32,7 +39,9 @@ toNamedTerm = go 0
resolveExpression :: NamedTerm -> Expression
resolveExpression = resolve []
where
- resolve vs (NamedVariable i ) = Bruijn $ vs !! i
+ resolve vs (NamedVariable i) = Bruijn $ case vs !? i of
+ Just v -> v
+ _ -> i
resolve vs (NamedAbstraction v t) = Abstraction $ resolve (v : vs) t
resolve vs (NamedApplication l r) = Application (resolve vs l) (resolve vs r)