@@ -201,7 +201,7 @@ rewriteStep cutLabels terminalLabels pat = do
201
201
AppliedRules ([] , _remainder) ->
202
202
-- TODO check that remainder is trivial, abort otherwise
203
203
processGroups rest
204
- AppliedRules ([(rule, newPat, _subst)], remainder)
204
+ AppliedRules ([(rule, newPat, _subst, _rulePred )], remainder)
205
205
| not (Set. null remainder) && not (any isFalse remainder) -> do
206
206
-- a non-trivial remainder with a single applicable rule is
207
207
-- an indication if semantics incompleteness: abort
@@ -223,7 +223,7 @@ rewriteStep cutLabels terminalLabels pat = do
223
223
-- multiple rules apply, analyse brunching and remainders
224
224
if any isFalse remainder
225
225
then do
226
- logRemainder (map (\ (r, _, _) -> r) xs) SMT. IsUnsat remainder
226
+ logRemainder (map (\ (r, _, _, _ ) -> r) xs) SMT. IsUnsat remainder
227
227
-- the remainder predicate is trivially false, return the branching result
228
228
pure $ mkBranch pat xs
229
229
else do
@@ -233,26 +233,29 @@ rewriteStep cutLabels terminalLabels pat = do
233
233
SMT. isSat solver (Set. toList $ pat. constraints <> remainder) >>= \ case
234
234
SMT. IsUnsat -> do
235
235
-- 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
237
237
pure $ mkBranch pat xs
238
238
satRes@ (SMT. IsSat {}) -> do
239
239
-- the remainder condition is satisfiable.
240
240
-- TODO construct the remainder branch and consider it
241
241
-- To construct the "remainder pattern",
242
242
-- 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
244
244
satRes@ SMT. IsUnknown {} -> do
245
245
-- solver cannot solve the remainder
246
246
-- 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
248
248
249
- mkBranch :: Pattern -> [(RewriteRule " Rewrite" , Pattern , Substitution )] -> RewriteResult Pattern
249
+ mkBranch ::
250
+ Pattern ->
251
+ [(RewriteRule " Rewrite" , Pattern , Substitution , Maybe Predicate )] ->
252
+ RewriteResult Pattern
250
253
mkBranch base leafs =
251
254
let ruleLabelOrLocT = renderOneLineText . ruleLabelOrLoc
252
255
uniqueId = (. uniqueId) . (. attributes)
253
256
in RewriteBranch base $
254
257
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
256
259
257
260
-- abort rewriting by throwing a remainder predicate as an exception, to be caught and processed in @performRewrite@
258
261
throwRemainder ::
@@ -323,7 +326,7 @@ applyRule ::
323
326
LoggerMIO io =>
324
327
Pattern ->
325
328
RewriteRule " Rewrite" ->
326
- RewriteT io (RewriteRuleAppResult (Pattern , Predicate , Substitution ))
329
+ RewriteT io (RewriteRuleAppResult (Pattern , Predicate , Substitution , Maybe Predicate ))
327
330
applyRule pat@ Pattern {ceilConditions} rule =
328
331
withRuleContext rule $
329
332
runRewriteRuleAppT $
@@ -425,16 +428,21 @@ applyRule pat@Pattern{ceilConditions} rule =
425
428
ceilConditions
426
429
withContext CtxSuccess $ do
427
430
case unclearRequiresAfterSmt of
428
- [] -> withPatternContext rewritten $ pure (rewritten, Predicate FalseBool , subst)
431
+ [] -> withPatternContext rewritten $ pure (rewritten, Predicate FalseBool , subst, Nothing )
429
432
_ -> do
433
+ rulePredicate <- mkSimplifiedRulePredicate subst
430
434
-- the requires clause was unclear:
431
435
-- - add it as an assumption to the pattern
432
436
-- - return it's negation as a rule remainder to construct
433
437
--- the remainder pattern in @rewriteStep@
434
438
let rewritten' = rewritten{constraints = rewritten. constraints <> Set. fromList unclearRequiresAfterSmt}
435
439
in withPatternContext rewritten' $
436
440
pure
437
- (rewritten', Predicate $ NotBool $ coerce $ collapseAndBools unclearRequiresAfterSmt, subst)
441
+ ( rewritten'
442
+ , Predicate $ NotBool $ coerce $ collapseAndBools unclearRequiresAfterSmt
443
+ , subst
444
+ , Just rulePredicate
445
+ )
438
446
where
439
447
filterOutKnownConstraints :: Set. Set Predicate -> [Predicate ] -> RewriteT io [Predicate ]
440
448
filterOutKnownConstraints priorKnowledge constraitns = do
@@ -559,6 +567,16 @@ applyRule pat@Pattern{ceilConditions} rule =
559
567
RuleConditionUnclear rule . coerce . foldl1 AndTerm $
560
568
map coerce predicates
561
569
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
+
562
580
data RuleGroupApplication a = OnlyTrivial | AppliedRules a
563
581
564
582
ruleGroupPriority :: [RewriteRule a ] -> Maybe Priority
@@ -573,8 +591,9 @@ ruleGroupPriority = \case
573
591
and return them as a set relating to the whole group
574
592
-}
575
593
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 )
578
597
postProcessRuleAttempts = \ case
579
598
[] -> AppliedRules ([] , mempty )
580
599
apps -> case filter ((/= NotApplied ) . snd ) apps of
@@ -587,7 +606,7 @@ postProcessRuleAttempts = \case
587
606
[] -> AppliedRules (reverse accPatterns, accRemainders)
588
607
((rule, appRes) : xs) ->
589
608
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
591
610
NotApplied -> go acc xs
592
611
Trivial -> go acc xs
593
612
@@ -890,6 +909,9 @@ performRewrite rewriteConfig pat = do
890
909
emitRewriteTrace $ RewriteSimplified (Just other)
891
910
pure $ Just p
892
911
912
+ simplifyRulePredicate :: Predicate -> io Predicate
913
+ simplifyRulePredicate = undefined
914
+
893
915
-- Results may change when simplification prunes a false side
894
916
-- condition, otherwise this would mainly be fmap simplifyP
895
917
simplifyResult ::
@@ -1071,14 +1093,3 @@ rewriteStart =
1071
1093
, traces = mempty
1072
1094
, simplifierCache = mempty
1073
1095
}
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