explicit signatures and default for integer literals

Mirko Rahn rahn at ira.uka.de
Mon May 30 05:30:59 EDT 2005

Dinko Tenev wrote:

> *Main> :type new
> new :: (New a b) => a -> b
> The type of new probably accounts for the difference (even though it
> still makes me wonder what the big deal is :)

The big deal is to export just abstract types. When exporting abstract 
types one needs functions to create them, e.g. fromList, singleton, ..., 
and therefore one needs names for the creating functions. In realistic 
situations this solution tends to produce a number of similiar named 
functions. (One for every possible input.) Exactly this is captured by 
the class New, since all this functions have the same name now.

> Using functional dependencies seems to fix it, i.e.:

Yes of course, but

> class New a b | a -> b where new :: a -> b

No, I want to be able to define

instance a b
instance a c

>    or, perhaps more appropriately:
> class New a b | b -> a where new :: a -> b

No again, I want to be able to define

instance b a
instance c a

I re-ask my question:

>>{-# OPTIONS -fglasgow-exts #-}
>>import Data.Map
>>class New a b where new :: a -> b
>>instance Ord a => New [(a,b)] (Map a b) where new = fromList
 >>f :: Ord a => [a] -> Map a Int
 >>f xs = fromList $ zip xs [0..]
>>g :: Ord a => [a] -> Map a Int
>>g xs = new $ zip xs [0..]

Why is ghc unable the determine the type of the Literal 0 in the 
definition of g? The definition of f works fine instead.


-- Mirko Rahn -- Tel +49-721 608 7504 --
--- http://liinwww.ira.uka.de/~rahn/ ---

More information about the Glasgow-haskell-users mailing list