Skip to content

Commit 9fc991d

Browse files
authored
Merge pull request rust-lang#131 from sunjay/overview-of-chalk
Added An Overview of Chalk
2 parents b6f63df + 389bd07 commit 9fc991d

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
- [The lowering module in rustc](./traits-lowering-module.md)
3939
- [Well-formedness checking](./traits-wf.md)
4040
- [The SLG solver](./traits-slg.md)
41+
- [An Overview of Chalk](./chalk-overview.md)
4142
- [Bibliography](./traits-bibliography.md)
4243
- [Type checking](./type-checking.md)
4344
- [Method Lookup](./method-lookup.md)

src/chalk-overview.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# An Overview of Chalk
2+
3+
> Chalk is under heavy development, so if any of these links are broken or if
4+
> any of the information is inconsistent with the code or outdated, please
5+
> [open an issue][rustc-issues] so we can fix it. If you are able to fix the
6+
> issue yourself, we would love your contribution!
7+
8+
[Chalk][chalk] recasts Rust's trait system explicitly in terms of logic
9+
programming by "lowering" Rust code into a kind of logic program we can then
10+
execute queries against. (See [*Lowering to Logic*][lowering-to-logic] and
11+
[*Lowering Rules*][lowering-rules]) Its goal is to be an executable, highly
12+
readable specification of the Rust trait system.
13+
14+
There are many expected benefits from this work. It will consolidate our
15+
existing, somewhat ad-hoc implementation into something far more principled and
16+
expressive, which should behave better in corner cases, and be much easier to
17+
extend.
18+
19+
## Resources
20+
21+
* [Chalk Source Code](https://github.com/rust-lang-nursery/chalk)
22+
* [Chalk Glossary](https://github.com/rust-lang-nursery/chalk/blob/master/GLOSSARY.md)
23+
* The traits section of the rustc guide (you are here)
24+
25+
### Blog Posts
26+
27+
* [Lowering Rust traits to logic](http://smallcultfollowing.com/babysteps/blog/2017/01/26/lowering-rust-traits-to-logic/)
28+
* [Unification in Chalk, part 1](http://smallcultfollowing.com/babysteps/blog/2017/03/25/unification-in-chalk-part-1/)
29+
* [Unification in Chalk, part 2](http://smallcultfollowing.com/babysteps/blog/2017/04/23/unification-in-chalk-part-2/)
30+
* [Negative reasoning in Chalk](http://aturon.github.io/blog/2017/04/24/negative-chalk/)
31+
* [Query structure in chalk](http://smallcultfollowing.com/babysteps/blog/2017/05/25/query-structure-in-chalk/)
32+
* [Cyclic queries in chalk](http://smallcultfollowing.com/babysteps/blog/2017/09/12/tabling-handling-cyclic-queries-in-chalk/)
33+
* [An on-demand SLG solver for chalk](http://smallcultfollowing.com/babysteps/blog/2018/01/31/an-on-demand-slg-solver-for-chalk/)
34+
35+
## Parsing
36+
37+
Chalk is designed to be incorporated with the Rust compiler, so the syntax and
38+
concepts it deals with heavily borrow from Rust. It is convenient for the sake
39+
of testing to be able to run chalk on its own, so chalk includes a parser for a
40+
Rust-like syntax. This syntax is orthogonal to the Rust AST and grammar. It is
41+
not intended to look exactly like it or support the exact same syntax.
42+
43+
The parser takes that syntax and produces an [Abstract Syntax Tree (AST)][ast].
44+
You can find the [complete definition of the AST][chalk-ast] in the source code.
45+
46+
The syntax contains things from Rust that we know and love, for example: traits,
47+
impls, and struct definitions. Parsing is often the first "phase" of
48+
transformation that a program goes through in order to become a format that
49+
chalk can understand.
50+
51+
## Lowering
52+
53+
After parsing, there is a "lowering" phase. This aims to convert traits/impls
54+
into "program clauses". A [`ProgramClause` (source code)][programclause] is
55+
essentially one of the following:
56+
57+
* A [clause] of the form `consequence :- conditions` where `:-` is read as
58+
"if" and `conditions = cond1 && cond2 && ...`
59+
* A universally quantified clause of the form
60+
`forall<T> { consequence :- conditions }`
61+
* `forall<T> { ... }` is used to represent [universal quantification]. See the
62+
section on [Lowering to logic][lowering-forall] for more information.
63+
* A key thing to note about `forall` is that we don't allow you to "quantify"
64+
over traits, only types and regions (lifetimes). That is, you can't make a
65+
rule like `forall<Trait> { u32: Trait }` which would say "`u32` implements
66+
all traits". You can however say `forall<T> { T: Trait }` meaning "`Trait`
67+
is implemented by all types".
68+
* `forall<T> { ... }` is represented in the code using the [`Binders<T>`
69+
struct][binders-struct].
70+
71+
*See also: [Goals and Clauses][goals-and-clauses]*
72+
73+
Lowering is the phase where we encode the rules of the trait system into logic.
74+
For example, if we have the following Rust:
75+
76+
```rust,ignore
77+
impl<T: Clone> Clone for Vec<T> {}
78+
```
79+
80+
We generate the following program clause:
81+
82+
```rust,ignore
83+
forall<T> { (Vec<T>: Clone) :- (T: Clone) }
84+
```
85+
86+
This rule dictates that `Vec<T>: Clone` is only satisfied if `T: Clone` is also
87+
satisfied (i.e. "provable").
88+
89+
### Well-formedness checks
90+
91+
As part of lowering from the AST to the internal IR, we also do some "well
92+
formedness" checks. See the [source code][well-formedness-checks] for where
93+
those are done. The call to `record_specialization_priorities` checks
94+
"coherence" which means that it ensures that two impls of the same trait for the
95+
same type cannot exist.
96+
97+
## Intermediate Representation (IR)
98+
99+
The second intermediate representation in chalk is called, well, the "ir". :)
100+
The [IR source code][ir-code] contains the complete definition. The
101+
`ir::Program` struct contains some "rust things" but indexed and accessible in
102+
a different way. This is sort of analogous to the [HIR] in Rust.
103+
104+
For example, if you have a type like `Foo<Bar>`, we would represent `Foo` as a
105+
string in the AST but in `ir::Program`, we use numeric indices (`ItemId`).
106+
107+
In addition to `ir::Program` which has "rust-like things", there is also
108+
`ir::ProgramEnvironment` which is "pure logic". The main field in that struct is
109+
`program_clauses` which contains the `ProgramClause`s that we generated
110+
previously.
111+
112+
## Rules
113+
114+
The `rules` module works by iterating over every trait, impl, etc. and emitting
115+
the rules that come from each one. See [Lowering Rules][lowering-rules] for the
116+
most up-to-date reference on that.
117+
118+
The `ir::ProgramEnvironment` is created [in this module][rules-environment].
119+
120+
## Testing
121+
122+
TODO: Basically, [there is a macro](https://github.com/rust-lang-nursery/chalk/blob/94a1941a021842a5fcb35cd043145c8faae59f08/src/solve/test.rs#L112-L148)
123+
that will take chalk's Rust-like syntax and run it through the full pipeline
124+
described above.
125+
[This](https://github.com/rust-lang-nursery/chalk/blob/94a1941a021842a5fcb35cd043145c8faae59f08/src/solve/test.rs#L83-L110)
126+
is the function that is ultimately called.
127+
128+
## Solver
129+
130+
See [The SLG Solver][slg].
131+
132+
[rustc-issues]: https://github.com/rust-lang-nursery/rustc-guide/issues
133+
[chalk]: https://github.com/rust-lang-nursery/chalk
134+
[lowering-to-logic]: traits-lowering-to-logic.html
135+
[lowering-rules]: traits-lowering-rules.html
136+
[ast]: https://en.wikipedia.org/wiki/Abstract_syntax_tree
137+
[chalk-ast]: https://github.com/rust-lang-nursery/chalk/blob/master/chalk-parse/src/ast.rs
138+
[universal quantification]: https://en.wikipedia.org/wiki/Universal_quantification
139+
[lowering-forall]: https://rust-lang-nursery.github.io/rustc-guide/traits-lowering-to-logic.html#type-checking-generic-functions-beyond-horn-clauses
140+
[programclause]: https://github.com/rust-lang-nursery/chalk/blob/94a1941a021842a5fcb35cd043145c8faae59f08/src/ir.rs#L721
141+
[clause]: https://github.com/rust-lang-nursery/chalk/blob/master/GLOSSARY.md#clause
142+
[goals-and-clauses]: traits-goals-and-clauses.html
143+
[well-formedness-checks]: https://github.com/rust-lang-nursery/chalk/blob/94a1941a021842a5fcb35cd043145c8faae59f08/src/ir/lowering.rs#L230-L232
144+
[ir-code]: https://github.com/rust-lang-nursery/chalk/blob/master/src/ir.rs
145+
[HIR]: hir.html
146+
[binders-struct]: https://github.com/rust-lang-nursery/chalk/blob/94a1941a021842a5fcb35cd043145c8faae59f08/src/ir.rs#L661
147+
[rules-environment]: https://github.com/rust-lang-nursery/chalk/blob/94a1941a021842a5fcb35cd043145c8faae59f08/src/rules.rs#L9
148+
[slg]: traits-slg.html

src/traits-slg.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
# The SLG solver
2+
3+
TODO: <https://github.com/rust-lang-nursery/chalk/blob/master/chalk-engine/src/README.md>

0 commit comments

Comments
 (0)