@@ -55,7 +55,7 @@ Next we'll go through each stage required to produce the output above.
55
55
56
56
[ chalk-tests ] : https://github.com/rust-lang-nursery/chalk/blob/4bce000801de31bf45c02f742a5fce335c9f035f/src/test.rs#L115
57
57
58
- ### Parsing
58
+ ### Parsing ( [ chalk_parse ] )
59
59
60
60
Chalk is designed to be incorporated with the Rust compiler, so the syntax and
61
61
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
71
71
transformation that a program goes through in order to become a format that
72
72
chalk can understand.
73
73
74
- ### Lowering
74
+ ### Rust Intermediate Representation ( [ rust_ir ] )
75
75
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:
79
91
80
92
* A [ clause] of the form ` consequence :- conditions ` where ` :- ` is read as
81
93
"if" and ` conditions = cond1 && cond2 && ... `
@@ -93,8 +105,8 @@ essentially one of the following:
93
105
94
106
* See also: [ Goals and Clauses] [ goals-and-clauses ] *
95
107
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:
98
110
99
111
``` rust,ignore
100
112
impl<T: Clone> Clone for Vec<T> {}
@@ -109,54 +121,61 @@ forall<T> { (Vec<T>: Clone) :- (T: Clone) }
109
121
This rule dictates that ` Vec<T>: Clone ` is only satisfied if ` T: Clone ` is also
110
122
satisfied (i.e. "provable").
111
123
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.
113
128
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
119
142
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.
121
145
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 ] *
126
147
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
129
149
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
134
151
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.
136
155
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
140
157
141
- The ` ir::ProgramEnvironment ` is created [ in this module ] [ rules-environment ] .
158
+ ### Solver ( [ chalk_solve ] )
142
159
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* .
144
163
145
- See [ The SLG Solver] [ slg ] .
164
+ * See also: [ The SLG Solver] [ slg ] *
146
165
147
166
## Crates
148
167
149
168
Chalk's functionality is broken up into the following crates:
150
169
- [ ** 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
152
171
types, lifetimes, and goals.
153
172
- [ ** chalk_solve** ] [ doc-chalk-solve ] : Combines ` chalk_ir ` and ` chalk_engine ` ,
154
173
effectively.
155
174
- [ ` 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.
157
176
- [ ** chalk** ] [ doc-chalk ] : Brings everything together. Defines the following
158
177
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
160
179
- ` rust_ir::lowering ` , which converts AST to ` rust_ir `
161
180
- ` rules ` , which implements logic rules converting ` rust_ir ` to ` chalk_ir `
162
181
- ` coherence ` , which implements coherence rules
@@ -167,11 +186,12 @@ Chalk's functionality is broken up into the following crates:
167
186
[ engine-context ] : https://rust-lang-nursery.github.io/chalk/doc/chalk_engine/context/index.html
168
187
169
188
[ 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
171
190
[ 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
173
192
[ 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
175
195
[ doc-chalki ] : https://rust-lang-nursery.github.io/chalk/doc/chalki/index.html
176
196
177
197
## Testing
@@ -201,15 +221,14 @@ is the function that is ultimately called.
201
221
[ chalk ] : https://github.com/rust-lang-nursery/chalk
202
222
[ lowering-to-logic ] : ./lowering-to-logic.html
203
223
[ lowering-rules ] : ./lowering-rules.html
224
+ [ wf-checking ] : ./wf.html
204
225
[ ast ] : https://en.wikipedia.org/wiki/Abstract_syntax_tree
205
226
[ chalk-ast ] : https://github.com/rust-lang-nursery/chalk/blob/master/chalk-parse/src/ast.rs
206
227
[ universal quantification ] : https://en.wikipedia.org/wiki/Universal_quantification
207
228
[ 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
209
229
[ clause ] : https://github.com/rust-lang-nursery/chalk/blob/master/GLOSSARY.md#clause
210
230
[ 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
213
232
[ HIR ] : ../hir.html
214
233
[ binders-struct ] : https://github.com/rust-lang-nursery/chalk/blob/94a1941a021842a5fcb35cd043145c8faae59f08/src/ir.rs#L661
215
234
[ rules-environment ] : https://github.com/rust-lang-nursery/chalk/blob/94a1941a021842a5fcb35cd043145c8faae59f08/src/rules.rs#L9
0 commit comments