[Haskell-cafe] Defining show for a function type.

Fritz Ruehr 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  
>> least
>> for finite, total functions (you can also enumerate them, compare them
>> for equality, etc.). See my haskell-cafe message at
>> <http://www.haskell.org/pipermail/haskell-cafe/2006-April/ 
>> 015197.html>.
>
> Hmm, interesting. How does it handle curried functions?

The trick is to define appropriate instances of Bounded, Enum, Eq, Ord  
and Show
for function types, like this (and similarly for Bounded (a,b) and Enum  
(a,b)):

> 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)  
> where
>   ...
> instance (Enum a, Bounded a, Eq b)                    => Eq (a -> b)  
> where
>   ...
> instance (Enum a, Bounded a, Enum b, Bounded b, Eq b) => Ord (a -> b)  
> where
>   ...
> instance (Enum a, Bounded a, Show a, Show b)          => Show (a -> b)  
> where
>   ...

Then curried functions of arbitrarily high order are handled  
automatically, so long
as the "base" types are suitably Enum, Bounded, Showable, etc. In other  
words,
a function of type (Bool,Bool) -> (Bool -> Bool) -> Bool -> Bool (or  
whatever)
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  
*extensional* description,
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).

   --  Fritz

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 mailing list