hoare logic and model checking semantics of hoare logic
play

Hoare Logic and Model Checking Semantics of Hoare Logic Kasper - PowerPoint PPT Presentation

Hoare Logic and Model Checking Semantics of Hoare Logic Kasper Svendsen University of Cambridge CST Part II 2016/17 Acknowledgement: slides heavily based on previous versions by Mike Gordon and Alan Mycroft Semantics of Hoare Logic


  1. Hoare Logic and Model Checking Semantics of Hoare Logic Kasper Svendsen University of Cambridge CST Part II – 2016/17 Acknowledgement: slides heavily based on previous versions by Mike Gordon and Alan Mycroft Semantics of Hoare Logic Recall, to define a Hoare Logic we need three main components: • the programming language that we want to reason about, along with its operational semantics • an assertion language for defining state predicates, Operational semantics for WHILE along with a semantics • a formal interpretation of Hoare triples, together with a (sound) formal proof system for deriving Hoare triples This lecture will define defines a formal semantics of Hoare Logic and introduces some meta-theoretic results about Hoare Logic (soundness & completeness). 1

  2. Operational semantics of WHILE Operational semantics of WHILE The operational semantics of WHILE will be defined as a Stores are functions from variables to integers: transition system that consists of def Store = Var ! Z • a set of stores, stores , and These are total functions and define the current value of every program and auxiliary variable. • a reduction relation, + 2 P ( Cmd ⇥ Store ⇥ Store ). This models WHILE with arbitrary precision integer arithmetic. A The reduction relation, written h C , s i + s 0 , expresses that the command C reduces to the terminal state s 0 when executed from more realistic model might use 32-bit integers are require reasoning about overflow, etc. initial state s . 2 3 Operational semantics of WHILE Semantics of expressions E [ [ E ] ]( s ) evaluates expression E to an integer in store s : The reduction relation is defined inductively by a set of rules. [ � ] ](=) : Exp ⇥ Store ! Z E [ To reduce an assignment we first evaluate the expression E using E [ [ N ] ]( s ) = N the current store and update the store with the value of E . E [ [ V ] ]( s ) = s ( V ) E [ [ E ] ]( s ) = n E [ [ E 1 + E 2 ] ]( s ) = E [ [ E 1 ] ]( s ) + E [ [ E 2 ] ]( s ) h X := E , s i + s [ X 7! n ] . . . We use functions E [ [ E ] ]( s ) and B [ [ B ] ]( s ) to evaluate expressions This semantics is too simple to handle operations such as division, and boolean expressions in a given store s . which fails to evaluate to an integer on some inputs. 4 5

  3. Semantics of boolean expressions Operational semantics of WHILE h C 1 , s i + s 0 h C 2 , s 0 i + s 00 E [ [ E ] ]( s ) = n B [ [ B ] ]( s ) evaluates boolean expression B to an boolean in store s : h X := E , s i + s [ X 7! n ] h C 1 ; C 2 , s i + s 00 B [ [ � ] ](=) : BExp ⇥ Store ! B ]( s ) = > h C 1 , s i + s 0 ]( s ) = ? h C 2 , s i + s 0 B [ [ B ] B [ [ B ] E [ [ T ] ]( s ) = > h if B then C 1 else C 2 , s i + s 0 h if B then C 1 else C 2 , s i + s 0 E [ [ F ] ]( s ) = ? 8 > if E [ [ E 1 ] ]( s )  E [ [ E 2 ] ]( s ) h C , s i + s 0 h while B do C , s 0 i + s 00 B [ [ B ] ]( s ) = > < E [ [ E 1  E 2 ] ]( s ) = ? otherwise h while B do C , s i + s 00 : . . . B [ [ B ] ]( s ) = ? h while B do C , s i + s h skip , s i + s 6 7 Meta-theory Meta-theory We will need the following expression substitution property Note that the operational semantics of WHILE is deterministic: later to prove soundness of the Hoare assignment axiom: h C , s i + s 0 ^ h C , s i + s 00 ) s 0 = s 00 ]( s [ V 7! E [ E [ [ E 1 [ E 2 / V ]] ]( s ) = E [ [ E 1 ] [ E 2 ] ]( s )]) We have already implicitly used this in the definition of total correctness triples. The expression substitution property follows by induction on E 1 . Without this property, we would have to specify whether all Case E 1 ⌘ N : reductions or just some reductions were required to terminate. E [ [ N [ E 2 / V ]] ]( s ) = N = E [ [ N ] ]( s [ V 7! E [ [ E 2 ] ]( s )]) 8 9

  4. Meta-theory Meta-theory E [ [ E 1 [ E 2 / V ]] ]( s ) = E [ [ E 1 ] ]( s [ V 7! E [ [ E 2 ] ]( s )]) E [ [ E 1 [ E 2 / V ]] ]( s ) = E [ [ E 1 ] ]( s [ V 7! E [ [ E 2 ] ]( s )]) Case E 1 ⌘ E a + E b : Case E 1 ⌘ V 0 : E [ [( E a + E b )[ E 2 / V ]] ]( s ) 8 E [ [ E 2 ] ]( s ) if V = V 0 < [ V 0 [ E 2 / V ]] E [ ]( s ) = = E [ [ E a [ E 2 / V ]] ]( s ) + E [ [ E b [ E 2 / V ]] ]( s ) s ( V 0 ) if V 6 = V 0 : = E [ [ E a ] ]( s [ V 7! E [ [ E 2 ] ]( s )]) + E [ [ E b ] ]( s [ V 7! E [ [ E 2 ] ]( s )]) [ V 0 ] = E [ ]( s [ V 7! E [ [ E 2 ] ]( s )]) ]( s [ V 7! E [ = E [ [ E a + E b ]] [ E 2 ] ]( s )]) 10 11 The language of assertions Now we have formally defined the semantics of the WHILE language that we wish to reason about. The next step is to formalise the assertion language that we will Semantics of assertions use to reason about states of WHILE programs. We take the language of assertions to be an instance of (single-sorted) first-order logic with equality. Knowledge of first-order logic is assumed. We will review some basic concepts now. 12

  5. Review of first-order logic Review of first-order logic: Terms Terms may contain variables like x, X, y, X, z, Z etc. Recall that in first-order logic there are two syntactic classes: Terms, like 1 and 4 + 5, that do not contain any free variables are called ground terms. • Terms: which denote values (e.g., numbers) • Assertions: describe properties that may be true or false We use conventional notation, e.g. here are some terms: Assertions are built out of terms, predicates and logical connectives X , y , Z , ( ^ , _ , etc.). 1 , 2 , 325 , Since we are reasoning about WHILE states, our assertions will � X , � ( X + 1) , ( x · y ) + Z , describe properties of WHILE states. q (1 + x 2 ) , X ! , sin ( x ) , rem ( X , Y ) 13 14 Review of first-order logic: Atomic assertions Review of first-order logic: Atomic assertions Examples of atomic assertions are: ? , > , X = 1 , R < Y , X = R + ( Y · Q ) In general, first-order logic is parameterised over a signature that > and ? are atomic assertions that are always true and false. defines non-logical function symbols (+ , � , · , ...) and predicate symbols ( ODD , PRIME , etc.). Other atomic assertions are built from terms using predicates, e.g. ( X + 1) 2 � x 2 We will be using a particular instance with a signature that ODD ( X ) , PRIME (3) , X = 1 , includes the usual functions and predicates on integers. Here ODD , PRIME , and � are examples of predicates ( � is written using infix notation) and X , 1, 3, X + 1, ( X + 1) 2 and x 2 are terms in above atomic assertions. 15 16

  6. Review of first-order logic: Compound assertions The assertion language The formal syntax of the assertion language is given below. Compound assertions are built up from atomic assertions using the P , Q ::= ? | > | B | P ^ Q | P _ Q | P ) Q assertions usual logical connectives: 8 x . P | 9 x . P | t 1 = t 2 | p ( t 1 , ..., t n ) | ^ ( conjunction ) , _ ( disjunction ) , ) ( implication ) t ::= E | f ( t 1 , ..., t n ) terms and quantification: Note that assertions quantify over logical variables. 8 ( universal ) , 9 ( existential ) Here p and f range over an unspecified set of predicates and Negation, ¬ P , is a shorthand for P ) ? . functions, respectively, that includes the usual mathematical operations on integers. 17 18 Semantics of terms Semantics of assertions [ [ P ] ] defines the set of stores that satisfy the assertion P . [ [ t ] ] defines the meaning of a term t . [ [ � ] ] : Assertion ! P ( Store ) [ [ ? ] ] = ; [ [ � ] ](=) : Term ⇥ Store ! Z [ [ > ] ] = Store def [ [ E ] ]( s ) = E [ [ E ] ]( s ) [ [ B ] ] = { s | B [ [ B ] ]( s ) = > } def [ [ f ( t 1 , ..., t n )] ]( s ) = [ [ f ] ]([ [ t 1 ] ]( s ) , ..., [ [ t n ] ]( s )) [ P _ Q ] ] [ [ [ ] = [ [ P ] [ Q ] ] [ [ P ^ Q ] ] = [ [ P ] ] \ [ [ Q ] ] We assume [ [ f ] ] is given by the implicit signature. [ [ P ) Q ] ] = { s | s 2 [ [ P ] ] ) s 2 [ [ Q ] ] } 19 20

  7. Semantics of assertions (continued) Substitutions [ [ 8 x . P ] ] = { s | 8 v . s [ x 7! v ] 2 [ [ P ] ] } [ 9 x . P ] ] = { s | 9 v . s [ x 7! v ] 2 [ [ [ P ] ] } [ [ t 1 = t 2 ] ] = { s | [ [ t 1 ] ]( s ) = [ [ t 2 ] ]( s ) } We use t [ E / V ] and P [ E / V ] to denote t and P with E substituted for every occurrence of program variable V , respectively. [ [ p ( t 1 , ..., t n )] ] = { s | [ [ p ] ]([ [ t 1 ] ]( s ) , ..., [ [ t 2 ] ]( s )) } Since our quantifiers bind logical variables and all free variables in We assume [ [ p ] ] is given by the implicit signature. E are program variables, there is no issue with variable capture. This interpretation is related to the forcing relation you used in ”Proof and Logic”: s 2 [ [ P ] ] , s | = P 21 22 Substitution property The term and assertion semantics satisfy a similar substitution property to the expression semantics: Semantics of Hoare Logic • [ [ t [ E / V ]] ]( s ) = [ [ t ] ]( s [ V 7! E [ [ E ] ]( s )]) • s 2 [ ] , s [ V 7! E [ ]( s )] 2 [ [ P [ E / V ]] [ E ] [ P ] ] They are easily provable by induction on t and P , respectively. (Exercise) 23

Recommend


More recommend