[PL-sem-jr] type classes experts, help me with SYB/C

Felix S Klock II pnkfelix at ccs.neu.edu
Mon Oct 24 00:27:47 EDT 2005


Carl and Jesse-

I'm trying to understand the "Scrap Your Boilerplate With Class"  
paper by Lammel and Peyton Jones that was in ICFP '05.

[[ the remainder of this email refers to the content of the paper, so  
you'll need a copy handy)

In section 3.1 of the paper, the authors describe a failed attempt to  
solve a particular problem (providing function extensions in a  
modular fashion).  They first give the code for the attempt:


> class Size a where
>     gsize :: a -> Int
>
> instance Size Name where
>     gize (N _)  = 1
>
> instance Data t => Size t where
>     gsize t = 1 + sum (gmapQ gsize t)
>

They then explain that this doesn't quite work, because the gmapQ  
function has type:


> gmapQ :: Data a => (forall b. Data b => b -> r) -> a -> [r]
>

and the gsize function has type


> gsize :: Size a => a -> Int
>

and the first argument to gmapQ "has access to the operations of the  
Data class (and its supercalsses) but no more", so gsize is not an  
acceptable argument there.

My problem with this: I interpreted the instance declaration  
"instance Data t => Size t where ..." as saying that for every type t  
that is a Data, t is also a Size.  Therefore, since the first  
argument to gmapQ is going to bind b to some type that is a Data, b  
will also be a Size, and therefore b *does* have access to functions  
such as gsize.

Where did my reasoning go wrong?  I understand the "solution" they  
give, which is to modify the class definition for Data to make Size a  
"superclass" of Data, but I don't understand why we didn't get a  
similar (side-)effect when we said "instance Data t => Size t where ..."

-Felix





More information about the Pl-sem-jr mailing list