Skip to content

Add syntax for creating finite maps and sets

Many languages have syntax for creating maps. Commonly used syntax is either

{key1: value1, key2: value2, ...}

or

{key1 => value1, key2 => value2, ...}

Since few Haskellers (with the notable exception of SPJ) use the curly bracket Haskell syntax for layout, I thought we could co-opt it and instead use it for map (and set literals.) The feature would of course be protected by a language pragma as to not break old code.

Strawman proposal:

{key1: value1, key2: value2, ...} desugars to fromList [(key1, value1), (key2, value2), ...], using a new type class:

class IsMap m where
  type Key m
  type Value m

  fromList :: [(Key m, Value m)] -> m

{:} is the literal for empty maps.

Analogously, {value1, value2, ...} desugars to fromList [value1, value2, ...], using a new type class:

class IsSet m where
  type Item m

  fromList :: [Item m] -> m

{} is the literal for empty sets.

Both the containers and unordered-containers packages would presumably provide instances. It's possible that mutable data structures, such as those provided by the hashtables package, could provide instances as well.

  • *Open question**: will this clash badly with the record syntax? It seems to work in other languages which use { and } to define other entities.
Edited by tibbe
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information