tags:

views:

231

answers:

3

Hi,

I've got a function that takes data and either returns the same data or a slightly modified version.

I want to have my program do one thing if it changed or another thing if it did not change.

Previously I was returning a pair (Bool,Object) and using fst to check if it changed. Lately it occurred to me that I could simplify the code by just returning the object and checking equality using ==.

But then I realized that Haskell doesn't differentiate between deep equality checking and "object identity" (i.e., pointer equality). So how can I know whether using == is going to be efficient or not? Should I avoid it for efficiency reasons, or are there cases where I can depend on the compiler figuring out that it doesn't need to do a deep equality check?

Normally I wouldn't be too worried about efficiency while writing an initial program, but this affects the interface to my module so I want to get it right before writing too much code, and it doesn't seem worth it to make the program much less efficient just to simply a small piece of code. Moreover, I'd like to get a better idea of what kind of optimizations I can depend on GHC to help me with.

+1  A: 

I'm still a relative haskell noob, so take my answer with a gran of salt, and please forgive me if my answer isn't as direct as it should be!

In Haskell, operators aren't special - they're just infix functions.

You can look at the definition of the equality operator yourself in the standard prelude.

Of course, it can be overloaded to work with whatever data type you've defined - but if you do the overloading, you'll know how efficient the implementation is.

It might be helpful to know that you can use Hoogle to find the function definition you want. That's how I found the definition of the equality operator.

nont
worth looking at this post too: http://stackoverflow.com/questions/1717553/pointer-equality-in-haskell
nont
+4  A: 

The derived (==) is always deep comparison. Your question has been discussed on haskell-cafe.

Wei Hu
+14  A: 
Norman Ramsey
Thanks, that feels like a pretty concrete answer.
Steve
I wonder how this works as a monad? The second version seems like the `Maybe` monad. However unlike `Maybe`, here you would like the last unchanged result, and not just knowing that it was later `Unchanged`.
yairchu