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 thi
It's always a bad idea to rely on uncertain compiler optimizations to provide such an important performance guarantee as constant-time equality vs linear-time deep equality. You're much better off with a new type that encapsulates a value plus information about whether the value is new. Depending on your application this can be either
data Changed a = Changed a | Unchanged a
or
data Changed a = Changed a | Unchanged
We actually use a similar type inside the Glasgow Haskell Compiler so we can keep running the optimizer until the code stops changing. We also run iterative dataflow analysis until the results stop changing.
We found it useful to make this type a monad so that we can write some simple higher-order functions using do
notation, but it's not necessary—just a convenience.
Summary: If you want constant-time checking, code it yourself—don't rely on a possible compiler optimization which might not be there—or which might change in the next release.
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.
The derived (==) is always deep comparison. Your question has been discussed on haskell-cafe.