Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 13 CS 1813 – Discrete Mathematics

Similar presentations


Presentation on theme: "Lecture 13 CS 1813 – Discrete Mathematics"— Presentation transcript:

1 Lecture 13 CS 1813 – Discrete Mathematics
Lecture 16 - CS 1813 Discrete Math, University of Oklahoma 11/18/2018 Lecture 13 CS 1813 – Discrete Mathematics Induction Induction … CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

2 Concatenating Sequences
Lecture 16 - CS 1813 Discrete Math, University of Oklahoma 11/18/2018 Concatenating Sequences (++) :: [a] -> [a] -> [a] (x: xs) ++ ys = x: (xs ++ ys) (++).: [ ] ++ ys = ys (++).[] Proposition P(n) (universe of discourse: n N ) length xs = n  length(xs ++ ys) = length xs + length ys P(0): length xs = 0  length(xs++ys) = length xs + length ys length(xs ++ ys) = length([ ] ++ ys) len 0  [ ] = length ys (++).[] = 0 + length ys 2nd-grade arithmetic = length xs + length ys hypothesis of implication Next, prove P(n)  P(n+1) … next slide CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

3 Concatenating Sequences the inductive case
Lecture 16 - CS 1813 Discrete Math, University of Oklahoma 11/18/2018 Concatenating Sequences the inductive case P(n+1): length xs = n+1  length(xs++ys) = (length xs) + (length ys) length(xs++ys) = length((z:zs) ++ ys) xs = (z: zs) {see note} = length(z: (zs ++ ys)) (++).: = 1 + length(zs ++ ys) (length).: = 1 + length zs + length ys P(n), since length zs = n = 1 + n + length ys (length zs) = n {see note} = n length ys + comm = length xs + length ys hypothesis in P(n+1) Note: z. zs. (xs = (z: zs))  ((length zs) = n) (:len) corollary CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

4 Concatenating Sequences applying the principle of induction
Lecture 16 - CS 1813 Discrete Math, University of Oklahoma 11/18/2018 Concatenating Sequences applying the principle of induction Proved: P(0) Proved: P(n)  P(n+1) Conclude nN. P(n) — by the principle of induction P(n): length xs = n  length(xs ++ ys) = length xs + length ys qed CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

5 Concatenating a List of Sequences the big ++
Lecture 16 - CS 1813 Discrete Math, University of Oklahoma 11/18/2018 Concatenating a List of Sequences the big ++ concat :: [[a]] -> [a] concat(xs: xss) = xs ++ concat xss (concat).: concat[ ] = [ ] (concat).[] Theorem: n N. P(n) where P(n) is defined as follows: P(n)  (k{1, 2, … n}. length(xsk)N)  length(concat [xs1, xs2, … xsn]) = sum[length xs1, length xs2, … length xsn] List Comprehension — like set comprehension, but for sequences [sequence-element | generator, optional-constraint] [x - 2 | x <- [12, 9, 27, 19, 13]] = [10, 7, 25, 17, 11] [x | x <- [12, 9, 27, 19, 13], x < 15] = [12, 9, 13] [x + 3 | x <- [12, 9, 27, 19, 13], x < 15] = [15, 12, 16] Proof Induction on n All this stuff is starting to look alike, isn’t it? CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

6 Lecture 16 - CS 1813 Discrete Math, University of Oklahoma
11/18/2018 shuffle shuffle :: [a] -> [a] -> [a] Examples shuffle [1, 2, 3, 4, 5] [6, 7, 8, 9, 10] = [1, 6, 2, 7, 3, 8, 4, 9, 5, 10] shuffle [1, 2, 3, 4, 5] [6, 7, 8] = [1, 6, 2, 7, 3, 8, 4, 5] shuffle [1, 2, 3, 4] [6, 7, 8, 9, 10] = [1, 6, 2, 7, 3, 8, 4, 9, 10] Pattern of computation shuffle [x1, x2, … xn] [y1, y2, … yn] = [x1, y1, x2 , y2, … xn , yn] Note: extra elements in either sequence appended to the end Definition shuffle (x: xs) (y: ys) = [x, y] ++ shuffle xs ys shuffle [ ] ys = ys shuffle xs [ ] = xs CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

7 Lecture 16 - CS 1813 Discrete Math, University of Oklahoma
11/18/2018 shuffle Works shuffle :: [a] -> [a] -> [a] shuffle (x: xs) (y: ys) = [x, y] ++ shuffle xs ys (shf).: shuffle [ ] ys = ys (shf).[]L shuffle xs [ ] = xs (shf).[]R Theorem (shuffle works) nN. P(n) where P(n)  shuffle [x1, x2, … xn] [y1, y2, … yn] = [x1, y1, x2 , y2, … xn , yn] P(0) = shuffle [ ] [ ] = [ ] — Why? Because (shf).[]L That’s why! P(n+1)  shuffle [x1, x2, … xn+1] [y1, y2, … yn+1]=[x1, y1, x2 , y2, … xn+1 , yn+1] Proof of P(n+1) shuffle [x1, x2, … xn+1] [y1, y2, … yn+1] = shuffle (x1 : [x2, x3, … xn+1] ) (y1 : [y2, y3, … yn+1] ) (: …) (twice) = [x1, y1] ++ shuffle [x2, x3, … xn+1] [y2, y3, … yn+1] (shf).: = [x1, y1] ++ [x2, y2, x3 , y3, … xn+1 , yn+1] P(n) = x1: (y1: [x2, y2, x3 , y3, … xn+1 , yn+1] ) (++).: (twice) = [x1, y1, x2 , y2, … xn+1 , yn+1] (: …) (twice) CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

