84
Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert U NIVERSITÄT KOBLENZ -L ANDAU Formal Methods in Software Engineering – p.1

Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Formal Methods in Software Engineering

Dynamic Logic

Bernhard Beckert

UNIVERSITÄT KOBLENZ -LANDAU

Formal Methods in Software Engineering – p.1

Page 2: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

WHILE: A Simple Programming Language

Logical basis

Typed first-order predicate logic

(Types, variables, terms, formulas, . . . )

Formal Methods in Software Engineering – p.2

Page 3: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

WHILE: A Simple Programming Language

Logical basis

Typed first-order predicate logic

(Types, variables, terms, formulas, . . . )

Assumption for examples

The signature contains a type Nat and appropriate symbols:

function symbols 0, s,+,∗ (terms s(0), s(s(0)), . . . written as 1, 2, . . .)

predicate symbols.=, <, ≤, >, ≥

NOTE: This is a “convenient assumption” not a definition

Formal Methods in Software Engineering – p.2

Page 4: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

WHILE: A Simple Programming Language

Programs

Assignments: X := t X: variable, t: term

Test: if B then α else β fi B: quantifier-free formula,α, β: programs

Loop: while B do α od B: quantifier-free formula,α: program

Composition: α; β α, β programs

WHILE is computationally complete

Formal Methods in Software Engineering – p.3

Page 5: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

WHILE: Examples

Compute the square of X and store it in Y

Y := X ∗X

Formal Methods in Software Engineering – p.4

Page 6: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

WHILE: Examples

Compute the square of X and store it in Y

Y := X ∗X

If X is positive then add one else subtract one

if X > 0 then X := X + 1 else X := X− 1 fi

Formal Methods in Software Engineering – p.4

Page 7: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

WHILE: Example – Square of a Number

Compute the square of X (the complicated way)

Making use of: n2= 1 + 3 + 5 + · · ·+ (2n− 1)

I := 0;

Y := 0;

while I < X do

Y := Y + 2 ∗ I + 1;

I := I + 1

od

αsquare

Formal Methods in Software Engineering – p.5

Page 8: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

WHILE: Example – Multiplication

Russian multiplication

Z := 0;

while ¬ (B.= 0) do

if ((B/2) ∗ 2.= B) then

A := 2 ∗A;

B := B/2

else

Z := Z + A;

A := 2 ∗A;

B := B/2

fi

od

αmult

Formal Methods in Software Engineering – p.6

Page 9: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

WHILE: Operational Semantics

Given

A (fixed) first-order structure A interpretingthe function and predicate symbols in the signature

State

s = (A , β) where

β a variable assignment (i.e. function interpreting the vari ables )

Formal Methods in Software Engineering – p.7

Page 10: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

WHILE: Operational Semantics

State update

s[X/e] = (A , β[X/e])

with

β[X/e](Y) =

e if Y = X

β(Y) otherwise

Formal Methods in Software Engineering – p.8

Page 11: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

WHILE: Operational Semantics

Define the relation s[[α]]s′ as follows

s[[X := t]]s′ iff s′ = s[X/s(t)]

s[[if B then α else β fi ]]s′ iff

s |= B and s[[α]]s′ or s |= ¬ B and s[[β]]s′

s[[while B do α od ]]s′ iff there are states s = s0, . . . , st = s′ s.t.

si |= B for 0 ≤ i ≤ t− 1 and st |= ¬ B and

s0[[α]]s1, s1[[α]]s2, . . . , st−1[[α]]st

s[[α; β]]s′ iff there is a state s′′ such that

s[[α]]s′′ and s′′[[β]]s′

[[α]] is a partial functionFormal Methods in Software Engineering – p.9

Page 12: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

A Different Approach to WHILE

Programs

X := t (atomic program)

α; β (sequential composition)

α∪ β (non-deterministic choice)

α∗ (non-deterministic iteration, n times for some n ≥ 0)

F? (test)remains in initial state if F is true, does not terminate if F is false

Formal Methods in Software Engineering – p.10

Page 13: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

A Different Approach to WHILE

Restriction to deterministic programs

Non-deterministic program constructors may only be used in

if B then α else β fi ≡ (B?; α)∪ ((¬ B)?; β)

while B do α od ≡ (B?; α)∗; (¬ B)?

