Closures for Rust - Michael J. Sullivan [PDF]

Aug 18, 2011 - Introduction. Rust. Closures. Conclusion. Goals. What do we want in a programming language? ˆ Fast: gene

0 downloads 5 Views 400KB Size

Recommend Stories


Michael J
Before you speak, let your words pass through three gates: Is it true? Is it necessary? Is it kind?

Michael J. Perkins Elementary
We may have all come on different ships, but we're in the same boat now. M.L.King

Michael J. Wilber
How wonderful it is that nobody need wait a single moment before starting to improve the world. Anne

Professor J. Michael Rotter
Don't fear change. The surprise is the only way to new discoveries. Be playful! Gordana Biernat

michael j raven
Learning never exhausts the mind. Leonardo da Vinci

Michael J. Landis
In the end only three things matter: how much you loved, how gently you lived, and how gracefully you

J. Michael Silver
Don't ruin a good today by thinking about a bad yesterday. Let it go. Anonymous

Michael J. Wilber
Open your mouth only if what you are going to say is more beautiful than the silience. BUDDHA

Michael J. Arnold - Resume
If you want to become full, let yourself be empty. Lao Tzu

michael j. norton
We may have all come on different ships, but we're in the same boat now. M.L.King

Idea Transcript


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

Disclaimer

3 / 25

Introduction

Rust

Closures

Conclusion

Disclaimer

ˆ Rust is under heavy development.

3 / 25

Introduction

Rust

Closures

Conclusion

Disclaimer

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

tomorrow.

3 / 25

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

Introduction

Rust

Closures

Conclusion

Goals What do we want in a programming language?

4 / 25

Introduction

Rust

Closures

Conclusion

Goals What do we want in a programming language?

ˆ Fast: generates efficient machine code

4 / 25

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

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

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

Introduction

Rust

Closures

Conclusion

Goals What do have?

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

5 / 25

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

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

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

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

Introduction

Rust

Closures

Conclusion

Rust

a systems language pursuing the trifecta safe, concurrent, fast -lkuper

6 / 25

Introduction

Rust

Closures

Conclusion

Rust

Design Status

7 / 25

Introduction

Rust

Closures

Conclusion

Design Goals (straight from the docs)

8 / 25

Introduction

Rust

Closures

Conclusion

Design Goals (straight from the docs)

ˆ Compile-time error detection and prevention

8 / 25

Introduction

Rust

Closures

Conclusion

Design Goals (straight from the docs)

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

8 / 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

8 / 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

8 / 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

8 / 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

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

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

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

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

Introduction

Rust

Closures

Conclusion

Status rustc

ˆ Self-hosting rust compiler

12 / 25

Introduction

Rust

Closures

Conclusion

Status rustc

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

12 / 25

Introduction

Rust

Closures

Conclusion

Status rustc

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

12 / 25

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

Introduction

Rust

Closures

Conclusion

Status The catch

ˆ Not ready for prime time

13 / 25

Introduction

Rust

Closures

Conclusion

Status The catch

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

13 / 25

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

Introduction

Rust

Closures

Conclusion

Closures

What closures are Closures in rust

14 / 25

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

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

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

Introduction

Rust

Closures

Conclusion

What closures are Traditional implementation

ˆ Represent functions as a code pointer, environment

pointer pair

18 / 25

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

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

Introduction

Rust

Closures

Conclusion

Closures in rust Solutions

ˆ Have two function types: block and fn

20 / 25

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

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

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

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

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

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

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

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

Smile Life

When life gives you a hundred reasons to cry, show life that you have a thousand reasons to smile

Get in touch

© Copyright 2015 - 2024 PDFFOX.COM - All rights reserved.