1 type-directed concurrency deepak garg, frank pfenning {dg+, fp+}@cs.cmu.edu carnegie mellon...

44
1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Post on 22-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

1

Type-Directed Concurrency

Deepak Garg, Frank Pfenning{dg+, fp+}@cs.cmu.edu

Carnegie Mellon University

Page 2: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 2

Design Goals

• Build a language starting from a logic

• Functional and concurrent features should arise from logical principles

• Write precise specifications for functions in the types– Functional reduction should be type-safe

• Concurrent computation should not interfere with functional type-safety

Page 3: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 3

Outline

• Design goals

• Computation-as-proof reduction, Computation-as-proof search, Computation-as-proof search and reduction

• Introduction to CLL

• fCLL: Functional core

• lCLL: Concurrent core

• Related Work and Conclusion

Page 4: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 4

Computation-as-proof reduction

• Curry-Howard isomorphism

• Strong guarantees about values computed by a program (type-safety)

• Captures behavior of full programs, not their interacting components

• Works very well for functional programs

• Unsuited for concurrent languages, and reactive systems in general

Page 5: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 5

Computation-as-proof search

• Logic programming languages (Prolog, …)

• Programs are sets of logic formulae (Rules, facts and a goal)

• Computation is proof-search

(Proving the goal from the rules and facts)

• Captures dynamics of concurrent programs (FCP, Concurrent Prolog, …)

• Hard to capture functional features

Page 6: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 6

Computation-as-proof search and reduction

• Add proof-terms to a logic programming language

• Interpret proof-terms as programs

• Synthesize proofs-terms on the fly

• Alternate proof search with reduction of synthesized proof-terms

• Captures both concurrent and functional features!

Page 7: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 7

W: B?: B

Example

(N: A!B), (M: A)

(V: A!B), (U: A)

(V U): B

Functional reduction, parallelism

Synchronization

Functional reduction

Proof reduction

Proof search

Proof reduction

FACTS

GOAL

Page 8: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 8

Where we are …

• Design goals

• Computation-as-proof reduction, Computation-as-proof search, Computation-as-proof search and reduction

• Introduction to CLL

• fCLL: Functional core of CLL

• lCLL: Concurrent core of CLL

• Related Work and Conclusion

Page 9: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 9

Introduction to CLL

• Typed programming language

• Logical design– Functional features from proof reduction– Concurrent features from proof synthesis

• Type system based on a variant of intuitionistic linear logic

Page 10: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 10

What CLL is and isn’t

• Foundational/conceptual language– Prototype implementation – Not a full-fledged language yet

• Functional part:– Type-safety

• Concurrent part:– Preserves well-typedness of terms– No progress guarantee: Computations may

deadlock

Page 11: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 11

CLL: Layered design

• Three layer design• fCLL: Functional language• lCLL: Concurrent + Functional language

– Concurrent computation can call functional computation

• full-CLL: Concurrent + Functional language– Functional computation can call upon

concurrent computation– Omitted from this talk

Page 12: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 12

Underlying Logic• Intuitionistic

– captures functional specifications

• Linear – captures concurrency in lCLL

• Equipped with a monad – captures effects (deadlocks) in full-CLL– used in communication

• First-order (dependent types)– precise functional specifications– model communication channels, process ids, etc. in

lCLL

Page 13: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 13

Where we are …

• Design Goals

• Computation-as-proof reduction, Computation-as-proof search, Computation-as-proof search and reduction

• Introduction to CLL

• fCLL: Functional core of CLL

• lCLL: Concurrent core of CLL

• Related Work and Conclusion

Page 14: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 14

fCLL: Type Syntax

Page 15: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 15

fCLL: Program Syntax

• Programs (P) divided into syntactic classes

Page 16: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 16

fCLL: Semantics

• Call-by-value reduction semantics• Typing judgments:

Non-linear variables: Linear variables:

Page 17: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 17

fCLL: Type-Safety

• Type-preservation:

If P : T and P reduces to P’, then P’ : T.

• Progress:

If P : T, then P is a value or P reduces.

Page 18: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 18

Where we are …

• Design Goals

• Computation-as-proof reduction, Computation-as-proof search, Computation-as-proof search and reduction

• Introduction to CLL

• fCLL: Functional core of CLL

• lCLL: Concurrent core of CLL

• Related Work and Conclusion

Page 19: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 19

lCLL: Concurrent Core of CLL

• Computation-as-proof search and reduction

• Functional part:– Includes all of fCLL directly

• Concurrent part:– Arises from proof-synthesis– Resembles asynchronous calculus in

communication primitives

• CHAM presentation

