Object Address - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Simon Hudon

Hi All! Is there a way (probably an unsafe primitive) to get the address of an object in Haskell? I'd like to use it to optimize search in a cache

Simon Hudon

Found part of my answer: GHC.Exts exports reallyUnsafePtrEquality : a -> a -> #Int with appropriate warnings. I still wish I could use an object's address to calculate an unsound hash code but this is a good start

Reed Mullanix

anyToAddr# :: a -> State# RealWorld -> (#State# RealWorld, Addr##) might also fit the bill

Simon Hudon

Nice! Thanks! I assume I would take the RealWorld from inside the IO monad, right?

Reed Mullanix

Yeah, that would be my guess!

Simon Hudon

Ah and now I see addr2Int# :: Addr# -> Int# if I want to use it to calculate a hash

Sandy Maguire

can you be more specific about your problem? this strikes me as almost certainly the wrong approach!

Simon Hudon

There's are data structures in automated reasoning (such as BDDs) that can be implemented quite efficiently in C / C++ because of the kind of pointer trick that I mentioned but in higher level languages such as ML and Haskell, we systematically lose orders of magnitude in performance. I'm trying to see how to use the pointer tricks in pure functional languages and prove that we still have referential transparency.

James King

If you're working on what I think you're working on I hope you'll find the time and energy to write about your findings. :surprise:

Simon Hudon

No worries, I started writing already. I have a bunch of my examples already implemented in Lean. I'll show you when I have something readable

Simon Hudon

I actually managed to write an address-based hashmap in Haskell too. It was a pleasant surprise