CS 1813 – Discrete Mathematics Lecture 27 - CS 1813 Discrete Math, University of Oklahoma 9/21/2018 CS 1813 – Discrete Mathematics Discrete Mathematics Review or Yes, the Final Will Be Comprehensive CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
Truth Tables for Logical Operators P Q P Q False False False False True False True False False True True True P Q False True P Q PQ PQ P CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
Predicates and Quantifiers Parameterized collection of propositions P(x) Typically a different proposition for each x Universe of discourse Values that x may take Quantifiers x.P(x) True iff the proposition P(x) is True for every x in the universe of discourse x.P(x) True iff there is at least one x in the universe of discourse for which the proposition P(x) is True CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
Rules of Inference Propositional Calculus Lecture 27 - CS 1813 Discrete Math, University of Oklahoma 9/21/2018 Rules of Inference Propositional Calculus Discrete Mathematics with a Computer Springer, 2000 Fig 2.1, Hall/O’Donnell After presenting this slide, put up a copy on the overhead projector to refer to in upcoming proofs. CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
Inference Rules of Predicate Calculus Lecture 27 - CS 1813 Discrete Math, University of Oklahoma 9/21/2018 Inference Rules of Predicate Calculus x. F(x) {y not in F(x)} {R} y. F(y) Renaming Variables F(x) {x arbitrary, y not in F(x)} {R} F(y) x. F(x) {y not in F(x)} {R} y. F(y) Introducing/Eliminating Quantifiers F(x) {x arbitrary} {I} x. F(x) x. F(x) {universe is not empty} {E} F(x) {E} rule triggers discharge F(x) {I} x. F(x) x. F(x) F(x) |– A {x not free in A} {E} A ...plus the inference rules of propositional calculus CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
Induction Rules of Inference P(0) n.(P(n)P(n+1)) {Ind} n.P(n) Induction n.((mn.P(m))P(n)) {StrInd} n.P(n) Strong Induction t. ((s t. P(s)) P(t)) {TrInd} t.P(t) Tree Induction s t means s is a proper subtree of t {P(v) B(v)} s {P(v)} {LInd} {P(v)} (while B(v) do s) {P(v)B(v)} Loop Induction v is a set v of variables P(v) and B(v) are predicates cannot alter values in v s is a command s may alter values in v CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
Some Theorems in Rule Form a b {Comm} b a Or Commutes a b {Comm} b a And Commutes ab bc {Chain} ac Implication Chain Rule (a b) {()Comm} (b a) Not Or Commutes a b b {modTol} a Modus Tollens a b {conPosF} (b)(a) Contrapositive Fwd {noMiddle} a (a) Law of Excluded Middle a a { +&- } False NeverBoth a b {F} (a) b Implication Fwd CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
More Theorems in Rule Form (a b) {DeMF} (a)(b) DeMorgan Or Fwd (a)(b) {DeMB} (a b) DeMorgan Or Bkw (a b) {DeMF} (a)(b) DeMorgan And Fwd (a)(b) {DeMB} (a b) DeMorgan And Bkw a b a {disjSyll} b Disjunctive Syllogism (a) { F} a Double Negation Fwd a { B} (a) Double Negation Bkw CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
Some Laws of Boolean Algebra From Fig 2.1, Hall & O’Donnell, Discrete Math with a Computer, Springer, 2000 CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
More Laws of Boolean Algebra From Fig 2.1, Hall & O’Donnell, Discrete Math with a Computer, Springer, 2000 (a b) b = b { absorption} (a b) b = b { absorption} (a b) c = (a c) (b c) { imp} ((a b) (a c) (b c)) c { Elim} CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
Algebraic Laws of Predicate Calculus Lecture 27 - CS 1813 Discrete Math, University of Oklahoma 9/21/2018 Algebraic Laws of Predicate Calculus x not free in q (x. P(x)) (y. Q(y)) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) y not in f(x) (x. f(x)) = (y. f(y)) {R} (x. f(x)) = (y. f(y)) {R} CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
Haskell Type Specifications x, y, z :: Integer -- x, y, and z have type Integer xs, ys :: [Integer] -- sequences with Integer elements or :: [Bool] -> Bool -- function with one argument argument is sequence with Bool elems delivers value of type Bool (++) :: [e] -> [e] -> [e] -- generic function with two arguments args are sequences with elems of same type type is not constrained (can be any type) delivers sequence with elements of same type as those in arguments sum :: Num n => [n] -> n -- generic function with one argument argument is a sequence with elems of type n n must a type of class Num Num is a set of types with +, , … operations powerSet :: (Eq e, Show e) => Set e -> Set(Set e) -- generic function with one argument argument is a set with elements of type e delivers set with elements of type (Set e) type e must be both class Eq and class Show Class Eq has == operator, Show displayable CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
Some Algebraic Laws of Software Algebraic law of sequence construction x : [x1, x2, …, xn] = [x, x1, x2, …, xn] -- (:) Algebraic laws of concatenation [ ] ++ ys = ys -- (++).[] (x : xs) ++ ys = x : (xs ++ ys) -- (++).: What is the type of (++) ? (++) :: [a] -> [a] -> [a] Algebraic laws of foldr foldr () z [ ] = z -- (foldr).[] foldr () z (x : xs) = x (foldr () z xs) -- (foldr).: What is the type of foldr ? foldr :: (a -> b -> b) -> b -> [a] -> b The big or (\/) :: Bool -> Bool -> Bool -- “little or” – satisfies Boolean laws for or = foldr (\/) False -- “big or” What is the type of or ? or :: [Bool] -> Bool CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
More Algebraic Laws of Software sum(x: xs) = x + sum xs (sum).: sum[ ] = 0 (sum).[] Theorem: sum = foldr (+) 0 Type: sum :: Num n => [n] -> n length(x: xs) = 1 + length xs (length).: length[ ] = 0 (length).[] Theorem: length = foldr oneMore 0 where oneMore x n = 1 + n Type: length :: [a] -> Int (x: xs) ++ ys = x: (xs ++ ys) (++).: [ ] ++ ys = ys (++).[] Theorem: xs ++ ys = foldr (:) ys xs Type: (++) :: [a] -> [a] -> [a] concat(xs: xss) = xs ++ concat xss (concat).: concat[ ] = [ ] (concat).[] Theorem: concat = foldr (++) [ ] Type: concat :: [[a]] -> [a] CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
Still More Algebraic Laws of Software Pattern: foldr () z [x1, x2, …, xn-1, xn ] = x1 ( x2 … (xn-1 ( xn z)) … ) foldr () z (x: xs) = x foldr () z xs (foldr).: foldr () z [ ] = z (foldr).[] Type: foldr :: (a -> b -> b) -> b -> [a] -> b Pattern: map f [x1, x2, … xn] = [f x1, f x2, … f xn] map f (x : xs) = (f x) : map f xs (map).: map f [ ] = [ ] (map).[] Type: map :: (a -> b) -> [a] -> [b] Pattern: zipWith b [x1, x2, … xn] [y1, y2, … yn] = [b x1 y1, b x2 y2, … b xn yn] Note: extra elements in either sequence are dropped zipWith b (x:xs) (y:ys) = (b x y): (zipWith xs ys) (zipW).: zipWith b [ ] ys = [ ] (zipW).[]-1 zipWith b xs [ ] = [ ] (zipW).[]-2 Type: zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
CS 1813 Discrete Mathematics, Univ Oklahoma Sets {2, 3, 5, 7, 11} — explicit enumeration 2 {2, 3, 5, 7, 11} — stylized epsilon means “element of” = { } — stylized Greek letter phi denotes empty set {x | p x} — set comprehension Denotes set with elements x, where (p x) is True {f x | p x} — set comprehension Denotes set with elements of form (f x), where (p x) is True A B x. (x A x B) — subset A = B (A B) (B A) — set equality A B = {x | x A x B} — union S = {x | AS. x A} — big union A B = {x | x A x B} — intersection S = {x | AS. x A} — big intersection A – B = {x | x A x B} — set difference A’ = U – A — complement (U = universe) P(A) = {S | S A} — power set A B = {(a, b) | a A b B} — Cartesian product CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
Loop Induction for verifying properties of loops Loop precondition: P(x1, x2, … xx) proved True while B(x1, x2, … xx) … body of loop … Loop invariant: P(x1, x2, … xx) proved True P(x1, x2, … xx) B(x1, x2, … xx) is True Loop Induction Proof by Loop Induction Prove: P(x1, x2, … xx) is true when a loop begins Prove: same P(x1, x2, … xx) is true at end of each iteration Proof may assume P(x1, x2, … xx) was true on previous iterations Conclude: P(x1, x2, … xx) is True and B(x1, x2, … xx) is False if and when the loop terminates Requirement Computing B(x1, x2, … xx) does not affect values of x1, x2, … xx CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
sum = foldr (+) 0 — as a loop Function precondition: a[1..n] defined integer sum(integer a[ ]) integer n = length(a[ ]) integer k, s s = 0 k = 0 while (k < n) k = k+1 s = s + a[k] return s Conclude at return (by loop induction) s = a[i] i=1 k Loop precondition: s = a[i] i=1 k But what is k at return? Loop invariant: s = a[i] i=1 k Loop terminates with k = n by counting-loop theorem (coming up) Loop precondition True Subscript set for is empty and empty sums are 0, by convention Loop invariant True at end of loop if True at beginning a[i] = i=1 k+1 a[i] where k denotes top-of-loop value of k k a[k+1] + CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
The Counting-Loop Theorem A type, c, is a “counting type” if c includes operations suc::c -> c and (<), (=)::c -> c -> bool (suc m) n whenever (m < n) {Note: x y means (x < y)(x = y)} (m < n) (n iterate suc m) iterate f x = x : (iterate f (f x)) Computation pattern: iterate f x = [x, f x, f(f x), f(f(f x), … ] Theorem (counting loop) If k, m, n :: c, and m n, and If neither cmd1 nor cmd2 affects the values of k, m, or n Then the following loop terminates and when it does, k = n k = m while (k < n) cmd1 k = suc k cmd2 CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
CS 1813 Discrete Mathematics, Univ Oklahoma What Is a Tree? Tree a diagram or graph that branches usually from a simple stem without forming loops or polygons Merriam-Webster a type of data structure in which each element is attached to one or more elements directly beneath it — Webopedia a node, together with a sequence of trees inductive definition Tree terminology subtree — a node in a tree, together with its sequence of trees root — the node that, with its subtrees, comprises the entire tree interior node — a node with a nonempty sequence of subtrees leaf — a tree with an empty sequence of subtrees branch — a line connecting a node to its subtrees (in tree diagram) binary tree — a tree with no nodes having more than 2 subtrees CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
Binary Search Tree a formal representation Nub – leaf constructor data SearchTree key dat = Nub | Cel key dat (SearchTree key dat) (SearchTree key dat) Cel – constructor, non-empty trees Key goes here Node data Left subtree (smaller keys) Right subtree (larger keys) Type parameters — key, dat key — might be Int, for example (datatype with an ordering) dat — could be any type, typically a tuple Example s :: SearchTree Int (String, Float, [String] ) s is a SearchTree key type – Int (maybe a catalog order number) dat type – tuple storing a String (product description), a Float (price), and a sequence of strings (inventory records) CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
Big O Notation and Computation Time Bounding the rate of growth Given: functions f and g f is big-O of g, written f = O(g), means c, s. x s. |f(x)| c|g(x)| deal (x1: (x2: xs)) = ( x1: ys, x2: zs ) where (ys, zs) = deal xs deal [x] = ( [x], [ ] ) deal [ ] = ( [ ], [ ] ) Computation time for deal Tn = time required for to compute deal[x1, x2, … xn] Recurrence equations T0 = T1 = 3 3 ops: matching, []-build, pair-build Tn = Tn-2 + 4 4 ops: matching, 2 insertions, pair-build plus deal sequence that is shorter by 2 Tn 4n, n 0 that is, Tn = O(n) — prove by induction CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
What a Relation Is as a mathematical object Relation — a definition A binary relation () :: A B is a subset of the Cartesian product of A (the domain) and B (the codomain) A B = {(a, b) | aA, bB} a b means (a, b) Reflexive ::AA, aA. a a Irreflexive ::AA, aA. (a, a) Symmetric ::AA, a, bA. ab ba Antisymmetric ::AA, a, bA. ab ba a = b Transitive ::AA, a, b, c A. ab bc ac Closure wrt P ::AA, {S | S S has property P} CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
CS 1813 Discrete Mathematics, Univ Oklahoma Classes of Relations Partial Order :: AA, reflexive, antisymmetric, transitive Total Order :: AA, partial order a, bA. (a, b) (b, a) Well Order :: AA, total order B, BA, B. bB. aA. (b, a) Equivalence Relation :: A A reflexive, symmetric, and transitive CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
CS 1813 Discrete Mathematics, Univ Oklahoma What It All Means Completeness in Formal Systems If a |= b, then a |– b (a, b arbitrary WFFs) If b is true whenever a is, there is a proof of a |– b Notions of Consistency in Formal Systems If a |– b, then a |= b (a, b arbitrary WFFs) No WFF a such that both |– a and |– a Predicate Logic Consistent Inference preserves tautologies Inconsistency would make all WFFs tautologies Some WFFs aren’t tautologies — QED (consistency) Complete There is a proof for every tautology in predicate calculus More powerful formal systems (such as arithmetic or Haskell) are not complete CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
Lecture 27 - CS 1813 Discrete Math, University of Oklahoma 9/21/2018 Why Bother with Proofs? 100s of inputs > 2100s of possibilities input signals software output signals Key presses Mouse gestures Files Databases … computation Images Sounds Software translates input signals to output signals A program is a constructive proof of a translation Responsible software developers are obligated specify what properties their software is guaranteed to have. Proofs play a big role in this. Other methods of confirming software properties, especially testing, leave software riddled with errors. Most systems have hundreds of input signals. Since each input signal must have at least two possible values, the number of different input states that must be checked to verify the operation of the software through testing is at least 2 to the power 100. If you can generate, run, and examine one test case every minute, round the clock, you will not finish testing before you die. Not even before the sun runs out of fuel. Not even if you do one test case every nanosecond. Forget it. Testing can work only on very small pieces of software. But what translation? Proofs can confirm that software works correctly Testing cannot confirm software correctness Practice with proofs improves software thinking CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
CS 1813 Discrete Mathematics Learning Goals Lecture 27 - CS 1813 Discrete Math, University of Oklahoma 9/21/2018 CS 1813 Discrete Mathematics Learning Goals Apply mathematical logic to prove software properties Predicate calculus and natural deduction Boolean algebra and equational reasoning Mathematical induction Understand fundamental data structures Sets Trees Functions and relations Additional topics Graphs Counting Algorithm Complexity proofs galore! CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page
Lecture 27 - CS 1813 Discrete Math, University of Oklahoma 9/21/2018 End of Lecture CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page