Skip to content

Commit 8ce4dbd

Browse files
committed
Rename stuff, add more logging
1 parent 605ef06 commit 8ce4dbd

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

booster/library/Booster/Pattern/Rewrite.hs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ rewriteStep cutLabels terminalLabels pat = do
167167

168168
-- process one priority group at a time (descending priority),
169169
-- until a result is obtained or the entire rewrite fails.
170+
logMessage
171+
( "Applicable rule groups (priority, count): "
172+
<> (Text.pack . show $ zip (map ruleGroupPriority rules) (map length rules))
173+
)
170174
processGroups rules
171175
where
172176
processGroups ::
@@ -191,19 +195,19 @@ rewriteStep cutLabels terminalLabels pat = do
191195
processGroups rest >>= \case
192196
RewriteStuck{} -> pure $ RewriteTrivial pat
193197
other -> pure other
194-
AppliedRules (RewriteGroupApplicationData{ruleApplicationData = [], remainderPrediate}) -> do
198+
AppliedRules (RewriteGroupApplicationData{ruleApplicationData = [], groupRemainderPredicate}) -> do
195199
-- no applicable rules in this group, try other groups
196-
assertRemainderUnsat [] remainderPrediate
200+
assertRemainderUnsat [] groupRemainderPredicate
197201
processGroups rest
198202
AppliedRules
199203
( RewriteGroupApplicationData
200204
{ ruleApplicationData = [(rule, applied@RewriteRuleAppliedData{})]
201-
, remainderPrediate
205+
, groupRemainderPredicate
202206
}
203207
) -> do
204208
-- a non-trivial remainder with a single applicable rule is
205209
-- an indication if semantics incompleteness: abort
206-
assertRemainderUnsat [rule] remainderPrediate
210+
assertRemainderUnsat [rule] groupRemainderPredicate
207211
-- only one rule applies, see if it's special and return an appropriate result
208212
if
209213
| labelOf rule `elem` cutLabels ->
@@ -213,23 +217,23 @@ rewriteStep cutLabels terminalLabels pat = do
213217
| otherwise ->
214218
pure $ RewriteFinished (Just $ ruleLabelOrLocT rule) (Just $ uniqueId rule) applied.rewritten
215219
AppliedRules
216-
(RewriteGroupApplicationData{ruleApplicationData = xs, remainderPrediate}) -> do
220+
(RewriteGroupApplicationData{ruleApplicationData, groupRemainderPredicate}) -> do
217221
-- multiple rules apply, analyse branching and remainders
218-
isSatRemainder remainderPrediate >>= \case
222+
isSatRemainder groupRemainderPredicate >>= \case
219223
SMT.IsUnsat -> do
220224
-- the remainder condition is unsatisfiable: no need to consider the remainder branch.
221-
logRemainder (map fst xs) SMT.IsUnsat remainderPrediate
222-
pure $ mkBranch pat xs
225+
logRemainder (map fst ruleApplicationData) SMT.IsUnsat groupRemainderPredicate
226+
pure $ mkBranch pat ruleApplicationData
223227
satRes@(SMT.IsSat{}) -> do
224228
-- the remainder condition is satisfiable.
225229
-- TODO construct the remainder branch and consider it.
226230
-- To construct the "remainder pattern",
227231
-- we add the remainder condition to the predicates of pat
228-
throwRemainder (map fst xs) satRes remainderPrediate
232+
throwRemainder (map fst ruleApplicationData) satRes groupRemainderPredicate
229233
satRes@SMT.IsUnknown{} -> do
230234
-- solver cannot solve the remainder
231235
-- TODO descend into the remainder branch anyway
232-
throwRemainder (map fst xs) satRes remainderPrediate
236+
throwRemainder (map fst ruleApplicationData) satRes groupRemainderPredicate
233237

234238
labelOf = fromMaybe "" . (.ruleLabel) . (.attributes)
235239
ruleLabelOrLocT = renderOneLineText . ruleLabelOrLoc
@@ -250,15 +254,15 @@ rewriteStep cutLabels terminalLabels pat = do
250254
-- check the remainder predicate for satisfiablity. Do nothing if unsat, abort rewriting otherwise
251255
assertRemainderUnsat ::
252256
LoggerMIO io => [RewriteRule "Rewrite"] -> Set.Set Predicate -> RewriteT io ()
253-
assertRemainderUnsat rules remainderPrediate =
257+
assertRemainderUnsat rules remainderPrediate = do
254258
isSatRemainder remainderPrediate >>= \case
255259
SMT.IsUnsat -> pure ()
256260
otherSatRes -> throwRemainder rules otherSatRes remainderPrediate
257261

