|
| 1 | +# Lowering rules |
| 2 | + |
| 3 | +This section gives the complete lowering rules for Rust traits into |
| 4 | +[program clauses][pc]. These rules reference the [domain goals][dg] defined in |
| 5 | +an earlier section. |
| 6 | + |
| 7 | +[pc]: ./traits-goals-and-clauses.html |
| 8 | +[dg]: ./traits-goals-and-clauses.html#domain-goals |
| 9 | + |
| 10 | +## Notation |
| 11 | + |
| 12 | +The nonterminal `Pi` is used to mean some generic *parameter*, either a |
| 13 | +named lifetime like `'a` or a type paramter like `A`. |
| 14 | + |
| 15 | +The nonterminal `Ai` is used to mean some generic *argument*, which |
| 16 | +might be a lifetime like `'a` or a type like `Vec<A>`. |
| 17 | + |
| 18 | +When defining the lowering rules, we will give goals and clauses in |
| 19 | +the [notation given in this section](./traits-goals-and-clauses.html). |
| 20 | +We sometimes insert "macros" like `LowerWhereClause!` into these |
| 21 | +definitions; these macros reference other sections within this chapter. |
| 22 | + |
| 23 | +## Lowering where clauses |
| 24 | + |
| 25 | +When used in a goal position, where clauses can be mapped directly |
| 26 | +[domain goals][dg], as follows: |
| 27 | + |
| 28 | +- `A0: Foo<A1..An>` maps to `Implemented(A0: Foo<A1..An>)`. |
| 29 | +- `A0: Foo<A1..An, Item = T>` maps to `ProjectionEq(<A0 as Foo<A1..An>>::Item = T)` |
| 30 | +- `T: 'r` maps to `Outlives(T, 'r)` |
| 31 | +- `'a: 'b` maps to `Outlives('a, 'b)` |
| 32 | + |
| 33 | +In the rules below, we will use `WC` to indicate where clauses that |
| 34 | +appear in Rust syntax; we will then use the same `WC` to indicate |
| 35 | +where those where clauses appear as goals in the program clauses that |
| 36 | +we are producing. In that case, the mapping above is used to convert |
| 37 | +from the Rust syntax into goals. |
| 38 | + |
| 39 | +### Transforming the lowered where clauses |
| 40 | + |
| 41 | +In addition, in the rules below, we sometimes do some transformations |
| 42 | +on the lowered where clauses, as defined here: |
| 43 | + |
| 44 | +- `FromEnv(WC)` -- this indicates that: |
| 45 | + - `Implemented(TraitRef)` becomes `FromEnv(TraitRef)` |
| 46 | + - `ProjectionEq(Projection = Ty)` becomes `FromEnv(Projection = Ty)` |
| 47 | + - other where-clauses are left intact |
| 48 | +- `WellFormed(WC)` -- this indicates that: |
| 49 | + - `Implemented(TraitRef)` becomes `WellFormed(TraitRef)` |
| 50 | + - `ProjectionEq(Projection = Ty)` becomes `WellFormed(Projection = Ty)` |
| 51 | + |
| 52 | +*TODO*: I suspect that we want to alter the outlives relations too, |
| 53 | +but Chalk isn't modeling those right now. |
| 54 | + |
| 55 | +## Lowering traits |
| 56 | + |
| 57 | +Given a trait definition |
| 58 | + |
| 59 | +```rust |
| 60 | +trait Trait<P1..Pn> // P0 == Self |
| 61 | +where WC |
| 62 | +{ |
| 63 | + // trait items |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +we will produce a number of declarations. This section is focused on |
| 68 | +the program clauses for the trait header (i.e., the stuff outside the |
| 69 | +`{}`); the [section on trait items](#trait-items) covers the stuff |
| 70 | +inside the `{}`. |
| 71 | + |
| 72 | +### Trait header |
| 73 | + |
| 74 | +From the trait itself we mostly make "meta" rules that setup the |
| 75 | +relationships between different kinds of domain goals. The first dush |
| 76 | +rule from the trait header creates the mapping between the `FromEnv` |
| 77 | +and `Implemented` predicates: |
| 78 | + |
| 79 | + forall<Self, P1..Pn> { |
| 80 | + Implemented(Self: Trait<P1..Pn>) :- FromEnv(Self: Trait<P1..Pn) |
| 81 | + } |
| 82 | + |
| 83 | +<a name="implied-bounds"> |
| 84 | + |
| 85 | +#### Implied bounds |
| 86 | + |
| 87 | +The next few clauses have to do with implied bounds (see also |
| 88 | +[RFC 2089]). For each trait, we produce two clauses: |
| 89 | + |
| 90 | +[RFC 2089]: https://rust-lang.github.io/rfcs/2089-implied-bounds.html |
| 91 | + |
| 92 | + // For each where clause WC: |
| 93 | + forall<Self, P1..Pn> { |
| 94 | + FromEnv(WC) :- FromEnv(Self: Trait<P1..Pn) |
| 95 | + } |
| 96 | + |
| 97 | +This clause says that if we are assuming that the trait holds, then we can also |
| 98 | +assume that it's where-clauses hold. It's perhaps useful to see an example: |
| 99 | + |
| 100 | +```rust |
| 101 | +trait Eq: PartialEq { ... } |
| 102 | +``` |
| 103 | + |
| 104 | +In this case, the `PartialEq` supertrait is equivalent to a `where |
| 105 | +Self: PartialEq` where clause, in our simplified model. The program |
| 106 | +clause above therefore states that if we can prove `FromEnv(T: Eq)` -- |
| 107 | +e.g., if we are in some function with `T: Eq` in its where clauses -- |
| 108 | +then we also know that `FromEnv(T: PartialEq)`. Thus the set of things |
| 109 | +that follow from the environment are not only the **direct where |
| 110 | +clauses** but also things that follow from them. |
| 111 | + |
| 112 | +The next rule is related; it defines what it means for a trait reference |
| 113 | +to be **well-formed**: |
| 114 | + |
| 115 | + // For each where clause WC: |
| 116 | + forall<Self, P1..Pn> { |
| 117 | + WellFormed(Self: Trait<P1..Pn>) :- Implemented(Self: Trait<P1..Pn>), WellFormed(WC) |
| 118 | + } |
| 119 | + |
| 120 | +This `WellFormed` rule states that `T: Trait` is well-formed if (a) |
| 121 | +`T: Trait` is implemented and (b) all the where-clauses declared on |
| 122 | +`Trait` are well-formed (and hence they are implemented). Remember |
| 123 | +that the `WellFormed` predicate is |
| 124 | +[coinductive](./traits-goals-and-clauses.html#coinductive); in this |
| 125 | +case, it is serving as a kind of "carrier" that allows us to enumerate |
| 126 | +all the where clauses that are transitively implied by `T: Trait`. |
| 127 | + |
| 128 | +An example: |
| 129 | + |
| 130 | +```rust |
| 131 | +trait Foo: A + Bar { } |
| 132 | +trait Bar: B + Foo { } |
| 133 | +trait A { } |
| 134 | +trait B { } |
| 135 | +``` |
| 136 | + |
| 137 | +Here, the transitive set of implications for `T: Foo` are `T: A`, `T: Bar`, and `T: B`. |
| 138 | +And indeed if we were to try to prove `WellFormed(T: Foo)`, we would have to prove each |
| 139 | +one of those: |
| 140 | + |
| 141 | +- `WellFormed(T: Foo)` |
| 142 | + - `Implemented(T: Foo)` |
| 143 | + - `WellFormed(T: A)` |
| 144 | + - `Implemented(T: A)` |
| 145 | + - `WellFormed(T: Bar)` |
| 146 | + - `Implemented(T: Bar)` |
| 147 | + - `WellFormed(T: B)` |
| 148 | + - `Implemented(T: Bar)` |
| 149 | + - `WellFormed(T: Foo)` -- cycle, true coinductively |
| 150 | + |
| 151 | +This `WellFormed` predicate is only used when proving that impls are |
| 152 | +well-formed -- basically, for each impl of some trait ref `TraitRef`, |
| 153 | +we must that `WellFormed(TraitRef)`. This in turn justifies the |
| 154 | +implied bounds rules that allow us to extend the set of `FromEnv` |
| 155 | +items. |
| 156 | + |
| 157 | +<a name=trait-items> |
| 158 | + |
| 159 | +## Lowering trait items |
| 160 | + |
| 161 | +### Associated type declarations |
| 162 | + |
| 163 | +Given a trait that declares a (possibly generic) associated type: |
| 164 | + |
| 165 | +```rust |
| 166 | +trait Trait<P1..Pn> // P0 == Self |
| 167 | +where WC |
| 168 | +{ |
| 169 | + type AssocType<Pn+1..Pm>: Bounds where WC1; |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +We will produce a number of program clases. The first two define |
| 174 | +the rules by which `ProjectionEq` can succeed; these two clauses are discussed |
| 175 | +in detail in the [section on associated types](./traits-associated-types.html). |
| 176 | + |
| 177 | + // ProjectionEq can succeed by normalizing: |
| 178 | + forall<Self, P1..Pn, Pn+1..Pm, U> { |
| 179 | + ProjectionEq(<Self as Trait<P1..Pn>>::AssocType<Pn+1..Pm> = U) :- |
| 180 | + Normalize(<Self as Trait<P1..Pn>>::AssocType<Pn+1..Pm> -> U) |
| 181 | + } |
| 182 | + |
| 183 | + // ProjectionEq can succeed by skolemizing: |
| 184 | + forall<Self, P1..Pn, Pn+1..Pm> { |
| 185 | + ProjectionEq( |
| 186 | + <Self as Trait<P1..Pn>>::AssocType<Pn+1..Pm> = |
| 187 | + (Trait::AssocType)<Self, P1..Pn, Pn+1..Pm> |
| 188 | + ) :- |
| 189 | + // But only if the trait is implemented, and the conditions from |
| 190 | + // the associated type are met as well: |
| 191 | + Implemented(Self: Trait<P1..Pn>), |
| 192 | + WC1 |
| 193 | + } |
| 194 | + |
| 195 | +The next rule covers implied bounds for the projection. In particular, |
| 196 | +the `Bounds` declared on the associated type must be proven to hold to |
| 197 | +show that the impl is well-formed, and hence we can rely on them from |
| 198 | +elsewhere. |
| 199 | + |
| 200 | + // XXX how exactly should we set this up? Have to be careful; |
| 201 | + // presumably this has to be a kind of `FromEnv` setup. |
| 202 | + |
| 203 | +### Lowering function and constant declarations |
| 204 | + |
| 205 | +Chalk didn't model functions and constants, but I would eventually |
| 206 | +like to treat them exactly like normalization. See [the section on function/constant |
| 207 | +values below](#constant-vals) for more details. |
| 208 | + |
| 209 | +## Lowering impls |
| 210 | + |
| 211 | +Given an impl of a trait: |
| 212 | + |
| 213 | +```rust |
| 214 | +impl<P0..Pn> Trait<A1..An> for A0 |
| 215 | +where WC |
| 216 | +{ |
| 217 | + // zero or more impl items |
| 218 | +} |
| 219 | +``` |
| 220 | + |
| 221 | +Let `TraitRef` be the trait reference `A0: Trait<A1..An>`. Then we |
| 222 | +will create the following rules: |
| 223 | + |
| 224 | + forall<P0..Pn> { |
| 225 | + Implemented(TraitRef) :- WC |
| 226 | + } |
| 227 | + |
| 228 | +In addition, we will lower all of the *impl items*. |
| 229 | + |
| 230 | +## Lowering impl items |
| 231 | + |
| 232 | +### Associated type values |
| 233 | + |
| 234 | +Given an impl that contains: |
| 235 | + |
| 236 | +```rust |
| 237 | +impl<P0..Pn> Trait<A1..An> for A0 |
| 238 | +where WC |
| 239 | +{ |
| 240 | + type AssocType<Pn+1..Pm>: Bounds where WC1 = T; |
| 241 | +} |
| 242 | +``` |
| 243 | + |
| 244 | +We produce the following rule: |
| 245 | + |
| 246 | + forall<P0..Pm> { |
| 247 | + forall<Pn+1..Pm> { |
| 248 | + Normalize(<A0 as Trait<A1..An>>::AssocType<Pn+1..Pm> -> T) :- |
| 249 | + WC, WC1 |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | +Note that `WC` and `WC1` both encode where-clauses that the impl can |
| 254 | +rely on, whereas the bounds `Bounds` on the associated type are things |
| 255 | +that the impl must prove (see the well-formedness checking). |
| 256 | + |
| 257 | +<a name=constant-vals> |
| 258 | + |
| 259 | +### Function and constant values |
| 260 | + |
| 261 | +Chalk didn't model functions and constants, but I would eventually |
| 262 | +like to treat them exactly like normalization. This presumably |
| 263 | +involves adding a new kind of parameter (constant), and then having a |
| 264 | +`NormalizeValue` domain goal. This is *to be written* because the |
| 265 | +details are a bit up in the air. |
| 266 | + |
| 267 | + |
0 commit comments