Core -calculi Simon Fowler Joint work with Sam Lindley and Philip - - PowerPoint PPT Presentation

core calculi
SMART_READER_LITE
LIVE PREVIEW

Core -calculi Simon Fowler Joint work with Sam Lindley and Philip - - PowerPoint PPT Presentation

Actors and Channels in Core -calculi Simon Fowler Joint work with Sam Lindley and Philip Wadler ABCD Meeting, January 2016 Actors and Channels Hopac Actors and Channels Process Actor Process Process Hopac Actors and Channels ? Hopac


slide-1
SLIDE 1

Actors and Channels in Core λ-calculi

Simon Fowler

Joint work with Sam Lindley and Philip Wadler

ABCD Meeting, January 2016

slide-2
SLIDE 2

Actors and Channels

Hopac

slide-3
SLIDE 3

Actors and Channels

Hopac

Actor Process Process Process

slide-4
SLIDE 4

Actors and Channels

Hopac

?

slide-5
SLIDE 5

Why?

  • Concise formalisations of transformations between actors

and channels

  • Explore design issues with (session) type-parameterised

actor calculi

  • Influence design of session-typed actors for Links
slide-6
SLIDE 6

𝜇ch: A channel-based 𝜇-calculus

slide-7
SLIDE 7

𝜇ch: Channel Typing Rules

slide-8
SLIDE 8

𝜇𝑑ℎ: Communication Semantics

slide-9
SLIDE 9

Actor-based Functional Languages

  • Based on the actor model (Agha, 1985), not the same

– Actors represented as lightweight processes, scheduled by RTS – No explicit notion of behaviour / become – Computation modelled directly

  • Primitives

– spawn – send – receive – (wait)

  • module(stack).
  • compile(export_all).

loop(State) -> receive {pop, Pid} -> [Hd|Tl] = State, Pid ! Hd, loop(Tl); {push, Item} -> loop([Item] ++ State) end. spawn_stack() -> spawn(stack, loop, [[]]).

slide-10
SLIDE 10

Client

The Type Pollution Problem

ChatServer ChatRoom joinRequest userJoinRequest joinOk / joinFailed joinSuccessful / roomFull

ChatServer has type Pid(joinRequest + joinSuccessful + roomFull) …wherever we use it!

slide-11
SLIDE 11

𝜇act: A core actor-based 𝜇- calculus

slide-12
SLIDE 12

𝜇act: Selected Typing Rules

slide-13
SLIDE 13

(An alternative formulation of wait)

spawnWait construct: combination of spawn and wait

slide-14
SLIDE 14

𝜇act: Configurations

Actors are a three-tuple:

Process ID Term being evaluated Mailbox

slide-15
SLIDE 15

𝜇act: Communication Semantics

slide-16
SLIDE 16

Translations: Desired Properties

  • Translation function : Actors in terms of channels
  • Type Preservation:

– Terms: – Configurations

  • Semantics Preservation

– Terms – Configurations

slide-17
SLIDE 17

Actors implemented using channels

Idea (based on Cooper et al., 2006): create a channel and use it as a mailbox, “threaded through” the translation

Mailbox Channel Result Channel Pass mailbox channel as an argument to the function

slide-18
SLIDE 18

𝜇𝑏𝑑𝑢 in 𝜇𝑑ℎ: Translation on T erms

Top-level term: create mailbox channel, pass as parameter to translation Extra parameter c’ to pass mailbox channel to the function c applied to translated function M

slide-19
SLIDE 19

𝜇act in 𝜇ch: Translation on T erms

Create a mailbox and result channel; fork a new process; evaluate result; and give to result channel Translate PID; give to mailbox channel Translate PID; take from result channel

slide-20
SLIDE 20

Translations: Desired Properties

  • Translation function : Channels in terms of actors
  • Type Preservation:

– Terms: – Configurations

  • Semantics Preservation

– Terms – Configurations

slide-21
SLIDE 21

Channels implemented using Actors

  • Idea: represent channels as processes; emulate give

and take using internal state

Channels: pair of take and give functions C can be any type, as translated functions don’t have the ability to receive from mailboxes

slide-22
SLIDE 22

Translations on T erms (the easy

  • nes)
slide-23
SLIDE 23

Translation of newCh…

