-
Notifications
You must be signed in to change notification settings - Fork 102
Add invariant checking #444
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
Merged
Merged
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c3c4943
Add valid
sjakobi 0fc5d49
Tweaks
sjakobi 298a444
Add Eq instances
sjakobi 7ae531d
Expose validation types
sjakobi 330c180
s/Validity/Debugging
sjakobi 6a438d2
Add test for intersection
sjakobi 31325e3
Add mappend
sjakobi 214711c
Fix validHash
sjakobi b03df23
Move debugging facilities to D.HM.I.Debug
sjakobi 07d6580
D.HM.I: Remove TypeApplications extension
sjakobi 7c5611d
Fix build with GHC 8.2
sjakobi b5c12b6
Document invariants on HashMap constructors
sjakobi 26d7a42
Re-number invariants
sjakobi 0e7a26c
Formatting
sjakobi 0d52bb2
Add haddocks for Error
sjakobi 79ea6e4
Improve docs for INV5
sjakobi 24f1fcb
valid: Optimize hash-checking
sjakobi 8c1db5d
Refactor valid
sjakobi 5a9e8b9
Add pFromListValid test
sjakobi d91259f
Fix hash matching bug
sjakobi ab15072
Add Bitmap invariant
sjakobi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
{-# LANGUAGE CPP #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
-- | = WARNING | ||
-- | ||
-- This module is considered __internal__. | ||
-- | ||
-- The Package Versioning Policy __does not apply__. | ||
-- | ||
-- The contents of this module may change __in any way whatsoever__ | ||
-- and __without any warning__ between minor versions of this package. | ||
-- | ||
-- Authors importing this module are expected to track development | ||
-- closely. | ||
-- | ||
-- = Description | ||
-- | ||
-- Debugging utilities for 'HashMap's. | ||
|
||
module Data.HashMap.Internal.Debug | ||
( valid | ||
, Validity(..) | ||
, Error(..) | ||
, SubHash | ||
, SubHashPath | ||
) where | ||
|
||
import Data.Bits (complement, countTrailingZeros, popCount, shiftL, | ||
unsafeShiftL, (.&.), (.|.)) | ||
import Data.Hashable (Hashable) | ||
import Data.HashMap.Internal (Bitmap, Hash, HashMap (..), Leaf (..), | ||
bitsPerSubkey, fullBitmap, hash, | ||
isLeafOrCollision, maxChildren, sparseIndex) | ||
import Data.Semigroup (Sum (..)) | ||
|
||
import qualified Data.HashMap.Internal.Array as A | ||
|
||
|
||
#if !MIN_VERSION_base(4,11,0) | ||
import Data.Semigroup (Semigroup (..)) | ||
#endif | ||
|
||
data Validity k = Invalid (Error k) SubHashPath | Valid | ||
deriving (Eq, Show) | ||
|
||
instance Semigroup (Validity k) where | ||
Valid <> y = y | ||
x <> _ = x | ||
|
||
instance Monoid (Validity k) where | ||
mempty = Valid | ||
mappend = (<>) | ||
|
||
-- | An error corresponding to a broken invariant. | ||
-- | ||
-- See 'HashMap' for the documentation of the invariants. | ||
data Error k | ||
= INV1_internal_Empty | ||
| INV2_bad_BitmapIndexed_size !Int | ||
| INV3_bitmap_array_size_mismatch !Bitmap !Int | ||
| INV4_BitmapIndexed_invalid_single_subtree | ||
| INV5_misplaced_hash !Hash | ||
| INV6_key_hash_mismatch k !Hash | ||
| INV7_bad_Full_size !Int | ||
| INV8_Collision_size !Int | ||
| INV9_Collision_duplicate_key k !Hash | ||
sjakobi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
deriving (Eq, Show) | ||
|
||
-- TODO: Name this 'Index'?! | ||
-- (https://github.com/haskell-unordered-containers/unordered-containers/issues/425) | ||
-- | A part of a 'Hash' with 'bitsPerSubkey' bits. | ||
type SubHash = Word | ||
|
||
data SubHashPath = SubHashPath | ||
{ partialHash :: !Word | ||
-- ^ The bits we already know, starting from the lower bits. | ||
-- The unknown upper bits are @0@. | ||
, lengthInBits :: !Int | ||
-- ^ The number of bits known. | ||
} deriving (Eq, Show) | ||
|
||
initialSubHashPath :: SubHashPath | ||
initialSubHashPath = SubHashPath 0 0 | ||
|
||
addSubHash :: SubHashPath -> SubHash -> SubHashPath | ||
addSubHash (SubHashPath ph l) sh = | ||
SubHashPath (ph .|. (sh `unsafeShiftL` l)) (l + bitsPerSubkey) | ||
|
||
hashMatchesSubHashPath :: SubHashPath -> Hash -> Bool | ||
hashMatchesSubHashPath (SubHashPath ph l) h = maskToLength h l == ph | ||
where | ||
-- Note: This needs to use `shiftL` instead of `unsafeShiftL` because | ||
-- @l'@ may be greater than 32/64 at the deepest level. | ||
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. Greater than, or just equal to? 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.
|
||
maskToLength h' l' = h' .&. complement (complement 0 `shiftL` l') | ||
|
||
valid :: Hashable k => HashMap k v -> Validity k | ||
valid Empty = Valid | ||
valid t = validInternal initialSubHashPath t | ||
where | ||
validInternal p Empty = Invalid INV1_internal_Empty p | ||
validInternal p (Leaf h l) = validHash p h <> validLeaf p h l | ||
validInternal p (Collision h ary) = validHash p h <> validCollision p h ary | ||
validInternal p (BitmapIndexed b ary) = validBitmapIndexed p b ary | ||
validInternal p (Full ary) = validFull p ary | ||
|
||
validHash p h | hashMatchesSubHashPath p h = Valid | ||
| otherwise = Invalid (INV5_misplaced_hash h) p | ||
|
||
validLeaf p h (L k _) | hash k == h = Valid | ||
| otherwise = Invalid (INV6_key_hash_mismatch k h) p | ||
|
||
validCollision p h ary = validCollisionSize <> A.foldMap (validLeaf p h) ary <> distinctKeys | ||
where | ||
n = A.length ary | ||
validCollisionSize | n < 2 = Invalid (INV8_Collision_size n) p | ||
| otherwise = Valid | ||
distinctKeys = A.foldMap (\(L k _) -> appearsOnce k) ary | ||
appearsOnce k | A.foldMap (\(L k' _) -> if k' == k then Sum @Int 1 else Sum 0) ary == 1 = Valid | ||
| otherwise = Invalid (INV9_Collision_duplicate_key k h) p | ||
|
||
validBitmapIndexed p b ary = validArraySize <> validSubTrees p b ary | ||
where | ||
n = A.length ary | ||
validArraySize | n < 1 || n >= maxChildren = Invalid (INV2_bad_BitmapIndexed_size n) p | ||
| popCount b == n = Valid | ||
| otherwise = Invalid (INV3_bitmap_array_size_mismatch b n) p | ||
|
||
validSubTrees p b ary | ||
| A.length ary == 1 | ||
, isLeafOrCollision (A.index ary 0) | ||
= Invalid INV4_BitmapIndexed_invalid_single_subtree p | ||
| otherwise = go b | ||
where | ||
go 0 = Valid | ||
go b' = validInternal (addSubHash p (fromIntegral c)) (A.index ary i) <> go b'' | ||
where | ||
c = countTrailingZeros b' | ||
m = 1 `unsafeShiftL` c | ||
i = sparseIndex b m | ||
b'' = b' .&. complement m | ||
|
||
validFull p ary = validArraySize <> validSubTrees p fullBitmap ary | ||
where | ||
n = A.length ary | ||
validArraySize | n == maxChildren = Valid | ||
| otherwise = Invalid (INV7_bad_Full_size n) p |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.