• Keine Ergebnisse gefunden

Korrekte Software: Grundlagen und Methoden Vorlesung 3 vom 17.04.18: Denotationale Semantik

N/A
N/A
Protected

Academic year: 2022

Aktie "Korrekte Software: Grundlagen und Methoden Vorlesung 3 vom 17.04.18: Denotationale Semantik"

Copied!
39
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Korrekte Software: Grundlagen und Methoden Vorlesung 3 vom 17.04.18: Denotationale Semantik

Serge Autexier, Christoph Lüth

Universität Bremen

Sommersemester 2018

(2)

Fahrplan

I Einführung

I Operationale Semantik

I Denotationale Semantik

I Äquivalenz der Operationalen und Denotationalen Semantik

I Die Floyd-Hoare-Logik

I Invarianten und die Korrektheit des Floyd-Hoare-Kalküls

I Strukturierte Datentypen

I Modellierung und Spezifikation

I Verifikationsbedingungen

I Vorwärts mit Floyd und Hoare

I Funktionen und Prozeduren

(3)

Überblick

I Kleinster Fixpunkt

I Denotationale Semantik für C0

(4)

Fixpunkt

I Seif :AAeine Funktion. EinFixpunkt von f ist einaA, so dassf(a) =a.

I Beispiele

I Fixpunkte vonf(x) =

x sind 0 und 1; ebenfalls fürf(x) =x2.

I Für die Sortierfunktion sind alle sortierten Listen Fixpunkte

(5)

Regeln und Regelinstanzen

Definition

Sei R eine Menge von Regeln x1y...xn,n≥0.

Die Anwendung einer Regel auf spezifische a1. . .an ist eine Regelinstanz

I Betrachte folgende RegelmengeR

− 22

− 23

n m

n·m

I Regelinstanzen sind

− 4

− 8

4 8

32

4 4

16

16 32

512

3 5

15 . . .

(6)

Induktive Definierte Mengen

Definition

Seit R eine Menge von Regelinstanzen undB eine Menge. Dann definieren wir

R(B) =ˆ {y | ∃x1, . . . ,xkB. x1, . . . ,xk

yR} und Rˆ0(B) =B und ˆRi+1(B) = ˆR( ˆRi(B))

(7)

Beispiel

I Betrachte folgende RegelmengeR

− 22

− 23

n m

n·m

I Was sind

Rˆ0(∅) =∅

Rˆ1(∅) = ˆR(∅) ={4,8}

Rˆ2(∅) = ? Rˆ3(∅) = ? Rˆi+1(∅) = ?

(8)

Beispiel

I Betrachte folgende RegelmengeR

− 22

− 23

n m

n·m

I Was sind

Rˆ0(∅) =∅

Rˆ1(∅) = ˆR(∅) ={4,8}

Rˆ2(∅) ={16,32,64,4,8}

Rˆ3(∅) = ? Rˆi+1(∅) = ?

(9)

Beispiel

I Betrachte folgende RegelmengeR

− 22

− 23

n m

n·m

I Was sind

Rˆ0(∅) =∅

Rˆ1(∅) = ˆR(∅) ={4,8}

Rˆ2(∅) ={16,32,64,4,8}

Rˆ3(∅) ={128,256,512,1024,2048,4096,16,32,64,4,8}

Rˆi+1(∅) = ?

(10)

Beispiel

I Betrachte folgende RegelmengeR

− 22

− 23

n m

n·m

I Was sind

Rˆ0(∅) =∅

Rˆ1(∅) = ˆR(∅) ={4,8}

Rˆ2(∅) ={16,32,64,4,8}

Rˆ3(∅) ={128,256,512,1024,2048,4096,16,32,64,4,8}

Rˆi+1(∅) ={22k+3l|1≤k+l ≤2i}

(11)

Induktive Definierte Mengen

Definition

Seit R eine Menge von Regelinstanzen undB eine Menge. Dann definieren wir

R(B) =ˆ {y | ∃x1, . . . ,xkB. x1, . . . ,xk

yR} und Rˆ0(B) =B und ˆRi+1(B) = ˆR( ˆRi(B))

Definition (Abgeschlossen und Monoton)

I Eine MengeS is abgeschlossen unter R (R-abgeschlossen)gdw.

R(S)ˆ ⊆S

I Eine Operationf ist monotongdw.

∀A,B.ABf(A)⊆f(B)

(12)

Kleinster Fixpunkt Operator

Lemma

