@@ -279,8 +279,8 @@ applyRule pat@Pattern{ceilConditions} rule =
279
279
getPrettyModifiers >>= \ case
280
280
ModifiersRep (_ :: FromModifiersT mods => Proxy mods ) -> do
281
281
def <- lift getDefinition
282
- -- unify terms
283
- subst <- withContext CtxMatch $ case matchTerms Rewrite def rule. lhs pat. term of
282
+ -- match term with rule's left-hand side
283
+ ruleSubstitution <- withContext CtxMatch $ case matchTerms Rewrite def rule. lhs pat. term of
284
284
MatchFailed (SubsortingError sortError) -> do
285
285
withContext CtxError $ logPretty' @ mods sortError
286
286
failRewrite $ RewriteSortError rule pat. term sortError
@@ -315,7 +315,7 @@ applyRule pat@Pattern{ceilConditions} rule =
315
315
Map. fromSet
316
316
(\ v -> Var $ freshenVar v{variableName = stripMarker v. variableName} forbiddenVars)
317
317
rule. existentials
318
- combinedSubstitution = matchingSubstitution <> existentialSubst
318
+ ruleSubstitution = matchingSubstitution <> existentialSubst
319
319
320
320
withContext CtxSuccess $ do
321
321
logMessage rule
@@ -324,17 +324,17 @@ applyRule pat@Pattern{ceilConditions} rule =
324
324
$ WithJsonMessage
325
325
( object
326
326
[ " substitution"
327
- .= (bimap (externaliseTerm . Var ) externaliseTerm <$> Map. toList combinedSubstitution )
327
+ .= (bimap (externaliseTerm . Var ) externaliseTerm <$> Map. toList ruleSubstitution )
328
328
]
329
329
)
330
330
$ renderOneLineText
331
331
$ " Substitution:"
332
332
<+> ( hsep $
333
333
intersperse " ," $
334
334
map (\ (k, v) -> pretty' @ mods k <+> " ->" <+> pretty' @ mods v) $
335
- Map. toList combinedSubstitution
335
+ Map. toList ruleSubstitution
336
336
)
337
- pure combinedSubstitution
337
+ pure ruleSubstitution
338
338
339
339
-- Also fail the whole rewrite if a rule applies but may introduce
340
340
-- an undefined term.
@@ -351,11 +351,11 @@ applyRule pat@Pattern{ceilConditions} rule =
351
351
rule. computedAttributes. notPreservesDefinednessReasons
352
352
353
353
-- check required constraints from lhs: Stop if any is false, abort rewrite if indeterminate.
354
- checkRequires subst
354
+ checkRequires ruleSubstitution
355
355
356
356
-- check ensures constraints (new) from rhs: stop and return `Trivial` if
357
357
-- any are false, remove all that are trivially true, return the rest
358
- ensuredConditions <- checkEnsures subst
358
+ ensuredConditions <- checkEnsures ruleSubstitution
359
359
360
360
-- if a new constraint is going to be added, the equation cache is invalid
361
361
unless (null ensuredConditions) $ do
@@ -366,39 +366,26 @@ applyRule pat@Pattern{ceilConditions} rule =
366
366
-- partition ensured constrains into substitution and predicates
367
367
let (newSubsitution, newConstraints) = partitionPredicates ensuredConditions
368
368
369
- -- merge existing substitution and newly acquired one, applying the latter to the former
370
- normalisedPatternSubst <-
371
- lift $
372
- normaliseSubstitution
373
- pat. substitution
374
- newSubsitution
375
- -- NOTE it is necessary to first apply the rule substitution and then the pattern/ensures substitution, but it is suboptimal to traverse the term twice.
376
- -- TODO a substitution composition operator
377
- let rewrittenTerm = substituteInTerm normalisedPatternSubst . substituteInTerm subst $ rule. rhs
369
+ -- compose the existing substitution pattern and the newly acquired one
370
+ let modifiedPatternSubst = newSubsitution `compose` pat. substitution
371
+
372
+ let rewrittenTerm = substituteInTerm (modifiedPatternSubst `compose` ruleSubstitution) rule. rhs
378
373
substitutedNewConstraints =
379
374
Set. fromList $
380
375
map
381
- (coerce . substituteInTerm normalisedPatternSubst . substituteInTerm subst . coerce)
376
+ (coerce . substituteInTerm (modifiedPatternSubst `compose` ruleSubstitution) . coerce)
382
377
newConstraints
383
378
let rewritten =
384
379
Pattern
385
380
rewrittenTerm
386
381
-- adding new constraints that have not been trivially `Top`, substituting the Ex# variables
387
382
(pat. constraints <> substitutedNewConstraints)
388
- normalisedPatternSubst
383
+ modifiedPatternSubst -- ruleSubstitution is not needed, do not attach it to the result
389
384
ceilConditions
390
385
withContext CtxSuccess $
391
386
withPatternContext rewritten $
392
387
return (rule, rewritten)
393
388
where
394
- -- Given known predicates, a known substitution and a newly acquired substitution (from the ensures clause):
395
- -- - apply the new substitution to the old substitution
396
- -- - merge with the new substitution items and return
397
- normaliseSubstitution ::
398
- Substitution -> Substitution -> RewriteT io Substitution
399
- normaliseSubstitution oldSubst newSubst = do
400
- let substitutedOldSubst = Map. map (substituteInTerm newSubst) oldSubst
401
- pure (newSubst `Map.union` substitutedOldSubst) -- new bindings take priority
402
389
filterOutKnownConstraints :: Set. Set Predicate -> [Predicate ] -> RewriteT io [Predicate ]
403
390
filterOutKnownConstraints priorKnowledge constraitns = do
404
391
let (knownTrue, toCheck) = partition (`Set.member` priorKnowledge) constraitns
0 commit comments