specyfikacja_Fis.pdf
(
5147 KB
)
Pobierz
The F# 2.0 Language Specification
Note: This documentation is the specification of the 2.0 release of F# made by Microsoft Research and the
Microsoft Developer Division in April 2010.
Discrepancies may exist between this specification and the 2.0 implementation. Some of these are noted as
comments in this document. If you find further discrepancies please contact us and we will gladly address the
issue in future releases of this specification. The F# team is 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
.
The latest version of this specification can be found at
www.fsharp.net
.
Many thanks to the F# user community
for their helpful feedback on the document so far.
Certain parts of this specification refer to the C# 4.0, Unicode, and IEEE specifications.
Authors:
Don Syme, with assistance from Anar Alimov,Keith Battocchi, Jomo Fisher, Michael Hale, Jack Hu,
Luke Hoban, Tao Liu, Dmitry Lomov, James Margetson, Brian McNamara, Joe Pamer, Penny Orwick, Daniel
Quirk, Chris Smith, Matteo Taveggia and others.
Notice
© 2005-2012 Microsoft Corporation. Made available under the
Apache 2.0 License
as part of F# 2.0 source code.
Microsoft, Windows, and Visual F# are either registered trademarks or trademarks of Microsoft Corporation in the
U.S.A. and/or other countries/regions.
Other product and company names mentioned herein may be the trademarks of their respective owners.
Document Updates:
Updated with glossary, index, and style corrections, August 2010
Updated with glossary, index, and style corrections, February 2011
Updated with grammar summary, December 2011
Updated with formatting changes, April 2012
Table of Contents
INTRODUCTION .....................................................................................................................................................11
1.
1.1
A
F
IRST
P
ROGRAM
......................................................................................................................................................... 11
1.1.1
Lightweight Syntax ........................................................................................................................................ 11
1.1.2
Making Data Simple ...................................................................................................................................... 12
1.1.3
Making Types Simple ..................................................................................................................................... 12
1.1.4
Functional Programming ............................................................................................................................... 13
1.1.5
Imperative Programming .............................................................................................................................. 14
1.1.6
.NET Interoperability and CLI Fidelity............................................................................................................. 14
1.1.7
Parallel and Asynchronous Programming ..................................................................................................... 15
1.1.8
Strong Typing for Floating-Point Code........................................................................................................... 15
1.1.9
Object-Oriented Programming and Code Organization................................................................................. 16
1.2
N
OTATIONAL
C
ONVENTIONS IN
T
HIS
S
PECIFICATION
.............................................................................................................. 17
PROGRAM STRUCTURE..........................................................................................................................................19
2.
LEXICAL ANALYSIS .................................................................................................................................................21
3.
3.1
W
HITESPACE
................................................................................................................................................................ 21
3.2
C
OMMENTS
................................................................................................................................................................. 21
3.3
C
ONDITIONAL
C
OMPILATION
............................................................................................................................................ 21
3.4
I
DENTIFIERS AND
K
EYWORDS
............................................................................................................................................ 22
3.5
S
TRINGS AND
C
HARACTERS
.............................................................................................................................................. 23
3.6
S
YMBOLIC
K
EYWORDS
.................................................................................................................................................... 24
3.7
S
YMBOLIC
O
PERATORS
................................................................................................................................................... 25
3.8
N
UMERIC
L
ITERALS
........................................................................................................................................................ 25
3.8.1
Post-filtering of Adjacent Prefix Tokens......................................................................................................... 26
3.8.2
Post-filteringofIntegersFollowedbydjacent“..”....................................................................................... 26
3.8.3
Reserved Numeric Literal Forms .................................................................................................................... 26
3.9
L
INE
D
IRECTIVES
............................................................................................................................................................ 27
3.10
H
IDDEN
T
OKENS
...................................................................................................................................................... 27
3.11
I
DENTIFIER
R
EPLACEMENTS
......................................................................................................................................... 27
BASIC GRAMMAR ELEMENTS.................................................................................................................................29
4.
4.1
O
PERATOR
N
AMES
........................................................................................................................................................ 29
4.2
L
ONG
I
DENTIFIERS
......................................................................................................................................................... 31
4.3
C
ONSTANTS
................................................................................................................................................................. 31
4.4
O
PERATORS AND
P
RECEDENCE
.......................................................................................................................................... 32
4.4.1
Categorization of Symbolic Operators ........................................................................................................... 32
4.4.2
Precedence of Symbolic Operators and Pattern/Expression Constructs ........................................................ 33
TYPES AND TYPE CONSTRAINTS.............................................................................................................................35
5.
5.1
C
HECKING
S
YNTACTIC
T
YPES
............................................................................................................................................ 36
5.1.1
Named Types ................................................................................................................................................. 37
5.1.2
Variable Types ............................................................................................................................................... 37
5.1.3
Tuple Types .................................................................................................................................................... 38
5.1.4
Array Types.................................................................................................................................................... 38
5.1.5
Constrained Types ......................................................................................................................................... 38
5.2
T
YPE
C
ONSTRAINTS
........................................................................................................................................................ 39
5.2.1
Subtype Constraints....................................................................................................................................... 39
5.2.2
Nullness Constraints ...................................................................................................................................... 39
5.2.3
Member Constraints ...................................................................................................................................... 40
5.2.4
Default Constructor Constraints .................................................................................................................... 40
5.2.5
Value Type Constraints .................................................................................................................................. 40
5.2.6
Reference Type Constraints ........................................................................................................................... 41
5.2.7
Enumeration Constraints ............................................................................................................................... 41
5.2.8
Delegate Constraints ..................................................................................................................................... 41
5.2.9
Unmanaged Constraints ................................................................................................................................ 42
5.2.10
Equality and Comparison Constraints............................................................................................................ 42
5.3
T
YPE
P
ARAMETER
D
EFINITIONS
......................................................................................................................................... 42
5.4
L
OGICAL
P
ROPERTIES OF
T
YPES
......................................................................................................................................... 43
5.4.1
Characteristics of Type Definitions ................................................................................................................ 43
5.4.2
Expanding Abbreviations and Inference Equations ....................................................................................... 44
5.4.3
Type Variables and Definition Sites ............................................................................................................... 45
5.4.4
Base Type of a Type ....................................................................................................................................... 45
2
5.4.5
Interfaces Types of a Type ............................................................................................................................. 46
5.4.6
Type Equivalence ........................................................................................................................................... 46
5.4.7
Subtyping and Coercion ................................................................................................................................. 46
5.4.8
Nullness ......................................................................................................................................................... 47
5.4.9
Default Initialization ...................................................................................................................................... 48
5.4.10
Dynamic Conversion Between Types ............................................................................................................. 48
EXPRESSIONS ........................................................................................................................................................51
6.
6.1
S
OME
C
HECKING AND
I
NFERENCE
T
ERMINOLOGY
.................................................................................................................. 53
6.2
E
LABORATION AND
E
LABORATED
E
XPRESSIONS
..................................................................................................................... 54
6.3
D
ATA
E
XPRESSIONS
........................................................................................................................................................ 55
6.3.1
Simple Constant Expressions ......................................................................................................................... 56
6.3.2
Tuple Expressions .......................................................................................................................................... 56
6.3.3
List Expressions .............................................................................................................................................. 57
6.3.4
Array Expressions .......................................................................................................................................... 58
6.3.5
Record Expressions ........................................................................................................................................ 58
6.3.6
Copy-and-update Record Expressions............................................................................................................ 59
6.3.7
Function Expressions ..................................................................................................................................... 60
6.3.8
Object Expressions ......................................................................................................................................... 60
6.3.9
Delayed Expressions ...................................................................................................................................... 62
6.3.10
Computation Expressions .............................................................................................................................. 62
6.3.11
Sequence Expressions .................................................................................................................................... 66
6.3.12
Range Expressions ......................................................................................................................................... 66
6.3.13
Lists via Sequence Expressions....................................................................................................................... 67
6.3.14
Arrays Sequence Expressions ......................................................................................................................... 67
6.3.15
Null Expressions ............................................................................................................................................. 68
6.3.16
'printf' Formats .............................................................................................................................................. 68
6.4
A
PPLICATION
E
XPRESSIONS
.............................................................................................................................................. 69
6.4.1
Basic Application Expressions ........................................................................................................................ 69
6.4.2
Object Construction Expressions.................................................................................................................... 70
6.4.3
Operator Expressions..................................................................................................................................... 71
6.4.4
Dynamic Operator Expressions ...................................................................................................................... 72
6.4.5
The AddressOf Operators .............................................................................................................................. 73
6.4.6
Lookup Expressions........................................................................................................................................ 73
6.4.7
Slice Expressions ............................................................................................................................................ 74
6.4.8
Member Constraint Invocation Expressions .................................................................................................. 74
6.4.9
Assignment Expressions................................................................................................................................. 75
6.5
C
ONTROL
F
LOW
E
XPRESSIONS
.......................................................................................................................................... 76
6.5.1
Parenthesized and Block Expressions ............................................................................................................ 76
6.5.2
Sequential Execution Expressions .................................................................................................................. 77
6.5.3
Conditional Expressions ................................................................................................................................. 77
6.5.4
Shortcut Operator Expressions ...................................................................................................................... 77
6.5.5
Pattern-Matching Expressions and Functions ............................................................................................... 78
6.5.6
Sequence Iteration Expressions ..................................................................................................................... 78
6.5.7
Simple for-Loop Expressions .......................................................................................................................... 79
6.5.8
While Expressions .......................................................................................................................................... 79
6.5.9
Try-with Expressions ...................................................................................................................................... 80
6.5.10
Reraise Expressions........................................................................................................................................ 80
6.5.11
Try-finally Expressions ................................................................................................................................... 80
3
6.5.12
Assertion Expressions .................................................................................................................................... 81
6.6
D
EFINITION
E
XPRESSIONS
................................................................................................................................................ 81
6.6.1
Value Definition Expressions.......................................................................................................................... 82
6.6.2
Function Definition Expressions ..................................................................................................................... 82
6.6.3
Recursive Definition Expressions.................................................................................................................... 83
6.6.4
Deterministic Disposal Expressions................................................................................................................ 83
6.7
T
YPE
-R
ELATED
E
XPRESSIONS
............................................................................................................................................ 84
6.7.1
Type-Annotated Expressions ......................................................................................................................... 84
6.7.2
Static Coercion Expressions ........................................................................................................................... 84
6.7.3
Dynamic Type-Test Expressions ..................................................................................................................... 84
6.7.4
Dynamic Coercion Expressions....................................................................................................................... 84
6.8
Q
UOTED
E
XPRESSIONS
.................................................................................................................................................... 85
6.8.1
Strongly Typed Quoted Expressions............................................................................................................... 86
6.8.2
Weakly Typed Quoted Expressions ................................................................................................................ 86
6.8.3
Expression Splices .......................................................................................................................................... 86
6.9
E
VALUATION OF
E
LABORATED
F
ORMS
................................................................................................................................ 87
6.9.1
Values and Execution Context ....................................................................................................................... 87
6.9.2
Parallel Execution and Memory Model.......................................................................................................... 88
6.9.3
Zero Values .................................................................................................................................................... 89
6.9.4
Taking the Address of an Elaborated Expression ........................................................................................... 89
6.9.5
Evaluating Value References ......................................................................................................................... 90
6.9.6
Evaluating Function Applications .................................................................................................................. 90
6.9.7
Evaluating Method Applications.................................................................................................................... 90
6.9.8
Evaluating Union Cases ................................................................................................................................. 91
6.9.9
Evaluating Field Lookups ............................................................................................................................... 91
6.9.10
Evaluating Array Expressions......................................................................................................................... 91
6.9.11
Evaluating Record Expressions ...................................................................................................................... 91
6.9.12
Evaluating Function Expressions.................................................................................................................... 91
6.9.13
Evaluating Object Expressions ....................................................................................................................... 91
6.9.14
Evaluating Definition Expressions.................................................................................................................. 92
6.9.15
Evaluating Integer For Loops ......................................................................................................................... 92
6.9.16
Evaluating While Loops ................................................................................................................................. 92
6.9.17
Evaluating Static Coercion Expressions.......................................................................................................... 92
6.9.18
Evaluating Dynamic Type-Test Expressions ................................................................................................... 92
6.9.19
Evaluating Dynamic Coercion Expressions..................................................................................................... 93
6.9.20
Evaluating Sequential Execution Expressions ................................................................................................ 93
6.9.21
Evaluating Try-with Expressions .................................................................................................................... 93
6.9.22
Evaluating Try-finally Expressions ................................................................................................................. 94
6.9.23
Evaluating AddressOf Expressions ................................................................................................................. 94
6.9.24
Values with Underspecified Object Identity and Type Identity ...................................................................... 94
PATTERNS .............................................................................................................................................................97
7.
7.1
S
IMPLE
C
ONSTANT
P
ATTERNS
........................................................................................................................................... 98
7.2
N
AMED
P
ATTERNS
......................................................................................................................................................... 98
7.2.1
Union Case Patterns ...................................................................................................................................... 99
7.2.2
Literal Patterns .............................................................................................................................................. 99
7.2.3
Active Patterns ............................................................................................................................................ 100
7.3
“A
S
”
P
ATTERNS
.......................................................................................................................................................... 101
7.4
W
ILDCARD
P
ATTERNS
................................................................................................................................................... 102
4
7.5
D
ISJUNCTIVE
P
ATTERNS
................................................................................................................................................ 102
7.6
C
ONJUNCTIVE
P
ATTERNS
............................................................................................................................................... 102
7.7
L
IST
P
ATTERNS
............................................................................................................................................................ 102
7.8
T
YPE
-A
NNOTATED
P
ATTERNS
......................................................................................................................................... 103
7.9
D
YNAMIC
T
YPE
-T
EST
P
ATTERNS
...................................................................................................................................... 103
7.10
R
ECORD
P
ATTERNS
................................................................................................................................................. 104
7.11
A
RRAY
P
ATTERNS
................................................................................................................................................... 104
7.12
N
ULL
P
ATTERNS
..................................................................................................................................................... 105
7.13
G
UARDED
P
ATTERN
R
ULES
....................................................................................................................................... 105
TYPE DEFINITIONS ...............................................................................................................................................107
8.
8.1
T
YPE
D
EFINITION
G
ROUP
C
HECKING AND
E
LABORATION
....................................................................................................... 110
8.2
T
YPE
K
IND
I
NFERENCE
.................................................................................................................................................. 112
8.3
T
YPE
A
BBREVIATIONS
................................................................................................................................................... 113
8.4
R
ECORD
T
YPE
D
EFINITIONS
............................................................................................................................................ 114
8.4.1
Members in Record Types............................................................................................................................ 114
8.4.2
Name Resolution and Record Field Labels ................................................................................................... 114
8.4.3
Structural Hashing, Equality, and Comparison for Record Types................................................................. 115
8.4.4
With/End in Record Type Definitions ........................................................................................................... 115
8.5
U
NION
T
YPE
D
EFINITIONS
............................................................................................................................................. 115
8.5.1
Members in Union Types ............................................................................................................................. 116
8.5.2
Structural Hashing, Equality, and Comparison for Union Types .................................................................. 116
8.5.3
With/End in Union Type Definitions............................................................................................................. 116
8.5.4
Compiled Form of Union Types for Use from Other CLI Languages ............................................................. 116
8.6
C
LASS
T
YPE
D
EFINITIONS
............................................................................................................................................... 117
8.6.1
Primary Constructors in Classes................................................................................................................... 117
8.6.2
Members in Classes ..................................................................................................................................... 120
8.6.3
Additional Object Constructors in Classes.................................................................................................... 120
8.6.4
Additional Fields in Classes .......................................................................................................................... 122
8.7
I
NTERFACE
T
YPE
D
EFINITIONS
......................................................................................................................................... 123
8.8
S
TRUCT
T
YPE
D
EFINITIONS
............................................................................................................................................. 123
8.9
E
NUM
T
YPE
D
EFINITIONS
.............................................................................................................................................. 125
8.10
D
ELEGATE
T
YPE
D
EFINITIONS
.................................................................................................................................... 126
8.11
E
XCEPTION
D
EFINITIONS
.......................................................................................................................................... 126
8.12
T
YPE
E
XTENSIONS
................................................................................................................................................... 127
8.12.1
Imported CLI C# Extensions Members ......................................................................................................... 128
8.13
M
EMBERS
............................................................................................................................................................ 129
8.13.1
Property Members....................................................................................................................................... 130
8.13.2
Method Members ........................................................................................................................................ 131
8.13.3
Curried Method Members ........................................................................................................................... 131
8.13.4
Named Arguments to Method Members..................................................................................................... 131
8.13.5
Optional Arguments to Method Members .................................................................................................. 132
8.13.6
Type-directed Conversions at Member Invocations..................................................................................... 134
8.13.7
Overloading of Methods .............................................................................................................................. 135
8.13.8
Naming Restrictions for Members ............................................................................................................... 136
8.13.9
Members Represented as Events................................................................................................................. 136
8.13.10
Members Represented as Static Members ............................................................................................. 137
8.14
A
BSTRACT
M
EMBERS AND
I
NTERFACE
I
MPLEMENTATIONS
............................................................................................... 137
8.14.1
Abstract Members ....................................................................................................................................... 138
5
Plik z chomika:
zuchel
Inne pliki z tego folderu:
specyfikacja_Fis.pdf
(5147 KB)
Inne foldery tego chomika:
(lisp)
CEO
CLIPS
DSL
elixir
Zgłoś jeśli
naruszono regulamin