Für jede Menge von Regelinstanzen R ist die induzierte Operation Rˆ monoton.

Lemma

Sei Ai = ˆRi(∅) für alle i ∈Nund A=Si∈NAi. Dann gilt (a) A ist R-abgeschlossen,

(b) R(A) =ˆ A, und

(c) A ist die kleinste R-abgeschlossene Menge.

(13)

Beweis von Lemma (a).

A istR-abgeschlossen:

Sei x1,...,xy kR und x1, . . . ,xkA.

DaA=Si∈NAi gibt es einj so dassx1, . . . ,xkAj. Also auch:

yR(Aˆ j) = ˆR( ˆRj(∅))

= ˆRj+1(∅)

=Aj+1A.

(14)

Beweis von Lemma (b): ˆR(A) =A.

I R(A)ˆ ⊆A:

DaA R-abgeschlossen gilt auch ˆR(A)A.

I AR(A):ˆ

SeiyA. Dann ∃n>0.yAn undy 6∈An−1.

Folglich muss es eine Regelinstanz x1,...,xy kR geben mit x1, . . . ,xkAn−1A.

Da ˆR monoton gilt ˆR(An−1)⊆R(A).ˆ

DayAn= ˆR(An−1) folgt darausyR(A).ˆ

(15)

Beweis von Lemma (c).

A ist die kleinsteR-abgeschlossene Menge, d.h. für jede R-abgeschlossene MengeB giltAB.

Beweis per Induktion über n dass gilt AnB:

I Basisfall:

A0 =∅ ⊆B

I Induktionsschritt:

DaB R-abgeschlossen ist gilt: ˆR(B)B.

Induktionsannahme:AnB.

Dann giltAn+1= ˆR(An)⊆R(B)ˆ ⊆B weil ˆR monoton und B ist R-abgeschlossen.

(16)

Kleinster Fixpunkt Operator

Definition

