Skip to content

Commit 11ae8e0

Browse files
committed
Simplify rule predicates
1 parent 8a2063a commit 11ae8e0

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

booster/library/Booster/Pattern/Rewrite.hs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ rewriteStep cutLabels terminalLabels pat = do
201201
AppliedRules ([], _remainder) ->
202202
-- TODO check that remainder is trivial, abort otherwise
203203
processGroups rest
204-
AppliedRules ([(rule, newPat, _subst)], remainder)
204+
AppliedRules ([(rule, newPat, _subst, _rulePred)], remainder)
205205
| not (Set.null remainder) && not (any isFalse remainder) -> do
206206
-- a non-trivial remainder with a single applicable rule is
207207
-- an indication if semantics incompleteness: abort
@@ -223,7 +223,7 @@ rewriteStep cutLabels terminalLabels pat = do
223223
-- multiple rules apply, analyse brunching and remainders
224224
if any isFalse remainder
225225
then do
226-
logRemainder (map (\(r, _, _) -> r) xs) SMT.IsUnsat remainder
226+
logRemainder (map (\(r, _, _, _) -> r) xs) SMT.IsUnsat remainder
227227
-- the remainder predicate is trivially false, return the branching result
228228
pure $ mkBranch pat xs
229229
else do
@@ -233,26 +233,29 @@ rewriteStep cutLabels terminalLabels pat = do
233233
SMT.isSat solver (Set.toList $ pat.constraints <> remainder) >>= \case
234234
SMT.IsUnsat -> do
235235
-- the remainder condition is unsatisfiable: no need to consider the remainder branch.
236-
logRemainder (map (\(r, _, _) -> r) xs) SMT.IsUnsat remainder
236+
logRemainder (map (\(r, _, _, _) -> r) xs) SMT.IsUnsat remainder
237237
pure $ mkBranch pat xs
238238
satRes@(SMT.IsSat{}) -> do
239239
-- the remainder condition is satisfiable.
240240
-- TODO construct the remainder branch and consider it
241241
-- To construct the "remainder pattern",
242242
-- we add the remainder condition to the predicates of the @pattr@
243-
throwRemainder (map (\(r, _p, _subst) -> r) xs) satRes remainder
243+
throwRemainder (map (\(r, _p, _subst, _) -> r) xs) satRes remainder
244244
satRes@SMT.IsUnknown{} -> do
245245
-- solver cannot solve the remainder
246246
-- TODO descend into the remainder branch anyway
247-
throwRemainder (map (\(r, _p, _subst) -> r) xs) satRes remainder
247+
throwRemainder (map (\(r, _p, _subst, _) -> r) xs) satRes remainder
248248

249-
mkBranch :: Pattern -> [(RewriteRule "Rewrite", Pattern, Substitution)] -> RewriteResult Pattern
249+
mkBranch ::
250+
Pattern ->
251+
[(RewriteRule "Rewrite", Pattern, Substitution, Maybe Predicate)] ->
252+
RewriteResult Pattern
250253
mkBranch base leafs =
251254
let ruleLabelOrLocT = renderOneLineText . ruleLabelOrLoc
252255
uniqueId = (.uniqueId) . (.attributes)
253256
in RewriteBranch base $
254257
NE.fromList $
255-
map (\(r, p, subst) -> (ruleLabelOrLocT r, uniqueId r, p, mkRulePredicate r subst, subst)) leafs
258+
map (\(r, p, subst, rulePred) -> (ruleLabelOrLocT r, uniqueId r, p, rulePred, subst)) leafs
256259

