1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan Chair of Software Engineering 2 Lecture 7: Modeling with the Pi-Calculus. Chair of Software Engineering The Pi-calculus Super-set of CCS. Allows monadic name communication. Scope extrusion is fundamental. Well-developed theory. Programming languages: PICT, Join, TyCO, … Chair of Software Engineering 3 Pi-Calculus Syntax P :: 0 | ( x ) P | [ x y ]P | .P | | | PP P|P !P We shall omit trailing Chair of Software Engineering :: a | a | a (b) | ab | 0’s, as usual. 4 Free and Bound Names 5 bn ( x ( y ).P ) { y} bn ( P ) bn (( x ) P ) {x} bn ( P ) bn (0) bn ( xy.P ) bn ( x .P ) bn ( P Q ) bn ( P | Q ) bn (! P ) bn ( x.P ) bn ( .P ) bn ( P ) bn (Q ) bn ( P ) bn (Q ) bn ( P ) Free names are names that are not bound. Chair of Software Engineering bn ( P ) Substitution Behaves similarly to the Lambda-Calculus substitution: replaces the free occurrences of names, many names can be substituted simultaneously, substitution may involve renaming of bound names to avoid incidental capture. Examples: (c.( c)ac.c.P )[a / c] a.( c)ac.c. P (c.( c)ac.c.P )[c / a ] a.( d )cd .d .P[d , c / c, a ] Chair of Software Engineering 6 Pi-Calculus Semantics (1) ( P a (b).Q ) | ( ac.R S ) Q[c / b] | R P P' P |Q P' |Q P P' ( x ) P ( x ) P ' P Q Q' P' P P' Chair of Software Engineering 7 Pi-Calculus Semantics (2) P0 P P Q Q P ( P Q ) R P (Q R ) ( x)0 0 P |0 P P |Q Q | P ( P | Q ) | R P | (Q | R) ( x )( y ) P ( y )( x ) P ( x ) P | Q ( x )( P | Q ) if x fn(Q ) ! P P |! P Chair of Software Engineering 8 Example: Scope Extrusion P ( c)ac.c.P ', Q a( x ).bx.Q ', R b( y ). y .R ' a P Q {c} {c} c b {c} R P | Q | R ( c)(c.P ' | bc.Q '[c / x ]) | R ( c)( c.P ' | Q '[c / x ] | c . R '[c / y ]) ( c)( P ' | Q '[c / x ] | R '[c / y ]) Chair of Software Engineering 9 Encoding Polyadic Interactions c (a1,..., an ).P ( x )(cx. xa1. ... . xan.P) with x fv ( P, a1,..., an ) c(a1,..., an ).P c( x ). x(a1). ... . x(an ).Q with x fv (Q, a1,..., an ) We can use these shortcuts transparently. We introduce notations: x ( x1,..., xm) P Chair of Software Engineering x1,..., xm ( x1)...( xm) P 10 Encoding Named Processes Defining named processes by adding a context to (plain) terms. def and and and in K 1( x1) P1 K 2( x 2 ) P 2 ... Kn ( xn ) Pn Q ( yK 1,..., yKn )(! yK 1( x1).P1 | ... |! yKn( xn ).Pn | Q ') where Q ' Q[ yK 1 x1,..., yKn xn / K 1( x1),..., Kn( xn)] Chair of Software Engineering 11 Named Process Example 12 def Get (i, o) i ( x ).Put (i, o, x ) and Put (i, o, x ) ox.Get (i, o) in Get ( p, q) ( xGet , xPut )( Chair of Software Engineering ! xGet (i, o).i ( x ). xPut i o x | ! xPut (i, o, x ).ox. xGet i o | xGet p q ) The Mobile Phone Example Borrowed from Milner(1991). Chair of Software Engineering 13 The Mobile Phone Example (2) System Car(t, s) ( talki, switchi , givei, alerti : i 1, 2) ( Car (talk 1, switch1) | Base1 | IdleBase2 | Centre1 ) t.Car(t, s) s(t ', s ').Car(t ', s ') Centre1 give1talk 2 switch 2.alert 2.Centre 2 Centre2 give2talk 1switch1.alert1.Centre1 Chair of Software Engineering 14 The Mobile Phone Example (3) Base(t , s, g , a ) Basei Base(talki, switchi, givei, alerti ) Base(t , g , s, a ) Basei t.Base(t , s, g , a ) g (t ' s ').st ' s '. IdleBase(t, s, g , a ) a.Base(t , g , s, a ) IdleBase(talki, switchi, givei, alerti ) What happens when the CENTRE give the switching order? Chair of Software Engineering 15 Encoding Booleans Now a process has to provide a port, in order to receive an answer! True( r ) False( r ) if r then P else Q And (b1, b2, r ) Chair of Software Engineering ( b)( rb |!b(t , f ).t ) ( b)( rb |!b(t , f ). f ) ( t , f )(r (b).b (t , f ).(t.P | f .Q )) if b1 then r b2 else False(r) 16 Encoding Numerals n ( x, z ) x . x . ... . x . z n times Define Copy so that: ( x z )( n ( x, z ) | Copy ( xz, yw)) n ( y, w) Solution: Copy ( xz, yw) x. y.Copy ( xz, yw) z.w Define Successor so that: ( x z)(n( x, z) | Succ( xz, yw)) n 1( y, w) Solution: Succ( xz, yw) Chair of Software Engineering y.Copy ( xz, yw) 17 Addition and Multiplication Add ( x1 z1 , x2 z2 , yw) x1. y . Add ( x1 z1 , x2 z2 , yw) z1.Copy ( x2 z2 , yw) Mult ( x1 z1 , x2 z2 , yw) ( x2 ' z2 ', x2 '' z2 '', xr zr )( Double( x2 z2 , x2 ' z2 ', x2 '' z2 '') | Add ( x2 ' z2 ', xr zr , yw) | Mult ( x1 z1 , x2 '' z2 '', yw) ) Chair of Software Engineering 18 Duplication & Zero Test Double( xz, y1w1 , y2 w2 ) ( y1 ' w1 ', y2 ' w2 ')( x.( Succ( y1 ' w1 ', y1w1 ) | Succ( y2 ' w2 ', y2 w2 ) | Double( xz, y1 ' w1 ', y2 ' w2 ')) z.( w1 | w2)) Iszero( r, xz, yw) Double( xz, yw, y ' w ') | ( y ' w ') ( y '. False ( r ) w '. True ( r )) Chair of Software Engineering 19 Encoding Lists Nil ( r ) 20 ( l )( rl |!l (n, c).n ) Cons(hd , tl , r ) ( l )( rl |!l (n, c).c (hd , tl )) Define “Pattern Matching”: match l with Nil P and Cons(hd , tl ) Q ( n, c)(l (l0 ).l0 (n, c).(n.P c(hd , tl )Q) Define Concatenation: Concat (l1 , l2 , r ) match l1 with Nil r l2 and Cons(hd , tl ) ( r ')(Concat (tl , l2 , r ') | r '(l ).Cons(hd , l , r )) Chair of Software Engineering Encoding The Lambda-Calculus (!) x.t x tu p ( x, q). t p 21 q xp p p ( x. x ) z ( q)( t p q | ( y )( qyp |! y ( r ). u r )) ( q)( x. x q | ( y )( qyp |! y ( r ). z r )) ( q)( q( x, q '). xq ' | ( y )( qyp |! y ( r ). zr )) ( q, y )( yp |! y ( r ). zr ) ( q, y )(! y ( r ). zr ) | zp zp z Chair of Software Engineering p Encoding State 22 A cell with read and write methods can be implemented in many ways. First one: ( l )( l i | Servread (l , r ) | Servwrite(l , w)) Cell ( r, w, i ) Servread (l , r ) ! r (c).l (v ).(cv | l v ) Servwrite(l , w) ! w(c, v ').l (v ).(cv | l v ') Second solution with exclusive access: Cell ( r, w, i ) ( l )( l i |!( Servread (l , r ) Servwrite(l , w))) Servread (l , r ) Servwrite(l , w) Chair of Software Engineering r (c).l (v ).(cv | l v ) w(c, v ').l (v ).(cv | l v ') Synchrony & Asynchrony Pi-Calculus communication is synchronous: ( x )( x ( y ).P | z.Q ) is blocked if x fn(Q ) But a message can be represented by: x ( y ).0 Example: Chair of Software Engineering ( z.( x ( y ) | Q ) | z . x ( y '). P ) ( x ( y ) | Q | x ( y '). P ) ( x ( y ) | Q ' | x ( y '). P ) (Q ' | P ) 23 Encoding Objects and Classes Both objects and classes correspond to processes. A class is an object with method new: (... new( r ).( self , m)( r (m).BodyObj ) ...) On the self name is first sent the list of method names m . An object is very much like a mutable cell. Example: CCell new( p ).( cell , r, w)( p ( r, w).Cell ( r, w,0)) Chair of Software Engineering 24 BBuffer using ACT++ Primitives class BBUFFER is public interface: … // as before behavior: empty = {put} partial = {put, get} full = {get} implementation: Boolean isFull, isEmpty; put (t: OBJECT) is … if (isFull) then become full; else become partial; end; OBJECT: get () is … if (isEmpty) then become empty; else become partial; end; end BBUFFER; Chair of Software Engineering 25 Example: The Bounded Buffer (1) BBuffer ! newBBuffer( r ).( put, get ).r ( put, get ). Init Init ( l , x, z )( Nil (l ) | n( x, z ) | Empty (l , x, z)) Empty (l , x, z ) Partial (l , x, z ) Put (l , x, z ) Put (l , x, z ) Get (l , x, z ) Full (l , x, z ) Get (l , x, z ) Chair of Software Engineering 26 Example: The Bounded Buffer (2) 27 Get (l , x, z ) get ( r ).l (l 0).( n, c)l 0( n, c ).c( hd , tl ). ( match tl with Nil r (hd ).Empty (tl , x, z ) and Cons( hd ', tl ') r (hd ).Partial (tl , x, z )) put ( r ).l (l 0).( n, c)l 0( n, c).c( hd , tl ). x. Iszero(b, xz, yw) | ( b, yw) if b then r ( hd ).Full (l , y , w) else r (hd ).Partial (l , y , w) Put (l , x, z ) Chair of Software Engineering Problems “Faithful” encodings, preserving types. Complexity of checking properties. Chair of Software Engineering 28 Next Some specific encodings. Verifying properties… Chair of Software Engineering 29