The F# 1.9.6 Draft Language Specification

Note: This documentation is an informal specification of the 1.9.6 “2008 CTP” release of F# made by Microsoft Research and the Microsoft Developer Division in September 2008. It is not official Microsoft documentation. Aspects of the design of any future Microsoft and Microsoft Research releases of F# and its libraries are subject to change.

Discrepancies may exist between this specification and the 1.9.6 implementation. Some of these are noted as comments in this document. If you note further discrepancies please contact us and we’ll gladly address the issue in future releases.

The F# team are always very grateful for feedback on this specification and on both the design and implementation of F#. You can submit feedback by emailing fsbugs@microsoft.com.

Many thanks to the F# user community for their helpful feedback on the document so far.

(Document revision: spec-1.9.6-v5)

Table of Contents

1         Introduction.. 9

Lightweight Syntax. 9

Making Data Simple. 9

Making Types Simple. 10

Functional Programming. 11

Imperative Programming. 12

.NET Interoperability and Fidelity. 12

Parallel and Asynchronous Programming. 12

Stronger Typing for Floating Point Code. 13

Object Oriented Programming and Code Organization. 13

1.1      About This Draft Specification.. 15

2         Program Structure. 16

3         Lexical Analysis. 18

3.1      Whitespace. 18

3.2      Comments. 18

3.3      Conditional Compilation.. 18

3.3.1        Conditional Compilation for OCaml Compatibility. 19

3.4      Identifiers and Keywords. 19

3.5      Strings and characters. 21

3.6      Symbolic Keywords. 22

3.7      Symbolic Operators. 22

3.8      Numeric Literals. 23

3.8.1        Post-filtering of adjacent, prefix “-“ tokens. 24

3.8.2        Post-filtering of integers followed by adjacent “..”. 25

3.8.3        Reserved numeric literal forms. 25

3.9      Pre-processor Declarations. 25

3.9.1        Line Directives. 25

3.10         Hidden Tokens. 25

3.11         Identifier Replacements. 26

4         Basic Grammar Elements. 27

4.1      Operator Names. 27

4.2      Long Identifiers. 29

4.3      Constants. 29

4.4      Precedence and Operators. 30

5         Types and Type Constraints. 32

5.1      Checking Syntactic Types. 33

5.1.1        Named Types. 34

5.1.2        Variable Types. 34

5.1.3        Tuple Types. 35

5.1.4        Array Types. 35

5.1.5        Constrained Types. 35

5.2      Type Parameter Definitions. 37

5.3      Logical Properties of Types. 38

5.3.1        Characteristics of Type Definitions. 38

5.3.2        Expanding Abbreviations and Inference Equations. 39

5.3.3        Type Variables and Binding. 39

5.3.4        Base Type of a Type. 39

5.3.5        Interfaces Types of a Type. 40

5.3.6        Type equivalence. 40

5.3.7        Subtyping and Coercion. 40

5.3.8        Nullness. 40

5.3.9        Dynamic Conversion Between Types. 42

5.4      Static Type Schemes. 43

6         Expressions. 44

6.1      Syntactic Sugar and Ambiguities. 46

6.1.1        Syntactic Sugar 46

6.1.2        Ambiguities. 47

6.2      Some checking and inference terminology. 47

6.3      Elaboration and Elaborated Expressions. 48

6.4      Data Expressions. 49

6.4.1        Simple constant expressions. 49

6.4.2        Tuple Expressions. 49

6.4.3        List Expressions. 50

6.4.4        Array Expressions. 51

6.4.5        Record Expressions. 51

6.4.6        Copy-and-update Record Expressions. 52

6.4.7        Function Expressions. 52

6.4.8        Object Expressions. 53

6.4.9        Delayed Expressions. 55

6.4.10      Computation Expressions. 55

6.4.11      Sequence expressions. 57

6.4.12      Range expressions. 57

6.4.13      Lists via sequence expressions. 58

6.4.14      Arrays via sequence expressions. 58

6.4.15      Null expressions. 58

6.4.16      The AddressOf Operators. 58

6.4.17      'printf' Formats. 59

6.5      Application Expressions. 61

6.5.1        Basic Application Expressions. 61

6.5.2        Object Construction Expressions. 62

6.5.3        Assignment expressions. 63

6.6      Control Flow Expressions. 64

6.6.1        Parenthesized and Block Expressions. 64

6.6.2        Sequential Execution Expressions. 64

6.6.3        Conditional Expressions. 64

6.6.4        Pattern Matching Expressions and Functions. 64

