[Haskell-cafe] tuple and HList

Keean Schupke k.schupke at imperial.ac.uk
Sun Mar 20 18:05:06 EST 2005


Frederik Eaton wrote:

>Another thing which I don't think is mentioned in the paper, which is
>convenient, is that you can define HLists all of whose elements are
>members of a given class:
>
>class HListShow l
>instance HListShow HNil
>instance (Show a, HListShow l) => HListShow (a :* l)
>  
>
You can avoid the need to declare a new class for each constrained list
by using the following:

 >class Constraint c a
 >
 >data SHOW
 >instance Show a => Constraint SHOW a
 >
 >class HListConstraint c l
 >instance HListConstraint c HNil
 >instance (Constraint c a,HListConstraint c l) => HListConstraint c 
(HCons a l)

You can now constrain a list as follows:

 >assertShow :: HListConstraint SHOW l => l -> l
 >assertShow = id

The type parameter can be made first class using:

 >showConstraint :: SHOW
 >showConstraint = undefined

So we can now pass this as a parameter:

 >assertConstraintOnHList :: HListConstraint c l => c -> l -> l
 >assertConstraintOnHList _ = id

    Keean.





More information about the Haskell-Cafe mailing list