Syntax of ML


This document was generated automatically from an ASCII file using Dick Botting's MATHS notation. It was slightly changed by Martin Eldracher for the local copy.

There is an alphabetical index.

Contents


Description of Standard ML

Syntax of Standard ML

Introduction

This message presents syntax rules for the full Standard ML language. These rules are intended to serve as documentation for programmers, and could be useful to compiler writers. They are slightly too liberal, in that they do not take account of syntactic restrictions or the precedence of certain constructs. I believe that they are complete.
Larry Paulson

Corrections by Dave Berry, Simon Finn, Kent Karlsson and David N. Turner. Adaptations for local Copy by Martin Eldracher.

Note

This was translated into MATHS by Dick Botting and some extra terms were introduced to simplify some definitions. Any errors that have crept in are mine, all mine. Mail me and they will be fixed!

Notes on meta-notation

math_64_Meta_Macros.html

Empty="". -- the null string.

O(...) encloses an optional phrase so O(A)=(A|Empty).

#(A) - zero or more occurences of A, and N(A) - one or more occurrences of A.

L(A, B) encloses a repetitive phrase of the form ABABA...BA -- 1 or more
repetitions of A, separated by B
L(A,B)::=
A #(B A).
List(A)::=
L(A, ",")= A #("," A).
And_List(A)::=
L(A, "and") = A #("and" A).
O_List(A)::=
O(List(A))::=An optional List of A's.

Items below may be separated by spaces, newlines, and comments.

Consecutive keywords and identifiers of the same sort (alphanumeric/symbolic) must be separated to avoid ambiguity.

Programs and Modules

Program::=
#(Top_Level_Declaration ";" ).

Top_Level_Declaration::=
Expression | Object_Declaration | Signature_Declaration | Functor_Declaration.

Object_Declaration::=
Empty | Declaration | "structure" And_List( Ident O(":" Signature) "=" Structure ) | "local" Object_Declaration "in" Object_Declaration "end" | Object_Declaration O(";") Object_Declaration.

Module::glossary=
A separably compilable unit - typically a file - containing module systems.

Module_system::glossary=
Made of structure, signatures, and functors.

Structure::glossary=
a collection of types, datatypes, functions, exceptions, etc we wish to encapsulate.

Signature_Declaration::=
Empty | "signature" And_List(Ident "=" Signature ) | Signature_Declaration O(";") Signature_Declaration.
Signature::glossary=
A collection of info describing and specifying some elements of a structure.

Functor_Declaration::=
Empty | "functor" And_List(Functor_Binding ) | Functor_Declaration O(";") Functor_Declaration.
Functor::Glossary=
An Operation that takes one or more structures and produces another one.

The ML Functor meets the same need to generate special cases of code from a general form that generics do in Ada and templates do in C++.

Functor_Binding::=
Ident "(" Functor_Arguments ")" O(":" Signature) "=" Structure.

Functor_Arguments::=
Ident ":" Signature | Specification.

Structure::=
"struct" Object_Declaration "end" | Compound_Ident | Ident "(" Structure ")" | Ident "(" Object_Declaration ")" | "let" Object_Declaration "in" Structure "end".

Signature::=
"sig" Specification "end" | Ident.

Specification::=
Empty | value_spec | various_type_spec | exception_spec | structure_spec | other_spec | inclusion | | Specification O(";") Specification.
value_specification::=
"val" And_List(Ident ":" Type ).
various_type_spec::=
("type" | "eqtype") And_List(Type_Var_List Ident ) | "datatype" And_List(Datatype_Binding )
exception_spec::=
"exception" And_List(Ident O("of" Type) ).
structure_spec::=
"structure" And_List(Ident ":" Signature ).
other_spec::=
"sharing" And_List( O("type") Ident "=" L(Ident , "=") ) | "local" Specification "in" Specification "end" | "open" L(Compound_Ident ).
inclusions::=
"include" N(Ident ) .

Declarations

Declaration::=
Empty | simple_declaration #(O(";")simple_declaration).
simple_declaration::=
value_declaration | function_declaration | type_declaration | abstract_data_type_declaration | abstract_type_declaration | exception_declaraion | local_declaration | open_declaration | operator_declaration.

value_declaration::=
"val" And_List( O("rec") Pattern "=" Expression ) .
function_declaration::=
"fun" And_List( L(Function_Heading O(":" Type) "=" Expression , "|") ).
type_declaration::=
"type" Type_Binding.
abstract_data_type_declaration::=
"datatype" Datatype_Binding O("withtype" Type_Binding).
abstract_type_declaration::=
"abstype" Datatype_Binding O("withtype" Type_Binding) "with" Declaration "end". In the above the Datatype_Binding defiing the structure of the data is hidden from all but the functions etc in the part of the declaration after the "withs".

