Вопрос

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?

Это было полезно?

Решение

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.

Другие советы

Use the ExistentialQuantification extension, like so:

{-# LANGUAGE ExistentialQuantification #-}

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

data SinglePP = forall a . (GLattice a) => SinglePP String a

This guarantees that the value stored in SinglePP's second field holds a type that implements the GLattice class, but does not specify which type. This means that you will only be able to use the GLattice operations on it.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top