Skip to content

Commit 3232de6

Browse files
committed
comments + pending goals in env
1 parent 31572f9 commit 3232de6

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,27 @@ use formality_types::{
55
rust::Visit,
66
};
77

8+
/// Captures the conditions under which something is true.
9+
///
10+
/// Examples:
11+
///
12+
/// * The `substitution` indicates equality constraints for existential (inference) variables.
13+
/// E.g. if you try to prove `Vec<?A> = Vec<u32>`, the result will be "True; [?A => u32]".
14+
/// This substitution is then to be applied to any future goals/terms that may reference `?A`.
815
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
916
pub struct Constraints {
17+
/// The environment in which future goals should be proven.
18+
/// This is an extension of the original environment; it may contain
19+
/// new variables and new pending proof obligations.
1020
pub env: Env,
21+
22+
/// If this is false, it indicates that the result is actually *ambiguous* and not known to be true.
23+
/// This occurs when e.g. we hit overflow or some other condition in which we can neither prove the
24+
/// result true nor false.
1125
pub known_true: bool,
26+
27+
/// Maps an existential variable to the value which it must be equal to.
28+
/// For example `Vec<?A> = Vec<u32>` would result in a substitution `[?A => u32]`.`
1229
pub substitution: Substitution,
1330
}
1431

@@ -31,9 +48,10 @@ impl Constraints {
3148
}
3249

3350
pub fn unconditionally_true(&self) -> bool {
34-
self.known_true && self.substitution.is_empty()
51+
self.known_true && self.substitution.is_empty() && self.env.pending().is_empty()
3552
}
3653

54+
/// Construct a set of constraints from a set of substitutions and the previous environment.
3755
pub fn from(
3856
env: impl Upcast<Env>,
3957
iter: impl IntoIterator<Item = (impl Upcast<Variable>, impl Upcast<Parameter>)>,

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use formality_macros::term;
33
use formality_types::{
44
grammar::{
55
Binder, ExistentialVar, ParameterKind, UniversalVar, VarIndex, VarSubstitution, Variable,
6+
Wc,
67
},
78
rust::{Fold, Visit},
89
};
@@ -39,15 +40,31 @@ pub enum Bias {
3940

4041
#[derive(Default, Debug, Clone, Hash, Ord, Eq, PartialEq, PartialOrd)]
4142
pub struct Env {
43+
/// The set of variables that are in scope (universal and existential).
44+
/// All terms should only reference free variables found in this set.
4245
variables: Vec<Variable>,
46+
47+
/// XXX this needs to be explained
4348
bias: Bias,
49+
50+
/// Pending goals that have not yet been proven.
51+
/// When we prove a conjunction `(A && B)`, proving `A` may result in
52+
/// some pending items (e.g., subtype constraints).
53+
///
54+
/// These are then added into the env when we prove `B` so that if `B`
55+
/// winds up constraining an inference varibale, it can also try to prove
56+
/// the pending constraints that are now unlocked.
57+
///
58+
/// Whenever a "successful" proof results, the pending obligations
59+
pending: Vec<Wc>,
4460
}
4561

4662
impl Env {
4763
pub fn new_with_bias(bias: Bias) -> Self {
4864
Env {
4965
variables: Default::default(),
5066
bias,
67+
pending: vec![],
5168
}
5269
}
5370

@@ -132,6 +149,11 @@ impl Env {
132149
}
133150
}
134151

152+
/// Pending goals that must still be proven
153+
pub fn pending(&self) -> &[Wc] {
154+
&self.pending
155+
}
156+
135157
/// An env A *encloses* a term `v` if all free variables in `v`
136158
/// are defined in A.
137159
pub fn encloses(&self, v: impl Visit) -> bool {
@@ -246,6 +268,7 @@ impl Env {
246268
.map(|&v| vs.map_var(v).unwrap_or(v))
247269
.collect(),
248270
bias: self.bias,
271+
pending: vs.apply(&self.pending),
249272
}
250273
}
251274

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use super::constraints::Constraints;
1919
use formality_types::grammar::Parameter::Ty;
2020

2121
judgment_fn! {
22+
/// The "heart" of the trait system -- prove that a where-clause holds given a set of declarations, variable environment, and set of assumptions.
23+
/// If successful, returns the constraints under which the where-clause holds.
2224
pub fn prove_wc(
2325
_decls: Decls,
2426
env: Env,

0 commit comments

Comments
 (0)