# hoare-logic.pdf - Handout C2: Reasoning About Code (Hoare...

• 16
• 100% (1) 1 out of 1 people found this document helpful

This preview shows page 1 - 3 out of 16 pages.

Handout C2: Reasoning About Code (Hoare Logic), CSE 331 Spring 20122program, what must be true earlier to provide that guarantee?”You’ve surely donesome of thisnaturally. Nowyou’ll learnto do it in a more structured way with techniques to help.AssertionsLet’s startwith a simple code example:x = 17;y = 42;z = x+y;At each point before/after/in between statements, what do we know about the state of the program,specifically the values of variables?Since we’re looking at this chunk ofcodein isolation, we don’t knowanything before it executes. After the first line executes, we know thatx = 17. After the second lineexecutes, we still know thatx = 17, and we know thaty = 42too. After the third line executes, wealso know thatz = 17 + 42 = 59. We annotate the code toshow this information:{ true }x = 17;{ x = 17 }y = 42;{ x = 17 Λ y= 42 }z = x+y;{ x= 17 Λy= 42 Λz = 59 }Each logical formula shows what must be true at that point in the program. Since we don’t knowanything at the beginning, only “true” itself must betrue, so we simply write{true}.Each of the lines with curly braces is an assertion. Anassertionis a logical formula inserted at somepoint in a program. It is presumed to hold true at that point in the program. There are two specialassertions: the precondition and the postcondition. Apreconditionis an assertion inserted prior toexecution, and apostcondition

Course Hero member to access this document

Course Hero member to access this document

End of preview. Want to read all 16 pages?