| 1 |
= XML |
| 2 |
|
| 3 |
<<XML#>> is an <<IntermediateLanguage#>>, translated from <<CoreML#>> by |
| 4 |
<<Defunctorize#>>, optimized by <<XMLSimplify#>>, and translated by |
| 5 |
<<Monomorphise#>> to <<SXML#>>. |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
<<XML#>> is polymorphic, higher-order, with flat patterns. Every |
| 10 |
<<XML#>> expression is annotated with its type. Polymorphic |
| 11 |
generalization is made explicit through type variables annotating |
| 12 |
`val` and `fun` declarations. Polymorphic instantiation is made |
| 13 |
explicit by specifying type arguments at variable references. <<XML#>> |
| 14 |
patterns can not be nested and can not contain wildcards, constraints, |
| 15 |
flexible records, or layering. |
| 16 |
|
| 17 |
== Implementation |
| 18 |
|
| 19 |
* https://github.com/MLton/mlton/blob/master/mlton/xml/xml.sig[`xml.sig`] |
| 20 |
* https://github.com/MLton/mlton/blob/master/mlton/xml/xml.fun[`xml.fun`] |
| 21 |
* https://github.com/MLton/mlton/blob/master/mlton/xml/xml-tree.sig[`xml-tree.sig`] |
| 22 |
* https://github.com/MLton/mlton/blob/master/mlton/xml/xml-tree.fun[`xml-tree.fun`] |
| 23 |
|
| 24 |
== Type Checking |
| 25 |
|
| 26 |
<<XML#>> also has a type checker, used for debugging. At present, the |
| 27 |
type checker is also the best specification of the type system of |
| 28 |
<<XML#>>. If you need more details, the type checker |
| 29 |
(https://github.com/MLton/mlton/blob/master/mlton/xml/type-check.sig[`type-check.sig`], |
| 30 |
https://github.com/MLton/mlton/blob/master/mlton/xml/type-check.fun[`type-check.fun`]), is pretty short. |
| 31 |
|
| 32 |
Since the type checker does not affect the output of the compiler |
| 33 |
(unless it reports an error), it can be turned off. The type checker |
| 34 |
recursively descends the program, checking that the type annotating |
| 35 |
each node is the same as the type synthesized from the types of the |
| 36 |
expressions subnodes. |
| 37 |
|
| 38 |
== Details and Notes |
| 39 |
|
| 40 |
<<XML#>> uses the same atoms as <<CoreML#>>, hence all identifiers |
| 41 |
(constructors, variables, etc.) are unique and can have properties |
| 42 |
attached to them. Finally, <<XML#>> has a simplifier (<<XMLShrink#>>), |
| 43 |
which implements a reduction system. |
| 44 |
|
| 45 |
=== Types |
| 46 |
|
| 47 |
<<XML#>> types are either type variables or applications of n-ary type |
| 48 |
constructors. There are many utility functions for constructing and |
| 49 |
destructing types involving built-in type constructors. |
| 50 |
|
| 51 |
A type scheme binds list of type variables in a type. The only |
| 52 |
interesting operation on type schemes is the application of a type |
| 53 |
scheme to a list of types, which performs a simultaneous substitution |
| 54 |
of the type arguments for the bound type variables of the scheme. For |
| 55 |
the purposes of type checking, it is necessary to know the type scheme |
| 56 |
of variables, constructors, and primitives. This is done by |
| 57 |
associating the scheme with the identifier using its property list. |
| 58 |
This approach is used instead of the more traditional environment |
| 59 |
approach for reasons of speed. |
| 60 |
|
| 61 |
=== XmlTree |
| 62 |
|
| 63 |
Before defining `XML`, the signature for language <<XML#>>, we need to |
| 64 |
define an auxiliary signature `XML_TREE`, that contains the datatype |
| 65 |
declarations for the expression trees of <<XML#>>. This is done solely |
| 66 |
for the purpose of modularity -- it allows the simplifier and type |
| 67 |
checker to be defined by separate functors (which take a structure |
| 68 |
matching `XML_TREE`). Then, `Xml` is defined as the signature for a |
| 69 |
module containing the expression trees, the simplifier, and the type |
| 70 |
checker. |
| 71 |
|
| 72 |
Both constructors and variables can have type schemes, hence both |
| 73 |
constructor and variable references specify the instance of the scheme |
| 74 |
at the point of references. An instance is specified with a vector of |
| 75 |
types, which corresponds to the type variables in the scheme. |
| 76 |
|
| 77 |
<<XML#>> patterns are flat (i.e. not nested). A pattern is a |
| 78 |
constructor with an optional argument variable. Patterns only occur |
| 79 |
in `case` expressions. To evaluate a case expression, compare the |
| 80 |
test value sequentially against each pattern. For the first pattern |
| 81 |
that matches, destruct the value if necessary to bind the pattern |
| 82 |
variables and evaluate the corresponding expression. If no pattern |
| 83 |
matches, evaluate the default. All patterns of a case statement are |
| 84 |
of the same variant of `Pat.t`, although this is not enforced by ML's |
| 85 |
type system. The type checker, however, does enforce this. Because |
| 86 |
tuple patterns are irrefutable, there will only ever be one tuple |
| 87 |
pattern in a case expression and there will be no default. |
| 88 |
|
| 89 |
<<XML#>> contains value, exception, and mutually recursive function |
| 90 |
declarations. There are no free type variables in <<XML#>>. All type |
| 91 |
variables are explicitly bound at either a value or function |
| 92 |
declaration. At some point in the future, exception declarations may |
| 93 |
go away, and exceptions may be represented with a single datatype |
| 94 |
containing a `unit ref` component to implement genericity. |
| 95 |
|
| 96 |
<<XML#>> expressions are like those of <<CoreML#>>, with the following |
| 97 |
exceptions. There are no records expressions. After type inference, |
| 98 |
all records (some of which may have originally been tuples in the |
| 99 |
source) are converted to tuples, because once flexible record patterns |
| 100 |
have been resolved, tuple labels are superfluous. Tuple components |
| 101 |
are ordered based on the field ordering relation. <<XML#>> eta expands |
| 102 |
primitives and constructors so that there are always fully applied. |
| 103 |
Hence, the only kind of value of arrow type is a lambda. This |
| 104 |
property is useful for flow analysis and later in code generation. |
| 105 |
|
| 106 |
An <<XML#>> program is a list of toplevel datatype declarations and a |
| 107 |
body expression. Because datatype declarations are not generative, |
| 108 |
the defunctorizer can safely move them to toplevel. |