[GHC] #8967: Add syntax for creating finite maps and sets

GHC ghc-devs at haskell.org
Mon Apr 7 15:31:23 UTC 2014


#8967: Add syntax for creating finite maps and sets
-------------------------------------+------------------------------------
        Reporter:  tibbe             |            Owner:
            Type:  feature request   |           Status:  new
        Priority:  normal            |        Milestone:
       Component:  Compiler          |          Version:  7.6.3
      Resolution:                    |         Keywords:
Operating System:  Unknown/Multiple  |     Architecture:  Unknown/Multiple
 Type of failure:  None/Unknown      |       Difficulty:  Unknown
       Test Case:                    |       Blocked By:
        Blocking:                    |  Related Tickets:
-------------------------------------+------------------------------------
Description changed by tibbe:

Old description:

> 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:
>
> {{{#!haskell
> 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:
>
> {{{#!haskell
> 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.

New description:

 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:

 {{{#!haskell
 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:

 {{{#!haskell
 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.

--

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/8967#comment:2>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list