






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!























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
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
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
and
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
and
are already declared. Such a state is
created by
. The fact that
this represents any state with
and
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
program,
Preconditions
Recall the specification of correctness for the program
to swap the value of variable
and variable
that we
have already discussed. With
taking the place of
, 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
and
are declared as described above.
Preconditions (II)
We shall call the equation
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
state predicate
, having the integer variables
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
in a module extending
, such that the set
of
variables in all terms in the equations has
at most one
variable
, which possibly appears more than
once, and the remaining variables are all of sort
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
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
is
paradigmatic of a general way of specifying properties of a sequential imperative program
Hoare triple
(after C.A.R. Hoare)
where
and
are state predicates, called, respectively, the
precondition
, and
postcondition
of the triple.
In this notation, the specification of
becomes
rephrased as,
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
Therefore, we define the
partial correctness
of a program
with respect to a Hoare triple by the equivalence,
Note that
20