Is there any way such that I can arbitrarily establish a partial order on a collection of functions.
That is, given two functions f,g; can I consistently evaluate f < g to some truth value.
Is there any way such that I can arbitrarily establish a partial order on a collection of functions.
That is, given two functions f,g; can I consistently evaluate f < g to some truth value.
No. There is no way to compare functions to each other. You can't even evaluate f == g
.
If you could do this (and you probably can, by eg reaching into IO to look up the functions' memory addresses), you would be violating referential transparency. Consider the following definitions:
f = (+) 2
g x = 2 + x
These two functions behave the same for all inputs, which means by referential transparency you can treat them interchangeably: foo f
should be equal to foo g
for any foo
, and as a consequence foo f g
should be the same as foo g f
. But if we let foo
be your (<)
function, we would break this law. A lot of Haskell depends on referential transparency, so even if you can find some way to cheat around this I would encourage you not to.
But if you want to use IO, I think it is relatively harmless to write a function with the signature:
(<) :: a -> b -> IO Bool
by looking at the memory addresses of the two arguments.
You kinda can. If in a -> b
a
is finite and b
can be ordered, then all you need is to compare f x
and g x
for all x
from a
until f x /= g x
. This is the same as comparing tuples, since a function from a finite a
is isomorphic to an n-ary tuple.