next up previous contents
Next: A lexer generator Up: Moscow ML Owner's Manual Previous: Value polymorphism

Quotations and antiquotations

 

Moscow ML implements quotations, a non-standard language feature useful for embedding object language phrases in ML programs. Quotations are disabled by default. This feature originates in the Standard ML of New Jersey implementation. To enable quotations in the interactive system (mosml), execute quotation := true. This allows quotations to appear in declarations entered at top-level and in files compiled by the primitive compile. To enable quotations in files compiled with the batch compiler mosmlc, invoke it with option -q as in mosmlc -q.

A quotation is a particular kind of expression and consists of a non-empty sequence of (possibly empty) fragments surrounded by backquotes:

quot840

The charseq is a possibly empty sequence of printable characters or spaces or tabs or newlines. A quotation evaluates to a value of type ty frag list where ty is the type of the antiquotation variables and antiquotation expressions, and the type 'a frag is defined as follows:

program864

A charseq fragment evaluates to QUOTE " charseq". An antiquotation fragment ^id or ^(exp) evaluates to ANTIQUOTE value where value is the value of the variable id resp. the expression exp. All antiquotations in a quotation must have the same type ty.

An antiquotation fragment is always surrounded by (possibly empty) quotation fragments; and no two quotation fragments can be adjacent. The entire quotation is parsed before any antiquotation inside it is evaluated. Hence changing the value of Meta.quotation in an antiquotation inside a quotation has no effect on the parsing of the containing quotation.

For an example, say we have written an ML program to analyse C program phrases, and that we want to enter the C declaration char s[6] = "abcde". We could simply define it as a string:

program879

but then we need to escape the quotes (") in the C declaration, which is tiresome. If instead we use a quotation, these escapes are not needed:

program882

It evaluates to [QUOTE "char s[6] = \"abcde\""] : 'a frag list. Moreover, suppose we want to generate such declarations for other strings than just "abcde", and that we have an abstract syntax for C phrases:

program886

Then we may replace the string "abcde" by an antiquotation ^(StrCst str), and the array dimension 6 by an antiquotation ^(IntCst (size str + 1)), and make the string str a function parameter:

program892

Evaluating mkphrase "longer" produces the following representation of a C phrase:

program895


next up previous contents
Next: A lexer generator Up: Moscow ML Owner's Manual Previous: Value polymorphism

Moscow ML 1.42