ITT8060: Advanced Programming (in F#)

Similar documents
02157 Functional Programming Lecture 1: Introduction and Getting Started

Introduction to SML Getting Started

02157 Functional Programming. Michael R. Ha. Lecture 1: Introduction and Getting Started. Michael R. Hansen

02157 Functional Programming Lecture 2: Functions, Basic Types and Tuples

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen

02157 Functional Programming. Michael R. Ha. Disjoint Unions and Higher-order list functions. Michael R. Hansen

02157 Functional Programming Tagged values and Higher-order list functions

02157 Functional Programming. Michael R. Ha. Tagged values and Higher-order list functions. Michael R. Hansen

CSE 341 Section 5. Winter 2018

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Sequential Search (Searching Supplement: 1-2)

Products and Records

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions

Programming Logic and Design Sixth Edition

F28PL1 Programming Languages. Lecture 11: Standard ML 1

Arrays. What if you have a 1000 line file? Arrays

Lists. Michael P. Fourman. February 2, 2010

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Why OCaml? Introduction to Objective Caml. Features of OCaml. Applications written in OCaml. Stefan Wehr

Introduction to Objective Caml

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Lecture #23: Conversion and Type Inference

SML A F unctional Functional Language Language Lecture 19

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Arrays. Arrays (8.1) Arrays. One variable that can store a group of values of the same type. Storing a number of related values.

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

A Brief Introduction to Standard ML

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

02157 Functional Programming Lecture 3: Lists

Example. Section: PS 709 Examples of Calculations of Reduced Hours of Work Last Revised: February 2017 Last Reviewed: February 2017 Next Review:

Programming Languages

Mini-ML. CS 502 Lecture 2 8/28/08

A Beginner s Guide to Programming Logic, Introductory. Chapter 6 Arrays

CSE 130, Winter 2011: Final Examination

COMPUTER SCIENCE 123. Foundations of Computer Science. 5. Strings

Introduction to OCaml

CSCI-GA Scripting Languages

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Introduction to OCaml

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Overloading, Type Classes, and Algebraic Datatypes

Fall 2018 Stephen Brookes. LECTURE 2 Thursday, August 30

Handout 2 August 25, 2008

CMSC 330, Fall 2013, Practice Problems 3

Programming Paradigms Languages F28PL, Lecture 1

Read and fill in this page now. Your lab section day and time: Name of the person sitting to your left: Name of the person sitting to your right:

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Programming, Problem Solving, and Abstraction. Chapter Three. Selection

Index. Cambridge University Press Functional Programming Using F# Michael R. Hansen and Hans Rischel. Index.

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

A general introduction to Functional Programming using Haskell

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Typed Scheme: Scheme with Static Types

Lecture 2: The Basis of SML

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

Functional Programming in Haskell Part I : Basics

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

An introduction to functional programming. July 23, 2010

NOTE: Answer ANY FOUR of the following 6 sections:

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Hands-On Lab. Introduction to F# Lab version: Last updated: 12/10/2010. Page 1

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

A quick introduction to SML

CS 11 Ocaml track: lecture 2

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Programmazione Avanzata

Programming language seminar The F# language. Peter Sestoft Wednesday Course plan

Begin at the beginning

Fall Lecture 3 September 4. Stephen Brookes

Programming Systems in Artificial Intelligence Functional Programming

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

Do not turn to the next page until the start of the exam.

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

C-LANGUAGE CURRICULAM

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

egrapher Language Reference Manual

7. Introduction to Denotational Semantics. Oscar Nierstrasz

Programming Languages

CSE 3302 Programming Languages Lecture 8: Functional Programming

This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator

Functional Programming. Pure Functional Programming

Metaprogramming assignment 3

Part I. Language Core

CONDITIONAL EXECUTION: PART 2

Different kinds of resources provided by a package

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Introduction to the Objective Caml Programming Language. Jason Hickey

Lecture 19: Functions, Types and Data Structures in Haskell

Principles of Functional Programming

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

CS111: PROGRAMMING LANGUAGE II

Programming Paradigms

Programming in Standard ML: Continued

Data types for mcrl2

Typed Racket: Racket with Static Types

CS Programming I: Arrays

Transcription:

based on slides by Michael R. Hansen ITT8060: Advanced Programming (in F#) Lecture 2: Identifiers, values, expressions, functions and types Juhan Ernits Department of Software Science, Tallinn University of Technology 13/09/2017

A major goal Teach abstraction (not a concrete programming language) Why? How? Modelling Design Programming More complex problems can be solved in an succinct, elegant and understandable manner Solving a broad class of problems showing the applicability of the theory, concepts, techniques and tools. Functional programming techniques once mastered are useful for the design of programs in other programming paradigms as well.

A major goal Teach abstraction (not a concrete programming language) Why? How? Modelling Design Programming More complex problems can be solved in an succinct, elegant and understandable manner Solving a broad class of problems showing the applicability of the theory, concepts, techniques and tools. Functional programming techniques once mastered are useful for the design of programs in other programming paradigms as well.

A major goal Teach abstraction (not a concrete programming language) Why? How? Modelling Design Programming More complex problems can be solved in an succinct, elegant and understandable manner Solving a broad class of problems showing the applicability of the theory, concepts, techniques and tools. Functional programming techniques once mastered are useful for the design of programs in other programming paradigms as well.

A major goal Teach abstraction (not a concrete programming language) Why? How? Modelling Design Programming More complex problems can be solved in an succinct, elegant and understandable manner Solving a broad class of problems showing the applicability of the theory, concepts, techniques and tools. Functional programming techniques once mastered are useful for the design of programs in other programming paradigms as well.

F# supports Functions as first class citizens Structured values like lists, trees,... Strong and flexible type discipline, including type inference and polymorphism Imperative and object-oriented programming assignments, loops, arrays, objects, Input/Output, etc. Programming as a modelling discipline High-level programming, declarative programming, executable declarative specifications B, Z, VDM, RAISE Fast time-to-market

F# supports Functions as first class citizens Structured values like lists, trees,... Strong and flexible type discipline, including type inference and polymorphism Imperative and object-oriented programming assignments, loops, arrays, objects, Input/Output, etc. Programming as a modelling discipline High-level programming, declarative programming, executable declarative specifications B, Z, VDM, RAISE Fast time-to-market

F# supports Functions as first class citizens Structured values like lists, trees,... Strong and flexible type discipline, including type inference and polymorphism Imperative and object-oriented programming assignments, loops, arrays, objects, Input/Output, etc. Programming as a modelling discipline High-level programming, declarative programming, executable declarative specifications B, Z, VDM, RAISE Fast time-to-market

F# supports Functions as first class citizens Structured values like lists, trees,... Strong and flexible type discipline, including type inference and polymorphism Imperative and object-oriented programming assignments, loops, arrays, objects, Input/Output, etc. Programming as a modelling discipline High-level programming, declarative programming, executable declarative specifications B, Z, VDM, RAISE Fast time-to-market

F# supports Functions as first class citizens Structured values like lists, trees,... Strong and flexible type discipline, including type inference and polymorphism Imperative and object-oriented programming assignments, loops, arrays, objects, Input/Output, etc. Programming as a modelling discipline High-level programming, declarative programming, executable declarative specifications B, Z, VDM, RAISE Fast time-to-market

Overview of Getting Started Main functional ingredients of F#: The interactive environment Values, expressions, types, patterns Declarations of values and recursive functions Binding, environment and evaluation Type inference

The Interactive Environment 2*3 + 4;; val it : int = 10 Input to the F# system Answer from the F# system The keyword val indicates a value is computed The integer 10 is the computed value int is the type of the computed value The identifier it names the (last) computed value The notion binding explains which entities are named by identifiers. it 10 reads: it is bound to 10

The Interactive Environment 2*3 + 4;; val it : int = 10 Input to the F# system Answer from the F# system The keyword val indicates a value is computed The integer 10 is the computed value int is the type of the computed value The identifier it names the (last) computed value The notion binding explains which entities are named by identifiers. it 10 reads: it is bound to 10

The Interactive Environment 2*3 + 4;; val it : int = 10 Input to the F# system Answer from the F# system The keyword val indicates a value is computed The integer 10 is the computed value int is the type of the computed value The identifier it names the (last) computed value The notion binding explains which entities are named by identifiers. it 10 reads: it is bound to 10

The Interactive Environment 2*3 + 4;; val it : int = 10 Input to the F# system Answer from the F# system The keyword val indicates a value is computed The integer 10 is the computed value int is the type of the computed value The identifier it names the (last) computed value The notion binding explains which entities are named by identifiers. it 10 reads: it is bound to 10

Value Declarations A value declaration has the form: let identifier = expression let price = 25 * 5;; val price : int = 125 A declaration as input Answer from the F# system The effect of a declaration is a binding: price 125 Bound identifiers can be used in expressions and declarations, e.g. let newprice = 2*price;; val newprice : int = 250 newprice > 500;; val it : bool = false A collection of bindings price 125 newprice 250 it false is called an environment

Value Declarations A value declaration has the form: let identifier = expression let price = 25 * 5;; val price : int = 125 A declaration as input Answer from the F# system The effect of a declaration is a binding: price 125 Bound identifiers can be used in expressions and declarations, e.g. let newprice = 2*price;; val newprice : int = 250 newprice > 500;; val it : bool = false A collection of bindings price 125 newprice 250 it false is called an environment

Value Declarations A value declaration has the form: let identifier = expression let price = 25 * 5;; val price : int = 125 A declaration as input Answer from the F# system The effect of a declaration is a binding: price 125 Bound identifiers can be used in expressions and declarations, e.g. let newprice = 2*price;; val newprice : int = 250 newprice > 500;; val it : bool = false A collection of bindings price 125 newprice 250 it false is called an environment

Function Declarations 1: let f x = e Declaration of the circle area function: let circlearea r = System.Math.PI * r * r;; System.Math is a program library PI is an identifier (with type float) for π in System.Math The type is automatically inferred in the answer: val circlearea : float -> float Applications of the function: circlearea 1.0;; (* this is a comment *) val it : float = 3.141592654 circlearea(3.2);; // A comment: optional brackets val it : float = 32.16990877

Function Declarations 1: let f x = e Declaration of the circle area function: let circlearea r = System.Math.PI * r * r;; System.Math is a program library PI is an identifier (with type float) for π in System.Math The type is automatically inferred in the answer: val circlearea : float -> float Applications of the function: circlearea 1.0;; (* this is a comment *) val it : float = 3.141592654 circlearea(3.2);; // A comment: optional brackets val it : float = 32.16990877

Function Declarations 1: let f x = e Declaration of the circle area function: let circlearea r = System.Math.PI * r * r;; System.Math is a program library PI is an identifier (with type float) for π in System.Math The type is automatically inferred in the answer: val circlearea : float -> float Applications of the function: circlearea 1.0;; (* this is a comment *) val it : float = 3.141592654 circlearea(3.2);; // A comment: optional brackets val it : float = 32.16990877

Anonymous functions: by example (1) An anonymous function computing the number of days in a month: fun x -> match x with 1 -> 31 // January 2 -> 28 // February // not a leap year 3 -> 31 // March 4 -> 30 // April 5 -> 31 // May 6 -> 30 // June 7 -> 31 // July 8 -> 31 // August 9 -> 30 // September 10 -> 31 // October 11 -> 30 // November 12 -> 31;;// December... warning... Incomplete pattern matches... val it : int -> int = <fun:clo@17-2> it 2;; val it : int = 28 A function expression with a pattern for every month

Anonymous functions: by example (1) An anonymous function computing the number of days in a month: fun x -> match x with 1 -> 31 // January 2 -> 28 // February // not a leap year 3 -> 31 // March 4 -> 30 // April 5 -> 31 // May 6 -> 30 // June 7 -> 31 // July 8 -> 31 // August 9 -> 30 // September 10 -> 31 // October 11 -> 30 // November 12 -> 31;;// December... warning... Incomplete pattern matches... val it : int -> int = <fun:clo@17-2> it 2;; val it : int = 28 A function expression with a pattern for every month

Anonymous functions: by example (1) An anonymous function computing the number of days in a month: fun x -> match x with 1 -> 31 // January 2 -> 28 // February // not a leap year 3 -> 31 // March 4 -> 30 // April 5 -> 31 // May 6 -> 30 // June 7 -> 31 // July 8 -> 31 // August 9 -> 30 // September 10 -> 31 // October 11 -> 30 // November 12 -> 31;;// December... warning... Incomplete pattern matches... val it : int -> int = <fun:clo@17-2> it 2;; val it : int = 28 A function expression with a pattern for every month

Anonymous functions: by example (2) One wildcard pattern can cover many similar cases: fun x -> match x with 2 -> 28 // February 4 -> 30 // April 6 -> 30 // June 9 -> 30 // September 11 -> 30 // November _ -> 31;;// All other months An even more succinct definition can be given using an or-pattern: fun x -> match x with 2 -> 28 // February 4 6 9 11 -> 30 // April, June, September, November _ -> 31 // All other months ;;

Anonymous functions: by example (2) One wildcard pattern can cover many similar cases: fun x -> match x with 2 -> 28 // February 4 -> 30 // April 6 -> 30 // June 9 -> 30 // September 11 -> 30 // November _ -> 31;;// All other months An even more succinct definition can be given using an or-pattern: fun x -> match x with 2 -> 28 // February 4 6 9 11 -> 30 // April, June, September, November _ -> 31 // All other months ;;

Recursion. Example n! = 1 2... n, n 0 Mathematical definition: 0! = 1 (i) n! = n (n 1)!, for n > 0 (ii) recursion formula Computation: 3! = 3 (3 1)! (ii) = 3 2 (2 1)! (ii) = 3 2 1 (1 1)! (ii) = 3 2 1 1 (i) = 6

Recursion. Example n! = 1 2... n, n 0 Mathematical definition: 0! = 1 (i) n! = n (n 1)!, for n > 0 (ii) recursion formula Computation: 3! = 3 (3 1)! (ii) = 3 2 (2 1)! (ii) = 3 2 1 (1 1)! (ii) = 3 2 1 1 (i) = 6

Recursive declaration. Example n! Function declaration: let rec fact x = match x with 0 -> 1 (* i *) n -> n * fact(n-1);; (* ii *) val fact : int -> int Evaluation: fact(3) 3 fact(3 1) (ii) [n 3] 3 2 fact(2 1) (ii) [n 2] 3 2 1 fact(1 1) (ii) [n 1] 3 2 1 1 (i) [n 0] 6 e 1 e 2 reads: e 1 evaluates to e 2

Recursive declaration. Example n! Function declaration: let rec fact x = match x with 0 -> 1 (* i *) n -> n * fact(n-1);; (* ii *) val fact : int -> int Evaluation: fact(3) 3 fact(3 1) (ii) [n 3] 3 2 fact(2 1) (ii) [n 2] 3 2 1 fact(1 1) (ii) [n 1] 3 2 1 1 (i) [n 0] 6 e 1 e 2 reads: e 1 evaluates to e 2

Recursive declaration. Example n! Function declaration: let rec fact x = match x with 0 -> 1 (* i *) n -> n * fact(n-1);; (* ii *) val fact : int -> int Evaluation: fact(3) 3 fact(3 1) (ii) [n 3] 3 2 fact(2 1) (ii) [n 2] 3 2 1 fact(1 1) (ii) [n 1] 3 2 1 1 (i) [n 0] 6 e 1 e 2 reads: e 1 evaluates to e 2

Recursive declaration. Example n! Function declaration: let rec fact x = match x with 0 -> 1 (* i *) n -> n * fact(n-1);; (* ii *) val fact : int -> int Evaluation: fact(3) 3 fact(3 1) (ii) [n 3] 3 2 fact(2 1) (ii) [n 2] 3 2 1 fact(1 1) (ii) [n 1] 3 2 1 1 (i) [n 0] 6 e 1 e 2 reads: e 1 evaluates to e 2

Recursive declaration. Example n! Function declaration: let rec fact x = match x with 0 -> 1 (* i *) n -> n * fact(n-1);; (* ii *) val fact : int -> int Evaluation: fact(3) 3 fact(3 1) (ii) [n 3] 3 2 fact(2 1) (ii) [n 2] 3 2 1 fact(1 1) (ii) [n 1] 3 2 1 1 (i) [n 0] 6 e 1 e 2 reads: e 1 evaluates to e 2

Recursive declaration. Example n! Function declaration: let rec fact x = match x with 0 -> 1 (* i *) n -> n * fact(n-1);; (* ii *) val fact : int -> int Evaluation: fact(3) 3 fact(3 1) (ii) [n 3] 3 2 fact(2 1) (ii) [n 2] 3 2 1 fact(1 1) (ii) [n 1] 3 2 1 1 (i) [n 0] 6 e 1 e 2 reads: e 1 evaluates to e 2

Recursion. Example x n = x... x, n occurrences of x Mathematical definition: x 0 = 1 (1) x n = x x n 1, for n > 0 (2) recursion formula Function declaration: let rec power (x,n) = match x,n with (,0) -> 1.0 (* 1 *) (x,n) -> x * power(x,n-1) (* 2 *) Patterns: (, 0) matches any pair of the form (x, 0). The wildcard pattern matches any value. (x, n) matches any pair (u, i) yielding the bindings x u, n i

Recursion. Example x n = x... x, n occurrences of x Mathematical definition: x 0 = 1 (1) x n = x x n 1, for n > 0 (2) recursion formula Function declaration: let rec power (x,n) = match x,n with (,0) -> 1.0 (* 1 *) (x,n) -> x * power(x,n-1) (* 2 *) Patterns: (, 0) matches any pair of the form (x, 0). The wildcard pattern matches any value. (x, n) matches any pair (u, i) yielding the bindings x u, n i

Recursion. Example x n = x... x, n occurrences of x Mathematical definition: x 0 = 1 (1) x n = x x n 1, for n > 0 (2) recursion formula Function declaration: let rec power (x,n) = match x,n with (,0) -> 1.0 (* 1 *) (x,n) -> x * power(x,n-1) (* 2 *) Patterns: (, 0) matches any pair of the form (x, 0). The wildcard pattern matches any value. (x, n) matches any pair (u, i) yielding the bindings x u, n i

Evaluation. Example: power(4.0, 2) Function declaration: let rec power (x,n) = match x,n with (,0) -> 1.0 (* 1 *) (x,n) -> x * power(x,n-1) (* 2 *) Evaluation: power(4.0, 2) 4.0 power(4.0, 2 1) Clause 2, [x 4.0, n 2] 4.0 power(4.0, 1) 4.0 (4.0 power(4.0, 1 1)) Clause 2, [x 4.0, n 1] 4.0 (4.0 power(4.0, 0)) 4.0 (4.0 1) Clause 1 16.0

If-then-else expressions Form: if b then e 1 else e 2 Evaluation rules: if true then e 1 else e 2 e 1 if false then e 1 else e 2 e 2 Alternative declarations: let rec fact n = if n=0 then 1 else n * fact(n-1); let rec power(x,n) = if n=0 then 1.0 else x * power(x,n-1); Use of patterns usually gives more understandable programs

If-then-else expressions Form: if b then e 1 else e 2 Evaluation rules: if true then e 1 else e 2 e 1 if false then e 1 else e 2 e 2 Alternative declarations: let rec fact n = if n=0 then 1 else n * fact(n-1); let rec power(x,n) = if n=0 then 1.0 else x * power(x,n-1); Use of patterns usually gives more understandable programs

If-then-else expressions Form: if b then e 1 else e 2 Evaluation rules: if true then e 1 else e 2 e 1 if false then e 1 else e 2 e 2 Alternative declarations: let rec fact n = if n=0 then 1 else n * fact(n-1); let rec power(x,n) = if n=0 then 1.0 else x * power(x,n-1); Use of patterns usually gives more understandable programs

Booleans Type name bool Values false, true Operator Type not bool -> bool negation not true = false not false = true Expressions e 1 && e 2 conjunction e 1 e 2 e 1 e 2 disjunction e 1 e 2 are lazily evaluated, e.g. 1<2 5/0 = 1 true Precedence: && has higher than

Booleans Type name bool Values false, true Operator Type not bool -> bool negation not true = false not false = true Expressions e 1 && e 2 conjunction e 1 e 2 e 1 e 2 disjunction e 1 e 2 are lazily evaluated, e.g. 1<2 5/0 = 1 true Precedence: && has higher than

Booleans Type name bool Values false, true Operator Type not bool -> bool negation not true = false not false = true Expressions e 1 && e 2 conjunction e 1 e 2 e 1 e 2 disjunction e 1 e 2 are lazily evaluated, e.g. 1<2 5/0 = 1 true Precedence: && has higher than

Strings Type name string Values "abcd", " ", "", "123\"321" (escape sequence for ") Operator Type String.length string -> int length of string + string*string -> string concatenation = < <=... string*string -> bool comparisons string obj -> string conversions Examples - "auto" < "car"; > val it = true : bool - "abc"+"de"; > val it = "abcde": string - String.length("abc"ˆ"def"); > val it = 6 : int - string(6+18); > val it = "24": string

Strings Type name string Values "abcd", " ", "", "123\"321" (escape sequence for ") Operator Type String.length string -> int length of string + string*string -> string concatenation = < <=... string*string -> bool comparisons string obj -> string conversions Examples - "auto" < "car"; > val it = true : bool - "abc"+"de"; > val it = "abcde": string - String.length("abc"ˆ"def"); > val it = 6 : int - string(6+18); > val it = "24": string

Strings Type name string Values "abcd", " ", "", "123\"321" (escape sequence for ") Operator Type String.length string -> int length of string + string*string -> string concatenation = < <=... string*string -> bool comparisons string obj -> string conversions Examples - "auto" < "car"; > val it = true : bool - "abc"+"de"; > val it = "abcde": string - String.length("abc"ˆ"def"); > val it = 6 : int - string(6+18); > val it = "24": string

Types every expression has a type e : τ Basic types: type name example of values Integers int 27, 0, 15, 21000 Floats float 27.3, 0.0, 48.21 Booleans bool true, false Pairs: If e 1 : τ 1 and e 2 : τ 2 then (e 1, e 2 ) : τ 1 τ 2 Functions: if f : τ 1 -> τ 2 and a : τ 1 then f (a) : τ 2 pair (tuple) type constructor function type constructor Examples: (4.0, 2): float*int power: float*int -> float power(4.0, 2): float * has higher precedence that ->

Types every expression has a type e : τ Basic types: type name example of values Integers int 27, 0, 15, 21000 Floats float 27.3, 0.0, 48.21 Booleans bool true, false Pairs: If e 1 : τ 1 and e 2 : τ 2 then (e 1, e 2 ) : τ 1 τ 2 Functions: if f : τ 1 -> τ 2 and a : τ 1 then f (a) : τ 2 pair (tuple) type constructor function type constructor Examples: (4.0, 2): float*int power: float*int -> float power(4.0, 2): float * has higher precedence that ->

Types every expression has a type e : τ Basic types: type name example of values Integers int 27, 0, 15, 21000 Floats float 27.3, 0.0, 48.21 Booleans bool true, false Pairs: If e 1 : τ 1 and e 2 : τ 2 then (e 1, e 2 ) : τ 1 τ 2 Functions: if f : τ 1 -> τ 2 and a : τ 1 then f (a) : τ 2 pair (tuple) type constructor function type constructor Examples: (4.0, 2): float*int power: float*int -> float power(4.0, 2): float * has higher precedence that ->

Types every expression has a type e : τ Basic types: type name example of values Integers int 27, 0, 15, 21000 Floats float 27.3, 0.0, 48.21 Booleans bool true, false Pairs: If e 1 : τ 1 and e 2 : τ 2 then (e 1, e 2 ) : τ 1 τ 2 Functions: if f : τ 1 -> τ 2 and a : τ 1 then f (a) : τ 2 pair (tuple) type constructor function type constructor Examples: (4.0, 2): float*int power: float*int -> float power(4.0, 2): float * has higher precedence that ->

Type inference: power let rec power = function (_,0) -> 1.0 (* 1 *) (x,n) -> x * power(x,n-1) (* 2 *) The type of the function must have the form: τ 1 * τ 2 -> τ 3, because argument is a pair. τ 3 = float because 1.0:float (Clause 1, function value.) τ 2 = int because 0:int. x*power(x,n-1):float, because τ 3 = float. multiplication can have int*int -> int or float*float -> float as types, but no mixture of int and float Therefore x:float and τ 1 =float. The F# system determines the type float*int -> float

Type inference: power let rec power = function (_,0) -> 1.0 (* 1 *) (x,n) -> x * power(x,n-1) (* 2 *) The type of the function must have the form: τ 1 * τ 2 -> τ 3, because argument is a pair. τ 3 = float because 1.0:float (Clause 1, function value.) τ 2 = int because 0:int. x*power(x,n-1):float, because τ 3 = float. multiplication can have int*int -> int or float*float -> float as types, but no mixture of int and float Therefore x:float and τ 1 =float. The F# system determines the type float*int -> float

Type inference: power let rec power = function (_,0) -> 1.0 (* 1 *) (x,n) -> x * power(x,n-1) (* 2 *) The type of the function must have the form: τ 1 * τ 2 -> τ 3, because argument is a pair. τ 3 = float because 1.0:float (Clause 1, function value.) τ 2 = int because 0:int. x*power(x,n-1):float, because τ 3 = float. multiplication can have int*int -> int or float*float -> float as types, but no mixture of int and float Therefore x:float and τ 1 =float. The F# system determines the type float*int -> float

Type inference: power let rec power = function (_,0) -> 1.0 (* 1 *) (x,n) -> x * power(x,n-1) (* 2 *) The type of the function must have the form: τ 1 * τ 2 -> τ 3, because argument is a pair. τ 3 = float because 1.0:float (Clause 1, function value.) τ 2 = int because 0:int. x*power(x,n-1):float, because τ 3 = float. multiplication can have int*int -> int or float*float -> float as types, but no mixture of int and float Therefore x:float and τ 1 =float. The F# system determines the type float*int -> float

Type inference: power let rec power = function (_,0) -> 1.0 (* 1 *) (x,n) -> x * power(x,n-1) (* 2 *) The type of the function must have the form: τ 1 * τ 2 -> τ 3, because argument is a pair. τ 3 = float because 1.0:float (Clause 1, function value.) τ 2 = int because 0:int. x*power(x,n-1):float, because τ 3 = float. multiplication can have int*int -> int or float*float -> float as types, but no mixture of int and float Therefore x:float and τ 1 =float. The F# system determines the type float*int -> float

Type inference: power let rec power = function (_,0) -> 1.0 (* 1 *) (x,n) -> x * power(x,n-1) (* 2 *) The type of the function must have the form: τ 1 * τ 2 -> τ 3, because argument is a pair. τ 3 = float because 1.0:float (Clause 1, function value.) τ 2 = int because 0:int. x*power(x,n-1):float, because τ 3 = float. multiplication can have int*int -> int or float*float -> float as types, but no mixture of int and float Therefore x:float and τ 1 =float. The F# system determines the type float*int -> float

Type inference: power let rec power = function (_,0) -> 1.0 (* 1 *) (x,n) -> x * power(x,n-1) (* 2 *) The type of the function must have the form: τ 1 * τ 2 -> τ 3, because argument is a pair. τ 3 = float because 1.0:float (Clause 1, function value.) τ 2 = int because 0:int. x*power(x,n-1):float, because τ 3 = float. multiplication can have int*int -> int or float*float -> float as types, but no mixture of int and float Therefore x:float and τ 1 =float. The F# system determines the type float*int -> float

Type inference: power let rec power = function (_,0) -> 1.0 (* 1 *) (x,n) -> x * power(x,n-1) (* 2 *) The type of the function must have the form: τ 1 * τ 2 -> τ 3, because argument is a pair. τ 3 = float because 1.0:float (Clause 1, function value.) τ 2 = int because 0:int. x*power(x,n-1):float, because τ 3 = float. multiplication can have int*int -> int or float*float -> float as types, but no mixture of int and float Therefore x:float and τ 1 =float. The F# system determines the type float*int -> float

Summary The interactive environment Values, expressions, types, patterns Declarations of values and recursive functions Binding, environment and evaluation Type inference Breath first round through many concepts aiming at program construction from the first day. We will go deeper into each of the concepts later in the course.

Summary The interactive environment Values, expressions, types, patterns Declarations of values and recursive functions Binding, environment and evaluation Type inference Breath first round through many concepts aiming at program construction from the first day. We will go deeper into each of the concepts later in the course.