I'm not sure there was enough context in your message to be sure
that I'm answering the question you intended, but here goes:

| Here's an oddity.  Does anyone know what's going on?  - Conal
| 
| -----Original Message-----
| From: Sigbjorn Finne (Intl Vendor) 
| Sent: Wednesday, September 08, 1999 11:33 AM
| To:   Conal Elliott
| Subject:      RE: Typed vs untyped representation
| 
| No idea why that happens, someone really familiar with the Hugs
| innards would know. A simpler example is "11.0 == unsafeCoerce ()"

Here's a longer, and I hope more illustrative example:

  Dynamic> 1.0
  1.0
  Dynamic> unsafeCoerce () == 11.0
  False
  Dynamic> unsafeCoerce () == 11.0
  True
  Dynamic>

Note that the same expression gives different results in the second
and third lines ... Here's why:  Hugs has a `register' called whnfFloat
that it uses to store the value of the last floating point calculation.
When the evaluator sees something that has type Float, it is assuming
that you haven't actually passed in a value of some other type.  When
you try to evaluate "unsafeCoerce ()" as a float, the evaluator
reduces it to weak head normal form of (), which isn't a float, so it
doesn't update the register, but otherwise doesn't cause any problems.
Thus the result produced by the three examples is the same as if you
had evaluated:

  1.0          ==> 1.0
  1.0 == 11.0  ==> False
  11.0 == 11.0 ==> True

Now I've just seen a little more of the context in Alastair's message,
I see that you have a further example:

  10 == unsafeCoerce ()

What happens here is that Hugs evaluates 10, which will be defaulted to
type Integer (i.e., it's a bignum), and then evaluates "unsafeCoerce ()"
to obtain (); the implementation of equality on Integer is expecting
both arguments to be heap allocated bignums, but you've tricked it:
the second argument is a constant, (), and not a pointer into the heap.
And hence when it tries to indirect through the pointer, it blows up
with a segment violation/crash/etc.

This is the problem with allowing unsafe primitives: if you use such a
primitive, then it's up to you to ensure that it really is safe to do
so.  And if it isn't, bad things can happen.

Hope this helps!

Mark

Reply via email to