@@ -221,30 +221,21 @@ rewriteStep cutLabels terminalLabels pat = do
221
221
AppliedRules
222
222
(RewriteGroupApplicationData {ruleApplicationData = xs, remainderPrediate = groupRemainderPrediate}) -> do
223
223
-- multiple rules apply, analyse branching and remainders
224
- if any isFalse groupRemainderPrediate
225
- then do
224
+ isSatRemainder groupRemainderPrediate >>= \ case
225
+ SMT. IsUnsat -> do
226
+ -- the remainder condition is unsatisfiable: no need to consider the remainder branch.
226
227
logRemainder (map fst xs) SMT. IsUnsat groupRemainderPrediate
227
- -- the remainder predicate is trivially false, return the branching result
228
228
pure $ mkBranch pat xs
229
- else do
230
- -- otherwise, we need to check the remainder predicate with the SMT solver
231
- -- and construct an additional remainder branch if needed
232
- solver <- getSolver
233
- SMT. isSat solver (Set. toList $ pat. constraints <> groupRemainderPrediate) pat. substitution >>= \ case
234
- SMT. IsUnsat -> do
235
- -- the remainder condition is unsatisfiable: no need to consider the remainder branch.
236
- logRemainder (map fst xs) SMT. IsUnsat groupRemainderPrediate
237
- pure $ mkBranch pat xs
238
- satRes@ (SMT. IsSat {}) -> do
239
- -- the remainder condition is satisfiable.
240
- -- TODO construct the remainder branch and consider it.
241
- -- To construct the "remainder pattern",
242
- -- we add the remainder condition to the predicates of the pattr
243
- throwRemainder (map fst xs) satRes groupRemainderPrediate
244
- satRes@ SMT. IsUnknown {} -> do
245
- -- solver cannot solve the remainder
246
- -- TODO descend into the remainder branch anyway
247
- throwRemainder (map fst xs) satRes groupRemainderPrediate
229
+ satRes@ (SMT. IsSat {}) -> do
230
+ -- the remainder condition is satisfiable.
231
+ -- TODO construct the remainder branch and consider it.
232
+ -- To construct the "remainder pattern",
233
+ -- we add the remainder condition to the predicates of pat
234
+ throwRemainder (map fst xs) satRes groupRemainderPrediate
235
+ satRes@ SMT. IsUnknown {} -> do
236
+ -- solver cannot solve the remainder
237
+ -- TODO descend into the remainder branch anyway
238
+ throwRemainder (map fst xs) satRes groupRemainderPrediate
248
239
249
240
labelOf = fromMaybe " " . (. ruleLabel) . (. attributes)
250
241
ruleLabelOrLocT = renderOneLineText . ruleLabelOrLoc
@@ -262,6 +253,15 @@ rewriteStep cutLabels terminalLabels pat = do
262
253
)
263
254
leafs
264
255
256
+ -- check the remainder predicate for satisfiability under the pre-branch pattern's constraints
257
+ isSatRemainder :: LoggerMIO io => Set. Set Predicate -> RewriteT io (SMT. IsSatResult () )
258
+ isSatRemainder remainderPredicate =
259
+ if any isFalse remainderPredicate
260
+ then pure SMT. IsUnsat
261
+ else do
262
+ solver <- getSolver
263
+ SMT. isSat solver (Set. toList $ pat. constraints <> remainderPredicate) pat. substitution
264
+
265
265
-- abort rewriting by throwing a remainder predicate as an exception, to be caught and processed in @performRewrite@
266
266
throwRemainder ::
267
267
LoggerMIO io => [RewriteRule " Rewrite" ] -> SMT. IsSatResult () -> Set. Set Predicate -> RewriteT io a
0 commit comments