Recursive Definitions and Structural Induction 7/12/2016 1 Recursive Definitions • Common way of defining mathematical objects – Base case(s) – Constructor rule(s) – “Nothing else” (generally implicit) • Have already seen recursive definition of the Fibonacci numbers – F0=0, F1=1 – Fn+1 = Fn+Fn-1 (n ≥ 1) 7/12/2016 2 The set of all strings of balanced parentheses • (), (()()), ()() but not )( or (() • Familiar “Counting rule”—an algorithm: – Start count at 0 – When you see a “(“, add 1 – When you see a “)”, subtract 1 – Balanced if count never goes negative and ends at 0 • But we want a structural definition 7/12/2016 3 A Structural Definition • Base case: – The empty string εis balanced • Constructor rules: – C1: If x is balanced then so is (x), that is, the result of writing a “(“, then x, then “)” – C2: If x and y are balanced then so is xy • (Implicit “that’s-all” clause) – (No string is balanced unless it can be constructed using the base and constructor rules) • NB: x and y are variables whose values are strings 7/12/2016 4 Some Balanced Strings ε: Base case ( ) : C1 rule, (x), where x =ε (( )): C1 rule, (x), where x = ( ) ( )( ): C2 rule, xy, where x = y = ( ) (( ))( )( ): C2 rule, xy, where x = (( )), y = ( )( ) … 7/12/2016 5 Showing a String is Balanced • Is (( )( )) balanced? Yes, because – ε is balanced (base rule) – (ε) is balanced by first constructor rule, but that is just another way of writing () – ( )( ) is balanced by second constructor rule – (( )( )) is balanced by first constructor rule 7/12/2016 6 Proof that every balanced string has equal numbers of “(“ and “)” • Suppose x is balanced. Then it is balanced because of either the base rule or a constructor rule • If by the base rule, then x is ε and has 0 left and 0 right parentheses. ✓ • If by the first constructor rule then x=(y) for some previously constructed balanced string y. Then y has the same number of “(“ and “)”, say n. Then x = (y) has one more “(“ and one more “)”, so n+1 of each. ✓ 7/12/2016 7 Every balanced string has equal numbers of “(“ and “)” • If by the second constructor rule then x=yz for some previously constructed balanced strings y and z. Then y has the same number of “(” and “)”, say n, and z has the same number of “(” and “)”, say m. Then x=yz has m+n “(” and m+n “)”. ✓ 7/12/2016 8 Structural Induction: The General Schema • To prove P(x) holds for all x in a recursively defined set S, prove – P(b) for each base case b∈S – P(c(x1,…,xk)) for each constructor c, assuming as the induction hypothesis that P(x1), …, and P(xk) all hold. 7/12/2016 9