Skip to content

Commit 8d363a8

Browse files
committed
refactor applyEquation
1 parent c5f8fcd commit 8d363a8

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

booster/library/Booster/Pattern/ApplyEquations.hs

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -953,50 +953,52 @@ applyEquation term rule = withRuleContext rule $ fmap (either Failure Success) $
953953
$ "Substitution:"
954954
<+> (hsep $ intersperse "," $ map (\(k, v) -> pretty k <+> "->" <+> pretty v) $ Map.toList subst)
955955

956-
-- check required conditions, using substitution
956+
-- instantiate the requires clause with the obtained substitution
957957
let required =
958958
concatMap
959959
(splitBoolPredicates . coerce . substituteInTerm subst . coerce)
960960
rule.requires
961961
-- If the required condition is _syntactically_ present in
962962
-- the prior (known constraints), we don't check it.
963963
knownPredicates <- (.predicates) <$> lift getState
964-
let (knownTrue, toCheck) = partition (`Set.member` knownPredicates) required
965-
unless (null knownTrue) $
966-
logMessage $
967-
renderOneLineText $
968-
"Known true side conditions (won't check):" <+> hsep (intersperse "," $ map pretty knownTrue)
964+
toCheck <- lift $ filterOutKnownConstraints knownPredicates required
965+
966+
-- check the filtered requires clause conditions
967+
unclearConditions <- catMaybes <$> mapM (checkConstraint ConditionFalse) toCheck
969968

970969
-- unclear conditions may have been simplified and
971-
-- could now be syntactically present in the path constraints, check again
972-
-- FIXME: factor this filtering out into a function ans use above
973-
unclearConditions' <- catMaybes <$> mapM (checkConstraint ConditionFalse) toCheck
974-
let (newKnownTrue, stillUnclear) = partition (`Set.member` knownPredicates) unclearConditions'
975-
unless (null newKnownTrue) $
976-
logMessage $
977-
renderOneLineText $
978-
"Known true side conditions (won't check):" <+> hsep (intersperse "," $ map pretty knownTrue)
979-
980-
case stillUnclear of
981-
[] -> do
982-
-- check ensured conditions, filter any
983-
-- true ones, prune if any is false
984-
let ensured =
985-
concatMap
986-
(splitBoolPredicates . substituteInPredicate subst)
987-
(Set.toList rule.ensures)
988-
ensuredConditions <-
989-
-- throws if an ensured condition found to be false
990-
catMaybes <$> mapM (checkConstraint EnsuresFalse) ensured
991-
lift $ pushConstraints $ Set.fromList ensuredConditions
992-
pure $ substituteInTerm subst rule.rhs
993-
unclearConditions -> do
994-
withContext "failure" $
995-
logMessage $
996-
renderOneLineText $
997-
"Uncertain about a condition(s) in rule:" <+> hsep (intersperse "," $ map pretty unclearConditions)
998-
throwE $ IndeterminateCondition unclearConditions
970+
-- could now be syntactically present in the path constraints, filter again
971+
stillUnclear <- lift $ filterOutKnownConstraints knownPredicates unclearConditions
972+
973+
-- abort if any of the conditions is still unclear at that point
974+
unless (null stillUnclear) $ do
975+
withContext "failure" $
976+
logMessage $
977+
renderOneLineText $
978+
"Uncertain about a condition(s) in rule:" <+> hsep (intersperse "," $ map pretty stillUnclear)
979+
throwE $ IndeterminateCondition unclearConditions
980+
981+
-- check ensured conditions, filter any
982+
-- true ones, prune if any is false
983+
let ensured =
984+
concatMap
985+
(splitBoolPredicates . substituteInPredicate subst)
986+
(Set.toList rule.ensures)
987+
ensuredConditions <-
988+
-- throws if an ensured condition found to be false
989+
catMaybes <$> mapM (checkConstraint EnsuresFalse) ensured
990+
lift $ pushConstraints $ Set.fromList ensuredConditions
991+
pure $ substituteInTerm subst rule.rhs
999992
where
993+
filterOutKnownConstraints :: Set Predicate -> [Predicate] -> EquationT io [Predicate]
994+
filterOutKnownConstraints priorKnowledge constraitns = do
995+
let (knownTrue, toCheck) = partition (`Set.member` priorKnowledge) constraitns
996+
unless (null knownTrue) $
997+
logMessage $
998+
renderOneLineText $
999+
"Known true side conditions (won't check):" <+> hsep (intersperse "," $ map pretty knownTrue)
1000+
pure toCheck
1001+
10001002
-- Simplify given predicate in a nested EquationT execution.
10011003
-- Call 'whenBottom' if it is Bottom, return Nothing if it is Top,
10021004
-- otherwise return the simplified remaining predicate.

0 commit comments

Comments
 (0)