Download Foundations of Programming Languages

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Foundations of Programming Languages
Closures and Thunks
Prof. Dr. Christoph Reichenbach
Fachbereich 12 / Institut für Informatik
14. November 2014
Partially Applied Functions
Haskell
add x =
let add2 y = x + y
in add2
add 2 3 --> 5
add 2 7 --> 9
2 / 10
Partially Applied Functions
Haskell
add x =
let add2 y = x + y
in add2
add 2 3 --> 5
add 2 7 --> 9
add 3
--> function: x 7→ x + 3
I
I
Can pass ‘add 3’ as function parameter
Other languages: store ‘add 3’ in variable
How can we implement such a thing?
2 / 10
Naive implementation
add 3
3 / 10
Naive implementation
add 3
move $v0, $a0
addi $v0, 3
jreturn
3 / 10
Naive implementation
add 3
add 5
move $v0, $a0
addi $v0, 3
jreturn
move $v0, $a0
addi $v0, 5
jreturn
3 / 10
Naive implementation
add 3
add 5
add z
move $v0, $a0
addi $v0, 3
jreturn
move $v0, $a0
addi $v0, 5
jreturn
z not defined until run-time
Not a fully general implementation scheme
3 / 10
Closures
Idea: store function + environment:
hf , ei
I
I
f : Code pointer
e: Environment
captures variables we know
4 / 10
Executing Closures
.text
heap
li
jal
move
; Create closure
$a0, 7
add
$s0, $v0
...
5 / 10
Executing Closures
.text
heap
7 (x)
ld
add
jreturn
li
jal
move
$v0, 8($a0)
$v0, $a1
; Create closure
$a0, 7
add
$s0, $v0
...
5 / 10
Executing Closures
.text
heap
ld
add
jreturn
7 (x)
$s0
li
jal
move
$v0, 8($a0)
$v0, $a1
; Create closure
$a0, 7
add
$s0, $v0
...
5 / 10
Executing Closures
.text
heap
ld
add
jreturn
7 (x)
$s0
li
jal
move
li
move
ld
jalr
$v0, 8($a0)
$v0, $a1
; Create closure
$a0, 7
add
$s0, $v0
...
; Invoke Closure
$a1, 35
$a0, $s0
$t0, 0($a0)
$t0
5 / 10
Thunks
I
I
Thunk: Closure that expects no parameters
Used to implement call-by-name
6 / 10
Thunks
I
I
Thunk: Closure that expects no parameters
Used to implement call-by-name
Some languages extend thunks with result caches
6 / 10
Thunks with Result Cache
.text
heap
2
sd
la
sd
jreturn
...
; computation
$v0, 8($a0)
$t0, quickret
$t0, 0($a0)
7 / 10
Thunks with Result Cache
heap
.text
value
...
; computation
$v0, 8($a0)
$t0, quickret
$t0, 0($a0)
sd
la
sd
jreturn
7 / 10
Thunks with Result Cache
heap
.text
value
...
; computation
$v0, 8($a0)
$t0, quickret
$t0, 0($a0)
sd
la
sd
jreturn
quickret:
ld
$v0, 8($a0)
jreturn
After first evaluation: result stored, code pointer
changed to fast read subroutine
7 / 10
Uses of Closures and Thunks
Closures:
I
Partial application/currying (functional programming):
add 7
I
Passing subprogram parameters
Thunks:
I
I
Pass-By-Name
Pass-By-Need (with updates)
8 / 10
Closures in the Wild
Python
def add(x):
def add2(y):
return x + y
return add2
C++
std :: function <int(int)> add(int i)
{ return [=] (int j) { return i + j; }; }
Java
static Function <Integer , Integer >
add(int x)
{ return y -> x + y; }
9 / 10
Summary
I
Closure: hf , ei
I
I
I
I
I
Closures can be used like functions
Callers must use special operations
Thunk: Closure without parameters
I
I
I
Subroutine pointer f
Environment e that captures local variables
May cache result
Call-by-name: implemented with uncached thunks
Call-by-need: implemented with cached thunks
10 / 10
Related documents