6.6.5        Sequence Iteration Expressions. 65

6.6.6        Try-catch Expressions. 65

6.6.7        Try-finally Expressions. 66

6.6.8        While Expressions. 66

6.6.9        Simple for-Loop Expressions. 66

6.6.10      Assertion Expressions. 67

6.7      Binding Expressions. 67

6.7.1        Binding Expressions. 67

6.7.2        Recursive Binding Expressions. 69

6.7.3        Deterministic Disposal Expressions. 70

6.8      Type-Related Expressions. 70

6.8.1        Rigid Type Annotation Expressions. 70

6.8.2        Static Coercion Expressions. 70

6.8.3        Dynamic Type Test Expressions. 71

6.8.4        Dynamic Coercion Expressions. 71

6.9      Expression Quotations. 71

6.9.1        Raw expression quotations. 72

6.10         Evaluation of Elaborated Forms. 72

6.10.1      Zero Values. 73

6.10.2      Evaluating Value References. 73

6.10.3      Evaluating Function Applications. 73

6.10.4      Evaluating Method Applications. 73

6.10.5      Evaluating Discriminated Union Cases. 73

6.10.6      Evaluating Field Lookups. 73

6.10.7      Evaluating Active Pattern Results. 73

6.10.8      Evaluating Array Expressions. 74

6.10.9      Evaluating Record Expressions. 74

6.10.10         Evaluating Function Expressions. 74

6.10.11         Evaluating Object Expressions. 74

6.10.12         Evaluating Binding Expressions. 74

6.10.13         Evaluating For Loops. 74

6.10.14         Evaluating While Loops. 74

6.10.15         Evaluating Static Coercion Expressions. 75

6.10.16         Evaluating Dynamic Type Test Expressions. 75

6.10.17         Evaluating Dynamic Coercion Expressions. 75

6.10.18         Evaluating Sequential Execution Expressions. 76

6.10.19         Evaluating Try-catch Expressions. 76

6.10.20         Evaluating Try-finally Expressions. 76

6.10.21         Types with Under-specified Object and Type Identity. 76

6.11         Constant Expressions. 77

7         Patterns. 78

7.1.1        Simple constant patterns. 79

7.1.2        Named Patterns. 79

7.1.3        'As' patterns. 81

7.1.4        Union patterns. 82

7.1.5        'And' patterns. 82

7.1.6        'Cons' and list patterns. 82

7.1.7        Type annotated patterns. 82

7.1.8        Dynamic type test patterns. 83

7.1.9        Record patterns. 83

7.1.10      Array patterns. 84

7.1.11      Null patterns. 84

7.1.12      Guarded pattern rules. 84

8         Declaration Elements. 85

8.1      Import Declarations. 87

8.2      Module Abbreviations. 87

8.3      “let” Bindings in Modules. 87

8.3.1        Processing of Let Bindings in Modules. 88

8.3.2        “do” bindings. 88

8.3.3        Active Pattern Bindings. 89

8.3.4        Arity inference for ‘let’ bindings. 89

8.3.5        Literals. 89

8.3.6        Type Functions. 90

8.4      Member Definitions. 91

8.4.1        Property Members. 91

8.4.2        Method Members. 93

8.4.3        Abstract Members. 93

8.4.4        Members Implementing Dispatch Slots. 94

8.4.5        Named Arguments to Method Members. 94

8.4.6        Optional Arguments to Method Members. 95

8.4.7        Overloading Members. 97

8.4.8        Representations of Members. 97

8.5      Interface Specifications and Implementations. 98

8.6      Custom Attributes. 99

8.6.1        Custom Attributes Imported by F#. 101

8.6.2        Custom Attributes Emitted by F#. 104

8.7      Extern P/Invoke Declarations. 104

8.7.1        Custom Attributes Relevant to the P/Invoke Marshalling. 104

8.8      Accessibility Annotations. 104

8.8.1        Permitted locations of accessibility modifiers: 105

8.9      Events. 106

9         Basic Type and Module Definitions. 108

9.1      Type Abbreviations. 109

9.2      Record Types. 110

9.2.1        Module paths for record types. 111

9.3      Union Types. 111

9.4      Exception Definitions. 112

9.5      Module Definitions. 112

9.6      Generated Equality, Hashing and Comparison.. 113

9.6.1        Controlling the Generation of Structural Equality, Hashing and Comparison Implementations. 114

9.6.2        Behaviour of the generated Object.Equals implementation. 115

9.6.3        Behaviour of the generated IComparable.CompareTo implementation. 116

