Skip to content

Commit 4a4ce1b

Browse files
committed
extractRules: WIP add assertion that ids are unique
1 parent 4e91cd0 commit 4a4ce1b

File tree

3 files changed

+67
-28
lines changed

3 files changed

+67
-28
lines changed

kore/src/Kore/Exec.hs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ import Kore.Internal.TermLike
103103
import Kore.Log.ErrorRewriteLoop
104104
( errorRewriteLoop
105105
)
106+
import Kore.Log.ErrorRuleMergeDuplicateId
107+
( errorRuleMergeDuplicateIds
108+
)
106109
import Kore.Log.InfoExecDepth
107110
import Kore.Log.KoreLogOptions
108111
( KoreLogOptions (..)
@@ -570,10 +573,11 @@ extractRules
570573
-> [Text]
571574
-> ExceptT Text m (NonEmpty (RewriteRule VariableName))
572575
extractRules rules names = do
573-
-- TODO: assert unique names
574576
let rulesById = mapMaybe ruleById rules
575577
rulesByLabel = mapMaybe ruleByLabel rules
576-
ruleRegistry = Map.fromList (rulesById <> rulesByLabel)
578+
whenDuplicate errorRuleMergeDuplicateIds rulesById
579+
-- whenDuplicate errorRuleMergeDuplicateLabel rulesByLabel
580+
let ruleRegistry = Map.fromList (rulesById <> rulesByLabel)
577581
extractedRules <- traverse (extractRule ruleRegistry) names
578582
case extractedRules of
579583
[] -> throwE "Empty rule list."
@@ -605,6 +609,24 @@ extractRules rules names = do
605609
return
606610
(Map.lookup ruleName registry)
607611

612+
whenDuplicate logError withNames = do
613+
let duplicateNames = findCollisions . mkMapWithCollisions $ withNames
614+
unless (null duplicateNames) (logError duplicateNames)
615+
616+
mkMapWithCollisions
617+
:: Ord key
618+
=> [(key, val)]
619+
-> Map.Map key [val]
620+
mkMapWithCollisions pairs =
621+
Map.fromListWith (<>)
622+
$ (fmap . fmap) pure pairs
623+
624+
findCollisions :: Map.Map key [val] -> Map.Map key [val]
625+
findCollisions = filter (not . isSingleton)
626+
where
627+
isSingleton [_] = True
628+
isSingleton _ = False
629+
608630
assertSingleClaim :: Monad m => [claim] -> m ()
609631
assertSingleClaim claims =
610632
when (length claims > 1) . error

kore/src/Kore/Log/ErrorRuleMergeDuplicateId.hs

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ License : NCSA
55
-}
66

77
module Kore.Log.ErrorRuleMergeDuplicateId
8-
( ErrorRuleMergeDuplicateId
9-
, errorRuleMergeDuplicateId
8+
( ErrorRuleMergeDuplicateIds
9+
, errorRuleMergeDuplicateIds
1010
) where
1111

1212
import Prelude.Kore
@@ -22,6 +22,10 @@ import Data.Generics.Product
2222
import Data.Generics.Wrapped
2323
( _Unwrapped
2424
)
25+
import Data.Map.Strict
26+
( Map
27+
)
28+
import qualified Data.Map.Strict as Map
2529
import Data.Text
2630
( Text
2731
)
@@ -43,43 +47,48 @@ import Pretty
4347
)
4448
import qualified Pretty
4549

46-
data ErrorRuleMergeDuplicateId =
47-
ErrorRuleMergeDuplicateId
48-
{ locations :: [SourceLocation]
49-
, ruleId :: Text
50+
newtype ErrorRuleMergeDuplicateIds =
51+
ErrorRuleMergeDuplicateIds
52+
{ unErrorRuleMergeDuplicateIds :: Map Text [SourceLocation]
5053
}
5154
deriving (Show)
5255
deriving (GHC.Generic)
5356
deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
5457

55-
instance Exception ErrorRuleMergeDuplicateId where
58+
instance Exception ErrorRuleMergeDuplicateIds where
5659
toException = toException . SomeEntry
5760
fromException exn =
5861
fromException exn >>= fromEntry
5962

60-
instance Entry ErrorRuleMergeDuplicateId where
63+
instance Entry ErrorRuleMergeDuplicateIds where
6164
entrySeverity _ = Error
6265
helpDoc _ =
6366
"error thrown during rule merging when\
6467
\ multiple rules have the same id"
6568

66-
instance Pretty ErrorRuleMergeDuplicateId where
67-
pretty ErrorRuleMergeDuplicateId { locations , ruleId } =
68-
Pretty.vsep
69-
$ ["The rules at the following locations:"]
70-
<> fmap (Pretty.indent 4 . pretty) locations
71-
<> [ Pretty.indent 2 "all have the following id:"
72-
, Pretty.indent 4 (pretty ruleId)
73-
]
69+
instance Pretty ErrorRuleMergeDuplicateIds where
70+
pretty (ErrorRuleMergeDuplicateIds duplicateIds) =
71+
Map.foldMapWithKey accum duplicateIds
72+
where
73+
accum ruleId locations =
74+
Pretty.vsep
75+
$ ["The rules at the following locations:"]
76+
<> fmap (Pretty.indent 4 . pretty) locations
77+
<> [ Pretty.indent 2 "all have the following id:"
78+
, Pretty.indent 4 (pretty ruleId)
79+
]
7480

75-
errorRuleMergeDuplicateId :: [RewriteRule VariableName] -> Text -> a
76-
errorRuleMergeDuplicateId rules ruleId =
77-
throw ErrorRuleMergeDuplicateId { locations, ruleId }
81+
errorRuleMergeDuplicateIds :: Map Text [RewriteRule VariableName] -> a
82+
errorRuleMergeDuplicateIds duplicateIds =
83+
throw (ErrorRuleMergeDuplicateIds idsWithlocations)
7884
where
79-
locations =
80-
Lens.view
81-
( _Unwrapped
82-
. field @"attributes"
83-
. field @"sourceLocation"
84-
)
85-
<$> rules
85+
idsWithlocations =
86+
(fmap . fmap)
87+
(
88+
Lens.view
89+
( _Unwrapped
90+
. field @"attributes"
91+
. field @"sourceLocation"
92+
)
93+
)
94+
duplicateIds

kore/src/Kore/Log/Registry.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ import Kore.Log.ErrorRewriteLoop
8080
import Kore.Log.ErrorRewritesInstantiation
8181
( ErrorRewritesInstantiation
8282
)
83+
import Kore.Log.ErrorRuleMergeDuplicateId
84+
( ErrorRuleMergeDuplicateIds
85+
)
86+
import Kore.Log.ErrorRuleMergeDuplicateLabel
87+
( ErrorRuleMergeDuplicateLabel
88+
)
8389
import Kore.Log.InfoAttemptUnification
8490
( InfoAttemptUnification
8591
)
@@ -155,6 +161,8 @@ entryHelpDocs :: [Pretty.Doc ()]
155161
, mk $ Proxy @ErrorBottomTotalFunction
156162
, mk $ Proxy @ErrorDecidePredicateUnknown
157163
, mk $ Proxy @ErrorParse
164+
, mk $ Proxy @ErrorRuleMergeDuplicateIds
165+
, mk $ Proxy @ErrorRuleMergeDuplicateLabel
158166
, mk $ Proxy @WarnFunctionWithoutEvaluators
159167
, mk $ Proxy @WarnSymbolSMTRepresentation
160168
, mk $ Proxy @WarnStuckClaimState

0 commit comments

Comments
 (0)