
On Wed, Nov 25, 2015 at 11:39:15AM +0000, Stephen Renehan wrote:
Hi,
I'm looking to do a comparison between 2 simple functions to see if they are equivalent but I appear to be running into some problems if anyone can help.
The two functions I want to compare are f (g a) and g (f a).
I have f defined and g defined as f :: a -> a f = undefined
g :: a -> a. g = undefined
The compiler was complaining about the type signature lacking a binding so added undefined to get around the error. I understand that I must be missing something very basic as they look incomparable in this form. Any suggestions of what changes I should make such a comparison practical?
Hello Stephen, I don't think there is a way to /prove/ f (g a) == g (f a) if their domain is not finite inside Haskell (you could do it with pen and paper). If you have two functions and a finite domain you can do, say: -- function function domain compfun :: (Eq a) => (a -> a) -> (a -> a) -> [a] -> Bool compfun f g d = and $ map (\x -> (f . g) x == (g . f) x) d λ> compfun (+1) (+17) [1..10] True λ> compfun (*5) (+17) [1..10] False Of course this is a toy example, use a proper library (quickcheck, etc.) if you are writing a real program.