Models of Distributed Systems

Models of Distributed Systems
Lecture 8
MSD (2015)
Lecture 9
1 / 41
Overview
1
CPN-ML
2
Coloured Petri Nets Definition
MSD (2015)
Lecture 9
2 / 41
CPN-ML
Overview
1
CPN-ML
2
Coloured Petri Nets Definition
MSD (2015)
Lecture 9
3 / 41
CPN-ML
Simple color sets in CPN ML:Int color sets
declaration syntax:
colset name = int [with int exp1..int exp2];
declaration example:
colset INT = int;
colset SmallInt = int with 1..10;
operations:
∼i
i1 + i2 , i1 − i2 , i1 ∗ i2
i1 div i2 , i1 mod i2
Int.min(i1 , i2 ), Int.max(i1 , i2 )
If the int type declaration is restricted by the clause ”with” and it contains less than 100 elements, then the type
is considered a small type, otherwise it is considered a large type.
MSD (2015)
Lecture 9
4 / 41
CPN-ML
Simple color sets in CPN ML: Boolean color sets
declaration syntax:
colset name = bool [with (new false, new true)];
values: true, false
declaration example:
colset B = bool;
colset Answer = bool with (no, yes);
operations:
not b
b1 andalso b2
b1 orelse b2
MSD (2015)
Lecture 9
5 / 41
CPN-ML
Simple color sets in CPN ML
Enumeration color sets
declaration syntax:
colset name = with id0|id1| . . . |idn;
declaration example:
colset Philosophers = with ph1|ph2|ph3;
operations: - general color set functions can be used
MSD (2015)
Lecture 9
6 / 41
CPN-ML
Simple color sets in CPN ML
Index color sets
declaration syntax:
colset name = index id with int exp1..int exp2;
The values of the type are id(i) or id i (int exp1 ≤ i ≤ int exp2)
declaration example:
colset Philosophers = index ph with 1..3;
(values: ph(1), ph(2), ph(3))
operations: - general color set functions can be used
MSD (2015)
Lecture 9
7 / 41
CPN-ML
Product color sets
declaration syntax:
colset name = product name1 ∗ name2 ∗ ... ∗ namen ;
(each namei should represent a previously defined color set)
values:
the values of this type are n-uples (v1 , v2 , . . . , vn ) where each vi is a value of the
type (color set) namei .
declaration example:
colset Name = string;
colset Income = int with 500..3000;
colset Job type = with tester |analyst|programmer ;
colset Employee = product Name ∗ Income ∗ Job type;
operations:#i x extracts the i-th element of the tuple x:
#2 (”John”, 2000, tester ) is 2000
MSD (2015)
Lecture 9
8 / 41
CPN-ML
Record color sets
declaration syntax:
colset name = record id1 : name1 ∗ id2 : name2 ∗ . . . ∗ idn : namen ;
values: {id1 = v1 , id2 = v 2, . . . , idn = vn } (each namei is a declared color set and
each vi is a value from the type namei )
declaration example:
colset Employee = record name : Name ∗ inc : Income ∗ job : Job type;
operations: idk rec - extracts the idk element from the record rec:
inc {name = ”John”, inc = 2000, job = tester } is 2000
MSD (2015)
Lecture 9
9 / 41
CPN-ML
Union color sets
A union color set is a disjoint union of previously declared color sets.
declaration syntax:
colset name = union id1 [: name1 ] + id2 [: name2 ] + . . . + idn [: namen ];
(each namei is a previously declared color set)
values: idk (v ) or idk v , where v is a value of type namek ; idk if namek is not
declared
declaration example:
colset TeaType = with black tea|white tea|green tea;
colset CoffeType = with arabica|robusta;
colset Products = union tea : TeaType + coffe : CoffeType + water ;
Example of values: tea(black tea), coffe(arabica), water
MSD (2015)
Lecture 9
10 / 41
CPN-ML
List color sets
declaration syntax:
colset name = list name type[with int exp1..int exp2];
a list of values of the previously declared color set with the name name type
values: [v1 , v2 , . . . , vn ] where vi has type name type for i = 1..n.
[] denotes the empty list;
declaration example:
colset LInt = list int with 1..100;
MSD (2015)
Lecture 9
11 / 41
CPN-ML
Some list functions from Standard ML:
nil
e :: l - prepend element e as head of list l
hd l - head, the first element of the list l
tl l - tail, list with exception of first element
length l - length of list l
map f l - use function f on each element in list l and returns a list with all the results
List.nth(l, n) - nth element in list l, where 0 ≤ n < length l
List.take(l, n) returns first n elements of list l
mem l x returns true if element x is in the list l
rmall x l removes all appearances (if any) of element x from list l
ins l x - inserts element x at the end of list l
ins new l x - if x is not a member of list l, then x is inserted at the end of l, otherwise l is returned
MSD (2015)
Lecture 9
12 / 41
CPN-ML
Subset color sets
Subsets are a selection of values in a previously declared color set.
declaration syntax:
1
colset name = subset name type by subset-function;
name type is a previously declared color set, subset − function returns boolean.
2
colset name = subset name type with subset-list;
subset − list is a list of elements from name type.
declaration example:
1
colset EvenNumbers = subset int by even;
even is a previously defined function which returns true if a int number is even and false
otherwise
2
colset Philosophers= index Ph with 1..100;
colset First3Philosophers = subset Philosophers with [Ph(1), Ph(2), Ph(3)];
MSD (2015)
Lecture 9
13 / 41
CPN-ML
Variables and expressions
Value bindings:
val x = expr;
A variable is always bound to the same value.
If-expressions:
if expr bool then expr1 else expr2
expr bool has type bool, expr1 and expr2 must have the same type t; the if expression
has type t.
MSD (2015)
Lecture 9
14 / 41
CPN-ML
Patterns and pattern mathcing
patterns are used to bind variables to (compound) values by pattern-matching.
patterns can be used to decompose compound values.
patterns can be seens as special expressions, ”templates” built up with variables
and constants.
pattern = compound value;
Pattern matching:
deconstructs compound values and binds their constituents to the corresponding
variables.
Matching a (compound) value v against a pattern pat will either succeed or fail.
If it succeeds, the match rule binds any value identifier of pat to the corresponding
value components of v .
Like expressions, patterns have types.
MSD (2015)
Lecture 9
15 / 41
CPN-ML
The pattern language
A variable can appear only once in a pattern. Patterns can be made from parts that
contain other patterns:
Wildcard: The pattern (underscore) has an arbitrary type t and matches all
values of that type. The actual runtime value is discarded, i.e., it is not bound to
any variable.
Variable: Any variable name x (except those currently bound to datatype constructors) can be used as a pattern. Such a pattern has an arbitrary type t (which
becomes the type of x within its scope) and matches any value of that type.
Tuple: If p1 , . . . , pn are patterns of types t1 , . . . , tn then (p1 , . . . , pn ) is a tuple
pattern of type t1 ∗ . . . ∗ tn . It matches a value (v1 , . . . , vn ) provided that for all i we
have that pi matches vi . In function definitions, tuple patterns are often used to
give the illusion of having multiple arguments.
MSD (2015)
Lecture 9
16 / 41
CPN-ML
The pattern language
Record Similarly, under the same assumption as above, the pattern
{l1 = p1 , . . . , ln = pn } is a record pattern of type {l1 : t1 , . . . , ln : tn } and matches
record values of that type.
List If p1 , . . . , pn are patterns of type t, then [p1 , . . . , pn ] is a pattern of type t list. It
matches list values [v1 , . . . , vn] that are precisely n elements long provided that
each element vi matches its corresponding sub-pattern pi .
Integer Any integer literal i is a pattern of type int that matches precisely the value
i.
String Any string literal s is a pattern of type string that matches precisely the
value s.
MSD (2015)
Lecture 9
17 / 41
CPN-ML
Patterns
colset SI = product STRING ∗ INT ;
val x = (”ana”, 2);
val (y , z) = (”bbb”, 3);
val (y 1, z1) = x;
val (y 1, z1) = 2;
val z2 :: l = [1, 2, 3, 4]
(y , z) is a pattern with two variable, y and z. By pattern-matching, y will be bound
to ”bbb” and z to 3.
(y 1, z1) is a pattern with two variable, y 1 and z1. By pattern-matching, y 1 will be
bound to ”aaa” and z1 to 2.
val(y 1, z1) = 2: error
z2 is 1 and l the list [2, 3, 4];
MSD (2015)
Lecture 9
18 / 41
CPN-ML
Patterns
val x = ((”ana”, 1), 2);
Type of x: (string ∗ int) ∗ int
val ((n, i1), 2) = x;
n = ”ana”, i1 = 1
val ((n, i1), 1) = x;
error!
MSD (2015)
Lecture 9
19 / 41
CPN-ML
Functions in CPN ML
function are values belonging to function types of the form DType → RType, where
type DType is the domain type (the type of arguments) of the function, and RType
is its range type (the type of its results).
function expressions:
fn arg pattern => expression;
or: fn arg pattern : Type => expression;
arg is a pattern denoting the argument of the function
Functions take a single argument/parameter; multiple arguments/parameters can
be simulatated by the use of tuples or records;
MSD (2015)
Lecture 9
20 / 41
CPN-ML
Functions in CPN ML
The function expression fn x => x + 1 can be used in function bindings:
val inc = fn x => x + 1;
inc is binded to a function value of type int → int
function invocation:
inc 6;
or:
(fn x => x + 1)(6);
after evaluating the expressions, the result is 7;
val add = fn (x, y ) => x + y ;
add is binded to a function value of type int ∗ int → int
MSD (2015)
Lecture 9
21 / 41
CPN-ML
Functions in CPN ML
Function applications:
(fn arg pattern => expression)(arg expression):
if val Id = fn arg pattern => expression, then function application:
Id(arg expression)
val Id = fn arg pattern => expression;
is equilvalent to:
fun Id arg pattern = expression;
MSD (2015)
Lecture 9
22 / 41
CPN-ML
Functions in CPN ML
val add = fn (x, y ) => x + y ;
is equivalent with:
fun add(x, y ) = x + y ;
or:
fun add(x : int, y : int) = x + y ;
or:
fun add(x : int, y : int) : int = x + y ;
The name (identifier) add is binded to a function value with the type int ∗ int → int
MSD (2015)
Lecture 9
23 / 41
CPN-ML
Function application
Let Id arg pattern = expr ;
and
Id arg expression
arg expression is evaluated and a value v is obtained (arg expression should
have the same type as expr )
value v is matched against the pattern describing the parameter of the function
any variable in the pattern will bind to its corresponding value
the body of the function (i.e. expr ) is evaluated in the resulting environment
MSD (2015)
Lecture 9
24 / 41
CPN-ML
Local variables declaration
The let construct permits the declaration of locally-scoped variables within a function definition or expression.
Synthax
let
val pat1 = exp1
val pat2 = exp2
...
val patn = expn
in
exp
end;
pat1 , . . . , patn are patterns; All variable occuring in these patterns can be used in exp (they bind to
corresponding values after pattern matching with exp1 , . . . , expn .
the above let construct is an expression having the same type as exp.
MSD (2015)
Lecture 9
25 / 41
CPN-ML
Local variables declaration
val y = 5;
val x = let val (z, y ) = (2, 4) val x = 2 in x + 2 ∗ y end + y ;
val z = 100;
val y = 7;
val x = if y > 5 then y + let val z = 2 in z ∗ 2 + z end else z;
MSD (2015)
Lecture 9
26 / 41
CPN-ML
Functions in CPN ML
The use of let in function expressions:
fun length(l) = if l=[] then 0 else
let val tail = tl l
in
if tail = [] then 1 else 1+length(tail)
end;
MSD (2015)
Lecture 9
27 / 41
CPN-ML
Functions in CPN ML
Clausal definitions of functions:
fun id pattern1 = expression1
| id pattern2 = expression2
...
| id patternn = expressionn ;
pattern 1, pattern 2, . . . , pattern n are patterns for the functions’s arguments
expression1 , expression2 , . . . , expressionn are expressions of the same type.
MSD (2015)
Lecture 9
28 / 41
CPN-ML
Functions in CPN ML
fun fact(0)=1
| fact(n)=n*fact(n-1) ;
MSD (2015)
Lecture 9
29 / 41
CPN-ML
Functions in CPN ML
fun fact(0)=1
| fact(n)=n*fact(n-1) ;
fun length([])=0
| length([a])=1
| length( :: l)=1+length(l);
Type of length is list ′ a → int
length([”a”, ”b”, ”c”])
length([1, 3, 3])
length([])
MSD (2015)
Lecture 9
29 / 41
CPN-ML
Functions in CPN ML - Polymorphism
fun make multiset(x, y , i) = i ′ x + +i ′ y ;
type of make multiset is ′ a ∗′ a ∗ int →′ a ms
make multiset(”a”, ”b”, 2) ⇒ 2‘”a” + +2′ ”b” (type string ms) (in standard ML
multisets are represented internally as lists [”a”, ”a”, ”b”, ”b”];
make multiset(4, 3, 2) ⇒ 2‘4 + +2‘3 (type int ms)
make multiset(”a”, 3, 2) ⇒ error: ”operator and operand don’t agree”
operator domain: string ∗ string ∗ int
operand: string ∗ int ∗ int
MSD (2015)
Lecture 9
30 / 41
Coloured Petri Nets Definition
Overview
1
CPN-ML
2
Coloured Petri Nets Definition
MSD (2015)
Lecture 9
31 / 41
Coloured Petri Nets Definition
Dining Philosophers
5 philosophers and 5 chopsticks:
MSD (2015)
Lecture 9
32 / 41
Coloured Petri Nets Definition
Dining Philosophers
colset Philo = index P with 1..5;
colset Chopsticks = index c with 1..5;
fun Chopstick (x : Philo) = if x = P(1) then 1‘c(1) + +1‘c(5) else if x =
P(2) then 1‘c(1) + +1‘c(2)
else if x = P(3) then 1‘c(2) + +1‘c(3) else if x = P(4) then 1‘c(3) + +1‘c(4)
else 1‘c(4) + +1‘c(5);
MSD (2015)
Lecture 9
33 / 41
Coloured Petri Nets Definition
Case expression
Synthax
case expr of
pattern1 ⇒ expression1
| pattern2 ⇒ expression2
...
| patternn ⇒ expressionn ;
expression1 , . . . , expressionn have all the same type;
MSD (2015)
Lecture 9
34 / 41
Coloured Petri Nets Definition
Dining Philosophers
fun Chopstick (P(i)) =
case i of 1 => 1‘c(1) + +1‘c(5)
|2 => 1‘c(1) + +1‘c(2)
|3 => 1‘c(2) + +1‘c(3)
|4 => 1‘c(3) + +1‘c(4)
| => 1‘c(4) + +1‘c(5);
MSD (2015)
Lecture 9
35 / 41
Coloured Petri Nets Definition
Example
fun Chopstick(P(i)) = 1‘c(i) + +1‘c(if i = 1 then 5 else i − 1);
MSD (2015)
Lecture 9
36 / 41
Coloured Petri Nets Definition
Behaviour of Coloured Petri Nets - Steps
Definition 1
A step is any finite multiset over BE.
The set of all the steps is denoted by Y:
Y = {Y ∈ BEMS | 0 < |Y | < ∞}
MSD (2015)
Lecture 9
37 / 41
Coloured Petri Nets Definition
Steps
b1 , b2 ∈ B(t1 ): b1 =< x = a, y = b >, b2 =< x = a, y = c >
b3 , b4 ∈ B(t2 ): b3 =< x = a, z = r 2 >, b4 =< x = a, z = r 2 >
Steps:
Y1 = 1′ (t1 , b1 ) + +1′ (t2 , b3 )
Y2 = 2′ (t1 , b1 )
Y3 = 1′ (t1 , b1 ) + +2′ (t1 , b2 ) + +2′ (t2 , b4 )
MSD (2015)
Lecture 9
38 / 41
Coloured Petri Nets Definition
The firing rule for steps
Definition 2
Let CPN = (P, T , F , Σ, C, G, E, I) be a coloured Petri net, Y ∈ Y a step and M a
marking of CPN.
a step Y is enabled in marking M (M[Y iCPN ) iff:
P
∀p ∈ P :
(t,b)∈Y E(p, t) < b > ≤ M(p);
if M[Y iCPN , then by firing the step Y a new marking M ′ is obtained (M[Y iCPN M ′ ),
such that:
∀ p ∈ P : M ′ (p) = M(p) ⊖
P
⊕ (t,b)∈Y E(t, p) < b > .
MSD (2015)
P
(t,b)∈Y
!
E(p, t) < b >
Lecture 9
39 / 41
Coloured Petri Nets Definition
Example
Y = (t1 , b1 ) + +(t2 , b2 ), b1 =< x = a, y = c >, b2 =< u = a, z = 3 >
M0 [Y i
MSD (2015)
Lecture 9
40 / 41
Coloured Petri Nets Definition
Example
M0 [Y iM1
MSD (2015)
Lecture 9
40 / 41
Coloured Petri Nets Definition
Concurrency
Definition 3
Let Y be a step enabled in marking M.
If (t1 , b1 ), (t2 , b2 ) ∈ Y and (t1 , b1 ) 6= (t2 , b2 ), then the binding elements (t1 , b1 ) and
(t2 , b2 ) are concurrently enabled in M, and also the transitions t1 and t2 are
concurrently enabled in M.
If Y (t, b) ≥ 2, then the binding element (t, b) is concurrently enabled with itself in
M.
If ∃(t, b1 ), (t, b2 ) ∈ Y , then transition t is concurrently enabled with itself in M.
MSD (2015)
Lecture 9
41 / 41