Closures for Rust Michael Sullivan August 18, 2011 1 / 25 - - PowerPoint PPT Presentation

closures for rust
SMART_READER_LITE
LIVE PREVIEW

Closures for Rust Michael Sullivan August 18, 2011 1 / 25 - - PowerPoint PPT Presentation

Introduction Rust Closures Conclusion Closures for Rust Michael Sullivan August 18, 2011 1 / 25 Introduction Rust Closures Conclusion Outline Introduction Rust Closures 2 / 25 Introduction Rust Closures Conclusion


slide-1
SLIDE 1

Introduction Rust Closures Conclusion

Closures for Rust

Michael Sullivan August 18, 2011

1 / 25

slide-2
SLIDE 2

Introduction Rust Closures Conclusion

Outline Introduction Rust Closures

2 / 25

slide-3
SLIDE 3

Introduction Rust Closures Conclusion

Disclaimer

❼ ❼ ❼

3 / 25

slide-4
SLIDE 4

Introduction Rust Closures Conclusion

Disclaimer

❼ Rust is under heavy development. ❼ ❼

3 / 25

slide-5
SLIDE 5

Introduction Rust Closures Conclusion

Disclaimer

❼ Rust is under heavy development. ❼ The things described in this talk may not be true

tomorrow.

3 / 25

slide-6
SLIDE 6

Introduction Rust Closures Conclusion

Disclaimer

❼ Rust is under heavy development. ❼ The things described in this talk may not be true

tomorrow.

❼ What I discuss and how I present issues reflect my

personal biases in language design.

3 / 25

slide-7
SLIDE 7

Introduction Rust Closures Conclusion

Goals What do we want in a programming language?

❼ ❼ ❼ ❼

4 / 25

slide-8
SLIDE 8

Introduction Rust Closures Conclusion

Goals What do we want in a programming language?

❼ Fast: generates efficient machine code ❼ ❼ ❼

4 / 25

slide-9
SLIDE 9

Introduction Rust Closures Conclusion

Goals What do we want in a programming language?

❼ Fast: generates efficient machine code ❼ Safe: type system provides guarantees that prevent

certain bugs

❼ ❼

4 / 25

slide-10
SLIDE 10

Introduction Rust Closures Conclusion

Goals What do we want in a programming language?

❼ Fast: generates efficient machine code ❼ Safe: type system provides guarantees that prevent

certain bugs

❼ Concurrent: easy to build concurrent programs and to

take advantage of parallelism

4 / 25

slide-11
SLIDE 11

Introduction Rust Closures Conclusion

Goals What do we want in a programming language?

❼ Fast: generates efficient machine code ❼ Safe: type system provides guarantees that prevent

certain bugs

❼ Concurrent: easy to build concurrent programs and to

take advantage of parallelism

❼ “Systemsy”: fine grained control, predictable performance

characteristics

4 / 25

slide-12
SLIDE 12

Introduction Rust Closures Conclusion

Goals What do have?

❼ Firefox is in C++, which is Fast and Systemsy ❼ ❼ ❼ ❼

5 / 25

slide-13
SLIDE 13

Introduction Rust Closures Conclusion

Goals What do have?

❼ Firefox is in C++, which is Fast and Systemsy ❼ ML is (sometimes) fast and (very) safe ❼ ❼ ❼

5 / 25

slide-14
SLIDE 14

Introduction Rust Closures Conclusion

Goals What do have?

❼ Firefox is in C++, which is Fast and Systemsy ❼ ML is (sometimes) fast and (very) safe ❼ Erlang is safe and concurrent ❼ ❼

5 / 25

slide-15
SLIDE 15

Introduction Rust Closures Conclusion

Goals What do have?

❼ Firefox is in C++, which is Fast and Systemsy ❼ ML is (sometimes) fast and (very) safe ❼ Erlang is safe and concurrent ❼ Haskell is (sometimes) fast, (very) safe, and concurrent ❼

5 / 25

slide-16
SLIDE 16

Introduction Rust Closures Conclusion

Goals What do have?

❼ Firefox is in C++, which is Fast and Systemsy ❼ ML is (sometimes) fast and (very) safe ❼ Erlang is safe and concurrent ❼ Haskell is (sometimes) fast, (very) safe, and concurrent ❼ Java and C# are fast and safe

5 / 25

slide-17
SLIDE 17

Introduction Rust Closures Conclusion

Rust a systems language pursuing the trifecta safe, concurrent, fast

  • lkuper

6 / 25

slide-18
SLIDE 18

Introduction Rust Closures Conclusion

Rust Design Status

7 / 25

slide-19
SLIDE 19

