
Is there in Haskell a non monadic function of type a -> a -> Bool which test for physical equality of two values? It would return True if only if both values are the same object in memory. For instance: value1 = "good" value2 = "good" eq value1 value2 => False value1 = "good" value2 = value1 eq value1 value2 => True Romildo

2010/6/28 José Romildo Malaquias
Is there in Haskell a non monadic function of type a -> a -> Bool which test for physical equality of two values? It would return True if only if both values are the same object in memory.
For instance:
value1 = "good" value2 = "good"
eq value1 value2 => False
value1 = "good" value2 = value1
eq value1 value2 => True
Not exactly what you ask for, but quite close: http://www.haskell.org/ghc/docs/6.12.1/html/libraries/base/System-Mem-Stable... Cheers, Thu

On 28 Jun 2010, at 09:38, José Romildo Malaquias wrote:
Is there in Haskell a non monadic function of type a -> a -> Bool which test for physical equality of two values? It would return True if only if both values are the same object in memory.
For instance:
value1 = "good" value2 = "good"
eq value1 value2 => False
value1 = "good" value2 = value1
eq value1 value2 => True
This simply isn't possible without manually tagging values yourself (or with a library), it would violate referential transparency. Remember, a function, called twice with semantically identical arguments must always return the same value, that isn't true of eq. Even if this weren't an issue, you're relying heavily on the runtime's behaviour here. There's nothing to stop the runtime, in the first example, observing that the two values are identical, and making them both take up the same memory – they are after all immutable, so that's totally safe. There's similarly nothing to stop the runtime, in the second example, arbitrarily copying the the string (although it's probably not a great idea for efficiency). Bob

On Monday, June 28, 2010 10:38:33 am José Romildo Malaquias wrote:
Is there in Haskell a non monadic function of type a -> a -> Bool which test for physical equality of two values? It would return True if only if both values are the same object in memory.
IIRC "observable sharing" does similar things. Sönke
For instance:
value1 = "good" value2 = "good"
eq value1 value2 => False
value1 = "good" value2 = value1
eq value1 value2 => True
Romildo _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

reallyUnsafePointerEquality :: a -> a -> Int#
but don't use as it can give both false negatives (i.e. GC in the middle of
evaluation) and false positives (that GC just finished and put one object
right where the other was.)
The better model to obtain what you want to use StableNames and seq and, if
you must, a little bit of unsafePerformIO, but note that this means that
side-effects and various inlining/sharing improvements caused by the usual
compilation process that previously were non-observable become critical to
the correctness of your function, which is why the result is in IO to begin
with.
Typically you can construct something purely and inspect the result using IO
all in one go, so the unsafePerformIO machinery isn't required.
-Edward Kmett
2010/6/28 José Romildo Malaquias
Is there in Haskell a non monadic function of type a -> a -> Bool which test for physical equality of two values? It would return True if only if both values are the same object in memory.
For instance:
value1 = "good" value2 = "good"
eq value1 value2 => False
value1 = "good" value2 = value1
eq value1 value2 => True
Romildo _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (5)
-
Edward Kmett
-
José Romildo Malaquias
-
Sönke Hahn
-
Thomas Davie
-
Vo Minh Thu