Pointer equality in Haskell?

2019-01-18 12:40发布

Is there any notion of pointer quality in Haskell? == requires things to be deriving Eq, and I have something which contains a (Value -> IO Value), and neither -> nor IO derive Eq.

EDIT: I'm creating an interpreter for another language which does have pointer equality, so I'm trying to model this behavior while still being able to use Haskell functions to model closures.

EDIT: Example: I want a function special that would do this:

> let x a = a * 2
> let y = x
> special x y
True
> let z a = a * 2
> special x z
False

6条回答
再贱就再见
2楼-- · 2019-01-18 12:53

EDIT: Given your example, you could model this with the IO monad. Just assign your functions to IORefs and compare them.

Prelude Data.IORef> z <- newIORef (\x -> x)
Prelude Data.IORef> y <- newIORef (\x -> x)
Prelude Data.IORef> z == z
True
Prelude Data.IORef> z == y
False
查看更多
三岁会撩人
3楼-- · 2019-01-18 12:59

Another way to do this is to exploit StableNames.

However, special would have to return its results inside of the IO monad unless you want to abuse unsafePerformIO.

The IORef solution requires IO throughout the construction of your structure. Checking StableNames only uses it when you want to check referential equality.

乱世女痞
4楼-- · 2019-01-18 13:11

Pointer equality would break referential transparency, so NO.

Perhaps surprisingly, it is actually possible to compute extensional equality of total functions on compact spaces, but in general (e.g. functions on the integers with possible non-termination) this is impossible.


EDIT: I'm creating an interpreter for another language

Can you just keep the original program AST or source location alongside the Haskell functions you've translated them into? It seems that you want "equality" based on that.

查看更多
\"骚年 ilove
5楼-- · 2019-01-18 13:12

IORefs derive Eq. I don't understand what else you need to get the pointer equality of.

Edit: The only things that it makes sense to compare the pointer equality of are mutable structures. But as I mentioned above, mutable structures, like IORefs, already instance Eq, which allows you to see if two IORefs are the same structure, which is exactly pointer equality.

Luminary・发光体
6楼-- · 2019-01-18 13:13

== requires things to be deriving Eq

Actually (==) requires an instance of Eq, not necessarily a derived instance. What you probably need to do is to provide your own instance of Eq which simply ignores the (Value -> IO Value) part. E.g.,

data D = D Int Bool (Value -> IO Value)

instance Eq D where
  D x y _ == D x' y' _ = x==x && y==y'

Does that help, maybe?

查看更多
你好瞎i
7楼-- · 2019-01-18 13:14

I'm creating an interpreter for another language which does have pointer equality, so I'm trying to model this behavior while still being able to use Haskell functions to model closures.

I am pretty sure that in order to write such an interpreter, your code should be monadic. Don't you have to maintain some sort of environment or state? If so, you could also maintain a counter for function closures. Thus, whenever you create a new closure you equip it with a unique id. Then for pointer equivalence you simply compare these identifiers.

查看更多
登录 后发表回答