Achim Krause | 25 Sep 16:56 2012

Overloading Lists

As was mentioned in Haskell Cafe some days ago, there is some work going 
on on overloading lists by George Giorgidze, Jeroen Weijers and me.
We are implementing two approaches in fact. The first one is using a 
typeclass called FromList, defined as
class FromList l where
  type (Elem l)
  fromList :: [Elem l] -> l

And explicit lists and arithmetic sequences get just desugared in the 
obvious way using this, so, for example, [1,2,3] gets desugared to 
(fromList [1,2,3]).
The disadvantages of this (although being very easy to implement and 
use) is that this goes through recursive lists each time it gets used. 
Since explicit lists need not to be static (i.e. can contain variables), 
this can really matter.
The second approach thus is the following (kinda motivated from the way 
parallel arrays are desugared): There is a class
class Singleton l where
  type (Elem l)
  singleton :: Elem l -> l
and now explicit lists like [1,2,3] get desugared to (singleton 1) 
`mappend` (singleton 2) `mappend` (singleton 3).
Arithmetic sequences are simply done by providing a class "GenericEnum" 
containing slightly more general versions of the classical from, 
fromThen, fromTo, fromThenTo, and the desugaring then really is exactly 
the same as in the non-overloaded case.

I wrote a patch for the GHC which is supposed to do this stuff. It 
compiles, and the typechecking and desugaring of arithmetic sequences 
works just fine.
(Continue reading)