Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative...

17
History TinyImp Hoare Logic Imperative Programming Languages Christine Rizkallah CSE, UNSW (and data61) Term 3 2019 1

Transcript of Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative...

Page 1: Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative programming is where programs are described as a series of statements or commands

History TinyImp Hoare Logic

Imperative Programming Languages

Christine RizkallahCSE, UNSW (and data61)

Term 3 2019

1

Page 2: Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative programming is where programs are described as a series of statements or commands

History TinyImp Hoare Logic

Imperative Programming

impero

Definition

Imperative programming is where programs are described as aseries of statements or commands to manipulate mutable state orcause externally observable effects.

States may take the form of a mapping from variable names totheir values, or even a model of a CPU state with a memory model(for example, in an assembly language).

2

Page 3: Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative programming is where programs are described as a series of statements or commands

History TinyImp Hoare Logic

The Old Days

Early microcomputer languages used a line numbering system withGO TO statements used to arrange control flow.

3

Page 4: Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative programming is where programs are described as a series of statements or commands

History TinyImp Hoare Logic

Factorial Example in BASIC (1964)

4

Page 5: Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative programming is where programs are described as a series of statements or commands

History TinyImp Hoare Logic

Dijkstra (1968)

The structured programming movement brought in controlstructures to mainstream use, such as conditionals and loops.

5

Page 6: Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative programming is where programs are described as a series of statements or commands

History TinyImp Hoare Logic

Factorial Example in Pascal (1970)

6

Page 7: Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative programming is where programs are described as a series of statements or commands

History TinyImp Hoare Logic

SyntaxWe’re going to specify a language TinyImp, based on structuredprogramming. The syntax consists of statements and expressions.

Grammar

Stmt ::= skip Do nothing| x := Expr Assignment| var y · Stmt Declaration| if Expr then Stmt else Stmt fi Conditional| while Expr do Stmt od Loop| Stmt ; Stmt Sequencing

Expr ::= 〈Arithmetic expressions〉

We already know how to make unambiguous abstract syntax, sowe will use concrete syntax in the rules for readability.

7

Page 8: Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative programming is where programs are described as a series of statements or commands

History TinyImp Hoare Logic

Examples

Example (Factorial and Fibonacci)

var i ·var m ·i := 0;m := 1;while i < N doi := i + 1;m := m × i

od

var m · var n · var i ·m := 1; n := 1;i := 1;while i < N do

var t · t := m;m := n;n := m + t;i := i + 1

od

8

Page 9: Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative programming is where programs are described as a series of statements or commands

History TinyImp Hoare Logic

Static SemanticsTypes? We only have one type (int), so type checking is a wash.

Scopes? We have to check that variables are declared before use.

Anything Else? We have to check that variables are initializedbefore they are used!

U ;V ` s ok W

Set of initially uninitialized variables

Set of initialized variables

Indicates that no unsafe reads occur

Set of definitely written to variables

9

Page 10: Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative programming is where programs are described as a series of statements or commands

History TinyImp Hoare Logic

Static Semantics Rules

U;V ` skip ok ∅x ∈ U ∪ V FV(e) ⊆ V

U;V ` x := e ok {x}U ∪ {y};V ` s ok W

U;V ` var y · s ok W \ {y}FV(e) ⊆ V U;V ` s1 ok W1 U;V ` s2 ok W2

U;V ` if e then s1 else s2 fi ok W1 ∩W2

FV(e) ⊆ V U;V ` s ok W

U;V ` while e do s od ok ∅U;V ` s1 ok W1 (U \W1); (V ∪W1) ` s2 ok W2

U;V ` s1; s2 ok W1 ∪W2

10

Page 11: Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative programming is where programs are described as a series of statements or commands

History TinyImp Hoare Logic

Dynamic Semantics

We will use big-step operational semantics. What are the sets ofevaluable expressions and values here?

Evaluable Expressions: A pair containing a statement to executeand a state σ.Values: The final state that results from executing the statement.

States

A state is a mutable mapping from variables to their values. Weuse the following notation:

To read a variable x from the state σ, we write σ(x).

To update an existing variable x to have value v inside thestate σ, we write (σ : x 7→ v).

To extend a state σ with a new, previously undeclared variablex , we write σ · x . In such a state, x has undefined value.

11

Page 12: Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative programming is where programs are described as a series of statements or commands

History TinyImp Hoare Logic

Evaluation RulesWe will assume we have defined a relation σ ` e ⇓ v for arithmeticexpressions, much like in the previous lecture.

(σ, skip) ⇓ σ(σ1, s1) ⇓ σ2 (σ2, s2) ⇓ σ3