Formal Methods in Software Engineering – p.11

Page 14: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Expressing Program Properties

Logic for expressing properties

Full first-order logic (usually with arithmetic)

Formal Methods in Software Engineering – p.12

Page 15: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Expressing Program Properties

Logic for expressing properties

Full first-order logic (usually with arithmetic)

Partial correctness assertion (Hoare formula)

{P} α {Q}

Meaning:

If α is started in a state satisfying P and terminates,then its final state satisfies Q

Formally:

{P} α {Q} is valid ifffor all states s, s′, if s |= P and s[[α]]s′, then s′ |= Q

Formal Methods in Software Engineering – p.12

Page 16: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Expressing Program Properties: Examples

{true} X := X + 1 {X > 1}

{even(X)} X := X + 2 {even(X)} where even(X) ≡ ∃Z (X.= 2 ∗Z)

{true} αsquare {Y = X ∗X}

Formal Methods in Software Engineering – p.13

Page 17: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

An Annotated Program

Z := 0;

assert X.= A ∧ Y

.= B;

while ¬ (B.= 0) do

assert A ∗ B + Z.= X ∗Y;

if ((B/2) ∗ 2.= B) then

A := 2 ∗A;

B := B/2

elseZ := Z + A;

A := 2 ∗A;

B := B/2

fiodassert B

.= 0

assert Z.= X ∗Y

Note

X, Y are “external” variables

Formal Methods in Software Engineering – p.14

Page 18: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Dynamic Logic

The idea of dynamic logic

Annotated programs use formulas within programs

Dynamic Logic uses programs within formulas

Instead of “assert F” after program segment α, write: [α] F

Formal Methods in Software Engineering – p.15

Page 19: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Dynamic Logic

The idea of dynamic logic

Annotated programs use formulas within programs

Dynamic Logic uses programs within formulas

Instead of “assert F” after program segment α, write: [α] F

A multi-modal logic

the states are the possible worlds

two modalities [α] and 〈α〉 for each program α

state s′ is α-reachable from state s iff s[[α]]s′

Formal Methods in Software Engineering – p.15

Page 20: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Dynamic Logic: Semantics

Semantics

[α] F true in a state s iffF is true in all states that are α-reachable from s

(partial correctness)

〈α〉F true in a state s iffF is true in some state that is α-reachable from s

(total correctness)

A formula is valid iff it is valid in all states

Formal Methods in Software Engineering – p.16

Page 21: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Dynamic Logic: Examples

Example formulas (validity depends on α,β)

(〈α〉X.= Y)↔ (〈β〉X

.= Y)

∃X 〈α〉 true

Formal Methods in Software Engineering – p.17

Page 22: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Dynamic Logic: Examples

Example formulas (validity depends on α,β)

(〈α〉X.= Y)↔ (〈β〉X

.= Y)

∃X 〈α〉 true

Valid formulas

[X := 1] X.= 1

[while true do X := X od ] false

〈α∗〉F→ (F ∨ 〈α∗〉(¬ F ∧ 〈α〉F))

Formal Methods in Software Engineering – p.17

Page 23: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Dynamic Logic: Examples

Example formulas (validity depends on α,β)

(〈α〉X.= Y)↔ (〈β〉X

.= Y)

∃X 〈α〉 true

Valid formulas

[X := 1] X.= 1

[while true do X := X od ] false

〈α∗〉F→ (F ∨ 〈α∗〉(¬ F ∧ 〈α〉F))

Multiplication example

∀A, B, X, Y, Z(X.= A∧Y

.= B→ [αmult] Z

.= X ∗Y)

Formal Methods in Software Engineering – p.17

Page 24: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Dynamic Logic: More Examples

Hoare formulas

{P} α {Q} the same as P→ [α] Q

Formal Methods in Software Engineering – p.18

Page 25: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Dynamic Logic: More Examples

Hoare formulas

{P} α {Q} the same as P→ [α] Q

Duality of the modal operators

[α] P ↔ ¬ 〈α〉¬ P

Formal Methods in Software Engineering – p.18

Page 26: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Some DL-Tautologies

Assumption: X does not occur in π

(∃X 〈π〉F) ↔ (〈π〉∃X F)

(∀X [π]F) ↔ ([π]∀X F)

(∃X [π]F) → ([π]∃X F)

