Skip to content

Commit 8999284

Browse files
authored
Improve Haddocks for partial functions (#1140)
Document partial functions in a consistent manner. Specify the error condition and mention total alternatives, if any.
1 parent 70b5b9e commit 8999284

File tree

12 files changed

+125
-52
lines changed

12 files changed

+125
-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

@@ -2328,14 +2330,18 @@ minView :: IntMap a -> Maybe (a, IntMap a)
23282330
minView t = fmap (\((_, x), t') -> (x, t')) (minViewWithKey t)
23292331

23302332
-- | \(O(\min(n,W))\). Delete and find the maximal element.
2331-
-- This function throws an error if the map is empty. Use 'maxViewWithKey'
2332-
-- if the map may be empty.
2333+
--
2334+
-- Calls 'error' if the map is empty.
2335+
--
2336+
-- __Note__: This function is partial. Prefer 'maxViewWithKey'.
23332337
deleteFindMax :: IntMap a -> ((Key, a), IntMap a)
23342338
deleteFindMax = fromMaybe (error "deleteFindMax: empty map has no maximal element") . maxViewWithKey
23352339

23362340
-- | \(O(\min(n,W))\). Delete and find the minimal element.
2337-
-- This function throws an error if the map is empty. Use 'minViewWithKey'
2338-
-- if the map may be empty.
2341+
--
2342+
-- Calls 'error' if the map is empty.
2343+
--
2344+
-- __Note__: This function is partial. Prefer 'minViewWithKey'.
23392345
deleteFindMin :: IntMap a -> ((Key, a), IntMap a)
23402346
deleteFindMin = fromMaybe (error "deleteFindMin: empty map has no minimal element") . minViewWithKey
23412347

@@ -2369,6 +2375,8 @@ lookupMin (Bin p l r) =
23692375
{-# INLINE lookupMin #-} -- See Note [Inline lookupMin] in Data.Set.Internal
23702376

23712377
-- | \(O(\min(n,W))\). The minimal key of the map. Calls 'error' if the map is empty.
2378+
--
2379+
-- __Note__: This function is partial. Prefer 'lookupMin'.
23722380
findMin :: IntMap a -> (Key, a)
23732381
findMin t
23742382
| Just r <- lookupMin t = r
@@ -2388,6 +2396,8 @@ lookupMax (Bin p l r) =
23882396
{-# INLINE lookupMax #-} -- See Note [Inline lookupMin] in Data.Set.Internal
23892397

23902398
-- | \(O(\min(n,W))\). The maximal key of the map. Calls 'error' if the map is empty.
2399+
--
2400+
-- __Note__: This function is partial. Prefer 'lookupMax'.
23912401
findMax :: IntMap a -> (Key, a)
23922402
findMax t
23932403
| 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
@@ -1103,13 +1103,17 @@ minView t =
11031103

11041104
-- | \(O(\min(n,W))\). Delete and find the minimal element.
11051105
--
1106-
-- > deleteFindMin set = (findMin set, deleteMin set)
1106+
-- Calls 'error' if the set is empty.
1107+
--
1108+
-- __Note__: This function is partial. Prefer 'minView'.
11071109
deleteFindMin :: IntSet -> (Key, IntSet)
11081110
deleteFindMin = fromMaybe (error "deleteFindMin: empty set has no minimal element") . minView
11091111

11101112
-- | \(O(\min(n,W))\). Delete and find the maximal element.
11111113
--
1112-
-- > deleteFindMax set = (findMax set, deleteMax set)
1114+
-- Calls 'error' if the set is empty.
1115+
--
1116+
-- __Note__: This function is partial. Prefer 'maxView'.
11131117
deleteFindMax :: IntSet -> (Key, IntSet)
11141118
deleteFindMax = fromMaybe (error "deleteFindMax: empty set has no maximal element") . maxView
11151119

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

11311135
-- | \(O(\min(n,W))\). The minimal element of the set. Calls 'error' if the set
11321136
-- is empty.
1137+
--
1138+
-- __Note__: This function is partial. Prefer 'lookupMin'.
11331139
findMin :: IntSet -> Key
11341140
findMin t
11351141
| Just r <- lookupMin t = r
@@ -1152,6 +1158,8 @@ lookupMax (Bin p l r) = Just $! lookupMaxSure (if signBranch p then l else r)
11521158

11531159
-- | \(O(\min(n,W))\). The maximal element of the set. Calls 'error' if the set
11541160
-- is empty.
1161+
--
1162+
-- __Note__: This function is partial. Prefer 'lookupMax'.
11551163
findMax :: IntSet -> Key
11561164
findMax t
11571165
| 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

@@ -623,8 +625,6 @@ notMember k m = not $ member k m
623625
{-# INLINE notMember #-}
624626
#endif
625627

626-
-- | \(O(\log n)\). Find the value at a key.
627-
-- Calls 'error' when the element can not be found.
628628
find :: Ord k => k -> Map k a -> a
629629
find = go
630630
where
@@ -1456,6 +1456,8 @@ alterFYoneda = go
14561456
-- including, the 'size' of the map. Calls 'error' when the key is not
14571457
-- a 'member' of the map.
14581458
--
1459+
-- __Note__: This function is partial. Prefer 'lookupIndex'.
1460+
--
14591461
-- > findIndex 2 (fromList [(5,"a"), (3,"b")]) Error: element is not in the map
14601462
-- > findIndex 3 (fromList [(5,"a"), (3,"b")]) == 0
14611463
-- > findIndex 5 (fromList [(5,"a"), (3,"b")]) == 1
@@ -1502,6 +1504,8 @@ lookupIndex = go 0
15021504
-- index in the sequence sorted by keys. If the /index/ is out of range (less
15031505
-- than zero, greater or equal to 'size' of the map), 'error' is called.
15041506
--
1507+
-- __Note__: This function is partial.
1508+
--
15051509
-- > elemAt 0 (fromList [(5,"a"), (3,"b")]) == (3,"b")
15061510
-- > elemAt 1 (fromList [(5,"a"), (3,"b")]) == (5, "a")
15071511
-- > elemAt 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range
@@ -1586,6 +1590,8 @@ splitAt i0 m0
15861590
-- the sequence sorted by keys. If the /index/ is out of range (less than zero,
15871591
-- greater or equal to 'size' of the map), 'error' is called.
15881592
--
1593+
-- __Note__: This function is partial.
1594+
--
15891595
-- > updateAt (\ _ _ -> Just "x") 0 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "x"), (5, "a")]
15901596
-- > updateAt (\ _ _ -> Just "x") 1 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "x")]
15911597
-- > updateAt (\ _ _ -> Just "x") 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range
@@ -1612,6 +1618,8 @@ updateAt f !i t =
16121618
-- the sequence sorted by keys. If the /index/ is out of range (less than zero,
16131619
-- greater or equal to 'size' of the map), 'error' is called.
16141620
--
1621+
-- __Note__: This function is partial.
1622+
--
16151623
-- > deleteAt 0 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
16161624
-- > deleteAt 1 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
16171625
-- > deleteAt 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range
@@ -1669,6 +1677,8 @@ lookupMin (Bin _ k x l _) = Just $! kvToTuple (lookupMinSure k x l)
16691677

