Is there any way to compare two functions in Haskell?
My thought is that the answer is no since functions would not derive the Eq type class. However I\'m trying to
As long as the functions are over appropriate domains and ranges (Enum, Bounded, Eq, etc., in slightly different combination for domain and range), you can compare finite functions, even higher-order ones, in a straight-forward way and then automate the process using type classes.
I wrote up the idea in a post to the one of the Haskell mailing lists and then (more properly) in a paper I presented at one of the FDPE conferences (Victoria, BC). I've never seen it done before, but I wouldn't be surprised if others have also done it: it's a pretty simple idea, once you get over the bugaboo that "functions can't be compared for equality". Of course, the usual caveats about partiality and strictness apply: e.g., you can't return True for two functions that both fail to terminate for some common argument.
But for finite domains this technique works and provides a lot of practical use. (Plus it's really just fun to use :) ).
Edit: I added the code to hpaste here; it works in Hugs, but needs some tweaks for GHC.
Edit 2: I added a pragma and comment to the hpaste-d code so that it works in both GHC and Hugs. I also added this example as test3 (it returns True, as expected, and in short order):
(\x -> [(&&x), (||x), not]) == (\y -> [(y&&), (y||), not . not . not])