([π]∃X F) → (∃X [π]F) provided π is deterministic

(〈π〉∀X F) → (∀X 〈π〉F)

(∀X 〈π〉F) → (〈π〉∀X F) provided π is deterministic

(〈π〉(F ∧ G)) → ((〈π〉F) ∧ 〈π〉G)

((〈π〉F) ∧ 〈π〉G) → (〈π〉(F ∧ G)) provided π is deterministic

Formal Methods in Software Engineering – p.19

Page 27: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

A Sequent Calculus for Dynamic Logic

Sequent

Γ ∆

MeaningV

Γ logically impliesW

(for all variable assignments, i.e.,free variables in the sequent are implicitly universally qu antified)

Formal Methods in Software Engineering – p.20

Page 28: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Sequent Rules

Form of sequent rules

Γ1 ∆1

Γ2 ∆2

orΓ1 ∆1 Γ′1 ∆′1

Γ2 ∆2

(rules can also have more than two premisses)

Formal Methods in Software Engineering – p.21

Page 29: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Sequent Rules

Form of sequent rules

Γ1 ∆1

Γ2 ∆2

orΓ1 ∆1 Γ′1 ∆′1

Γ2 ∆2

(rules can also have more than two premisses)

Meaning

The conclusion is true in a statewhenever all premisses are true in that state

In particular:

The conclusion is valid whenever all premisses are valid

Formal Methods in Software Engineering – p.21

Page 30: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Sequent Calculus for First-order Logic

Axioms

F, Γ F, ∆ false , Γ ∆ Γ true , ∆

Formal Methods in Software Engineering – p.22

Page 31: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Sequent Calculus for First-order Logic

Axioms

F, Γ F, ∆ false , Γ ∆ Γ true , ∆

Negation Γ F, ∆

Γ, ¬ F ∆

Γ, F ∆

Γ ¬ F, ∆

Formal Methods in Software Engineering – p.22

Page 32: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Sequent Calculus for First-order Logic

Axioms

F, Γ F, ∆ false , Γ ∆ Γ true , ∆

Negation Γ F, ∆

Γ, ¬ F ∆

Γ, F ∆

Γ ¬ F, ∆

Implication Γ F, ∆ Γ, G ∆

Γ, F→ G ∆

Γ, F G, ∆

Γ F→ G, ∆

Formal Methods in Software Engineering – p.22

Page 33: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Sequent Calculus for First-order Logic

Conjunction Γ, F, G ∆

Γ, F∧G ∆

Γ F, ∆ Γ G, ∆

Γ F∧G, ∆

Formal Methods in Software Engineering – p.23

Page 34: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Sequent Calculus for First-order Logic

Conjunction Γ, F, G ∆

Γ, F∧G ∆

Γ F, ∆ Γ G, ∆

Γ F∧G, ∆

Disjunction Γ, F ∆ Γ, G ∆

Γ, F∨G ∆

Γ F, G, ∆

Γ F∨G, ∆

Formal Methods in Software Engineering – p.23

Page 35: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Sequent Calculus for First-order Logic

Universal quantification

Γ, ∀X F, F{X← t} ∆

Γ, ∀X F ∆

t an arbitrary term,

{X← t} admissible for F

Γ F{X← Z}, ∆

Γ ∀X F, ∆

Z a new variable

Formal Methods in Software Engineering – p.24

Page 36: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Sequent Calculus for First-order Logic

Universal quantification

Γ, ∀X F, F{X← t} ∆

Γ, ∀X F ∆

t an arbitrary term,

{X← t} admissible for F

Γ F{X← Z}, ∆

Γ ∀X F, ∆

Z a new variable

Existential quantification

Γ ∃X F, F{X← t}, ∆

Γ ∃X F, ∆

t an arbitrary term,

{X← t} admissible for F

Γ, F{X← Z} ∆

Γ, ∃X F ∆

Z a new variable

Formal Methods in Software Engineering – p.24

Page 37: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example Proof

axiom6 p(V), p(U) p(U), ∀Y p(Y)

5 p(V) p(U), p(U)→ ∀Y p(Y)

4 p(V) p(U), ∃X(p(X)→ ∀Y p(Y))

3 p(V) ∀Y p(Y), ∃X (p(X)→ ∀Y p(Y))

