Tuple-like constructors

Robert Dockins robdockins at fastmail.fm
Tue Feb 7 14:32:08 EST 2006

On Feb 7, 2006, at 11:29 AM, Bulat Ziganshin wrote:

> Hello Robert,
> Tuesday, February 07, 2006, 6:42:41 PM, you wrote:
>>>> More disturbing is the complete inability to write general  
>>>> functions
>>>> over tuples.
> RD> As I understand it, you still have to write down the instance
> RD> declarations when using '-fgenerics'.
> only one generic instance. it's very much these ideas of using nested
> tuples, only with special syntax. below is my definitions of Binary
> class for types with only one constructor:


To cut an paste from the GHC manual:

  class Bin a where
     toBin   :: a -> [Int]
     fromBin :: [Int] -> (a, [Int])

     toBin {| Unit |}    Unit	  = []
     toBin {| a :+: b |} (Inl x)   = 0 : toBin x
     toBin {| a :+: b |} (Inr y)   = 1 : toBin y
     toBin {| a :*: b |} (x :*: y) = toBin x ++ toBin y

     fromBin {| Unit |}    bs      = (Unit, bs)
     fromBin {| a :+: b |} (0:bs)  = (Inl x, bs')    where (x,bs') =  
fromBin bs
     fromBin {| a :+: b |} (1:bs)  = (Inr y, bs')    where (y,bs') =  
fromBin bs
     fromBin {| a :*: b |} bs	  = (x :*: y, bs'') where (x,bs' ) =  
fromBin bs
							  (y,bs'') = fromBin bs'

Now you can make a data type into an instance of Bin like this:

   instance (Bin a, Bin b) => Bin (a,b)
   instance Bin a => Bin [a]

OK. So Now I want a Bin instance for 3-tuples. I have to write down:

instance (Bin a, Bin b, Bin c) => Bin (a,b,c)

Fine.  Now I want it for 4-tuples....

instance (Bin a,Bin b,Bin c,Bin d) => Bin (a,b,c,d)

See the problem?  Sooner or later (probably sooner) I'll get tired of  
typing.  I have to write down an 'instance' declaration for each  
value of n.  Clearly this can't generalize to all n.  So say I'm  
willing to deal with that and further suppose some super-helpful  
person writes down all the instances up to n=15 (say) and gets them  
included in some standard library.  Uh!  Now I discover I need a 17- 
tuple instance.  OK fine, I have to write down my own special 17- 
tuple instance.  Suppose (stay with me here), at some later time I  
import a library written by someone else and they ALSO discovered a  
need for an instance of this particular class for 17-tuples.  Now our  
instances overlap!  Double Ugh!  I need to remove one of the instances.

Still the problem is that I can't perform type level induction on the  
shape of the tuple.  However, I'm willing to concede that generics  
greatly simplifies the problem -- perhaps to the point where my  
objections are merely academic.

OK.  I'm really done now.

Rob Dockins

Speak softly and drive a Sherman tank.
Laugh hard; it's a long way to the bank.
           -- TMBG

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org//pipermail/haskell-prime/attachments/20060207/be259b2a/attachment.htm

More information about the Haskell-prime mailing list