0% found this document useful (0 votes)
9 views23 pages

Acce Notes 0 2

This document discusses transformational imperative programming and programming constructs. It introduces states and behaviors, provides examples specifications, and outlines the syntax and semantics of common programming constructs like skip, assignment, sequential composition, and iteration.

Uploaded by

yujial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views23 pages

Acce Notes 0 2

This document discusses transformational imperative programming and programming constructs. It introduces states and behaviors, provides examples specifications, and outlines the syntax and semantics of common programming constructs like skip, assignment, sequential composition, and iteration.

Uploaded by

yujial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Transformational Imperative Programming Programming Constructs

Table of Contents

1 Transformational Imperative Programming


States and Behaviours
Some Example Specifications.
2 Programming Constructs
Syntax (Form)
Comparison to C and Java
Semantics (Meaning)
Skip
Assignment (‘becomes’)
Parallel assignment
Sequential composition
Alternation
Iteration

(c) Theodore Norvell, adapted by Reza Shahidi


Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

In this part of the course we will:


Look at requirements specification for simple programming
problems.
Look at programs as systems
Look at ways of putting systems together (composing
systems)
Use the above to develop techniques for designing
programs that meet their specification.
We look at transformational programming rather than interactive
programming. That, is we will assume no output is required by
the environment until after the whole computation is complete.
Imperative programming means programming with commands.

(c) Theodore Norvell, adapted by Reza Shahidi


Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

States and Behaviours

States of the computer are modeled as mappings from


(unprimed) variable names to values.
For example, if we have variable names x and y of type
int, then example states include

Σ = {“x ” 7→ Z, “y ” 7→ Z} :
i = {“x ” 7→ 10, “y ” 7→ 5} : Σ
o = {“x ” 7→ 6, “y ” 7→ 5} : Σ

For this section of the course we will ignore internal states,


so:
A behaviour consists of two states, an initial state and a
final state. E.g.
{ }
i † o = “x ” 7→ 10, “y ” 7→ 5, “x 0 ” 7→ 6, “y 0 ” 7→ 5

is a behaviour.
(c) Theodore Norvell, adapted by Reza Shahidi
Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

States and Behaviours

Both input and output states belong to the same signature Σ; so


behaviours belong to Σ † Σ.
For the rest of the notes on programming the signature for
specifications will be Σ † Σ for some Σ.

(c) Theodore Norvell, adapted by Reza Shahidi


Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Some Example Specifications.

Consider the problem of computing the minimum of two


natural numbers
Σ = {“x ” 7→ N, “y ” 7→ N}
⟨ 0 ⟩
x = min(x , y )

Consider the problem of computing the greatest common


denominator of two natural numbers
Σ = {“x ” 7→ N, “y ” 7→ N}
⟨ 0 ⟩
x = gcd(x , y )

Consider the problem of searching for an element x in an


array a of N integers
{ ( ) }
tot
Σ = “b ” 7→ B, “a ” 7→ {0, ..N } → Z , “x ” 7→ Z
(c) Theodore Norvell, adapted by Reza Shahidi
Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Some Example Specifications.

and {0, ..N } = {i ∈ N | 0 ≤ i < N }.


(We model the value of array a with a total function from
{0, ..N } to Z.)
A specification is
⟨ ⟩
b 0 = (∃i ∈ {0, ..N } · a (i ) = x )

A better specification would be


⟨ 0 ⟩
b = (∃i ∈ {0, ..N } · a (i ) = x ) ∧ x 0 = x ∧ a 0 = a

if we require that program variables x and a not change.

(c) Theodore Norvell, adapted by Reza Shahidi


Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Some Example Specifications.

Here is another array search problem. This time the goal is


to report the location of an item that may be assumed to be
in the array
{ }
tot
Σ = “k ” 7→ N, “a ” 7→ {0, ..N } → Z, “x ” 7→ Z

Note that a specification


⟨ ⟩
a (k 0 ) = x
is unimplementable. (Remember the commandment.)
We can state assumptions about the input as the
antecedent of an implication. A reasonable
(implementable) specification would be
⟨ ⟩
(∃i ∈ {0, ..N } · a (i ) = x ) ⇒ a (k 0 ) = x