257260
-- abort rewriting by throwing a remainder predicate as an exception, to be caught and processed in @performRewrite@
258261
throwRemainder ::
@@ -323,7 +326,7 @@ applyRule ::
323326
LoggerMIO io =>
324327
Pattern ->
325328
RewriteRule "Rewrite" ->
326-
RewriteT io (RewriteRuleAppResult (Pattern, Predicate, Substitution))
329+
RewriteT io (RewriteRuleAppResult (Pattern, Predicate, Substitution, Maybe Predicate))
327330
applyRule pat@Pattern{ceilConditions} rule =
328331
withRuleContext rule $
329332
runRewriteRuleAppT $
@@ -425,16 +428,21 @@ applyRule pat@Pattern{ceilConditions} rule =
425428
ceilConditions
426429
withContext CtxSuccess $ do
427430
case unclearRequiresAfterSmt of
428-
[] -> withPatternContext rewritten $ pure (rewritten, Predicate FalseBool, subst)
431+
[] -> withPatternContext rewritten $ pure (rewritten, Predicate FalseBool, subst, Nothing)
429432
_ -> do
433+
rulePredicate <- mkSimplifiedRulePredicate subst
430434
-- the requires clause was unclear:
431435
-- - add it as an assumption to the pattern
432436
-- - return it's negation as a rule remainder to construct
433437
--- the remainder pattern in @rewriteStep@
434438
let rewritten' = rewritten{constraints = rewritten.constraints <> Set.fromList unclearRequiresAfterSmt}
435439
in withPatternContext rewritten' $
436440
pure
437-
(rewritten', Predicate $ NotBool $ coerce $ collapseAndBools unclearRequiresAfterSmt, subst)
441+
( rewritten'
442+
, Predicate $ NotBool $ coerce $ collapseAndBools unclearRequiresAfterSmt
443+
, subst
444+
, Just rulePredicate
445+
)
438446
where
439447
filterOutKnownConstraints :: Set.Set Predicate -> [Predicate] -> RewriteT io [Predicate]
440448
filterOutKnownConstraints priorKnowledge constraitns = do
@@ -559,6 +567,16 @@ applyRule pat@Pattern{ceilConditions} rule =
559567
RuleConditionUnclear rule . coerce . foldl1 AndTerm $
560568
map coerce predicates
561569

570+
-- Instantiate the requires clause of the rule and simplify, but not prune.
571+
-- Unfortunately this function may have to re-do work that was already done by checkRequires
572+
mkSimplifiedRulePredicate :: Substitution -> RewriteRuleAppT (RewriteT io) Predicate
573+
mkSimplifiedRulePredicate matchingSubst = do
574+
-- apply substitution to rule requires
575+
let ruleRequires =
576+
concatMap (splitBoolPredicates . coerce . substituteInTerm matchingSubst . coerce) rule.requires
577+
collapseAndBools . catMaybes
578+
<$> mapM (checkConstraint id returnNotApplied pat.constraints) ruleRequires
579+
562580
data RuleGroupApplication a = OnlyTrivial | AppliedRules a
563581

564582
ruleGroupPriority :: [RewriteRule a] -> Maybe Priority
@@ -573,8 +591,9 @@ ruleGroupPriority = \case
573591
and return them as a set relating to the whole group
574592
-}
575593
postProcessRuleAttempts ::
576-
[(RewriteRule "Rewrite", RewriteRuleAppResult (Pattern, Predicate, Substitution))] ->
577-
RuleGroupApplication ([(RewriteRule "Rewrite", Pattern, Substitution)], Set.Set Predicate)
594+
[(RewriteRule "Rewrite", RewriteRuleAppResult (Pattern, Predicate, Substitution, Maybe Predicate))] ->
595+
RuleGroupApplication
596+
([(RewriteRule "Rewrite", Pattern, Substitution, Maybe Predicate)], Set.Set Predicate)
578597
postProcessRuleAttempts = \case
579598
[] -> AppliedRules ([], mempty)
580599
apps -> case filter ((/= NotApplied) . snd) apps of
@@ -587,7 +606,7 @@ postProcessRuleAttempts = \case
587606
[] -> AppliedRules (reverse accPatterns, accRemainders)
588607
((rule, appRes) : xs) ->
589608
case appRes of
590-
Applied (pat, remainder, subst) -> go ((rule, pat, subst) : accPatterns, Set.singleton remainder <> accRemainders) xs
609+
Applied (pat, remainder, subst, rulePred) -> go ((rule, pat, subst, rulePred) : accPatterns, Set.singleton remainder <> accRemainders) xs
591610
NotApplied -> go acc xs
592611
Trivial -> go acc xs
593612

@@ -890,6 +909,9 @@ performRewrite rewriteConfig pat = do
890909
emitRewriteTrace $ RewriteSimplified (Just other)
891910
pure $ Just p
892911

912+
simplifyRulePredicate :: Predicate -> io Predicate
913+
simplifyRulePredicate = undefined
914+
893915
-- Results may change when simplification prunes a false side
894916
-- condition, otherwise this would mainly be fmap simplifyP
895917
simplifyResult ::
@@ -1071,14 +1093,3 @@ rewriteStart =
10711093
, traces = mempty
10721094
, simplifierCache = mempty
10731095
}
1074-
1075-
{- | Instantiate a rewrite rule's requires clause with a substitution.
1076-
Returns Nothing is the resulting @Predicate@ is trivially @True@.
1077-
-}
1078-
mkRulePredicate :: RewriteRule a -> Substitution -> Maybe Predicate
1079-
mkRulePredicate rule subst =
1080-
case concatMap
1081-
(splitBoolPredicates . coerce . substituteInTerm subst . coerce)
1082-
rule.requires of
1083-
[] -> Nothing
1084-
xs -> Just $ collapseAndBools xs

0 commit comments

Comments
 (0)