Skip to content

Commit ba0fe6d

Browse files
committed
RequirementMachine: Add Rule::isExplicit() bit
Eventually I might upgrade this to a SourceLoc, for redundancy diagnostics.
1 parent b771145 commit ba0fe6d

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

lib/AST/RequirementMachine/RewriteSystem.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ void Rule::dump(llvm::raw_ostream &out) const {
101101
out << LHS << " => " << RHS;
102102
if (Permanent)
103103
out << " [permanent]";
104+
if (Explicit)
105+
out << " [explicit]";
104106
if (Simplified)
105107
out << " [simplified]";
106108
if (Redundant)
@@ -133,7 +135,7 @@ void RewriteSystem::initialize(
133135
addPermanentRule(rule.first, rule.second);
134136

135137
for (const auto &rule : requirementRules)
136-
addRule(rule.first, rule.second);
138+
addExplicitRule(rule.first, rule.second);
137139
}
138140

139141
/// Reduce a term by applying all rewrite rules until fixed point.
@@ -412,6 +414,15 @@ bool RewriteSystem::addPermanentRule(MutableTerm lhs, MutableTerm rhs) {
412414
return added;
413415
}
414416

417+
/// Add a new rule, marking it explicit.
418+
bool RewriteSystem::addExplicitRule(MutableTerm lhs, MutableTerm rhs) {
419+
bool added = addRule(std::move(lhs), std::move(rhs));
420+
if (added)
421+
Rules.back().markExplicit();
422+
423+
return added;
424+
}
425+
415426
/// Delete any rules whose left hand sides can be reduced by other rules,
416427
/// and reduce the right hand sides of all remaining rules as much as
417428
/// possible.

lib/AST/RequirementMachine/RewriteSystem.h

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ class Rule final {
4242
Term LHS;
4343
Term RHS;
4444

45-
/// Associated type introduction rules are 'permanent', meaning they cannot
46-
/// be deleted by homotopy reduction. This is because they do not correspond
47-
/// to generic requirements and are re-added when the rewrite system is
48-
/// built, so by leaving them in place we can find other redundancies
49-
/// instead.
45+
/// A 'permanent' rule cannot be deleted by homotopy reduction. These
46+
/// do not correspond to generic requirements and are re-added when the
47+
/// rewrite system is built.
5048
unsigned Permanent : 1;
5149

50+
/// An 'explicit' rule is a generic requirement written by the user.
51+
unsigned Explicit : 1;
52+
5253
/// A 'simplified' rule was eliminated by simplifyRewriteSystem() if one of two
5354
/// things happen:
5455
/// - The rule's left hand side can be reduced via some other rule, in which
@@ -69,6 +70,7 @@ class Rule final {
6970
Rule(Term lhs, Term rhs)
7071
: LHS(lhs), RHS(rhs) {
7172
Permanent = false;
73+
Explicit = false;
7274
Simplified = false;
7375
Redundant = false;
7476
}
@@ -84,36 +86,40 @@ class Rule final {
8486

8587
bool isProtocolRefinementRule() const;
8688

87-
/// See above for an explanation.
89+
/// See above for an explanation of these predicates.
8890
bool isPermanent() const {
8991
return Permanent;
9092
}
9193

92-
/// See above for an explanation.
94+
bool isExplicit() const {
95+
return Explicit;
96+
}
97+
9398
bool isSimplified() const {
9499
return Simplified;
95100
}
96101

97-
/// See above for an explanation.
98102
bool isRedundant() const {
99103
return Redundant;
100104
}
101105

102-
/// Deletes the rule, which removes it from consideration in term
103-
/// simplification and completion. Deleted rules are simply marked as
104-
/// such instead of being physically removed from the rules vector
105-
/// in the rewrite system, to ensure that indices remain valid across
106-
/// deletion.
107106
void markSimplified() {
108107
assert(!Simplified);
109108
Simplified = true;
110109
}
111110

112111
void markPermanent() {
113-
assert(!Permanent);
112+
assert(!Explicit && !Permanent &&
113+
"Permanent and explicit are mutually exclusive");
114114
Permanent = true;
115115
}
116116

117+
void markExplicit() {
118+
assert(!Explicit && !Permanent &&
119+
"Permanent and explicit are mutually exclusive");
120+
Explicit = true;
121+
}
122+
117123
void markRedundant() {
118124
assert(!Redundant);
119125
Redundant = true;
@@ -255,6 +261,8 @@ class RewriteSystem final {
255261

256262
bool addPermanentRule(MutableTerm lhs, MutableTerm rhs);
257263

264+
bool addExplicitRule(MutableTerm lhs, MutableTerm rhs);
265+
258266
bool simplify(MutableTerm &term, RewritePath *path=nullptr) const;
259267

260268
void simplifySubstitutions(MutableTerm &term, RewritePath &path) const;

0 commit comments

Comments
 (0)