Note that pcall can be implemented using futures. That is, instead of. we can use

Similar documents
If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

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

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus

CS109A ML Notes for the Week of 1/16/96. Using ML. ML can be used as an interactive language. We. shall use a version running under UNIX, called

SML A F unctional Functional Language Language Lecture 19

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

F28PL1 Programming Languages. Lecture 11: Standard ML 1

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

Chapter 2 SML, a Functional Programming Language

2.1. Expressions. Chapter 2 SML, a Functional Programming Language. Integers. Interacting with ML

Lecture 2: The Basis of SML

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

CPS 506 Comparative Programming Languages. Programming Language Paradigm

2.1. Expressions. Chapter 2 ML, a Functional Programming Language. Integers. Interacting with ML

Chapter 15. Functional Programming Languages

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Chapter 3 Linear Structures: Lists

Chapter 3 Linear Structures: Lists

A Brief Introduction to Standard ML

Any questions. Say hello to OCaml. Say hello to OCaml. Why readability matters. History, Variants. Plan (next 4 weeks)

A quick introduction to SML

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

So what does studying PL buy me?

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Currying fun f x y = expression;

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

SMURF Language Reference Manual Serial MUsic Represented as Functions

Functional Programming. Pure Functional Programming

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

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

Full file at

CS 320: Concepts of Programming Languages

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Programming Languages

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

Begin at the beginning

Programming Languages

Exercises on ML. Programming Languages. Chanseok Oh

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Programming Languages

Vectors in Scheme. Data Structures in Scheme

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Functional Programming

egrapher Language Reference Manual

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Second release of the COMPASS Tool Tool Grammar Reference

Example Scheme Function: equal

Functional Programming

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

LECTURE 16. Functional Programming

User-defined Functions. Conditional Expressions in Scheme

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Zach Tatlock Winter 2018

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

Function definitions. CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Example, extended. Some gotchas. Zach Tatlock Winter 2018

Online Resource. Online introductory book on programming in SML (Standard ML): online.pdf

Functional Programming Languages (FPL)

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

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

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

The type checker will complain that the two branches have different types, one is string and the other is int

Data Types The ML Type System

Advanced Algorithms and Computational Models (module A)

Introduction to SML Getting Started

F28PL1 Programming Languages. Lecture 14: Standard ML 4

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

The SPL Programming Language Reference Manual

Introduction to C# Applications

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Dan Grossman Fall 2011

Type Checking and Type Inference

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3)

CSCI-GA Scripting Languages

Built-in Types of Data

CSE 341 Lecture 9. type systems; type safety; defining types Ullman 6-6.2; 5.3. slides created by Marty Stepp

Functional Programming. Big Picture. Design of Programming Languages

Imperative languages

COMP 110 Introduction to Programming. What did we discuss?

Chapter 15. Functional Programming Languages

Lists. Michael P. Fourman. February 2, 2010

Lisp. Versions of LISP

CS 115 Lecture 4. More Python; testing software. Neil Moore

Implementing Coroutines with call/cc. Producer/Consumer using Coroutines

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

CIS24 Project #3. Student Name: Chun Chung Cheung Course Section: SA Date: 4/28/2003 Professor: Kopec. Subject: Functional Programming Language (ML)

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

The PCAT Programming Language Reference Manual

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Data types for mcrl2

Transcription:

Note that pcall can be implemented using futures. That is, instead of (pcall F X Y Z) we can use ((future F) (future X) (future Y) (future Z)) In fact the latter version is actually more parallel execution of F can begin even if all the parameters aren t completely evaluated. 235

Another Example of Futures The following function, partition, will take a list and a data value (called pivot). partition will partition the list into two sublists: (a) Those elements pivot (b) Those elements > pivot (define (partition pivot L) (if (null? L) (cons () () ) (let ((tail-part (partition pivot (cdr L)))) (if (<= (car L) pivot) (cons (cons (car L) (car tail-part)) (cdr tail-part)) (cons (car tail-part)) (cons (car L) (cdr tail-part)) ) ) ) ) 236

