[Haskell-cafe] Defining show for a function type.
fruehr at willamette.edu
Tue Jul 11 16:36:56 EDT 2006
On Jul 11, 2006, at 8:27 AM, ihope wrote:
> On 7/10/06, Fritz Ruehr <fruehr at willamette.edu> wrote:
>> Were you interested in "seeing" the function, you could do so, at
>> for finite, total functions (you can also enumerate them, compare them
>> for equality, etc.). See my haskell-cafe message at
> Hmm, interesting. How does it handle curried functions?
The trick is to define appropriate instances of Bounded, Enum, Eq, Ord
for function types, like this (and similarly for Bounded (a,b) and Enum
> instance (Enum a, Bounded a, Enum b, Bounded b) => Bounded (a ->
> b) where
> instance (Enum a, Bounded a, Enum b, Bounded b) => Enum (a -> b)
> instance (Enum a, Bounded a, Eq b) => Eq (a -> b)
> instance (Enum a, Bounded a, Enum b, Bounded b, Eq b) => Ord (a -> b)
> instance (Enum a, Bounded a, Show a, Show b) => Show (a -> b)
Then curried functions of arbitrarily high order are handled
automatically, so long
as the "base" types are suitably Enum, Bounded, Showable, etc. In other
a function of type (Bool,Bool) -> (Bool -> Bool) -> Bool -> Bool (or
is resolved in stages, working down to the "base" type of Bool.
Note of course that the Show form for such a function gives only an
in terms of argument-result pairs: there's no way to get the original
function definition back, short
of resorting to tricks (i.e, by defining something function-like, from
which a function can be extracted,
but which internally "stores" its means of definition).
PS: I can email you the source if you're really interested ... but it's
more instructive to at least try it yourself :) .
More information about the Haskell-Cafe