S. Haridi and P. Van Roy1 Lazy Execution Seif Haridi KTH Peter Van Roy UCL.

Slides:



Advertisements
Similar presentations
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Accumulators, Difference Lists (VRH ) Carlos Varela.
Advertisements

C. Varela; Adapted from S. Haridi and P. Van Roy1 Declarative Programming Techniques Lazy Execution (VRH 4.5) Carlos Varela RPI Adapted with permission.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model From kernel to practical language (CTM 2.6) Exceptions (CTM.
0 PROGRAMMING IN HASKELL Chapter 12 – Lazy evaluation and infinite lists Slides not from Hutton.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management, Exceptions, From kernel to practical language.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Single assignment store Kernel language syntax Carlos Varela.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Data Abstraction and State Abstract Data Types (3.7) State/Data Abstraction(6.1, 6.3, 6.4.1,
C. Varela; Adapted with permission from S. Haridi and P. Van Roy1 Oz, Declarative Concurrency, and Active Objects (VRH 4, 7.8) Carlos Varela RPI Adapted.
C. Varela; Adapted w. permission from S. Haridi and P. Van Roy1 Introduction to Programming Concepts Carlos Varela RPI Adapted with permission from: Seif.
Comp 205: Comparative Programming Languages Lazy Evaluation and Infinite Lists Lecture notes, exercises, etc., can be found at:
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Kernel language semantics Carlos Varela RPI Adapted with permission.
C. Varela; Adapted with permission from S. Haridi and P. Van Roy1 Declarative Concurrency (VRH4) Carlos Varela RPI Adapted with permission from: Seif Haridi.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management, Exceptions, From kernel to practical language.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Non-declarative needs (VRH 3.8) Program design in the.
FUNCTIONAL PROGRAMMING IN ERLANG ID1218 Lecture Christian Schulte Software and Computer Systems School of Information and.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Declarativeness, iterative computation (VRH 3.1-2) Higher-order.
S. Haridi and P. Van Roy1 Declarative Concurrency Seif Haridi KTH Peter Van Roy UCL.
9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC Lecture 8 (Multiple) Threads Semantics.
High-Level Programming Languages: C++
Chapter 5: Control Structures II (Repetition)
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management (CTM 2.5) Carlos Varela RPI April 6, 2015.
CSE 102 Introduction to Computer Engineering What is an Algorithm?
C. Varela1 Programming Languages (CSCI 4430/6430) Part 1: Functional Programming: Summary Carlos Varela Rennselaer Polytechnic Institute September 29,
C. Varela; Adapted w. permission from S. Haridi and P. Van Roy1 Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections ,
COMP313A Functional Programming (1)
S. Haridi and P. Van Roy1 Introduction to Programming Seif Haridi KTH Peter Van Roy UCL.
Com Functional Programming Lazy Evaluation Marian Gheorghe Lecture 13 Module homepage Mole & ©University of Sheffieldcom2010.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
S. Haridi and P. Van Roy1 Declarative Computation Model Seif Haridi KTH Peter Van Roy UCL.
1 Lecture 4: Concurrency Per Brand. 2 Threads and the store Store f Thread 1Thread 2Thread 3 Thread 4 ‘|’ Two runnable and two suspended threads Execution.
C. Varela1 Logic Programming (CTM ) Constraint Programming: Constraints and Computation Spaces Carlos Varela Rennselaer Polytechnic Institute.
CS321 Functional Programming 2 © JAS Programming with Streams A stream is never finite and could be defined as special polymorphic type data stream.
Higher-Order Programming: Iterative computation (CTM Section 3
Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections , 3.2, , 4.7.2) Carlos Varela RPI September 12,
Carlos Varela Rennselaer Polytechnic Institute September 27, 2016
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2017.
6.001 SICP Variations on a Scheme
Higher-Order Programming: Iterative computation (CTM Section 3
Carlos Varela RPI September 22, 2017 Adapted with permission from:
Declarative Programming Techniques Accumulators (CTM 3. 4
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Winter 2013.
Closures and Streams cs784(Prasad) L11Clos
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Zach Tatlock Winter 2018.
Declarative Computation Model Kernel language semantics (Non-)Suspendable statements (VRH ) Carlos Varela RPI October 11, 2007 Adapted with.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2013.
Streams Sections 3.5.1,3.5.2 Pages
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2017.
Higher-Order Programming: Closures, procedural abstraction, genericity, instantiation, embedding. Control abstractions: iterate, map, reduce, fold, filter.
Declarative Concurrency (CTM 4)
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2018.
CSCE 314: Programming Languages Dr. Dylan Shell
Streams, Delayed Evaluation and a Normal Order Interpreter
Streams and Lazy Evaluation in Lisp and Scheme
Declarative Programming Techniques Accumulators (CTM 3. 4
Lazy Programming Lazy evaluation:
CSE 3302 Programming Languages
Introduction to Programming Concepts (VRH )
Declarative Computation Model Single assignment store (VRH 2
Announcements Quiz 5 HW6 due October 23
Introduction to Programming Concepts
Declarative Programming Techniques Accumulators (CTM 3. 4
Streams Contract is the same as pairs...
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2019.
Presentation transcript:

S. Haridi and P. Van Roy1 Lazy Execution Seif Haridi KTH Peter Van Roy UCL

S. Haridi and P. Van Roy2 Lazy execution Up to now the execution order of each thread follows textual order, when a statement comes as the first in sequence it will execute, whether or not its results are needed later The execution scheme is called eager execution, or supply- driven execution Another execution order is that a statement is executed only if its results are needed some in the program This scheme is called lazy evaluation, or demand-driven evaluation

S. Haridi and P. Van Roy3 Example B = {F1 X} C = {F2 Y} A = B+C Assume F1 and F2 are lazy functions (see Haskell) B = {F1 X} and C = {F2 Y} are executed only if their results are needed in A = B+C

S. Haridi and P. Van Roy4 Example In Lazy execution, an operation suspends until its result are needed The suspended operation is triggered when another operation needs the value for its arguments In general multiple suspended operations could start concurrently B = {F1 X}C = {F2 Y} A = B+C Demand

S. Haridi and P. Van Roy5 Example II In data-driven execution, an operation suspend until the values of its arguments results are available In general the suspended computation could start concurrently B = {F1 X}C = {F2 Y} A = B+C Data driven

S. Haridi and P. Van Roy6 Lazy streams fun lazy {Generate N} N|{Generate N+1} end fun {Sum Xs A Limit} if Limit>0 then case Xs of X|Xr then {Sum Xr A+X Limit-1} end else A end end local Xs S in thread Xs={Generate 0} end S={Sum Xs } {Show S} end

S. Haridi and P. Van Roy7 How does it work? fun {Sum Xs A Limit} if Limit>0 then case Xs of X|Xr then {Sum Xr A+X Limit-1} end else A end end fun lazy {Generate N} N | {Generate N+1} end local Xs S in thread Xs = {Generate 0} end S={Sum Xs } {Show S} end

S. Haridi and P. Van Roy8 Improving throughput Use a lazy buffer It takes a lazy input stream In and an integer N, and returns a lazy output stream Out When it is first called, it first fills itself with N elements by asking the producer The buffer now has N elements filled Whenever the consumer asks for an element, the buffer in turn asks the producer for another element

S. Haridi and P. Van Roy9 The buffer example producerbufferconsumer N producerbufferconsumer N

S. Haridi and P. Van Roy10 The buffer fun {Buffer1 In N} End={List.drop In N} fun lazy {Loop In End} In.1|{Loop In.2 End.2} end in {Loop In End} end Traverse the In stream, forces the producer to emit N elements

S. Haridi and P. Van Roy11 The buffer II fun {Buffer1 In N} thread End={List.drop In N} end fun lazy {Loop In End} In.1|{Loop In.2 End.2} end in {Loop In End} end Traverse the In stream, forces the producer to emit N elements and at the same time serves the consumer

S. Haridi and P. Van Roy12 The buffer III fun {Buffer1 In N} thread End={List.drop In N} end fun lazy {Loop In End} thread E2 = End.2 end In.1|{Loop In.2 E2} end in {Loop In End} end Traverse the In stream, forces the producer to emit N elements and at the same time serves the consumer, and request the next element ahead

S. Haridi and P. Van Roy13 Larger Example: The sieve of Eratosthenes Produces prime numbers It takes a stream 2...N, peals off 2 from the rest of the stream Delivers the rest to the next sieve Sieve FilterSieve Xs Xr X Ys Zs X|Zs

S. Haridi and P. Van Roy14 Lazy Sieve fun lazy {Sieve Xs} X|Xr = Xs in X | {Sieve {LFilter Xr fun {$ Y} Y mod X \= 0 end }} end fun {Primes} {Sieve {IntsFrom 2}} end

S. Haridi and P. Van Roy15 Lazy Filter For the Sieve program we need a lazy filter fun lazy {LFilter Xs F} case Xs of nil then nil [] X|Xr then if {F X} then X|{LFilter Xr F} else {LFilter Xr F} end end

S. Haridi and P. Van Roy16 Define streams implicitly Ones = 1 | Ones Infinite stream of ones 1 cons Ones

S. Haridi and P. Van Roy17 Define streams implicitly Xs = 1 | {LMap Xs fun {$ X} X+1 end} What is Xs ? 1 cons +1 Xs?

S. Haridi and P. Van Roy18 The Hamming problem Generate the first N elements of stream of integers of the form: 2 a 3 b 5 c with a,b,c  0 (in ascending order) *3*2 *5

S. Haridi and P. Van Roy19 The Hamming problem Generate the first N elements of stream of integers of the form: 2 a 3 b 5 c with a,b,c  0 (in ascending order) *3*2 *5 Merge

S. Haridi and P. Van Roy20 The Hamming problem Generate the first N elements of stream of integers of the form: 2 a 3 b 5 c with a,b,c  0 (in ascending order) *3*2 *5 Merge 1 cons H

S. Haridi and P. Van Roy21 Lazy File reading fun {ToList FO} fun lazy {LRead} L T in if {File.readBlock FO L T} then T = {LRead} else T = nil {File.close FO} end L end {LRead} end This avoids reading the whole file in memory

S. Haridi and P. Van Roy22 List Comprehensions Abstraction provided in lazy functional languages that allows writing highe level set-like expression In our context we produce lazy lists instead of sets The mathemnatical set expression –{x*y | 1  x  10, 1  y  x} Equivalent List comprehension expression is –[X*Y | X = ; Y = 1..X] Example: –[1*1 2*1 2*2 3*1 3*2 3* *10]

S. Haridi and P. Van Roy23 List Comprehensions The general form is [ f(x,y,...,z) | x  gen(a1,...,an) ; guard(x,...) y  gen(x, a1,...,an) ; guard(y,x,...).... ] No linguistic support in Mozart/Oz, but can be easily expressed

S. Haridi and P. Van Roy24 Example 1 z = [x#x | x  from(1,10)] Z = {LMap {LFrom 1 10} fun{$ X} X#X end} z = [x#y | x  from(1,10), y  from(1,x)] Z = { LFlatten {LMap {LFrom 1 10} fun{$ X} {LMap {LFrom 1 X} fun {$ Y} X#Y end } end } }

S. Haridi and P. Van Roy25 Example 2 z = [x#y | x  from(1,10), y  from(1,x), x+y  10] Z ={LFilter { LFlatten {LMap {LFrom 1 10} fun{$ X} {LMap {LFrom 1 X} fun {$ Y} X#Y end } end } } fun {$ X#Y} X+Y=<10 end} }

S. Haridi and P. Van Roy26 Implementation of lazy execution  s  ::= skip empty statement |... | thread  s 1  end thread creation |{ByNeed fun {$}  e  end  x  } by need statement The following defines the syntax of a statement,  s  denotes a statement zero arity function variable

S. Haridi and P. Van Roy27 Implementation some statement fxfx {ByNeed fun {$}  e  end X,E } stack store A function value is created in the store (say f) the function f is associated with the variable x execution proceeds immediately to next statement f

S. Haridi and P. Van Roy28 Implementation some statement f x : f {ByNeed fun {$}  e  end X,E } stack store A function value is created in the store (say f) the function f is associated with the variable x execution proceeds immediately to next statement f ( fun {$}  e  end X,E)

S. Haridi and P. Van Roy29 Accessing the byNeed variable X = {ByNeed fun{$} 111*111 end} (by thread T0) Access by some thread T1 –if X > 1000 then {Show hello#X} end or –{Wait X} –Causes X to be bound to (i.e. 111*111)

S. Haridi and P. Van Roy30 Implementation Thread T1 1.X is needed 2.start a thread T2 to exeute F (the function) 3.only T2 is allowed to bind X Thread T2 1.Evaluate Y = {F} 2.Bind X the value Y 3.Terminate T2 1.Allow access on X

S. Haridi and P. Van Roy31 Lazy functions fun lazy {From N} N | {From N+1} end fun {From N} fun {F} N | {From N+1} end in {ByNeed F} end