fix( ˆR) = [

n∈N

Rˆn(∅) ist der kleinste Fixpunkt.

(17)

Kleinster Fixpunkt

I Betrachte folgende RegelmengeR

− 22

− 23

n m

n·m

I Was sind

Rˆ1(∅) = ˆR(∅) ={4,8}

Rˆ2(∅) =?

Rˆ3(∅) =?

Rˆi+1(∅) =?

I Wie siehtfix( ˆR) aus?

(18)

Kleinster Fixpunkt

I Betrachte folgende RegelmengeR

− 22

− 23

n m

n·m

I Was sind

Rˆ1(∅) = ˆR(∅) ={4,8}

Rˆ2(∅) =?

Rˆ3(∅) =?

Rˆi+1(∅) =?

(19)

Denotationale Semantik - Motivation

I Operationale Semantik

Eine Menge von Regeln, die einen Zustand und ein Programm in einen neuen Zustand oder Fehler überführen

hc, σi →Stmt σ0|⊥

I Denotationale Semantik

Eine Menge von Regeln, die ein Programm in einepartielle Funktion

| {z }

Denotat

von Zustand nach Zustand überführen C[[c]] : Σ*Σ

(20)

Denotationale Semantik - Motivation

Zwei Programme sind äquivalent gdw. sie immer zum selben Zustand (oder Fehler) auswerten

c0c1 iff (∀σ, σ0.hc0, σi →Stmt σ0⇔ hc1, σi →Stmt σ0) oder

Zwei Programme sind äquivalent gdw. sie die selbe partielle Funktion denotieren

c0c1 iff {(σ, σ0)|hc0, σi →Stmt σ0}={(σ, σ0)|hc1, σi →Stmt σ0}

(21)

Denotierte Funktionen

I jedera:Aexp denotiert eine partielle Funktion Σ*Z

I jederb :Bexpdenotiert eine partielle Funktion Σ*T

I jedesc :Stmt denotiert eine partielle Funktion Σ*Σ

(22)

Denotat von Aexp

A[[a]] :Aexp→(Σ*Z) A[[n]] = {(σ,n)|σ ∈Σ}

A[[x]] = {(σ, σ(x))|σ ∈Σ,xDom(σ)}

A[[a0+a1]] = {(σ,n0+n1)|(σ,n0)∈ A[[a0]]∧(σ,n1)∈ A[[a1]]}

A[[a0a1]] = {(σ,n0n1)|(σ,n0)∈ A[[a0]]∧(σ,n1)∈ A[[a1]]}

A[[a0a1]] = {(σ,n0n1)|(σ,n0)∈ A[[a0]]∧(σ,n1)∈ A[[a1]]}

A[[a0/a1]] = {(σ,n0/n1)|(σ,n0)∈ A[[a0]]∧(σ,n1)∈ A[[a1]]∧n1 6= 0}

(23)

Denotat von Bexp

B[[a]] :Bexp→(Σ*T) B[[1]] = {(σ,1)|σ∈Σ}

B[[0]] = {(σ,0)|σ∈Σ}

B[[a0==a1]] = {(σ,1)|σ∈Σ,(σ,n0)∈ A[[a0]](σ), (σ,n1)∈ A[[a1]],n0=n1}

∪{(σ,0)|σ ∈Σ,(σ,n0)∈ A[[a0]](σ), (σ,n1)∈ A[[a1]],n06=n1} B[[a0 <a1]] = {(σ,1)|σ∈Σ,(σ,n0)∈ A[[a0]](σ),

(σ,n1)∈ A[[a1]],n0<n1}

∪{(σ,0)|σ ∈Σ,(σ,n0)∈ A[[a0]](σ), (σ,n1)∈ A[[a1]],n0n1}

(24)

Denotat von Bexp

B[[a]] :Bexp→(Σ*T) B[[!b]] = {(σ,1)|σ∈Σ,(σ,0)∈ B[[b]]}

∪{(σ,0)|σ∈Σ,(σ,1)∈ B[[b]]}

B[[b1 &&b2]] = {(σ,0)|σ∈Σ,(σ,0)∈ B[[b1]]}

∪{(σ,t2)|σ ∈Σ,(σ,1)∈ B[[b1]],(σ,t2)∈ B[[b2]]}

B[[b1 ||b2]] = {(σ,1)|σ∈Σ,(σ,1)∈ B[[b1]]}

∪{(σ,t2)|σ ∈Σ,(σ,0)∈ B[[b1]],(σ,t2)∈ B[[b2]]}

(25)

Denotat von Stmt

C[[.]] :Stmt→(Σ*Σ)

C[[x =a]] = {(σ, σ[n/x])|σ∈Σ∧(σ,n)∈ A[[a]]}

C[[c1;c2]] = C[[c2]]◦ C[[c1]] Komposition von Relationen C[[{ }]] = Id Id:={(σ, σ)|σ∈Σ}

C[[if(b) c0 else c1]] = {(σ, σ0)|(σ,1)∈ B[[b]]∧(σ, σ0)∈ C[[c0]]}

∪{(σ, σ0)|(σ,0)∈ B[[b]]∧(σ, σ0)∈ C[[c1]]}

Aber was ist

C[[while(b) c]] =??

(26)

Denotat von Stmt

C[[.]] :Stmt→(Σ*Σ)

C[[x =a]] = {(σ, σ[n/x])|σ∈Σ∧(σ,n)∈ A[[a]]}

C[[c1;c2]] = C[[c2]]◦ C[[c1]] Komposition von Relationen C[[{ }]] = Id Id:={(σ, σ)|σ∈Σ}

C[[if(b) c0 else c1]] = {(σ, σ0)|(σ,1)∈ B[[b]]∧(σ, σ0)∈ C[[c0]]}

∪{(σ, σ0)|(σ,0)∈ B[[b]]∧(σ, σ0)∈ C[[c1]]}

Aber was ist

C[[while(b) c]] =??

(27)

Denotationale Semantik für while

Sei wwhile(b) c (undσ∈Σ). Wir wissen bereits, dass gilt wif(b) {c;w} else { }

C[[w]] = {(σ, σ0)|(σ,1)∈ B[[b]]∧(σ, σ0)∈ C[[{c;w}]]}

∪{(σ, σ)|(σ,0)∈ B[[b]]}

= {(σ, σ0)|(σ,1)∈ B[[b]]∧(σ, σ0)∈ C[[w]]◦ C[[c]]}

∪{(σ, σ)|(σ,0)∈ B[[b]]}

= {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]∧(σ00, σ0)∈ C[[w]]}

∪{(σ, σ)|(σ,0)∈ B[[b]]}

(28)

Denotationale Semantik für while

Sei wwhile(b) c (undσ∈Σ). Wir wissen bereits, dass gilt wif(b) {c;w} else { }

C[[w]] = {(σ, σ0)|(σ,1)∈ B[[b]]∧(σ, σ0)∈ C[[{c;w}]]}

∪{(σ, σ)|(σ,0)∈ B[[b]]}

