Here's the context. I have a data type of syntactic expressions
(trees/dags) and the frequent need to perform equality checks. It's
very common for expressions to be not only equal, but pointer-equal, so
I want a fast eq check followed by a slower structural equality check,
as follows:
-- Equality test that first tries cheap pointer-equality
(==%) :: Exp -> Exp -> Bool
x ==% y = (x `unsafePtrEq` y) || x == y
I did exactly the same thing in Pan, and it seems a common need. It's
also a classic old Lisp trick.
- Conal
-----Original Message-----
From: Simon Peyton-Jones
Sent: Wednesday, April 24, 2002 3:15 AM
To: Conal Elliott; Ghc-Bugs
Subject: RE: More pointer-equality weirdness
Well, it says it's unsafe!
The point is that
(1::Int) `unsafePtrEq` (1::Int)
is not necessarily True. The 1 is boxed, and the interpreter
chooses to allocate two boxes while the compiler decides to just
share one. Even with the compiler, when it computes a new
Int it allocates it in a fresh box; then at GC time it looks at the
Int and redirects the pointer to a table of static Ints between +/- 100
if it's a small Int. So pointer equality may change over time.
That's what you get for pointer-equality.
I'm don't have enough context to know how to solve your problem
properly.
Simon
| -----Original Message-----
| From: Conal Elliott [mailto:[EMAIL PROTECTED]]
| Sent: 22 April 2002 22:47
| To: Ghc-Bugs
| Subject: More pointer-equality weirdness
|
|
| I'm getting some _really_ weird results under ghci from the
| unsafePtrEq I'm using (thanks to Sigbjorn). It works fine
| under ghc. I'm running 5.03.20020208 under Windows XP. Is
| there a work-around, perhaps via a different implementation
| of unsafePtrEq? - Conal
|
| module Main where
|
| import PrelGHC
|
| unsafePtrEq :: a -> a -> Bool
| unsafePtrEq a b = (unsafeCoerce# a) `eqAddr#` (unsafeCoerce# b)
|
| main = do print $ 1 `unsafePtrEq` 1
| print $ 1 `unsafePtrEq` 1 || True
|
| {- Results:
|
| bash-2.05a$ ghci TestPtrEq
| ___ ___ _
| / _ \ /\ /\/ __(_)
| / /_\// /_/ / / | | GHC Interactive, version 5.03,
| for Haskell
| 98.
| / /_\\/ __ / /___| | http://www.haskell.org/ghc/
| \____/\/ /_/\____/|_| Type :? for help.
|
| Loading package std ... linking ... done.
| Loading package lang ... linking ... done.
| Loading package concurrent ... linking ... done.
| Loading package util ... linking ... done.
| Loading package text ... linking ... done.
| Loading package data ... linking ... done.
| Compiling TestPtrEq ( TestPtrEq.hs, interpreted )
| Ok, modules loaded: TestPtrEq.
| Main> main
|
| False
| TestPtrEq>
|
| It works, however, under ghc:
|
| bash-2.05a$ ghc -fglasgow-exts -o test.exe TestPtrEq.hs
| bash-2.05a$ ./test.exe
| True
| True
|
| -}
|
| _______________________________________________
| Glasgow-haskell-bugs mailing list
| [EMAIL PROTECTED]
| http://www.haskell.org/mailman/listinfo/glasgow-| haskell-bugs
|
_______________________________________________
Glasgow-haskell-bugs mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs