25.10.2015 Views

Write You a Haskell Stephen Diehl

1kEcQTb

1kEcQTb

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

process :: String -> IO ()<br />

process line = do<br />

let res = parseExpr line<br />

case res of<br />

Left err -> print err<br />

Right ex -> print $ runEval ex<br />

main :: IO ()<br />

main = runInputT defaultSettings loop<br />

where<br />

loop = do<br />

minput ”<br />

case minput of<br />

Nothing -> outputStrLn ”Goodbye.”<br />

Just input -> (liftIO $ process input) >> loop<br />

Soundness<br />

Great, now let’s test our little interpreter and indeed we see that it behaves as expected.<br />

Arith> succ 0<br />

succ 0<br />

Arith> succ (succ 0)<br />

succ (succ 0)<br />

Arith> iszero 0<br />

true<br />

Arith> if false then true else false<br />

false<br />

Arith> iszero (pred (succ (succ 0)))<br />

false<br />

Arith> pred (succ 0)<br />

0<br />

Arith> iszero false<br />

Cannot evaluate<br />

Arith> if 0 then true else false<br />

Cannot evaluate<br />

Oh no, our calculator language allows us to evaluate terms which are syntactically valid but semantically<br />

meaningless. We’d like to restrict the existence of such terms since when we start compiling our languages<br />

42

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!