9.6.4        Legacy generation of Object.Equals implementation. 116

9.6.5        Behaviour of the generated Object.GetHashCode and IStructuralHash.GetStructuralHashCode implementation  117

9.6.6        Behaviour of hash, (=) and compare. 117

10       Object Oriented Type Definitions. 120

10.1         Type Kind Inference. 120

10.2         Class Types. 121

10.2.1      “as” Declarations in Classes. 121

10.2.2      “inherit” Declarations in Classes. 122

10.2.3      “let” and “do” Declarations in Classes. 122

10.2.4      “static let” and “static do” Declarations in Classes. 122

10.2.5      “member” Declarations in Classes. 123

10.2.6      Explicit Object Constructors. 123

10.2.7      Explicit Fields. 124

10.3         Interface Types. 125

10.4         Struct Types. 126

10.5         Enum Types. 127

10.6         Delegate Types. 127

10.7         Type Extensions. 128

11       Signatures. 130

11.1         Signature Types. 130

11.2         Signature Conformance. 131

11.2.1      Conformance for values. 131

11.2.2      Conformance for members. 132

12       Implementation Files and Signature Files. 134

12.1         Namespaces Fragments and Implementation Files. 134

12.1.1      Implicit open of the enclosing namespace for a namespace fragment 135

12.1.2      Checking of Implementation Files. 135

12.1.3      Initialization Semantics for Implementation Files. 136

12.1.4      Explicit “Main” Entry Point 136

12.2         Signature Files. 137

13       Units-of-measure. 138

13.1         Measure Constants. 138

13.2         Measure Type Annotations. 139

13.3         Equivalence of measures and constraint solving.. 140

13.3.1      Constraint solving. 141

13.3.2      Generalization. 141

13.4         Measure Definitions. 141

13.5         Measure Parameter Definitions. 142

13.6         Measure Parameter Erasure. 142

13.7         Static members on floating-point types. 143

14       Inference Procedures. 144

14.1         Name Resolution.. 144

14.1.1      Name Environments. 144

14.1.2      Name Resolution in Module and Namespace Paths. 144

14.1.3      Name Resolution in Expressions. 145

14.1.4      Name Resolution for Members. 146

14.1.5      Name Resolution in Patterns. 146

14.1.6      Name Resolution for Types. 147

14.1.7      Name Resolution for Type Variables. 147

14.1.8      Field Label Resolution. 148

14.1.9      Opening Modules and Namespace Fragments. 148

14.2         Resolving Application Expressions. 149

14.2.1      Unqualified Lookup. 149

14.2.2      Item-Qualified Lookup. 150

14.2.3      Expression-Qualified Lookup. 151

14.3         Function Application Resolution.. 153

14.4         Method Application Resolution.. 153

14.4.1      Conditional Compilation of Member Calls. 155

14.5         Implicit Insertion of Flexibility for Uses of Values and Members. 155

14.6         Constraint Solving.. 156

14.6.1      Solving Equational Constraints. 157

14.6.2      Solving Subtype Constraints. 157

14.6.3      Solving Nullness, Struct and other Simple Constraints. 158

14.6.4      Solving Member Constraints. 158

14.6.5      Over-constrained user type annotations. 159

14.7         Generalization.. 159

14.8         Dispatch Slot Inference. 161

14.9         Dispatch Slot Checking.. 163

14.10       Byref Safety Analysis. 163

14.10.1         Passing ref to methods expecting byref values. 163

14.11       Recursive Safety Analysis. 164

14.12       Establishing Type Definitions. 165

14.12.1         Checking members in types. 165

15       Lexical Filtering.. 167

15.1         The Lightweight Syntax Option (#light) 167

15.1.1      Basic #light rules by example. 167

15.1.2      Inserted Tokens. 168

15.1.3      Grammar rules including inserted tokens. 169

15.1.4      Offside  lines and contexts. 169

15.1.5      The Pre-Parse Stack. 170

15.1.6      Full List of Offside Contexts. 170

15.1.7      Balancing rules. 171

15.1.8      Exceptions to when tokens are offside. 172

15.1.9      Permitted Undentations. 174

15.2         High Precedence Application.. 175

15.3         Lexical analysis of type applications. 176

16       The F# Library FSharp.Core.dll. 177

16.1         Basic Types (Microsoft.FSharp.Core) 177

16.1.1      Basic Type Abbreviations. 177

16.1.2      nativeptr<_>. 178

16.2         Basic Operators and Functions (Microsoft.FSharp.Core.Operators) 178

16.2.1