Skip to content

Commit e98a14f

Browse files
add documentation about mixed cycles
1 parent 897d0df commit e98a14f

File tree

5 files changed

+82
-46
lines changed

5 files changed

+82
-46
lines changed

book/src/glossary.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,15 @@ valuation `A = true, B = false` makes the premise true and the conclusion false.
229229
## Valuation
230230
A valuation is an assignment of values to all variables inside a logical
231231
formula.
232+
233+
## Fixed-Points
234+
A fixed-point of a function `f` is a value `x` for which `f(x)=x`.
235+
Similarly a pre-fixed-point is defined as `x ≤ f(x)`, whereas for a post-fixed-point it holds that `f(x) ≤ x`.
236+
237+
A least fixed-point (lfp) of `f` is the fixed-point `x` of `f` for which all other fixed-points `y` are greater or equal (i.e. if `f(y)=y` then `x ≤ y`).
238+
Similarly, a greatest fixed-point (gfp) is greater or equal than all other fixed-points.
239+
If `f` is a function on sets, the least fixed-point is defined as the intersection of all pre-fixed-points, which are then defined as sets `x` for which `x ⊆ f(x)`.
240+
The greatest fixed-point is in this case the union of all post-fixed-points, respectively.
241+
242+
This simple definition of lfp and gfp can also be lifted to general lattices.
243+
The results for Chalk goals form such a lattice and, thus, every solver for such goals tries to find such fixed-points.

book/src/recursive/coinduction.md

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,74 @@
22

