On 28-Apr-2000, Erik Meijer <[EMAIL PROTECTED]> wrote:
> Hi Jan,
> 
> >     When can I safely cheat haskell compiler/interpreter
> >     by pretending that I perform pure computations,
> >     when in fact they are not?
> 
> If the computation is not pure, you cannot pretend it is.

Indeed.  And if the computation were pure, then why would it have `IO'
in its type in the first place? ;-)

> >     Here is a real example,
> >     from my Md5Digest module which works fine in Hugs:
> >
> >     digest :: String -> String
> >     digest string
> >             = unsafePerformIO (
> >                 marshall_string_ string   >>= \x1 ->
> >                 prim_Md5Digest_digest x1  >>= \x2 ->
> >             unmarshall_string_ x2     >>= \x3 ->
> >                 return x3
> >               )
> 
> I gues that for digest it holds that
> 
>   s1 == s2
>   ==>
>   digest s1 == digest s2
> 
> The only reason that the underlying function is impure is that is allocates
> memory to marshall its argument and then calls a C function. These
> side-effects don't influence the result of computing digest

I'm not familiar with Jan's Md5Digest module, or the various functions
that it calls.  But I remain unconvinced that your argument above
need hold in general.  Certainly there is nothing in the Haskell 98 Report
that guarantees it, and last time I looked there was nothing in the
ghc manual that guarantees it either.

A Haskell implementation is free to assume that something of type
`String' has no side effects at all (not just no side effects that
alter the return value), and it may perform optimizations that rely on
this.  If you break that invariant, then the compiler may break your
program.

No doubt it works with the current version of ghc, but who knows what
optimizations some future version of ghc may have?

If implementations are to provide `unsafePerformIO', then really
they ought to document when it can safely be used.  Current
implementations don't do an adequate job of that, AFAIK.

-- 
Fergus Henderson <[EMAIL PROTECTED]>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger [EMAIL PROTECTED]        |     -- the last words of T. S. Garp.

Reply via email to