Skip to content

Commit 9f53478

Browse files
Docs: Reformulate match type reduction algorithm
I didn't understand that section on MT reduction, the rewritten version seams to correspond more closely to what's implemented in the compiler. I didn't get to review that text when it was first added to the docs, but here are the parts that are giving me trouble: - Which part of the code implements the "minimal instantiation" as described in the text? - What is `i` in the two "can-reduce" code blocks? Is it related to T/T'? - The "cannot possibly match" relation, which is central to the algorithm, isn't defined anywhere.
1 parent b3e27ad commit 9f53478

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

docs/docs/reference/new-types/match-types.md

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -90,34 +90,25 @@ Here, `[Xs]` is a type parameter clause of the variables bound in pattern `Pi`.
9090
`B` is the declared upper bound of the match type, or `Any` if no such bound is given.
9191
We will leave it out in places where it does not matter for the discussion. Scrutinee, bound and pattern types must be first-order types.
9292

93+
9394
## Match type reduction
9495

95-
We define match type reduction in terms of an auxiliary relation, `can-reduce`:
96+
Match type reduction follows the semantics of match expression, that is, a match type of the form `S match { P1 => T1 ... Pn => Tn }` reduces to `Ti` if and only if `s: S match { _: P1 => T1 ... _: Pn => Tn }` evaluates to a value of type `Ti` for all `s: S`.
9697

97-
```
98-
Match(S, C1, ..., Cn) can-reduce i, T'
99-
```
100-
if `Ci = [Xs] => P => T` and there are minimal instantiations `Is` of the type variables `Xs` such that
101-
```
102-
S <: [Xs := Is] P
103-
T' = [Xs := Is] T
104-
```
105-
An instantiation `Is` is _minimal_ for `Xs` if all type variables in `Xs` that appear
106-
covariantly and nonvariantly in `Is` are as small as possible and all type variables in `Xs` that appear contravariantly in `Is` are as large as possible. Here, "small" and "large" are understood with respect to `<:`.
98+
The compiler implements the following reduction algorithm:
10799

108-
For simplicity, we have omitted constraint handling so far. The full formulation of subtyping tests describes them as a function from a constraint and a pair of types to
109-
either _success_ and a new constraint or _failure_. In the context of reduction, the subtyping test `S <: [Xs := Is] P` is understood to leave the bounds of all variables
110-
in the input constraint unchanged, i.e. existing variables in the constraint cannot be instantiated by matching the scrutinee against the patterns.
100+
- If the scrutinee type `S` is an empty set of values (such as `Nothing` or `String & Int`), do not reduce.
101+
- Sequentially consider each pattern `Pi`
102+
- If `S <: Pi` reduce to `Ti`.
103+
- Otherwise, try constructing a proof that `S` and `Pi` are disjoint, or, in other words, that no value `s` of type `S` is also of type `Pi`.
104+
- If such proof is found, proceed to the case (`Pi+1`), otherwise, do not reduce.
105+
106+
Disjointness proofs rely on the following properties of Scala types:
107+
108+
1. Single inheritance of classes
109+
2. Final classes cannot be extended
110+
3. Constant types with distinct values are nonintersecting
111111

112-
Using `can-reduce`, we can now define match type reduction proper in the `reduces-to` relation:
113-
```
114-
Match(S, C1, ..., Cn) reduces-to T
115-
```
116-
if
117-
```
118-
Match(S, C1, ..., Cn) can-reduce i, T
119-
```
120-
and, for `j` in `1..i-1`: `Cj` is disjoint from `Ci`, or else `S` cannot possibly match `Cj`.
121112

122113
## Subtyping Rules for Match Types
123114

0 commit comments

Comments
 (0)