Skip to content

Commit 6a6ad7d

Browse files
committed
Improve Haddocks for partial functions
Document partial functions in a consistent manner. Specify the error condition and total alternatives.
1 parent c83961f commit 6a6ad7d

File tree

12 files changed

+119
-52
lines changed

12 files changed

+119
-52
lines changed

containers/src/Data/IntMap/Internal.hs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ deriving instance Lift a => Lift (IntMap a)
416416
-- | \(O(\min(n,W))\). Find the value at a key.
417417
-- Calls 'error' when the element can not be found.
418418
--
419+
-- __Note__: This function is partial. Prefer '!?'.
420+
--
419421
-- > fromList [(5,'a'), (3,'b')] ! 1 Error: element not in the map
420422
-- > fromList [(5,'a'), (3,'b')] ! 5 == 'a'
421423

@@ -2325,14 +2327,18 @@ minView :: IntMap a -> Maybe (a, IntMap a)
23252327
minView t = fmap (\((_, x), t') -> (x, t')) (minViewWithKey t)
23262328

23272329
-- | \(O(\min(n,W))\). Delete and find the maximal element.
2328-
-- This function throws an error if the map is empty. Use 'maxViewWithKey'
2329-
-- if the map may be empty.
2330+
--
2331+
-- Calls 'error' if the map is empty.
2332+
--
2333+
-- __Note__: This function is partial. Prefer 'maxViewWithKey'.
23302334
deleteFindMax :: IntMap a -> ((Key, a), IntMap a)
23312335
deleteFindMax = fromMaybe (error "deleteFindMax: empty map has no maximal element") . maxViewWithKey
23322336

23332337
-- | \(O(\min(n,W))\). Delete and find the minimal element.
2334-
-- This function throws an error if the map is empty. Use 'minViewWithKey'
2335-
-- if the map may be empty.
2338+
--
2339+
-- Calls 'error' if the map is empty.
2340+
--
2341+
-- __Note__: This function is partial. Prefer 'minViewWithKey'.
23362342
deleteFindMin :: IntMap a -> ((Key, a), IntMap a)
23372343
deleteFindMin = fromMaybe (error "deleteFindMin: empty map has no minimal element") . minViewWithKey
23382344

