The F# 4.0 Language Specification

Total Page:16

File Type:pdf, Size:1020Kb

The F# 4.0 Language Specification The F# 4.0 Language Specification Note: This documentation is the specification of version 4.0 of the F# language, released in 2015-16. Discrepancies may exist between this specification and the 4.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 grateful for feedback on this specification, and on both the design and implementation of F#. You can submit feedback by opening issues, comments and pull requests at https://github.com/fsharp/fsfoundation/tree/gh-pages/specs/language-spec. The latest version of this specification can be found at fsharp.org. 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, Kevin Ransom, Chris Smith, Matteo Taveggia, Donna Malayeri, Wonseok Chae, Uladzimir Matsveyeu, Lincoln Atkinson, and others. Notice © 2005-2016 various contributors. Made available under the Creative Commons CC-by 4.0 licence. Product and company names mentioned herein may be the trademarks of their respective owners. Document Updates: Updates for F# 4.0, January 2016 Updates for F# 3.1 and type providers, January 2016 Edits to change version numbers for F# 3.1, May 2014 Initial updates for F# 3.1, June 2013 (see online description of language updates) Updated to F# 3.0, September 2012 Updated with formatting changes, April 2012 Updated with grammar summary, December 2011 Updated with glossary, index, and style corrections, February 2011 Updated with glossary, index, and style corrections, August 2010 Table of Contents 1. INTRODUCTION ................................................................................................................. 11 1.1 A FIRST PROGRAM ................................................................................................................... 11 1.1.1 Lightweight Syntax .......................................................................................................... 11 1.1.2 Making Data Simple ........................................................................................................ 12 1.1.3 Making Types Simple ....................................................................................................... 13 1.1.4 Functional Programming ................................................................................................. 14 1.1.5 Imperative Programming ................................................................................................ 15 1.1.6 .NET Interoperability and CLI Fidelity .............................................................................. 16 1.1.7 Parallel and Asynchronous Programming ....................................................................... 16 1.1.8 Strong Typing for Floating-Point Code ............................................................................ 17 1.1.9 Object-Oriented Programming and Code Organization .................................................. 17 1.1.10 Information-rich Programming ................................................................................... 19 1.2 NOTATIONAL CONVENTIONS IN THIS SPECIFICATION ...................................................................... 20 2. PROGRAM STRUCTURE ...................................................................................................... 23 3. LEXICAL ANALYSIS .............................................................................................................. 25 3.1 WHITESPACE ........................................................................................................................... 25 3.2 COMMENTS ............................................................................................................................ 25 3.3 CONDITIONAL COMPILATION ...................................................................................................... 26 3.4 IDENTIFIERS AND KEYWORDS ...................................................................................................... 26 3.5 STRINGS AND CHARACTERS ........................................................................................................ 28 3.6 SYMBOLIC KEYWORDS ............................................................................................................... 30 3.7 SYMBOLIC OPERATORS .............................................................................................................. 31 3.8 NUMERIC LITERALS ................................................................................................................... 31 3.8.1 Post-filtering of Adjacent Prefix Tokens .......................................................................... 32 3.8.2 Post-filtering of Integers Followed by Adjacent “..” ........................................................ 33 3.8.3 Reserved Numeric Literal Forms ...................................................................................... 33 3.8.4 Shebang ........................................................................................................................... 33 3.9 LINE DIRECTIVES ...................................................................................................................... 33 3.10 HIDDEN TOKENS ...................................................................................................................... 33 3.11 IDENTIFIER REPLACEMENTS ........................................................................................................ 34 4. BASIC GRAMMAR ELEMENTS.............................................................................................. 35 4.1 OPERATOR NAMES ................................................................................................................... 35 4.2 LONG IDENTIFIERS .................................................................................................................... 39 4.3 CONSTANTS ............................................................................................................................ 39 4.4 OPERATORS AND PRECEDENCE ................................................................................................... 40 4.4.1 Categorization of Symbolic Operators ............................................................................ 40 4.4.2 Precedence of Symbolic Operators and Pattern/Expression Constructs ......................... 41 5. TYPES AND TYPE CONSTRAINTS .......................................................................................... 43 5.1 CHECKING SYNTACTIC TYPES ...................................................................................................... 44 5.1.1 Named Types ................................................................................................................... 45 5.1.2 Variable Types ................................................................................................................. 45 5.1.3 Tuple Types ...................................................................................................................... 46 5.1.4 Array Types ...................................................................................................................... 46 2 5.1.5 Constrained Types ........................................................................................................... 47 5.2 TYPE CONSTRAINTS .................................................................................................................. 47 5.2.1 Subtype Constraints ........................................................................................................ 47 5.2.2 Nullness Constraints ........................................................................................................ 48 5.2.3 Member Constraints ........................................................................................................ 48 5.2.4 Default Constructor Constraints ...................................................................................... 49 5.2.5 Value Type Constraints .................................................................................................... 49 5.2.6 Reference Type Constraints ............................................................................................. 50 5.2.7 Enumeration Constraints ................................................................................................. 50 5.2.8 Delegate Constraints ....................................................................................................... 50 5.2.9 Unmanaged Constraints .................................................................................................. 51 5.2.10 Equality and Comparison Constraints ......................................................................... 51 5.3 TYPE PARAMETER DEFINITIONS .................................................................................................. 52 5.4 LOGICAL PROPERTIES OF TYPES ................................................................................................... 53 5.4.1 Characteristics of Type Definitions .................................................................................
Recommended publications
  • Comparative Studies of Programming Languages; Course Lecture Notes
    Comparative Studies of Programming Languages, COMP6411 Lecture Notes, Revision 1.9 Joey Paquet Serguei A. Mokhov (Eds.) August 5, 2010 arXiv:1007.2123v6 [cs.PL] 4 Aug 2010 2 Preface Lecture notes for the Comparative Studies of Programming Languages course, COMP6411, taught at the Department of Computer Science and Software Engineering, Faculty of Engineering and Computer Science, Concordia University, Montreal, QC, Canada. These notes include a compiled book of primarily related articles from the Wikipedia, the Free Encyclopedia [24], as well as Comparative Programming Languages book [7] and other resources, including our own. The original notes were compiled by Dr. Paquet [14] 3 4 Contents 1 Brief History and Genealogy of Programming Languages 7 1.1 Introduction . 7 1.1.1 Subreferences . 7 1.2 History . 7 1.2.1 Pre-computer era . 7 1.2.2 Subreferences . 8 1.2.3 Early computer era . 8 1.2.4 Subreferences . 8 1.2.5 Modern/Structured programming languages . 9 1.3 References . 19 2 Programming Paradigms 21 2.1 Introduction . 21 2.2 History . 21 2.2.1 Low-level: binary, assembly . 21 2.2.2 Procedural programming . 22 2.2.3 Object-oriented programming . 23 2.2.4 Declarative programming . 27 3 Program Evaluation 33 3.1 Program analysis and translation phases . 33 3.1.1 Front end . 33 3.1.2 Back end . 34 3.2 Compilation vs. interpretation . 34 3.2.1 Compilation . 34 3.2.2 Interpretation . 36 3.2.3 Subreferences . 37 3.3 Type System . 38 3.3.1 Type checking . 38 3.4 Memory management .
    [Show full text]
  • A Modular Structured Approach Using C++ © Kenneth Leroy Busbee
    Programming Fundamentals -A Modular Structured Approach using C++ © Kenneth Leroy Busbee This work is licensed under a Creative Commons-ShareAlike 4.0 International License Original source: Multimedia Educational Resource for Learning and Online Teaching http://www.merlot.org/merlot/viewMaterial.htm?id=515164 Contents Preface .............................................................................................................................1 Orientation and Syllabus ...............................................................................................2 Chapter 1 Introduction to Programming ....................................................................4 1.1 Systems Development Life Cycle .....................................................................................4 1.1.1 Discussion ................................................................................................................4 1.1.2 Definitions ...............................................................................................................5 1.2 Bloodshed Dev-C++ 5 Compiler/IDE ................................................................................6 1.2.1 Introduction ............................................................................................................6 1.2.2 Bloodshed Dev-C++ 5 compiler/IDE .....................................................................6 1.2.3 Preparation before Installation ............................................................................7 1.2.3.1 Creating the
    [Show full text]
  • The Early History of F
    The Early History of F# DON SYME, Microsoft, United Kingdom Shepherd: Philip Wadler, University of Edinburgh, UK This paper describes the genesis and early history of the F# programming language. I start with the origins of strongly-typed functional programming (FP) in the 1970s, 80s and 90s. During the same period, Microsoft was founded and grew to dominate the software industry. In 1997, as a response to Java, Microsoft initiated internal projects which eventually became the .NET programming framework and the C# language. From 1997 the worlds of academic functional programming and industry combined at Microsoft Research, Cambridge. The researchers engaged with the company through Project 7, the initial effort to bring multiple languages to .NET, leading to the initiation of .NET Generics in 1998 and F# in 2002. F# was one of several responses by advocates of strongly-typed functional programming to the “object-oriented tidal wave” of the mid-1990s. The 75 development of the core features of F# 1.0 happened from 2004-2007, and I describe the decision-making process that led to the “productization” of F# by Microsoft in 2007-10 and the release of F# 2.0. The origins of F#’s characteristic features are covered: object programming, quotations, statically resolved type parameters, active patterns, computation expressions, async, units-of-measure and type providers. I describe key developments in F# since 2010, including F# 3.0-4.5, and its evolution as an open source, cross-platform language with multiple delivery channels. I conclude by examining some uses of F# and the influence F# has had on other languages so far.
    [Show full text]
  • Computer Science (2Nd Year)
    Class XII – Computer Science – Notes Practicals Chapter 01 : Data Basics 1.1 Overview Q : 01-01-01 : Define Data, Information and Explain Operations on Data ? Answer : Data : Data is a collection of facts, figures and statistics - related to an object, that can be processed to produce a meaningful information. Information : The manipulated and processed data is called information e.g., the percentage of students results. It is an output of a certain process. Operations : Manipulation of data (after capturing from different sources) to achieve the required objectives and results. For this purpose, a software (program) is used to process raw data which is converted to meaningful information. These are categorized into three basic activities : Data Capturing : Data must be recorded or captured in some form before it can be processed. Data Manipulation : The following operations may then be performed on the gathered data. Classifying : Organizing data into classes /groups. Items may be assigned predetermined codes, they can be numeric, alphabetic or alphanumeric. Calculations : Arithmetic manipulation of the data. Sorting : Data is arranged in logical sequence (numerically or alphabetically). Summarizing : Masses of data are reduced to a more concise and usable form. Managing The Output Results : Once the data is captured & manipulated it may be : Storing and Retrieval : Data is retained for future reference. Accessing / fetching the stored data and / or information is the Retrieve Activity. Communication and Reproduction : Data may be transferred from one location or operation to another, for further processing. It is sometimes necessary to copy or to make duplicate of data, called Reproduction. 1.2 Traditional File System Q : 01-02-01 : Define and Explain Record and File ? Answer : 1 Record : A collection of related fields (facts about something) treated as a single unit is called a record.
    [Show full text]
  • LECTURE NOTE on PROGRAMMING in “C”
    LECTURE NOTE on PROGRAMMING IN “C” COURSE CODE: MCA 101 By Asst. Professor Mrs Etuari Oram Asst. Prof. Mr Bighnaraj Naik SYLLABUS Module –I C Language Fundamentals. Character set, Identifiers, keyword, data types, Constants and variables, statements, expression, operators, precedence of operators, Input-output, Assignments, control structures decision making and branching. Module -II Arrays, Functions and Strings: Declaration, manipulation and String – handling functions, monolithic vs. Modular programs, user defined vs. standard functions, formal vs. actual arguments, function – category, function prototypes, parameter passing, recursion, and storage classes: auto, extern, global, static. Module –III Pointers, Structures, Unions, File handling: Pointer variable and its importance, pointer arithmetic, passing parameters, Declaration of structures, pointer to pointer, pointer to structure, pointer to function, union, dynamic memory allocation, file managements. 2 *Under revision CONTENTS Module: 1 Lecture 1: Introduction to C Lecture 2: Structure of C, compilation, execution Lecture 3:character set, identifiers, keywords Lecture 4: constants, variables Lecture 5: expression, operators Lecture 6: operators continue… Lecture 7: loops: do while, while Lecture 8: for loop, break, continue statement Lecture 9: control Statements Lecture 10: nesting of if else…, if else ladder Lecture 11: arrays Lecture 12: 2-diamensional array Module: 2 3 *Under revision Lecture 13: String library functions Lecture 14: functions, categories Lecture 15: functions
    [Show full text]
  • Program Structure and Object-Oriented Programming
    Programming Languages Sessions 7 & 8 – Main Theme Program Structure and Object-Oriented Programming Dr. Jean-Claude Franchitti New York University Computer Science Department Courant Institute of Mathematical Sciences Adapted from course textbook resources Programming Language Pragmatics (3rd Edition) Michael L. Scott, Copyright © 2009 Elsevier 1 Agenda 1 Session Overview 2 Program Structure 3 Object-Oriented Programming 4 Conclusion 2 What is the course about? .Course description and syllabus: » http://www.nyu.edu/classes/jcf/CSCI-GA.2110-001 » http://www.cs.nyu.edu/courses/summer14/CSCI-GA.2110- 001/index.html .Textbook: » Programming Language Pragmatics (3rd Edition) Michael L. Scott Morgan Kaufmann ISBN-10: 0-12374-514-4, ISBN-13: 978-0-12374-514-4, (04/06/09) .Additional References: » Osinski, Lecture notes, Summer 2010 » Grimm, Lecture notes, Spring 2010 » Gottlieb, Lecture notes, Fall 2009 » Barrett, Lecture notes, Fall 2008 3 Session Agenda . Session Overview . Program Structure . Object-Oriented Programming . Conclusion 4 Icons / Metaphors Information Common Realization Knowledge/Competency Pattern Governance Alignment Solution Approach 55 Session 6 Review . Data Types » Strong vs. Weak Typing » Static vs. Dynamic Typing . Type Systems » Type Declarations . Type Checking » Type Equivalence » Type Inference » Subtypes and Derived Types . Scalar and Composite Types » Records, Variant Records, Arrays, Strings, Sets . Pointers and References » Pointers and Recursive Types . Function Types . Files and Input / Output . Conclusions 6 Agenda 1 Session Overview 2 Program Structure 3 Object-Oriented Programming 4 Conclusion 7 Program Structure . Key Concepts » Modules » Packages » Interfaces » Abstract types and information hiding . Review Session 2 » Textbook Sections 3.3.4, 3.3.5, 3.7 8 Software Complexity . Tony Hoare: » here are two ways of constructing a software design: one way is to make it so simple that there are obviously no deficiencies, and the other is to make it so complicated that there are no obvious deficiencies.
    [Show full text]
  • What Is ''Object-Oriented Programming''? (1991 Revised
    What is ‘‘Object-Oriented Programming’’? (1991 revised version) Bjarne Stroustrup AT&T Bell Laboratories Murray Hill, New Jersey 07974 ABSTRACT ‘‘Object-Oriented Programming’’ and ‘‘Data Abstraction’’ have become very com- mon terms. Unfortunately, few people agree on what they mean. I will offer informal definitions that appear to make sense in the context of languages like Ada, C++, Modula- 2, Simula, and Smalltalk. The general idea is to equate ‘‘support for data abstraction’’ with the ability to define and use new types and equate ‘‘support for object-oriented pro- gramming’’ with the ability to express type hierarchies. Features necessary to support these programming styles in a general purpose programming language will be discussed. The presentation centers around C++ but is not limited to facilities provided by that lan- guage. 1 Introduction Not all programming languages can be ‘‘object oriented’’. Yet claims have been made to the effect that APL, Ada, Clu, C++, LOOPS, and Smalltalk are object-oriented programming languages. I have heard dis- cussions of object-oriented design in C, Pascal, Modula-2, and CHILL. Could there somewhere be propo- nents of object-oriented Fortran and Cobol programming? I think there must be. ‘‘Object-oriented’’ has in many circles become a high-tech synonym for ‘‘good’’, and when you examine discussions in the trade press, you can find arguments that appear to boil down to syllogisms like: Ada is good Object oriented is good ----------------------------------- Ada is object oriented This paper presents one view of what ‘‘object oriented’’ ought to mean in the context of a general purpose programming language.
    [Show full text]
  • Using GNU Fortran (Gfortran.Pdf)
    Using GNU Fortran For gcc version 4.8.0 (pre-release) (GCC) The gfortran team Published by the Free Software Foundation 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA Copyright c 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with the Invariant Sections being “Funding Free Software”, the Front-Cover Texts being (a) (see below), and with the Back-Cover Texts being (b) (see below). A copy of the license is included in the section entitled “GNU Free Documentation License”. (a) The FSF’s Front-Cover Text is: A GNU Manual (b) The FSF’s Back-Cover Text is: You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development. i Short Contents 1 Introduction ......................................... 1 Invoking GNU Fortran 2 GNU Fortran Command Options ........................ 7 3 Runtime: Influencing runtime behavior with environment variables ........................................... 27 Language Reference 4 Fortran 2003 and 2008 Status .......................... 33 5 Compiler Characteristics .............................. 37 6 Extensions .......................................... 41 7 Mixed-Language Programming ......................... 53 8 Intrinsic Procedures .................................
    [Show full text]
  • Programming Paradigms
    Programming Paradigms (Lectures on High-performance Computing for Economists VII) Jes´usFern´andez-Villaverde,1 Pablo Guerr´on,2 and David Zarruk Valencia3 September 14, 2020 1University of Pennsylvania 2Boston College 3ITAM Programming Approaches Paradigms I • A paradigm is the preferred approach to programming that a language supports. • Main paradigms in scientific computation (many others for other fields): 1. Imperative. 2. Structured. 3. Procedural. 4. Object-Oriented. 5. Functional. 1 Paradigms II • Multi-paradigm languages: C++, recent introduction of λ-calculus features. • Different problems are better suited to different paradigms. • You can always \speak" with an accent. • Idiomatic programming. 2 Imperative, structured, and procedural Imperative • Oldest approach. • Closest to the actual mechanical behavior of a computer) original imperative languages were abstractions of assembly language. • A program is a list of instructions that change a memory state until desired end state is achieved. • Useful for quite simple programs. • Difficult to scale. • Soon it led to spaghetti code. 3 Structured • Go To Statement Considered Harmful, by Edsger Dijkstra in 1968. • Structured program theorem (B¨ohm-Jacopini):sequencing, selection, and iteration are sufficient to express any computable function. • Hence, structured: subroutines/functions, block structures, and loops, and tests. • This is paradigm you are likely to be most familiar with. 4 Procedural • Evolution of structured programming. • Divide the code in procedures: routines, subroutines, modules methods, or functions. • Advantages: 1. Division of work. 2. Debugging and testing. 3. Maintenance. 4. Reusability. 5 OOP Object-oriented programming I • Predecesors in the late 1950s and 1960s in the LISP and Simula communities. • 1970s: Smalltalk from the Xerox PARC.
    [Show full text]
  • Programming Paradigms
    Overview Good code Paradigms Outlook Programming Paradigms Kilian Lieret1,2 Mentors: Sebastien Ponce3, Enric Tejedor3 1Ludwig-Maximilian University 2Excellence Cluster Origins 3CERN 28 September 2020 Kilian Lieret Programming Paradigms 1 / 43 Overview Good code Paradigms Outlook Overview Lecture 1: Programming Paradigms (PPs): Monday 14:15 – 15:25 Lecture 2: Design Patterns (DPs): Tuesday 14:00 – 15:10 Exercise consultation time: Thursday 17:00 – 17:30 All material at: github.com/klieret/icsc-paradigms-and-patterns The goal of this course This course does not try to make you a better programmer But it does convey basic concepts and vocabulary to make your design decisions more consciously Thinking while coding + reflecting your decisions after coding −! Experience −! Great code! Kilian Lieret Programming Paradigms 2 / 43 Overview Good code Paradigms Outlook Programming Paradigms What is a programming paradigm? A classification of programming languages based on their features (but most popular languages support multiple paradigms) A programming style or way programming/thinking Example: Object Oriented Programming (thinking in terms of objects which contain data and code) Many common languages support (to some extent) multiple paradigms (C++, python, …) Why should I care? Discover new ways of thinking −! challenge your current believes about how to code Choose the right paradigm for the right problem or pick the best of many worlds Kilian Lieret Programming Paradigms 3 / 43 Overview Good code Paradigms Outlook Programming Paradigms Some problems Too formal definitions can be hard to grasp and sometimes impractical, too loose definitions can be meaningless Comparing different paradigms requires experience and knowledge in both (if all you [know] is a hammer, everything looks like a nail) A perfect programmer might write great software using any PP My personal approach Rather than asking “How to define paradigm X?”, ask “How would I approach my problems in X?”.
    [Show full text]
  • What Is Object-Oriented Programming?
    What is Object-Oriented Programming9 Bjmne StMStNp,AT&T Bell Laboratories Ohjedenkntedhas ot all programminglanguagescan duce C++and partly because C++is one of be object-oriented. Yet, claims the few languages that supports data ah become a buzzword have been made that APL, Ada, straction, object-oriented programming, that implies “good” Clu, C++,Iaops, and Smalltalk are object- and traditional programming techniques. proghmming But oriented languages. I have heard discus- I do not cover issues of concurrency and sions of object-oriented design in C, Pas- hardware support for specific,higher level when it comes to cal, Modula-2, and Chili. Could there language constructs. really supporting this somewhere be proponents of object- oriented programming in Fortran and Programming paradigms pardighm, not all Cobol? I think there must be. Object-oriented programming is a tech- lameesare equal. “Object-oriented” has become a high- nique - a paradigm for writing “good” tech synonym for “good.” Articles in the programs for a set of problems. If the term trade presscontain arguments that appear “object-oriented language” means any- to boil down to syllogisms like: thing, it must mean a language that has Ada is good; object-oriented is good; mechanisms that support the object- thert$oore, Ada is object+niated. oriented style of programming well. There is an important distinction here: This article presents my view ofwhat ob ject-oriented means in the context of a A language suppurtsa programming style if it provides facilities that make it con- general-purpose programming language. venient (reasonably easy, safe, and efi- I present examples in C++,partly to intre cient) to use that style.
    [Show full text]