Please use the alphabetical Index, Contents List, and your browsers search feature to find what you need.

This page generated on Tue May 2 11:36:53 PDT 2000. This page is a sample of documentation generated from ml.syntax.mth . To find more look in the contents and index page. For information on how this was done see Dr. Botting's MATHS project.

# Description of Standard ML

## Syntax of Standard ML

### Source

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.

. . . . . . . . . ( end of section Source) <<Contents | Index>> The above message 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 ]

1. Empty::="". -- the null string.

O(...) encloses an optional phrase so

2. O(A)::=(A|Empty).

\$#(A) - zero or more occurences of A, and N(A) - one or more occurrences of A, so

3. #(A)::= O(N(A)).
4. N(A)::= (A) \$#(A).

L(A, B) encloses a repetitive phrase of the form ABABA...BA -- 1 or more repetitions of A, separated by B.

5. L(A,B)::= A \$#(B A).
6. List(A)::=L(A, ",")= A \$#("," A).
7. And_List(A)::=L(A, "and") = A \$#("and" A).
8. Sequence(A)::=L(A, ";") = A \$#(";" A).
9. 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

1. Program::= \$#(Top_Level_Declaration ";" ).

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

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

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

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

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

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

9. Functor_Declaration::= Empty | "functor" And_List(Functor_Binding ) | Functor_Declaration O(";") Functor_Declaration.
10. 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++.

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

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

13. Functor_Application::= Ident "(" Structure | Sequence(structure_declaration) ")".

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

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

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

### Declarations

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

25. value_declaration::="val" And_List( O("rec") Pattern "=" Expression ) .
26. function_declaration::="fun" And_List( L(Function_Heading O(":" Type) "=" Expression , "|") ).
27. structure_declaration::="structure" And_list( Ident O(":" Signature) "=" Structure ) | Declaration | "local" structure_declaration "in" Sequence(Structure_Declarations))

28. type_declaration::="type" Type_Binding.
29. abstract_data_type_declaration::="datatype" Datatype_Binding O("withtype" Type_Binding).
30. 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".

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

33. 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.

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

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

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

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

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

### Expressions

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

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

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

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

Patterns">

### Matches and Patterns

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

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

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

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

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

### Types

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

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

### Lexicon

Use MATHS.Lexemes

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

56. Compound_Name::= Compound_Ident | "op" Infix_Operator.

57. Name::= Ident | "op" Infix_Operator.

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

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

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

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

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

63. Ident::= Alphanumeric_Ident | N(Symbol).

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

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

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

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

### Functions of SML-NJ

[ ml.functions.html ]

### Syntax Diagrams

Tobias Nipkow(nipkow@informatik.tu-muenchen.de) has translated Larry Paulson's BNF syntax for Standard ML into diagram form. The result is available as a compressed DVI file from Germany: [ ml-syntax.dvi.gz ] (Thanks to Tobias.Nipkow@informatik.tu-muenchen.de for updating this link).

### Source

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

. . . . . . . . . ( end of section See Also...) <<Contents | Index>>

. . . . . . . . . ( end of section Description of Standard ML) <<Contents | Index>>