On Sat, 29 Apr 2000, Fergus Henderson wrote:

> On 28-Apr-2000, Jan Skibinski <[EMAIL PROTECTED]> wrote:
> > 
> >     When can I safely cheat haskell compiler/interpreter
> >     by pretending that I perform pure computations,
> >     when in fact they are not?
> 
> That depends on what degree of safety and portability you want.
> If you want the greatest degree of both of those, then currently
> the only safe answer is "never".  The Haskell 98 Report does not
> standardize `unsafePerformIO', and so there are no guarantees
> about whether future implementations will have such a function,
> or what it would do, or when it would be safe.


        That's the best answer I got. It should be framed.

        If my implementation currently cheats a bit but
        it works in Hugs, that's my responsibility and
        my possible future headaches. Period.

 
Lennart wrote:

> So, currentSecond is not safe to the kind of compiler optimizations
> that a good Haskell compiler can do.
> You can't make a working currentSecond if you don't involve IO in the
> type, that's just the way it is.

        Just out of curiosity: Is your compiler clever enough
        to do just what you said? Another words, would this
        attached code fail to produce random nonce string (
        the idea apparently criticized by Erik, but I do not care
        where this came from. It works fine in Hugs-98,
        February 2000 release). Humor me please :-)

        nonce :: Int -> String
        nonce size
            = take size (filter isAlpha
               (randoms $ mkStdGen (fst $ unsafePerformIO timeFrom1970)))


        timeFrom1970 :: IO (Int, Int)
        -- you can simulate it somehow, but
        -- source code is available to all
        -- at www.numeric-quest.com/haskell/bridge/

------------------------------------------------------------

Erik, Frank and Nigel:

        I appreciate your answers too. Please try to understand
        that I am searching for clear answers about the limits
        of usage of "unsafePerformIO". I do not try to
        "outsmart compilers".

        I often do just that in my other life - rocking a boat
        a little. Really! When I take a dinghy for my
        first sail, when the weather is warm, wind gusty,
        no baggage and children on boat then I drive her
        to her limits to gain some confidence of what can
        I do with her.

        Jan Skibinski




Reply via email to