2 p(V)→ ∀Y p(Y), ∃X (p(X)→ ∀Y p(Y))

1 ∃X (p(X)→ ∀Y p(Y))

ex-right

impl-right

all-right

ex-right

impl-right

Formal Methods in Software Engineering – p.25

Page 38: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Admissibility of Substitutions

Motivation

We want to have that

∀X F → Fσ

Fσ → ∃X F

is valid for all formulas F and substitutions σ

Formal Methods in Software Engineering – p.26

Page 39: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Admissibility of Substitutions

Definition

A substitution

{X← t}

is admissible for a formula F iff

there is no variable Y such that

Y occurs in t

there is a quantification ∀Y or ∃Y in F

there is a free occurrencence of X in the scope of that quantification

Formal Methods in Software Engineering – p.27

Page 40: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Sequent Calculus for Dynamic Logic

Cut rule Γ F, ∆ F, Γ ∆

Γ ∆

Formal Methods in Software Engineering – p.28

Page 41: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Sequent Calculus for Dynamic Logic

Cut rule Γ F, ∆ F, Γ ∆

Γ ∆

Equality rules

Γ t.= t, ∆

s.= t, Γ{s← t} ∆{s← t}

s.= t, Γ ∆

t.= s, Γ{s← t} ∆{s← t}

t.= s, Γ ∆

Formal Methods in Software Engineering – p.28

Page 42: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Sequent Calculus for Dynamic Logic

Oracle for first-order logic

Γ ∆if no programs occur in Γ, ∆ and A |=

V

Γ→W

Only of theoretical use! Not computable!

Formal Methods in Software Engineering – p.29

Page 43: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

A Sequent Calculus for Dynamic Logic

Composition rule

Γ [α][β] F, ∆

Γ [α; β] F, ∆

Formal Methods in Software Engineering – p.30

Page 44: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

A Sequent Calculus for Dynamic Logic

Composition rule

Γ [α][β] F, ∆

Γ [α; β] F, ∆

Assignment rule

Γ{X← X′}, X.= t{X← X′} F, ∆{X← X′}

Γ [X := t] F, ∆X′ a new variable

Example:

even(X′), X.= X′+ 2 even(X)

even(X) [X := X + 2] even(X)

Formal Methods in Software Engineering – p.30

Page 45: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

A Sequent Calculus for Dynamic Logic

Conditional rule

Γ, B [α] F, ∆ Γ,¬ B [β] F, ∆

Γ [if B then α else β fi ] F, ∆

Formal Methods in Software Engineering – p.31

Page 46: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Reasoning about Loops

To prove

[while B do body od ] F

find an (arbitrary) formula Inv such that

1. Inv is true before execution of the loop

2. Inv∧ B→ [body]Inv is true

3. Inv∧¬ B→ F is true

Note

Inv is a loop invariant

Formal Methods in Software Engineering – p.32

Page 47: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Sequent Calculus for Dynamic Logic

Loop rule

Γ Inv, ∆ Inv, B [α] Inv Inv,¬ B F

Γ [while B do α od ] F, ∆

Formal Methods in Software Engineering – p.33

Page 48: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

[αsquare] Y.= X ∗X

B: I < X

α: Y := Y + 2 ∗ I + 1; I := I + 1

Formal Methods in Software Engineering – p.34

Page 49: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

[I := 0; Y := 0; while B do α od ] Y.= X ∗X

[αsquare] Y.= X ∗X

B: I < X

α: Y := Y + 2 ∗ I + 1; I := I + 1

Formal Methods in Software Engineering – p.34

Page 50: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

[I := 0] [Y := 0] [while B do α od ] Y.= X ∗X

[I := 0; Y := 0; while B do α od ] Y.= X ∗X

[αsquare] Y.= X ∗X

B: I < X

α: Y := Y + 2 ∗ I + 1; I := I + 1

Formal Methods in Software Engineering – p.34

Page 51: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

I.= 0 [Y := 0] [while B do α od ] Y

.= X ∗X

[I := 0] [Y := 0] [while B do α od ] Y.= X ∗X

[I := 0; Y := 0; while B do α od ] Y.= X ∗X

[αsquare] Y.= X ∗X

B: I < X

α: Y := Y + 2 ∗ I + 1; I := I + 1

Formal Methods in Software Engineering – p.34

Page 52: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

