Skip to content

Commit 493978b

Browse files
committed
Move docs/crates.md to book
1 parent 4b5a4cc commit 493978b

File tree

4 files changed

+116
-141
lines changed

4 files changed

+116
-141
lines changed

book/src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- [Rust lifetimes](./types/rust_lifetimes.md)
1313
- [Operations](./types/operations.md)
1414
- [Fold and the Folder trait](./types/operations/fold.md)
15-
- [Representing traits, impls, and other parts of Rust programs](./rust_ir.md)
15+
- [Intermediate representations](./rust_ir.md)
1616
- [Lowering Rust IR to logic](./clauses.md)
1717
- [Goals and clauses](./clauses/goals_and_clauses.md)
1818
- [Type equality and unification](./clauses/type_equality.md)

book/src/rust_ir.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
# Representing traits, impls, and other parts of Rust programs
1+
# Intermediate representations
2+
3+
Intermediate representations (IR) are used to represent parts of Rust programs such as traits and impls.
4+
5+
Chalk contains three levels of IR:
6+
7+
- The **AST**. This is used purely for writing test cases
8+
with a Rust-like syntax. This is consumed by **lowering** code, which
9+
takes AST and products **Rust IR** (the next bullet point).
10+
- The **Rust IR**. This is a "HIR-like" notation that defines the
11+
interesting properties of things like traits, impls, and structs.
12+
It is an input to the **rules** code, which produces
13+
- The **Chalk IR**. This is most "Prolog-like" of the various IRs. It
14+
contains the definition of **types** as well as prolog-like concepts
15+
such as goals (things that must be proven true) and clauses (things
16+
that are assumed to be true).

book/src/what_is_chalk/crates.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ other programs:
1717

1818
The following crate is an implementation detail, used internally by `chalk-solve`:
1919

20-
* The `chalk-engine` crate, which defines the actual engine that solves logical predicate. This
20+
* The `chalk-engine` crate, which defines the actual engine that solves logical predicate. This
2121
engine is quite general and not really specific to Rust.
2222
* The `chalk-derive` crate defines custom derives for the `chalk_ir::fold::Fold` trait and other
2323
such things.
@@ -35,3 +35,101 @@ define a kind of "minimal embedding" of chalk.
3535
`chalk-rust-ir` by a process called "lowering'.
3636
* Finally, the main `chalk` crate, along with the testing crate in the
3737
`tests` directory, define the actual entry points.
38+
39+
## The chalk-solve crate
40+
41+
| The `chalk-solve` crate | |
42+
|---|--- |
43+
| Purpose: | to solve a given goal |
44+
| Depends on IR: | chalk-ir but not rust-ir |
45+
| Context required: | `ChalkSolveDatabase` |
46+
47+
The `chalk-solve` crate exposes a key type called `Solver`. This is a
48+
solver that, given a goal (expressed in chalk-ir) will solve the goal
49+
and yield up a `Solution`. The solver caches intermediate data between
50+
invocations, so solving the same goal twice in a row (or solving goals
51+
with common subgoals) is faster.
52+
53+
The solver is configured by a type that implements the
54+
`ChalkSolveDatabase` trait. This trait contains some callbacks that
55+
provide needed context for the solver -- notably, the solver can ask:
56+
57+
- **What are the program clauses that might solve given rule?** This
58+
is answered by the code in the chalk-rules crate.
59+
- **Is this trait coinductive?** This is answered by the rust-ir.
60+
61+
62+
## The chalk-engine crate
63+
64+
| The `chalk-engine` crate | |
65+
|---|--- |
66+
| Purpose: | define the base solving strategy |
67+
| IR: | none |
68+
| Context required: | `Context` trait |
69+
70+
For the purposes of chalk, the `chalk-engine` crate is effectively
71+
encapsulated by `chalk-solve`. It defines the base SLG engine. It is
72+
written in a very generic style that knows next to nothing about Rust
73+
itself. In particular, it does not depend on any of the Chalk IRs,
74+
which allows it to be used by rustc (which currently doesn't use
75+
chalk-ir). The engine can be configured via the traits defined in
76+
`chalk_engine::context::Context`, which contain (for example)
77+
associated types that define what a goal or clause is, as well as
78+
functions that operate on those things.
79+
80+
## The chalk-rules crate
81+
82+
| The `chalk-rules` crate | |
83+
|---|--- |
84+
| Purpose: | create chalk-ir goals/clauses given rust-ir |
85+
| Depends on IR: | chalk-ir and rust-ir |
86+
| Context required: | `Context` trait |
87+
88+
The `chalk-rules` defines code that "lowers" rust-ir into chalk-ir,
89+
producing both goals and clauses.
90+
91+
- For example, the `clauses` module defines a trait
92+
(`ToProgramClauses`) that is implemented for various bits of
93+
rust-ir. It might (for example) lower an impl into a set of program
94+
clauses.
95+
- The coherence rules are defined in the `coherence` module; these
96+
include code to check if an impl meets the orphan rules, and to
97+
check for overlap between impls.
98+
- These can also return information about the specialization tree
99+
for a given trait.
100+
- Finally, the well-formedness rules are defined in the `wf` module.
101+
102+
The chalk-rules crate defines a `ChalkRulesDatabase` trait that contains
103+
a number of callbacks that it needs. These callbacks are grouped into
104+
two sub-traits:
105+
106+
- The `GoalSolver` trait, which exposes a `solve` method for solving
107+
goals. This solving is ultimately done by the code in the
108+
`chalk-solve` crate.
109+
- The `RustIrDatabase` trait, which offers a number of accessors to
110+
fetch rust-ir. For example, the `trait_datum` method returns the
111+
`TraitDatum` for a given `TraitId`.
112+
- Note that -- by design -- this trait does not include any
113+
"open-ended" methods that ask queries like "return all the impls
114+
in the program" or "return all structs". These sorts of open-ended
115+
walks are expected to be performed at an outer level (in our case,
116+
in the chalk crate).
117+
118+
## The flow
119+
120+
This section tries to document how the flow of information proceeds in
121+
the main chalk testing harness. This can help give an idea how all the
122+
parts of the system interact.
123+
124+
- To begin with, the integration crate is asked to solve some goal
125+
(via `ChalkRulesDatabase::solve`, for example).
126+
- It will get access to its internal `Solver` (instantiating one, if
127+
one does not already exist) and invoke the `Solver::solve` method.
128+
- The solver may periodically request the set of applicable program clauses
129+
for the main goal or some subgoal.
130+
- The integration crate will examine the goal in question and use the code in the `chalk-rules`
131+
crate to instantiate program clauses.
132+
- This may, in the case of specialization, require recursively solving goals.
133+
- Once the program clauses are known, the solver can continue. It may
134+
periodically ask the integration crate whether a given bit of IR is
135+
coinductive.

doc/crates.md

Lines changed: 0 additions & 138 deletions
This file was deleted.

0 commit comments

Comments
 (0)