4 Program Constructs
4 Program Constructs
Program constructs
Definition: Let A be the common base state space of the program S1 and S2 . Let σ ∈ A be
the current state.
∞
(S1 ; S2 )(σ) ::= {α | α ∈ S1 (σ) ∩ A } ∪
{α | α ∈ S1 (σ) and | α |< ∞ and α|α| = fail} ∪
∗
{α ⊗ β | α ∈ S1 (σ) ∩ A and β ∈ S2 (α|α| )}
(S1 ; S2 ) denotes the sequence of programs S1 and S2 and can be described by a structogram
in the following way:
S1
S2
Definition: Let A be the common base state space of the program S1 . . . Sn and the condi-
tions π1 . . . πn . Let σ ∈ A be the current state.
n
S
(if π1 → S1 . . . πn → Sn fi)(σ) ::= ω(σ) ∪ Si (σ)
i=1
σ∈Dπi ∧πi (σ)
(
{< σ, fail >} if ∃i ∈ [1..n] : σ ∈
/ Dπi ∨ ∀i ∈ [1..n] : σ ∈ Dπi ∧ ¬πi (σ)
where ω(σ) =
∅ otherwise
A π1 · · · A πn
A A
S1 ··· Sn
1
Theory of Programming Zsolt Borsi
Definition: Let A be the common base state space of the program S0 and the condition π.
Let σ ∈ A be the current state.
(S0 ; while π do S0 od)(σ) if σ ∈ Dπ ∧ π(σ)
(while π do S0 od)(σ) ::= {< σ >} if σ ∈ Dπ ∧ ¬π(σ)
{< σ, fail >} if σ ∈
/ Dπ
while π do S0 od denotes the loop constructed by the loopbody S0 and the π loopcondition,
and can be described by a structogram in the following way:
π
S0
• (1, 3) and
• (4, 6)
S
i := 0
i 6= n
A
A
i=1 A
A
i62
i := 3 · i i := i + 1
2.2 Answer
Our S program is a sequence constructed from an assignment i := 0 and a loop. The loop
will stop if its loopcondition becomes f alse, that is when i and n are equal.
The semantics of sequence and loop is well known, however we define the meaning of se-
lection in a different way than it is common in programming languages like JAVA or C++.
In our mathematical model, non-determinism is allowed. Particularly, if there are more
branches of a selection where the conditions are all true for a given state in the statespace,
then we do not select one branch from them according a special rule, instead, all the execu-
tions are allowed that can be generated starting from the given state and taking any of the
2
Theory of Programming Zsolt Borsi
For example, when i equals to 1 then both the conditions i = 1 and i 6 2 are true, we do
not prefer any of the corresponding programs, both i := i · 3 and i := i + 1 can be executed
starting from the state where the value of variable i is 1.
Another difference in our model compared to programming languages, that if none of the
conditions of a selection is true for a given state, then the selection aborts at the given state.
For example, when i equals to 3 then neither i = 1 nor i 6 2 is true, the selection aborts
starting from a state where the value of variable i is 3.
A program is a relation that assigns sequences to every state in the statespace. All the se-
quences assigned to any state start with the same state they are assigned to. Our S program
maps sequences to every state of the statespace such that the elements of the sequences are
states of A (i.e. the elements of the sequences are pairs).
The assignment i := 0 takes as from a state (i, n) to a state where i is set to 0 and n is un-
changed, that is (0, n). There are two sequences assigned to the state (1, 3):
< (1, 3), (0, 3), (1, 3), (3, 3) > and < (1, 3), (0, 3), (1, 3), (2, 3), (3, 3) >
Notice that the reason why two sequences are assigned to (1, 3) is, that when we are in the
state (1, 3), then both the conditions i = 1 and i 6 2 are true, there are two possible ways
how we can continue the execution of the program. In the state (3, 3) the loop stops, as its
loopcondition becomes f alse.
< (4, 6), (0, 6), (1, 6), (3, 6), f ail > and < (4, 6), (0, 6), (1, 6), (2, 6), (3, 6), f ail >
Notice that the reason why these sequences terminates in the special f ail state is, that in the
state (3, 6) neither the conditions i = 1 and nor i 6 2 is true, the selection (the body of the
loop) aborts.
More precisely, the following two sequences are assigned to the state {i : 1, n : 3} by the
program:
< {i : 1, n : 3}, {i : 0, n : 3}, {i : 1, n : 3}, {i : 3, n : 3} > and < {i : 1, n : 3}, {i : 0, n : 3}, {i :
1, n : 3}, {i : 2, n : 3}, {i : 3, n : 3} >
The program assigns the following two sequences to the state {i : 4, n : 6}:
< {i : 4, n : 6}, {i : 0, n : 6}, {i : 1, n : 6}, {i : 3, n : 6}, f ail > and < {i : 4, n : 6}, {i : 0, n :
6}, {i : 1, n : 6}, {i : 2, n : 6}, {i : 3, n : 6}, f ail >