Imperative Language - Hoare Logic
Hoare logic works with an Algol-like language (later texts used Pascal-like syntax). I’m going to make explicit the syntax and semantics of the language in this note, so I won’t repeat myself over and over. As a compromise, I’m going to use Free Pascal’s syntax for statements and expressions.
For simplicity, we will focus on integer and Boolean expressions. In EBNF, the grammar would look like:
expression = int-expr | bool-expr ;
int-expr = integer
| "(" int-expr ")"
| int-expr int-bin-op int-expr
| "-" int-expr ;
int-bin-op = "+" | "-" | "*" | "/"
| "=" | "<>" | "<" | "<=" | ">" | ">=" ;
integer = ["-"] NONZERO-DIGIT {DIGIT} | "0" ;
NONZERO-DIGIT = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
DIGIT = "0" | NONZERO-DIGIT ;
bool-expr = "True" | "False"
| "(" bool-expr ")"
| "Not" bool-expr
| bool-expr bool-bin-op bool-expr ;
bool-bin-op = "and" | "or" | "xor" | "=" | "<>" ;
The statements are:
statement = assignment | if-else | while-do | compound-statement ;
assignment = identifier ":=" expression ;
if-else = "If" bool-expr "then" statement "else" statement ;
while-do = "While" bool-expr "do" statement ;
compound-statement = "begin" statement ";" {statement ";"} "end" ;
The semantics are, well, defined by Hoare logic itself.
Operational Semantics
If we consider an associative array from identifiers (variables) to
their current values (i.e., we can specify the current state), then we
may define the operational semantics for our language. We will write
s[x:=v]
for extending the state s
by the following steps:
- If
s
containsx
, then update its value to bev
- If
s
does not containx
, then extend it by adding the entry associatingv
tox
The statements have the following semantics:
- Assignment. If in state
s
the expressione
evaluates to valuev
, then the assignment statementx := e
updates the state tos[x := v]
. - Sequence. If in state
s
the statementc1
transforms the state tos'
, then the sequence of statementsc1; c2
becomes statmentc2
evaluated in states'
. - If true. Suppose, in state
s
, the Boolean expressionb
evaluates toTrue
, and that the statementc1
transforms states
into states'
. Then in states
, the statementIf b then c1 else c2
transforms the states
into states'
(i.e., it amounts to executing the “true” branch). - If false. Suppose, in state
s
, the Boolean expressionb
evaluates toFalse
, and that the statementc2
transforms states
into states'
. Then in states
, the statementIf b then c1 else c2
transforms the states
into states'
(i.e., it amounts to executing the “false” branch). - While false. Suppose, in state
s
, the Boolean expressionb
evaluates toFalse
. Then in states
executing the statementwhile b do c
does not alter the state of the computer (i.e., the body of the loop is not executed, there is no iteration). - While true. Suppose, in state
s
, the Boolean expressionb
evaluates toTrue
; and suppose statementc
transforms states
intos'
. We also assume that, when executing the loopwhile b do c
in states'
that it will terminate in states''
. Then in states
executing the statementwhile b do c
transforms the computer to be in states''
.
References
- Kasper Svendesn, “Hoare Logic and Model Checking”. Cambridge University Slides 2016, PDF
- Abhishek Kr Singh, Raja Natrajan, “An Outline of Separation Logic”. arXiv:1703.10994