Assertions and Propositional Calculus - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Assertions and Propositional Calculus

Description:

Chapter 3 of Stanat and Weiss. http://www.cs.unc.edu/~weiss/COMP114/BOOK/03RulesOfPgm.doc ... How could we test that list is sorted? Quantifiers allow you to say ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 37
Provided by: anselmo9
Category:

less

Transcript and Presenter's Notes

Title: Assertions and Propositional Calculus


1
Assertions andPropositional Calculus
  • COMP 114
  • Tuesday March 5

2
Announcements
  • 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

3
Read
  • Chapter 3 of Stanat and Weiss
  • http//www.cs.unc.edu/weiss/COMP114/BOOK/03RulesO
    fPgm.doc

4
Topics
  • Last Time
  • Complexity
  • This Class
  • Program State
  • Assertions
  • Pre- and Post- Conditions
  • Propositional Calculus

5
Program Documentation
  • Programs very hard to understand!
  • Typical documentation in English
  • Typical documentation
  • // Sets i to 5
  • i 5

6
Formal Documentation
  • Ways to make documentation precise.
  • More like programs.
  • Why? To make you think more formally about what
    program is doing.

7
Program 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)

8
Assertions
  • 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

9
Silly 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.

10
Pre 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.

11
Definition
  • 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.

12
What 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.

13
Propositional Calculus
  • A language of Boolean expressions
  • Values are true or false
  • Weve seen this before
  • if( x gt 10 y lt 5 )

14
Propositions
  • Can be constructed using Boolean operators
  • , , , !, xor ( in Java)
  • Example
  • (p q) !q

15
Boolean Operators
16
One and Zero as True and False
17
Implication
  • Less common Boolean operator gt
  • Read as p implies q
  • q is true whenever p is true

18
Combination of Arithmetic and Boolean Expressions
  • Just like Java
  • ( (x y) lt 7 ) ( y gt 9 )

19
Weak 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))

20
Other Examples
  • (p r ) gt p

If p true, true gt true false gt true If p
false, false gt false
  • p gt (p r)

If p true, true gt true If p false,
false gt false false gt true
21
Other Examples
  • Is this true?
  • p gt true
  • How about this?
  • false gt q

22
No Implication Operator in Java?
  • No problem
  • You can make one
  • How?

!p q
23
Simple Assert Method
  • public static boolean assert(boolean b,
  • String error)
  • if (!b)
  • System.out.println(
  • "Assertion failure error)
  • System.exit(0)
  • return true

24
Assertion Exception Class
  • public class AssertionEx extends
    RuntimeException
  • AssertionEx()
  • super("Assertion failed")
  • AssertionEx(String s)
  • super("Assertion failed "s)

25
New Assert Class
  • public class Assert
  • public static void assert(
  • boolean b,String s)
  • if (!b)
  • throw new AssertionEx(s)

26
Now 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.

27
Quantifiers
  • 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

28
Universal 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.

29
Existential Quantifier
  • There exists an integer x, such that x gt 3
  • x x gt 3
  • or
  • Ex x gt 3

30
Quantified 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)

31
Examples
  • 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)

32
Examples
  • 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)

33
Examples
  • 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)

34
Example
  • Code segment that initializes all entries of an
    array Bn to 0.
  • assert (true) // Precondition
  • // (Ai 0 lt i lt n Bi 0) Post

35
Summary
  • 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

36
Next Time
  • Loop invariants
  • Way to say something about whats happening in a
    loop
Write a Comment
User Comments (0)
About PowerShow.com