Skip to content

Commit 3203683

Browse files
committed
refactor applyEquation
1 parent ca0e798 commit 3203683

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
@@ -872,50 +872,52 @@ applyEquation term rule = fmap (either Failure Success) $ runExceptT $ do
872872
$ "Substitution:"
873873
<+> (hsep $ intersperse "," $ map (\(k, v) -> pretty k <+> "->" <+> pretty v) $ Map.toList subst)
874874

875-
-- check required conditions, using substitution
875+
-- instantiate the requires clause with the obtained substitution
876876
let required =
877877
concatMap
878878
(splitBoolPredicates . coerce . substituteInTerm subst . coerce)
879879
rule.requires
880880
-- If the required condition is _syntactically_ present in
881881
-- the prior (known constraints), we don't check it.
882882
knownPredicates <- (.predicates) <$> lift getState
883-
let (knownTrue, toCheck) = partition (`Set.member` knownPredicates) required
884-
unless (null knownTrue) $
885-
logMessage $
886-
renderOneLineText $
887-
"Known true side conditions (won't check):" <+> hsep (intersperse "," $ map pretty knownTrue)
883+
toCheck <- lift $ filterOutKnownConstraints knownPredicates required
884+
885+
-- check the filtered requires clause conditions
886+
unclearConditions <- catMaybes <$> mapM (checkConstraint ConditionFalse) toCheck
888887

889888
-- unclear conditions may have been simplified and
890-
-- could now be syntactically present in the path constraints, check again
891-
-- FIXME: factor this filtering out into a function ans use above
892-
unclearConditions' <- catMaybes <$> mapM (checkConstraint ConditionFalse) toCheck
893-
let (newKnownTrue, stillUnclear) = partition (`Set.member` knownPredicates) unclearConditions'
894-
unless (null newKnownTrue) $
895-
logMessage $
896-
renderOneLineText $
897-
"Known true side conditions (won't check):" <+> hsep (intersperse "," $ map pretty knownTrue)
898-
899-
case stillUnclear of
900-
[] -> do
901-
-- check ensured conditions, filter any
902-
-- true ones, prune if any is false
903-
let ensured =
904-
concatMap
905-
(splitBoolPredicates . substituteInPredicate subst)
906-
(Set.toList rule.ensures)
907-
ensuredConditions <-
908-
-- throws if an ensured condition found to be false
909-
catMaybes <$> mapM (checkConstraint EnsuresFalse) ensured
910-
lift $ pushConstraints $ Set.fromList ensuredConditions
911-
pure $ substituteInTerm subst rule.rhs
912-
unclearConditions -> do
913-
withContext "failure" $
914-
logMessage $
915-
renderOneLineText $
916-
"Uncertain about a condition(s) in rule:" <+> hsep (intersperse "," $ map pretty unclearConditions)
917-
throwE $ IndeterminateCondition unclearConditions
889+
-- could now be syntactically present in the path constraints, filter again
890+
stillUnclear <- lift $ filterOutKnownConstraints knownPredicates unclearConditions
891+
892+
-- abort if any of the conditions is still unclear at that point
893+
unless (null stillUnclear) $ do
894+
withContext "failure" $
895+
logMessage $
896+
renderOneLineText $
897+
"Uncertain about a condition(s) in rule:" <+> hsep (intersperse "," $ map pretty stillUnclear)
898+
throwE $ IndeterminateCondition unclearConditions
899+
900+
-- check ensured conditions, filter any
901+
-- true ones, prune if any is false
902+
let ensured =
903+
concatMap
904+
(splitBoolPredicates . substituteInPredicate subst)
905+
(Set.toList rule.ensures)
906+
ensuredConditions <-
907+
-- throws if an ensured condition found to be false
908+
catMaybes <$> mapM (checkConstraint EnsuresFalse) ensured
909+
lift $ pushConstraints $ Set.fromList ensuredConditions
910+
pure $ substituteInTerm subst rule.rhs
918911
where
912+
filterOutKnownConstraints :: Set Predicate -> [Predicate] -> EquationT io [Predicate]
913+
filterOutKnownConstraints priorKnowledge constraitns = do
914+
let (knownTrue, toCheck) = partition (`Set.member` priorKnowledge) constraitns
915+
unless (null knownTrue) $
916+
logMessage $
917+
renderOneLineText $
918+
"Known true side conditions (won't check):" <+> hsep (intersperse "," $ map pretty knownTrue)
919+
pure toCheck
920+
919921
-- Simplify given predicate in a nested EquationT execution.
920922
-- Call 'whenBottom' if it is Bottom, return Nothing if it is Top,
921923
-- otherwise return the simplified remaining predicate.

0 commit comments

Comments
 (0)