Search code examples
haskellinterpreterlanguage-design

How to bring an interpreter to the IO monad?


My question relates to the simple interpreter written in in this answer

How could one add IO capabilities to this interpreter (the first non monadic version)? By this I simply mean adding a statement that uses putStrLn. I'm not that well versed in Haskell yet, but I'm guessing you can just combine the IO monad somehow. Can somebody point me in the right direction?

data Stmt
  = Var := Exp                                   
  | While Exp Stmt                                               
  | Seq [Stmt]      
  | Print Exp       -- a print statement

Solution

  • You can make your interpreter have a result in the IO monad:

    exec :: Stmt -> Store -> IO Store
    

    Now the exec function can do anything in IO. The existing operations will need to be modified accordingly.