Opened 18 months ago

Last modified 11 months ago

#8967 new feature request

Add syntax for creating finite maps and sets

Reported by: tibbe Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.6.3
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: #436 Differential Rev(s):

Description (last modified by tibbe)

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

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


{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.

Change History (3)

comment:1 Changed 18 months ago by tibbe

  • Description modified (diff)

comment:2 Changed 18 months ago by tibbe

  • Description modified (diff)

comment:3 Changed 11 months ago by thomie

Note: See TracTickets for help on using tickets.