Skip to content

Commit db77ff4

Browse files
1 parent 72906f3 commit db77ff4

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ is `Match(S, C1, ..., Cn) <: B` where each case `Ci` is of the form
8585
```
8686
[Xs] => P => T
8787
```
88+
8889
Here, `[Xs]` is a type parameter clause of the variables bound in pattern `Pi`. If there are no bound type variables in a case, the type parameter clause is omitted and only the function type `P => T` is kept. So each case is either a unary function type or a type lambda over a unary function type.
8990

9091
`B` is the declared upper bound of the match type, or `Any` if no such bound is given.
9192
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.
9293

93-
9494
## Match type reduction
9595

9696
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`.
@@ -115,28 +115,36 @@ Disjointness proofs rely on the following properties of Scala types:
115115
The following rules apply to match types. For simplicity, we omit environments and constraints.
116116

117117
The first rule is a structural comparison between two match types:
118+
118119
```
119-
Match(S, C1, ..., Cm) <: Match(T, D1, ..., Dn)
120+
S match { P1 => T1 ... Pm => Tm } <: T match { Q1 => U1 ... Qn => Un }
120121
```
122+
121123
if
124+
122125
```
123-
S <: T, m >= n, Ci <: Di for i in 1..n
126+
S =:= T, m >= n, Pi =:= Qi and Ti <: Qi for i in 1..n
124127
```
125-
I.e. scrutinees and corresponding cases must be subtypes, no case re-ordering is allowed, but the subtype can have more cases than the supertype.
128+
129+
I.e. scrutinees and patterns must be equal and the corresponding bodies must be subtypes. No case re-ordering is allowed, but the subtype can have more cases than the supertype.
126130

127131
The second rule states that a match type and its redux are mutual subtypes
132+
128133
```
129-
Match(S, Cs) <: T
130-
T <: Match(S, Cs)
134+
S match { P1 => T1 ... Pn => Tn } <: U
135+
U <: S match { P1 => T1 ... Pn => Tn }
131136
```
137+
132138
if
139+
133140
```
134-
Match(S, Cs) reduces-to T
141+
S match { P1 => T1 ... Pn => Tn } reduces-to U
135142
```
136143

137-
The third rule states that a match type conforms to its upper bound
144+
The third rule states that a match type conforms to its upper bound:
145+
138146
```
139-
(Match(S, Cs) <: B) <: B
147+
(S match { P1 => T1 ... Pn => Tn } <: B) <: B
140148
```
141149

142150
## Variance Laws for Match Types

0 commit comments

Comments
 (0)