Docsity

Understanding Hoare Logic and Its Application to Program Verification - Prof. Jose Mesegue, Study notes of Computer Science

Partial preview of the text

Download Understanding Hoare Logic and Its Application to Program Verification - Prof. Jose Mesegue and more Study notes Computer Science in PDF only on Docsity!

Program Verification:

Lecture 16

Jos´

e Meseguer

Computer Science Department University of Illinois at Urbana-Champaign

An Interpreter

The above functional module has given a preciseaxiomatization of our chosen subset of Java that issufficient for reasoning and program verification purposes.But it has done more than that. Since the semanticequations are ground confluent, the above equations havealso given an

operational semantics

to this language.

Indeed, we can

describe the execution of the language

by

algebraic simplification

with the equations from left to right.

Therefore, the above equations have in essence given us an interpreter

for our language.

An Interpreter (III)

Some examples are: red (initial | (int ’x = #i(5) ; int ’y = #i(4) ;

{’x = #i(42) ; } ’x = ’x + ’y ;))[’x].

red (initial | (int ’x = #i(5) ; int ’y = #i(4) ;

{int ’x = #i(42) ; } ’x = ’x + ’y ;))[’x]

.

which return rewrites: 86 in 10ms cpu (10ms real) (8600 rewrites/second)result Value: int(46) respectively, because of shadowing of the assignment to

’x

in the block, rewrites: 86 in 0ms cpu (0ms real) (~ rewrites/second)result Value: int(9)

An Interpreter (IV)

red (initial | (int ’x = #i(0) ; if #b(false)

if #b(true) ’x = #i(1) ;else ’x = #i(2) ;))[’x]

.

This is interesting because it shows us how the danglingelse problem is solved in this particular implementation.According to the Java Language Specification an else blockbelongs to the innermost if part, unless parentheses showotherwise. The result in this case is: rewrites: 18 in 0ms cpu (0ms real) (~ rewrites/second)result Value: int(0) It would have been

int(2)

if the else was attributed

(wrongly) to the outer if.

An Interpreter (VI)

red (initial | (int ’x = #i(7) ; int ’y = #i(5) ;

int ’t = ’x ; ’x = ’y ; ’y = ’t ;))[’x].

red (initial | (int ’x = #i(7) ; int ’y = #i(5) ;

int ’t = ’x ; ’x = ’y ; ’y = ’t ;))[’y].

A simple swap example indeed swaps the values of

’x

and

’y, the results are

5 , respectively

7 , as expected.

red (initial | (int ’n = #i(5) ; int ’c = #i(0) ; int ’x = #i(1) ;

while (’c < ’n) { ’c = ’c + #i(1) ; ’x = ’x * ’c ; })) [’x].

Finally this computes the factorial of

and thus returns:

rewrites: 416 in 0ms cpu (0ms real) (~ rewrites/second)result Value: int(120)

Formal Reasoning

In fact, the equational axiomatization of our language

is

much more than an interpreter

Since we have axiomatized the language as an

equational

theory

, we can do

inference

about our programs using such

a theory.In fact, since the semantic equations are ground-confluent,such equational reasoning is mechanically supported by theMaude system. Therefore, we can do a substantial amountof^ formal reasoning

about programs in our language,

even

without using induction

Formal Reasoning (III)

Actually we cannot do that for any state but only for statesin which

’X

and

’Y

are already declared. Such a state is

created by

ctxState((int ’X ; ’int ’Y ;))

. The fact that

this represents any state with

’X

and

’Y

declared results

from the generic values put into both variable’s locations.Whether other variables are declared or not does notmatter, as long as a candidate for the swap program doesnot use other variables without declaring them first. This isverified with a quick look at the semantics.

Formal Reasoning (IV)

This

correctness specification

can be made mathematically

precise as the equations: (ctxState((int ’X ; int ’Y ;)) | (swap))[’X]

=^ (ctxState((int ’X ; int ’Y ;)))[’Y

(ctxState((int ’X ; int ’Y ;)) | (swap))[’Y]

=^ (ctxState((int ’X ; int ’Y ;)))[’X

Consider now a possible candidate for our

swap

program,

namely the program,^ int ’T = ’X ; ’X = ’Y ; ’Y = ’T ;

Preconditions

Recall the specification of correctness for the program

swap

to swap the value of variable

’X

and variable

’Y

that we

have already discussed. With

S^

taking the place of

ctxState((int ’X ; int ’Y ;))

, but with the universal

quantification implicitly limited to states of that form weget the specification,

(∀I^

:^ Int

)(∀J

:^ Int

)(∀S

:^ State

)(S)[’Y]

=^ int(I)

∧^ (S)[’X]

=^ int(J)

⇒^ (S | (swap))[’X]

=^ int(I)

∧^ (S | (swap))[’Y]

=^ int(J)

Here we have the implicit precondition that we are startingin a state where

’X

and

’Y

are declared as described above.

Preconditions (II)

We shall call the equation

(S)[’Y]

=^ int(I)

∧^ (S)[’X]

=^ int(J)

which is assumed to hold

before

the execution of the

program, the

precondition

Note that this equation has a

single occurrence

of the state

variable

S^ in each equation, and can be thought of as a

state predicate

, having the integer variables

I^ and

J^ as

parameters

State Predicates

This example suggests a general notion of “statepredicate,” intuitively a

property

that holds or doesn’t hold

of a state, perhaps relative to some extra

data parameters

Since in our language the only data are integers, suchparameters must be integer variables.Therefore, for our language we can define a

state predicate

as a conjunction of equations

t=^1

′ t 1

∧^...

∧^ t

=n

′ t n

in a module extending

JAVAX

, such that the set

V^

of

variables in all terms in the equations has

at most one

variable

S^ of sort

State

, which possibly appears more than

once, and the remaining variables are all of sort

Int

16

State Predicates (II)

How general is this definition of state predicate?One can of course generalize things further, by allowing an arbitrary first-order formula

(with the same condition on its

variables

V^ ) instead of just a conjuction of equations. Also,

the notion extends naturally to other sequential languageswhich may have

other data structures

besides integers.

However, in practice the notion is quite general; amongother things because, using an equationally defined equalitypredicate, we can express

arbitrary Boolean combinations

of

equations as a

single equation

17

Hoare Triples

The above example of our specification for

swap

is

paradigmatic of a general way of specifying properties of a sequential imperative program

p^ by means of a

Hoare triple

(after C.A.R. Hoare)

{A}

p^ {

B}

where

A^

and

B^

are state predicates, called, respectively, the

precondition

, and

postcondition

of the triple.

In this notation, the specification of

swap

becomes

rephrased as,

{(S)[’Y]

=^ int(I)

∧^ (S)[’X]

=^ int(J)

swap

{(S)[’X]

=^ int(I)

∧^ (S)[’Y]

=^ int(J)

Hoare Triples (II)

Given our algebraic approach to the semantics of imperativeprograms, this is all just an (indeed very useful)

fa¸con de

parler

about an

ordinary first-order property

satisfied by the

initial model of our language, namely the initial algebra TJAVAX

Therefore, we define the

partial correctness

of a program

p

with respect to a Hoare triple by the equivalence,

TJAVAX

|=^

{A}

p^ {

B}^

TJAVAX

∀V^ )

A^ ∧

((S | p) :

WrappedState

)^ ⇒

(B

(S/

S | p

Note that

p^ terminates iff

(S | p) :

WrappedState

20