Ticket #1611: Map.2.diff
File Map.2.diff, 47.4 KB (added by guest, 9 years ago) 


Data/Map.hs
old new 1 { # OPTIONS_GHC fnobangpatterns #}1 {TODO  enable # OPTIONS_GHC fnobangpatterns #} 2 2 3 3  4 4   5 5  Module : Data.Map 6 6  Copyright : (c) Daan Leijen 2002 7  (c) Andriy Palamarchuk 2007 7 8  License : BSDstyle 8 9  Maintainer : libraries@haskell.org 9 10  Stability : provisional … … 31 32  Note that the implementation is /leftbiased/  the elements of a 32 33  first argument are always preferred to the second, for example in 33 34  'union' or 'insert'. 35  36  Operation comments contain the operation time complexity in 37  the BigO notation <http://en.wikipedia.org/wiki/Big_O_notation>. 34 38  35 39 36 40 module Data.Map ( … … 92 96 , mapWithKey 93 97 , mapAccum 94 98 , mapAccumWithKey 95 96 97 99 , mapKeys 100 , mapKeysWith 101 , mapKeysMonotonic 98 102 99 103  ** Fold 100 104 , fold … … 103 107  * Conversion 104 108 , elems 105 109 , keys 106 110 , keysSet 107 111 , assocs 108 112 109 113  ** Lists … … 196 200 197 201   /O(log n)/. Find the value at a key. 198 202  Calls 'error' when the element can not be found. 203  204  > fromList [(5,'a'), (3,'b')] ! 1 Error: element not in the map 205  > fromList [(5,'a'), (3,'b')] ! 5 == 'a' 206 199 207 (!) :: Ord k => Map k a > k > a 200 208 m ! k = find k m 201 209 202   /O(n+m)/. See'difference'.210   Same as 'difference'. 203 211 (\\) :: Ord k => Map k a > Map k b > Map k a 204 212 m1 \\ m2 = difference m1 m2 205 213 … … 239 247 Query 240 248 } 241 249   /O(1)/. Is the map empty? 250  251  > Data.Map.null (empty) == True 252  > Data.Map.null (singleton 'a' 1) == False 253 242 254 null :: Map k a > Bool 243 255 null t 244 256 = case t of … … 246 258 Bin sz k x l r > False 247 259 248 260   /O(1)/. The number of elements in the map. 261  262  > size empty == 0 263  > size (singleton 'a' 1) == 1 264  > size (fromList([('a',1), ('c', 2), ('b', 3)])) == 3 265 249 266 size :: Map k a > Int 250 267 size t 251 268 = case t of … … 253 270 Bin sz k x l r > sz 254 271 255 272 256   /O(log n)/. Lookup the value at a key in the map. 273   /O(log n)/. Lookup the value at a key in the map. 257 274  258 275  The function will 259 276  @return@ the result in the monad or @fail@ in it the key isn't in the 260 277  map. Often, the monad to use is 'Maybe', so you get either 261 278  @('Just' result)@ or @'Nothing'@. 279  280  > let m = fromList [(5,'a'), (3,'b'), (7,'c')] 281  > value1 < Data.Map.lookup 5 m 282  > value1 283  > 'a' 284  > value2 < Data.Map.lookup 1 m 285  > Error: Key not found 286  287  An example of using @lookup@ with @Maybe@ monad: 288  289  > import Prelude hiding (lookup) 290  > import Data.Map 291  > 292  > employeeDept = fromList([("John","Sales"), ("Bob","IT")]) 293  > deptCountry = fromList([("IT","USA"), ("Sales","France")]) 294  > countryCurrency = fromList([("USA", "Dollar"), ("France", "Euro")]) 295  > 296  > employeeCurrency :: String > Maybe String 297  > employeeCurrency name = do 298  > dept < lookup name employeeDept 299  > country < lookup dept deptCountry 300  > lookup country countryCurrency 301  > 302  > main = do 303  > putStrLn $ "John's currency: " ++ (show (employeeCurrency "John")) 304  > putStrLn $ "Pete's currency: " ++ (show (employeeCurrency "Pete")) 305  306  The output of this program: 307  308  > John's currency: Just "Euro" 309  > Pete's currency: Nothing 310 262 311 lookup :: (Monad m,Ord k) => k > Map k a > m a 263 312 lookup k t = case lookup' k t of 264 313 Just x > return x … … 283 332 GT > lookupAssoc k r 284 333 EQ > Just (kx,x) 285 334 286   /O(log n)/. Is the key a member of the map? 335   /O(log n)/. Is the key a member of the map? See also 'notMember'. 336  337  > member 5 (fromList [(5,'a'), (3,'b')]) == True 338  > member 1 (fromList [(5,'a'), (3,'b')]) == False 339 287 340 member :: Ord k => k > Map k a > Bool 288 341 member k m 289 342 = case lookup k m of 290 343 Nothing > False 291 344 Just x > True 292 345 293   /O(log n)/. Is the key not a member of the map? 346   /O(log n)/. Is the key not a member of the map? See also 'member'. 347  348  > notMember 5 (fromList [(5,'a'), (3,'b')]) == False 349  > notMember 1 (fromList [(5,'a'), (3,'b')]) == True 350 294 351 notMember :: Ord k => k > Map k a > Bool 295 352 notMember k m = not $ member k m 296 353 … … 303 360 Just x > x 304 361 305 362   /O(log n)/. The expression @('findWithDefault' def k map)@ returns 306  the value at key @k@ or returns @def@ when the key is not in the map. 363  the value at key @k@ or returns default value @def@ 364  when the key is not in the map. 365  366  > findWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x' 367  > findWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a' 368 307 369 findWithDefault :: Ord k => a > k > Map k a > a 308 370 findWithDefault def k m 309 371 = case lookup k m of … … 316 378 Construction 317 379 } 318 380   /O(1)/. The empty map. 381  382  > empty == fromList [] 383  > size empty == 0 384 319 385 empty :: Map k a 320 386 empty 321 387 = Tip 322 388 323 389   /O(1)/. A map with a single element. 390  391  > singleton 1 'a' == fromList [(1, 'a')] 392  > size (singleton 1 'a') == 1 393 324 394 singleton :: k > a > Map k a 325 395 singleton k x 326 396 = Bin 1 k x Tip Tip … … 330 400 } 331 401   /O(log n)/. Insert a new key and value in the map. 332 402  If the key is already present in the map, the associated value is 333  replaced with the supplied value , i.e. 'insert' is equivalent to403  replaced with the supplied value. 'insert' is equivalent to 334 404  @'insertWith' 'const'@. 405  406  > insert 5 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'x')] 407  > insert 7 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'a'), (7, 'x')] 408  > insert 5 'x' empty == singleton 5 'x' 409 335 410 insert :: Ord k => k > a > Map k a > Map k a 336 411 insert kx x t 337 412 = case t of … … 342 417 GT > balance ky y l (insert kx x r) 343 418 EQ > Bin sz kx x l r 344 419 345   /O(log n)/. Insert with a combining function.420   /O(log n)/. Insert with a function, combining new value and old value. 346 421  @'insertWith' f key value mp@ 347 422  will insert the pair (key, value) into @mp@ if key does 348 423  not exist in the map. If the key does exist, the function will 349 424  insert the pair @(key, f new_value old_value)@. 425  426  > insertWith (++) 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "xxxa")] 427  > insertWith (++) 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")] 428  > insertWith (++) 5 "xxx" empty == singleton 5 "xxx" 429 350 430 insertWith :: Ord k => (a > a > a) > k > a > Map k a > Map k a 351 431 insertWith f k x m 352 432 = insertWithKey (\k x y > f x y) k x m … … 357 437 = insertWithKey' (\k x y > f x y) k x m 358 438 359 439 360   /O(log n)/. Insert with a combining function.440   /O(log n)/. Insert with a function, combining key, new value and old value. 361 441  @'insertWithKey' f key value mp@ 362 442  will insert the pair (key, value) into @mp@ if key does 363 443  not exist in the map. If the key does exist, the function will 364 444  insert the pair @(key,f key new_value old_value)@. 365 445  Note that the key passed to f is the same key passed to 'insertWithKey'. 446  447  448  > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "" ++ old_value 449  > insertWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:xxxa")] 450  > insertWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")] 451  > insertWithKey f 5 "xxx" empty == singleton 5 "xxx" 452 366 453 insertWithKey :: Ord k => (k > a > a > a) > k > a > Map k a > Map k a 367 454 insertWithKey f kx x t 368 455 = case t of … … 385 472 EQ > let x' = f kx x y in seq x' (Bin sy kx x' l r) 386 473 387 474 388   /O(log n)/. The expression (@'insertLookupWithKey' f k x map@) 475   /O(log n)/. Combines insert operation with old value retrieval. 476  The expression (@'insertLookupWithKey' f k x map@) 389 477  is a pair where the first element is equal to (@'lookup' k map@) 390 478  and the second element equal to (@'insertWithKey' f k x map@). 479  480  > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "" ++ old_value 481  > insertLookupWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:xxxa")]) 482  > insertLookupWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a"), (7, "xxx")]) 483  > insertLookupWithKey f 5 "xxx" empty == (Nothing, singleton 5 "xxx") 484  485  This is how to define @insertLookup@ using @insertLookupWithKey@: 486  487  > let insertLookup kx x t = insertLookupWithKey (\_ a _ > a) kx x t 488  > insertLookup 5 "x" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "x")]) 489  > insertLookup 7 "x" (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a"), (7, "x")]) 490 391 491 insertLookupWithKey :: Ord k => (k > a > a > a) > k > a > Map k a > (Maybe a,Map k a) 392 492 insertLookupWithKey f kx x t 393 493 = case t of … … 404 504 } 405 505   /O(log n)/. Delete a key and its value from the map. When the key is not 406 506  a member of the map, the original map is returned. 507  508  > delete 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" 509  > delete 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] 510  > delete 5 empty == empty 511 407 512 delete :: Ord k => k > Map k a > Map k a 408 513 delete k t 409 514 = case t of … … 414 519 GT > balance kx x l (delete k r) 415 520 EQ > glue l r 416 521 417   /O(log n)/. Adjust a value at a specific key. When the key is not 522   /O(log n)/. Update a value at a specific key with the result of the provided function. 523  When the key is not 418 524  a member of the map, the original map is returned. 525  526  > adjust ("new " ++) 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")] 527  > adjust ("new " ++) 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] 528  > adjust ("new " ++) 7 empty == empty 529 419 530 adjust :: Ord k => (a > a) > k > Map k a > Map k a 420 531 adjust f k m 421 532 = adjustWithKey (\k x > f x) k m 422 533 423 534   /O(log n)/. Adjust a value at a specific key. When the key is not 424 535  a member of the map, the original map is returned. 536  537  > let f key x = (show key) ++ ":new " ++ x 538  > adjustWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")] 539  > adjustWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] 540  > adjustWithKey f 7 empty == empty 541 425 542 adjustWithKey :: Ord k => (k > a > a) > k > Map k a > Map k a 426 543 adjustWithKey f k m 427 544 = updateWithKey (\k x > Just (f k x)) k m … … 429 546   /O(log n)/. The expression (@'update' f k map@) updates the value @x@ 430 547  at @k@ (if it is in the map). If (@f x@) is 'Nothing', the element is 431 548  deleted. If it is (@'Just' y@), the key @k@ is bound to the new value @y@. 549  550  > let f x = if x == "a" then Just "new a" else Nothing 551  > update f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")] 552  > update f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] 553  > update f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" 554 432 555 update :: Ord k => (a > Maybe a) > k > Map k a > Map k a 433 556 update f k m 434 557 = updateWithKey (\k x > f x) k m … … 437 560  value @x@ at @k@ (if it is in the map). If (@f k x@) is 'Nothing', 438 561  the element is deleted. If it is (@'Just' y@), the key @k@ is bound 439 562  to the new value @y@. 563  564  > let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing 565  > updateWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")] 566  > updateWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] 567  > updateWithKey f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" 568 440 569 updateWithKey :: Ord k => (k > a > Maybe a) > k > Map k a > Map k a 441 570 updateWithKey f k t 442 571 = case t of … … 449 578 Just x' > Bin sx kx x' l r 450 579 Nothing > glue l r 451 580 452   /O(log n)/. Lookup and update. 581   /O(log n)/. Lookup and update. See also 'updateWithKey'. 582  The function returns changed value, if it is updated. 583  Returns the original key value if the map entry is deleted. 584  585  > let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing 586  > updateLookupWithKey f 5 (fromList [(5,"a"), (3,"b")]) == (Just "5:new a", fromList [(3, "b"), (5, "5:new a")]) 587  > updateLookupWithKey f 7 (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a")]) 588  > updateLookupWithKey f 3 (fromList [(5,"a"), (3,"b")]) == (Just "b", singleton 5 "a") 589 453 590 updateLookupWithKey :: Ord k => (k > a > Maybe a) > k > Map k a > (Maybe a,Map k a) 454 591 updateLookupWithKey f k t 455 592 = case t of … … 464 601 465 602   /O(log n)/. The expression (@'alter' f k map@) alters the value @x@ at @k@, or absence thereof. 466 603  'alter' can be used to insert, delete, or update a value in a 'Map'. 467  In short : @'lookup' k ('alter' f k m) = f ('lookup' k m)@ 604  In short : @'lookup' k ('alter' f k m) = f ('lookup' k m)@. 605  606  > let f _ = Nothing 607  > alter f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")] 608  > alter f 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" 609  > 610  > let f _ = Just "c" 611  > alter f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "c")] 612  > alter f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "c")] 613 468 614 alter :: Ord k => (Maybe a > Maybe a) > k > Map k a > Map k a 469 615 alter f k t 470 616 = case t of … … 485 631   /O(log n)/. Return the /index/ of a key. The index is a number from 486 632  /0/ up to, but not including, the 'size' of the map. Calls 'error' when 487 633  the key is not a 'member' of the map. 634  635  > findIndex 2 (fromList [(5,"a"), (3,"b")]) Error: element is not in the map 636  > findIndex 3 (fromList [(5,"a"), (3,"b")]) == 0 637  > findIndex 5 (fromList [(5,"a"), (3,"b")]) == 1 638  > findIndex 6 (fromList [(5,"a"), (3,"b")]) Error: element is not in the map 639 488 640 findIndex :: Ord k => k > Map k a > Int 489 641 findIndex k t 490 642 = case lookupIndex k t of … … 492 644 Just idx > idx 493 645 494 646   /O(log n)/. Lookup the /index/ of a key. The index is a number from 495  /0/ up to, but not including, the 'size' of the map. 647  /0/ up to, but not including, the 'size' of the map. 648  649  > isJust (lookupIndex 2 (fromList [(5,"a"), (3,"b")])) == False 650  > fromJust (lookupIndex 3 (fromList [(5,"a"), (3,"b")])) == 0 651  > fromJust (lookupIndex 5 (fromList [(5,"a"), (3,"b")])) == 1 652  > isJust (lookupIndex 6 (fromList [(5,"a"), (3,"b")])) == False 653 496 654 lookupIndex :: (Monad m,Ord k) => k > Map k a > m Int 497 655 lookupIndex k t = case lookup 0 t of 498 656 Nothing > fail "Data.Map.lookupIndex: Key not found." … … 507 665 508 666   /O(log n)/. Retrieve an element by /index/. Calls 'error' when an 509 667  invalid index is used. 668  669  > elemAt 0 (fromList [(5,"a"), (3,"b")]) == (3,"b") 670  > elemAt 1 (fromList [(5,"a"), (3,"b")]) == (5, "a") 671  > elemAt 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range 672 510 673 elemAt :: Int > Map k a > (k,a) 511 674 elemAt i Tip = error "Map.elemAt: index out of range" 512 675 elemAt i (Bin _ kx x l r) … … 519 682 520 683   /O(log n)/. Update the element at /index/. Calls 'error' when an 521 684  invalid index is used. 685  686  > updateAt (\ _ _ > Just "x") 0 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "x"), (5, "a")] 687  > updateAt (\ _ _ > Just "x") 1 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "x")] 688  > updateAt (\ _ _ > Just "x") 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range 689  > updateAt (\ _ _ > Just "x") (1) (fromList [(5,"a"), (3,"b")]) Error: index out of range 690  > updateAt (\_ _ > Nothing) 0 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" 691  > updateAt (\_ _ > Nothing) 1 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" 692  > updateAt (\_ _ > Nothing) 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range 693  > updateAt (\_ _ > Nothing) (1) (fromList [(5,"a"), (3,"b")]) Error: index out of range 694 522 695 updateAt :: (k > a > Maybe a) > Int > Map k a > Map k a 523 696 updateAt f i Tip = error "Map.updateAt: index out of range" 524 697 updateAt f i (Bin sx kx x l r) … … 533 706 534 707   /O(log n)/. Delete the element at /index/. 535 708  Defined as (@'deleteAt' i map = 'updateAt' (\k x > 'Nothing') i map@). 709  710  > deleteAt 0 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" 711  > deleteAt 1 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" 712  > deleteAt 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range 713  > deleteAt (1) (fromList [(5,"a"), (3,"b")]) Error: index out of range 714 536 715 deleteAt :: Int > Map k a > Map k a 537 716 deleteAt i map 538 717 = updateAt (\k x > Nothing) i map … … 541 720 { 542 721 Minimal, Maximal 543 722 } 544   /O(log n)/. The minimal key of the map. 723   /O(log n)/. The minimal key of the map. Calls 'error' is the map is empty. 724  725  > findMin (fromList [(5,"a"), (3,"b")]) == (3,"b") 726  > findMin empty Error: empty map has no minimal element 727 545 728 findMin :: Map k a > (k,a) 546 729 findMin (Bin _ kx x Tip r) = (kx,x) 547 730 findMin (Bin _ kx x l r) = findMin l 548 731 findMin Tip = error "Map.findMin: empty map has no minimal element" 549 732 550   /O(log n)/. The maximal key of the map. 733   /O(log n)/. The maximal key of the map. Calls 'error' is the map is empty. 734  735  > findMax (fromList [(5,"a"), (3,"b")]) == (5,"a") 736  > findMax empty Error: empty map has no maximal element 737 551 738 findMax :: Map k a > (k,a) 552 739 findMax (Bin _ kx x l Tip) = (kx,x) 553 740 findMax (Bin _ kx x l r) = findMax r 554 741 findMax Tip = error "Map.findMax: empty map has no maximal element" 555 742 556   /O(log n)/. Delete the minimal key. 743   /O(log n)/. Delete the minimal key. Returns an empty map if the map is empty. 744  745  > deleteMin (fromList [(5,"a"), (3,"b"), (7,"c")]) == fromList [(5,"a"), (7,"c")] 746  > deleteMin empty == empty 747 557 748 deleteMin :: Map k a > Map k a 558 749 deleteMin (Bin _ kx x Tip r) = r 559 750 deleteMin (Bin _ kx x l r) = balance kx x (deleteMin l) r 560 751 deleteMin Tip = Tip 561 752 562   /O(log n)/. Delete the maximal key. 753   /O(log n)/. Delete the maximal key. Returns an empty map if the map is empty. 754  755  > deleteMax (fromList [(5,"a"), (3,"b"), (7,"c")]) == fromList [(3,"b"), (5,"a")] 756  > deleteMax empty == empty 757 563 758 deleteMax :: Map k a > Map k a 564 759 deleteMax (Bin _ kx x l Tip) = l 565 760 deleteMax (Bin _ kx x l r) = balance kx x l (deleteMax r) 566 761 deleteMax Tip = Tip 567 762 568 763   /O(log n)/. Update the value at the minimal key. 764  765  > updateMin (\ a > Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "Xb"), (5, "a")] 766  > updateMin (\ _ > Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" 767 569 768 updateMin :: (a > Maybe a) > Map k a > Map k a 570 769 updateMin f m 571 770 = updateMinWithKey (\k x > f x) m 572 771 573 772   /O(log n)/. Update the value at the maximal key. 773  774  > updateMax (\ a > Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "Xa")] 775  > updateMax (\ _ > Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" 776 574 777 updateMax :: (a > Maybe a) > Map k a > Map k a 575 778 updateMax f m 576 779 = updateMaxWithKey (\k x > f x) m 577 780 578 781 579 782   /O(log n)/. Update the value at the minimal key. 783  784  > updateMinWithKey (\ k a > Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"3:b"), (5,"a")] 785  > updateMinWithKey (\ _ _ > Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" 786 580 787 updateMinWithKey :: (k > a > Maybe a) > Map k a > Map k a 581 788 updateMinWithKey f t 582 789 = case t of … … 587 794 Tip > Tip 588 795 589 796   /O(log n)/. Update the value at the maximal key. 797  798  > updateMaxWithKey (\ k a > Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"b"), (5,"5:a")] 799  > updateMaxWithKey (\ _ _ > Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" 800 590 801 updateMaxWithKey :: (k > a > Maybe a) > Map k a > Map k a 591 802 updateMaxWithKey f t 592 803 = case t of … … 598 809 599 810   /O(log n)/. Retrieves the minimal (key,value) pair of the map, and the map stripped from that element 600 811  @fail@s (in the monad) when passed an empty map. 812  813  > v < minViewWithKey (fromList [(5,"a"), (3,"b")]) 814  > v == ((3,"b"), singleton 5 "a") 815  > minViewWithKey empty Error: empty map 816 601 817 minViewWithKey :: Monad m => Map k a > m ((k,a), Map k a) 602 minViewWithKey Tip = fail "Map.minView : empty map"818 minViewWithKey Tip = fail "Map.minViewWithKey: empty map" 603 819 minViewWithKey x = return (deleteFindMin x) 604 820 605 821   /O(log n)/. Retrieves the maximal (key,value) pair of the map, and the map stripped from that element 606 822  @fail@s (in the monad) when passed an empty map. 823  824  > v < maxViewWithKey (fromList [(5,"a"), (3,"b")]) 825  > v == ((5,"a"), singleton 3 "b") 826  > maxViewWithKey empty Error: empty map 827 607 828 maxViewWithKey :: Monad m => Map k a > m ((k,a), Map k a) 608 maxViewWithKey Tip = fail "Map.maxView : empty map"829 maxViewWithKey Tip = fail "Map.maxViewWithKey: empty map" 609 830 maxViewWithKey x = return (deleteFindMax x) 610 831 611 832   /O(log n)/. Retrieves the minimal key\'s value of the map, and the map stripped from that element 612 833  @fail@s (in the monad) when passed an empty map. 834  835  > v < minView (fromList [(5,"a"), (3,"b")]) 836  > v == ("b", singleton 5 "a") 837  > minView empty Error: empty map 838 613 839 minView :: Monad m => Map k a > m (a, Map k a) 614 840 minView Tip = fail "Map.minView: empty map" 615 841 minView x = return (first snd $ deleteFindMin x) 616 842 617 843   /O(log n)/. Retrieves the maximal key\'s value of the map, and the map stripped from that element 618 844  @fail@s (in the monad) when passed an empty map. 845  846  > v < maxView (fromList [(5,"a"), (3,"b")]) 847  > v == ("a", singleton 3 "b") 848  > maxView empty Error: empty map 849 619 850 maxView :: Monad m => Map k a > m (a, Map k a) 620 851 maxView Tip = fail "Map.maxView: empty map" 621 852 maxView x = return (first snd $ deleteFindMax x) … … 629 860 } 630 861   The union of a list of maps: 631 862  (@'unions' == 'Prelude.foldl' 'union' 'empty'@). 863  864  > unions [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])] 865  > == fromList [(3, "b"), (5, "a"), (7, "C")] 866  > unions [(fromList [(5, "A3"), (3, "B3")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "a"), (3, "b")])] 867  > == fromList [(3, "B3"), (5, "A3"), (7, "C")] 868 632 869 unions :: Ord k => [Map k a] > Map k a 633 870 unions ts 634 871 = foldlStrict union empty ts 635 872 636 873   The union of a list of maps, with a combining operation: 637 874  (@'unionsWith' f == 'Prelude.foldl' ('unionWith' f) 'empty'@). 875  876  > unionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])] 877  > == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")] 878 638 879 unionsWith :: Ord k => (a>a>a) > [Map k a] > Map k a 639 880 unionsWith f ts 640 881 = foldlStrict (unionWith f) empty ts … … 644 885  It prefers @t1@ when duplicate keys are encountered, 645 886  i.e. (@'union' == 'unionWith' 'const'@). 646 887  The implementation uses the efficient /hedgeunion/ algorithm. 647  Hedgeunion is more efficient on (bigset `union` smallset) 888  Hedgeunion is more efficient on (bigset \``union`\` smallset). 889  890  > union (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "a"), (7, "C")] 891 648 892 union :: Ord k => Map k a > Map k a > Map k a 649 893 union Tip t2 = t2 650 894 union t1 Tip = t1 … … 681 925 Union with a combining function 682 926 } 683 927   /O(n+m)/. Union with a combining function. The implementation uses the efficient /hedgeunion/ algorithm. 928  929  > unionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "aA"), (7, "C")] 930 684 931 unionWith :: Ord k => (a > a > a) > Map k a > Map k a > Map k a 685 932 unionWith f m1 m2 686 933 = unionWithKey (\k x y > f x y) m1 m2 687 934 688 935   /O(n+m)/. 689 936  Union with a combining function. The implementation uses the efficient /hedgeunion/ algorithm. 690  Hedgeunion is more efficient on (bigset `union` smallset). 937  Hedgeunion is more efficient on (bigset \``union`\` smallset). 938  939  > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "" ++ old_value 940  > unionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "5:aA"), (7, "C")] 941 691 942 unionWithKey :: Ord k => (k > a > a > a) > Map k a > Map k a > Map k a 692 943 unionWithKey f Tip t2 = t2 693 944 unionWithKey f t1 Tip = t1 … … 712 963 Difference 713 964 } 714 965   /O(n+m)/. Difference of two maps. 966  Return elements of the first map not existing in the second map. 715 967  The implementation uses an efficient /hedge/ algorithm comparable with /hedgeunion/. 968  969  > difference (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 3 "b" 970 716 971 difference :: Ord k => Map k a > Map k b > Map k a 717 972 difference Tip t2 = Tip 718 973 difference t1 Tip = t1 … … 729 984 cmpkx k = compare kx k 730 985 731 986   /O(n+m)/. Difference with a combining function. 987  When two equal keys are 988  encountered, the combining function is applied to the values of these keys. 989  If it returns 'Nothing', the element is discarded (proper set difference). If 990  it returns (@'Just' y@), the element is updated with a new value @y@. 732 991  The implementation uses an efficient /hedge/ algorithm comparable with /hedgeunion/. 992  993  > let f al ar = if al == "b" then Just (al ++ ":" ++ ar) else Nothing 994  > differenceWith f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (7, "C")]) 995  > == singleton 3 "b:B" 996 733 997 differenceWith :: Ord k => (a > b > Maybe a) > Map k a > Map k b > Map k a 734 998 differenceWith f m1 m2 735 999 = differenceWithKey (\k x y > f x y) m1 m2 … … 739 1003  If it returns 'Nothing', the element is discarded (proper set difference). If 740 1004  it returns (@'Just' y@), the element is updated with a new value @y@. 741 1005  The implementation uses an efficient /hedge/ algorithm comparable with /hedgeunion/. 1006  1007  > let f k al ar = if al == "b" then Just ((show k) ++ ":" ++ al ++ "" ++ ar) else Nothing 1008  > differenceWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (10, "C")]) 1009  > == singleton 3 "3:bB" 1010 742 1011 differenceWithKey :: Ord k => (k > a > b > Maybe a) > Map k a > Map k b > Map k a 743 1012 differenceWithKey f Tip t2 = Tip 744 1013 differenceWithKey f t1 Tip = t1 … … 767 1036 { 768 1037 Intersection 769 1038 } 770   /O(n+m)/. Intersection of two maps. The values in the first 771  map are returned, i.e. (@'intersection' m1 m2 == 'intersectionWith' 'const' m1 m2@). 1039   /O(n+m)/. Intersection of two maps. 1040  Return data in the first map for the keys existing in both maps. 1041  (@'intersection' m1 m2 == 'intersectionWith' 'const' m1 m2@). 1042  1043  > intersection (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "a" 1044 772 1045 intersection :: Ord k => Map k a > Map k b > Map k a 773 1046 intersection m1 m2 774 1047 = intersectionWithKey (\k x y > x) m1 m2 775 1048 776 1049   /O(n+m)/. Intersection with a combining function. 1050  1051  > intersectionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "aA" 1052 777 1053 intersectionWith :: Ord k => (a > b > c) > Map k a > Map k b > Map k c 778 1054 intersectionWith f m1 m2 779 1055 = intersectionWithKey (\k x y > f x y) m1 m2 780 1056 781 1057   /O(n+m)/. Intersection with a combining function. 782  Intersection is more efficient on (bigset `intersection` smallset) 1058  Intersection is more efficient on (bigset \``intersection`\` smallset). 1059  1060  > let f k al ar = (show k) ++ ":" ++ al ++ "" ++ ar 1061  > intersectionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "5:aA" 1062 783 1063 intersectionWithKey :: Ord k => (k > a > b > c) > Map k a > Map k b > Map k c 784 1064 intersectionWithKey f Tip t = Tip 785 1065 intersectionWithKey f t Tip = Tip … … 796 1076  tl = intersectWithKey f lt l 797 1077  tr = intersectWithKey f gt r 798 1078 799 800 1079 intersectionWithKey :: Ord k => (k > a > b > c) > Map k a > Map k b > Map k c 801 1080 intersectionWithKey f Tip t = Tip 802 1081 intersectionWithKey f t Tip = Tip … … 820 1099 { 821 1100 Submap 822 1101 } 823   /O(n+m)/. 1102   /O(n+m)/. 824 1103  This function is defined as (@'isSubmapOf' = 'isSubmapOfBy' (==)@). 1104  825 1105 isSubmapOf :: (Ord k,Eq a) => Map k a > Map k a > Bool 826 1106 isSubmapOf m1 m2 827 1107 = isSubmapOfBy (==) m1 m2 828 1108 829 {  /O(n+m)/. 1109 {  /O(n+m)/. 830 1110 The expression (@'isSubmapOfBy' f t1 t2@) returns 'True' if 831 1111 all keys in @t1@ are in tree @t2@, and when @f@ returns 'True' when 832 1112 applied to their respective values. For example, the following … … 841 1121 > isSubmapOfBy (==) (fromList [('a',2)]) (fromList [('a',1),('b',2)]) 842 1122 > isSubmapOfBy (<) (fromList [('a',1)]) (fromList [('a',1),('b',2)]) 843 1123 > isSubmapOfBy (==) (fromList [('a',1),('b',2)]) (fromList [('a',1)]) 1124 1125 844 1126 } 845 1127 isSubmapOfBy :: Ord k => (a>b>Bool) > Map k a > Map k b > Bool 846 1128 isSubmapOfBy f t1 t2 … … 876 1158 > isProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)]) 877 1159 > isProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)]) 878 1160 > isProperSubmapOfBy (<) (fromList [(1,1)]) (fromList [(1,1),(2,2)]) 1161 1162 879 1163 } 880 1164 isProperSubmapOfBy :: Ord k => (a > b > Bool) > Map k a > Map k b > Bool 881 1165 isProperSubmapOfBy f t1 t2 … … 885 1169 Filter and partition 886 1170 } 887 1171   /O(n)/. Filter all values that satisfy the predicate. 1172  1173  > filter (> "a") (fromList [(5,"a"), (3,"b")]) == singleton 3 "b" 1174  > filter (> "x") (fromList [(5,"a"), (3,"b")]) == empty 1175  > filter (< "a") (fromList [(5,"a"), (3,"b")]) == empty 1176 888 1177 filter :: Ord k => (a > Bool) > Map k a > Map k a 889 1178 filter p m 890 1179 = filterWithKey (\k x > p x) m 891 1180 892 1181   /O(n)/. Filter all keys\/values that satisfy the predicate. 1182  1183  > filterWithKey (\k _ > k > 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" 1184 893 1185 filterWithKey :: Ord k => (k > a > Bool) > Map k a > Map k a 894 1186 filterWithKey p Tip = Tip 895 1187 filterWithKey p (Bin _ kx x l r) … … 897 1189  otherwise = merge (filterWithKey p l) (filterWithKey p r) 898 1190 899 1191 900   /O(n)/. partition the map according to a predicate. The first1192   /O(n)/. Partition the map according to a predicate. The first 901 1193  map contains all elements that satisfy the predicate, the second all 902 1194  elements that fail the predicate. See also 'split'. 1195  1196  > partition (> "a") (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a") 1197  > partition (< "x") (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty) 1198  > partition (> "x") (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")]) 1199 903 1200 partition :: Ord k => (a > Bool) > Map k a > (Map k a,Map k a) 904 1201 partition p m 905 1202 = partitionWithKey (\k x > p x) m 906 1203 907   /O(n)/. partition the map according to a predicate. The first1204   /O(n)/. Partition the map according to a predicate. The first 908 1205  map contains all elements that satisfy the predicate, the second all 909 1206  elements that fail the predicate. See also 'split'. 1207  1208  > partitionWithKey (\ k _ > k > 3) (fromList [(5,"a"), (3,"b")]) == (singleton 5 "a", singleton 3 "b") 1209  > partitionWithKey (\ k _ > k < 7) (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty) 1210  > partitionWithKey (\ k _ > k > 7) (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")]) 1211 910 1212 partitionWithKey :: Ord k => (k > a > Bool) > Map k a > (Map k a,Map k a) 911 1213 partitionWithKey p Tip = (Tip,Tip) 912 1214 partitionWithKey p (Bin _ kx x l r) … … 917 1219 (r1,r2) = partitionWithKey p r 918 1220 919 1221   /O(n)/. Map values and collect the 'Just' results. 1222  1223  > let f x = if x == "a" then Just "new a" else Nothing 1224  > mapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a" 1225 920 1226 mapMaybe :: Ord k => (a > Maybe b) > Map k a > Map k b 921 1227 mapMaybe f m 922 1228 = mapMaybeWithKey (\k x > f x) m 923 1229 924 1230   /O(n)/. Map keys\/values and collect the 'Just' results. 1231  1232  > let f k _ = if k < 5 then Just ("key : " ++ (show k)) else Nothing 1233  > mapMaybeWithKey f (fromList [(5,"a"), (3,"b")]) == singleton 3 "key : 3" 1234 925 1235 mapMaybeWithKey :: Ord k => (k > a > Maybe b) > Map k a > Map k b 926 1236 mapMaybeWithKey f Tip = Tip 927 1237 mapMaybeWithKey f (Bin _ kx x l r) = case f kx x of … … 929 1239 Nothing > merge (mapMaybeWithKey f l) (mapMaybeWithKey f r) 930 1240 931 1241   /O(n)/. Map values and separate the 'Left' and 'Right' results. 1242  1243  > let f a = if a < "c" then Left a else Right a 1244  > mapEither f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) 1245  > == (fromList [(3,"b"), (5,"a")], fromList [(1,"x"), (7,"z")]) 1246  > 1247  > mapEither (\ a > Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) 1248  > == (empty, fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) 1249 932 1250 mapEither :: Ord k => (a > Either b c) > Map k a > (Map k b, Map k c) 933 1251 mapEither f m 934 1252 = mapEitherWithKey (\k x > f x) m 935 1253 936 1254   /O(n)/. Map keys\/values and separate the 'Left' and 'Right' results. 1255  1256  > let f k a = if k < 5 then Left (k * 2) else Right (a ++ a) 1257  > mapEitherWithKey f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) 1258  > == (fromList [(1,2), (3,6)], fromList [(5,"aa"), (7,"zz")]) 1259  > 1260  > mapEitherWithKey (\_ a > Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")]) 1261  > == (empty, fromList [(1,"x"), (3,"b"), (5,"a"), (7,"z")]) 1262 937 1263 mapEitherWithKey :: Ord k => 938 1264 (k > a > Either b c) > Map k a > (Map k b, Map k c) 939 1265 mapEitherWithKey f Tip = (Tip, Tip) … … 948 1274 Mapping 949 1275 } 950 1276   /O(n)/. Map a function over all values in the map. 1277  1278  > map (++ "x") (fromList [(5,"a"), (3,"b")]) == fromList [(3, "bx"), (5, "ax")] 1279 951 1280 map :: (a > b) > Map k a > Map k b 952 1281 map f m 953 1282 = mapWithKey (\k x > f x) m 954 1283 955 1284   /O(n)/. Map a function over all values in the map. 1285  1286  > let f key x = (show key) ++ ":" ++ x 1287  > mapWithKey f (fromList [(5,"a"), (3,"b")]) == fromList [(3, "3:b"), (5, "5:a")] 1288 956 1289 mapWithKey :: (k > a > b) > Map k a > Map k b 957 1290 mapWithKey f Tip = Tip 958 1291 mapWithKey f (Bin sx kx x l r) … … 960 1293 961 1294   /O(n)/. The function 'mapAccum' threads an accumulating 962 1295  argument through the map in ascending order of keys. 1296  1297  > let f a b = (a ++ b, b ++ "X") 1298  > mapAccum f "Everything: " (fromList [(5,"a"), (3,"b")]) == ("Everything: ba", fromList [(3, "bX"), (5, "aX")]) 1299 963 1300 mapAccum :: (a > b > (a,c)) > a > Map k b > (a,Map k c) 964 1301 mapAccum f a m 965 1302 = mapAccumWithKey (\a k x > f a x) a m 966 1303 967 1304   /O(n)/. The function 'mapAccumWithKey' threads an accumulating 968 1305  argument through the map in ascending order of keys. 1306  1307  > let f a k b = (a ++ " " ++ (show k) ++ "" ++ b, b ++ "X") 1308  > mapAccumWithKey f "Everything:" (fromList [(5,"a"), (3,"b")]) == ("Everything: 3b 5a", fromList [(3, "bX"), (5, "aX")]) 1309 969 1310 mapAccumWithKey :: (a > k > b > (a,c)) > a > Map k b > (a,Map k c) 970 1311 mapAccumWithKey f a t 971 1312 = mapAccumL f a t … … 994 1335 (a3,l') = mapAccumR f a2 l 995 1336 in (a3,Bin sx kx x' l' r') 996 1337 997   /O(n*log n)/. 1338   /O(n*log n)/. 998 1339  @'mapKeys' f s@ is the map obtained by applying @f@ to each key of @s@. 999 1340  1000 1341  The size of the result may be smaller if @f@ maps two or more distinct 1001 1342  keys to the same new key. In this case the value at the smallest of 1002 1343  these keys is retained. 1344  1345  > mapKeys (+ 1) (fromList [(5,"a"), (3,"b")]) == fromList [(4, "b"), (6, "a")] 1346  > mapKeys (\ _ > 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "c" 1347  > mapKeys (\ _ > 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "c" 1003 1348 1004 1349 mapKeys :: Ord k2 => (k1>k2) > Map k1 a > Map k2 a 1005 1350 mapKeys = mapKeysWith (\x y>x) 1006 1351 1007   /O(n*log n)/. 1352   /O(n*log n)/. 1008 1353  @'mapKeysWith' c f s@ is the map obtained by applying @f@ to each key of @s@. 1009 1354  1010 1355  The size of the result may be smaller if @f@ maps two or more distinct 1011 1356  keys to the same new key. In this case the associated values will be 1012 1357  combined using @c@. 1358  1359  > mapKeysWith (++) (\ _ > 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "cdab" 1360  > mapKeysWith (++) (\ _ > 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "cdab" 1013 1361 1014 1362 mapKeysWith :: Ord k2 => (a > a > a) > (k1>k2) > Map k1 a > Map k2 a 1015 1363 mapKeysWith c f = fromListWith c . List.map fFirst . toList … … 1019 1367   /O(n)/. 1020 1368  @'mapKeysMonotonic' f s == 'mapKeys' f s@, but works only when @f@ 1021 1369  is strictly monotonic. 1370  That is, for any values @x@ and @y@, if @x@ < @y@ then @f x@ < @f y@. 1022 1371  /The precondition is not checked./ 1023 1372  Semiformally, we have: 1024 1373  1025 1374  > and [x < y ==> f x < f y  x < ls, y < ls] 1026 1375  > ==> mapKeysMonotonic f s == mapKeys f s 1027 1376  > where ls = keys s 1377  1378  This means that @f@ maps distinct original keys to distinct resulting keys. 1379  This function has better performance than 'mapKeys'. 1380  1381  > mapKeysMonotonic (\ k > k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")] 1382  > valid (mapKeysMonotonic (\ k > k * 2) (fromList [(5,"a"), (3,"b")])) == True 1383  > valid (mapKeysMonotonic (\ _ > 1) (fromList [(5,"a"), (3,"b")])) == False 1028 1384 1029 1385 mapKeysMonotonic :: (k1>k2) > Map k1 a > Map k2 a 1030 1386 mapKeysMonotonic f Tip = Tip … … 1041 1397  1042 1398  > elems map = fold (:) [] map 1043 1399  1400  > let f a len = len + (length a) 1401  > fold f 0 (fromList [(5,"a"), (3,"bbb")]) == 4 1402 1044 1403 fold :: (a > b > b) > b > Map k a > b 1045 1404 fold f z m 1046 1405 = foldWithKey (\k x z > f x z) z m … … 1051 1410  1052 1411  > keys map = foldWithKey (\k x ks > k:ks) [] map 1053 1412  1413  > let f k a result = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")" 1414  > foldWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (5:a)(3:b)" 1415 1054 1416 foldWithKey :: (k > a > b > b) > b > Map k a > b 1055 1417 foldWithKey f z t 1056 1418 = foldr f z t … … 1075 1437 } 1076 1438   /O(n)/. 1077 1439  Return all elements of the map in the ascending order of their keys. 1440  1441  > elems (fromList [(5,"a"), (3,"b")]) == ["b","a"] 1442  > elems empty == [] 1443 1078 1444 elems :: Map k a > [a] 1079 1445 elems m 1080 1446 = [x  (k,x) < assocs m] 1081 1447 1082 1448   /O(n)/. Return all keys of the map in ascending order. 1449  1450  > keys (fromList [(5,"a"), (3,"b")]) == [3,5] 1451  > keys empty == [] 1452 1083 1453 keys :: Map k a > [k] 1084 1454 keys m 1085 1455 = [k  (k,x) < assocs m] 1086 1456 1087 1457   /O(n)/. The set of all keys of the map. 1458  1459  > keysSet (fromList [(5,"a"), (3,"b")]) == Data.Set.fromList [3,5] 1460  > keysSet empty == Data.Set.empty 1461 1088 1462 keysSet :: Map k a > Set.Set k 1089 1463 keysSet m = Set.fromDistinctAscList (keys m) 1090 1464 1091 1465   /O(n)/. Return all key\/value pairs in the map in ascending key order. 1466  1467  > assocs (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")] 1468  > assocs empty == [] 1469 1092 1470 assocs :: Map k a > [(k,a)] 1093 1471 assocs m 1094 1472 = toList m … … 1098 1476 use [foldlStrict] to reduce demand on the controlstack 1099 1477 } 1100 1478   /O(n*log n)/. Build a map from a list of key\/value pairs. See also 'fromAscList'. 1479  If the list contains more than one value for the same key, the last value 1480  for the key is retained. 1481  1482  > fromList [] == empty 1483  > fromList [(5,"a"), (3,"b"), (5, "c")] == fromList [(5,"c"), (3,"b")] 1484  > fromList [(5,"c"), (3,"b"), (5, "a")] == fromList [(5,"a"), (3,"b")] 1485 1101 1486 fromList :: Ord k => [(k,a)] > Map k a 1102 1487 fromList xs 1103 1488 = foldlStrict ins empty xs … … 1105 1490 ins t (k,x) = insert k x t 1106 1491 1107 1492   /O(n*log n)/. Build a map from a list of key\/value pairs with a combining function. See also 'fromAscListWith'. 1493  1494  > fromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")] 1495  > fromListWith (++) [] == empty 1496 1108 1497 fromListWith :: Ord k => (a > a > a) > [(k,a)] > Map k a 1109 1498 fromListWith f xs 1110 1499 = fromListWithKey (\k x y > f x y) xs 1111 1500 1112 1501   /O(n*log n)/. Build a map from a list of key\/value pairs with a combining function. See also 'fromAscListWithKey'. 1502  1503  > let f k a1 a2 = (show k) ++ a1 ++ a2 1504  > fromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "3ab"), (5, "5a5ba")] 1505  > fromListWithKey f [] == empty 1506 1113 1507 fromListWithKey :: Ord k => (k > a > a > a) > [(k,a)] > Map k a 1114 1508 fromListWithKey f xs 1115 1509 = foldlStrict ins empty xs … … 1117 1511 ins t (k,x) = insertWithKey f k x t 1118 1512 1119 1513   /O(n)/. Convert to a list of key\/value pairs. 1514  1515  > toList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")] 1516  > toList empty == [] 1517 1120 1518 toList :: Map k a > [(k,a)] 1121 1519 toList t = toAscList t 1122 1520 1123 1521   /O(n)/. Convert to an ascending list. 1522  1523  > toAscList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")] 1524 1124 1525 toAscList :: Map k a > [(k,a)] 1125 1526 toAscList t = foldr (\k x xs > (k,x):xs) [] t 1126 1527 1127   /O(n)/. 1528   /O(n)/. 1128 1529 toDescList :: Map k a > [(k,a)] 1129 1530 toDescList t = foldl (\xs k x > (k,x):xs) [] t 1130 1531 … … 1138 1539 } 1139 1540   /O(n)/. Build a map from an ascending list in linear time. 1140 1541  /The precondition (input list is ascending) is not checked./ 1542  1543  > fromAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")] 1544  > fromAscList [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "b")] 1545  > valid (fromAscList [(3,"b"), (5,"a"), (5,"b")]) == True 1546  > valid (fromAscList [(5,"a"), (3,"b"), (5,"b")]) == False 1547 1141 1548 fromAscList :: Eq k => [(k,a)] > Map k a 1142 1549 fromAscList xs 1143 1550 = fromAscListWithKey (\k x y > x) xs 1144 1551 1145 1552   /O(n)/. Build a map from an ascending list in linear time with a combining function for equal keys. 1146 1553  /The precondition (input list is ascending) is not checked./ 1554  1555  > fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")] 1556  > valid (fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")]) == True 1557  > valid (fromAscListWith (++) [(5,"a"), (3,"b"), (5,"b")]) == False 1558 1147 1559 fromAscListWith :: Eq k => (a > a > a) > [(k,a)] > Map k a 1148 1560 fromAscListWith f xs 1149 1561 = fromAscListWithKey (\k x y > f x y) xs … … 1151 1563   /O(n)/. Build a map from an ascending list in linear time with a 1152 1564  combining function for equal keys. 1153 1565  /The precondition (input list is ascending) is not checked./ 1566  1567  > let f k a1 a2 = (show k) ++ ":" ++ a1 ++ a2 1568  > fromAscListWithKey f [(3,"b"), (5,"a"), (5,"b"), (5,"b")] == fromList [(3, "b"), (5, "5:b5:ba")] 1569  > valid (fromAscListWithKey f [(3,"b"), (5,"a"), (5,"b"), (5,"b")]) == True 1570  > valid (fromAscListWithKey f [(5,"a"), (3,"b"), (5,"b"), (5,"b")]) == False 1571 1154 1572 fromAscListWithKey :: Eq k => (k > a > a > a) > [(k,a)] > Map k a 1155 1573 fromAscListWithKey f xs 1156 1574 = fromDistinctAscList (combineEq f xs) … … 1170 1588 1171 1589   /O(n)/. Build a map from an ascending list of distinct elements in linear time. 1172 1590  /The precondition is not checked./ 1591  1592  > fromDistinctAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")] 1593  > valid (fromDistinctAscList [(3,"b"), (5,"a")]) == True 1594  > valid (fromDistinctAscList [(3,"b"), (5,"a"), (5,"b")]) == False 1595 1173 1596 fromDistinctAscList :: [(k,a)] > Map k a 1174 1597 fromDistinctAscList xs 1175 1598 = build const (length xs) xs … … 1256 1679 Split 1257 1680 } 1258 1681   /O(log n)/. The expression (@'split' k map@) is a pair @(map1,map2)@ where 1259  the keys in @map1@ are smaller than @k@ and the keys in @map2@ larger than @k@. Any key equal to @k@ is found in neither @map1@ nor @map2@. 1682  the keys in @map1@ are smaller than @k@ and the keys in @map2@ larger than @k@. 1683  Any key equal to @k@ is found in neither @map1@ nor @map2@. 1684  1685  > split 2 (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3,"b"), (5,"a")]) 1686  > split 3 (fromList [(5,"a"), (3,"b")]) == (empty, singleton 5 "a") 1687  > split 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a") 1688  > split 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", empty) 1689  > split 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], empty) 1690 1260 1691 split :: Ord k => k > Map k a > (Map k a,Map k a) 1261 1692 split k Tip = (Tip,Tip) 1262 1693 split k (Bin sx kx x l r) … … 1267 1698 1268 1699   /O(log n)/. The expression (@'splitLookup' k map@) splits a map just 1269 1700  like 'split' but also returns @'lookup' k map@. 1701  1702  > splitLookup 2 (fromList [(5,"a"), (3,"b")]) == (empty, Nothing, fromList [(3,"b"), (5,"a")]) 1703  > splitLookup 3 (fromList [(5,"a"), (3,"b")]) == (empty, Just "b", singleton 5 "a") 1704  > splitLookup 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Nothing, singleton 5 "a") 1705  > splitLookup 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Just "a", empty) 1706  > splitLookup 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], Nothing, empty) 1707 1270 1708 splitLookup :: Ord k => k > Map k a > (Map k a,Maybe a,Map k a) 1271 1709 splitLookup k Tip = (Tip,Nothing,Tip) 1272 1710 splitLookup k (Bin sx kx x l r) … … 1370 1808 1371 1809 1372 1810   /O(log n)/. Delete and find the minimal element. 1811  1812  > deleteFindMin (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((3,"b"), fromList[(5,"a"), (10,"c")]) 1813  > deleteFindMin Error: can not return the minimal element of an empty map 1814 1373 1815 deleteFindMin :: Map k a > ((k,a),Map k a) 1374 1816 deleteFindMin t 1375 1817 = case t of … … 1378 1820 Tip > (error "Map.deleteFindMin: can not return the minimal element of an empty map", Tip) 1379 1821 1380 1822   /O(log n)/. Delete and find the maximal element. 1823  1824  > deleteFindMax (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((10,"c"), fromList [(3,"b"), (5,"a")]) 1825  > deleteFindMax empty Error: can not return the maximal element of an empty map 1826 1381 1827 deleteFindMax :: Map k a > ((k,a),Map k a) 1382 1828 deleteFindMax t 1383 1829 = case t of … … 1532 1978 1533 1979 1534 1980   /O(n)/. Show the tree that implements the map. The tree is shown 1535  in a compressed, hanging format. 1981  in a compressed, hanging format. See 'showTreeWith'. 1536 1982 showTree :: (Show k,Show a) => Map k a > String 1537 1983 showTree m 1538 1984 = showTreeWith showElem True False m … … 1633 2079 Assertions 1634 2080 } 1635 2081   /O(n)/. Test if the internal map structure is valid. 2082  2083  > valid (fromAscList [(3,"b"), (5,"a")]) == True 2084  > valid (fromAscList [(5,"a"), (3,"b")]) == False 2085 1636 2086 valid :: Ord k => Map k a > Bool 1637 2087 valid t 1638 2088 = balanced t && ordered t && validsize t