@@ -337,8 +337,11 @@ void RewriteSystem::checkMergedAssociatedType(Term lhs, Term rhs) {
337
337
// / where \p rhs begins at \p from, which must be an iterator pointing
338
338
// / into \p lhs.
339
339
// /
340
- // / The resulting pair is pushed onto \p result only if it is non-trivial,
341
- // / that is, the left hand side and right hand side are not equal.
340
+ // / The resulting pair, together with a rewrite path relating them is
341
+ // / pushed onto \p pairs only if it is non-trivial, that is, the left
342
+ // / hand side and right hand side are not equal.
343
+ // /
344
+ // / Otherwise, we record a rewrite loop in \p loops.
342
345
// /
343
346
// / Returns true if the pair was non-trivial, false if it was trivial.
344
347
// /
@@ -372,9 +375,7 @@ void RewriteSystem::checkMergedAssociatedType(Term lhs, Term rhs) {
372
375
bool
373
376
RewriteSystem::computeCriticalPair (ArrayRef<Symbol>::const_iterator from,
374
377
const Rule &lhs, const Rule &rhs,
375
- std::vector<std::pair<MutableTerm,
376
- MutableTerm>> &pairs,
377
- std::vector<RewritePath> &paths,
378
+ std::vector<CriticalPair> &pairs,
378
379
std::vector<RewriteLoop> &loops) const {
379
380
auto end = lhs.getLHS ().end ();
380
381
if (from + rhs.getLHS ().size () < end) {
@@ -420,8 +421,7 @@ RewriteSystem::computeCriticalPair(ArrayRef<Symbol>::const_iterator from,
420
421
}
421
422
422
423
// Add the pair (X, TYV).
423
- pairs.emplace_back (x, tyv);
424
- paths.push_back (path);
424
+ pairs.emplace_back (x, tyv, path);
425
425
} else {
426
426
// lhs == TU -> X, rhs == UV -> Y.
427
427
@@ -476,8 +476,7 @@ RewriteSystem::computeCriticalPair(ArrayRef<Symbol>::const_iterator from,
476
476
}
477
477
478
478
// Add the pair (XV, TY).
479
- pairs.emplace_back (xv, ty);
480
- paths.push_back (path);
479
+ pairs.emplace_back (xv, ty, path);
481
480
}
482
481
483
482
return true ;
@@ -508,15 +507,16 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
508
507
509
508
bool again = false ;
510
509
511
- std::vector<std::pair<MutableTerm, MutableTerm>> resolvedCriticalPairs;
512
- std::vector<RewritePath> resolvedPaths;
510
+ std::vector<CriticalPair> resolvedCriticalPairs;
513
511
std::vector<RewriteLoop> resolvedLoops;
514
512
515
513
do {
516
514
// For every rule, looking for other rules that overlap with this rule.
517
515
for (unsigned i = 0 , e = Rules.size (); i < e; ++i) {
518
516
const auto &lhs = getRule (i);
519
- if (lhs.isSimplified ())
517
+ if (lhs.isLHSSimplified () ||
518
+ lhs.isRHSSimplified () ||
519
+ lhs.isSubstitutionSimplified ())
520
520
continue ;
521
521
522
522
// Look up every suffix of this rule in the trie using findAll(). This
@@ -529,7 +529,9 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
529
529
while (from < to) {
530
530
Trie.findAll (from, to, [&](unsigned j) {
531
531
const auto &rhs = getRule (j);
532
- if (rhs.isSimplified ())
532
+ if (rhs.isLHSSimplified () ||
533
+ rhs.isRHSSimplified () ||
534
+ rhs.isSubstitutionSimplified ())
533
535
return ;
534
536
535
537
if (from == lhs.getLHS ().begin ()) {
@@ -557,23 +559,21 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
557
559
// Try to repair the confluence violation by adding a new rule.
558
560
if (computeCriticalPair (from, lhs, rhs,
559
561
resolvedCriticalPairs,
560
- resolvedPaths,
561
562
resolvedLoops)) {
562
563
if (Debug.contains (DebugFlags::Completion)) {
563
564
const auto &pair = resolvedCriticalPairs.back ();
564
- const auto &path = resolvedPaths.back ();
565
565
566
566
llvm::dbgs () << " $ Overlapping rules: (#" << i << " ) " ;
567
567
llvm::dbgs () << lhs << " \n " ;
568
568
llvm::dbgs () << " -vs- (#" << j << " ) " ;
569
569
llvm::dbgs () << rhs << " :\n " ;
570
570
llvm::dbgs () << " $$ First term of critical pair is "
571
- << pair.first << " \n " ;
571
+ << pair.LHS << " \n " ;
572
572
llvm::dbgs () << " $$ Second term of critical pair is "
573
- << pair.second << " \n\n " ;
573
+ << pair.RHS << " \n\n " ;
574
574
575
575
llvm::dbgs () << " $$ Resolved via path: " ;
576
- path. dump (llvm::dbgs (), pair.first , *this );
576
+ pair. Path . dump (llvm::dbgs (), pair.LHS , *this );
577
577
llvm::dbgs () << " \n\n " ;
578
578
}
579
579
} else {
@@ -598,18 +598,13 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
598
598
599
599
simplifyLeftHandSides ();
600
600
601
- assert (resolvedCriticalPairs.size () == resolvedPaths.size ());
602
-
603
601
again = false ;
604
- for (unsigned index : indices (resolvedCriticalPairs)) {
605
- const auto &pair = resolvedCriticalPairs[index];
606
- const auto &path = resolvedPaths[index];
607
-
602
+ for (const auto &pair : resolvedCriticalPairs) {
608
603
// Check if we've already done too much work.
609
604
if (Rules.size () > maxIterations)
610
605
return std::make_pair (CompletionResult::MaxIterations, steps);
611
606
612
- if (!addRule (pair.first , pair.second , &path ))
607
+ if (!addRule (pair.LHS , pair.RHS , &pair. Path ))
613
608
continue ;
614
609
615
610
// Check if the new rule is too long.
@@ -626,10 +621,10 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
626
621
}
627
622
628
623
resolvedCriticalPairs.clear ();
629
- resolvedPaths.clear ();
630
624
resolvedLoops.clear ();
631
625
632
- simplifyRightHandSidesAndSubstitutions ();
626
+ simplifyRightHandSides ();
627
+ simplifyLeftHandSideSubstitutions ();
633
628
634
629
// If the added rules merged any associated types, process the merges now
635
630
// before we continue with the completion procedure. This is important
0 commit comments