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

Similar documents
CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Functions. Nate Foster Spring 2018

Introduction to OCaml

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

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

CMSC 330: Organization of Programming Languages. Rust Basics

CMSC 330: Organization of Programming Languages. OCaml Data Types

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

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

CMSC 330: Organization of Programming Languages

CS 11 Ocaml track: lecture 2

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

To figure this out we need a more precise understanding of how ML works

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Lecture 2: Big-Step Semantics

Introduction to Functional Programming in Haskell 1 / 56

The Substitution Model. Nate Foster Spring 2018

CMSC 330: Organization of Programming Languages. Closures (Implementing Higher Order Functions)

CMSC 330: Organization of Programming Languages. Closures (Implementing Higher Order Functions)

CSE 341: Programming Languages

To figure this out we need a more precise understanding of how ML works

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

CSE 341: Programming Languages

The Substitution Model

An introduction introduction to functional functional programming programming using usin Haskell

Data Types. Guest Lecture: Andrew Myers Spring 2018

COS 320. Compiling Techniques

CS558 Programming Languages

Introduction to OCaml

CMSC 330: Organization of Programming Languages. Objects and Abstract Data Types

CS Lecture 7: The dynamic environment. Prof. Clarkson Spring Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Begin at the beginning

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

Programming Languages and Techniques (CIS120)

Typed Racket: Racket with Static Types

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions. Dan Grossman Spring 2013

Introduction to Objective Caml

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

CS Lecture 6: Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

CS153: Compilers Lecture 14: Type Checking

Programming Languages

Lecture #23: Conversion and Type Inference

CS 11 Haskell track: lecture 1

Data Types. Prof. Clarkson Fall Today s music: Pokémon Theme by Jason Paige

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

CS Lecture 5: Pattern Matching. Prof. Clarkson Fall Today s music: Puff, the Magic Dragon by Peter, Paul & Mary

CMSC 330: Organization of Programming Languages. Basic OCaml Modules

Anonymous Functions CMSC 330: Organization of Programming Languages

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Lecture 13 CIS 341: COMPILERS

Concepts of Programming Languages

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

OCaml Style Guide Fall 2017

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages

CIS 120 Midterm I February 16, 2015 SOLUTIONS

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

F28PL1 Programming Languages. Lecture 12: Standard ML 2

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

CS1 Lecture 3 Jan. 18, 2019

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

CMSC 330: Organization of Programming Languages

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

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

The Typed Racket Guide

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Lecture Notes on Program Equivalence

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

CS1 Lecture 3 Jan. 22, 2018

CS Lectures 2-3. Introduction to OCaml. Polyvios Pratikakis

CMSC 330: Organization of Programming Languages

The Environment Model. Nate Foster Spring 2018

Reasoning About Imperative Programs. COS 441 Slides 10

CSCI 2041: Basic OCaml Syntax and Features

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

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

CMSC 330: Organization of Programming Languages. Operational Semantics

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Transcription:

CMSC 330: Organization of Programming Languages OCaml Expressions and Functions CMSC330 Spring 2018 1

Lecture Presentation Style Our focus: semantics and idioms for OCaml Semantics is what the language does Idioms are ways to use the language well We will also cover some useful libraries Syntax is what you type, not what you mean In one lang: Different syntax for similar concepts Across langs: Same syntax for different concepts Syntax can be a source of fierce disagreement among language designers! 2

Expressions Expressions are our primary building block Akin to statements in imperative languages Every kind of expression has Syntax We use metavariable e to designate an arbitrary expression Semantics Type checking rules (static semantics): produce a type or fail with an error message Evaluation rules (dynamic semantics): produce a value (or an exception or infinite loop) Used only on expressions that type-check 3

Values A value is an expression that is final Evaluating an expression means running it until it becomes a value We use metavariable v to designate an arbitrary value 34 is a value, true is a value 34+17 is an expression, but not a value It evaluates to 51 expressions values 4

Types Types classify expressions Characterize the set of possible values an expression could evaluate to We use metavariable t to designate an arbitrary type Examples include int, bool, string, and more. Expression e has type t if e will (always) evaluate to a value of type t {, -1, 0, 1, } are values of type int 34+17 is an expression of type int, since it evaluates to 51, which has type int Write e : t to say e has type t 5

If Expressions Syntax if e1 then e2 else e3 Evaluation If e1 evaluates to true, and if e2 evaluates to v, then if e1 then e2 else e3 evaluates to v If e1 evaluates to false, and if e3 evaluates to v, then if e1 then e2 else e3 evaluates to v Type checking If e1 has type bool and e2 has type t and e3 has type t then if e1 then e2 else e3 has type t 6

