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
scontainsx, then update its value to bev - If
sdoes not containx, then extend it by adding the entry associatingvtox
The statements have the following semantics:
- Assignment. If in state
sthe expressioneevaluates to valuev, then the assignment statementx := eupdates the state tos[x := v]. - Sequence. If in state
sthe statementc1transforms the state tos', then the sequence of statementsc1; c2becomes statmentc2evaluated in states'. - If true. Suppose, in state
s, the Boolean expressionbevaluates toTrue, and that the statementc1transforms statesinto states'. Then in states, the statementIf b then c1 else c2transforms the statesinto states'(i.e., it amounts to executing the “true” branch). - If false. Suppose, in state
s, the Boolean expressionbevaluates toFalse, and that the statementc2transforms statesinto states'. Then in states, the statementIf b then c1 else c2transforms the statesinto states'(i.e., it amounts to executing the “false” branch). - While false. Suppose, in state
s, the Boolean expressionbevaluates toFalse. Then in statesexecuting the statementwhile b do cdoes 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 expressionbevaluates toTrue; and suppose statementctransforms statesintos'. We also assume that, when executing the loopwhile b do cin states'that it will terminate in states''. Then in statesexecuting the statementwhile b do ctransforms 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