(c) Theodore Norvell, adapted by Reza Shahidi


Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

This illustrates an important pattern. Specifications are


often of the form
hP ⇒ Q i
where P represents an assumption about the input. Then
P is called the precondition and Q is called the
postcondition. For inputs, for which P is false, the
expression simplifies as follows
P⇒Q
=
false ⇒ Q
=
true
For such inputs, the specification imposes no restrictions
on the output.
(c) Theodore Norvell, adapted by Reza Shahidi
Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Table of Contents

1 Transformational Imperative Programming


States and Behaviours
Some Example Specifications.
2 Programming Constructs
Syntax (Form)
Comparison to C and Java
Semantics (Meaning)
Skip
Assignment (‘becomes’)
Parallel assignment
Sequential composition
Alternation
Iteration

(c) Theodore Norvell, adapted by Reza Shahidi


Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Syntax (Form)

We will consider commands of the following forms

skipΣ†Σ Skip
V :=Σ†Σ E Assignment
V0 , V1 , ..., Vk :=Σ†Σ E0 , E1 , ..., Ek Parallel asignment
fΣ†Σ ; gΣ†Σ Sequential composition
if B then fΣ†Σ else gΣ†Σ Alternation
while B do fΣ†Σ Iteration
(fΣ†Σ ) Grouping

where
V and each Vi is a variable name mapped in Σ
E is an expression of type Σ(V)
each Ei is an expression of type Σ(Vi )
B is an expression of type B.
fΣ†Σ and gΣ†Σ are specifications
(c) Theodore Norvell, adapted by Reza Shahidi
Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Syntax (Form)

Note that expressions E and B should use only unprimed


variables
Precedence: “()”, then “if-then-else” and “while-do”, then “;”
Normally the signature is clear from context, so we just write
skip or :=.

(c) Theodore Norvell, adapted by Reza Shahidi


Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Comparison to C and Java

The syntax above is based on the Algol and Pascal languages.


The corresponding syntax in C/C++ and Java, which will not be
used in this course, is

The Course C/C++/Java


skip ; or {}
V := E V=E;
f; g fg
if B then f else g if( B ) f else g
while B do f while( B ) f
(f ) {f }

(c) Theodore Norvell, adapted by Reza Shahidi


Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Semantics (Meaning)

Each command describes a set of acceptable behaviours.


Thus each command is a specification.
Furthermore, we will define composition operators so that they
operate on arbitrary specifications, not just on commands.
skip is the do-nothing transformation. Its output must be the
same as its input
skip(i † o ) = (i = o )
Suppose that Σ maps names “x ”, “y ”, and “z ”. Then
⟨ ⟩
skip = x 0 = x ∧ y 0 = y ∧ z 0 = z

(c) Theodore Norvell, adapted by Reza Shahidi


Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Semantics (Meaning)

Suppose that Σ maps names “x ”, “y ”, and “z ”. Then


x := E
is defined to be
⟨ ⟩
x 0 = E ∧ y 0 = y ∧ z0 = z
Example
Consider Σ = {“x ” 7→ N, “y ” 7→ N} then
⟨ ⟩
(x := x − y ) = x 0 = x − y ∧ y 0 = y

Suppose that Σ maps names “x ”, “y ”, and “z ” Then


(x , y := E, F ) is defined to be
⟨ 0 ⟩
x = E ∧ y 0 = F ∧ z0 = z
Example
(c) Theodore Norvell, adapted by Reza Shahidi
Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Semantics (Meaning)

Consider Σ = {“x ” 7→ N, “y ” 7→ N} then x , y := y , x is


⟨ ⟩
x0 = y ∧ y0 = x

(c) Theodore Norvell, adapted by Reza Shahidi


Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Semantics (Meaning)

The sequential composition f ; g performs the transformation f


and then the transformation g on the result.
We call the output of f (which is the input to g ) m.
If the input is i and the output is o , then we want
f (i † m) and g (m † o )
Since we are ignoring intermediate states, we don’t care what
the value of m is, only that it exists.
Thus we define
(f ; g )(i † o ) = (∃m : Σ · f (i † m) ∧ g (m † o ))
Suppose that Σ maps names “x ”, “y ”, and “z ” Then
hAi ; hBi
is
⟨ ⟩
∃ẋ , ẏ , ż · A[x 0 , y 0 , z 0 : ẋ , ẏ , ż ] ∧ B[x , y , z : ẋ , ẏ , ż ]
(c) Theodore Norvell, adapted by Reza Shahidi
Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Semantics (Meaning)

