Skip to content

Commit b5024c3

Browse files
committed
Chalk Overview: Update old content
1 parent d2238c3 commit b5024c3

File tree

1 file changed

+59
-40
lines changed

1 file changed

+59
-40
lines changed

src/traits/chalk-overview.md

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Next we'll go through each stage required to produce the output above.
5555

5656
[chalk-tests]: https://github.com/rust-lang-nursery/chalk/blob/4bce000801de31bf45c02f742a5fce335c9f035f/src/test.rs#L115
5757

58-
### Parsing
58+
### Parsing ([chalk_parse])
5959

6060
Chalk is designed to be incorporated with the Rust compiler, so the syntax and
6161
concepts it deals with heavily borrow from Rust. It is convenient for the sake
@@ -71,11 +71,23 @@ impls, and struct definitions. Parsing is often the first "phase" of
7171
transformation that a program goes through in order to become a format that
7272
chalk can understand.
7373

74-
### Lowering
74+
### Rust Intermediate Representation ([rust_ir])
7575

76-
After parsing, there is a "lowering" phase. This aims to convert traits/impls
77-
into "program clauses". A [`ProgramClause` (source code)][programclause] is
78-
essentially one of the following:
76+
After getting the AST we convert it to a more convenient intermediate
77+
representation called [`rust_ir`][rust_ir]. This is sort of analogous to the
78+
[HIR] in Rust. The process of converting to IR is called *lowering*.
79+
80+
The [`rust_ir::Program`][rust_ir-program] struct contains some "rust things"
81+
but indexed and accessible in a different way. For example, if you have a
82+
type like `Foo<Bar>`, we would represent `Foo` as a string in the AST but in
83+
`rust_ir::Program`, we use numeric indices (`ItemId`).
84+
85+
The [IR source code][ir-code] contains the complete definition.
86+
87+
### Chalk Intermediate Representation ([chalk_ir])
88+
89+
Once we have Rust IR it is time to convert it to "program clauses". A
90+
[`ProgramClause`] is essentially one of the following:
7991

8092
* A [clause] of the form `consequence :- conditions` where `:-` is read as
8193
"if" and `conditions = cond1 && cond2 && ...`
@@ -93,8 +105,8 @@ essentially one of the following:
93105

94106
*See also: [Goals and Clauses][goals-and-clauses]*
95107

96-
Lowering is the phase where we encode the rules of the trait system into logic.
97-
For example, if we have the following Rust:
108+
This is where we encode the rules of the trait system into logic. For
109+
example, if we have the following Rust:
98110

