Skip to content

Commit 31b3aeb

Browse files
committed
cleaup, comment, slightly simplify
1 parent 0b8e9f8 commit 31b3aeb

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

crates/formality-prove/src/prove/prove_eq.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use formality_types::{
2-
cast::{Downcast, Downcasted, Upcast, Upcasted},
2+
cast::{Downcast, Upcast, Upcasted},
33
collections::{Deduplicate, Set},
44
grammar::{
55
AliasTy, AtomicRelation, InferenceVar, Parameter, PlaceholderVar, RigidTy, Substitution,
@@ -79,27 +79,20 @@ judgment_fn! {
7979
(
8080
(if let None = t.downcast::<Variable>())
8181
(equate_variable(program, env, assumptions, v, t) => (env, c))
82-
----------------------------- ("existential-l")
82+
----------------------------- ("existential-nonvar")
8383
(prove_ty_eq(program, env, assumptions, Variable::InferenceVar(v), t) => (env, c))
8484
)
8585

86-
(
87-
(if let None = t.downcast::<Variable>())
88-
(equate_variable(program, env, assumptions, v, t) => (env, c))
89-
----------------------------- ("existential-r")
90-
(prove_ty_eq(program, env, assumptions, t, Variable::InferenceVar(v)) => (env, c))
91-
)
92-
9386
(
9487
// Map the higher rank variable to the lower rank one.
9588
(let (a, b) = env.order_by_universe(l, r))
96-
----------------------------- ("existential-both")
89+
----------------------------- ("existential-existential")
9790
(prove_ty_eq(_program, env, _assumptions, Variable::InferenceVar(l), Variable::InferenceVar(r)) => (env, (b, a)))
9891
)
9992

10093
(
10194
(if env.universe(p) < env.universe(v))
102-
----------------------------- ("existential-vs-placeholder")
95+
----------------------------- ("existential-placeholder")
10396
(prove_ty_eq(_program, env, _assumptions, Variable::InferenceVar(v), Variable::PlaceholderVar(p)) => (env, (v, p)))
10497
)
10598

@@ -124,9 +117,14 @@ fn equate_variable(
124117
let span = tracing::debug_span!("equate_variable", ?x, ?p, ?env);
125118
let _guard = span.enter();
126119

127-
let fvs = p.free_variables().deduplicate();
120+
// Preconditions:
121+
// * Environment contains all free variables
122+
// * `p` is some compound type, not a variable
123+
// (variables are handled via special rules above)
124+
env.assert_encloses((x, (&assumptions, &p)));
125+
assert!(!p.is_a::<Variable>());
128126

129-
env.assert_encloses((x, &fvs));
127+
let fvs = p.free_variables().deduplicate();
130128

131129
// Ensure that `x` passes the occurs check for the free variables in `p`.
132130
if occurs_in(x, &fvs) {
@@ -154,14 +152,18 @@ fn equate_variable(
154152

155153
// Introduce the following constraints:
156154
//
157-
// * `fv = universe_subst(fv)` for each free variable `fv` in `p` (e.g., `Y => Z` in our example above)
155+
// * `fv = universe_subst(fv)` for each free existential variable `fv` in `p` (e.g., `Y => Z` in our example above)
158156
// * `x = universe_subst(p)` (e.g., `Vec<Z>` in our example above)
159157
let constraints: Constraints = universe_subst
160158
.iter()
161159
.filter(|(v, _)| v.is_a::<InferenceVar>())
162160
.chain(Some((x, universe_subst.apply(&p)).upcast()))
163161
.collect();
164162

163+
// For each placeholder variable that we replaced with an inference variable
164+
// above, we now have to prove that goal. e.g., if we had `X = Vec<!Y>`, we would replace `!Y` with `?Z`
165+
// (where `?Z` is in a lower universe than `X`), but now we must prove that `!Y = ?Z`
166+
// (this may be posible due to assumptions).
165167
let goals: Wcs = universe_subst
166168
.iter()
167169
.filter(|(v, _)| v.is_a::<PlaceholderVar>())

0 commit comments

Comments
 (0)