Skip to content

Commit 8b5991c

Browse files
committed
Refactor remainder check into a function
1 parent 7d7267e commit 8b5991c

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

booster/library/Booster/Pattern/Rewrite.hs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -221,30 +221,21 @@ rewriteStep cutLabels terminalLabels pat = do
221221
AppliedRules
222222
(RewriteGroupApplicationData{ruleApplicationData = xs, remainderPrediate = groupRemainderPrediate}) -> do
223223
-- 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.
226227
logRemainder (map fst xs) SMT.IsUnsat groupRemainderPrediate
227-
-- the remainder predicate is trivially false, return the branching result
228228
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
248239

249240
labelOf = fromMaybe "" . (.ruleLabel) . (.attributes)
250241
ruleLabelOrLocT = renderOneLineText . ruleLabelOrLoc
@@ -262,6 +253,15 @@ rewriteStep cutLabels terminalLabels pat = do
262253
)
263254
leafs
264255

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+
265265
-- abort rewriting by throwing a remainder predicate as an exception, to be caught and processed in @performRewrite@
266266
throwRemainder ::
267267
LoggerMIO io => [RewriteRule "Rewrite"] -> SMT.IsSatResult () -> Set.Set Predicate -> RewriteT io a

0 commit comments

Comments
 (0)