Title: Assertions and Propositional Calculus
1Assertions andPropositional Calculus
2Announcements
- No class Thursday, March 21
- Test 2 is on April 9
- Homework 3 due March 21
- All electronic, Word file on web page
- Submit via Blackboard system
- No office hours Th 3/7 and Mon 3/18
- Email if youd like to meet well set time
3Read
- Chapter 3 of Stanat and Weiss
- http//www.cs.unc.edu/weiss/COMP114/BOOK/03RulesO
fPgm.doc
4Topics
- This Class
- Program State
- Assertions
- Pre- and Post- Conditions
- Propositional Calculus
5Program Documentation
- Programs very hard to understand!
- Typical documentation in English
- Typical documentation
- // Sets i to 5
- i 5
6Formal Documentation
- Ways to make documentation precise.
- More like programs.
- Why? To make you think more formally about what
program is doing.
7Program State
- All of the information that would be necessary to
restart at same place - Program counter
- Values of variables
- Also something about I/O
- (we will typically ignore the state of I/O)
8Assertions
- An assertion is a statement that something is
true. - Precondition an assertion about state before a
program (or chunk of code). - Postcondition assertion after code
9Silly Example
- // Precondition x 6
- x
- // Postcondition x 7
- The behavior of the program is described.
- If x 6 at the beginning,
- then x 7 at the end.
10Pre and Post conditions
- Says nothing about implementation!
- Could be
- // Precondition x 6
- x 7
- // Postcondition x 7
- This description is called the specification
- Also says nothing about termination.
11Definition
- A program C is correct with respect to
precondition P and a postcondition Q if, - whenever condition P holds prior to execution of
program C, - and C terminates,
- then condition Q will (always!) hold after C has
finished execution.
12What if Precondition is False?
- Then the assertions mean nothing
- The statements are only based on the state being
what you expect on entry to the block.
13Propositional Calculus
- A language of Boolean expressions
- Values are true or false
- Weve seen this before
- if( x gt 10 y lt 5 )
14Propositions
- Can be constructed using Boolean operators
- , , , !, xor ( in Java)
- Example
- (p q) !q
15Boolean Operators
16One and Zero as True and False
17Implication
- Less common Boolean operator gt
- Read as p implies q
- q is true whenever p is true
18Combination of Arithmetic and Boolean Expressions
- Just like Java
- ( (x y) lt 7 ) ( y gt 9 )
19Weak vs. Strong Assertions
- A gt B
- A is said to be stronger than B (or B weaker than
A) - Example
- ((x gt 3) (x lt 7)) gt
- ((x gt 0) (x lt 10))
20Other Examples
If p true, true gt true false gt true If p
false, false gt false
If p true, true gt true If p false,
false gt false false gt true
21Other Examples
- How about this?
- false gt q
22No Implication Operator in Java?
- No problem
- You can make one
- How?
!p q
23Simple Assert Method
- public static boolean assert(boolean b,
- String error)
-
- if (!b)
-
- System.out.println(
- "Assertion failure error)
- System.exit(0)
-
- return true
-
24Assertion Exception Class
- public class AssertionEx extends
RuntimeException -
- AssertionEx()
- super("Assertion failed")
-
- AssertionEx(String s)
- super("Assertion failed "s)
-
-
25New Assert Class
- public class Assert
-
- public static void assert(
- boolean b,String s)
-
- if (!b)
- throw new AssertionEx(s)
-
26Now We Can Code Assertions
- Our silly example
- Assert.assert( x 6, pre x ! 6)
- x
- Assert. assert( x 7, post x ! 7)
- We can also save values of variables from
assertion to assertion.
27Quantifiers
- Still missing ability to say something about sets
of variables - How could we test that list is sorted?
- Quantifiers allow you to say
- All of the entries of array B are gt 0
- There is an element of B that is zero
28Universal Quantifier
- For all integers x, x gt 3
- We can write this as
- "x x gt 3
- Or, if you dont have cool symbols
- Ax x gt 3
- Read as
- For all x, x is greater than 3.
- Generally false, of course.
29Existential Quantifier
- There exists an integer x, such that x gt 3
- x x gt 3
- or
- Ex x gt 3
30Quantified Assertions
- In the Stanat Weiss text, they write assertions
as - (QxD(x)P(x))
- where
- Qx is the quantifier Ai or Ei
- D(x) is a domain predicate 4 lt i gt 10
- P(x) is the assertion Bi gt 0
- (Ai 4 lt i lt 10 Bi gt 0)
31Examples
- Informal
- The value 4 occurs in the array B
- Formal
- There exists a value of i between 0 and n-1,
inclusive, such that Bi 4. - Notation
- (Ei 0 lt i lt n Bi 4)
32Examples
- Informal The first element of the array B is the
largest. - Could mean
- The value of B0 is at least as large as every
entry of B. - (Ai 0 lt i lt n Bi lt B0)
- Â or
- The value of B0 is strictly larger than every
other entry of B. - (Ai 0 lt i lt n Bi lt B0)
33Examples
- Informal The array B is sorted in non-decreasing
order. - If i is less than j, then Bi is less than or
equal to Bj - (Ai Aj 0 lt i lt j lt n Bi lt Bj)
34Example
- Code segment that initializes all entries of an
array Bn to 0. - assert (true) // Precondition
-
- // (Ai 0 lt i lt n Bi 0) Post
35Summary
- Assertions as pre- and post- conditions formally
specify program behavior - If the precondition is true, then
- if the postcondition is true, the program behaves
as specified. - Some assertions can be coded in Java and checked
at run time
36Next Time
- Loop invariants
- Way to say something about whats happening in a
loop