I.= 0, Y

.= 0 [while B do α od ] Y

.= X ∗X

I.= 0 [Y := 0] [while B do α od ] Y

.= X ∗X

[I := 0] [Y := 0] [while B do α od ] Y.= X ∗X

[I := 0; Y := 0; while B do α od ] Y.= X ∗X

[αsquare] Y.= X ∗X

B: I < X

α: Y := Y + 2 ∗ I + 1; I := I + 1

Formal Methods in Software Engineering – p.34

Page 53: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

Invariant Inv: I ≤ X ∧ Y.= I ∗ I

I.= 0, Y

.= 0 Inv Inv, B [α] Inv Inv, ¬ B Y

.= X ∗X

I.= 0, Y

.= 0 [while B do α od ] Y

.= X ∗X

I.= 0 [Y := 0] [while B do α od ] Y

.= X ∗X

[I := 0] [Y := 0] [while B do α od ] Y.= X ∗X

[I := 0; Y := 0; while B do α od ] Y.= X ∗X

[αsquare] Y.= X ∗X

B: I < X

α: Y := Y + 2 ∗ I + 1; I := I + 1

Formal Methods in Software Engineering – p.34

Page 54: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

Left branch (pre-condition implies invariant)

I.= 0, Y

.= 0 I ≤ X ∧ Y

.= I ∗ I

Formal Methods in Software Engineering – p.35

Page 55: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

Left branch (pre-condition implies invariant)

I.= 0, Y

.= 0 0 ≤ X ∧ Y

.= 0 ∗ 0

I.= 0, Y

.= 0 I ≤ X ∧ Y

.= I ∗ I

Formal Methods in Software Engineering – p.35

Page 56: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

Left branch (pre-condition implies invariant)

I.= 0, Y

.= 0 0 ≤ X I

.= 0, Y

.= 0 Y

.= 0 ∗ 0

I.= 0, Y

.= 0 0 ≤ X ∧ Y

.= 0 ∗ 0

I.= 0, Y

.= 0 I ≤ X ∧ Y

.= I ∗ I

Formal Methods in Software Engineering – p.35

Page 57: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

Middle branch (invariant is indeed invariant)

Inv, B [α] Inv

Formal Methods in Software Engineering – p.36

Page 58: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

Middle branch (invariant is indeed invariant)

I ≤ X, Y.= I ∗ I, I < X [Y := Y + 2 ∗ I + 1; I := I + 1] Inv

Inv, B [α] Inv

Formal Methods in Software Engineering – p.36

Page 59: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

Middle branch (invariant is indeed invariant)

I ≤ X, Y.= I ∗ I, I < X [Y := Y + 2 ∗ I + 1] [I := I + 1] Inv

I ≤ X, Y.= I ∗ I, I < X [Y := Y + 2 ∗ I + 1; I := I + 1] Inv

Inv, B [α] Inv

Formal Methods in Software Engineering – p.36

Page 60: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

Middle branch (invariant is indeed invariant)

I ≤ X, Y′.= I ∗ I, I < X, Y := Y′+ 2 ∗ I + 1 [I := I + 1] Inv

I ≤ X, Y.= I ∗ I, I < X [Y := Y + 2 ∗ I + 1] [I := I + 1] Inv

I ≤ X, Y.= I ∗ I, I < X [Y := Y + 2 ∗ I + 1; I := I + 1] Inv

Inv, B [α] Inv

Formal Methods in Software Engineering – p.36

Page 61: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

Middle branch (invariant is indeed invariant)

I′ ≤ X, Y′.= I′ ∗ I′, I′ < X, Y

.= Y′+ 2 ∗ I′+ 1, I

.= I′+ 1 Inv

I ≤ X, Y′.= I ∗ I, I < X, Y := Y′ + 2 ∗ I + 1 [I := I + 1] Inv

I ≤ X, Y.= I ∗ I, I < X [Y := Y + 2 ∗ I + 1] [I := I + 1] Inv

I ≤ X, Y.= I ∗ I, I < X [Y := Y + 2 ∗ I + 1; I := I + 1] Inv

Inv, B [α] Inv

Formal Methods in Software Engineering – p.36

Page 62: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

Middle branch (invariant is indeed invariant)

I′ < X, I.= I′+ 1 I ≤ X

