Skip to content

Commit

Permalink
Check constraints for consistency in evaluatePattern'
Browse files Browse the repository at this point in the history
  • Loading branch information
geo2a committed Aug 1, 2024
1 parent d66088a commit 7501ba3
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions booster/library/Booster/Pattern/ApplyEquations.hs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,11 @@ evaluateTerm' ::
evaluateTerm' direction = iterateEquations direction PreferFunctions

{- | Simplify a Pattern, processing its constraints independently.
Returns either the first failure or the new pattern if no failure was encountered
Before evaluating the term of the pattern,
the constraints of the pattern are checked for consistency with an SMT solver.
Returns either the first failure or the new pattern if no failure was encountered.
-}
evaluatePattern ::
LoggerMIO io =>
Expand All @@ -430,13 +434,28 @@ evaluatePattern' ::
Pattern ->
EquationT io Pattern
evaluatePattern' pat@Pattern{term, ceilConditions} = withPatternContext pat $ do
newTerm <- withTermContext term $ evaluateTerm' BottomUp term `catch_` keepTopLevelResults
-- after evaluating the term, evaluate all (existing and
-- newly-acquired) constraints, once
traverse_ simplifyAssumedPredicate . predicates =<< getState
-- this may yield additional new constraints, left unevaluated
evaluatedConstraints <- predicates <$> getState
pure Pattern{constraints = evaluatedConstraints, term = newTerm, ceilConditions}
solver <- (.smtSolver) <$> getConfig
-- check initial constraints for consistency, reporting an error if they are Bottom
SMT.isSat solver pat.constraints >>= \case
Right False -> do
let collapseAndBools :: Set Predicate -> Predicate
collapseAndBools = undefined
-- the constraints are unsatisfiable, which means that the patten is Bottom
throw . SideConditionFalse . collapseAndBools $ pat.constraints
Left unknwon@SMT.SMTSolverUnknown{} -> do
-- unlikely case of an Unknown response to a consistency check.
-- What to do here? fail hard for now.
liftIO $ Exception.throw unknwon
Left other -> liftIO $ Exception.throw other -- fail hard on other SMT errors
Right True -> do
-- constrains are consistent, continue
newTerm <- withTermContext term $ evaluateTerm' BottomUp term `catch_` keepTopLevelResults
-- after evaluating the term, evaluate all (existing and
-- newly-acquired) constraints, once
traverse_ simplifyAssumedPredicate . predicates =<< getState
-- this may yield additional new constraints, left unevaluated
evaluatedConstraints <- predicates <$> getState
pure Pattern{constraints = evaluatedConstraints, term = newTerm, ceilConditions}
where
-- when TooManyIterations exception occurred while evaluating the top-level term,
-- i.e. not in a recursive evaluation of a side-condition,
Expand Down

0 comments on commit 7501ba3

Please sign in to comment.