8 Lecture 16 - CS 1813 Discrete Math, University of Oklahoma
11/18/2018 Indexing (!!) (!!):: [a] -> Int -> a (x: xs) !! 0 = x (!!).0 (x: xs) !! (n+1) = xs !! n (!!).n Note: n N N = {0, 1, 2, …} Pattern of computation [x0, x1, x2, …] !! k = xk What do the (!!)-equations say about the following formula? [ ] !! 0 How about this formula? [ ] !! k How about these? (x: xs) !! (-1) xs !! (-1) CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

9 shuffle Works – more formally
Lecture 16 - CS 1813 Discrete Math, University of Oklahoma 11/18/2018 shuffle Works – more formally shuffle (x: xs) (y: ys) = [x, y] ++ shuffle xs ys (shf).: Theorem (shuffle works) nN. P(n) where P(n)  ( ((length xs) > n)  ((length ys) > n) )  (k, 0  k  n. (((shuffle xs ys) !! (2*k)) = (xs !! k))  (((shuffle xs ys) !! (2*k + 1)) = (ys !! k)) ) P(0)  ( ((length xs) > 0)  ((length ys) > 0) )  (k, 0  k  0. (((shuffle xs ys) !! (2*k)) = (xs !! k))  (((shuffle xs ys) !! (2*k + 1)) = (ys !! k)) ) = ( ((length xs) > 0)  ((length ys) > 0) )  ( (((shuffle xs ys) !! 0) = (xs !! 0))  (((shuffle xs ys) !! 1) = (ys !! 0)) ) {finite -universe} = ( (x. ws. xs = (x: ws))  (y. zs. ys = (y: zs)) )  ( (((shuffle (x: ws) (y: zs)) !! 0) = ((x: ws) !! 0))  (((shuffle (x: ws) (y: zs)) !! 1) = ((y: zs) !! 0)) ) {(:), len 0  [ ], arith} = ( (x. ws. xs = (x: ws))  (y. zs. ys = (y: zs)) )  ( ((([x, y] ++ shuffle ws zs) !! 0) = ((x: ws) !! 0))  ((([x, y] ++ shuffle ws zs) !! 1) = ((y: zs) !! 0)) ) {(shf).:} = ( (x. ws. xs = (x: ws))  (y. zs. ys = (y: zs)) )  ( (x = x )  (y = y) ) { (++).:, , (!!).n+1, (!!).0} = True {= reflexive,  id, Thm: (a  True) = True } CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page end of base case

10 shuffle Works – more formally inductive case
Lecture 16 - CS 1813 Discrete Math, University of Oklahoma 11/18/2018 shuffle Works – more formally inductive case P(n+1)  ( ((length xs) > n+1)  ((length ys) > n+1) )  (k, 0  k  n+1. (((shuffle xs ys) !! (2*k)) = (xs !! k))  (((shuffle xs ys) !! (2*k + 1)) = (ys !! k)) ) = ( (x.ws.xs=(x:ws)((length ws) > n) )(y.zs.ys=(y:zs)((length zs) > n)) )  (k, 0  k  n+1. (((shuffle xs ys) !! (2*k)) = (xs !! k))  (((shuffle xs ys) !! (2*k + 1)) = (ys !! k)) ) {len 0  [ ], } = ( (x.ws.xs=(x:ws)((length ws) > n) )(y.zs.ys=(y:zs)((length zs) > n)) )  ((k, 0  k  n.( (((shuffle xs ys) !! (2*k)) = (xs !! k))  (((shuffle xs ys) !! (2*k + 1)) = (ys !! k)) )  (((shuffle (x:ws) (y:zs))!!(2*(n+1))) = ((x:ws)!!(n+1)))  (((shuffle (x:ws) (y:zs))!!(2*(n+1)+1)) = ((y:zs)!!(n+1))) ) {subst, finite -univ} = ( (x.ws.xs=(x: ws)((length ws) > n))  (y.zs.ys=(y: zs) ((length zs) > n)))  (k, 0  k  n.( (((shuffle xs ys) !! (2*k)) = (xs !! k))  (((shuffle xs ys) !! (2*k + 1)) = (ys !! k)) )  (((shuffle ws zs)!!(2*n)) = (ws!!n))  (((shuffle ws zs)!!(2*n+1)) = (zs!!n)) ) {(shf).:, (++).:, , (!!).n+1} = ( (x.ws.xs=(x: ws)((length ws) > n))  (y.zs.ys=(y: zs)  ((length zs) > n)))  (True  True  True) {P(n)} = True { id, Thm: (a  True) = True} CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page qed

11 Lecture 16 - CS 1813 Discrete Math, University of Oklahoma
11/18/2018 End of Lecture CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page


Download ppt "Lecture 13 CS 1813 – Discrete Mathematics"

Similar presentations


Ads by Google