99111
```rust,ignore
100112
impl<T: Clone> Clone for Vec<T> {}
@@ -109,54 +121,61 @@ forall<T> { (Vec<T>: Clone) :- (T: Clone) }
109121
This rule dictates that `Vec<T>: Clone` is only satisfied if `T: Clone` is also
110122
satisfied (i.e. "provable").
111123

112-
#### Well-formedness checks
124+
Similar to [`rust_ir::Program`][rust_ir-program] which has "rust-like
125+
things", chalk_ir defines [`ProgramEnvironment`] which which is "pure logic".
126+
The main field in that struct is `program_clauses`, which contains the
127+
[`ProgramClause`]s generated by the rules module.
113128

114-
As part of lowering from the AST to the internal IR, we also do some "well
115-
formedness" checks. See the [source code][well-formedness-checks] for where
116-
those are done. The call to `record_specialization_priorities` checks
117-
"coherence" which means that it ensures that two impls of the same trait for the
118-
same type cannot exist.
129+
#### Rules
130+
131+
The `rules` module ([source code][rules-src]) defines the logic rules we use
132+
for each item in the Rust IR. It works by iterating over every trait, impl,
133+
etc. and emitting the rules that come from each one.
134+
135+
*See also: [Lowering Rules][lowering-rules]*
136+
137+
[`ProgramEnvironment`]: https://rust-lang-nursery.github.io/chalk/doc/chalk_ir/struct.ProgramEnvironment.html
138+
[`ProgramClause`]: https://rust-lang-nursery.github.io/chalk/doc/chalk_ir/enum.ProgramClause.html
139+
[rules-src]: https://github.com/rust-lang-nursery/chalk/blob/4bce000801de31bf45c02f742a5fce335c9f035f/src/rules.rs
140+
141+
#### Well-formedness checks
119142

120-
### Intermediate Representation (IR)
143+
As part of lowering to logic, we also do some "well formedness" checks. See
144+
the [`rules::wf` source code][rules-wf-src] for where those are done.
121145

122-
The second intermediate representation in chalk is called, well, the "ir". :)
123-
The [IR source code][ir-code] contains the complete definition. The
124-
`ir::Program` struct contains some "rust things" but indexed and accessible in
125-
a different way. This is sort of analogous to the [HIR] in Rust.
146+
*See also: [Well-formedness checking][wf-checking]*
126147

127-
For example, if you have a type like `Foo<Bar>`, we would represent `Foo` as a
128-
string in the AST but in `ir::Program`, we use numeric indices (`ItemId`).
148+
[rules-wf-src]: https://github.com/rust-lang-nursery/chalk/blob/4bce000801de31bf45c02f742a5fce335c9f035f/src/rules/wf.rs
129149

130-
In addition to `ir::Program` which has "rust-like things", there is also
131-
`ir::ProgramEnvironment` which is "pure logic". The main field in that struct is
132-
`program_clauses` which contains the `ProgramClause`s that we generated
133-
previously.
150+
#### Coherence
134151

135-
### Rules
152+
The function `record_specialization_priorities` in the `coherence` module
153+
([source code][coherence-src]) checks "coherence", which means that it
154+
ensures that two impls of the same trait for the same type cannot exist.
136155

137-
The `rules` module works by iterating over every trait, impl, etc. and emitting
138-
the rules that come from each one. See [Lowering Rules][lowering-rules] for the
139-
most up-to-date reference on that.
156+
[coherence-src]: https://github.com/rust-lang-nursery/chalk/blob/master/src/coherence.rs
140157

141-
The `ir::ProgramEnvironment` is created [in this module][rules-environment].
158+
### Solver ([chalk_solve])
142159

143-
### Solver
160+
Finally, when we've collected all the program clauses we care about, we want
161+
to perform queries on it. The component that finds the answer to these
162+
queries is called the *solver*.
144163

145-
See [The SLG Solver][slg].
164+
*See also: [The SLG Solver][slg]*
146165

147166
## Crates
148167

149168
Chalk's functionality is broken up into the following crates:
150169
- [**chalk_engine**][doc-chalk-engine]: Defines the core [SLG solver][slg].
151-
- [**chalk_ir**][doc-chalk-ir]: Defines chalk's internal representation of
170+
- [**chalk_ir**][chalk_ir]: Defines chalk's internal representation of
152171
types, lifetimes, and goals.
153172
- [**chalk_solve**][doc-chalk-solve]: Combines `chalk_ir` and `chalk_engine`,
154173
effectively.
155174
- [`chalk_engine::context`][engine-context] provides the necessary hooks.
156-
- [**chalk_parse**][doc-chalk-parse]: Defines the raw AST and a parser.
175+
- [**chalk_parse**][chalk_parse]: Defines the raw AST and a parser.
157176
- [**chalk**][doc-chalk]: Brings everything together. Defines the following
158177
modules:
159-
- [`rust_ir`][doc-chalk-rust-ir], containing the "HIR-like" form of the AST
178+
- [`rust_ir`][rust_ir], containing the "HIR-like" form of the AST
160179
- `rust_ir::lowering`, which converts AST to `rust_ir`
161180
- `rules`, which implements logic rules converting `rust_ir` to `chalk_ir`
162181
- `coherence`, which implements coherence rules
@@ -167,11 +186,12 @@ Chalk's functionality is broken up into the following crates:
167186
[engine-context]: https://rust-lang-nursery.github.io/chalk/doc/chalk_engine/context/index.html
168187

169188
[doc-chalk-engine]: https://rust-lang-nursery.github.io/chalk/doc/chalk_engine/index.html
170-
[doc-chalk-ir]: https://rust-lang-nursery.github.io/chalk/doc/chalk_ir/index.html
189+
[chalk_ir]: https://rust-lang-nursery.github.io/chalk/doc/chalk_ir/index.html
171190
[doc-chalk-solve]: https://rust-lang-nursery.github.io/chalk/doc/chalk_solve/index.html
172-
[doc-chalk-parse]: https://rust-lang-nursery.github.io/chalk/doc/chalk_parse/index.html
191+
[chalk_parse]: https://rust-lang-nursery.github.io/chalk/doc/chalk_parse/index.html
173192
[doc-chalk]: https://rust-lang-nursery.github.io/chalk/doc/chalk/index.html
174-
[doc-chalk-rust-ir]: https://rust-lang-nursery.github.io/chalk/doc/chalk/rules/index.html
193+
[rust_ir]: https://rust-lang-nursery.github.io/chalk/doc/chalk/rust_ir/index.html
194+
[rust_ir-program]: https://rust-lang-nursery.github.io/chalk/doc/chalk/rust_ir/struct.Program.html
175195
[doc-chalki]: https://rust-lang-nursery.github.io/chalk/doc/chalki/index.html
176196

177197
## Testing
@@ -201,15 +221,14 @@ is the function that is ultimately called.
201221
[chalk]: https://github.com/rust-lang-nursery/chalk
202222
[lowering-to-logic]: ./lowering-to-logic.html
203223
[lowering-rules]: ./lowering-rules.html
224+
[wf-checking]: ./wf.html
204225
[ast]: https://en.wikipedia.org/wiki/Abstract_syntax_tree
205226
[chalk-ast]: https://github.com/rust-lang-nursery/chalk/blob/master/chalk-parse/src/ast.rs
206227
[universal quantification]: https://en.wikipedia.org/wiki/Universal_quantification
207228
[lowering-forall]: ./lowering-to-logic.html#type-checking-generic-functions-beyond-horn-clauses
208-
[programclause]: https://github.com/rust-lang-nursery/chalk/blob/94a1941a021842a5fcb35cd043145c8faae59f08/src/ir.rs#L721
209229
[clause]: https://github.com/rust-lang-nursery/chalk/blob/master/GLOSSARY.md#clause
210230
[goals-and-clauses]: ./goals-and-clauses.html
211-
[well-formedness-checks]: https://github.com/rust-lang-nursery/chalk/blob/94a1941a021842a5fcb35cd043145c8faae59f08/src/ir/lowering.rs#L230-L232
212-
[ir-code]: https://github.com/rust-lang-nursery/chalk/tree/master/chalk-ir
231+
[ir-code]: https://github.com/rust-lang-nursery/chalk/blob/master/src/rust_ir.rs
213232
[HIR]: ../hir.html
214233
[binders-struct]: https://github.com/rust-lang-nursery/chalk/blob/94a1941a021842a5fcb35cd043145c8faae59f08/src/ir.rs#L661
215234
[rules-environment]: https://github.com/rust-lang-nursery/chalk/blob/94a1941a021842a5fcb35cd043145c8faae59f08/src/rules.rs#L9

0 commit comments

Comments
 (0)