Y′.= I′ ∗ I′, Y

.= Y′+ 2 ∗ I′+ 1, I

.= I′+ 1 Y

.= I ∗ I

I′ ≤ X, Y′.= I′ ∗ I′, I′ < X, Y

.= Y′+ 2 ∗ I′+ 1, I

.= I′+ 1 Inv

I ≤ X, Y′.= I ∗ I, I < X, Y := Y′ + 2 ∗ I + 1 [I := I + 1] Inv

I ≤ X, Y.= I ∗ I, I < X [Y := Y + 2 ∗ I + 1] [I := I + 1] Inv

I ≤ X, Y.= I ∗ I, I < X [Y := Y + 2 ∗ I + 1; I := I + 1] Inv

Inv, B [α] Inv

Formal Methods in Software Engineering – p.36

Page 63: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

Right branch(invariant and negated loop condition imply post-conditio n)

Inv ∧ ¬ B Q

Formal Methods in Software Engineering – p.37

Page 64: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

Right branch(invariant and negated loop condition imply post-conditio n)

I ≤ X, Y.= I ∗ I, ¬ (I < X) Y

.= X ∗X

Inv ∧ ¬ B Q

Formal Methods in Software Engineering – p.37

Page 65: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

Right branch(invariant and negated loop condition imply post-conditio n)

I ≤ X, Y.= I ∗ I, ¬ (I < X) I

.= X, Y

.= X ∗X

I ≤ X, Y.= I ∗ I, ¬ (I < X) Y

.= X ∗X

Inv ∧ ¬ B Q

Formal Methods in Software Engineering – p.37

Page 66: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example

Right branch(invariant and negated loop condition imply post-conditio n)

I ≤ X, Y.= I ∗ I, ¬ (I < X) I

.= X, Y

.= I ∗ I

I ≤ X, Y.= I ∗ I, ¬ (I < X) I

.= X, Y

.= X ∗X

I ≤ X, Y.= I ∗ I, ¬ (I < X) Y

.= X ∗X

Inv ∧ ¬ B Q

Formal Methods in Software Engineering – p.37

Page 67: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example II: Multiplication

X.= A, Y

.= B [αmult] Z

.= X ∗Y

Formal Methods in Software Engineering – p.38

Page 68: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example II: Multiplication

X.= A, Y

.= B [Z := 0; αwhile] Z

.= X ∗Y

X.= A, Y

.= B [αmult] Z

.= X ∗Y

Formal Methods in Software Engineering – p.38

Page 69: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example II: Multiplication

X.= A, Y

.= B, Z

.= 0 [αwhile] Z

.= X ∗Y

X.= A, Y

.= B [Z := 0; αwhile] Z

.= X ∗Y

X.= A, Y

.= B [αmult] Z

.= X ∗Y

Formal Methods in Software Engineering – p.38

Page 70: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example II: Multiplication

Invariant Inv: A ∗ B + Z.= X ∗Y

X.= A, Y

.= B, Z

.= 0 InvInv, ¬ B

.= 0 [αbody] Inv

Inv, B.= 0 Z

.= X

X.= A, Y

.= B, Z

.= 0 [αwhile] Z

.= X ∗Y

X.= A, Y

.= B [Z := 0; αwhile] Z

.= X ∗Y

X.= A, Y

.= B [αmult] Z

.= X ∗Y

Formal Methods in Software Engineering – p.38

Page 71: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example II: Multiplication

Left branch (pre-condition implies invariant)

X.= A, Y

.= B, Z

.= 0 A ∗ B + Z

.= X ∗Y

Formal Methods in Software Engineering – p.39

Page 72: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example II: Multiplication

Left branch (pre-condition implies invariant)

X.= A, Y

.= B, Z

.= 0 A ∗ B + Z

.= X ∗Y

Middle branch (invariant is indeed invariant)

A ∗ B + Z.= X ∗Y, ¬ B

.= 0 [αbody] A ∗ B + Z

.= X ∗Y

Formal Methods in Software Engineering – p.39

Page 73: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Example II: Multiplication

Left branch (pre-condition implies invariant)

X.= A, Y

.= B, Z

.= 0 A ∗ B + Z

.= X ∗Y

Middle branch (invariant is indeed invariant)

A ∗ B + Z.= X ∗Y, ¬ B

