Skip to content

Commit 8eeb9ce

Browse files
committed
Check consistency of the pattern's constraints before evaluating it
1 parent 42f950c commit 8eeb9ce

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

booster/library/Booster/Pattern/ApplyEquations.hs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,15 +457,39 @@ evaluatePattern ::
457457
evaluatePattern def mLlvmLibrary smtSolver cache pat =
458458
runEquationT def mLlvmLibrary smtSolver cache pat.constraints . evaluatePattern' $ pat
459459

460+
-- version for internal nested evaluation
460461
-- version for internal nested evaluation
461462
evaluatePattern' ::
462463
LoggerMIO io =>
463464
Pattern ->
464465
EquationT io Pattern
465-
evaluatePattern' pat@Pattern{term, ceilConditions} = withPatternContext pat $ do
466+
evaluatePattern' pat@Pattern{term, constraints, ceilConditions} = withPatternContext pat $ do
467+
solver <- (.smtSolver) <$> getConfig
468+
-- check the pattern's constraints for satisfiability to ensure they are consistent
469+
consistent <-
470+
withContext CtxConstraint $ do
471+
withContext CtxDetail . withTermContext (coerce $ collapseAndBools constraints) $ pure ()
472+
consistent <- SMT.isSat solver (Set.toList constraints)
473+
withContext CtxConstraint $
474+
logMessage $
475+
"Constraints consistency check returns: " <> show consistent
476+
pure consistent
477+
case consistent of
478+
SMT.IsUnsat -> do
479+
-- the constraints are unsatisfiable, which means that the patten is Bottom
480+
throw . SideConditionFalse . collapseAndBools $ constraints
481+
SMT.IsUnknown{} -> do
482+
-- unlikely case of an Unknown response to a consistency check.
483+
-- continue to preserver the old behaviour.
484+
withContext CtxConstraint . logWarn . Text.pack $
485+
"Constraints consistency UNKNOWN: " <> show consistent
486+
pure ()
487+
SMT.IsSat{} ->
488+
-- constraints are consistent, continue
489+
pure ()
490+
466491
newTerm <- withTermContext term $ evaluateTerm' BottomUp term `catch_` keepTopLevelResults
467-
-- after evaluating the term, evaluate all (existing and
468-
-- newly-acquired) constraints, once
492+
-- after evaluating the term, evaluate all (existing and newly-acquired) constraints, once
469493
traverse_ simplifyAssumedPredicate . predicates =<< getState
470494
-- this may yield additional new constraints, left unevaluated
471495
evaluatedConstraints <- predicates <$> getState
@@ -482,6 +506,9 @@ evaluatePattern' pat@Pattern{term, ceilConditions} = withPatternContext pat $ do
482506
pure partialResult
483507
err -> throw err
484508

509+
collapseAndBools :: Set Predicate -> Predicate
510+
collapseAndBools = coerce . foldAndBool . map coerce . Set.toList
511+
485512
-- evaluate the given predicate assuming all others
486513
simplifyAssumedPredicate :: LoggerMIO io => Predicate -> EquationT io ()
487514
simplifyAssumedPredicate p = do

0 commit comments

Comments
 (0)