[Haskell-cafe] "show" for functional types

Fritz Ruehr fruehr at willamette.edu
Sat Apr 1 05:01:24 EST 2006

On Mar 31, 2006, at 11:43 PM, Henning Thielemann wrote:

> A function is a set of assignments from arguments to function values. 
> That is, a natural way to show a function would be to print all 
> assigments. Say
> Prelude> toLower
> [('A','a'), ('a','a'), ('1', '1'), ...
> In principle this instance of 'show' could be even implemented, if 
> there would be a function that provides all values of a type.

You can use type classes to implement this for *finite* functions, 
i.e., total functions over types which are Enum and Bounded (and 
Show-able), using for example a tabular format for the show:

	> putStr (show (uncurry (&&)))
	(False,False)   False
	(False,True)    False
	(True,False)    False
	(True,True)     True

It's especially easy to justify showing functions in this context, 
since one is providing the entire extension of the function (and in a 
nice canonical order, given the Enum class of the domain type). You can 
also extend the Enum and Bounded classes to functions over Enum and 
Bounded types, allowing fun stuff like this:

	> fromEnum (&&)
	> (toEnum 4 :: Bool->Bool->Bool) True False

(Unfortunately, although the tabular show style thus extends to 
higher-order functions, it doesn't work well.)

Of course, this is all a bit of a hack, since the straightforward 
implementation really only accounts for total functions ... .

   --  Fritz

More information about the Haskell-Cafe mailing list