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