[Haskell-cafe] tuple and HList
Frederik Eaton
frederik at a5.repetae.net
Sun Mar 20 19:28:28 EST 2005
That's a neat technique. Since it's so general it would be nice if
there were a way to make it more automatic, could one use template
haskell? It seems one should be able to write
HListConstraint $(mkConstraint Show) l
to generate the declarations automatically.
Frederik
On Sun, Mar 20, 2005 at 11:05:06PM +0000, Keean Schupke wrote:
> 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.
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
--
http://ofb.net/~frederik/
More information about the Haskell-Cafe
mailing list