programming language concepts, cs2104 lecture …pvr/ds/cs2104/lectures/...process messages ordered...

58
1 2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 1 Programming Language Concepts, cs2104 Lecture 11 (2003-10-31) Seif Haridi Department of Computer Science, NUS 2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 2 Overview Previous lecture message sending protocols soft real-time programming This lecture agents with state cells ports revisited state encapsulation objects and classes Next lecture overview outlook

Upload: others

Post on 31-Aug-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

1

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 1

Programming Language Concepts, cs2104

Lecture 11 (2003-10-31)

Seif HaridiDepartment of Computer Science,

[email protected]

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 2

Overview� Previous lecture� message sending� protocols� soft real-time programming� This lecture� agents with state� cells� ports revisited� state encapsulation� objects and classes� Next lecture� overview� outlook

Page 2: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

2

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 3

Agents andMessage Passing

Concurrency

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 4

Client-Server Architectures� Server provides some service� receives message� replies to message� example: web server, mail server, …� Clients know address of server and use service by sending messages� Server and client run independently

Page 3: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

3

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 5

Peer-to-Peer Architectures� Similar to Client-Server:� every client is also a server� communicate by sending messages to each other� We call all these guys (client, server, peer)agent� In the book this is called portObject

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 6

Common Features� Agents� have identity mail address� receive messages mailbox� process messages ordered mailbox� reply to messages pre-addressed return letter� Now how to cast into a programming language model?

Page 4: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

4

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 7

Message Sending� Message data structure� Address port (oz concept)� Mailbox stream of messages� Reply dataflow variable in message

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 8

Port� Port name:[S]� stores stream S under unique address (oz name)� stored stream changes over time� The S is tail of message stream� sending a message M adds message to end

Page 5: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

5

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 9

Message Sending to Port� Port a:[S]� Send M to a� read stored stream S from address a� create new store variable S’� bind S to M|S’ (cons)� update stored stream to S’� The state of the port changes over time� a:[S] before sending the message M� a:[S’] after receiving the message M

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 10

Port Procedures� Port creationP={NewPort Xs}� Message sending{Send P X}

Page 6: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

6

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 11

Exampledeclare S PP={NewPort S}{Browse S}� Displays initially S (or _)

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 12

Example

declare S PP={NewPort S}{Browse S}� Execute {Send P a}� Shows a|_

Page 7: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

7

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 13

Exampledeclare S PP={NewPort S}{Browse S}� Execute {Send P b}� Shows a|b|_

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 14

Question

declare S P

P={NewPort S}

{Browse S}

thread {Send P a} end

thread {Send P b} end� What will the Browser show?

Page 8: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

8

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 15

Questiondeclare S P P={NewPort S}{Browse S}thread {Send P a} endthread {Send P b} end� What will the Browser show?� Either a|b|_ or b|a|_� non-determinism: we can’t say what

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 16

Answering Messages I� Include the port P’ of the sender in the message:� {Send P pair(Message P’)}� Receiver sends answer message to P’� {Send P’ AnsMessage}� Traditional view

Page 9: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

9

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 17

Answering Messages� Do not reply by address, use something like pre-addressed reply envelope� dataflow variable!!!� {Send P pair(Message Answer)}� Receiver can bind Answer!� Answer = AnsMessage

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 18

A Math Agentproc {Math M}case Mof add(N M A) then A=N+M[] mul(N M A) then A=N*M[] int(Formula A) thenA = …endend

Page 10: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

10

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 19

Making the Agent WorkMP = {NewPort S}proc {MathProcess Ms}case Ms of M|Mr then{Math M} {MathProcess Mr}endendthread {MathProcess S} end

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 20

Smells of Higher-Order…proc {ForAll Xs P}case Xsof nil then skip[] X|Xr then {P X} {ForAll Xr P}endend� Call procedure P for all elements in Xs

Page 11: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

11

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 21

Smells of Higher-Order…� Using ForAll , we haveproc {MathProcess Ms}{ForAll Xs Math}end

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 22

