[Haskell-cafe] The container problem

Antoine Latter aslatter at gmail.com
Sat Sep 27 13:39:36 EDT 2008


On Sat, Sep 27, 2008 at 12:23 PM, Andrew Coppin
<andrewcoppin at btinternet.com> wrote:
>
> Can anybody actually demonstrate concretely how FDs and/or ATs would solve
> this problem? (I.e., enable you to write a class that any container can be a
> member of, despite constraints on the element types.)
>

Sure!  Using type-families:

> class Container c where
>    type Elem c
>    insert :: Elem c -> c -> c

> instance Container [a] where
>    type Elem [a] = a
>    insert = (:)

> instance Container ByteString where
>    type Elem ByteString = Word8
>    insert = BS.cons

> instance Ord a => Container (Set a) where
>    type Elem (Set a) = a
>    insert = Set.insert

In GHCi:

> :t insert
insert :: forall c. (Container c) => Elem c -> c -> c

Now the hard part is coming up with a proper API and class hierarchy.

-Antoine


More information about the Haskell-Cafe mailing list