(σ1, s1; s2) ⇓ σ3σ ` e ⇓ v

(σ, x := e) ⇓ (σ : x 7→ v)

(σ1 · x , s) ⇓ (σ2 · x)

(σ1, var x · s) ⇓ σ2σ1 ` e ⇓ v v 6= 0 (σ1, s1) ⇓ σ2

(σ1, if e then s1 else s2 fi) ⇓ σ2σ1 ` e ⇓ 0 (σ1, s2) ⇓ σ2

(σ1, if e then s1 else s2 fi) ⇓ σ2

σ1 ` e ⇓ 0

(σ1, while e do s od) ⇓ σ1

σ1 ` e ⇓ v v 6= 0(σ1, s) ⇓ σ2 (σ2, while e do s od) ⇓ σ3

(σ1, while e do s od) ⇓ σ312

Page 13: Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative programming is where programs are described as a series of statements or commands

History TinyImp Hoare Logic

Hoare LogicTo give you a taste of axiomatic semantics, and also how formalverification works, we are going to define what’s called a HoareLogic for TinyImp to allow us to prove properties of our program.We write a Hoare triple judgement as:

{ϕ} s {ψ}

Where ϕ and ψ are logical formulae about state variables, calledassertions, and s is a statement. This triple states that if thestatement s successfully evaluates from a starting state satisfyingthe precondition ϕ, then the result state will satisfy thepostcondition ψ:

ϕ(σ) ∧ (σ, s) ⇓ σ′ ⇒ ψ(σ′)

13

Page 14: Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative programming is where programs are described as a series of statements or commands

History TinyImp Hoare Logic

Proving Hoare Triples

To prove a Hoare triple like:

{True}var i · var m ·i := 0;m := 1;while i < N do

i := i + 1;m := m × i

od{m = N!}

It is undesirable to look at the operational semantics derivations ofthis whole program to compute what the possible final states arefor a given input state. Instead we shall define a set of rules toprove Hoare triples directly (called a proof calculus).

14

Page 15: Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative programming is where programs are described as a series of statements or commands

History TinyImp Hoare Logic

Hoare Rules

(σ, skip) ⇓ σ {ϕ} skip {ϕ}

(σ1, s1) ⇓ σ2 (σ2, s2) ⇓ σ3(σ1, s1; s2) ⇓ σ3

{ϕ} s1 {α} {α} s2 {ψ}{ϕ} s1; s2 {ψ}

σ ` e ⇓ v

(σ, x := e) ⇓ (σ : x 7→ v) {ϕ[x := e]} x := e {ϕ}

Continuing on, we can get rules for if, and while with a loopinvariant:

{ϕ ∧ e} s1 {ψ} {ϕ ∧ ¬e} s2 {ψ}{ϕ} if e then s1 else s2 fi {ψ}

{ϕ ∧ e} s {ϕ}{ϕ} while e do s od {ϕ ∧ ¬e}

15

Page 16: Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative programming is where programs are described as a series of statements or commands

History TinyImp Hoare Logic

Consequence

There is one more rule, called the rule of consequence, that weneed to insert ordinary logical reasoning into our Hoare logicproofs:

ϕ⇒ α {α} s {β} β ⇒ ψ

{ϕ} s {ψ}

This is the only rule that is not directed entirely by syntax. Thismeans a Hoare logic proof need not look like a derivation tree.Instead we can sprinkle assertions through our program andspecially note uses of the consequence rule.

16

Page 17: Imperative Programming Languagescs3161/19t3/Week 05/2Fri/Slides... · 2020. 9. 6. · Imperative programming is where programs are described as a series of statements or commands

History TinyImp Hoare Logic

Factorial Example

Let’s verify the Factorial program using our Hoare rules:

{True}var i · var m ·{1 = 0!} i := 0; {1 = i !}{1 = i !}m := 1; {m = i !}{m = i !}while i < N do {m = i ! ∧ i < N}{m × (i + 1) = (i + 1)!}i := i + 1;{m × i = i !}m := m × i{m = i !}

od {m = i ! ∧ i = N}{m = N!}

{ϕ ∧ e} s1 {ψ} {ϕ ∧ ¬e} s2 {ψ}{ϕ} if e then s1 else s2 fi {ψ}

{ϕ[x := e]} x := e {ϕ}

{ϕ ∧ e} s {ϕ}{ϕ} while e do s od {ϕ ∧ ¬e}

{ϕ} s1 {α} {α} s2 {ψ}{ϕ} s1; s2 {ψ}

ϕ⇒ α {α} s {β} β ⇒ ψ

{ϕ} s {ψ}

note: (i + 1)! = i!× (i + 1)

17