If Expressions Syntax if e1 then e2 else e3 Evaluation If e1 evaluates to true, and if e2 evaluates to v, then if e1 then e2 else e3 evaluates to v If e1 evaluates to false, and if e3 evaluates to v, then if e1 then e2 else e3 evaluates to v Type checking If e1 : bool and e2 : t and e3 : t then if e1 then e2 else e3 : t 7

If Expressions Syntax if e1 then e2 else e3 Evaluation If e1 evaluates to true, and if e2 evaluates to v, then if e1 then e2 else e3 evaluates to v If e1 evaluates to false, and if e3 evaluates to v, then if e1 then e2 else e3 evaluates to v Type checking If e1 : bool and e2 : t and e3 : t then (if e1 then e2 else e3) : t 8

If Expressions: Examples # if 7 > 42 then "hello" else goodbye";; - : string = "goodbye" # if true then 3 else 4;; - : int = 3 # if false then 3 else 3.0;; This expression has type float but is here used with type int 9

Quiz 1 To what value does this expression evaluate? if 22=0 then 1 else 2 A. 0 B. 1 C. 2 D. none of the above 10

Quiz 1 To what value does this expression evaluate? if 22=0 then 1 else 2 A. 0 B. 1 C. 2 D. none of the above 11

Quiz 2 To what value does this expression evaluate? if 22=0 then bear else 2 A. 0 B. 1 C. 2 D. none of the above 12

Quiz 2 To what value does this expression evaluate? if 22=0 then bear else 2 A. 0 B. 1 C. 2 D. none of the above: doesn t type check so never gets a chance to be evaluated 13

Function Definitions OCaml functions are like mathematical functions Compute a result from provided arguments (* requires n>=0 *) (* returns: n! *) let rec fact n = if n = 0 then 1 else n * fact (n-1) function body Use (* *) for comments (may nest) Parameter (type inferred) rec needed for recursion Structural equality Line breaks, spacing ignored (like C, C++, Java, not like Ruby) 14

Function Types In OCaml, -> is the function type constructor Type t1 -> t is a function with argument or domain type t1 and return or range type t Type t1 -> t2 -> t is a function that takes two inputs, of types t1 and t2, and returns a value of type t. Etc. Examples let next x = x + 1 (* type int -> int *) let fn x = (int_of_float x) * 3 fact (* type float -> int *) (* type int -> int *) 15

Type Checking Functions Syntax let rec f x1 xn = e Type checking Conclude that f : t1 -> -> tn -> u if e : u under the following assumptions: x1 : t1,, xn : tn (arguments with their types) Example f : t1 -> -> tn -> u (for recursion) Given n : int, fact : int -> int Does if n = 0 then 1 : int? It does! Conclude fact : int -> int let rec fact n = if n = 0 then 1 else n * fact (n-1) 16

Calling Functions Syntax f e1 en Parentheses not required around argument(s) No commas; use spaces instead Type checking If f : t1 -> -> tn -> u and e1 : t1,, en : tn then f e1 en : u Example: fact 1 : int since fact : int -> int and 1 : int Function call aka function application 17

Calling Functions Syntax f e1 en Evaluation Evaluate arguments e1 en to values v1 vn Order is actually right to left, not left to right But this doesn t matter if e1 en don t have side effects Find the definition of f let rec f x1 xn = e Substitute vi for xi in e, yielding new expression e Evaluate e to value v, which is the final result 18

Calling Functions Example evaluation fact 2 Ø if 2=0 then 1 else 2*fact(2-1) Ø 2 * fact 1 Ø 2 * (if 1=0 then 1 else 1*fact(1-1)) Ø 2 * 1 * fact 0 Ø 2 * 1 * (if 0=0 then 1 else 0*fact(0-1)) Ø 2 * 1 * 1 Ø 2 let rec fact n = if n = 0 then 1 else n * fact (n-1) 19

Type Annotations The syntax (e : t) asserts that e has type t This can be added (almost) anywhere you like let (x : int) = 3 let z = (x : int) + 5 Define functions parameter and return types let fn (x:int):float = (float_of_int x) *. 3.14 Note special position for return type Thus let g x:int =... means g returns int Not that x has type int Checked by compiler: Very useful for debugging 20

Quiz 3: What is the value of foo 4 2 let rec foo n m = if n >= 9 n<0 then m else n + m + 1 Type Error 2 8 7 21

Quiz 3: What is the value of foo 4 2 let rec foo n m = if n >= 9 n<0 then m else n + m + 1 Type Error 2 8 7 22

Quiz 4: What is the value of bar 4 let rec bar(n:int):int = if n = 0 n = 1 then 1 else bar (n-1) + bar (n-2) Syntax Error 4 5 8 23

Quiz 4: What is the value of bar 4 let rec bar(n:int):int = if n = 0 n = 1 then 1 else bar (n-1) + bar (n-2) Syntax Error 4 5 8 24