Download OCaml + XDuce - Virtual building 8

Transcript
cided not to have parametric polymorphism on XDuce types.
A type variable can of course be instantiated to an XDuce
type (or to a type which contains a nested XDuce type), but
it is not possible to force a generalized variable to be instantiated only to XDuce types or to use a type variable within an
XDuce type. The technical presentation introduces a notion
of foreign type variables, but they are nothing more than a
technical device for inferring ground XDuce types.
The exemple is intended to illustrate the use of the OCaml
type checker to perform a data-flow analysis of XML values,
and also how OCaml features (here, higher-order functions
and data-structures) interact with XDuce features.
Double curly-braces {{...}} are used in OCamlDuce only
to avoid ambiguities in the grammar; they carry no typing
information. For instance, the symbol @ used for list concatenation in OCaml is re-used for denote XML sequence
concatenation. Similarly, the square brackets [...] are
used both to denote OCaml list literals (whose elements are
separated by semi-colons) and XML sequences literals when
used within double curly braces (their elements are separated
by whitespace). XML element literals are written in the form
<tag>content.
Overview In Section 2, we give some intuitions about the
behavior of OCamlDuce’s type-checker.
The formalization of the type system will be developped
by abstracting away from details about XDuce. In Section 3, we introduce an abstract notion of extension (foreign types and foreign operators) and show of XDuce can
be seen as an extension. In Section 4, we present the typesystem and type inference algorithm for a calculus made of
ML [Mil78, Dam85] plus an arbitrary extension. The basic
idea is to rely on standard techniques for ML type inference.
Indeed, we start from a type system which is an instance
of ML where foreign types are considered as atomic types
and foreign operators are explicitly annotated with their input and output types. Then we present an algorithm to infer
these annotations. This algorithm is described as two successive passes: the first one is a slightly modified version of
an ML type-checker, and the second one is a simple forward
computation on foreign types.
The first line of the program above declares a function
f which consists of an XML pattern matching on its argument, with a single branch. The XML pattern p =
[ (y::<a>_ | _)* ] extracts from an XML sequence
all the elements with a tag <a> and put them (in order) in the
capture variable y. The function is then used twice, including once indirectly through a call to the function List.map
(from the OCaml standard library) of type ∀α, β.(α → β) →
α list → β list. For the purpose of explaining typechecking, we will rewrite the body of the function f as:
let f x =
let y = match[y;p](x) in
{{ y @ y }}
In Section 5, we present a preprocessing pass, called
strengthening, whose purpose is to make more programs accepted by the type system by allowing implicit use of subtyping.
The y and p parameters of the match operator represent the
capture variable under consideration and the pattern itself.
In this section, we illustrate the behavior of OCamlDuce’s
type-checker on the following code snippet:
In OCamlDuce, XML values (elements, sequences, . . . ) and
regular OCaml values are kept appart. An XML value can of
course appear as part of an OCaml value (e.g. the XML elements which are put into an OCaml list), but an OCaml value
cannot appear within an XML value. The same applies to
types: an XML type can appear as part of a complex OCaml
type expression, but the converse is impossible. XML operators can be applied to XML values and return new XML
values. In the example, we can see three kind of XML operators: XML literals (no argument), XML concatenation (two
arguments), and XML pattern matching (one argument).
let f x = match x with
{{ [ (y::<a>_ | _)* ] }} -> {{ y @ y }}
let z1 =
f {{ [ <a>[] <b>[] <a>[<b>[]] ] }}
let z2 =
List.map f
[ {{ [ <a>[<a>[]] ] }};
{{ [ <a>[<c>[]] ] }} ]
The basic idea of the OCamlDuce type system is to infer
XML types for the inputs and outputs of XML operators.
This is done by introducing internally a new kind of type
variables, called XML type variables. Before proper typechecking starts, each XML operator used in the program is
annotated with fresh XML type variables (in subscript position for the inputs, and in superscript position for the outputs):
In Section 6, we present other details of the concrete integration in OCaml. In Section 7, we compare our approach to
related works.
2
An example
2