Com S 541 Lecture -*- Outline -*- * Port Objects (5.2) ** abstraction ------------------------------------------ PORT OBJECTS (5.2) def: a port object is a "combination of one or more ports and a stream object" Extends stream objects by: - allowing multiple - can be embedded in data structures Example: declare P in local S in {NewPort S P} thread {ForAll S Browse} end end ------------------------------------------ ... sends = many to one communication Also known as "agents" or "active objects" Q: How would you make an abstraction of this kind of thing? ------------------------------------------ MAKING PORTS WITH STATE declare fun {NewPortObject Init Fun} Sin Sout in thread {FoldL Sin Fun Init Sout} end {NewPort Sin} end ------------------------------------------ Q: How does {NewPortObject [a b c] F 0} execute? It does {F {F {F 0 a} b} c} Q: What does NewPortObject return? the final port Q: What happens to the final state, if any? It's ignored, since Sout is not returned and it's local ------------------------------------------ MAKING STATELESS PORTS declare fun {NewPortObject2 Proc} Sin in thread for Msg in Sin do {Proc Msg} end end {NewPort Sin} end ------------------------------------------ *** example (5.2.2) See BallToss.oz ------------------------------------------ TO DO Write NewMerge: }: > such that the following code can be used: \insert 'NewMerge.oz' declare P S thread {Browse S} end P={NewMerge S} thread for I in 1..10 do {Send P I} {Delay 500} end end thread for J in 201..210 do {Send P J} {Delay 300} end end ------------------------------------------ \insert 'NewPortObject2.oz' declare fun {NewMerge ?Sout} S in Sout = S {NewPort S} end *** Reasoning (5.2.3) modular correctness reasoning 1. show each ADT is correct wrt to its invariant 2. show the program with the ports is correct look at possible sequences define causality reason about state transitions