@@ -543,10 +543,19 @@ bool RewriteSystem::addRule(Term lhs, Term rhs) {
543
543
if (i == j)
544
544
continue ;
545
545
546
+ // We don't have to check for overlap with deleted rules.
547
+ if (Rules[j].isDeleted ())
548
+ continue ;
549
+
546
550
// The overlap check is not commutative so we have to check both
547
551
// directions.
548
552
Worklist.emplace_back (i, j);
549
553
Worklist.emplace_back (j, i);
554
+
555
+ if (DebugCompletion) {
556
+ llvm::dbgs () << " $ Queued up (" << i << " , " << j << " ) and " ;
557
+ llvm::dbgs () << " (" << j << " , " << i << " )\n " ;
558
+ }
550
559
}
551
560
552
561
// Tell the caller that we added a new rule.
@@ -779,16 +788,14 @@ void RewriteSystem::processMergedAssociatedTypes() {
779
788
//
780
789
// [P1&P2].[Q] => [P1&P2]
781
790
//
782
- auto otherRHS = otherRule. getRHS () ;
783
- assert (otherRHS. size () == 1 );
784
- assert (otherRHS[ 0 ] == otherLHS[0 ]);
791
+ Term newLHS ;
792
+ newLHS. add (mergedAtom );
793
+ newLHS. add ( otherLHS[1 ]);
785
794
786
- otherRHS.back () = mergedAtom;
795
+ Term newRHS;
796
+ newRHS.add (mergedAtom);
787
797
788
- auto newLHS = otherRHS;
789
- newLHS.add (Atom::forProtocol (otherLHS[1 ].getProtocol (), Context));
790
-
791
- addRule (newLHS, otherRHS);
798
+ addRule (newLHS, newRHS);
792
799
}
793
800
}
794
801
}
@@ -820,12 +827,28 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
820
827
const auto &lhs = Rules[pair.first ];
821
828
const auto &rhs = Rules[pair.second ];
822
829
823
- // If either rule was deleted since, we don't have to check for overlap.
824
- if (lhs.isDeleted () || rhs.isDeleted ())
825
- continue ;
830
+ if (DebugCompletion) {
831
+ llvm::dbgs () << " $ Check for overlap: (#" << pair.first << " ) " ;
832
+ lhs.dump (llvm::dbgs ());
833
+ llvm::dbgs () << " \n " ;
834
+ llvm::dbgs () << " -vs- (#" << pair.second << " ) " ;
835
+ rhs.dump (llvm::dbgs ());
836
+ llvm::dbgs () << " :" ;
837
+ }
826
838
827
- if (!lhs.checkForOverlap (rhs, first))
839
+ if (!lhs.checkForOverlap (rhs, first)) {
840
+ if (DebugCompletion) {
841
+ llvm::dbgs () << " no overlap\n\n " ;
842
+ }
828
843
continue ;
844
+ }
845
+
846
+ if (DebugCompletion) {
847
+ llvm::dbgs () << " \n " ;
848
+ llvm::dbgs () << " $$ Overlapping term is " ;
849
+ first.dump (llvm::dbgs ());
850
+ llvm::dbgs () << " \n " ;
851
+ }
829
852
830
853
assert (first.size () > 0 );
831
854
@@ -841,6 +864,15 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
841
864
lhs.apply (first);
842
865
rhs.apply (second);
843
866
867
+ if (DebugCompletion) {
868
+ llvm::dbgs () << " $$ First term of critical pair is " ;
869
+ first.dump (llvm::dbgs ());
870
+ llvm::dbgs () << " \n " ;
871
+
872
+ llvm::dbgs () << " $$ Second term of critical pair is " ;
873
+ second.dump (llvm::dbgs ());
874
+ llvm::dbgs () << " \n\n " ;
875
+ }
844
876
unsigned i = Rules.size ();
845
877
846
878
// Try to repair the confluence violation by adding a new rule
@@ -869,8 +901,14 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
869
901
continue ;
870
902
871
903
// If this rule reduces some existing rule, delete the existing rule.
872
- if (rule.canReduceLeftHandSide (newRule))
904
+ if (rule.canReduceLeftHandSide (newRule)) {
905
+ if (DebugCompletion) {
906
+ llvm::dbgs () << " $ Deleting rule " ;
907
+ rule.dump (llvm::dbgs ());
908
+ llvm::dbgs () << " \n " ;
909
+ }
873
910
rule.markDeleted ();
911
+ }
874
912
}
875
913
876
914
// If this new rule merges any associated types, process the merge now
@@ -882,17 +920,14 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
882
920
883
921
// This isn't necessary for correctness, it's just an optimization.
884
922
for (auto &rule : Rules) {
923
+ if (rule.isDeleted ())
924
+ continue ;
925
+
885
926
auto rhs = rule.getRHS ();
886
927
simplify (rhs);
887
928
rule = Rule (rule.getLHS (), rhs);
888
929
}
889
930
890
- // Just for aesthetics in dump().
891
- std::sort (Rules.begin (), Rules.end (),
892
- [&](Rule lhs, Rule rhs) -> int {
893
- return lhs.getLHS ().compare (rhs.getLHS (), Protos) < 0 ;
894
- });
895
-
896
931
return CompletionResult::Success;
897
932
}
898
933
0 commit comments