Page 20: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 20

lCLL: Syntax

fCLL values with types

fCLL programs with types

Replicable Linear

Page 21: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 21

Type Constructs in lCLL

• Each construct has an intuitive meaning

Page 22: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 22

Interaction Example

Server: Listens on channel k

Computes factorials

Client: Listens on channel k’

k <6>

Compute factorial(6) = 720

k’ <720> Return 720 and terminate

Objective: compute factorial(6)

Page 23: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 23

Modeling messages in lCLL

• Channels k, k’,… modeled as index terms

• messint : type constructor of arity 1

• (messint k) is the type of integer messages on channel k

Page 24: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 24

Server Process

1. Wait for message “y” on channel k

2. Extract content of y and compute its factorial

3. Send computed result on channel k’

Page 25: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 25

Client Process

Send message 6 on channel k

Wait for message “x” on channel k’, and return content of x

Parallel composition

Page 26: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 26

lCLL: Semantics

• Type-directed rewrite semantics

• Three alternating rewrite phases:1. fCLL’s functional reduction (proof-term

reduction)

2. Structural decomposition

3. Synchronization (proof-term synthesis)

Page 27: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 27

1. Functional reduction

• Applies to all non-values in a configuration• If P : T, then P’ : T by fCLL’s type-preservation

theorem!

Page 28: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 28

2. Structural decomposition

• Resemble -calculus structural decomposition

Page 29: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 29

3. Synchronization

• Takes several values in a configuration and links them to form a larger program

• Site of all communication

• Directed by types

• Three golden rules:– Applies to asynchronous types only– Applies to values only– Result must have type {S}

Page 30: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 30

Synchronization examples

Page 31: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 31

Incorrect Synchronizations

Not of type {S}

Not a value

Page 32: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 32

Why have a monad {…}

• Atomicity - {…} determines how far each synchronization will go

Page 33: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 33

Interaction Example

Server: Listens on channel k

Computes factorials

Client: Listens on channel k’

k <6>

Compute factorial(6) = 720

k’ <720> Return 720 and terminate

Objective: compute factorial(6)

Page 34: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 34

Example: Execution in lCLL

Page 35: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 35

Example: Execution in lCLL

Page 36: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 36

Are the rewrite rules ad-hoc?

• No!

• Rewrite rules obtained from a proof search procedure by adding proof-terms

• See paper for details

Page 37: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 37

What does lCLL give us?

• A “well-typedness” preservation result:

If every program in a lCLL configuration is well-typed with the type indicated, then after any number of rewrite steps, the same property still holds.

• Concurrent computation does not interfere with functional type-safety!

Page 38: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 38

What lCLL lacks …

• Concurrent computations in lCLL may deadlock

• Reason: Linear logic is not expressive enough

Page 39: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 39

Where we are …

• Design Goals

• Computation-as-proof reduction, Computation-as-proof search, Computation-as-proof search and reduction

• Introduction to CLL

• fCLL: Functional core of CLL

• lCLL: Concurrent core of CLL

• Related Work and Conclusion

Page 40: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 40

Closely Related Work

• Facile, CML, Concurrent Haskell, JOCaml– Combine functional and concurrent prog.

• Type Systems for -calculus

[Kobayashi ’03], [Igarashi, Kobayashi ’03], [Yoshida ’04], [Kobayashi, Pierce, Turner ’95], …– Properties like type-safety, deadlock freedom

• Computational Interpretations of Linear Logic [Abramsky ’93]– Classical linear logic and concurrency

Page 41: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 41

Closely Related Work

• MSR Framework [Cervesato ’04]– Rewriting as logical rules

• CLF [Cervesato, Pfenning, Walker, Watkins ’03],

LolliMon [ Lopez, Pfenning, Polakow, Watkins’05]– Share the same underlying logic

• Lots more …

Page 42: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 42

Future Work

• A realistic implementation

• Challenges– Low level of concurrency in CLL– Linear functional programming– Type inference

• Reasoning about deadlock freedom and program correctness

Page 43: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 43

Conclusion

• CLL– Built from a logic– Functional part arises from proof reduction– Concurrent part arises from proof search– Type-safety for functional reduction

• Concurrency does not interfere with functional type-safety

Page 44: 1 Type-Directed Concurrency Deepak Garg, Frank Pfenning {dg+, fp+}@cs.cmu.edu Carnegie Mellon University

Type-Directed Concurrency. CONCUR 2005 44

In the paper …

• Multi-sorted index terms

• Existential types– Creating channel names

• Encoding the asynchronous -calculus

• A three phase classification of fCLL programs– Terms, expressions and monadic terms