-
Notifications
You must be signed in to change notification settings - Fork 183
Added HasCallStack to partial functions #493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -431,7 +431,9 @@ infixl 9 !,!?,\\ -- | |
#else | ||
(!) :: Ord k => Map k a -> k -> a | ||
#endif | ||
(!) m k = find k m | ||
(!) m k | ||
| Just a <- lookup k m = a | ||
| otherwise = error "Map.!: given key is not an element in the map" | ||
#if __GLASGOW_HASKELL__ | ||
{-# INLINE (!) #-} | ||
#endif | ||
|
@@ -609,22 +611,6 @@ notMember k m = not $ member k m | |
{-# INLINE notMember #-} | ||
#endif | ||
|
||
-- | /O(log n)/. Find the value at a key. | ||
-- Calls 'error' when the element can not be found. | ||
find :: Ord k => k -> Map k a -> a | ||
find = go | ||
where | ||
go !_ Tip = error "Map.!: given key is not an element in the map" | ||
go k (Bin _ kx x l r) = case compare k kx of | ||
LT -> go k l | ||
GT -> go k r | ||
EQ -> x | ||
#if __GLASGOW_HASKELL__ | ||
{-# INLINABLE find #-} | ||
#else | ||
{-# INLINE find #-} | ||
#endif | ||
|
||
-- | /O(log n)/. The expression @('findWithDefault' def k map)@ returns | ||
-- the value at key @k@ or returns default value @def@ | ||
-- when the key is not in the map. | ||
|
@@ -1490,9 +1476,17 @@ lookupIndex = go 0 | |
|
||
#if __GLASGOW_HASKELL__ >= 800 | ||
elemAt :: HasCallStack => Int -> Map k a -> (k,a) | ||
elemAt = go where | ||
go !_ Tip = error "Map.elemAt: index out of range" | ||
go i (Bin _ kx x l r) | ||
= case compare i sizeL of | ||
LT -> elemAt i l | ||
GT -> elemAt (i-sizeL-1) r | ||
EQ -> (kx,x) | ||
where | ||
sizeL = size l | ||
#else | ||
elemAt :: Int -> Map k a -> (k,a) | ||
#endif | ||
elemAt !_ Tip = error "Map.elemAt: index out of range" | ||
elemAt i (Bin _ kx x l r) | ||
= case compare i sizeL of | ||
|
@@ -1501,6 +1495,7 @@ elemAt i (Bin _ kx x l r) | |
EQ -> (kx,x) | ||
where | ||
sizeL = size l | ||
#endif | ||
|
||
-- | Take a given number of entries in key order, beginning | ||
-- with the smallest keys. | ||
|
@@ -1583,9 +1578,20 @@ splitAt i0 m0 | |
|
||
#if __GLASGOW_HASKELL__ >= 800 | ||
updateAt :: HasCallStack => (k -> a -> Maybe a) -> Int -> Map k a -> Map k a | ||
updateAt = go where | ||
go f !i t = | ||
case t of | ||
Tip -> error "Map.updateAt: index out of range" | ||
Bin sx kx x l r -> case compare i sizeL of | ||
LT -> balanceR kx x (go f i l) r | ||
GT -> balanceL kx x l (go f (i-sizeL-1) r) | ||
EQ -> case f kx x of | ||
Just x' -> Bin sx kx x' l r | ||
Nothing -> glue l r | ||
where | ||
sizeL = size l | ||
#else | ||
updateAt :: (k -> a -> Maybe a) -> Int -> Map k a -> Map k a | ||
#endif | ||
updateAt f !i t = | ||
case t of | ||
Tip -> error "Map.updateAt: index out of range" | ||
|
@@ -1597,6 +1603,7 @@ updateAt f !i t = | |
Nothing -> glue l r | ||
where | ||
sizeL = size l | ||
#endif | ||
|
||
-- | /O(log n)/. Delete the element at /index/, i.e. by its zero-based index in | ||
-- the sequence sorted by keys. If the /index/ is out of range (less than zero, | ||
|
@@ -1609,9 +1616,18 @@ updateAt f !i t = | |
|
||
#if __GLASGOW_HASKELL__ >= 800 | ||
deleteAt :: HasCallStack => Int -> Map k a -> Map k a | ||
deleteAt = go where | ||
go !i t = | ||
case t of | ||
Tip -> error "Map.deleteAt: index out of range" | ||
Bin _ kx x l r -> case compare i sizeL of | ||
LT -> balanceR kx x (go i l) r | ||
GT -> balanceL kx x l (go (i-sizeL-1) r) | ||
EQ -> glue l r | ||
where | ||
sizeL = size l | ||
#else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question here. |
||
deleteAt :: Int -> Map k a -> Map k a | ||
#endif | ||
deleteAt !i t = | ||
case t of | ||
Tip -> error "Map.deleteAt: index out of range" | ||
|
@@ -1621,6 +1637,7 @@ deleteAt !i t = | |
EQ -> glue l r | ||
where | ||
sizeL = size l | ||
#endif | ||
|
||
|
||
{-------------------------------------------------------------------- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -887,9 +887,20 @@ atKeyIdentity k f t = Identity $ atKeyPlain Strict k (coerce f) t | |
|
||
#if __GLASGOW_HASKELL__ >= 800 | ||
updateAt :: HasCallStack => (k -> a -> Maybe a) -> Int -> Map k a -> Map k a | ||
updateAt = go where | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this actually give the right call stacks? Thinking about this more carefully, I suspect you want to check if the operation will succeed, and only then call a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My tests show that, by default, functions like |
||
go f i t = i `seq` | ||
case t of | ||
Tip -> error "Map.updateAt: index out of range" | ||
Bin sx kx x l r -> case compare i sizeL of | ||
LT -> balanceR kx x (go f i l) r | ||
GT -> balanceL kx x l (go f (i-sizeL-1) r) | ||
EQ -> case f kx x of | ||
Just x' -> x' `seq` Bin sx kx x' l r | ||
Nothing -> glue l r | ||
where | ||
sizeL = size l | ||
#else | ||
updateAt :: (k -> a -> Maybe a) -> Int -> Map k a -> Map k a | ||
#endif | ||
updateAt f i t = i `seq` | ||
case t of | ||
Tip -> error "Map.updateAt: index out of range" | ||
|
@@ -901,6 +912,7 @@ updateAt f i t = i `seq` | |
Nothing -> glue l r | ||
where | ||
sizeL = size l | ||
#endif | ||
|
||
{-------------------------------------------------------------------- | ||
Minimal, Maximal | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
lookup
will incur aJust
allocation. One possible solution (for very recent GHC versions only) would be to define alookup#
function producing an unboxed sum:The (potentially) great thing about this is that the
Just
gets applied on the "outside", so inlining and the case-of-case transformation will end up making it go away altogether:Unlike a CPS version, this theoretically helps user-written code as well. However, I have not actually tried benchmarking anything like this. And the CPP is potentially troublesome too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems cool -- it seems like it could be a fairly big change that would even prevent an allocation when one uses
lookup
(or related functions that returnMaybe
values) and immediately unwraps -- but it's a bit big for what I was hoping to contribute just now (also, a bit over my head). I think I'll take the easy way out and go back to using afind
function to get a result without the extra allocation. Perhaps my next PR will be to overhaul the backend to use unboxed sums where possible :).