[Haskell-cafe] overloaded list literals?

Neil Brown nccb2 at kent.ac.uk
Mon Sep 6 06:47:16 EDT 2010


On 06/09/10 11:23, Johannes Waldmann wrote:
> We have overloaded numerical literals (Num.fromInteger)
> and we can overload string literals (IsString.fromString),
> so how about using list syntax ( [], : )
> for anything list-like (e.g., Data.Sequence)?
>    
I would have thought you have two obvious choices for the type-class 
(things like folding are irrelevant to overloading list literals):

class IsList f where
   fromList :: [a] -> f a

or:

class IsList f where
   cons :: a -> f a -> f a
   empty :: f a

I'd go for the first, as I'd imagine you are only overloading the 
[a,b,c] form, not the a:b:c:[] form, and the first reflects this 
better.  Both of these could be used to convert a list literal into a 
list-like type (e.g. Sequence).  But neither of them would be useful for 
sets or maps, because the classes lack an Ord constraint on the type a 
-- maybe this makes overloaded list literals fairly limited in utility.

Thanks,

Neil.


More information about the Haskell-Cafe mailing list