Introduction Rust Closures Conclusion

Design Goals (straight from the docs)

❼ ❼ ❼ ❼ ❼ ❼ ❼

8 / 25

slide-20
SLIDE 20

Introduction Rust Closures Conclusion

Design Goals (straight from the docs)

❼ Compile-time error detection and prevention ❼ ❼ ❼ ❼ ❼ ❼

8 / 25

slide-21
SLIDE 21

Introduction Rust Closures Conclusion

Design Goals (straight from the docs)

❼ Compile-time error detection and prevention ❼ Run-time fault tolerance and containment ❼ ❼ ❼ ❼ ❼

8 / 25

slide-22
SLIDE 22

Introduction Rust Closures Conclusion

Design Goals (straight from the docs)

❼ Compile-time error detection and prevention ❼ Run-time fault tolerance and containment ❼ System building, analysis and maintenance affordances ❼ ❼ ❼ ❼

8 / 25

slide-23
SLIDE 23

Introduction Rust Closures Conclusion

Design Goals (straight from the docs)

❼ Compile-time error detection and prevention ❼ Run-time fault tolerance and containment ❼ System building, analysis and maintenance affordances ❼ Clarity and precision of expression ❼ ❼ ❼

8 / 25

slide-24
SLIDE 24

Introduction Rust Closures Conclusion

Design Goals (straight from the docs)

❼ Compile-time error detection and prevention ❼ Run-time fault tolerance and containment ❼ System building, analysis and maintenance affordances ❼ Clarity and precision of expression ❼ Implementation simplicity ❼ ❼

8 / 25

slide-25
SLIDE 25

Introduction Rust Closures Conclusion

Design Goals (straight from the docs)

❼ Compile-time error detection and prevention ❼ Run-time fault tolerance and containment ❼ System building, analysis and maintenance affordances ❼ Clarity and precision of expression ❼ Implementation simplicity ❼ Run-time efficiency ❼

8 / 25

slide-26
SLIDE 26

Introduction Rust Closures Conclusion

Design Goals (straight from the docs)

❼ Compile-time error detection and prevention ❼ Run-time fault tolerance and containment ❼ System building, analysis and maintenance affordances ❼ Clarity and precision of expression ❼ Implementation simplicity ❼ Run-time efficiency ❼ High concurrency

8 / 25

slide-27
SLIDE 27

Introduction Rust Closures Conclusion

Design Type system features

❼ Algebraic data type and pattern matching (no null

pointers!)

❼ Polymorphism: functions and types can have generic type

parameters

❼ Type inference on local variables ❼ Lightweight object system ❼ Data structures are immutable by default

9 / 25

slide-28
SLIDE 28

Introduction Rust Closures Conclusion

Design Other features

❼ Lightweight tasks with no shared state ❼ Control over memory allocation ❼ Move semantics, unique pointers ❼ Function arguments can be passed by alias ❼ Typestate system tracks predicates that hold at points in

the program

10 / 25

slide-29
SLIDE 29

Introduction Rust Closures Conclusion

Design ...What? “It’s like C++ grew up, went to grad school, started dating ML, and is sharing an office with Erlang.”

11 / 25

slide-30
SLIDE 30

Introduction Rust Closures Conclusion

Status rustc

❼ Self-hosting rust compiler ❼ ❼ ❼

12 / 25

slide-31
SLIDE 31

Introduction Rust Closures Conclusion

Status rustc

❼ Self-hosting rust compiler ❼ Uses LLVM as a backend ❼ ❼

12 / 25

slide-32
SLIDE 32

Introduction Rust Closures Conclusion

Status rustc

❼ Self-hosting rust compiler ❼ Uses LLVM as a backend ❼ Handles polymorphism through type passing (blech) ❼

12 / 25

slide-33
SLIDE 33

Introduction Rust Closures Conclusion

Status rustc

❼ Self-hosting rust compiler ❼ Uses LLVM as a backend ❼ Handles polymorphism through type passing (blech) ❼ Memory management through automatic reference

counting (eww)

12 / 25

slide-34
SLIDE 34

Introduction Rust Closures Conclusion

Status The catch

❼ Not ready for prime time ❼ ❼

13 / 25

slide-35
SLIDE 35

Introduction Rust Closures Conclusion

Status The catch

❼ Not ready for prime time ❼ Lots of bugs and exposed sharp edges ❼

13 / 25

slide-36
SLIDE 36

Introduction Rust Closures Conclusion

Status The catch

❼ Not ready for prime time ❼ Lots of bugs and exposed sharp edges ❼ Language still changing rapidly

13 / 25

slide-37
SLIDE 37

