Tuple-like constructors

Bulat Ziganshin bulatz at HotPOP.com
Tue Feb 7 11:29:03 EST 2006


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:

class Binary a where
    -- | Convert value to sequence of bits and write it to the stream
    put_ :: (BinaryStream m h) => h -> a -> m ()
    -- | Read value written to the stream as bit sequence
    get  :: (BinaryStream m h) => h -> m a
{-
    Using "generic type classes" extension, GHC allows to semi-automatically
    generate instances of Binary for any types. All what you need to define
    Binary instance for some type MyType is to write:

    instance Binary MyType where

    These is the all definitions required, but they don't work
    because of current restrictions in GHC's generics support:

    put_ {| Unit  |}  h  Unit     = return ()
    put_ {| a:*:b |}  h (x :*: y) = do put_ h x; put_ h y

    get  {| Unit  |}  h = return ()
    get  {| a:*:b |}  h = do x <- get h; y <- get h; return (x :*: y)
-}



-- 
Best regards,
 Bulat                            mailto:bulatz at HotPOP.com





More information about the Haskell-prime mailing list