exception_declaration::=
"exception" And_List( Name O( "of" Type | "=" Compound_Name ) ).
local_declaration::=
"local" Declaration "in" Declaration "end".

open_declaration::=
"open" N(Compound_Ident ). An Open declaration allows you to refer to the contents of a structure without having to include the name of the structure each time. The names of parts of the openned structure override those declared before the open.

operator_declaration::=
infix_declaration | "nonfix" N(Ident ).
infix_declaration::=
("infix" | "infixr") N(Ident).

infix_operator::=
Ident & visible in an infix declaration & (declared as or in a function of two parameters|`declared as constructor with two parts`).

Function_Heading::=
Name N(Atomic_Pattern) | "(" Atomic_Pattern Infix_Operator Atomic_Pattern ")" #(Atomic_Pattern) | Atomic_Pattern Infix_Operator Atomic_Pattern.

Type_Binding::=
And_List(Type_Var_List Ident "=" Type ).

Datatype_Binding::=
And_List( Type_Var_List Ident "=" L( constructor O("of" Type) , "|" ) ).
constructor::=
Ident & visible in a datatype_binding.

Expressions

Expression::=
Infix_Expression | Expression ":" Type | Expression boolean_operator Expression | Expression "handle" Match | "raise" Expression | selection| loop | "fn" Match.

boolean_operator::=
"andalso" | "orelse".
loop::=
"while" Expression "do" Expression.
selection::=
"if" Expression "then" Expression "else" Expression | "case" Expression "of" Match.

Infix_Expression::=
N(Atomic_Expression ) | Infix_Expression Infix_Operator Infix_Expression.

Atomic_Expression::=
Compound_Name | Constant | "(" O_List(Expression ) ")" | "[" List(Expression ) "]" | "{" O_List(Label "=" Expression ) "}" | "#" Label | "(" L(Expression , ";") ")" | "let" Declaration "in" L(Expression , ";") "end".

Matches and Patterns

Match::=
L(Pattern "=>" Expression , "|").

Pattern::=
Atomic_Pattern | Compound_Name Atomic_Pattern | Pattern infix_constructor Pattern | Pattern ":" Type | Name O(":" Type) "as" Pattern.

infix_constructor::=
Infix_Operator & constructor.
Infix_constructor::=
"::" | ... ;

Atomic_Pattern::=
"_" | Compound_Name | Constant | "(" O_List(Pattern ) ")" | "[" List(Pattern ) "]" | "{" O_List(Field_Pattern ) O("...") "}".

Field_Pattern::=
Label "=" Pattern | Ident O(":" Type) O("as" Pattern).

Types

Type::=
Type_Var | Type_Var_List Compound_Ident | Type "*" L(Type , "*") | Type "->" Type | "{" O_List( Label ":" Type ) "}" | "(" Type ")".

Type_Var_List::=
Empty | Type_Var | "(" List(Type_Var ) ")".

Lexicon

Use MATHS.Lexemes

Compound_Ident::=
L(Ident , ".") .

Compound_Name::=
Compound_Ident | "op" Infix_Operator.

Name::=
Ident | "op" Infix_Operator.

Infix_Operator::=
any identifier that has been declared to be infix.

Constant::=
Numeral O( "." N(digit ) ) O("E" Numeral) | double_quote #(character ~ (backslash | double_quotes) | "\" String_Escape ) double_quote.

String_Escape::=
"n" | "t" | "^" img("@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_") | digit digit digit | double_quote | "\" | N( SP | HT | EOLN | FF ) "\".

Numeral::=
O("~") N(digit ).

Type_Var::=
"'" O("'") O("_") Alphanumeric_Ident.

Ident::=
Alphanumeric_Ident | N(Symbol).

Label::=
Ident | (digit~"0") #(digit).

Alphanumeric_Ident::=
letter #( letter | digit | "_" | "'").

Symbol::=
img("!%&$#+-/:<=>?@\~`^|*").

Comment::=
"(*" L(Commentary, Comment) "*)".
Commentary::=
any text that does not include (* or *) as a substring.

See Also...

Functions of SML-NJ

Syntax Diagrams

Tobias Nipkow(nipkow@informatik.tu-muenchen.de) has translated Larry Paulson's BNF syntax for Standard ML into diagram form. The file is available via ftp or www from Munich ml-syntax.dvi.gz or Cambridge ML-syntax.dvi.gz

Source

Used with permission from Larry.Paulson@cl.cam.ac.uk from the Usenet SML Archives for 1991, message # 2.txt


Assumptions and Definitions in Alphabetical Order


This was the work of Dick Botting at CSUSB.

Copyright(1995): Copy the document in which the link appeared and use as you wish as long as you include the following as source, disclaimer, and copyright.
Also see my home page
Martin Eldracher, 1995-11-14