Making the Agent Work

MP = {NewPort S}

thread {ForAll S Math} end

Page 12: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

12

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 23

Making the Agent WorkMP = {NewPort S}thread for M in S do {Math M} endend

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 24

Smells Even Stronger…fun {NewAgent Process}Port Streamin Port={NewPort Stream}thread {ForAll Process} endPortend

Page 13: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

13

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 25

Why Do Agents/Processes Matter?� Model to capture communicating entities� Each agent is simply defined in terms of how it

replies to messages� Each agent has a thread of its own� no screw-up with concurrency� we can easily extend the model so that each agent have a state (encapsulated)� Extremely useful to model systems!

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 26

Summary� Ports for message sending� use stream (list of messages) as mailbox� port serves as unique address� Use agent abstraction� combines port with thread running agent� simple concurrency scheme� Introduces non-determinism… and state!

Page 14: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

14

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 27

Message Sending

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 28

Message Sending: Properties� Message sending � asynchronous� ordered per thread� no order from multiple threads� first-class messages

Page 15: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

15

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 29

Asynchronous Sending

P={NewPort S}

thread … {Send P M} … end (1)thread … {Process S} … end (2)� Asynchronous: (1) continues immediately

after sending� Sender does not know when message processed� message processed eventually

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 30

Asynchronous Reply� Sender sends message containing dataflow variable for answer� does not wait for receipt� does not wait for answer when sending� Waiting for answer, only if answer needed� Helps to avoid latency� sender continues computation� receiver might already deliver message

Page 16: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

16

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 31

Synchronous Sending� Sometimes more synchronization needed� sender wants to synchronize with receiver upon receipt of message� known as: handshake, rendezvous� Can also be used for delivering reply� sender does not wait for reply computed, or� sender waits until reply computed

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 32

Waiting for Variables� How to express that execution resumes only if variable X bound?� Notice that conditional is suspendable

proc {Wait X}

if X==1 then skip else skip end

end

Page 17: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

17

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 33

Synchronous Send

proc {SyncSend P M}

