[Haskell-cafe] Equality of functions
Adam Zachary Wyner
wyner at dcs.kcl.ac.uk
Tue Nov 30 08:22:26 EST 2004
After some weeks of experimenting and digging around, I understand that
functions cannot be compared for equality. Thanks to Bjorn Lisper for
pointing this out. I basically understand (?) the underlying mathematical
issue, which is that functions in general may have infinite domains and
ranges. For example, a function f, which gives the successor of an
integer, x + 1, could not be compared to a function g, which gives the
predecessor of an integer which has 2 added, (x + 2) - 1, even though
these two functions give exactly the same result on all arguments, because
we would have to compare them on all possible values for x.
Question -- is this all there is to the issue, or is there something more?
Apologies in advance for not understanding the math better here.
Question -- though Haskell, as a lazy language, does not otherwise have
problems with infinite lists, why does it here?
Question -- is there some way around this problem? It is very handy to be
able to have a variable over functions and then compare the value to a
designated function. This would let me say things like:
when variableFunctionX == functionA then....
One possible (dirty?) solution I thought up is to use the record types of
Hugs to correlate the function with a name of type String:
type FunctionNameFunction = Rec (functionName :: String, functionFunction
Then, whenever one needs to use the function, one uses the value of
functionFunction, but whenever one needs to evaluate for identity, one
uses the value of functionName, using just string identity. Of course,
this makes lots of things less 'pretty' (to apply a function to an
argument, one has to first 'pull out' the function from the record), but
it addresses a general problem. One would also have to keep careful track
of which functionNames correlate with with functionFunctions.
More information about the Haskell-Cafe