= {(σ, σ0)|(σ,1)∈ B[[b]]∧(σ, σ0)∈ C[[w]]◦ C[[c]]}

∪{(σ, σ)|(σ,0)∈ B[[b]]}

= {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]∧(σ00, σ0)∈ C[[w]]}

∪{(σ, σ)|(σ,0)∈ B[[b]]}

(29)

Denotationale Semantik für while

Sei wwhile(b) c (undσ∈Σ). Wir wissen bereits, dass gilt wif(b) {c;w} else { }

C[[w]] = {(σ, σ0)|(σ,1)∈ B[[b]]∧(σ, σ0)∈ C[[{c;w}]]}

∪{(σ, σ)|(σ,0)∈ B[[b]]}

= {(σ, σ0)|(σ,1)∈ B[[b]]∧(σ, σ0)∈ C[[w]]◦ C[[c]]}

∪{(σ, σ)|(σ,0)∈ B[[b]]}

= {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]∧(σ00, σ0)∈ C[[w]]}

∪{(σ, σ)|(σ,0)∈ B[[b]]}

(30)

Denotationale Semantik von while

Sei wwhile(b) c (undσ∈Σ). Wir wissen bereits, dass gilt wif(b) {c;w} else { }

C[[w]]0 = {(σ, σ)|(σ,0)∈ B[[b]](σ)}

C[[w]]1 = {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]

∧(σ00, σ0)∈ C[[w]]0}

C[[w]]2 = {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]

∧(σ00, σ0)∈ C[[w]]1} ...

C[[w]]i+1 = {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]

∧(σ00, σ0)∈ C[[w]]i}

Γ(ϕ) = {(σ, σ0)| ∃σ00.B[[b]](σ) = 1∧(σ, σ00)∈ C[[c]]∧(σ00, σ0)∈ϕ}

∪{(σ, σ)| B[[b]](σ) = 0}

(31)

Denotationale Semantik von while

Sei wwhile(b) c (undσ∈Σ). Wir wissen bereits, dass gilt wif(b) {c;w} else { }

C[[w]]0 = {(σ, σ)|(σ,0)∈ B[[b]](σ)}

C[[w]]1 = {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]

∧(σ00, σ0)∈ C[[w]]0}

C[[w]]2 = {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]

∧(σ00, σ0)∈ C[[w]]1} ...

C[[w]]i+1 = {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]

∧(σ00, σ0)∈ C[[w]]i}

Γ(ϕ) = {(σ, σ0)| ∃σ00.B[[b]](σ) = 1∧(σ, σ00)∈ C[[c]]∧(σ00, σ0)∈ϕ}

∪{(σ, σ)| B[[b]](σ) = 0}

(32)

Denotationale Semantik von while

Sei wwhile(b) c (undσ∈Σ). Wir wissen bereits, dass gilt wif(b) {c;w} else { }

C[[w]]0 = {(σ, σ)|(σ,0)∈ B[[b]](σ)}

C[[w]]1 = {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]

∧(σ00, σ0)∈ C[[w]]0}

C[[w]]2 = {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]

∧(σ00, σ0)∈ C[[w]]1} ...

C[[w]]i+1 = {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]

∧(σ00, σ0)∈ C[[w]]i}

Γ(ϕ) = {(σ, σ0)| ∃σ00.B[[b]](σ) = 1∧(σ, σ00)∈ C[[c]]∧(σ00, σ0)∈ϕ}

∪{(σ, σ)| B[[b]](σ) = 0}

(33)

Denotationale Semantik von while

Sei wwhile(b) c (undσ∈Σ). Wir wissen bereits, dass gilt wif(b) {c;w} else { }

C[[w]]0 = {(σ, σ)|(σ,0)∈ B[[b]](σ)}

C[[w]]1 = {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]

∧(σ00, σ0)∈ C[[w]]0}

C[[w]]2 = {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]

∧(σ00, σ0)∈ C[[w]]1} ...

C[[w]]i+1 = {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]

∧(σ00, σ0)∈ C[[w]]i}

Γ(ϕ) = {(σ, σ0)| ∃σ00.B[[b]](σ) = 1∧(σ, σ00)∈ C[[c]]∧(σ00, σ0)∈ϕ}

∪{(σ, σ)| B[[b]](σ) = 0}

(34)

Denotationale Semantik von while

Sei wwhile(b) c (undσ∈Σ). Wir wissen bereits, dass gilt wif(b) {c;w} else { }

