From c3a175e808b279eb415bcb5dbcb9db5f34035f98 Mon Sep 17 00:00:00 2001 From: Arnaud Bailly Date: Fri, 17 Oct 2025 09:52:06 +0200 Subject: feat: add basic application to evaluate "programs" The main simply reads from its stdin, evaluates its input, and dump the result of the evaluation. --- lambda-calcul/haskell/src/Minilang/IO.hs | 32 ++++++----------------- lambda-calcul/haskell/src/Minilang/Lambda/Eval.hs | 2 +- 2 files changed, 9 insertions(+), 25 deletions(-) (limited to 'lambda-calcul/haskell/src/Minilang') diff --git a/lambda-calcul/haskell/src/Minilang/IO.hs b/lambda-calcul/haskell/src/Minilang/IO.hs index bfcb125..c468316 100644 --- a/lambda-calcul/haskell/src/Minilang/IO.hs +++ b/lambda-calcul/haskell/src/Minilang/IO.hs @@ -1,40 +1,24 @@ -{-# LANGUAGE DuplicateRecordFields #-} -{-# LANGUAGE GeneralizedNewtypeDeriving #-} -{-# LANGUAGE NamedFieldPuns #-} -{-# LANGUAGE RecordWildCards #-} -{-# LANGUAGE TypeSynonymInstances #-} -{-# OPTIONS_GHC "-fno-warn-orphans" #-} - module Minilang.IO where import qualified Data.ByteString as BS +import qualified Data.Text as Text import Data.Text.Encoding ( decodeUtf8With, encodeUtf8, ) import Data.Text.Encoding.Error (lenientDecode) -import Minilang.Lambda.Eval hiding (rho) -import Minilang.Parser -import Minilang.Type -import Prettyprinter -import Prettyprinter.Render.Text +import Minilang.Lambda.Eval (eval) import System.IO (Handle) --- | Read an `AST` from @hin@ handle, evaluate it and dump the result +-- | Read a "program" from @hin@ handle, evaluate it and dump the result -- on @hout@. runEval :: Handle -> Handle -> IO () runEval hin hout = do - programText <- decodeUtf8With lenientDecode <$> BS.hGetContents hin - let ast = parseProgram False programText - ρ = EmptyEnv - γ = EmptyContext - (ρ', _) <- loadProgram ast ρ γ - let val = eval ast ρ' + programText <- Text.unpack . decodeUtf8With lenientDecode <$> BS.hGetContents hin + let ast = read programText + env = mempty + result = eval ast env BS.hPut hout - ( encodeUtf8 - ( renderStrict $ layoutPretty defaultLayoutOptions $ pretty val - ) - <> "\n" - ) + (encodeUtf8 (Text.pack (show result) <> "\n")) diff --git a/lambda-calcul/haskell/src/Minilang/Lambda/Eval.hs b/lambda-calcul/haskell/src/Minilang/Lambda/Eval.hs index 68b01be..0829186 100644 --- a/lambda-calcul/haskell/src/Minilang/Lambda/Eval.hs +++ b/lambda-calcul/haskell/src/Minilang/Lambda/Eval.hs @@ -6,7 +6,7 @@ data Term = Var Text | Lam Text Term | App Term Term - deriving (Show, Eq) + deriving (Show, Read, Eq) type Env = [(Text, Term)] -- cgit v1.2.3