33
This sub-chapter is meant to describe the current handling of coinductive goals in the recursive solver rather than providing an extensive overview over the theoretical backgrounds and ideas.
44
It follows the description in [this GitHub comment](https://github.com/rust-lang/chalk/issues/399#issuecomment-643420016) and the Zulip topic linked there.
5+
In general, coinductive cycles can arise for well-formedness checking and autotraits.
6+
Therefore, correctly handling coinductive cycles is necessary to model the Rust trait system in its entirety.
57

68
## General Idea
7-
The general idea for the handling of coinductive cycles in the recursive solver is to start by assuming the goal is provable and then try to find evidence that it is not.
8-
This search for a disproof is done by the standard recursive solving process described in the sub-chapters before.
9+
Coinductive cycles can be handled the same way as inductive cycles described [before](./inductive_cycles.md).
10+
The only difference is the start value for coinductive goals.
11+
Whereas inductive goals start with a negative result and are iterated until a least fixed-point is found, coinductive goals start with a positive result (i.e. a unique solution with identity substitution).
12+
This negative result is then iterated until a greatest fixed-point is reached.
913

10-
Albeit this approach would allow for the handling of mixed inductive/co-inductive cycles, these are actually handled as errors to prevent propagation of the assumed provability outside of the coinductive cycle.
11-
This propagation of the assumed solution might also happen in pure coinductive cycles and can potentially lead to invalid results.
14+
## Mixed co-inductive and inductive Cycles
15+
As described above, the handling of inductive and coindutive cycles differs only in the start value from which the computation begins.
16+
Thus, it might seem reasonable to have mixed inductive and coinductive cycles as all goals inside these cycles would be handled the same way anyway.
17+
Unfortunately, this is not possible for the kind of logic that Chalk is based on (i.e. essentially an extension of co-LP for Hereditary Harrop clauses, cf. [this paper][co-LP]).
18+
19+
There is fundamental difference between results for inductive cycles and results for coinductive cycles of goals.
20+
An inductive goal is provable if and only if there exists a proof for it consisting of a finite chain of derivations from axioms that are members of the least-fixed point of the underlying logic program.
21+
On the other hand, coinductive goals are provable if there exists an at most infinite derivation starting from the axioms that proves it (this includes in particular all finite derivations).
22+
This infinite derivation is then part of the greatest fixed-point of the logic program.
23+
As infinite derivations are not feasible to compute, it is enough to show that such a derivation contains no contradiction.
24+
25+
A simple example `X :- X.` (with `X` a free variable) is thus not provable by inductive reasoning (the least solution/lfp for this is the empty solution, a failure) but it is provable by coinductive reasoning (the greatest solution/gfp is the universe, i.e. all values).
26+
27+
This difference between inductive and coinductive results becomes a problem when combined in a single cycle.
28+
Consider a coinductive goal `CG` and an inductive goal `IG`. Now consider the simplest possible mixed cycle:
29+
```notrust
30+
CG :- IG
31+
IG :- CG
32+
```
33+
It is apparent, that there can not exist a solution for `IG` as the cyclic dependency prevents a finite proof derivation.
34+
In contrast to that, `CG` could potentially be provable as the derivation *`CG` if `IG` if `CG` if `IG` ...* is infinite and based only on the two axioms.
35+
As a result, `CG` would hold whereas `IG` would not hold, creating a contradiction.
36+
37+
The simplest solution to this problem, proposed by Simon et al. in [their paper about co-LP][co-LP], is to disallow mixed inductive and coinductive cycles.
38+
This approach is also used by Chalk.
1239

1340
## Prevention of Invalid Results
1441
The problem of invalid results propagated outside of the coinductive cycle is also described in the [Coinduction chapter](../engine/logic/coinduction.md) for the SLG solver alongside the rather complex handling used with it.
42+
Whereas the SLG solver introduces [special constructs](../engine/logic/coinduction.html#nikos-proposed-solution) to handle coinduction, it is sufficient for the recursive solver to use the same logic for inductive and coinductive cycles.
43+
The following is a description of how this works in more detail.
1544

1645
### The Problem
17-
The problem arises if a solution that is purely based on the positive starting value for the coinductive cycle is cached and as such propagated to other goals that are possibly reliant on this. An example may look like this (cf. the test case `coinduction::coinductive_unsound`):
46+
The problem arises if a solution that is purely based on the positive starting value for the coinductive cycle is cached (or tabled in logic programming terms) and as such propagated to other goals that are possibly reliant on this. An example where all clause goals are assumedly coinductive may look like this (cf. the test case `coinduction::coinductive_unsound1`):
1847

1948
```notrust
2049
C :- C1.
21-
C :- C2
50+
C :- C2.
2251
C1 :- C2, C3.
2352
C2 :- C1.
2453
```
25-
26-
Here `C` may be proved by either showing `C1` or `C2`.
27-
Assuming the solver starts evaluating the branch with `C1` first, it then recursively tries to prove `C2` and `C3`.
28-
For proving `C2` it needs to show `C1` again, the coinductive cycle becomes evident.
29-
Therefore, `C1` is assumed to be provable and the solver proves `C2` with this information.
30-
Assuming, the solve does not handle this case specifically, the solution for `C2` is cached.
31-
Now it tries solving `C3` but fails due to the lack of information about it.
32-
As such, `C1` can also not be proven for this program.
33-
The recursive solver will now attempt to prove the initial goal `C` by solving `C2`.
34-
Unfortunately, it finds the invalidly cached solution and returns it as proof for `C`.
35-
36-
By visualizing this path of computation, it becomes evident, where the problem lies:
37-
* Start proving `C` with `C1`:
38-
* For `C1` prove `C2` and `C3`:
39-
* For `C2` prove `C1`:
40-
* This is a coinductive cycle. Assume that `C1` holds.
41-
* Thus `C2` also holds. Store this result about `C2` in the cache.
42-
* There is no way to prove `C3`. Lift this failure up.
43-
* Due to the failure of `C3` there is also no solution for `C1`.
44-
* Try proving `C` with `C2`:
45-
* Find the cached result that `C2` has a solution and return it as the solution for `C`.
46-
* Stop with the invalid result for `C`.
54+
The following is a computation to find out whether there exists a type that implements `C`.
55+
Here the implementation of `C` may be proved by either showing that the type implements `C1` or `C2`.
56+
* Start proving `C` by trying to prove `C1`:
57+
* For `C1` try to prove `C2` and `C3`:
58+
* Start with `C2`. For `C2` we need to prove `C1`:
59+
* This is a (coinductive) cycle. Assume that `C1` holds, i.e. use the positive start value.
60+
* Based on this `C2` also holds. If this case is not handled specifically, the solution for `C2` is cached without a reference to the solution for `C1` on which it depends.
61+
* Now try to prove `C3`:
62+
* Find that there is no way do so from the given axioms.
63+
* Thus, there exists no solution for `C3` and the computation fails. This valid result is cached and lifted back up.
64+
* Due to the failure of `C3` there is also no solution for `C1`. This failure is also cached correctly and lifted back up. The cached solution for `C2` has now become invalid as it depends on a positive result for `C1`.
65+
* As a result of the failure for `C1`, `C` can not be proved from `C1`. Try proving `C` from `C2` instead:
66+
* Find the cached result that `C2` has a solution and lift it back up.
67+
* Due to the solution for `C2`, `C` is also proved with the same solution.
68+
* Stop with this positive but invalid result for `C`.
4769

4870
### The Solution
4971
The above example should make it evident that the caching of found solutions in coinductive cycles can lead to invalid results and should therefore be prevented.
50-
This can be achieved by delaying the caching of all results inside the coinductive cycle until it is clear whether the start of the cycle (i.e. `C1` in the example above) is provable.
72+
This can be achieved by delaying the caching of all results inside the coinductive cycle until it is clear whether the start of the cycle (i.e. `C1` in the example above) is provable (cf. the handling of inductive cycles [before](./inductive_cycles.md)).
5173
If the start of the cycle can be proven by the results of the cycle and related subgoals then the assumption about it was correct and thus all results for goals inside the cycle are also valid.
5274
If, however, the start of the cycle can not be proved, i.e. the initial assumption was false, then a subset of the found solutions for the coinductive cycle may be invalid (i.e. the solution for `C2` in the example).
5375

@@ -66,8 +88,11 @@ With this procedure, the example is handled as follows:
6688
* For `C2` prove `C1`:
6789
* `C1` has now a negative result.
6890
* Thus, `C2` also has a negative result which is not yet cached.
69-
* The result for `C3` is already cached.
70-
* Nothing changed regarding `C1` (this would indicate a negative cycle which is currently not allowed) and the negative result for `C1` and `C2` are cached. Lift negative result for `C1`.
91+
* Find the already cached negative result for `C3`.
92+
* Nothing changed regarding `C1` (this would indicate a negative cycle which is currently not allowed) and the negative result for `C1` and `C2` are cached. Lift the negative result for `C1` back up.
7193
* Start proving `C` with `C2`:
7294
* Find negative cached result for `C2`. Lift the result back up.
7395
* Neither `C1` nor `C2` have a positive result. Stop with the valid disproof of `C`.
96+
97+
98+
[co-LP]: https://link.springer.com/chapter/10.1007%2F978-3-540-73420-8_42

book/src/todo.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ Some topics yet to be written:
44

55
- Elaborate on the proof procedure
66
- SLG solving – introduce negative reasoning
7-
- Recursive solver coinduction chapter

chalk-recursive/src/recursive.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ impl<'me, I: Interner> SolveDatabase<I> for Solver<'me, I> {
212212
// Check if this table is still on the stack.
213213
if let Some(depth) = self.context.search_graph[dfn].stack_depth {
214214
self.context.stack[depth].flag_cycle();
215-
// Mixed cycles are not allowed.
215+
// Mixed cycles are not allowed. For mor information about this
216+
// see the corresponding section in the coinduction chapter:
217+
// https://rust-lang.github.io/chalk/book/recursive/coinduction.html#mixed-co-inductive-and-inductive-cycles
216218
if self
217219
.context
218220
.stack

chalk-recursive/src/stack.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,18 @@ impl Stack {
6767
self.entries.pop();
6868
}
6969

70-
/// True if either all the goals from the top of the stack down to (and
71-
/// including) the given depth are coinductive or if all goals are inductive
72-
/// (i.e. not coinductive).
70+
/// True iff there exist at least one coinductive goal
71+
/// and one inductive goal each from the top of the stack
72+
/// down to (and including) the given depth.
7373
pub(crate) fn mixed_inductive_coinductive_cycle_from(&self, depth: StackDepth) -> bool {
74-
let (inductive, coinductive) =
75-
self.entries[depth.depth..]
76-
.iter()
77-
.fold((false, false), |(ind, coind), entry| {
78-
(
79-
ind || !entry.coinductive_goal,
80-
coind || entry.coinductive_goal,
81-
)
82-
});
83-
inductive && coinductive
74+
let coinductive_count = self.entries[depth.depth..]
75+
.iter()
76+
.filter(|entry| entry.coinductive_goal)
77+
.count();
78+
let total_count = self.entries.len() - depth.depth;
79+
let any_coinductive = coinductive_count != 0;
80+
let any_inductive = coinductive_count != total_count;
81+
any_coinductive && any_inductive
8482
}
8583
}
8684

0 commit comments

Comments
 (0)