Docsity

Program Meaning with Predicates & Hoare Triples: Axiomatic Semantics & Logic - Prof. Jon R, Study notes of Programming Languages

Partial preview of the text

Download Program Meaning with Predicates & Hoare Triples: Axiomatic Semantics & Logic - Prof. Jon R and more Study notes Programming Languages in PDF only on Docsity!

©^ 2007 by Spencer Rugaber

SEMANTICS

• What is the meaning of a program?• What do we mean by

meaning

anyway?

• How can we define languages in such a

way that their meanings are clear toprogrammers and compiler writers?

©^ 2007 by Spencer Rugaber

THE MEANING OF A PROGRAM

• A program takes input and uses it to

compute output

• That is, it is a mapping from a domain

consisting of all possible inputs to a rangeconsisting all possible output

• Precisely defining the mapping enables

reasoning about the program's correctness

©^ 2007 by Spencer Rugaber

SEMANTIC MECHANISMS

•^

There are three categories of semanticmechanisms for programming languages^ – Natural language, such as we saw for Algol or Ada– Interpreters, such as the Java virtual machine. These

are called

operational

definitions

  • Mathematics, such as predicate logic. We will look at

two mathematical approaches^ •

Axiomatic

: use of predicate calculus (with equality) to

express properties of system state

-^

Denotational

: adds the use of well-defined constructs such as

sets, lists, and maps to model state directly

©^ 2007 by Spencer Rugaber

AXIOMATIC SEMANTICS

•^

Floyd, Hoare

•^

Assertions expressed in the first-orderpredicate calculus

•^

Meaning of a program is the relationshipbetween its input assertions and its outputassertions

•^

Concerned mostly with statements ratherthan expressions

•^

Extended to deal with concurrent programs

©^ 2007 by Spencer Rugaber

PREDICATES

^

A predicate is an expressions whose valuesare

true

or

false

^

Predicates are also called

assertions

or

logical expressions

^

Predicates may include quantifiers such as for all

)^

or

there exists

©^ 2007 by Spencer Rugaber

AXIOMS

•^

In axiomatic semantics, the meaning of aprogramming language is given by a set ofrules, one for each kind of statement in thelanguage. These rules are called

axioms

– Because they may contain variables, axioms

should actually be called

axiom schemata

or

schemes

©^ 2007 by Spencer Rugaber

RULES OF INFERENCE

•^

To make it easier to reason about languagesand programs, axioms are augmented with thenormal laws of logic including inference rulessuch as

modus ponens

A

B and

A then

B

" is the turnstile symbol and should be read as “provable”

©^ 2007 by Spencer Rugaber

THE RULES OF CONSEQUENCE

•^

{P} S {Q} and

{Q}

{R} then

{P} S {R}

•^

{P} S {Q} and

{R}

{P} then

{R} S {Q}

That is, you can

weaken

post-conditions and

strengthen

preconditions

•^

Notice that there are three separate notations usedhere −^

S^

is a statement in a programming language

−^

P, Q

and

R

are predicates in predicate logic

−^

|−

, and,

,^ and

and

then

are symbols in this specific

axiomatic semantics

©^ 2007 by Spencer Rugaber

ASSIGNMENT STATEMENTS

•^

What is the meaning of an assignmentstatement?

•^

Must think in terms of the predicates thathold

true

before and after its execution

•^

In fact, the meaning of the assignmentstatement is exactly the effect it has onpredicates

•^

Predicates qualify the values of variables

©^ 2007 by Spencer Rugaber

ASSIGNMENT STATEMENTS - 2

•^

If an assignment statement looks like

V := E;

where

E

might be a complex expression, possibly involvingvariable

V

, then we must consider two cases: predicates

that refer to

V

and predicates that do not

•^

For predicates that do not refer to

V

, we would expect

that they would be unchanged by the assignmentstatement. That is, {P} V := E; {P}

-^

But how about predicates that refer to

V

•^

To understand these, we must introduce the concept of substitution

©^ 2007 by Spencer Rugaber

ASSIGNMENT STATEMENT - 3

•^

The meaning of the assignment statement V := E;

is given by

P

VE

V := E;

{P}

–^

P

VE

is the logical expression obtained from

P

by

substituting the expression

E

for all occurrences of

V

– P

VE is sometimes expressed as

P[V/N]

©^ 2007 by Spencer Rugaber

EXAMPLE ASSIGNMENT

•^

Consider the assignment statement

c := 5;

. If we

know before executing it that

{a > b}

is true, we

would like to be able to conclude after execution that {a > b

c = 5}

is true

•^

Formally, the reasoning for this uses the rule ofconsequence as follows– The assignment statement is

c := 5;

V

is

c E

is

P

is

{a > b

c = 5}

P

VE is

{a > b

{a > b

true}

{a > b}

©^ 2007 by Spencer Rugaber

APOSTROPHE NOTATION

•^

A minor notational enhancement can be used to makepredicates describing this kind of situation more readable^ – In the predicate that comes

after

the assignment, an apostrophe

can adorn the variable being assigned to. The apostrophe isintended to denote the value of the variable after the assignmentis complete. An unadorned occurrence of the same variableindicates the value before the assignment. The apostrophe ismerely syntactic sugar; it has no semantic significance

•^

For example, the assignment

b := b + 5

can be

denoted as

{a > b + 5} b := b + 5 {a > b’}

©^ 2007 by Spencer Rugaber

FORWARD SUBSTITUTION

•^

The semantic for assignment given above encouragesubstituting backwards. That is, the substitution takesplace in the predicate that comes before the assignmentstatement. This approach is called the

weakest

precondition semantics

-^

Notice what happens when we try to substitute in theforward direction^ {b > 7} b := b + 5; {(b+5) > 7}

⇒

{b > 2}

which is not at all what this statement means

-^

To get this one right, a little algebra must be applied