AxiomaticSemantics

AxiomaticSemantics - COP4020 Programming Languages...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP4020 Spring 2011 2 6/16/11 Assertions and Preconditions ! Assertions are used by programmers to verify run-time execution " An assertion is a logical formula to verify the state of variables and data to ensure safe continuation " A failed assertion should stop the program " Assertions are placed by programmers explicitly in code assert (len>0); mean = sum/len; ! Preconditions state the necessary conditions for “safe” execution " Programmer decides which conditions apply at a program point " Testing too few does not make the program safe
Background image of page 2
COP4020 Spring 2011 3 6/16/11 Axiomatic Semantics and Preconditions ! What if we want to guarantee that the program always produces the correct output assuming that the initial assertion passes? ! Axiomatic semantics: axiomatic proof that a program produces a machine state described by a postcondition if the precondition on the initial state holds (initial assertion passes) ! Example: assert (len > 0); // given this passes mean = sum/len; assert (mean > 0); // is this always true? answer: no, cannot be proven can be proven when we change precondition to (len>0 && sum>0)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP4020 Spring 2011 4 6/16/11 Preconditions, Postconditions and Partial Correctness ! Triple notation : place conditions before and after a command C : { Precondition } C { Postcondition } ! We say that C is partially correct with respect to the < precondition , postcondition > specification, provided that " The command C is executed in a machine state that makes the precondition true " If the command terminates, then we guarantee that the resulting machine state makes the postcondition true ! Total correctness requires termination
Background image of page 4
COP4020 Spring 2011 5 6/16/11 Assignment Axiom ! If we view conditions in assertions as logical predicates, the assignment axiom can be stated { P ( E ) } V := E { P ( V ) } that is, if we state a property of V after the assignment, then the property must hold for expression E before the assignment ! We can use substitution to derive the precondition given a postcondition formula P : this is the assignment axiom : { P [ V ! E ] } V := E { P } where P [ V ! E ] denotes the substitution of V by E in P
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 6/16/11 Examples for Assignments ! { k = 5 } k := k + 1 { k = 6 } ( k = 6)[ k ! k +1] " ( k +1 = 6) " ( k = 5) ! { j = 3 and k = 4 } j := j + k { j = 7 and k = 4 } ( j = 7 and k = 4)[ j ! j + k ] " ( j + k = 7 and k = 4) " ( j = 3 and k = 4) ! { true } x := 2 { x = 2 } ( x = 2)[ x ! 2] " (2 = 2) " (true) ! { a > 0 } a := a - 1 { a > 0 } ( a > 0)[ a ! a - 1] " ( a - 1 > 0) " ( a > 0) Assuming a is int ! !
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 26

AxiomaticSemantics - COP4020 Programming Languages...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online