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.