[Haskell-cafe] Intersection types for Haskell?
Brian Hulley
brianh at metamilk.com
Tue Jan 10 15:00:01 EST 2006
Taral wrote:
> On 1/10/06, Brian Hulley <brianh at metamilk.com> wrote:
>> 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)
>
> I have no idea what kind of function would have type (a -> b & c ->
> d). Can you give an example?
g x = x
because g 3 = 3 so g has type Int -> Int but also g 'a' = 'a' so g has type
Char -> Char hence g has type Int -> Int & Char -> Char
Also, h x = (x,x) ie Int -> (Int,Int) & Char -> (Char,Char)
The reason I can't just use a -> b for g's type is that then I would have no
way to write out the result of f, since it is not (b,b)
>
>> f :: (a -> b a) -> c -> d -> (b c, b d)
>
> f :: (forall a. a -> b a) -> c -> d -> (b c, b d)
That would be nice but unfortunately is not accepted by GHC because it
cannot unify a->a with a -> b a, for example the following does not compile:
{-# OPTIONS -fglasgow-exts #-}
f :: (forall a. a -> b a) -> c -> d -> (b c, b d)
f g x y = (g x, g y)
g x = x
main = do
putStrLn (show (f g 3 'c'))
Regards,
Brian Hulley
More information about the Haskell-Cafe
mailing list