Skip to content

Add pair operator to base

Motivation

Haskell doesn't have literals for maps. But many people have to deal with them. We often do it like this:

a_map =
  Map.fromList
    [ ( "alpha"
      , 42
      )
    , ( "bravo"
      , 43
      )
    ]

A lot of syntax far from other languages is a problem.

Proposal

Add a little code to Data.Tuple:

pattern (:=) :: a -> b -> (a, b)
pattern a := b = (a, b)

Then map literals will look like this:

a_map =
  Map.fromList
    [ "alpha" :=
        42
    , "bravo" :=
        43
    ]

or, with OverloadedLists:

a_map =
  [ "alpha" :=
      42
  , "bravo" :=
      43
  ]

Very close! Even destructuring works:

reverse_map = [v := k | k := v <- Map.assocs a_map]

Drawbacks

k := f $ g x will be parsed counter-intuitively. All we can do is to define infix 0 := to make (:=) and ($) incomposable.

Name

Name is debatable. Some libraries use (:=) already for their own purpose. (:-) is fine, too.

References

Operator (->) in Scala https://www.scala-lang.org/api/current/scala/Predef$$ArrowAssoc.html#-%3EB:(A,B)

Implementation

https://gitlab.com/cblp/pair/-/blob/master/Pair.hs

Edited by Yuriy Syrovetskiy
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information