258-
-- check the remainder predicate for satisfiability under the pre-branch pattern's constraints
262+
-- check the remainder predicate for satisfiability under the pre-branch pattern's constraints. Empty set is considered UNSAT
259263
isSatRemainder :: LoggerMIO io => Set.Set Predicate -> RewriteT io (SMT.IsSatResult ())
260264
isSatRemainder remainderPredicate =
261-
if any isFalse remainderPredicate
265+
if Set.null remainderPredicate || any isFalse remainderPredicate
262266
then pure SMT.IsUnsat
263267
else do
264268
solver <- getSolver
@@ -269,16 +273,15 @@ rewriteStep cutLabels terminalLabels pat = do
269273
LoggerMIO io => [RewriteRule "Rewrite"] -> SMT.IsSatResult () -> Set.Set Predicate -> RewriteT io a
270274
throwRemainder rules satResult remainderPredicate =
271275
throw $
272-
RewriteRemainderPredicate rules satResult . coerce . foldl1 AndTerm $
273-
map coerce . Set.toList $
274-
remainderPredicate
276+
RewriteRemainderPredicate rules satResult . coerce . collapseAndBools . Set.toList $
277+
remainderPredicate
275278

276279
-- log a remainder predicate as an exception without aborting rewriting
277280
logRemainder ::
278281
LoggerMIO io => [RewriteRule "Rewrite"] -> SMT.IsSatResult () -> Set.Set Predicate -> RewriteT io ()
279282
logRemainder rules satResult remainderPredicate = do
280283
ModifiersRep (_ :: FromModifiersT mods => Proxy mods) <- getPrettyModifiers
281-
let remainderForLogging = coerce . foldl1 AndTerm $ map coerce . Set.toList $ remainderPredicate
284+
let remainderForLogging = coerce . collapseAndBools . Set.toList $ remainderPredicate
282285
withContext CtxRemainder . withContext CtxContinue
283286
$ logMessage
284287
$ WithJsonMessage
@@ -622,7 +625,7 @@ applyRule pat@Pattern{ceilConditions} rule =
622625
"Uncertain about condition(s) in a rule:"
623626
<+> (hsep . punctuate comma . map (pretty' @mods) $ predicates)
624627
failRewrite $
625-
RuleConditionUnclear rule . coerce . foldl1 AndTerm $
628+
RuleConditionUnclear rule . coerce . collapseAndBools $
626629
map coerce predicates
627630

628631
-- Instantiate the requires clause of the rule and simplify, but not prune.
@@ -645,7 +648,7 @@ applyRuleGroup ::
645648
LoggerMIO io => [RewriteRule "Rewrite"] -> Pattern -> RewriteT io RuleGroupApplication
646649
applyRuleGroup rules pat =
647650
postProcessGroupResults . zip rules
648-
<$> traverse (applyRule pat) rules
651+
<$> mapM (applyRule pat) rules
649652

650653
-- | The result of applying a group of rules independently to the same input pattern
651654
data RuleGroupApplication
@@ -663,7 +666,7 @@ data RuleGroupApplication
663666
data RewriteGroupApplicationData = RewriteGroupApplicationData
664667
{ ruleApplicationData :: [(RewriteRule "Rewrite", RewriteRuleAppliedData)]
665668
-- ^ several applied rules with the rewritten term and metadata
666-
, remainderPrediate :: Set.Set Predicate
669+
, groupRemainderPredicate :: Set.Set Predicate
667670
-- ^ the remainder predicate of the whole group
668671
}
669672

@@ -677,14 +680,18 @@ postProcessGroupResults ::
677680
[(RewriteRule "Rewrite", RewriteRuleAppResult RewriteRuleAppliedData)] ->
678681
RuleGroupApplication
679682
postProcessGroupResults = \case
680-
[] -> AppliedRules (RewriteGroupApplicationData{ruleApplicationData = [], remainderPrediate = mempty})
683+
[] ->
684+
AppliedRules
685+
(RewriteGroupApplicationData{ruleApplicationData = [], groupRemainderPredicate = mempty})
681686
apps -> case filter ((/= NotApplied) . snd) apps of
682-
[] -> AppliedRules (RewriteGroupApplicationData{ruleApplicationData = [], remainderPrediate = mempty})
687+
[] ->
688+
AppliedRules
689+
(RewriteGroupApplicationData{ruleApplicationData = [], groupRemainderPredicate = mempty})
683690
xs
684691
| all ((== Trivial) . snd) xs -> OnlyTrivial
685692
| otherwise ->
686-
let (ruleApplicationData, remainderPrediate) = go ([], mempty) xs
687-
in AppliedRules (RewriteGroupApplicationData{ruleApplicationData, remainderPrediate})
693+
let (ruleApplicationData, groupRemainderPredicate) = go ([], mempty :: Set.Set Predicate) xs
694+
in AppliedRules (RewriteGroupApplicationData{ruleApplicationData, groupRemainderPredicate})
688695
where
689696
go acc@(accPatterns, accRemainders) = \case
690697
[] -> (reverse accPatterns, accRemainders)
@@ -749,7 +756,10 @@ instance FromModifiersT mods => Pretty (PrettyWithModifiers mods (RewriteFailed
749756
RewriteRemainderPredicate rules satResult predicate ->
750757
hsep
751758
[ pretty (SMT.showIsSatResult satResult)
752-
, "remainder predicate after applying rules"
759+
, "remainder predicate after applying"
760+
, pretty (length rules)
761+
, "rules at priority"
762+
, pretty (show $ ruleGroupPriority rules)
753763
, hsep $ punctuate comma $ map ruleLabelOrLoc rules
754764
, ": "
755765
, pretty' @mods predicate

0 commit comments

Comments
 (0)