Prog Method
Prog Method
Programming methodology
Proving correctness of algorithms
Gergely Feldhoffer
Correctness of algorithms
Proof of correctness
Method to prove correctness
Proofs for basic algorithms
Some practical tools at the end of the semester concerning
software correctness (gdb, valgrind)
Course material
Relations
State space
Strings
Problem
Def.: Problem
We will call F ⊆ A × A relation a problem on state space A
For a starting state a ∈ A the possible valid solutions are the set
F (a).
A relation is deterministic if ∀a ∈ A : |F (a)| ≤ 1
If a problem does not assign anything to an a ∈ A that means
that there are no demands for this starting state. A possible
misunderstanding would be to think that there are no valid
answers for the state, but we will use this state in quite an
opposite way, so F (a) = ∅ and F (a) = A are equivalent in
some way.
Program
Def.: Program
S ⊆ A × A∗∗ is program if
1 DS = A
2 ∀a ∈ A : ∀α ∈ S(a) : α1 = a
3 ∀α ∈ RS : α = red(α)
Def: Variable
Let A = A1 × A2 × . . . × An be a state space. In this case for each
i ∈ [1 . . . n] : fi ⊆ A × Ai = {x ∈ A : fi (x) = projAi (x)}
Program function
Solution
Def.: Solution
An S ⊆ A × A∗∗ program solves the problem F if
1 DF ⊆ Dp(S)
2 ∀a ∈ DF : p(S)(a) ⊆ F (a)
Truth set
If R ⊆ A × L then
Truth set of R :
dRe = {a ∈ A : R(A) = {true}}
Weak truth set of R :
bRc = {a ∈ A : true ∈ R(A)}
Note that if R is function then dRe = bRc
Q → R ⇒ dQe ⊆ dRe
Precondition, postcondition
Weakest precondition
Parameter space
Specification
Example of specification
Example of specification
Example of specification
Elementary program
Elementary program
S ⊆ A × A∗∗ program is elementary if
∀a ∈ A : ∀α ∈ S(a) : (∃b ∈ A : α = red(habi)) ∨ α = haa . . .i
Skip Abort
SKIP
SKIP = {(a, α) ∈ A × A∗∗ : α = hai}
ABORT
ABORT = {(a, α) ∈ A × A∗∗ : α = haaa . . .i}
Assignment
So if we have an E as DE = A, then
SE = {(a, α) ∈ A × A∗∗ : α = red(habi) : b ∈ E (a)}
Assignment
wp(SKIP, R) = R
wp(ABORT , R) = False
Dp(SKIP) = A
∀a ∈ A : p(SKIP)(a) = a
Dp(ABORT ) = ∅
p(SE ) = E
Notations
Example: A = Zx × Zy
E = {(a, b) ∈ A × A : y (b) = 2 ∗ x(a) ∧ x(b) = x(a)}
We will be noted as y := 2x, this is a simple assignment
E = {(a, b) ∈ A × A : y (b) ∈ X }
as y :∈ X , this is a value choice
Program structures
The sequence means running two program after each other. The
branch means state dependent conditional program execution. The
loop is state dependent repeated execution of a program.
Sequence
Def: Sequence
Let S1 , S2 ⊆ A × A∗∗ be programs. We will call S ⊆ A × A∗∗ as
Sequence of S1 and S2 if ∀a ∈ A :
S(a) = {α ∈ A∞ : α ∈ S1 (a)}∪
∪{red(concat(α, β)) ∈ A∗∗ : α ∈ S1 (a) ∩ A∗ ∧ β ∈ S2 (τ (α))}
Branch
Def: Branch
Let π1 , . . . , πn : A → L be conditions, S1 , . . . , Sn programs on A.
We will call IF ⊆ A × A∗∗ program as Branch of
(π1 : S1 , . . . , πn : Sn ) if ∀a ∈ A:
n
[
IF (a) = wi (a) ∪ w0 (a)
i=1
Si (a) if πi (a),
where ∀i ∈ [1..n] : wi (a) =
∅ else
haaa . . .i if ∀i ∈ [1..n] : ¬πi (a),
w0 (a) =
∅ else
Branch
Loop
Def: Loop
Let π be a condition and S0 a program on A. We will call
DO ⊆ A × A∗∗ a Loop if
∀a ∈
/ dπe : DO(a) = {hai}
∀a ∈ dπe : DO(a) =
{α ∈ A∗∗ : ∃α1 , . . . , αn ∈ A∗∗ : (1)
α = red(concat(α1 , . . . , αn )) ∧ α1 ∈ S0 (a)∧ (2)
∧(∀i ∈ [1..n − 1] : αi ∈ A∗ ∧ (3)
∧αi+1 ∈ S0 (τ (αi )) ∧ π(τ (αi )))∧ (4)
∧((αn ∈ A∞ ) ∨ (αn ∈ A∗ ∧ ¬π(τ (αn ))))} (5)
∪ {a ∈ A∞ : ∀i ∈ N : (6)
∃αi ∈ A∗ : α = red(concat(α1 , α2 , . . .))∧ (7)
α1 ∈ S0 (a) ∧ (∀i ∈ N : αi ∈ A∗ ∧ (8)
αi+1 ∈ S0 (τ (αi )) ∧ π(τ (αi )))} (9)
Loop
Composition of relations R1 ⊆ A × B, R2 ⊆ B × C :
R2 ◦ R1 = {(a, c) ∈ A × C : ∃b ∈ B : (a, b) ∈ R1 ∧ (b, c) ∈ R2 }
Deduction rules
behold, adventurer
no slides beyond
todo
todo
todo
todo