Skip to content

Commit 605ef06

Browse files
committed
Assert unsatisfiable remainder
1 parent 8b5991c commit 605ef06

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

booster/library/Booster/Pattern/Rewrite.hs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -191,51 +191,45 @@ rewriteStep cutLabels terminalLabels pat = do
191191
processGroups rest >>= \case
192192
RewriteStuck{} -> pure $ RewriteTrivial pat
193193
other -> pure other
194-
AppliedRules (RewriteGroupApplicationData{ruleApplicationData = []}) ->
195-
-- not applicable rules in this group, try other groups
196-
-- TODO check that remainder is trivial, abort otherwise
194+
AppliedRules (RewriteGroupApplicationData{ruleApplicationData = [], remainderPrediate}) -> do
195+
-- no applicable rules in this group, try other groups
196+
assertRemainderUnsat [] remainderPrediate
197197
processGroups rest
198198
AppliedRules
199199
( RewriteGroupApplicationData
200200
{ ruleApplicationData = [(rule, applied@RewriteRuleAppliedData{})]
201-
, remainderPrediate = groupRemainderPrediate
201+
, remainderPrediate
202202
}
203-
)
203+
) -> do
204+
-- a non-trivial remainder with a single applicable rule is
205+
-- an indication if semantics incompleteness: abort
206+
assertRemainderUnsat [rule] remainderPrediate
204207
-- only one rule applies, see if it's special and return an appropriate result
205-
| not (Set.null groupRemainderPrediate) && not (any isFalse groupRemainderPrediate) -> do
206-
-- a non-trivial remainder with a single applicable rule is
207-
-- an indication if semantics incompleteness: abort
208-
-- TODO refactor remainder check into a function and reuse below
209-
solver <- getSolver
210-
satRes <- SMT.isSat solver (Set.toList $ pat.constraints <> groupRemainderPrediate) pat.substitution
211-
throw $
212-
RewriteRemainderPredicate [rule] satRes . coerce . foldl1 AndTerm $
213-
map coerce . Set.toList $
214-
groupRemainderPrediate
215-
| labelOf rule `elem` cutLabels ->
216-
pure $ RewriteCutPoint (labelOf rule) (uniqueId rule) pat applied.rewritten
217-
| labelOf rule `elem` terminalLabels ->
218-
pure $ RewriteTerminal (labelOf rule) (uniqueId rule) applied.rewritten
219-
| otherwise ->
220-
pure $ RewriteFinished (Just $ ruleLabelOrLocT rule) (Just $ uniqueId rule) applied.rewritten
208+
if
209+
| labelOf rule `elem` cutLabels ->
210+
pure $ RewriteCutPoint (labelOf rule) (uniqueId rule) pat applied.rewritten
211+
| labelOf rule `elem` terminalLabels ->
212+
pure $ RewriteTerminal (labelOf rule) (uniqueId rule) applied.rewritten
213+
| otherwise ->
214+
pure $ RewriteFinished (Just $ ruleLabelOrLocT rule) (Just $ uniqueId rule) applied.rewritten
221215
AppliedRules
222-
(RewriteGroupApplicationData{ruleApplicationData = xs, remainderPrediate = groupRemainderPrediate}) -> do
216+
(RewriteGroupApplicationData{ruleApplicationData = xs, remainderPrediate}) -> do
223217
-- multiple rules apply, analyse branching and remainders
224-
isSatRemainder groupRemainderPrediate >>= \case
218+
isSatRemainder remainderPrediate >>= \case
225219
SMT.IsUnsat -> do
226220
-- the remainder condition is unsatisfiable: no need to consider the remainder branch.
227-
logRemainder (map fst xs) SMT.IsUnsat groupRemainderPrediate
221+
logRemainder (map fst xs) SMT.IsUnsat remainderPrediate
228222
pure $ mkBranch pat xs
229223
satRes@(SMT.IsSat{}) -> do
230224
-- the remainder condition is satisfiable.
231225
-- TODO construct the remainder branch and consider it.
232226
-- To construct the "remainder pattern",
233227
-- we add the remainder condition to the predicates of pat
234-
throwRemainder (map fst xs) satRes groupRemainderPrediate
228+
throwRemainder (map fst xs) satRes remainderPrediate
235229
satRes@SMT.IsUnknown{} -> do
236230
-- solver cannot solve the remainder
237231
-- TODO descend into the remainder branch anyway
238-
throwRemainder (map fst xs) satRes groupRemainderPrediate
232+
throwRemainder (map fst xs) satRes remainderPrediate
239233

240234
labelOf = fromMaybe "" . (.ruleLabel) . (.attributes)
241235
ruleLabelOrLocT = renderOneLineText . ruleLabelOrLoc
@@ -253,6 +247,14 @@ rewriteStep cutLabels terminalLabels pat = do
253247
)
254248
leafs
255249

250+
-- check the remainder predicate for satisfiablity. Do nothing if unsat, abort rewriting otherwise
251+
assertRemainderUnsat ::
252+
LoggerMIO io => [RewriteRule "Rewrite"] -> Set.Set Predicate -> RewriteT io ()
253+
assertRemainderUnsat rules remainderPrediate =
254+
isSatRemainder remainderPrediate >>= \case
255+
SMT.IsUnsat -> pure ()
256+
otherSatRes -> throwRemainder rules otherSatRes remainderPrediate
257+
256258
-- check the remainder predicate for satisfiability under the pre-branch pattern's constraints
257259
isSatRemainder :: LoggerMIO io => Set.Set Predicate -> RewriteT io (SMT.IsSatResult ())
258260
isSatRemainder remainderPredicate =
@@ -262,7 +264,7 @@ rewriteStep cutLabels terminalLabels pat = do
262264
solver <- getSolver
263265
SMT.isSat solver (Set.toList $ pat.constraints <> remainderPredicate) pat.substitution
264266

265-
-- abort rewriting by throwing a remainder predicate as an exception, to be caught and processed in @performRewrite@
267+
-- abort rewriting by throwing a remainder predicate as an exception, to be caught and processed in performRewrite
266268
throwRemainder ::
267269
LoggerMIO io => [RewriteRule "Rewrite"] -> SMT.IsSatResult () -> Set.Set Predicate -> RewriteT io a
268270
throwRemainder rules satResult remainderPredicate =

0 commit comments

Comments
 (0)