[Haskell-cafe] Intersection types for Haskell?

David Menendez zednenem at psualum.com
Wed Jan 11 01:32:30 EST 2006


Brian Hulley writes:

> Also, as a second point, could functional dependencies in type
> classes be written using a similar syntax eg instead of
> 
>     class Insert t c a | c a -> t where
>         insert :: t -> c a -> c a
> 
> we could write:
> 
>     class Insert (h (c a)) c a where
>         insert :: h (c a) -> c a -> c a
> 
> or
>     class Insert t@(h (c a)) c a where   -- re-using as-pattern syntax
>         insert :: t -> c a -> c a
> 
> to avoid having to have a special syntax just for functional
> dependencies and/or to be able to write more complicated fundeps more
> succinctly?

You might be interested in associated types and associated type
synonyms[1]. These are alternatives to functional dependencies which are
less powerful but possibly easier to use. (Of course, no Haskell
compiler supports them.)

For example:

    class Collection c where
        type Elem c
        
        insert :: Elem c -> c -> c
        ...
    
    instance Collection [a] where
        type Elem [a] = a
        
        insert = (:)
        ...

Your example, in the syntax of associated type synonyms, would look
something like this:

    class Insert c a where
        type T c a
        insert :: T c a -> c a -> c a
        
        
[1]: <http://research.microsoft.com/Users/simonpj/papers/assoc-types/>
-- 
David Menendez <zednenem at psualum.com> | "In this house, we obey the laws
<http://www.eyrie.org/~zednenem>      |        of thermodynamics!"


More information about the Haskell-Cafe mailing list