[Haskell-cafe] Physical equality
Thomas Davie
tom.davie at gmail.com
Mon Jun 28 04:46:54 EDT 2010
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
More information about the Haskell-Cafe
mailing list