Search code examples
classgenericshaskelltype-constructor

Haskell construct a type contains class


I want to construct a haskell type:

    type SinglePP = (String,GLattice)

Where the GLattice is defined as:

    class GLattice l where
        join :: l->l->l
        ....

Is there a way for me to do that?


Solution

  • type SinglePP a = (String, a)
    

    and then when you use SinglePP in a function, restrict a to be a GLattice

    someFunc :: GLattice a => SinglePP a -> ()
    someFunc a = doMagic a
    

    If you like, you can use more type system-foo and go with existential types, which let you avoid the boilerplate after each function, but in exchange you must use a language extension and a data declaration with an explicit constructor. This means more pattern matching when you want to get at the a but less typing in the type declarations.

    However most of the types can be inferred.