16701678
-- | \(O(\log n)\). The minimal key of the map. Calls 'error' if the map is empty.
16711679
--
1680+
-- __Note__: This function is partial. Prefer 'lookupMin'.
1681+
--
16721682
-- > findMin (fromList [(5,"a"), (3,"b")]) == (3,"b")
16731683
-- > findMin empty Error: empty map has no minimal element
16741684

@@ -1695,6 +1705,8 @@ lookupMax (Bin _ k x _ r) = Just $! kvToTuple (lookupMaxSure k x r)
16951705

16961706
-- | \(O(\log n)\). The maximal key of the map. Calls 'error' if the map is empty.
16971707
--
1708+
-- __Note__: This function is partial. Prefer 'lookupMax'.
1709+
--
16981710
-- > findMax (fromList [(5,"a"), (3,"b")]) == (5,"a")
16991711
-- > findMax empty Error: empty map has no maximal element
17001712

@@ -4107,19 +4119,19 @@ maxViewSure !k x !l r = case r of
41074119

41084120
-- | \(O(\log n)\). Delete and find the minimal element.
41094121
--
4110-
-- > deleteFindMin (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((3,"b"), fromList[(5,"a"), (10,"c")])
4111-
-- > deleteFindMin empty Error: can not return the minimal element of an empty map
4112-
4122+
-- Calls 'error' if the map is empty.
4123+
--
4124+
-- __Note__: This function is partial. Prefer 'minViewWithKey'.
41134125
deleteFindMin :: Map k a -> ((k,a),Map k a)
41144126
deleteFindMin t = case minViewWithKey t of
41154127
Nothing -> (error "Map.deleteFindMin: can not return the minimal element of an empty map", Tip)
41164128
Just res -> res
41174129

41184130
-- | \(O(\log n)\). Delete and find the maximal element.
41194131
--
4120-
-- > deleteFindMax (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((10,"c"), fromList [(3,"b"), (5,"a")])
4121-
-- > deleteFindMax empty Error: can not return the maximal element of an empty map
4122-
4132+
-- Calls 'error' if the map is empty.
4133+
--
4134+
-- __Note__: This function is partial. Prefer 'maxViewWithKey'.
41234135
deleteFindMax :: Map k a -> ((k,a),Map k a)
41244136
deleteFindMax t = case maxViewWithKey t of
41254137
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)