| 1 |
= MatchCompile |
| 2 |
|
| 3 |
<<MatchCompile#>> is a translation pass, agnostic in the |
| 4 |
<<IntermediateLanguage#>>s between which it translates. |
| 5 |
|
| 6 |
== Description |
| 7 |
|
| 8 |
<<MatchCompilation#,Match compilation>> converts a case expression with |
| 9 |
nested patterns into a case expression with flat patterns. |
| 10 |
|
| 11 |
== Implementation |
| 12 |
|
| 13 |
* https://github.com/MLton/mlton/blob/master/mlton/match-compile/match-compile.sig[`match-compile.sig`] |
| 14 |
* https://github.com/MLton/mlton/blob/master/mlton/match-compile/match-compile.fun[`match-compile.fun`] |
| 15 |
|
| 16 |
== Details and Notes |
| 17 |
|
| 18 |
[source,sml] |
| 19 |
---- |
| 20 |
val matchCompile: |
| 21 |
{caseType: Type.t, (* type of entire expression *) |
| 22 |
cases: (NestedPat.t * ((Var.t -> Var.t) -> Exp.t)) vector, |
| 23 |
conTycon: Con.t -> Tycon.t, |
| 24 |
region: Region.t, |
| 25 |
test: Var.t, |
| 26 |
testType: Type.t, |
| 27 |
tyconCons: Tycon.t -> {con: Con.t, hasArg: bool} vector} |
| 28 |
-> Exp.t * (unit -> ((Layout.t * {isOnlyExns: bool}) vector) vector) |
| 29 |
---- |
| 30 |
|
| 31 |
`matchCompile` is complicated by the desire for modularity between the |
| 32 |
match compiler and its caller. Its caller is responsible for building |
| 33 |
the right hand side of a rule `p \=> e`. On the other hand, the match |
| 34 |
compiler is responsible for destructing the test and binding new |
| 35 |
variables to the components. In order to connect the new variables |
| 36 |
created by the match compiler with the variables in the pattern `p`, |
| 37 |
the match compiler passes an environment back to its caller that maps |
| 38 |
each variable in `p` to the corresponding variable introduced by the |
| 39 |
match compiler. |
| 40 |
|
| 41 |
The match compiler builds a tree of n-way case expressions by working |
| 42 |
from outside to inside and left to right in the patterns. For example, |
| 43 |
[source,sml] |
| 44 |
---- |
| 45 |
case x of |
| 46 |
(_, C1 a) => e1 |
| 47 |
| (C2 b, C3 c) => e2 |
| 48 |
---- |
| 49 |
is translated to |
| 50 |
[source,sml] |
| 51 |
---- |
| 52 |
let |
| 53 |
fun f1 a = e1 |
| 54 |
fun f2 (b, c) = e2 |
| 55 |
in |
| 56 |
case x of |
| 57 |
(x1, x2) => |
| 58 |
(case x1 of |
| 59 |
C2 b' => (case x2 of |
| 60 |
C1 a' => f1 a' |
| 61 |
| C3 c' => f2(b',c') |
| 62 |
| _ => raise Match) |
| 63 |
| _ => (case x2 of |
| 64 |
C1 a_ => f1 a_ |
| 65 |
| _ => raise Match)) |
| 66 |
end |
| 67 |
---- |
| 68 |
|
| 69 |
Here you can see the necessity of abstracting out the ride hand sides |
| 70 |
of the cases in order to avoid code duplication. Right hand sides are |
| 71 |
always abstracted. The simplifier cleans things up. You can also see |
| 72 |
the new (primed) variables introduced by the match compiler and how |
| 73 |
the renaming works. Finally, you can see how the match compiler |
| 74 |
introduces the necessary default clauses in order to make a match |
| 75 |
exhaustive, i.e. cover all the cases. |
| 76 |
|
| 77 |
The match compiler uses `numCons` and `tyconCons` to determine |
| 78 |
the exhaustivity of matches against constructors. |