Haskell Language Common GHC Language Extensions OverloadedLists

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

added in GHC 7.8.

OverloadedLists, similar to OverloadedStrings, allows list literals to be desugared as follows:

[]          -- fromListN 0 []
[x]         -- fromListN 1 (x : [])
[x .. ]     -- fromList (enumFrom x)

This comes handy when dealing with types such as Set, Vector and Maps.

['0' .. '9']             :: Set Char
[1 .. 10]                :: Vector Int
[("default",0), (k1,v1)] :: Map String Int
['a' .. 'z']             :: Text

IsList class in GHC.Exts is intended to be used with this extension.

IsList is equipped with one type function, Item, and three functions, fromList :: [Item l] -> l, toList :: l -> [Item l] and fromListN :: Int -> [Item l] -> l where fromListN is optional. Typical implementations are:

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

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

Examples taken from OverloadedLists – GHC.



Got any Haskell Language Question?