Skip to content

Commit

Permalink
fixup rename Pam to InvertedMap
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrisby committed Mar 2, 2020
1 parent 3acec92 commit 3de1fc8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 70 deletions.
Expand Up @@ -38,11 +38,11 @@ library
Test.Util.FS.Sim.MockFS
Test.Util.FS.Sim.Pure
Test.Util.FS.Sim.STM
Test.Util.InvertedMap
Test.Util.MockChain
Test.Util.Orphans.Arbitrary
Test.Util.Orphans.IOLike
Test.Util.Orphans.NoUnexpectedThunks
Test.Util.Pam
Test.Util.QSM
Test.Util.QuickCheck
Test.Util.Random
Expand Down
Expand Up @@ -49,8 +49,8 @@ import Ouroboros.Consensus.Util.Condense (Condense (..))

import Test.ThreadNet.Util.NodeJoinPlan

import Test.Util.Pam (Pam)
import qualified Test.Util.Pam as Pam
import Test.Util.InvertedMap (InvertedMap)
import qualified Test.Util.InvertedMap as InvertedMap

oneK :: Num a => PBftParams -> a
oneK PBftParams{pbftSecurityParam} =
Expand Down Expand Up @@ -360,7 +360,7 @@ simulateShort params nodeJoinPlan (NumSlots t)
-- See 'Result'.
--
data ShortState = ShortState
{ ssAbsent :: !(Pam SlotNo CoreNodeId)
{ ssAbsent :: !(InvertedMap SlotNo CoreNodeId)
-- ^ The nodes that haven't yet joined
--
-- INVARIANT @all (>= 'ssNextSlot') 'ssAbsent'@
Expand All @@ -376,7 +376,7 @@ data ShortState = ShortState
--
emptyShortState :: NodeJoinPlan -> ShortState
emptyShortState (NodeJoinPlan m) = ShortState
{ ssAbsent = Pam.fromMap m
{ ssAbsent = InvertedMap.fromMap m
, ssChains = Map.empty
, ssJoined = Map.empty
, ssNextSlot = 0
Expand All @@ -401,20 +401,20 @@ stepShort params st

joinSlotOf :: CoreNodeId -> SlotNo
joinSlotOf =
coreNodeIdJoinSlot $ NodeJoinPlan $ ssJoined' <> Pam.toMap ssAbsent'
coreNodeIdJoinSlot $ NodeJoinPlan $ ssJoined' <> InvertedMap.toMap ssAbsent'

(ssAbsent', ssJoined') =
(later, ssJoined <> Pam.toMap now)
(later, ssJoined <> InvertedMap.toMap now)
where
(now, later) = assertInvariant $
Pam.spanAntitone (== ssNextSlot) ssAbsent
InvertedMap.spanAntitone (== ssNextSlot) ssAbsent
ssChains' =
maybe id (\l -> Map.insert l ssNextSlot) mbLeader $
ssChains
ssNextSlot' = succ ssNextSlot

assertInvariant :: forall a. a -> a
assertInvariant x = case Pam.minViewWithKey ssAbsent of
assertInvariant x = case InvertedMap.minViewWithKey ssAbsent of
Just ((s, _), _) | s < ssNextSlot -> error $
"ShortState invariant violation: " <> show (ssNextSlot, ssAbsent)
_ -> x
Expand Down Expand Up @@ -490,7 +490,7 @@ stepShort params st
-- all nodes have joined and there are not multiple chains
detAllHaveJoinedWithoutMultipleChains :: Maybe DetOrNondet
detAllHaveJoinedWithoutMultipleChains = do
guard $ Pam.null ssAbsent'
guard $ InvertedMap.null ssAbsent'
case Map.toList ssChains' of
-- the degenerateLim guard should have prevented this evaluation; we
-- know no other way for this state to be reachable
Expand Down
@@ -0,0 +1,66 @@
module Test.Util.InvertedMap (
-- * InvertedMap type
InvertedMap,
-- * Query
Test.Util.InvertedMap.null,
-- * Construction
toMap,
unsafeInvertedMap,
-- * Conversion
fromMap,
unsafeCoercion,
-- * Filter
spanAntitone,
-- * Min/Max
minViewWithKey,
) where

import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NE
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Type.Coercion

-- | An inverted 'Map'
--
-- INVARIANT the @k@s are all unique
--
-- INVARIANT the 'NonEmpty's are all ascending
--
newtype InvertedMap v k = UnsafeInvertedMap {getInvertedMap :: Map v (NonEmpty k)}
deriving (Show)

unsafeCoercion :: Coercion (InvertedMap v k) (Map v (NonEmpty k))
unsafeCoercion = Coercion

unsafeInvertedMap :: Map v (NonEmpty k) -> InvertedMap v k
unsafeInvertedMap = UnsafeInvertedMap

-- | This inverts the given 'Map'
--
fromMap :: Ord v => Map k v -> InvertedMap v k
fromMap m =
UnsafeInvertedMap $ Map.fromListWith (<>) $
[ (v, k NE.:| []) | (k, v) <- Map.toList m ]

minViewWithKey :: InvertedMap v k -> Maybe ((v, NonEmpty k), InvertedMap v k)
minViewWithKey =
fmap (fmap UnsafeInvertedMap) . Map.minViewWithKey . getInvertedMap

null :: InvertedMap v k -> Bool
null = Map.null . getInvertedMap

spanAntitone :: (v -> Bool) -> InvertedMap v k -> (InvertedMap v k, InvertedMap v k)
spanAntitone f (UnsafeInvertedMap m) = (UnsafeInvertedMap l, UnsafeInvertedMap r)
where
(l, r) = Map.spanAntitone f m

-- | This inverts the given 'InvertedMap'
--
-- Inversion is an <https://en.wikipedia.org/wiki/Involution_(mathematics)>, so
-- this returns to 'Map'.
--
toMap :: Ord k => InvertedMap v k -> Map k v
toMap (UnsafeInvertedMap m) =
Map.fromList $
[ (k, v) | (v, ks) <- Map.toList m, k <- NE.toList ks ]

This file was deleted.

0 comments on commit 3de1fc8

Please sign in to comment.