Monomorphize, was: Re: Proposal for generalized function partition in List-library

Jan de Wit Jan de Wit" <jwit@studenten.net
Sat, 19 May 2001 17:14:15 +0200


From: "Marcin 'Qrczak' Kowalczyk" <qrczak@knm.org.pl>
> Fri, 18 May 2001 12:32:11 -0700, John Meacham <john@repetae.net> pisze:
>
> > this is interesting, could someone give an example of how polymorphic
> > recursion would disallow specialization of a function?
>
> test:: Show a => a -> [String]
> test x = show x : test [x]

Another source of programs needing polymorphic recursion are non-regular
datatypes,
which occur frequently in Chris Okasaki's
_Purely_Functional_Data_Structures_.
A bare-bones example is:
| data Perfect a = One a | Two (Perfect (a,a))
|  deriving Show
|
| mkPerfect :: Int -> a -> Perfect a
| mkPerfect 1 x = One x
| mkPerfect (n+1) x = Two (mkPerfect n (x,x))

mkPerfect will be called with a number of different types for its second
argument, depending on the first argument.

HTH,

Jan de Wit