180 likes | 327 Views
Types and Programming Languages. Lecture 16. Simon Gay Department of Computing Science University of Glasgow. 2006/07. Beyond Sequential Programming. To illustrate that static type systems can be of use in other contexts, we will move on from sequential programming and look
E N D
Types and Programming Languages Lecture 16 Simon Gay Department of Computing Science University of Glasgow 2006/07
Beyond Sequential Programming To illustrate that static type systems can be of use in other contexts, we will move on from sequential programming and look at two areas: concurrent programming and security. First, concurrent programming. There are many ways of formulating concurrent programming. We will look at the pi calculus ( calculus), which is a core concurrent programming language with the following distinctive features: • Point-to-point communication between concurrent processes. • Channels as first class values. • Dynamic creation of channels. Types and Programming Languages Lecture 16 - Simon Gay
Pi Calculus Syntax The syntax of processes is defined by this grammar: P ::= stop inactive process | out x(e,...,f) asynchronous output | inp x(y,...,z);P input (y,...,z bound with scope P) | new(x);P channel creation (x is bound with scope P) | P | P parallel | repeat P replication x,y stand for channel names or variables (no need to distinguish) e stands for an expression of SEL (say), so that we have some values and operations (this is useful for examples, although it is possible to encode all computation by means of channels) Types and Programming Languages Lecture 16 - Simon Gay
Example: Simple Messages (out a(1) | inp b(x); P(x) ) | (inp a(y); out b(y+1) ) (inp b(x); P(x) ) | out b(1+1) P(2) Abbreviation: write out x(e,...,e); P for out x(e,...,e) | P Types and Programming Languages Lecture 16 - Simon Gay
Example: Client/Server Server | Client1 | Client2 Server = repeat inp a(x); inp x(y); out x(y+1) Client1 = new (c); out a(c); out c(2); inp c(x); C(x) Client2 = new (d); out a(d); out d(3); inp d(x); D(x) repeat (inp a(x); inp x(y); out x(y+1)) | new (c); out a(c); out c(2); inp c(x); C(x) | new (d); out a(d); out d(3); inp d(x); D(x) repeat (inp a(x); inp x(y); out x(y+1)) | inp a(x); inp x(y); out x(y+1) | new (c); out a(c); out c(2); inp c(x); C(x) | new (d); out a(d); out d(3); inp d(x); D(x) Types and Programming Languages Lecture 16 - Simon Gay
Example: Client/Server repeat (inp a(x); inp x(y); out x(y+1)) | inp a(x); inp x(y); out x(y+1) | new (c); out a(c); out c(2); inp c(x); C(x) | new (d); out a(d); out d(3); inp d(x); D(x) inp c(y); out c(y+1) | out c(2); inp c(x); C(x) (several steps) C(3) repeat (inp a(x); inp x(y); out x(y+1)) | inp a(x); inp x(y); out x(y+1) | new (d); out a(d); out d(3); inp d(x); D(x) | inp c(y);... | out c(2);... reducing inp d(y); out d(y+1) | out d(3); inp d(x); D(x) (several steps) D(4) Types and Programming Languages Lecture 16 - Simon Gay
Client/Server: Structure Server (replicated thread) a Client1 Client2 Client 1 creates channel c, Server spawns a thread Server thread Server thread Server thread Server (replicated thread) c d c a Client2 Client1 Client2 Client1 Types and Programming Languages Lecture 16 - Simon Gay
The Reduction Relation The reduction relation on processes is defined by a few rules: (R-Comm) Values v are channel names or values of SEL. P[v/y,...] is capture-avoiding substitution (rename bound vars if necessary) (R-Par) (R-New) Reduction takes place within parallel combinations and within the scope of new, but not within an input. Types and Programming Languages Lecture 16 - Simon Gay
Structural Equivalence To factor out inessential aspects of the way processes are written, introduce the structural equivalence relation. (E-Refl) (E-Symm) (E-Trans) Freely rearrange parallel processes: (E-Com) (E-Assoc) Garbage collection: (E-ParStop) (E-NewStop) Types and Programming Languages Lecture 16 - Simon Gay
Structural Equivalence Order of channel creation: (E-NewNew) Scope expansion: (E-NewPar) fn(P) is the set of free names in P Replication: (E-RepPar) Types and Programming Languages Lecture 16 - Simon Gay
Structural Equivalence The following rules make structural equivalence into a congruence: equivalence is preserved by all the syntactic constructions. So we refer to structural congruence. (E-Inp) (E-New) (E-Par) (E-Rep) Types and Programming Languages Lecture 16 - Simon Gay
Structural Equivalence and Reduction Finally we add a rule which makes the connection between structural equivalence and the reduction relation. (R-Struct) This allows structural equivalence to be used to rewrite a process before or after a reduction step. Structural equivalence allows us to rewrite until an output and a matching input are adjacent; then (R-Comm) gives us a reduction, often inside a parallel collection of processes (using rule (R-Par)) or within the scope of a new channel (using rule (R-New)). Types and Programming Languages Lecture 16 - Simon Gay
Example: Client/Server Server | Client1 = repeat (inp a(x); inp x(y); out x(y+1)) | new (c); out a(c); out c(2); inp c(x); C(x) (E-Rep) inp a(x); inp x(y); out x(y+1) | repeat (inp a(x); inp x(y); out x(y+1)) | new (c); out a(c); out c(2); inp c(x); C(x) (E-Com) repeat (inp a(x); inp x(y); out x(y+1)) | inp a(x); inp x(y); out x(y+1) | new (c); out a(c); out c(2); inp c(x); C(x) Types and Programming Languages Lecture 16 - Simon Gay
Example: Client/Server (E-NewPar) repeat (inp a(x); inp x(y); out x(y+1)) | new (c); (inp a(x); inp x(y); out x(y+1) | out a(c); out c(2); inp c(x); C(x)) R-Comm, R-New, R-Par repeat (inp a(x); inp x(y); out x(y+1)) | new (c); (inp c(y); out c(y+1) | out c(2); inp c(x); C(x)) R-Comm, R-New, R-Par repeat (inp a(x); inp x(y); out x(y+1)) | new (c); (out c(2+1) | inp c(x); C(x)) Types and Programming Languages Lecture 16 - Simon Gay
Example: Client/Server reductions in SEL repeat (inp a(x); inp x(y); out x(y+1)) | new (c); (out c(3) | inp c(x); C(x)) R-Comm, R-New, R-Par repeat (inp a(x); inp x(y); out x(y+1)) | new (c); C(3) (if cfn(C)) repeat (inp a(x); inp x(y); out x(y+1)) | C(3) Types and Programming Languages Lecture 16 - Simon Gay
Exercise One of the reductions in the example is labelled “reductions in SEL”. How should we formally include reductions in SEL into the definition of reductions in pi calculus? Types and Programming Languages Lecture 16 - Simon Gay
Facts about Pi Calculus Even without including SEL (so we just have channel names and pure communication), pi calculus is able to express all computation. Data can be represented by the use of channels. Recursive process definitions can be encoded by using replication (repeat). Theories of behavioural equivalence can be developed, supporting reasoning about correctness of systems. Numerous variations of pi calculus exist, including concepts such as locations and distribution, failure of processes or locations, time, … Types and Programming Languages Lecture 16 - Simon Gay