Slide 1

Slide 1 text

Class 16: Structural Induction cs2102: Discrete Mathematics | F17 uvacs2102.github.io David Evans Mohammad Mahmoody University of Virginia

Slide 2

Slide 2 text

Plan for today •Continuing Recursive Data Types •Structural Induction

Slide 3

Slide 3 text

Problem Set 6 Due tomorrow Problem 7 “correction”

Slide 4

Slide 4 text

Defining List Definition 1. A List is an ordered sequence of objects. Definition 2. A List is either: (1) the empty list or (2) and object followed by another list ′. Are these equivalent definitions?

Slide 5

Slide 5 text

Operators corresponding to Base Case and Constructor Case null: prepend ∶ × ⟶

Slide 6

Slide 6 text

More Operations over Lists null: prepend: × ⟶ first: List ⟶ rest: ⟶ empty: ⟶

Slide 7

Slide 7 text

Defining them Using Base and Constructor empty null ⟶ empty prepend (, ) ⟶ first prepend (, ) ⟶ rest prepend (, ) ⟶

Slide 8

Slide 8 text

Another operation: length null: prepend: × ⟶ first: List ⟶ rest: ⟶ empty: ⟶ length: ⟶

Slide 9

Slide 9 text

How to define length recursively? null: prepend: × ⟶ length: ⟶

Slide 10

Slide 10 text

Length of a List Definition. The length of a list, , is: 0 : if is null 1 + length : if = prepend , 10

Slide 11

Slide 11 text

Another operation: Concatenation null: prepend: × ⟶ Concatenation: × ⟶

Slide 12

Slide 12 text

Concatenation: Descriptive Def Definition. The concatenation of two lists, = 1 , 2 , … , and = 1 , 2 , … , is 1 , 2 , … , , 1 , 2 , … , . How can we define this constructively? 12

Slide 13

Slide 13 text

Concatenation Definition. The concatenation of two lists, = 1 , 2 , … , and = 1 , 2 , … , is 1 , 2 , … , , 1 , 2 , … , . How can we define this constructively? Also: Any questions about definitions so far, recursive data, etc. 13

Slide 14

Slide 14 text

Concatenation: Descriptive Def Definition. The concatenation of two lists, = 1 , 2 , … , and = 1 , 2 , … , is 1 , 2 , … , , 1 , 2 , … , . How can we define this constructively? 14 Suggestion: Define p+q ::= Prepend(p,q) Why does not work: then first(p+q) would be p not 1 Another reason this is not a good idea to define concatenation: We might define Lists of “restricted objects” (e.g. integers) in which case Prepend(e,q) only accepts “e” to be in restricted form (not including lists)

Slide 15

Slide 15 text

Concatenation: Constructive Def Definition. The concatenation ( + ) of two lists, and , is recursively defined as: Base case: if = (empty list) then + = Constructor case: if = prepend(, ) for some list , obj then + = prepend(, + ) 15

Slide 16

Slide 16 text

Did we define length and concatenation properly? Prove. For any two lists, and , length( + ) = length() + length() 16

Slide 17

Slide 17 text

Structural Induction To prove for all List 1. Prove for all base list . 2. Prove ⇒ for all constructable from . 17

Slide 18

Slide 18 text

Prove. For any two lists, and , length( + ) = length() + length() Base case: = (empty list) + = Constructor case: = prepend (, ) + = prepend (, + ) 18 Proposition F(p) for any list p : F(p) : ∀ ∈ . length( + ) = length() + length()

Slide 19

Slide 19 text

Structural Induction (Data Types) Invariant Principle (State Machines) (Regular) Induction (Natural Numbers) for all data type objects for all reachable states To prove ∙ for all natural numbers prove a base case 0 0 base object prove an inductive step ⇒ ( + 1) ⇒ for all constructable from ⇒ for all reachable from 19

Slide 20

Slide 20 text

Charge Next week: Infinite sets and cardinalities.. Problem Set 6 due tomorrow (make sure to read the updated problem 7) Problem set 7 posted tomorrow night (or Sat morning) – read chapter 7 + notes/slides of this weak