Closed
Description
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 alter
does 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
Labels
No labels