@@ -167,6 +167,10 @@ rewriteStep cutLabels terminalLabels pat = do
167
167
168
168
-- process one priority group at a time (descending priority),
169
169
-- 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
+ )
170
174
processGroups rules
171
175
where
172
176
processGroups ::
@@ -191,19 +195,19 @@ rewriteStep cutLabels terminalLabels pat = do
191
195
processGroups rest >>= \ case
192
196
RewriteStuck {} -> pure $ RewriteTrivial pat
193
197
other -> pure other
194
- AppliedRules (RewriteGroupApplicationData {ruleApplicationData = [] , remainderPrediate }) -> do
198
+ AppliedRules (RewriteGroupApplicationData {ruleApplicationData = [] , groupRemainderPredicate }) -> do
195
199
-- no applicable rules in this group, try other groups
196
- assertRemainderUnsat [] remainderPrediate
200
+ assertRemainderUnsat [] groupRemainderPredicate
197
201
processGroups rest
198
202
AppliedRules
199
203
( RewriteGroupApplicationData
200
204
{ ruleApplicationData = [(rule, applied@ RewriteRuleAppliedData {})]
201
- , remainderPrediate
205
+ , groupRemainderPredicate
202
206
}
203
207
) -> do
204
208
-- a non-trivial remainder with a single applicable rule is
205
209
-- an indication if semantics incompleteness: abort
206
- assertRemainderUnsat [rule] remainderPrediate
210
+ assertRemainderUnsat [rule] groupRemainderPredicate
207
211
-- only one rule applies, see if it's special and return an appropriate result
208
212
if
209
213
| labelOf rule `elem` cutLabels ->
@@ -213,23 +217,23 @@ rewriteStep cutLabels terminalLabels pat = do
213
217
| otherwise ->
214
218
pure $ RewriteFinished (Just $ ruleLabelOrLocT rule) (Just $ uniqueId rule) applied. rewritten
215
219
AppliedRules
216
- (RewriteGroupApplicationData {ruleApplicationData = xs, remainderPrediate }) -> do
220
+ (RewriteGroupApplicationData {ruleApplicationData, groupRemainderPredicate }) -> do
217
221
-- multiple rules apply, analyse branching and remainders
218
- isSatRemainder remainderPrediate >>= \ case
222
+ isSatRemainder groupRemainderPredicate >>= \ case
219
223
SMT. IsUnsat -> do
220
224
-- 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
223
227
satRes@ (SMT. IsSat {}) -> do
224
228
-- the remainder condition is satisfiable.
225
229
-- TODO construct the remainder branch and consider it.
226
230
-- To construct the "remainder pattern",
227
231
-- we add the remainder condition to the predicates of pat
228
- throwRemainder (map fst xs ) satRes remainderPrediate
232
+ throwRemainder (map fst ruleApplicationData ) satRes groupRemainderPredicate
229
233
satRes@ SMT. IsUnknown {} -> do
230
234
-- solver cannot solve the remainder
231
235
-- TODO descend into the remainder branch anyway
232
- throwRemainder (map fst xs ) satRes remainderPrediate
236
+ throwRemainder (map fst ruleApplicationData ) satRes groupRemainderPredicate
233
237
234
238
labelOf = fromMaybe " " . (. ruleLabel) . (. attributes)
235
239
ruleLabelOrLocT = renderOneLineText . ruleLabelOrLoc
@@ -250,15 +254,15 @@ rewriteStep cutLabels terminalLabels pat = do
250
254
-- check the remainder predicate for satisfiablity. Do nothing if unsat, abort rewriting otherwise
251
255
assertRemainderUnsat ::
252
256
LoggerMIO io => [RewriteRule " Rewrite" ] -> Set. Set Predicate -> RewriteT io ()
253
- assertRemainderUnsat rules remainderPrediate =
257
+ assertRemainderUnsat rules remainderPrediate = do
254
258
isSatRemainder remainderPrediate >>= \ case
255
259
SMT. IsUnsat -> pure ()
256
260
otherSatRes -> throwRemainder rules otherSatRes remainderPrediate
257
261
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
259
263
isSatRemainder :: LoggerMIO io => Set. Set Predicate -> RewriteT io (SMT. IsSatResult () )
260
264
isSatRemainder remainderPredicate =
261
- if any isFalse remainderPredicate
265
+ if Set. null remainderPredicate || any isFalse remainderPredicate
262
266
then pure SMT. IsUnsat
263
267
else do
264
268
solver <- getSolver
@@ -269,16 +273,15 @@ rewriteStep cutLabels terminalLabels pat = do
269
273
LoggerMIO io => [RewriteRule " Rewrite" ] -> SMT. IsSatResult () -> Set. Set Predicate -> RewriteT io a
270
274
throwRemainder rules satResult remainderPredicate =
271
275
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
275
278
276
279
-- log a remainder predicate as an exception without aborting rewriting
277
280
logRemainder ::
278
281
LoggerMIO io => [RewriteRule " Rewrite" ] -> SMT. IsSatResult () -> Set. Set Predicate -> RewriteT io ()
279
282
logRemainder rules satResult remainderPredicate = do
280
283
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
282
285
withContext CtxRemainder . withContext CtxContinue
283
286
$ logMessage
284
287
$ WithJsonMessage
@@ -622,7 +625,7 @@ applyRule pat@Pattern{ceilConditions} rule =
622
625
" Uncertain about condition(s) in a rule:"
623
626
<+> (hsep . punctuate comma . map (pretty' @ mods ) $ predicates)
624
627
failRewrite $
625
- RuleConditionUnclear rule . coerce . foldl1 AndTerm $
628
+ RuleConditionUnclear rule . coerce . collapseAndBools $
626
629
map coerce predicates
627
630
628
631
-- Instantiate the requires clause of the rule and simplify, but not prune.
@@ -645,7 +648,7 @@ applyRuleGroup ::
645
648
LoggerMIO io => [RewriteRule " Rewrite" ] -> Pattern -> RewriteT io RuleGroupApplication
646
649
applyRuleGroup rules pat =
647
650
postProcessGroupResults . zip rules
648
- <$> traverse (applyRule pat) rules
651
+ <$> mapM (applyRule pat) rules
649
652
650
653
-- | The result of applying a group of rules independently to the same input pattern
651
654
data RuleGroupApplication
@@ -663,7 +666,7 @@ data RuleGroupApplication
663
666
data RewriteGroupApplicationData = RewriteGroupApplicationData
664
667
{ ruleApplicationData :: [(RewriteRule " Rewrite" , RewriteRuleAppliedData )]
665
668
-- ^ several applied rules with the rewritten term and metadata
666
- , remainderPrediate :: Set. Set Predicate
669
+ , groupRemainderPredicate :: Set. Set Predicate
667
670
-- ^ the remainder predicate of the whole group
668
671
}
669
672
@@ -677,14 +680,18 @@ postProcessGroupResults ::
677
680
[(RewriteRule " Rewrite" , RewriteRuleAppResult RewriteRuleAppliedData )] ->
678
681
RuleGroupApplication
679
682
postProcessGroupResults = \ case
680
- [] -> AppliedRules (RewriteGroupApplicationData {ruleApplicationData = [] , remainderPrediate = mempty })
683
+ [] ->
684
+ AppliedRules
685
+ (RewriteGroupApplicationData {ruleApplicationData = [] , groupRemainderPredicate = mempty })
681
686
apps -> case filter ((/= NotApplied ) . snd ) apps of
682
- [] -> AppliedRules (RewriteGroupApplicationData {ruleApplicationData = [] , remainderPrediate = mempty })
687
+ [] ->
688
+ AppliedRules
689
+ (RewriteGroupApplicationData {ruleApplicationData = [] , groupRemainderPredicate = mempty })
683
690
xs
684
691
| all ((== Trivial ) . snd ) xs -> OnlyTrivial
685
692
| 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 })
688
695
where
689
696
go acc@ (accPatterns, accRemainders) = \ case
690
697
[] -> (reverse accPatterns, accRemainders)
@@ -749,7 +756,10 @@ instance FromModifiersT mods => Pretty (PrettyWithModifiers mods (RewriteFailed
749
756
RewriteRemainderPredicate rules satResult predicate ->
750
757
hsep
751
758
[ 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)
753
763
, hsep $ punctuate comma $ map ruleLabelOrLoc rules
754
764
, " : "
755
765
, pretty' @ mods predicate
0 commit comments