Two Proposals

George Giorgidze giorgidze at
Fri Sep 30 19:28:30 CEST 2011

GHC Users,

I would like to make to the following two proposals:
  * Eliminate the default grouping close from SQL-like comprehensions
  * Introduce a GHC extension for list literal overloading

OK, let me start with the first proposal.

Currently, the SQL-like comprehension notation (both in its list comprehension and monad comprehension variants) features the following five clauses:

then f
then f by e
then group by e
then group using f
then group by e using f

The first two clauses are used for specifying transformations of type [a] -> [a] (or Monad m => m a-> m a for monad comprehensions). The following three clauses are used for specifying transformations of type [a] -> [[a]] (or Monad m, Functor f => m a -> m (f a) for monad comprehensions). See [1] for further details.

Note that the third clause does not mention which function is used for grouping. In this case GHC.Exts.groupWith function is used as a default for list comprehensions and the mgroupWith function from the MonadGroup class is used as a default for monad comprehensions.

I would like to suggest to remove the third clause for the following reasons:
* Currently the syntax is asymmetrical. Note that there is the default case for the 'then group' clause and not for the 'then' clause.
* In the current notation it is not clear which grouping function is used in the default case
* For many monads including lists it is not clear which function should be selected as a default (e.g., the groupWith function also does sorting and it is not clear to me why this should be the default)
* Gets rid of the MonadGroup class. Currently the sole purpose of this class is to introduce a default grouping function for monad comprehensions.
* Explicit mention of the grouping function would make  monad/list comprehensions much easier to read by making it immediately apparent which function is used for grouping.

My second proposal is to introduce the OverloadedLists extension that overloads list literals. See Section 5.2 in [1] for details.

Basically the idea is to treat list literals like:



fromList [1,2,3]


class IsList l where
  type Item l
  fromList :: [Item l] -> l

In the following I give useful instances of the IsList class.

instance IsList [a] where
  type Item [a] = a
  fromList = id

instance (Ord a) => IsList (Set a) where
  type Item (Set a) = a
  fromList = Set.fromList

instance (Ord k) => IsList (Map k v) where
  type Item (Map k v) = (k,v)
  fromList = Map.fromList 

instance IsList (IntMap v) where
  type Item (IntMap v) = (Int,v)
  fromList = IntMap.fromList 

instance IsList Text where
  type Item Text = Char
  fromList = Text.pack

As you can see the extension would allow list literals to be used for sets, maps and integer maps. In addition the suggested OverloadedLists extension would subsume OverloadedStrings extension (see the instance for Text, for example). Having said that, for now, I am not suggesting to remove the OverloadedStrings extension as it appears to be widely used.

This extension could also be used for giving data-parallel array literals instead of the special syntax used currently.

Unless there is a vocal opposition to the aforementioned two proposals, I would like to implement them in GHC. Both changes appear to be straightforward to implement.

Thanks in advance for your feedback.

Cheers, George


More information about the Glasgow-haskell-users mailing list