[Haskell-cafe] Using unsafePerformIO safely

Luke Palmer lrpalmer at gmail.com
Thu Jun 25 00:05:12 EDT 2009

On Wed, Jun 24, 2009 at 7:56 PM, Hector Guilarte <hectorg87 at gmail.com>wrote:

> Thanks for answering so fast.
> Yes, GCL == Guarded Command Language... It is for an assigment I have in my
> Languages and Machines Course.
> About the nicer/Haskellier solution you proposed: If there is a way of
> printing right in the moment the Interpreter finds the show instruction then
> I don't think the teacher is gonna like this soluttion (I would bet on that)
> so, can you (or somebody) explain a little bit better how would the ugly
> solution be? As I said earlier, I'm no expert in monads, actually the truth
> is that I know almost nothing about monads, so please, explain it to me as
> if you are explaining it to a Monads newbie...

The ugly solution is essentially to write it as an imperative program.  "IO
monad" is a fancy word for "imperative programming".

I'm beginning to think the best way to teach monads is not to (until the
student is essentially begging for them without knowing it), and let them be
grown into as the pattern is seen.  So, let's not use them.

Let's give the language a semantics of the form Tabla -> ([String], Tabla).
That means that every expression has the meaning of a function which takes a
symbol table, and outputs a series of lines and a new symbol table.  This
will be the range of your evalInstruccion.

I'll do the ShowY case and a "Sequence" case for sequencing together
multiple instructions, which is important.  The rest should be
straightforward from there.

evalInstruccion :: Instruccion -> Tabla -> ([String], Tabla)
evalInstruccion (ShowY showY) tabla = ([evalShow showY tabla], tabla)
evalInstruccion (Sequence instr1 instr2) tabla =
    let (out1, tabla')  = evalInstruccion instr1 tabla
        (out2, tabla'') = evalInstruccion instr2 tabla'
    in (out1 ++ out2, tabla'')

evalShow :: ShowY -> Tabla -> String
evalShow = ...

This pattern can be used for more than lists of strings; it can be used for
any "monoid".  If I were to follow the pattern of your code, the monoid
would be IO (), and the operation (instead of (++)) would be (>>).  But
there would still be no unsafePerformIO, the semantics of the langauge would
be Tabla -> (IO (), Tabla), and we would put build one big IO () to return
at the end from the output of the subexpressions.

Does this make sense?


> Also, can somebody explain me how would it be using the Writer Monad?
> remember is for a Monads newbie...
> Thanks a lot!
> Hector Guilarte
> On Thu, Jun 25, 2009 at 6:04 PM, Jochem Berndsen <jochem at functor.nl>wrote:
>> Hector Guilarte wrote:
>> > I made a GCL compiler using Alex and Happy and now I'm making the
>> > interpreter to that program. Here's the deal:
>> >
>> > First of all, I'm no expert in the usage of monads. Now:
>> >
>> > Whenever a "show" instruction is found in any GCL program while the
>> > interpretation is being done it is supposed to print on the stdout the
>> > string or the aritmetic expresion it was called with, so I guessed I
>> need to
>> > run an IO operation and continue the interpretation of my program. I
>> managed
>> > to do this using unsafePerformIO and `seq` like is shown below. My
>> question
>> > is: Is it safe to use it this way? So far it is working great, but I
>> need to
>> > be sure I'm using it in a "safe" way. Like I said, I'm no expert in
>> monads
>> > and the System.IO.Unsafe documentation says:
>> >
>> > "
>> > *unsafePerformIO* ::
>> > IO<
>> http://www.haskell.org/ghc/docs/latest/html/libraries/base/System-IO.html#t%3AIO
>> >a
>> > -> a
>> > This is the "back door" into the
>> > IO<
>> http://www.haskell.org/ghc/docs/latest/html/libraries/base/System-IO.html#t%3AIO
>> >monad,
>> > allowing
>> > IO<
>> http://www.haskell.org/ghc/docs/latest/html/libraries/base/System-IO.html#t%3AIO
>> >computation
>> > to be performed at any time. For this to be safe, the
>> > IO<
>> http://www.haskell.org/ghc/docs/latest/html/libraries/base/System-IO.html#t%3AIO
>> >computation
>> > should be free of side effects and independent of its
>> > environment.
>> > "
>> >
>> > I don't know if the IO computation I'm doing is free of side effects and
>> > independent of its enviroment :s. (is just hPutStr stdout ....)
>> Well, writing to the standard output is certainly a side effect. (This
>> does not mean that you cannot use unsafePerformIO. The compiler,
>> however, may assume that any value is free from side effects. This means
>> that you could get, in theory, less or more output from your program
>> than you want. In this sense it is not "safe".)
>> > Also I've read something about my code not being executed for sure or
>> > something like that. Can somebody check the code and tell me if I'm
>> "safe"
>> > with it?
>> It's "safe" in the sense that it probably won't blow up your computer.
>> It may also work. On the other hand, I would not recommend using
>> unsafePerformIO in this way.
>> I see two possibilities for resolving this issue:
>> * (ugly) run your GCL (Guarded Command Language?) interpreter in the IO
>> monad, and using "print"/"putStr"/... whenever you encounter a 'show'
>> statement in the GCL program.
>> * (nicer/Haskellier) adapt your interpreter such that it returns a list
>> of Strings to output. You have then a purely functional interpreter, and
>> in the main function of your program you can print this list. This will
>> be lazily evaluated as the GCL program runs. You now have a very nice
>> separation of clean, pure code, and impure code in the IO monad (your
>> "main" function, which can be pretty small in your case). To avoid
>> boilerplate, you can use the Writer monad, for example, but others may
>> have better suggestions.
>> Kind regards,
>> --
>> Jochem Berndsen | jochem at functor.nl
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090624/9542fa39/attachment.html

More information about the Haskell-Cafe mailing list