Download - E cient linear types for human beings Iain Lane

Transcript
Page 1: E cient linear types for human beings Iain Lane

Linear MLEfficient linear types for human beings

Iain Lane

School of Computer ScienceUniversity of Nottingham

9 July, 2011

Page 2: E cient linear types for human beings Iain Lane

Outline

Introduction

Linear logic

Linear ML

Demo!

Fin

Page 3: E cient linear types for human beings Iain Lane

Introduction

I Internship at the beginning of the year (January–April)

I Funded by Horizon project (www.horizon.ac.uk)

Page 4: E cient linear types for human beings Iain Lane

Project

I Work on a new programming language: Linear MLI Got some coverage on the internets

I Reddit(http://www.reddit.com/r/types/comments/g118j/linearml_is_a_pogramming_language_designed_to/)

I Ycombinator(http://news.ycombinator.com/item?id=2310587)

Page 5: E cient linear types for human beings Iain Lane

This talk

I Introduction to linear logic & linear type systems

I Introduction to Linear ML

I Demo, if time

Page 6: E cient linear types for human beings Iain Lane

Outline

Introduction

Linear logic

Linear ML

Demo!

Fin

Page 7: E cient linear types for human beings Iain Lane

WTF?

I First proposed by Girard in 1987 (Girard is also the inventor ofthe mustard watch)

I The connectives of linear logic have meanings in terms ofparallel and concurrent computation

I Traditional logic encourages casual use of assumptions.Duplication is free. In linear logic it is restricted via alterationsto the weakening and contraction rules

I Beginning to be used in OS research: Microsoft “Singularity”project uses linear typing for message passing

Page 8: E cient linear types for human beings Iain Lane

Linear logicNew connectives

I N “choose from A and B”

I ⊕ “either A or B”

I A⊗ B “both A and B”

I A ( B “A linearly implies B” or “consuming A yields B”

I !A “bang A”

Page 9: E cient linear types for human beings Iain Lane

Variables

Two kinds of variables

I Linear: ¿A

I Nonlinear (or sometimes “intuitionistic”): ?A

Page 10: E cient linear types for human beings Iain Lane

Some inference rules

both

∆ ` A ∆′ ` B

∆,∆′ ` A⊗ B⊗I

∆ ` A⊗ B ∆′, ¿A, ¿B ` C

∆,∆′ ` C⊗E

choose

∆ ` A ∆ ` B

∆ ` A N BNI

∆ ` A N B

∆ ` ANE1

∆ ` A N B

∆ ` BNE2

Page 11: E cient linear types for human beings Iain Lane

either

∆ ` A

∆ ` A⊕ B⊕I1

∆ ` B

∆ ` A⊕ B⊕I2

wand

∆, ¿A ` B

∆ ` A ( B( I

Page 12: E cient linear types for human beings Iain Lane

structural

∆ ` B

∆, ?A ` BWeakening

∆, ?A, ?A ` B

∆, ?A ` BContraction

bang

?∆ ` A

?∆ `!A!I

∆ `!A ∆′, ?A ` B

∆,∆′ ` B!E

Page 13: E cient linear types for human beings Iain Lane

ExampleA packet-response system

Given three types: Packet, Response and Command.

A packet can either elicit a response or issue a command

¿Packet ` Response ¿Packet ` Command

Page 14: E cient linear types for human beings Iain Lane

Given two distinct packets, we can produce both a responseand a command

¿Packet, ¿Packet ` Response⊗ Command

Given one packet, the server can decide whether to send aresponse or elicit a command

¿Packet ` Response N Command

Page 15: E cient linear types for human beings Iain Lane

Linear types?

I Variables must be used exactly once

I Enforced by going out of scope after they are used

I Useful for expressing ownership

Page 16: E cient linear types for human beings Iain Lane

Example

The code

w r i t e ‘ ‘ E n t e r your name : ’ ’s t r i n g name = r e a d L i n ew r i t e L i n e ( s p r i n t f ” H e l l o %s ” name )

Works just as you’d expect.

Page 17: E cient linear types for human beings Iain Lane

Example

BUT, the code

w r i t e ‘ ‘ E n t e r your name : ’ ’s t r i n g name = r e a d L i n ew r i t e L i n e ( s p r i n t f ” H e l l o %s ” name )w r i t e F i l e ‘ ‘ e . v . l . h a c k e r s . log ’ ’ name

Fails. Line 4: Variable ’name’ not in scope. name was consumedby the first writeLine.

Page 18: E cient linear types for human beings Iain Lane

Outline

Introduction

Linear logic

Linear ML

Demo!

Fin

Page 19: E cient linear types for human beings Iain Lane

Linear ML

I Linearly typed ‘dialect’ of ML (uses OCaml-like syntax)

I Compiled to LLVM

I Statically typed

I Strict evaluation order

Page 20: E cient linear types for human beings Iain Lane

Linear MLLinear types imply

I Messages passed between threads without copying

I Majority of operations performed in-place

I No need for GC

Page 21: E cient linear types for human beings Iain Lane

Outline

Introduction

Linear logic

Linear ML

Demo!

Fin

Page 22: E cient linear types for human beings Iain Lane

Outline

Introduction

Linear logic

Linear ML

Demo!

Fin

Page 23: E cient linear types for human beings Iain Lane

The end

https://github.com/pikatchu/LinearML