Skip to content

implementation of Map is not always left-biased for keys #52

Closed
@Flupp

Description

@Flupp

The documentation states, the implementation of Map is left-biased. I think, this property shall hold for values and keys. For example:

module Main where

import Data.Function (on)
import Data.Map.Lazy


main :: IO ()
main = do
  print
    $ insert    (OrdOnFst ('a', 'b')) ()
    $ singleton (OrdOnFst ('a', 'a')) ()
  print
    $ alter id  (OrdOnFst ('a', 'b'))
    $ singleton (OrdOnFst ('a', 'a')) ()


newtype OrdOnFst a b = OrdOnFst {unOrdOnFst :: (a, b)} deriving Show

instance Eq a => Eq (OrdOnFst a b) where
  (==) = (==) `on` (fst . unOrdOnFst)

instance Ord a => Ord (OrdOnFst a b) where
  compare = compare `on` (fst . unOrdOnFst)

actual output:

fromList [(OrdOnFst {unOrdOnFst = ('a','b')},())]
fromList [(OrdOnFst {unOrdOnFst = ('a','a')},())]

expected output:

fromList [(OrdOnFst {unOrdOnFst = ('a','b')},())]
fromList [(OrdOnFst {unOrdOnFst = ('a','b')},())]

In this example, insert works left-biased as expected, but alterdoes not. By looking at the source code of Data.Map.Base, I suspect there are other functions suffering from the same problem, e.g., update and updateWithKey.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions