Predicate logic assertions about program state, specifically focusing on the values of variables at various program points and statement-specific semantic rules such as assignment, sequence, conditional, and iteration. It covers program specification in terms of preconditions and postconditions, proof of program matching its specification, and the semantics of different types of statements like assignments, blocks, conditionals, and iteration statements.
Download Predicate Logic and Program State Assertions in Programming Language Semantics and more Assignments Computer Science in PDF only on Docsity!
Program Verification
Deductive argument that a program matches its specification - Complementary to testing and inspections
Predicate logic assertions about program state - Values of variables at various program points
Statement-specific semantic rules
Assignment; sequence; conditional; iteration
Program Specification
The preconditions plus the postconditions together specify what it is that the program computes - Expressed in terms of only input and output variables - Avoids implementation details - Assumes termination
Statements
The proof of a program naturally depends on the program's statements - Programming language semantics
We will consider four main types of statements
Assignments
Blocks
Conditionals
Iteration statements
We will also briefly mention subprograms
Assignment Statement
In program proofs, an assignment statement looks like the following - { P } V ← E; { Q } - Curly brackets delimit assertions - V is the variable being assigned; E is an expression that when evaluated will produce the value assigned to V; { P } is an assertion describing the state that exists before execution of the assignment; and { Q } is an assertion describing the state afterwards
In general, we would expect most of the state to be unaffected; only the part that describes V should change
To describe the semantics of the assignment completely, we need to express the relationship between { P } and { Q }
Assignment Statement
Case 2: Left hand side variable appears on right hand side - The state before the assignment has a direct contribution to the state after - Solve for right hand side variable (the value before the assignment) - Substitute solution into pre assertion to get post assertion - Use apostrophe to denote value after assignment - This is just to distinguish the occurrences of the variable. It can be dropped once the right hand side predicate is determined
Assignment Example
• { P ∧ Q(X) } X ← X + Y {? }
Assignment Statement
• { P ∧ Q(X) } X ← X + Y {? }
Use an apostrophe to distinguish the two occurrences of X
X' ← X + Y
Assignment Statement
• { P ∧ Q(X) } X ← X + Y {? }
Use an apostrophe to distinguish the two occurrences of X
X' ← X + Y
Solve for X, the value from the state existing before the assignment is executed
Assignment Statement
• { P ∧ Q(X) } X ← X + Y {? }
• X' ← X + Y
• X = X' - Y
Plug into the pre assertion to get the post assertion
Assignment Statement
• { P ∧ Q(X) } X ← X + Y {? }
• X' ← X + Y
• X = X' - Y
Plug into pre assertion to get post assertion
{ P ∧ Q(X) } X ← X + Y { P ∧ Q(X' - Y) }
Assignment Statement
• { P ∧ Q(X) } X ← X + Y {? }
• X' ← X + Y
• X = X' - Y
Plug into pre assertion to get post assertion
{ P ∧ Q(X) } X ← X + Y { P ∧ Q(X' - Y) }
Drop the apostrophe
{ P ∧ Q(X) } X ← X + Y { P ∧ Q(X - Y) }
Assignment Example
{ Z = 3 ∧ X > Z) } X ← X + 4 {? }
Assignments - General Rule
{ P E v } V ← E { P }
P E v is the logical expression obtained from P by substituting the expression E for all occurrences of V
Actually, only free occurrences are substituted. Thus renaming of bound variables may be required
Example: { a > b ∧ 5 = 5 } c := 5 { a > b ∧ c = 5 } ⇒ { a > b ∧ true } ⇒ { a > b }
Another example: { a > b + 5 } b := b + 5 { a > b }
Sequential Composition
Consecutive statements can be treated as a single SESE if their assertions can be logically composed - The essence is to find a single, general predicate that describes the post assertion of the first statement and the pre assertion of the second - Here is the general rule { P } S1 { Q1 } and { Q1 } S2 { R } then { P } S1; S2 { R }
Example {x = 3 ∧ y = 2} z := x + 2 {z = 5 ∧ y = 2 ∧ x = 3} ⇒ {z = 5 ∧ y = 2} and {z = 5 ∧ y = 2} w := z + y {z = 5 ∧ y = 2 ∧ w = 7} ⇒ {z = 5 ∧ w = 7} then {x = 3 ∧ y = 2} z:= x + 2; w := z + y {z = 5 ∧ w = 7}