Ack in {Send P M#Ack} {Wait Ack}

end

proc {Process MA}

case MA of M#Ack then

Ack=okay …

end

end

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 34

Asynchronous Send� Synchronous send can be turned into asynchronous send again by use of threads

proc {AsyncSyncSend P M}

thread {SyncSend P M} end

end� Sending: variants can be mutually expressed

Page 18: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

18

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 35

Message Order� Order on same thread: A always before Bthread

… {Send P A} … {Send P B} …

end� No order among threadsthread … {Send P A} … end

thread … {Send P B} … end

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 36

Messages� Important aspect of agents� messages are first-class values: can be computed, tested, manipulated, stored� can contain any data structure including procedure values� First-class messages are expressive� messages received stored in a log� agent forwards by adding time-stamp to message

Page 19: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

19

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 37

A Compute Server

proc {ComputeAgent M}

case M

of run(P) then {P}

[] run(F R) then R={F}

end

end� Runs as an agent in its own thread� Executes procedures contained in messages

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 38

Distribution� Spawn computations across several computers connected by network� Message sending important way to structure distributed programs� Compute servers make sense in this setting� Oz: transparent distribution

Page 20: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

20

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 39

Soft RealtimeProgramming

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 40

Soft Realtime Programming� Realtime� control computations by time� animations, simulations, timeouts, …� Soft� suggested time� no time guarantees� no hard deadlines as for controllers, etc.

Page 21: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

21

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 41

Delay

{Delay N}

suspends the thread for Nmilliseconds� Useful for building abstractions� timeouts� repeating actions

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 42

Protocols

Page 22: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

22

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 43

Protocols� Protocol: rules for sending and receiving messages� programming with agents� Examples� broadcasting messages to group of agents� choosing an agent� Important properties of protocols� deadlock free

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 44

Broadcast� Just send message M to all agents As

proc {Broadcast As M}

{ForAll As proc {$ A}

{Send A M}

end}

end

Page 23: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

23

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 45

Reminder: ForAll

proc {ForAll Xs P}

case Xs

of nil then skip

[] X|Xr then {P X} {ForAll Xr P}

end

end

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 46

Choosing an Agent� Example: choosing the best lift� More general: seeking agreement� General idea:� Master Floor agent� send message to all slaves containing answer variable� Slaves Lift agents� answer by binding in the answer variable� if decision to be known, use dataflow variable again

Page 24: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

24

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 47

Choosing an Agent� Master to one slave{Send S m(… Reply)}case Reply of r(… Status) then

if … then Status=reserveelse Status=rejectend� Slave

case M of m(… Reply) thenReply=r(… Status)if Status==reject then … else … end

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 48

Choosing an Agent� Master: � sends original message including variable for Reply� suspends until Reply bound� Slave:� receives message� binds Reply, includes variable for Status� suspends until Status bound� Master: � decides and binds Status� Slave:� continues according to Status

Page 25: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

25

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 49

Master to Multiple Slaves

Rs={Map Ss fun {$ S}Reply in{Send S m(… Reply)}Reply

end}

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 50

Choosing an Agent� Master: � sends original message including variable for Reply to each slave agent (List of messages)� suspends on list of Reply messages� Slave:� receives message� binds Reply, includes variable for Status� suspends until Status bound� Master: � decides and binds Status in each Reply message� Slave:� continues according to Status

Page 26: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

26

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 51

Avoiding Deadlock� Master can only proceed, after all slaves answered� will not process any more messages until then� Slave can only proceed, after master answered� will not process any more messages until then� What happens if multiple masters for same slaves?

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 52

Deadlock

M

N

B

A

C

D

Master Slave

Page 27: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

27

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 53

Deadlock

M

N

B

A

C

D

Master Slave

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 54

Deadlock

M

N

B

A

C

D

Master Slave

Page 28: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

28

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 55

Deadlock

M

N

B

A

C

D

Master Slave

blocks!

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 56

Deadlock

M

N

B

A

C

D

Master Slave

blocks!

blocks!

Page 29: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

29

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 57

Deadlock

M

N

B

A

C

D

Master Slave

blocks!

blocks!� M blocks A, waits for B� N blocks B, waits for A� Deadlock!!!!

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 58

Avoiding Deadlock� Force all masters to send in order to all slaves:� First slave A, then B, then C, …� Guarantee: If A available, all others will be available� That is as in lab assignment� Use an adaptor� access to slaves through one single master

Page 30: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

30

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 59

Adaptor

M

N

B

A

C

D

Master Slave

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 60

Summary� Protocols for coordinating agents� Can lead to deadlocks� Simple structure best� Details: Distributed Systems (and algorithms)

Page 31: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

31

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 61

Next Lecture� Agents with state� State: cells and abstract data types� Objects

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 62

Agents With State

Page 32: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

32

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 63

Agents� Receive messages� To be useful they need to maintain state� changing over time� Model: agent a is modeled by functionstate ×××× message � state

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 64

Agent States

s0 s1 s2 …a a a

portmessages

Page 33: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

33

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 65

Defining an Agent� Agent� how it reacts to messages� how it transforms the state� from which initial state it starts� Additionally� agent might compute by sending messages to other agents

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 66

Example: A Cell Agent

fun {CellProcess S M}

case M

of assign(New) then

New

[] access(Old) then

Old=S S

end

end

Page 34: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

34

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 67

The NewAgent Abstraction� To create a new agent with state� create a port for receiving messages� create a thread that executes agent� start agent by applying its function an initial state� agent made available by the port

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 68

NewAgent

fun {NewAgent Process InitState}

Port Stream

in

Port={NewPort Stream}

thread

%% Execute agent…

end

Port

end

Page 35: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

35

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 69

Executing the Agent

threadproc {Execute State Stream}

case Stream of M|Rest thenNewState={Process State M}

in{Execute NewState Rest}

endend

in{Execute InitState Stream}

end

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 70

Structure of Execute� Does Execute ring a bell with you:� starting from initial state� successively applying agent to state and message� agent is binary operation� …�

Page 36: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

36

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 71

Structure of Execute� Does Execute ring a bell with you:� starting from initial state� successively applying agent to state and message� agent is binary operation� …� Of course: we are folding!� which folding is it: left or right…

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 72

Reminder: FoldL

fun {FoldL Xs F S}

case Xs

of nil then S

[] X|Xr then {FoldL Xr F {F S X}}

end

end

Page 37: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

37

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 73

Executing With FoldL

thread

{FoldL Stream Process InitState}

end� Wait!

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 74

Executing With FoldL

thread

{FoldL Stream Process InitState}

end� This is wrong! Why?

Page 38: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

38

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 75

Executing With FoldL

thread

{FoldL Stream Process InitState}

end� Right: FoldL returns result…

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 76

Executing With FoldL

thread Dummy in

Dummy={FoldL Stream Process

InitState}

end

Page 39: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

39

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 77

Complete NewAgent

fun {NewAgent Process InitState}

Port Streamin

Port={NewPort Stream}

thread Dummy inDummy={FoldL Process Stream

InitState}end

Port

end

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 78

Creating a Cell Agent

declare

CA = {NewAgent CellProcess 0}� Cell agent� initialized with zero as state

Page 40: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

40

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 79

A Simple Task

proc {Inc C}N

in{Send C access(N)} {Send C assign(N+1)}

end� Increment the cell’s content by one� Get the old value� Put the new value� Does this work? NO! NO! Why?

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 80

A Simple Task Screwed…

C={NewAgent …}

thread {Inc C} end

thread {Inc C} end� We insist on result being 2!� sometimes: 2� sometimes: 1� Why?

Page 41: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

41

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 81

Execution Sketch A: Good� Thread 1� executes access: value got: 0� Thread 1� executes assign: value put: 1� Thread 2� executes access: value got: 1� Thread 2� executes assign: value put: 2

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 82

Execution Sketch B: Bad� Thread 1� executes access: value got: 0� Thread 2� executes access: value got: 0� Thread 1� executes assign: value put: 1� Thread 2� executes assign: value put: 1

Page 42: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

42

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 83

What Is Needed� We need to avoid that multiple access and assign operations get out of order� We need to combine access and assign into one operation� we cannot guarantee that not interrupted� we can guarantee that state is correct� immediately put a dataflow variable� Also: called atomic exchange� operation is atomic

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 84

A Cell Agent with Exchange

fun {CellProcess S M}

case M

of assign(New) then New

[] access(Old) then Old=S Old

[] exchange(New Old) then

Old=S New

end

end

Page 43: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

43

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 85

Incrementing Rectified

proc {Inc C}

New Old

in

{Send C exchange(New Old)}

New = Old+1

end

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 86

State and Concurrency� Difficult to guarantee that state is maintained consistently in a concurrent setting� Typically needed: atomic execution of several statements together� Agents guarantee atomic execution

Page 44: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

44

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 87

Other Approaches� Atomic exchange� Low level� hardware: test-and-set� Locks: allow only one thread in a “critical section”� monitor: use a lock together with a thread� generalizations to single writer/multiple reader

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 88

Maintaining State

Page 45: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

45

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 89

Maintaining State� Agents maintain implicit state� state maintained as values passed as arguments� Agents encapsulate state� state is only available within one agent� in particular, only one thread� With cells we can have explicit state� programs can manipulate state by manipulating cells

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 90

Explicit State� So far, the models considered do not have explicit state� Explicit state is of course useful� algorithms might require state (such as arrays)� the right model for the task

Page 46: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

46

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 91

Using State� Programs should be modular� composed from components� Some components can use state� use only, if necessary� Components from outside (interface) can still behave like functions

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 92

State: Example� Lab assignment 3: computing the frequency map� consider all bytes, increase frequency by creating a

new record with a just a single field changed� Much more efficient: using state with dictionaries

Page 47: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

47

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 93

State: Abstract Datatypes� Many useful abstractions are abstract datatypes using encapsulated state� arrays� dictionaries� queues� …

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 94

Cells

Page 48: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

48

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 95

Cells as Abstract Datatypes� C={NewCell X}� creates new cell C� with initial value X� X={Access C}� returns current value of C� {Assign C X}� assigns value of C to be X� {Exchange C X Y}� atomically assigns C to Y and returns old value X

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 96

Cells� Are a model for explicit state� Useful in few cases on itself� Device to explain other stateful datatypessuch as arrays

Page 49: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

49

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 97

Array Model� Simple array� fields indexed from 1 to n� values can be accessed, assigned, and exchanged� Model: tuple of cells

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 98

Arrays� A={NewArray N I}� create array with fields from 1 to N� all fields initialized to value I� X={ArrayAccess A N}� return value at position N in array A� {ArrayAssign A N X}� set value at position N to X in array A� {ArrayExchange A N X Y}� exchange value at position N in A from X to Y

Page 50: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

50

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 99

Homework� Implement array abstract datatype� use tuple of cells

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 100

Ports Revisited

Page 51: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

51

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 101

Ports� Provide send operation� Always maintain tail of stream� Sending is appending cons with message

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 102

How to Program Ports?� Idea: cell keeps current tail of stream� invariant: cell keep unassigned logic variable!� Sending� access current tail of stream� appending message� assign current tail of stream

…must of course to be atomic with exchange!what could happen otherwise?

Page 52: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

52

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 103

Ports from Cells: Bad

fun {NewPort Stream}

{NewCell Stream}

end

proc {Send P M}

Old New

in

{Exchange P Old New}

Old = M|New

end

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 104

Why Bad?� Port is a cell in previous solution� I can break the port abstraction…� How: just put some junk into the cell{Assign P crash}

Page 53: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

53

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 105

Real Ports…� Abstraction cannot be compromised� sending messages always works� However: everybody is allowed to send messages to a port

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 106

How to Fix Ports from Cells� Confine the cell to the send operation� by lexical scoping� Invariant can never be broken

Page 54: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

54

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 107

Ports from Cells

fun {NewSend Stream}C={NewCell Stream}proc {Send M}

Old New in

{Exchange C Old New}Old = M|New

endin

Sendend

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 108

Ports from Cells

fun {NewSend Stream}C={NewCell Stream}proc {Send M}

Old New in

{Exchange C Old New}New = M|Old

endin

Sendend

only send can use cell

Page 55: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

55

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 109

Ports from Cells� Returns the send procedure� Example of use:S={NewSend Xs}

{Browse Xs}

{S a}

{S b}

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 110

State Encapsulation� State is encapsulated by construction of NewSend� Encapsulation� guarantees invariant (cells maintains stream tail)� makes the abstraction “secure”

Page 56: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

56

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 111

Objects and Classes

Outlook

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 112

Objects� Object is one convenient way of encapsulating state� only methods can access state� important invariants can be secured� As well as very useful to model objects of the real world

Page 57: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

57

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 113

Model for Objects� Methods are procedures� have access to state� restrict access to state� State of an object� record of cells� similar to our construction of arrays

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 114

Classes� Describe how objects are constructed� initial state� methods� Classes can be constructed from other classes by inheritance

Page 58: Programming Language Concepts, cs2104 Lecture …pvr/ds/CS2104/Lectures/...process messages ordered mailbox reply to messages pre-addressed return letter Now how to cast into a programming

58

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 115

Classes and Objects� A full-blown object system can be obtained easily from� records state field name � cells state field value� procedures methods� lexical scoping access from methods to state� First-class procedures are very powerful� allow to program inheritance and object creation

2003-10-31 S. Haridi, CS2104, L11 (slides: C. Schulte, S. Haridi) 116

Have a Nice Weekend