Introduction Rust Closures Conclusion

Closures What closures are Closures in rust

14 / 25

slide-38
SLIDE 38

Introduction Rust Closures Conclusion

What closures are Definition

❼ In civilized languages, functions are first-class values and

are allowed to reference variables in enclosing scopes

❼ That is, they close over their environments

15 / 25

slide-39
SLIDE 39

Introduction Rust Closures Conclusion

What closures are Example

function add(x) { return function(y) { return x + y; }; } var foo = add (42)(1337); // 1379

❼ Produces a function that adds x to its argument ❼ Note that the inner function outlives the enclosing

  • function. x can’t just be stored on the stack.

16 / 25

slide-40
SLIDE 40

Introduction Rust Closures Conclusion

What closures are Another Example

function scale(x, v) { return map(function(y) { return x * y; }, v); } var v = scale(2, [1, 2, 3]); // [2, 4, 6]

❼ Multiplies every element in an array by some amount ❼ Note that here the lifetime of the inner function is shorter

than the lifetime of the enclosing one. x could just be stored on the stack.

17 / 25

slide-41
SLIDE 41

Introduction Rust Closures Conclusion

What closures are Traditional implementation

❼ Represent functions as a code pointer, environment

pointer pair

18 / 25

slide-42
SLIDE 42

Introduction Rust Closures Conclusion

What closures are Traditional implementation

❼ Represent functions as a code pointer, environment

pointer pair

❼ Heap allocate stack frames (or at least the parts that are

closed over)

18 / 25

slide-43
SLIDE 43

Introduction Rust Closures Conclusion

Closures in rust Design constraints

❼ Want to be explicit about when we are allocating memory ❼ Don’t want to have to heap allocate closures when it isn’t

necessary

19 / 25

slide-44
SLIDE 44

Introduction Rust Closures Conclusion

Closures in rust Solutions

❼ Have two function types: block and fn ❼ ❼ ❼

20 / 25

slide-45
SLIDE 45

Introduction Rust Closures Conclusion

Closures in rust Solutions

❼ Have two function types: block and fn ❼ Values of a block type may not be copied, but can be

passed by alias; this prevents them from escaping

❼ ❼

20 / 25

slide-46
SLIDE 46

Introduction Rust Closures Conclusion

Closures in rust Solutions

❼ Have two function types: block and fn ❼ Values of a block type may not be copied, but can be

passed by alias; this prevents them from escaping

❼ Values of fn type can be automatically coerced to block

type when passed as function arguments; this allows more code reuse

20 / 25

slide-47
SLIDE 47

Introduction Rust Closures Conclusion

Closures in rust Solutions

❼ Have two function types: block and fn ❼ Values of a block type may not be copied, but can be

passed by alias; this prevents them from escaping

❼ Values of fn type can be automatically coerced to block

type when passed as function arguments; this allows more code reuse

❼ Explicitly state what sort of function you writing

20 / 25

slide-48
SLIDE 48

Introduction Rust Closures Conclusion

Closures in rust Lambda example

fn add(x: int) -> fn(int) -> int { ret lambda(y: int) -> int { ret x + y; }; }

❼ lambda produces a fn that closes over its environment by

copying upvars into a heap allocated environment

❼ Since the variables are copied, changes made to the

variables in the enclosing scope will not be reflected in the nested function

21 / 25

slide-49
SLIDE 49

Introduction Rust Closures Conclusion

Closures in rust Block example

fn scale(x: int , v: &[int]) -> [int] { map(block(y: &int) -> int { x * y }, v) }

❼ block produces a block that closes over its environment

by storing pointers to the stack locations of the variables in a stack allocated environment

22 / 25

slide-50
SLIDE 50

Introduction Rust Closures Conclusion

Closures in rust Inference example

fn scale(x: int , v: &[int]) -> [int] { map ({|&y| x * y}, v) }

❼ Provides an abbreviation for block; the argument and

return types are type inferred

❼ Only allowed to appear as a function argument, making

type inference easy

23 / 25

slide-51
SLIDE 51

Introduction Rust Closures Conclusion

Closures in rust Coercion example

fn add1(x: &int) -> int { x + 1 } fn increment(v: &[int]) -> [int] { map(add1 , v) }

❼ add1 is coerced to a block when passed to map

24 / 25

slide-52
SLIDE 52

Introduction Rust Closures Conclusion

Conclusion

❼ Rust is a new systems language out of Mozilla Research

that is designed to be fast, concurrent, and safe

❼ Closures are a tricky design space in languages that want

to be explicit about performance

❼ Rust approaches the issues by separating functions into

multiple varieties.

25 / 25