Example
Suppose that Σ maps names “x ”, “y ”, and “z ”.

x := x − 1; y := 2 × x
= “definition of assignment”
⟨ 0 ⟩
x = x − 1 ∧ y 0 = y ∧ z0 = z ;
⟨ 0 ⟩
x = x ∧ y 0 = 2 × x ∧ z0 = z
= “definition of sequential composition”
 
⟨  (x 0 = x − 1 ∧ y 0 = y ∧ z 0 = z ) ⟩
 
 [x 0 , y 0 , z 0 : ẋ , ẏ , ż ] 
∃ẋ , ẏ , ż ·  

 ∧ (x = x ∧ y = 2 × x ∧ z 0 = z )
0 0


[x , y , z : ẋ , ẏ , ż ]
= “making the substitutions”
⟨ ( )⟩
(ẋ = x − 1 ∧ ẏ = y ∧ ż = z )
∃ẋ , ẏ , ż ·
∧ (x 0 = ẋ ∧ y 0 = 2 × ẋ ∧ z 0 = ż )
(c) Theodore Norvell, adapted by Reza Shahidi
Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Semantics (Meaning)

= “one-point law”
⟨ 0 ⟩
x = x − 1 ∧ y 0 = 2 × (x − 1) ∧ z 0 = z

(c) Theodore Norvell, adapted by Reza Shahidi


Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Semantics (Meaning)

First we define how the propositional operators to act on


boolean functions f and g
(¬f ) (b ) = ¬f (b )
(f ∧ g ) (b ) = f (b ) ∧ g (b )
(f ∨ g ) (b ) = f (b ) ∨ g (b )
(f ⇒ g ) (b ) = f (b ) ⇒ g (b )
And therefore we have the following distribution laws.
¬ hAi = h¬Ai
hAi ∧ hBi = hA ∧ Bi
hAi ∨ hBi = hA ∨ Bi
hAi ⇒ hBi = hA ⇒ Bi
Now we can define
if A then f else g
(c) Theodore Norvell, adapted by Reza Shahidi
Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Semantics (Meaning)

to be
(hAi ∧ f ) ∨ (¬ hAi ∧ g )
Exercise. Show that

(if A then f else g ) = (hAi ⇒ f ) ∧ (¬ hAi ⇒ g )

(c) Theodore Norvell, adapted by Reza Shahidi


Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Semantics (Meaning)

Example

Suppose that Σ maps names “x ”, “y ”, and “z ”

if y < x then x := y else y := x


= “Definition assignment”
if y < x
then hx 0 = y 0 = y ∧ z 0 = z i
else hx 0 = y 0 = x ∧ z 0 = z i
= “Definition of alternation”
(hy < x i ∧ hx 0 = y 0 = y ∧ z 0 = z i)
∨ (hy ≥ x i ∧ hx 0 = y 0 = x ∧ z 0 = z i)
= “Distributing angle brackets”
⟨ ⟩
(y < x ∧ x 0 = y 0 = y ∧ z 0 = z )
∨ (y ≥ x ∧ x 0 = y 0 = x ∧ z 0 = z )
(c) Theodore Norvell, adapted by Reza Shahidi
Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Semantics (Meaning)

= “Distributivity”
⟨( ) ⟩
(y < x ∧ x 0 = y 0 = y ) 0
∧z =z
∨ (y ≥ x ∧ x 0 = y 0 = x )
= “Definition of min ”
⟨ 0 ⟩
x = y 0 = min(x , y ) ∧ z 0 = z

(c) Theodore Norvell, adapted by Reza Shahidi


Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming
Transformational Imperative Programming Programming Constructs

Semantics (Meaning)

Iteration (while-loop) is a bit more complicated. We will return


to it later, once the non-iterative constructs are better
understood.

(c) Theodore Norvell, adapted by Reza Shahidi


Advanced Computing Concepts for Engineering, 2024. Slide Set 0-2 Imperative Programming

You might also like