[Haskell-cafe] Using unsafePerformIO safely
Hector Guilarte
hectorg87 at gmail.com
Thu Jun 25 01:41:07 EDT 2009
On Fri, Jun 26, 2009 at 12:58 AM, Brandon S. Allbery KF8NH <
allbery at ece.cmu.edu> wrote:
> On Jun 26, 2009, at 00:43 , Hector Guilarte wrote:
>
> Thanks! Actually, if I understood well what you proposed, that's how I
> first tought of doing it, but with a [Maybe String] and only append whenever
> I actually had a (Just string), but as I said before, I don't think my
> teacher is gonna like that solution since it is not going to print when the
> interpreter finds the show instruction in the GCL code, it is gonna wait
> until it finishes
>
>
> I think maybe you don't quite have a grasp of lazy evaluation yet. Try it.
>
> Also, nobody has told me why I shouldn't just use my original solution
> using unsafePerformIO, is it really bad? is it dangerous? why is it
> "unsafe"?
>
>
> You were told earlier:
>
> 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".)
>>>>
>>>
> Because pure (i.e. non-IO) values by definition never change, the compiler
> is free to assume that it can do them exactly once and remember the result.
> This means that it is possible for the compiler to evaluate your
> unsafePerformIO once and never again... or, more likely, to notice that the
> pure result is always () (because unsafePerformIO "hides" the IO from the
> compiler) and optimize it away completely (the "do it exactly once" being at
> compile time instead of run time).
>
Ok, I got it this time, thanks! I should really talk this with my teacher.
I'll post whatever he tells me... Let's hope he lets me just acumulate all
the strings and print them in the end.
Thanks again!
Hector Guilarte
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090625/8bb20b4d/attachment.html
More information about the Haskell-Cafe
mailing list