From b87d5b45110cd7efe7109dd6d4e9c19ef28522aa Mon Sep 17 00:00:00 2001 From: Arnaud Bailly Date: Thu, 6 Nov 2025 08:35:36 +0100 Subject: evaluate terms when defining them --- lambda-calcul/haskell/src/Minilang/Lambda.hs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'lambda-calcul/haskell/src/Minilang/Lambda.hs') diff --git a/lambda-calcul/haskell/src/Minilang/Lambda.hs b/lambda-calcul/haskell/src/Minilang/Lambda.hs index d223658..ff636ce 100644 --- a/lambda-calcul/haskell/src/Minilang/Lambda.hs +++ b/lambda-calcul/haskell/src/Minilang/Lambda.hs @@ -3,7 +3,7 @@ module Minilang.Lambda where import Data.Bifunctor (second) import Minilang.Lambda.DeBruijn (eval, eval', fromDeBruijn, toDeBruijn) import Minilang.Lambda.Eval (Env, Term) -import Minilang.Lambda.Infer (Context, TypeError, infer) +import Minilang.Lambda.Infer (Context, TypeError, extendContext, infer) import Minilang.Lambda.Parser (AST (..), desugar) import Minilang.Lambda.Unify (Type (..)) import Minilang.REPL.Types (Language (..)) @@ -38,24 +38,28 @@ loadProgram language env context exprs = exprs runExpr language (Def ident body) env context = - let term = desugar body + let term = evalInEnv env body in case language of Typed -> case infer term of Left typeError -> Left typeError Right typ -> - Right (Nothing, (ident, term) : env, context) + Right (Nothing, (ident, term) : env, extendContext ident typ context) Untyped -> Right (Nothing, (ident, term) : env, context) runExpr language expr env context = - let term = desugar expr - env' = second toDeBruijn <$> env - v = fromDeBruijn $ eval' env' (toDeBruijn term) + let v = evalInEnv env expr in case language of Typed -> - case infer term of + case infer v of Left typeError -> Left typeError Right typ -> Right (Just (v, typ), env, context) Untyped -> Right (Just (v, TyVar ""), env, context) + +evalInEnv :: Env -> AST -> Term +evalInEnv env expr = + let term = desugar expr + env' = second toDeBruijn <$> env + in fromDeBruijn $ eval' env' (toDeBruijn term) -- cgit v1.3.1