Beruflich Dokumente
Kultur Dokumente
Hoare-Logik
Software
Engineering
Hoare-Kalkül
• Semantik
• Nicht einfach
• Fehleranfällig
2
Lernziele
3
fi
fi
{P} S {Q}
Precondition Postcondition
• S ist total korrekt wenn es partiell korrekt ist und immer terminiert
4
Beispiele
{x 0}
y = x*x;
valid
{y > 0}
{x > 0}
x = y;
invalid
{x > 0}
{x ≥ 0}
y = 2*x;
invalid
{y > x}
{y ≥ x} valid
5
≠
Elementare Mathematik
• Angenommen wir wollen • In der Regel benötigen wir
folgendes für Integer beweisen:
weitere Theorien, z.B. Arithmetik
mit Integern, um die Korrektheit
{x > 0} A {y ≥ 0} von Programmen zu beweisen
6
Variablen
7
Motivation
x y
{n ≥ 0}
4 1
x = n;
• Eingabe: n (Integer)
3 4
y = 1;
while (x 0) {
• Was wird hier berechnet?
2 12
1 24
y = y * x;
• y = n!
x = x - 1;
0 24
8
≠
Motivation
x y
{n ≥ 0}
4 1
x = n;
y = 1;
• Symbolische Berechnung, welche Werte die 3 4
Variablen über den Programmverlauf annehmen 2 12
while (x 0) {
können
1 24
y = y * x;
x = x - 1;
• Zusicherungen über Werte von Variablen im 0 24
Programm aufschreiben und beweisen
}
{y = n!}
9
≠
Zusicherungen
Vorbedingung
{n ≥ 0} (precondition)
x = n;
Eine Zusicherung ist eine
{x ≥ 0 ∧ n ≥ 0 ∧ x = n}
Zwischenliegende
logische Formel an einer Stelle
y = 1;
Zusicherung
{y = 1 ∧ x ≥ 0 ∧ n ≥ 0 ∧ x = n} im Programm. Es wird
while (x 0) { angenommen, dass diese
y = y * x;
x = x - 1;
{y = n!}
(post condition)
10
≠
{P} S {Q}
• Induktionsannahme = Invariante
11
Schlussregeln
Prämissen
Konklusion
12
Hoare-Kalkül
• A0. Zuweisung
• R1. Konsequenzregel
• R0. Konstanzregel
• A3. Kompositionsregel
16
≠
≠
fi
≠
fi
A1. Schwächere Nachbedingung
{Q ⟹ R},{P}S{Q}
{P} S {R}
{(x>0) ⟹ (x≥0)},{P}S{x>0}
{P} S {x ≥ 0}
17
A2. Stärkere Vorbedingung
{P ⟹ Q},{Q}S{R}
{P} S {R}
{(x>0) ⟹ (x≥0)},{x≥0}S{Q}
{x > 0} S {Q}
18
R1. Konsequenzregel
P‘ ⟹ P,Q ⟹ Q’,{P} A {Q}
{P‘} A {Q‘}
P’ P Q Q’ P Q
{(x>0) ⟹ (x≥0)}, {(y>3) ⟹ (y≥0)} ,{x≥0} S {y>3}
{x > 0} S {y ≥ 0}
P’ Q’
19
A3. Kompositionsregel {P} A {Q}, {Q} B {R}
{P} A; B {R}
{x > 0} {x > 0}
x = x+3; x = x+3;
{x > 0} {x > 0}
{x > 3} {x > 3}
x = x+3; x = x+3;
{x ≥ 3} (A1)
x = x+1; x = x+1;
{x > 3} {x ≥ 3}
{x > 4} {x ≥ 4}
x = x+1; x = x+1;
{x > 4} {x ≥ 4}
20 Alle verwendeten Regeln angeben!
A5. Bedingungsregel {P ∧ c} A {Q}, {P ∧ ¬c} B {Q}
{P} if (c) then A else B end {Q}
{P}
if (c) then
{P ∧ c}
{Q}
end
{Q}
21
A5. Bedingungsregel {P ∧ c} A {Q}, {P ∧ ¬c} B {Q}
{P} if (c) then A else B end {Q}
{y > 0}
if (x > 0) then y = y+x; else y = y-x; end
{y > 0}
{y > 0} if ( x > 0 ) then
{y > 0 ∧ x > 0}
y = y+x;
{y > 0}
else
{y > 0 ∧ x ≤ 0}
y = y-x;
{y > 0}
end
{y > 0}
22
A4. Schleifenregel (Partielle Korrektheit)
{P ∧ c} A {P}
• while-loop
{P} while (c) do A end {P ∧ ¬c}
• Idee:
23
A4. Schleifenregel (Partielle Korrektheit)
{P ∧ c} A {P}
• Induktion
{P} while (c) do A end {P ∧ ¬c}
24
A4. Schleifenregel (Partielle Korrektheit)
{P ∧ c} A {P}
{P} while (c) do A end {P ∧ ¬c}
{x ≤ 10}
{P}
while (x < 10) do
while (c) do
{x ≤ 10 ∧ x < 10}
{P ∧ c}
{x+1 ≤ 10}
A;
x = x+1;
{P}
{x ≤ 10}
end
end
{P ∧ ¬c}
{x ≤ 10 ∧ ¬(x < 10)}
25 Alle verwendeten Regeln angeben!
Was beschreibt eine Schleifeninvariante?
26
Wie ndet man eine Schleifeninvariante?
• Kreativität
• Faustregel
27
fi
{n ≥ 0} Beispiel
x = n;
Was wissen wir?
x y
y = 1;
{Invariante}
4 1 • Zu Beginn x=n, y=1
3 4
while (x 0) { • Am Ende x=0, y=n!
2 12
{Invariante ∧ (x 0)}
y = y * x;
1 24 • In jeder Iteration:
0 24
x = x - 1;
• y wird größer, x wird kleiner
{Invariante}
} • y hält das Zwischenergebnis
x! * y = n!
{Invariante ∧ ¬ (x 0)}
• x! muss noch berechnet werden
{y = n!}
28 • n! ist das gewünschte Ergebnis
≠
≠
≠
{n ≥ 0}
x = n;
Beispiel
y = 1;
{x! * y = n!}
while (x 0) {
{x! * y = n! ∧ (x 0)}
{(x-1)! * y *x = n!} {x!*y = n! ∧ x=0} // 0!=1
y = y * x;
{y=n! ∧ x=0}
{(x-1)! * y = n!}
x = x - 1;
{x! * y = n!}
}
{x! * y = n! ∧ ¬ (x 0)}
{y = n!} 29 Alle verwendeten Regeln angeben!
≠
≠
≠
{n > 0 ∧ m ≥ 0} Beispiel: n (3 )
m 4
r s
s = 0;
{Invariante}
3 1
while (s<m) { • Am Ende r=nm, s=m
9 2
{Invariante ∧ (s<m)}
27 3
• In jeder Iteration:
r=r*n;
s = s+1;
81 4 • s wird größer, r wird größer
{Invariante}
• r hält das Zwischenergebnis ns
} r*n(m-s) = nm
r = ns • n(m-s) muss noch berechnet werden
{Invariante ∧ ¬(s<m)}
{r = nm}
• nm ist das gewünschte Ergebnis
30
{n > 0 ∧ m ≥ 0}
r = 1;
Beispiel: n m
s = 0;
{r = ns}
while (s<m) { • s≥m, aber wir benötigen s=m, um von r = ns auf
{r = ns ∧ (s<m)} r = nm zu schließen
{r*n = ns+1}
• Zwei mögliche Lösungen:
r=r*n;
s = s+1;
Variante: x
{x-1 < z}
x = x – 1;
while(Divisor<=r.Rest){
r.Rest = r.Rest-Divisor;
{x>=0, y>0}
r.Quotient = r.Quotient+1;
q = 0;
} r = x;
return r;
while(y<=r) {
}
r = r-y;
Q: {r.Quotient = Dividend/Divisor, r.Rest = q = q+1;
Dividend%Divisor} }
{q = x/y, r = x%y}
34
{x>=0, y>0}
{x>=0, y>0, 0=0, x=x} EM
x y q r
Invariante: x = r + q*y
q = 0;
Variante: r-y
13 3 0 13
{x>=0, y>0, q=0, x=x} A0
r = x;
13 3 1 10 {r-y=z, y≤r, y>0}
{x>=0, y>0, q=0, r=x} A0, A3
{r-y-y<z}
13 3 2 7 A0
{x = r+q*y} A4
r = r-y;
while(y<=r) { 13 3 3 4
{r-y<z} A0
{x = r+q*y, y<=r} A4
13 3 4 1 q = q+1;
{x = r-y+q*y+y} A0, EM
{r-y<z}
r = r-y;
{x = r+(q+1)*y} A0, A3
q = q+1;
{x = r+q*y} A4
}
{x = r+q*y, y>r} A4
{x>=0, y>0} R0