From ae59f17cb3d58de7afca7f441d8bcf9a718e024c Mon Sep 17 00:00:00 2001 From: Arnaud Bailly Date: Wed, 5 Nov 2025 09:15:46 +0100 Subject: handle defitions in untyped language --- lambda-calcul/haskell/src/Minilang/REPL/Run.hs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'lambda-calcul/haskell/src/Minilang/REPL/Run.hs') diff --git a/lambda-calcul/haskell/src/Minilang/REPL/Run.hs b/lambda-calcul/haskell/src/Minilang/REPL/Run.hs index bc552cb..a98ff41 100644 --- a/lambda-calcul/haskell/src/Minilang/REPL/Run.hs +++ b/lambda-calcul/haskell/src/Minilang/REPL/Run.hs @@ -7,13 +7,14 @@ module Minilang.REPL.Run where import Control.Monad.Catch +import Data.Bifunctor (second) import Data.Text hiding (replicate) import Minilang.Lambda (loadProgram) -import Minilang.Lambda.DeBruijn (eval, fromDeBruijn, toDeBruijn) +import Minilang.Lambda.DeBruijn (eval, eval', fromDeBruijn, toDeBruijn) import Minilang.Lambda.Eval (newEnv) import Minilang.Lambda.Infer (infer, newContext) import qualified Minilang.Lambda.Infer as Infer -import Minilang.Lambda.Parser (desugar, parse, resugar) +import Minilang.Lambda.Parser (AST (..), desugar, parse, resugar) import Minilang.Lambda.Unify (Type (TyVar)) import Minilang.REPL.Types @@ -35,12 +36,25 @@ handleUserInput :: Text -> m () handleUserInput txt = do - REPLEnv {language} <- getEnv + repl@REPLEnv {language, env} <- getEnv case parse txt of Left err -> output (Msg $ pack $ show err) + Right (Def ident body) -> do + let term = desugar body + case language of + Typed -> + case infer term of + Left typeError -> output (Msg $ Infer.render typeError) + Right typ -> do + setEnv $ repl {env = (ident, term) : env} + output (Defined ident typ) + Untyped -> do + setEnv $ repl {env = (ident, term) : env} + output (Defined ident (TyVar "")) Right expr -> do let term = desugar expr - v = fromDeBruijn $ eval (toDeBruijn term) + env' = second toDeBruijn <$> env + v = fromDeBruijn $ eval' env' (toDeBruijn term) ast = resugar v case language of Typed -> -- cgit v1.3.1