Semantics of ; || []
For now, let us focus on executional semantics. We will revisit these after Hoare's Axioms and Dijkstra's Weakest Preconditions.
1 Semantics of Semicolon ;
- Consider the execution of S ≡ S1 ; S2. Think of semi-colon as an operator, much like + * are arithmetic operators.
- What is the meaning of S given the meanings of S1 and S2?
- Let Tb(Si) stand for the state at the beginning of Si, and Te(Si) at the end. Let tb(Si) stand for the timestamp of when Si began, and te(Si) when it ended (if it did).
- The meaning of S1 tells us about the relation between Te(S1) and Tb(S1).
- Clearly, Tb(S2) = Te(S1) and tb(S2) >= te(S1)
- Must tb(S2) == te(S1)?
- Is it acceptable if tb(S2) == te(S1) + 10 hours?
2 Semantics of Parallel Bar ||
- Let S ≡ S1 || S2. Often written as
co S1 || s2 oc
- Is {S1 || S2} the same as {S2 || S1}?
- Must it be tb(S2) == tb(S1)?
- Even so, we cannot expect that te(S2) == te(S1)
- Is it acceptable/ necessary: tb(S2) >= te(S1)?
- Is it acceptable: tb(S2) < tb(S1)?
- te(S) == max(te(S1), te(S2))?
- tb(S) == min(tb(S1), tb(S2))?
- Is {S1 ; S2} a "pathological" version of {S1 || S2}?
3 Semantics of Fat Bar []
- Let S ≡ S1 [] S2. Non-determinism
- This is defined as execute one of S1 or S2. Never both.
- Choice is deliberately unspecified.
- Is {S1 [] S2} the same as {S2 [] S1}?
- Non-determinism does not mean random.
4 Guarded Commands of Dijkstra
4.1 IF statement
if G0 ==> S0
[] G1 ==> S1
…
[] Gn ==> Sn
fi
The Gi are boolean expressions, called the guards. Imagine: The Gi are evaluated at the top of the if-stmt. An Si is enbaled if its Gi evalueted to true. If multiple Gi were true, all the corresponding Si are enabled.
Case 1: One or more Si are enabled. From among the enabled Si, choose one non-determininistically, and execute it.
Case 2: If no Si are enabled the if-stmt aborts.
4.2 DO statement
do G0 ==> S0
[] G1 ==> S1
…
[] Gn ==> Sn
od
- This gives us a loop.
- Corresp if-stmt: Replace the
do
withif
andfi
forod
. - The do-stmt terminates if and when all the guards are false.
- We execute the corresp if-stmt first. If any of the guards were true, then we execute the do-stmt again.
5 Exercise
Consider {y := 0; z := 0; co x := y + z || {y := 1; z := 2} co} How can x be 0, 1, 2 or 3 on page 60 of Andrew's Concurrent Programming?