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

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

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

Reading Assignment. Lazy Evaluation

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

F28PL1 Programming Languages. Lecture 11: Standard ML 1

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

Chapter 2 SML, a Functional Programming Language

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

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

Lecture 2: The Basis of SML

SML A F unctional Functional Language Language Lecture 19

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

A quick introduction to SML

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

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

So what does studying PL buy me?

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

Currying fun f x y = expression;

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

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

A Brief Introduction to Standard ML

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

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

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

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

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

CPS 506 Comparative Programming Languages. Programming Language Paradigm

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

Vectors in Scheme. Data Structures in Scheme

Functional Programming

Chapter 15. Functional Programming Languages

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

Functional Programming

All the operations used in the expression are over integers. a takes a pair as argument. (a pair is also a tuple, or more specifically, a 2-tuple)

Exercises on ML. Programming Languages. Chanseok Oh

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

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

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

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

Full file at

Advanced Algorithms and Computational Models (module A)

SMURF Language Reference Manual Serial MUsic Represented as Functions

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

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

LECTURE 16. Functional Programming

Introduction to C# Applications

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

Functional Programming. Pure Functional Programming

CS 320: Concepts of Programming Languages

Language Reference Manual

Imperative languages

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

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

Course outline. CSE 341: Programming Languages. Why study programming languages? Course motivation and objectives. 1 lecture: Concepts

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

Chapter 3 Linear Structures: Lists

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Lists. Michael P. Fourman. February 2, 2010

Types. Chapter Six Modern Programming Languages, 2nd ed. 1

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Type Checking and Type Inference

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

Chapter 3 Linear Structures: Lists

egrapher Language Reference Manual

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

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Programming Paradigms Languages F28PL, Lecture 1

CMPT 125: Lecture 3 Data and Expressions

Functional Programming Languages (FPL)

Introduction to SML Getting Started

CSCI-GA Scripting Languages

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

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

Part I. Language Core

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

A Java program contains at least one class definition.

COMP 110 Introduction to Programming. What did we discuss?

The SPL Programming Language Reference Manual

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

Common LISP Tutorial 1 (Basic)

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

Chapter 15. Functional Programming Languages

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

F28PL1 Programming Languages. Lecture 14: Standard ML 4

Topic 1: Introduction

Example Scheme Function: equal

CS558 Programming Languages

Principles of Programming Languages 2017W, Functional Programming

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

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

CSCC24 Functional Programming Typing, Scope, Exceptions ML

CS558 Programming Languages

Program Fundamentals

BASIC ELEMENTS OF A COMPUTER PROGRAM

1 Lexical Considerations

Lexical Considerations

Language Reference Manual

Transcription:

If we have a call (map slow-function long-list where slow-function executes slowly and long-list is a large data structure, we can expect to wait quite a while for computation of the result list to complete. Now consider fastmap, a version of map that uses futures: (define (fastmap f L (if (null? L ( (future (f (car L (fastmap f (cdr L Now look at the call (fastmap slow-function long-list We will exploit a useful aspect of futures they can be cons ed together without delay, even if the computation isn t completed yet. Why? Well a cons just stores a pair of pointers, and it really doesn t matter what the pointers reference (a future or an actual result value. The call to fastmap can actually return before any of the call to slow-function have completed: future1 future2 future3... 231 232 Eventually all the futures automatically transform themselves into data values: answer1 answer2 answer3... 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. Note that pcall can be implemented using futures. 233 234

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 ( ( (let ((tail-part (partition pivot (cdr L (if (<= (car L pivot (car L (car tail-part (cdr tail-part (car tail-part (car L (cdr tail-part 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: 235 236 (define (partition pivot L (if (null? L ( ( (let ((tail-part (partition pivot (cdr L (if (<= (car L pivot (car L (car tail-part (cdr tail-part (car tail-part (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: (define (partition pivot L (if (null? L ( ( (let ((tail-part (partition pivot (cdr L (if (<= (car L pivot (car L (car tail-part (cdr tail-part (car tail-part (car L (cdr tail-part 237 238

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 tailpart. The call builds Reading Assignment Introduction to Standard ML (linked from class web page Webber: Chapters 5, 7, 9, 11 future3 5 future2 239 240 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. 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 241 242

SML is Interactive Basic SML Predefined Types 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 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 < > <= >= = <> 243 244 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. 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: < > <= >= = <> 245 246

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. 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 247 248 Equality is checked componentwise: (1,2 = (0+1,1+1; val it = true : bool (1,2,3 = (1,2 causes a compile-time 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 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 249 250

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. 251