C[[w]]0 = {(σ, σ)|(σ,0)∈ B[[b]](σ)}

C[[w]]1 = {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]

∧(σ00, σ0)∈ C[[w]]0}

C[[w]]2 = {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]

∧(σ00, σ0)∈ C[[w]]1} ...

C[[w]]i+1 = {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]

∧(σ00, σ0)∈ C[[w]]i}

(35)

Denotationale Semantik von while

Sei wwhile(b) c (undσ∈Σ). Wir wissen bereits, dass gilt wif(b) {c;w} else { }

Γ(ψ) = {(σ, σ0)| ∃σ00.(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]∧(σ00, σ0)∈ψ}

∪{(σ, σ)|(σ,0)∈ B[[b]]}

Γ ist wie ˆR, wobeiR definiert ist wie folgt:

R =

00, σ0)

(σ, σ0) |(σ,1)∈ B[[b]]∧(σ, σ00)∈ C[[c]]

(σ, σ) |(σ,0)∈ B[[b]]

und die Semantik von w ist der Fixpunkt von Γ, d.h. C[[w]] =fix(Γ)

(36)

Denotation für Stmt

C[[.]] :Stmt→(Σ*Σ)

C[[x =a]] = {(σ, σ[n/X])|σ ∈Σ∧(σ,n)∈ A[[a]]}

C[[c1;c2]] = C[[c2]]◦ C[[c1]] Komposition von Relationen C[[{ }]] = Id Id:={(σ, σ)|σ∈Σ}

C[[if(b) c0 else c1]] = {(σ, σ0)|(σ,1)∈ B[[b]]∧(σ, σ0)∈ C[[c0]]}

∪ {(σ, σ0)|(σ,0)∈ B[[b]]∧(σ, σ0)∈ C[[c1]]}

C[[while (b)c]] = fix(Γ) mit

Γ(ψ) = {(σ, σ0)|(σ,1)∈ B[[b]]∧(σ, σ0)∈ψ◦ C[[c]]}

(37)

Weitere Intuition zur Fixpunkt Konstruktion

I Seiwwhile (b) c

I Zur Erinnerung: Wir haben begonnen mitwif(b) {c;w} else { }

I Dann müsste auch gelten

C[[w]]=! C[[if(b) {c;w} else { }]]

I Beweis an der Tafel

(38)

Beweis C [[w ]] =

!

C [[if (b) {c ; w } else { }]]

C[[w]] =fix(Γ)

= Γ(fix(Γ))

= Γ(C[[w]])

={(σ, σ0)|(σ,1)∈ B[[b]]∧(σ, σ0)∈ C[[w]]◦ C[[c]]}

∪ {(σ, σ)|(σ,0)∈ B[[b]]}

={(σ, σ0)|(σ,1)∈ B[[b]]∧(σ, σ0)∈ C[[c; w]]}

∪ {(σ, σ)|(σ,0)∈ B[[b]]}

=C[[if (b) {c;w} else { }]]

(39)

Fahrplan

I Einführung

I Operationale Semantik

I Denotationale Semantik

I Äquivalenz der Operationalen und Denotationalen Semantik

I Die Floyd-Hoare-Logik

I Invarianten und die Korrektheit des Floyd-Hoare-Kalküls

I Strukturierte Datentypen

I Modellierung und Spezifikation

I Verifikationsbedingungen

I Vorwärts mit Floyd und Hoare

I Funktionen und Prozeduren

I Referenzen

I Ausblick und Rückblick

Referenzen

ÄHNLICHE DOKUMENTE

Partial cor- rectness means that if the programs starts in a state where the precondition P holds, and it terminates, then it does so in a state which satisfies the postcondition

I Die (operationale) Semantik einer imperativen Sprache wie C0 ist ein Zustandsübergang: das System hat einen impliziten Zustand, der durch Zuweisung von Werten an Adressen

Zu zeigen ist, dass sowohl w also auch if (b) {c; w} else { } zu dem selben Programmzustand auswerten oder beide zu

[r]

Für die While-Schleife Rückgriff auf Definition des Fixpunkts und Induktion über die Teilmengen Γ i (∅) des Fixpunkts.. Für die While-Schleife Rückgriff auf Definition des

Partial cor- rectness means that if the programs starts in a state where the precondition P holds, and it terminates, then it does so in a state which satisfies the postcondition

Korrekte Software: Grundlagen und Methoden Vorlesung 4 vom 24.04.17: Denotationale Semantik.. Serge Autexier,

[r]