Chapter 6  The OCaml language

Foreword

This document is intended as a reference manual for the OCaml language. It lists the language constructs, and gives their precise syntax and informal semantics. It is by no means a tutorial introduction to the language: there is not a single example. A good working knowledge of OCaml is assumed.

No attempt has been made at mathematical rigor: words are employed with their intuitive meaning, without further definition. As a consequence, the typing rules have been left out, by lack of the mathematical framework required to express them, while they are definitely part of a full formal definition of the language.

Notations

The syntax of the language is given in BNF-like notation. Terminal symbols are set in typewriter font (like this). Non-terminal symbols are set in italic font (like  that). Square brackets […] denote optional components. Curly brackets {…} denotes zero, one or several repetitions of the enclosed components. Curly bracket with a trailing plus sign {…}+ denote one or several repetitions of the enclosed components. Parentheses (…) denote grouping.

6.3  Names

Identifiers are used to give names to several classes of language objects and refer to these objects by name later:

These eleven name spaces are distinguished both by the context and by the capitalization of the identifier: whether the first letter of the identifier is in lowercase (written lowercase-ident below) or in uppercase (written capitalized-ident). Underscore is considered a lowercase letter for this purpose.

Naming objects

value-name::= lowercase-ident  
  ( operator-name )  
 
operator-name::= prefix-symbol ∣  infix-op  
 
infix-op::= infix-symbol  
  * ∣  = ∣  or ∣  & ∣  :=  
  mod ∣  land ∣  lor ∣  lxor ∣  lsl ∣  lsr ∣  asr  
 
constr-name::= capitalized-ident  
 
label-name::= lowercase-ident  
 
tag-name::= ident  
 
typeconstr-name::= lowercase-ident  
 
field-name::= lowercase-ident  
 
module-name::= capitalized-ident  
 
modtype-name::= ident  
 
class-name::= lowercase-ident  
 
inst-var-name::= lowercase-ident  
 
method-name::= lowercase-ident

As shown above, prefix and infix symbols as well as some keywords can be used as value names, provided they are written between parentheses. The capitalization rules are summarized in the table below.

Name spaceCase of first letter
Valueslowercase
Constructorsuppercase
Labelslowercase
Polymorphic variant tagsuppercase
Exceptionsuppercase
Type constructorslowercase
Record fieldslowercase
Classeslowercase
Instance variableslowercase
Methodslowercase
Modulesuppercase
Module typesany

Note on polymorphic variant tags: the current implementation accepts lowercase variant tags in addition to uppercase variant tags, but we suggest you avoid lowercase variant tags for portability and compatibility with future OCaml versions.

Referring to named objects

value-path::=module-path . ]  value-name  
 
constr::=module-path . ]  constr-name  
 
typeconstr::=extended-module-path . ]  typeconstr-name  
 
field::=module-path . ]  field-name  
 
module-path::=module-name . }  module-name  
 
extended-module-path::=extended-module-path . ]  module-name  
  extended-module-path (  extended-module-path )  
 
modtype-path::=extended-module-path . ]  modtype-name  
 
class-path::=module-path . ]  class-name

A named object can be referred to either by its name (following the usual static scoping rules for names) or by an access path prefix .  name, where prefix designates a module and name is the name of an object defined in that module. The first component of the path, prefix, is either a simple module name or an access path name1 .  name2 …, in case the defining module is itself nested inside other modules. For referring to type constructors or module types, the prefix can also contain simple functor applications (as in the syntactic class extended-module-path above), in case the defining module is the result of a functor application.

Label names, tag names, method names and instance variable names need not be qualified: the former three are global labels, while the latter are local to a class.