Lazy Execution, Chapter 6, Lecture 9 - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Lazy Execution, Chapter 6, Lecture 9

Description:

Example II ... and at the same time serves the. consumer, and request the next ... Abstraction provided in lazy functional languages that allows writing highe ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 33
Provided by: sei110
Category:

less

Transcript and Presenter's Notes

Title: Lazy Execution, Chapter 6, Lecture 9


1
Lazy Execution, Chapter 6, Lecture 9
  • Seif Haridi
  • KTH
  • Peter Van Roy
  • UCL

2
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

3
Example
  • B F1 X
  • C F2 Y
  • A BC
  • 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 BC

4
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
Demand
A BC
5
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
Data driven
A BC
6
Lazy streams
  • fun lazy Generate N
  • NGenerate N1
  • end
  • fun Sum Xs A Limit
  • if Limitgt0 then
  • case Xs of XXr then
  • Sum Xr AX Limit-1
  • end
  • else A end
  • end

local Xs S in thread XsGenerate 0 end
SSum Xs 0 15000000 Show S end
7
How does it work?
  • fun Sum Xs A Limit
  • if Limitgt0 then
  • case Xs of XXr then
  • Sum Xr AX Limit-1
  • end
  • else A end
  • end

fun lazy Generate N N Generate N1 end
local Xs S in thread Xs Generate 0 end
SSum Xs 0 15000000 Show S end
8
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

9
The buffer example
10
The buffer
  • fun Buffer1 In N
  • EndList.drop In N
  • fun lazy Loop In End
  • In.1Loop In.2 End.2
  • end
  • in
  • Loop In End
  • end

Traverse the In stream, forces the producer to
emit N elements
11
The buffer II
  • fun Buffer1 In N
  • thread
  • EndList.drop In N
  • end
  • fun lazy Loop In End
  • In.1Loop 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
12
The buffer III
  • fun Buffer1 In N
  • thread
  • EndList.drop In N
  • end
  • fun lazy Loop In End thread E2 End.2
    end
  • In.1Loop 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
13
Larger ExampleThe 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
X
Xs
XZs
Filter
Sieve
Zs
Xr
Ys
14
Lazy Sieve
  • fun lazy Sieve Xs
  • XXr Xs in
  • X Sieve LFilter
  • Xr
  • fun Y Y mod X \ 0 end
  • end
  • fun Primes Sieve IntsFrom 2 end

15
Lazy Filter
  • For the Sieve program we need a lazy filter
  • fun lazy LFilter Xs F
  • case Xs
  • of nil then nil
  • XXr then
  • if F X then XLFilter Xr F else
    LFilter Xr F end
  • end
  • end

16
Define streams implicitly
  • Ones 1 Ones
  • Infinite stream of ones

1
Ones
cons
17
Define streams implicitly
  • Xs 1 LMap Xs fun X X1
    end
  • What is Xs ?

1
Xs?
cons
1
18
The Hamming problem
  • Generate the first N elements of stream of
    integers of the form 2a 3b5c with a,b,c ? 0 (in
    ascending order)

19
The Hamming problem
  • Generate the first N elements of stream of
    integers of the form 2a 3b5c with a,b,c ? 0 (in
    ascending order)

20
The Hamming problem
  • Generate the first N elements of stream of
    integers of the form 2a 3b5c with a,b,c ? 0 (in
    ascending order)

1
H
cons
21
Lazy File reading
  • fun ToList FOfun lazy LRead L T in if
    File.readBlock FO L T then T
    LRead else T nil File.close FO
    end LendLRead
  • end
  • This avoids reading the whole file in memory

22
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
  • xy 1?x ?10, 1?y ?x
  • Equivalent List comprehension expression is
  • XY X 1..10 Y 1..X
  • Example
  • 11 21 22 31 32 33 ... 1010

23
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

24
Example 1
  • z xx x ? from(1,10)
  • Z LMap LFrom 1 10 fun X XX end
  • z xy x ? from(1,10), y ? from(1,x)
  • Z LFlatten LMap LFrom 1 10
    fun X LMap LFrom 1 X
    fun Y XY end
    end

25
Example 2
  • z xy x ? from(1,10), y ? from(1,x), xy?10
  • Z LFilter LFlatten LMap LFrom 1 10
    fun X LMap LFrom 1 X
    fun Y XY end
    end
  • fun XY XYlt10 end

26
Implementation of lazy execution
27
Implementation of lazy execution
The following defines the syntax of a statement,
?s? denotes a statement
?s? skip
empty statement ...
thread
?s1? end thread creation ByNeed fun
?e? end ?x? by need statement
zero arityfunction
variable
28
Implementation
some statement
stack
ByNeed fun ?e? end X,E
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
store
  • f
  • x

29
Implementation
some statement
stack
ByNeed fun ?e? end X,E
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
store
  • f
  • x f

(fun ?e? end X,E)
30
accessing the byNeed variable
  • X ByNeed fun 111111 end (by thread T0)
  • Access by some thread T1
  • if X gt 1000 then Show helloX end
  • or
  • Wait X
  • Causes X to be bound to 12321 (i.e. 111111)

31
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

32
Lazy functions
  • fun lazy From N N From N1
  • end

fun From N fun F N From N1 end in
ByNeed F end
Write a Comment
User Comments (0)
About PowerShow.com