.= 0 [αbody] A ∗ B + Z

.= X ∗Y

Right branch(invariant and negated loop condition imply post-conditio n)

A ∗ B + Z.= X ∗Y, B

.= 0 Z

.= X ∗Y

Formal Methods in Software Engineering – p.39

Page 74: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Induction Rule

Purpose

Needed to prove first-order theorems on natural numbers(oracle not available in practice)

Handling loops in 〈·〉 modality

Formal Methods in Software Engineering – p.40

Page 75: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Induction Rule

Purpose

Needed to prove first-order theorems on natural numbers(oracle not available in practice)

Handling loops in 〈·〉 modality

Γ F{N← 0}, ∆ Γ, F F{N← N + 1}, ∆ Γ, ∀N F ∆

Γ ∆

N not occurring in Γ, ∆N not occurring in any program in F

Formal Methods in Software Engineering – p.40

Page 76: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Induction Rule: Example

even(2 ∗ 0), even(2 ∗ 3)

even(2 ∗N) even(2 ∗ (N + 1)), even(2 ∗ 3)

∀N (even(2 ∗N)) even(2 ∗ 3)

even(2 ∗ 3)

Formal Methods in Software Engineering – p.41

Page 77: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Loop Unwind Rule

Rule

Γ,¬ B F, ∆ Γ, B 〈α〉〈while B do α od〉F, ∆

Γ 〈while B do α od〉F, ∆

Formal Methods in Software Engineering – p.42

Page 78: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Loop Unwind Rule

Rule

Γ,¬ B F, ∆ Γ, B 〈α〉〈while B do α od〉F, ∆

Γ 〈while B do α od〉F, ∆

Note

Only useful

in connection with induction rule, or

if number of loop iterations has a (small) known upper bound

Formal Methods in Software Engineering – p.42

Page 79: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Loop Unwind Rule / Induction Rule: Example

Proof goal

〈while I > 0 do I := I− 1 od〉 I.= 0

Induction hypothesis

F(N) ≡ ∀ I (I ≤ N → 〈while I > 0 do I := I− 1 od〉 I.= 0)

Formal Methods in Software Engineering – p.43

Page 80: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Admissibility of Substitutions Revisited

Problem

Previous definition of admissibilityis not sufficient if formulas contain programs

Example

F ≡ J.= K→ [I := 0] (J

.= K) valid

Formal Methods in Software Engineering – p.44

Page 81: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Admissibility of Substitutions Revisited

Problem

Previous definition of admissibilityis not sufficient if formulas contain programs

Example

F ≡ J.= K→ [I := 0] (J

.= K) valid

F{I← J} ≡ J.= K→ [J := 0] (J

.= K) not valid

Formal Methods in Software Engineering – p.44

Page 82: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Admissibility of Substitutions Revisited

Problem

Previous definition of admissibilityis not sufficient if formulas contain programs

Example

F ≡ J.= K→ [I := 0] (J

.= K) valid

F{I← J} ≡ J.= K→ [J := 0] (J

.= K) not valid

F{J← I} ≡ I.= K→ [I := 0] (I

.= K) not valid

Formal Methods in Software Engineering – p.44

Page 83: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Admissibility of Substitutions Revisited

Problem

Previous definition of admissibilityis not sufficient if formulas contain programs

Example

F ≡ J.= K→ [I := 0] (J

.= K) valid

F{I← J} ≡ J.= K→ [J := 0] (J

.= K) not valid

F{J← I} ≡ I.= K→ [I := 0] (I

.= K) not valid

F{I← 1} ≡ J.= K→ [1 := 0] (J

.= K) not a formula

Formal Methods in Software Engineering – p.44

Page 84: Dynamic Logic - formal.iti.kit.edubeckert/teaching/Spezifikation... · Formal Methods in Software Engineering Dynamic Logic Bernhard Beckert UNIVERSITÄT KOBLENZ-LANDAU Formal Methods

Admissibility of Substitutions Revisited

Revised definition

A substitution {X← t} is admissible for a formula F iff

1. t = X, or

2. t is a variable not occurring in F, or

3. there is no variable Y in t such thata free occurrence of X in F is in the scope of

(a) a quantification ∀Y or ∃Y, or

(b) a modality containing an assignment of the form Y := s

Formal Methods in Software Engineering – p.45