[Haskell-cafe] Intersection types for Haskell?

José Miguel Vilaça jmvilaca at di.uminho.pt
Tue Jan 10 13:44:43 EST 2006


If I understand your problem than the following is a solution:


{-# OPTIONS -fglasgow-exts #-}

class Foo a b where 
   g :: a -> b

type A = {- change the following -} Int
type B = {- change the following -} Char
instance Foo A B where
   g a = {- change the following -} ' '
type C = {- change the following -} Float
type D = {- change the following -} String

instance Foo C D where
   g c = {- change the following -} ""
f :: (Foo a b, Foo c d) => a -> c -> (b, d)  
f x y = (g x, g y)


Simply create a class an give instances for the wanted types.

The code above is also in a file in attachment.


José Miguel Vilaça 
Departamento de Informática - Universidade do Minho 
jmvilaca at di.uminho.pt

-----Mensagem original-----
De: haskell-cafe-bounces at haskell.org
[mailto:haskell-cafe-bounces at haskell.org] Em nome de Brian Hulley
Enviada: terça-feira, 10 de Janeiro de 2006 18:01
Para: Haskell-cafe
Assunto: [Haskell-cafe] Intersection types for Haskell?

Hi -
I'm wondering if there is any possiblility of getting intersection types 
into Haskell. For example, at the moment there is no (proper) typing for:

    f g x y = (g x, g y)

Ideally, I'd like to be able to write:

    f:: (a -> b & c -> d) -> a -> c -> (b,d)


    f :: (a -> b a) -> c -> d -> (b c, b d)

which is perhaps clearer and prevents bad types such as (Int -> String & 
Int -> Char) by construction.

While it may be impossible (?) to infer such a type for f, would it be 
possible to make use of such an annotation (esp since Haskell with GHC 
extensions already has arbitary rank polymorphism)?

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

    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?

Brian Hulley 

Haskell-Cafe mailing list
Haskell-Cafe at haskell.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tep.hs
Type: application/octet-stream
Size: 440 bytes
Desc: not available
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20060110/6f1170b9/tep-0001.obj

More information about the Haskell-Cafe mailing list