Capstone B: the REPL
A loop over getLine
The REPL reads one line, evaluates it and prints the value or the error, and it stops on :quit with a count of the errors. The parser and the evaluator are the ones of the previous lessons; the new part is the recursion in IO that keeps the loop alive.
The loop is a function of the error count: it reads a line, matches it against :quit, the empty line and an expression, and calls itself with the new count. The accumulator errors is an ordinary argument of the loop and not a cell, so the count is a fold over the lines the session read. A bad line does not end the session, which is the point of a REPL.
The program
module Main (main) where import Data.Char (isDigit) data Token = TNum Int | TPlus | TMinus | TStar | TSlash | TLParen | TRParen deriving (Show, Eq) data Expr = Num Int | Add Expr Expr | Sub Expr Expr | Mul Expr Expr | Div Expr Expr deriving (Show, Eq) lexer :: String -> [Token] lexer [] = [] lexer (c : cs) | c == ' ' = lexer cs | c `elem` "+-*/()" = symbol c : lexer cs | isDigit c = let (digits, rest) = span isDigit (c : cs) in TNum (read digits) : lexer rest | otherwise = error ("bad character: " ++ [c]) where symbol '+' = TPlus symbol '-' = TMinus symbol '*' = TStar symbol '/' = TSlash symbol '(' = TLParen symbol ')' = TRParen symbol other = error ("bad symbol: " ++ [other]) parseExpr :: [Token] -> Either String (Expr, [Token]) parseExpr tokens = do (left, rest) <- parseTerm tokens restExpr left rest where restExpr left (TPlus : rest) = do (right, rest') <- parseTerm rest restExpr (Add left right) rest' restExpr left (TMinus : rest) = do (right, rest') <- parseTerm rest restExpr (Sub left right) rest' restExpr left rest = Right (left, rest) parseTerm :: [Token] -> Either String (Expr, [Token]) parseTerm tokens = do (left, rest) <- parseFactor tokens restTerm left rest where restTerm left (TStar : rest) = do (right, rest') <- parseFactor rest restTerm (Mul left right) rest' restTerm left (TSlash : rest) = do (right, rest') <- parseFactor rest restTerm (Div left right) rest' restTerm left rest = Right (left, rest) parseFactor :: [Token] -> Either String (Expr, [Token]) parseFactor (TNum n : rest) = Right (Num n, rest) parseFactor (TLParen : rest) = do (expr, rest') <- parseExpr rest case rest' of TRParen : rest'' -> Right (expr, rest'') _ -> Left "expected )" parseFactor [] = Left "unexpected end of input" parseFactor (token : _) = Left ("unexpected token " ++ show token) parseAll :: String -> Either String Expr parseAll input = do (expr, rest) <- parseExpr (lexer input) case rest of [] -> Right expr _ -> Left "unexpected input after the expression" eval :: Expr -> Either String Int eval (Num n) = Right n eval (Add a b) = (+) <$> eval a <*> eval b eval (Sub a b) = (-) <$> eval a <*> eval b eval (Mul a b) = (*) <$> eval a <*> eval b eval (Div a b) = do x <- eval a y <- eval b if y == 0 then Left "division by zero" else Right (x `div` y) main :: IO () main = loop (0 :: Int) where loop errors = do line <- getLine case line of ":quit" -> putStrLn ("bye, " ++ show errors ++ " errors") "" -> loop errors _ -> case parseAll line >>= eval of Left message -> do putStrLn ("error: " ++ message) loop (errors + 1) Right value -> do print value loop errors
ghc -Wall -O1 -threaded -o repl repl.hs
printf '1+2\n2*3\n1/0\n:quit\n' | ./repl
3
6
error: division by zero
bye, 1 errors
Reading the session
- Two good lines print their values, in the order they were typed.
- The division by zero prints the error and does not stop the loop, and the count at the end is the proof.
- The input is piped, so the program sees the lines as a file and the session ends at :quit like a typed one.
The next lesson tests the whole pipeline, parser and evaluator together, with a table of expressions and the values they must give.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What happens on a bad line in the REPL?
Answer
It prints the error, counts it and continues
A REPL stays alive after an error, which is why the error is a value.
-
How is the error count kept?
Answer
As an argument of the loop
The loop passes the number to its next call.
-
What ends the session?
Answer
:quit
The literal :quit is the command and prints the count.
-
What does this program print?
module Main (main) where main :: IO () main = print (2 + 2 :: Int)Answer
4
The sum is printed without quotes.
-
Why does an empty line do nothing?
Answer
The loop matches it and starts again without evaluating
The empty string is a case of its own in the loop.
-
What does this program print?
module Main (main) where main :: IO () main = print (1 / 0)Answer
Infinity
The literal defaults to Double and 1/0 in Double is Infinity; the evaluator uses div, which fails instead.
-
What is printed at the end of the session?
Answer
bye and the number of errors
The :quit branch prints the count it carried.
-
What does this program print?
module Main (main) where main :: IO () main = print (length ["a", "b"])Answer
2
The list has two elements.
-
Why is the loop recursive in IO?
Answer
Each line is an effect and the next line needs the loop again
The session is a sequence of actions, one per line.
-
What does this program print?
module Main (main) where main :: IO () main = putStrLn (show (3 * 3 :: Int))Answer
9
putStrLn prints the shown product without quotes.
The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev