unsafePtrCompare, anybody?

Leon Smith lps@po.cwru.edu
Tue, 18 Sep 2001 10:16:04 -0400


--------------Boundary-00=_SM3V1FS8RCJUQEV4UXOW
Content-Type: text/plain;
  charset="iso-8859-1"
Content-Transfer-Encoding: 8bit

> An exact criterion would require a formal semantics for Haskell, which
> we don't have.  But informally, if a function which is implemented using
> unsafePerformIO is pure - that is, its result depends only on the values
> of its arguments and possibly its free variables - then that is a "safe"
> use of unsafePerformIO.

I'm not sure I completely understand you here.  Obviously, these free 
variables must also include mutable variables, as mkFastString employs these, 
and the side-effects can be seen from within the module itself.  

> > "unsafePerformIO" is used in the implementation of
> > mkFastString, so how is it's side effects "safe".
>
> It is safe because the side effects aren't visible outside the
> implementation of mkFastString.

My intuition says that none of the side-effects in my implementation are 
visible from the abstract level of the module.  However, the use of 
unsafePerformIO certainly modifies the behaviour of the module.  For example, 
the following definitions at the beginning of a GHCi session on the attached 
code lead to the subsequent behaviour:


foo1 <- return (unsafePerformIO (mkAtom "foo"))
foo2 <- return (unsafePerformIO (mkAtom "foo"))
bar  <- return (unsafePerformIO (mkAtom "bar"))
safefoo1 <- mkAtom "foo"
safefoo2 <- mkAtom "foo"
safebar  <- mkAtom "bar"
list <- return [safefoo1, safefoo2, safebar, foo1, foo2, bar]


Atom> [x == y| x <- list, y <- list]
[True ,True ,False,False,False,False,
 True ,True ,False,False,False,False,
 False,False,True ,False,False,False,
 False,False,False,True ,False,False,
 False,False,False,False,True ,False,
 False,False,False,False,False,True  ]

As can be seen from above, the only times that (==) returns true, aside from 
reflexive cases, is when  (safefoo1 == safefoo2) and (safefoo2 == safefoo1).  

I've also played with the order of atom creation, and it doesn't appear to 
have any effect on the truth table.

best,
leon
--------------Boundary-00=_SM3V1FS8RCJUQEV4UXOW
Content-Type: text/plain;
  charset="iso-8859-1";
  name="Atom.hs"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="Atom.hs"

bW9kdWxlIEF0b20KICAgICggICBBdG9tKCkKICAgICwgICBta0F0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