Skip to content

Commit e8d9041

Browse files
committed
Add logs of unsat remainders
1 parent 9e830fa commit e8d9041

File tree

1 file changed

+60
-38
lines changed

1 file changed

+60
-38
lines changed

booster/library/Booster/Pattern/Rewrite.hs

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -218,45 +218,67 @@ rewriteStep cutLabels terminalLabels pat = do
218218
pure $ RewriteTerminal (labelOf rule) (uniqueId rule) newPat
219219
| otherwise ->
220220
pure $ RewriteFinished (Just $ ruleLabelOrLocT rule) (Just $ uniqueId rule) newPat
221-
AppliedRules (xs, remainder)
221+
AppliedRules (xs, remainder) -> do
222+
ModifiersRep (_ :: FromModifiersT mods => Proxy mods) <- getPrettyModifiers
223+
let remainderForLogging = coerce . foldl1 AndTerm $ map coerce . Set.toList $ remainder
222224
-- multiple rules apply, analyse brunching and remainders
223-
| any isFalse remainder -> do
224-
-- the remainder predicate is trivially false, return the branching result
225-
pure $
226-
RewriteBranch pat $
227-
NE.fromList $
228-
map (\(r, p, subst) -> (ruleLabelOrLocT r, uniqueId r, p, mkRulePredicate r subst, subst)) xs
229-
| otherwise -> 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 <> remainder) >>= \case
234-
SMT.IsUnsat -> do
235-
-- the remainder condition is unsatisfiable: no need to consider the remainder branch.
236-
-- pure resultsWithoutRemainders
237-
pure $
238-
RewriteBranch pat $
239-
NE.fromList $
240-
map (\(r, p, subst) -> (ruleLabelOrLocT r, uniqueId r, p, mkRulePredicate r subst, subst)) xs
241-
satRes@(SMT.IsSat{}) -> do
242-
-- the remainder condition is satisfiable.
243-
-- Have to construct the remainder branch and consider it
244-
-- To construct the "remainder pattern",
245-
-- we add the remainder condition to the predicates of the @pattr@
246-
-- (resultsWithoutRemainders <>) <$> processGroups lowerPriorityRules
247-
let rs = map (\(r, _p, _subst) -> r) xs
248-
throw $
249-
RewriteRemainderPredicate rs satRes . coerce . foldl1 AndTerm $
250-
map coerce . Set.toList $
251-
remainder
252-
satRes@SMT.IsUnknown{} -> do
253-
-- solver cannot solve the remainder. Descend into the remainder branch anyway
254-
-- (resultsWithoutRemainders <>) <$> processGroups lowerPriorityRules
255-
let rs = map (\(r, _p, _subst) -> r) xs
256-
throw $
257-
RewriteRemainderPredicate rs satRes . coerce . foldl1 AndTerm $
258-
map coerce . Set.toList $
259-
remainder
225+
if any isFalse remainder
226+
then do
227+
withContext CtxRemainder . withContext CtxContinue
228+
$ logMessage
229+
$ WithJsonMessage
230+
( object
231+
[ "remainder"
232+
.= externalisePredicate (externaliseSort $ sortOfPattern pat) remainderForLogging
233+
]
234+
)
235+
$ renderOneLineText
236+
$ pretty' @mods (RewriteRemainderPredicate (map (\(r, _, _) -> r) xs) SMT.IsUnsat remainderForLogging)
237+
-- the remainder predicate is trivially false, return the branching result
238+
pure $
239+
RewriteBranch pat $
240+
NE.fromList $
241+
map (\(r, p, subst) -> (ruleLabelOrLocT r, uniqueId r, p, mkRulePredicate r subst, subst)) xs
242+
else do
243+
-- otherwise, we need to check the remainder predicate with the SMT solver
244+
-- and construct an additional remainder branch if needed
245+
solver <- getSolver
246+
SMT.isSat solver (Set.toList $ pat.constraints <> remainder) >>= \case
247+
SMT.IsUnsat -> do
248+
-- the remainder condition is unsatisfiable: no need to consider the remainder branch.
249+
withContext CtxRemainder . withContext CtxContinue
250+
$ logMessage
251+
$ WithJsonMessage
252+
( object
253+
[ "remainder"
254+
.= externalisePredicate (externaliseSort $ sortOfPattern pat) (coerce remainderForLogging)
255+
]
256+
)
257+
$ renderOneLineText
258+
$ pretty' @mods (RewriteRemainderPredicate (map (\(r, _, _) -> r) xs) SMT.IsUnsat remainderForLogging)
259+
pure $
260+
RewriteBranch pat $
261+
NE.fromList $
262+
map (\(r, p, subst) -> (ruleLabelOrLocT r, uniqueId r, p, mkRulePredicate r subst, subst)) xs
263+
satRes@(SMT.IsSat{}) -> do
264+
-- the remainder condition is satisfiable.
265+
-- Have to construct the remainder branch and consider it
266+
-- To construct the "remainder pattern",
267+
-- we add the remainder condition to the predicates of the @pattr@
268+
-- (resultsWithoutRemainders <>) <$> processGroups lowerPriorityRules
269+
let rs = map (\(r, _p, _subst) -> r) xs
270+
throw $
271+
RewriteRemainderPredicate rs satRes . coerce . foldl1 AndTerm $
272+
map coerce . Set.toList $
273+
remainder
274+
satRes@SMT.IsUnknown{} -> do
275+
-- solver cannot solve the remainder. Descend into the remainder branch anyway
276+
-- (resultsWithoutRemainders <>) <$> processGroups lowerPriorityRules
277+
let rs = map (\(r, _p, _subst) -> r) xs
278+
throw $
279+
RewriteRemainderPredicate rs satRes . coerce . foldl1 AndTerm $
280+
map coerce . Set.toList $
281+
remainder
260282

261283
-- if any isFalse remainder -- no need to call SMT if any of the conditions is trivially false
262284
-- then do

0 commit comments

Comments
 (0)