Skip to content

Commit d03799c

Browse files
committed
Tweak substitution naming in applyRule
1 parent 7a3629f commit d03799c

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

booster/library/Booster/Pattern/Rewrite.hs

+14-27
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ applyRule pat@Pattern{ceilConditions} rule =
279279
getPrettyModifiers >>= \case
280280
ModifiersRep (_ :: FromModifiersT mods => Proxy mods) -> do
281281
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
284284
MatchFailed (SubsortingError sortError) -> do
285285
withContext CtxError $ logPretty' @mods sortError
286286
failRewrite $ RewriteSortError rule pat.term sortError
@@ -315,7 +315,7 @@ applyRule pat@Pattern{ceilConditions} rule =
315315
Map.fromSet
316316
(\v -> Var $ freshenVar v{variableName = stripMarker v.variableName} forbiddenVars)
317317
rule.existentials
318-
combinedSubstitution = matchingSubstitution <> existentialSubst
318+
ruleSubstitution = matchingSubstitution <> existentialSubst
319319

320320
withContext CtxSuccess $ do
321321
logMessage rule
@@ -324,17 +324,17 @@ applyRule pat@Pattern{ceilConditions} rule =
324324
$ WithJsonMessage
325325
( object
326326
[ "substitution"
327-
.= (bimap (externaliseTerm . Var) externaliseTerm <$> Map.toList combinedSubstitution)
327+
.= (bimap (externaliseTerm . Var) externaliseTerm <$> Map.toList ruleSubstitution)
328328
]
329329
)
330330
$ renderOneLineText
331331
$ "Substitution:"
332332
<+> ( hsep $
333333
intersperse "," $
334334
map (\(k, v) -> pretty' @mods k <+> "->" <+> pretty' @mods v) $
335-
Map.toList combinedSubstitution
335+
Map.toList ruleSubstitution
336336
)
337-
pure combinedSubstitution
337+
pure ruleSubstitution
338338

339339
-- Also fail the whole rewrite if a rule applies but may introduce
340340
-- an undefined term.
@@ -351,11 +351,11 @@ applyRule pat@Pattern{ceilConditions} rule =
351351
rule.computedAttributes.notPreservesDefinednessReasons
352352

353353
-- check required constraints from lhs: Stop if any is false, abort rewrite if indeterminate.
354-
checkRequires subst
354+
checkRequires ruleSubstitution
355355

356356
-- check ensures constraints (new) from rhs: stop and return `Trivial` if
357357
-- any are false, remove all that are trivially true, return the rest
358-
ensuredConditions <- checkEnsures subst
358+
ensuredConditions <- checkEnsures ruleSubstitution
359359

360360
-- if a new constraint is going to be added, the equation cache is invalid
361361
unless (null ensuredConditions) $ do
@@ -366,39 +366,26 @@ applyRule pat@Pattern{ceilConditions} rule =
366366
-- partition ensured constrains into substitution and predicates
367367
let (newSubsitution, newConstraints) = partitionPredicates ensuredConditions
368368

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
378373
substitutedNewConstraints =
379374
Set.fromList $
380375
map
381-
(coerce . substituteInTerm normalisedPatternSubst . substituteInTerm subst . coerce)
376+
(coerce . substituteInTerm (modifiedPatternSubst `compose` ruleSubstitution) . coerce)
382377
newConstraints
383378
let rewritten =
384379
Pattern
385380
rewrittenTerm
386381
-- adding new constraints that have not been trivially `Top`, substituting the Ex# variables
387382
(pat.constraints <> substitutedNewConstraints)
388-
normalisedPatternSubst
383+
modifiedPatternSubst -- ruleSubstitution is not needed, do not attach it to the result
389384
ceilConditions
390385
withContext CtxSuccess $
391386
withPatternContext rewritten $
392387
return (rule, rewritten)
393388
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
402389
filterOutKnownConstraints :: Set.Set Predicate -> [Predicate] -> RewriteT io [Predicate]
403390
filterOutKnownConstraints priorKnowledge constraitns = do
404391
let (knownTrue, toCheck) = partition (`Set.member` priorKnowledge) constraitns

0 commit comments

Comments
 (0)