let drainBufferFn = rec drainBuffer(runningState: List(C) * List(C ->C 1)): (List(C), * List(C ->C 1)) . let (vals, readers) = runningState in case vals of [] |-> (vals, readers) [v] ++ vs |-> case readers of [] |-> (vals, readers) [rFn] ++ rs |-> rFn v; drainBuffer (vs, rs) in let stepFn = rec step(state: (List(C) * List(C ->C 1)): 1. let (vals, readers) = drainBufferFn state in let msg = receive in case msg of inl v |-> step (vals ++ [v], readers) inr sendFn |-> step (vals, readers ++ [sendFn]) in let chanPid = spawn(stepFn([], [])) in let giveFn = λx. send (inl x) chanPid in let takeFn = λx. let newPid = spawn (λnewPid -> send chanPid (inr (λval -> send val newPid)); receive) in wait newPid in (takeFn, giveFn)

slide-24
SLIDE 24

Seriously though:

  • 1. Define a function to ensure that either the buffer or list
  • f blocked readers is empty at each step.

let drainBufferFn = rec drainBuffer(runningState: List(C) * List(C ->C 1)): (List(C) * List(C ->C 1)) . let (vals, readers) = runningState in case vals of [] |-> (vals, readers) [v] ++ vs |-> case readers of [] |-> (vals, readers) [rFn] ++ rs |-> rFn v; drainBuffer (vs, rs) in …

State: list of buffered values, and list

  • f send callbacks

If buffer is empty, stop If buffer is nonempty, but list of readers is, stop Otherwise, send the top value and recurse

slide-25
SLIDE 25

Seriously though:

  • 2. Define a loop function stepFn which calls

drainBufferFn, then receives a message and modifies the state

let stepFn = rec step(state: (List(C) * List(C ->C 1)): 1. let (vals, readers) = drainBufferFn state in let msg = receive in case msg of inl v |-> step (vals ++ [v], readers) inr sendFn |-> step (vals, readers ++ [sendFn]) in ...

Message can either be inl value or inr callback If a value, add to buffer If a callback, add to callback list

slide-26
SLIDE 26

let chanPid = spawn(stepFn([], [])) in let giveFn = λx. send (inl x) chanPid in let takeFn = λx. let newPid = spawn (λnewPid -> send (inr (λval -> send val newPid) chanPid); receive) in wait newPid in (takeFn, giveFn)

Seriously though:

  • 3. Spawn a new actor with empty state; define functions

for give and take

Spawn a new actor, executing stepFn give implemented by sending inl value to the channel process take: spawn new actor which sends callback to channel process, then receives result. wait for result from spawned actor.

slide-27
SLIDE 27

Still to do / the future

  • Goal: A minimal behaviourally typed actor calculus

– Conjecture: there exists a minimal session-typed actor calculus

  • …which we can do analogous translations to / from asynchronous

GV

  • …not needing recursion for the channel -> actor translation
  • …with simpler session types (no ! / ? required?)

– Influence a design for session-typed actors in Links

  • A better solution to type pollution

– Subtyping?

  • Lots of proving to do!
slide-28
SLIDE 28

Extra slides

slide-29
SLIDE 29

Actor Configuration Typing

slide-30
SLIDE 30

Session Actor Calculus Sketch

slide-31
SLIDE 31

Actor-based Functional Languages vs. the Actor Model

  • A word of caution regarding terminology!
  • Actors: a minimal concurrency model

– Unforgeable PID, message queue (mailbox) – Behaviour

A B C

hello Привет

  • 1. Send a finite set of messages

to another actor

slide-32
SLIDE 32

Actor-based Functional Languages vs. the Actor Model

  • A word of caution regarding terminology!
  • Actors: a minimal concurrency model

– Unforgeable PID, message queue (mailbox) – Behaviour

A D

  • 2. Spawn a finite set of new

actors E

slide-33
SLIDE 33

Actor-based Functional Languages vs. the Actor Model

  • A word of caution regarding terminology!
  • Actors: a minimal concurrency model

– Unforgeable PID, message queue (mailbox) – Behaviour

A

  • 3. Change behaviour: react

differently when processing next message A

slide-34
SLIDE 34

Actor-based Functional Languages vs. the Actor Model

  • Agha (1985) introduces minimal actor languages SAL and

Act, which stay very true to the core actor model:

Acquaintance List Communication List Send Message Spawn new actor Change Behaviour