@@ -457,15 +457,39 @@ evaluatePattern ::
457
457
evaluatePattern def mLlvmLibrary smtSolver cache pat =
458
458
runEquationT def mLlvmLibrary smtSolver cache pat. constraints . evaluatePattern' $ pat
459
459
460
+ -- version for internal nested evaluation
460
461
-- version for internal nested evaluation
461
462
evaluatePattern' ::
462
463
LoggerMIO io =>
463
464
Pattern ->
464
465
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
+
466
491
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
469
493
traverse_ simplifyAssumedPredicate . predicates =<< getState
470
494
-- this may yield additional new constraints, left unevaluated
471
495
evaluatedConstraints <- predicates <$> getState
@@ -482,6 +506,9 @@ evaluatePattern' pat@Pattern{term, ceilConditions} = withPatternContext pat $ do
482
506
pure partialResult
483
507
err -> throw err
484
508
509
+ collapseAndBools :: Set Predicate -> Predicate
510
+ collapseAndBools = coerce . foldAndBool . map coerce . Set. toList
511
+
485
512
-- evaluate the given predicate assuming all others
486
513
simplifyAssumedPredicate :: LoggerMIO io => Predicate -> EquationT io ()
487
514
simplifyAssumedPredicate p = do
0 commit comments