We want to add futures to partition, but where? It makes sense to use a future when a computation may be lengthy and we may not need to use the value computed immediately. What computation fits that pattern? The computation of tail-part. We ll mark it in a blue box to show we plan to evaluate it using a future: 237

(define (partition pivot L) (if (null? L) (cons () () ) (let ((tail-part (partition pivot (cdr L)))) (if (<= (car L) pivot) (cons (cons (car L) (car tail-part)) (cdr tail-part)) (cons (car tail-part)) (cons (car L) (cdr tail-part)) ) ) ) ) But this one change isn t enough! We soon access the car and cdr of tail-part, which forces us to wait for its computation to complete. To avoid this delay, we can place the four references to car or cdr of tail-part into futures too: 238

(define (partition pivot L) (if (null? L) (cons () () ) (let ((tail-part (partition pivot (cdr L)))) (if (<= (car L) pivot) (cons (cons (car L) (car tail-part)) (cdr tail-part)) (cons (car tail-part)) (cons (car L) (cdr tail-part)) ) ) ) ) 239

Now we can build the initial part of the partitioned list (that involving pivot and (car L) independently of the recursive call of partition, which completes the rest of the list. For example, (partition 17 '(5 3 8...)) creates a future (call it future1) to compute (partition 17 '(3 8...)) It also creates future2 to compute (car tail-part) and future3 to compute (cdr tail-part). The call builds 5 future2 future3 240

Reading Assignment Roosta: Section 13.3 Introduction to Standard ML (linked from class web page) Webber: Chapters 5, 7, 9, 11 241

ML Meta Language SML is Standard ML, a popular ML variant. ML is a functional language that is designed to be efficient and typesafe. It demonstrates that a functional language need not use Scheme s odd syntax and need not bear the overhead of dynamic typing. SML s features and innovations include: 1. Strong, compile-time typing. 2. Automatic type inference rather than user-supplied type declarations. 3. Polymorphism, including type variables. 242

4. Pattern-directed Programming fun len([]) = 0 len(a::b) = 1+len(b); 5. Exceptions 6. First-class functions 7. Abstract Data Types coin of int bill of int check of string*real; val dime = coin(10); A good ML reference is Elements of ML Programming, by Jeffrey Ullman (Prentice Hall, 1998) 243

SML is Interactive You enter a definition or expression, and SML returns a result with an inferred type. The command use "file name"; loads a set of ML definitions from a file. For example (SML responses are in blue): 21; val it = 21 : int (2 div 3); val it = 0 : int true; val it = true : bool "xyz"; val it = "xyz" : string 244

Basic SML Predefined Types Unit Its only value is (). Type unit is similar to void in C; it is used where a type is needed, but no real type is appropriate. For example, a call to a write function may return unit as its result. Integer Constants are sequences of digits. Negative values are prefixed with a ~ rather than a - (- is a binary subtraction operator). For example, ~123 is negative 123. Standard operators include + - * div mod < > <= >= = <> 245

Real Both fractional (123.456) and exponent forms (10e7) are allowed. Negative signs and exponents use ~ rather than - (~10.0e~12). Standard operators include + - * / < > <= >= Note that = and <> aren t allowed! (Why?) Conversion routines include real(int) to convert an int to a real, floor(real) to take the floor of a real, ceil(real) to take the ceiling of a real. round(real) to round a real, trunc(real) to truncate a real. 246

For example, real(3) returns 3.0, floor(3.1) returns 3, ceiling(3.3) returns 4, round(~3.6) returns ~4, trunc(3.9) returns 3. Mixed mode expressions, like 1 + 2.5 aren t allowed; you must do explicit conversion, like real(1) + 2.5 Strings Strings are delimited by double quotes. Newlines are \n, tabs are \t, and \" and \\ escape double quotes and backslashes. E.g. "Bye now\n" The ^ operator is concatenation. "abc" ^ "def" = "abcdef" The usual relational operators are provided: < > <= >= = <> 247

Characters Single characters are delimited by double quotes and prefixed by a #. For example, #"a" or #"\t". A character is not a string of length one. The str function may be used to convert a character into a string. Thus str(#"a") = "a" Boolean Constants are true and false. Operators include andalso (shortcircuit and), orelse (short-circuit or), not, = and <>. A conditional expression, (if boolval v 1 else v 2 ) is available. 248

Tuples A tuple type, composed of two or more values of any type is available. Tuples are delimited by parentheses, and values are separated by commas. Examples include: (1,2); val it = (1,2) : int * int ("xyz",1=2); val it = ("xyz",false) : string * bool (1,3.0,false); val it = (1,3.0,false) : int * real * bool (1,2,(3,4)); val it = (1,2,(3,4)) : int * int * (int * int) 249

Equality is checked componentwise: (1,2) = (0+1,1+1); val it = true : bool (1,2,3) = (1,2) causes a compiletime type error (tuples must be of the same length and have corresponding types to be compared). #i selects the i-th component of a tuple (counting from 1). Hence #2(1,2,3); val it = 2 : int 250

Lists Lists are required to have a single element type for all their elements; their length is unbounded. Lists are delimited by [ and ] and elements are separated by commas. Thus [1,2,3] is an integer list. The empty (or null) list is [] or nil. The cons operator is :: Hence [1,2,3] 1::2::3::[] Lists are automatically typed by ML: [1,2]; val it = [1,2] : int list 251

Cons Cons is an infix operator represented as :: The left operand of :: is any value of type T. The right operand of :: is any list of type T list. The result of :: is a list of type T list. Hence :: is polymorphic. [] is the empty list. It has a type 'a list. The symbol 'a, read as alpha or tic a is a type variable. Thus [] is a polymorphic constant. 252

List Equality Two lists may be compared for equality if they are of the same type. Lists L1 and L2 are considered equal if: (1) They have the same number of elements (2) Corresponding members of the two lists are equal. List Operators hd head of list operator car tl tail of list operator cdr null null list predicate null? @ infix list append operator append 253

Records Their general form is {name 1 =val 1, name 2 =val 2,... } Field selector names are local to a record. For example: {a=1,b=2}; val it = {a=1,b=2} : {a:int, b:int} {a=1,b="xyz"}; val it = {a=1,b="xyz"} : {a:int, b:string} {a=1.0,b={c=[1,2]}}; val it = {a=1.0,b={c=[1,2]}} : {a:real, b:{c:int list}} 254

The order of fields is irrelevant; equality is tested using field names. {a=1,b=2}={b=2,a=2-1}; val it = true : bool #id extracts the field named id from a record. #b {a=1,b=2} ; val it = 2 : int 255

Identifiers There are two forms: Alphanumeric (excluding reserved words) Any sequence of letters, digits, single quotes and underscores; must begin with a letter or single quote. Case is significant. Identifiers that begin with a single quote are type variables. Examples include: abc a10 'polar sum_of_20 Symbolic Any sequence (except predefined operators) of! % & + - / : < = >? @ \ ~ ^ # * Usually used for user-defined operators. Examples include: ++ <=>!= 256

Comments Of form (* text *) May cross line boundaries. Declaration of Values The basic form is val id = expression; This defines id to be bound to expression; ML answers with the name and value defined and the inferred type. For example val x = 10*10; val x = 100 : int 257

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus val x = 100; val x = (x=100); is fine; there is no type error even though the first x is an integer and then it is a boolean. val x = 100 : int val x = true : bool 258

Examples val x = 1; val x = 1 : int val z = (x,x,x); val z = (1,1,1) : int * int * int val L = [z,z]; val L = [(1,1,1),(1,1,1)] : (int * int * int) list val r = {a=l}; val r = {a=[(1,1,1),(1,1,1)]} : {a:(int * int * int) list} After rebinding, the nearest (most recent) binding is used. 259