@@ -2366,6 +2372,8 @@ lookupMin (Bin p l r) =
23662372
{-# INLINE lookupMin #-} -- See Note [Inline lookupMin] in Data.Set.Internal
23672373

23682374
-- | \(O(\min(n,W))\). The minimal key of the map. Calls 'error' if the map is empty.
2375+
--
2376+
-- __Note__: This function is partial. Prefer 'lookupMin'.
23692377
findMin :: IntMap a -> (Key, a)
23702378
findMin t
23712379
| Just r <- lookupMin t = r
@@ -2385,6 +2393,8 @@ lookupMax (Bin p l r) =
23852393
{-# INLINE lookupMax #-} -- See Note [Inline lookupMin] in Data.Set.Internal
23862394

23872395
-- | \(O(\min(n,W))\). The maximal key of the map. Calls 'error' if the map is empty.
2396+
--
2397+
-- __Note__: This function is partial. Prefer 'lookupMax'.
23882398
findMax :: IntMap a -> (Key, a)
23892399
findMax t
23902400
| Just r <- lookupMax t = r

containers/src/Data/IntMap/Lazy.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,8 @@ module Data.IntMap.Lazy (
249249
-- * Min\/Max
250250
, lookupMin
251251
, lookupMax
252-
, findMin
253-
, findMax
254252
, deleteMin
255253
, deleteMax
256-
, deleteFindMin
257-
, deleteFindMax
258254
, updateMin
259255
, updateMax
260256
, updateMinWithKey
@@ -263,6 +259,10 @@ module Data.IntMap.Lazy (
263259
, maxView
264260
, minViewWithKey
265261
, maxViewWithKey
262+
, findMin
263+
, findMax
264+
, deleteFindMin
265+
, deleteFindMax
266266
) where
267267

268268
import Data.IntMap.Internal as IM

containers/src/Data/IntMap/Strict.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,8 @@ module Data.IntMap.Strict (
267267
-- * Min\/Max
268268
, lookupMin
269269
, lookupMax
270-
, findMin
271-
, findMax
272270
, deleteMin
273271
, deleteMax
274-
, deleteFindMin
275-
, deleteFindMax
276272
, updateMin
277273
, updateMax
278274
, updateMinWithKey
@@ -281,6 +277,10 @@ module Data.IntMap.Strict (
281277
, maxView
282278
, minViewWithKey
283279
, maxViewWithKey
280+
, findMin
281+
, findMax
282+
, deleteFindMin
283+
, deleteFindMax
284284
) where
285285

286286
import Data.IntMap.Strict.Internal

containers/src/Data/IntSet.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,14 @@ module Data.IntSet (
166166
-- * Min\/Max
167167
, lookupMin
168168
, lookupMax
169-
, findMin
170-
, findMax
171169
, deleteMin
172170
, deleteMax
173-
, deleteFindMin
174-
, deleteFindMax
175171
, maxView
176172
, minView
173+
, findMin
174+
, findMax
175+
, deleteFindMin
176+
, deleteFindMax
177177

178178
-- * Conversion
179179

containers/src/Data/IntSet/Internal.hs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,13 +1099,17 @@ minView t =
10991099

11001100
-- | \(O(\min(n,W))\). Delete and find the minimal element.
11011101
--
1102-
-- > deleteFindMin set = (findMin set, deleteMin set)
1102+
-- Calls 'error' if the set is empty.
1103+
--
1104+
-- __Note__: This function is partial. Prefer 'minView'.
11031105
deleteFindMin :: IntSet -> (Key, IntSet)
11041106
deleteFindMin = fromMaybe (error "deleteFindMin: empty set has no minimal element") . minView
11051107

11061108
-- | \(O(\min(n,W))\). Delete and find the maximal element.
11071109
--
1108-
-- > deleteFindMax set = (findMax set, deleteMax set)
1110+
-- Calls 'error' if the set is empty.
1111+
--
1112+
-- __Note__: This function is partial. Prefer 'maxView'.
11091113
deleteFindMax :: IntSet -> (Key, IntSet)
11101114
deleteFindMax = fromMaybe (error "deleteFindMax: empty set has no maximal element") . maxView
11111115

@@ -1126,6 +1130,8 @@ lookupMin (Bin p l r) = Just $! lookupMinSure (if signBranch p then r else l)
11261130

11271131
-- | \(O(\min(n,W))\). The minimal element of the set. Calls 'error' if the set
11281132
-- is empty.
1133+
--
1134+
-- __Note__: This function is partial. Prefer 'lookupMin'.
11291135
findMin :: IntSet -> Key
11301136
findMin t
11311137
| Just r <- lookupMin t = r
@@ -1148,6 +1154,8 @@ lookupMax (Bin p l r) = Just $! lookupMaxSure (if signBranch p then l else r)
11481154

11491155
-- | \(O(\min(n,W))\). The maximal element of the set. Calls 'error' if the set
11501156
-- is empty.
1157+
--
1158+
-- __Note__: This function is partial. Prefer 'lookupMax'.
11511159
findMax :: IntSet -> Key
11521160
findMax t
11531161
| Just r <- lookupMax t = r

containers/src/Data/Map/Internal.hs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ infixl 9 !,!?,\\ --
434434
-- | \(O(\log n)\). Find the value at a key.
435435
-- Calls 'error' when the element can not be found.
436436
--
437+
-- __Note__: This function is partial. Prefer '!?'.
438+
--
437439
-- > fromList [(5,'a'), (3,'b')] ! 1 Error: element not in the map
438440
-- > fromList [(5,'a'), (3,'b')] ! 5 == 'a'
439441

@@ -621,8 +623,6 @@ notMember k m = not $ member k m
621623
{-# INLINE notMember #-}
622624
#endif
623625

624-
-- | \(O(\log n)\). Find the value at a key.
625-
-- Calls 'error' when the element can not be found.
626626
find :: Ord k => k -> Map k a -> a
627627
find = go
628628
where
@@ -1454,6 +1454,8 @@ alterFYoneda = go
14541454
-- including, the 'size' of the map. Calls 'error' when the key is not
14551455
-- a 'member' of the map.
14561456
--
1457+
-- __Note__: This function is partial. Prefer 'lookupIndex'.
1458+
--
14571459
-- > findIndex 2 (fromList [(5,"a"), (3,"b")]) Error: element is not in the map
14581460
-- > findIndex 3 (fromList [(5,"a"), (3,"b")]) == 0
14591461
-- > findIndex 5 (fromList [(5,"a"), (3,"b")]) == 1
@@ -1500,6 +1502,8 @@ lookupIndex = go 0
15001502
-- index in the sequence sorted by keys. If the /index/ is out of range (less
15011503
-- than zero, greater or equal to 'size' of the map), 'error' is called.
15021504
--
1505+
-- __Note__: This function is partial.
1506+
--
15031507
-- > elemAt 0 (fromList [(5,"a"), (3,"b")]) == (3,"b")
15041508
-- > elemAt 1 (fromList [(5,"a"), (3,"b")]) == (5, "a")
15051509
-- > elemAt 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range
@@ -1584,6 +1588,8 @@ splitAt i0 m0
15841588
-- the sequence sorted by keys. If the /index/ is out of range (less than zero,
15851589
-- greater or equal to 'size' of the map), 'error' is called.
15861590
--
1591+
-- __Note__: This function is partial.
1592+
--
15871593
-- > updateAt (\ _ _ -> Just "x") 0 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "x"), (5, "a")]
15881594
-- > updateAt (\ _ _ -> Just "x") 1 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "x")]
15891595
-- > updateAt (\ _ _ -> Just "x") 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range
@@ -1610,6 +1616,8 @@ updateAt f !i t =
16101616
-- the sequence sorted by keys. If the /index/ is out of range (less than zero,
16111617
-- greater or equal to 'size' of the map), 'error' is called.
16121618
--
1619+
-- __Note__: This function is partial.
1620+
--
16131621
-- > deleteAt 0 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
16141622
-- > deleteAt 1 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
16151623
-- > deleteAt 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range
@@ -1667,6 +1675,8 @@ lookupMin (Bin _ k x l _) = Just $! kvToTuple (lookupMinSure k x l)
16671675

16681676
-- | \(O(\log n)\). The minimal key of the map. Calls 'error' if the map is empty.
16691677
--
1678+
-- __Note__: This function is partial. Prefer 'lookupMin'.
1679+
--
16701680
-- > findMin (fromList [(5,"a"), (3,"b")]) == (3,"b")
16711681
-- > findMin empty Error: empty map has no minimal element
16721682

@@ -1693,6 +1703,8 @@ lookupMax (Bin _ k x _ r) = Just $! kvToTuple (lookupMaxSure k x r)
16931703

16941704
-- | \(O(\log n)\). The maximal key of the map. Calls 'error' if the map is empty.
16951705
--
1706+
-- __Note__: This function is partial. Prefer 'lookupMax'.
1707+
--
16961708
-- > findMax (fromList [(5,"a"), (3,"b")]) == (5,"a")
16971709
-- > findMax empty Error: empty map has no maximal element
16981710

@@ -4105,19 +4117,19 @@ maxViewSure !k x !l r = case r of
41054117

41064118
-- | \(O(\log n)\). Delete and find the minimal element.
41074119
--
4108-
-- > deleteFindMin (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((3,"b"), fromList[(5,"a"), (10,"c")])
4109-
-- > deleteFindMin empty Error: can not return the minimal element of an empty map
4110-
4120+
-- Calls 'error' if the map is empty.
4121+
--
4122+
-- __Note__: This function is partial. Prefer 'minViewWithKey'.
41114123
deleteFindMin :: Map k a -> ((k,a),Map k a)
41124124
deleteFindMin t = case minViewWithKey t of
41134125
Nothing -> (error "Map.deleteFindMin: can not return the minimal element of an empty map", Tip)
41144126
Just res -> res
41154127

41164128
-- | \(O(\log n)\). Delete and find the maximal element.
41174129
--
4118-
-- > deleteFindMax (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((10,"c"), fromList [(3,"b"), (5,"a")])
4119-
-- > deleteFindMax empty Error: can not return the maximal element of an empty map
4120-
4130+
-- Calls 'error' if the map is empty.
4131+
--
4132+
-- __Note__: This function is partial. Prefer 'maxViewWithKey'.
41214133
deleteFindMax :: Map k a -> ((k,a),Map k a)
41224134
deleteFindMax t = case maxViewWithKey t of
41234135
Nothing -> (error "Map.deleteFindMax: can not return the maximal element of an empty map", Tip)

containers/src/Data/Map/Lazy.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,8 @@ module Data.Map.Lazy (
272272
-- * Min\/Max
273273
, lookupMin
274274
, lookupMax
275-
, findMin
276-
, findMax
277275
, deleteMin
278276
, deleteMax
279-
, deleteFindMin
280-
, deleteFindMax
281277
, updateMin
282278
, updateMax
283279
, updateMinWithKey
@@ -286,6 +282,10 @@ module Data.Map.Lazy (
286282
, maxView
287283
, minViewWithKey
288284
, maxViewWithKey
285+
, findMin
286+
, findMax
287+
, deleteFindMin
288+
, deleteFindMax
289289

290290
-- * Debugging
291291
, valid

containers/src/Data/Map/Strict.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,8 @@ module Data.Map.Strict
287287
-- * Min\/Max
288288
, lookupMin
289289
, lookupMax
290-
, findMin
291-
, findMax
292290
, deleteMin
293291
, deleteMax
294-
, deleteFindMin
295-
, deleteFindMax
296292
, updateMin
297293
, updateMax
298294
, updateMinWithKey
@@ -301,6 +297,10 @@ module Data.Map.Strict
301297
, maxView
302298
, minViewWithKey
303299
, maxViewWithKey
300+
, findMin
301+
, findMax
302+
, deleteFindMin
303+
, deleteFindMax
304304

305305
-- * Debugging
306306
, valid

containers/src/Data/Map/Strict/Internal.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,8 @@ atKeyIdentity k f t = Identity $ atKeyPlain Strict k (coerce f) t
838838
-- | \(O(\log n)\). Update the element at /index/. Calls 'error' when an
839839
-- invalid index is used.
840840
--
841+
-- __Note__: This function is partial.
842+
--
841843
-- > updateAt (\ _ _ -> Just "x") 0 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "x"), (5, "a")]
842844
-- > updateAt (\ _ _ -> Just "x") 1 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "x")]
843845
-- > updateAt (\ _ _ -> Just "x") 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range

0 commit comments

Comments
 (0)