Induction and Recursion

Similar documents
Finite Automata Theory and Formal Languages TMV027/DIT321 LP Recap: Logic, Sets, Relations, Functions

Automated Reasoning Lecture 17: Inductive Proof (in Isabelle)

Notes on Inductive Sets and Induction

CS 151. Red Black Trees & Structural Induction. Thursday, November 1, 12

Regular Languages. Kleene Theorem I. Proving Kleene Theorem. Kleene Theorem. Proving Kleene Theorem. Proving Kleene Theorem

Models of Computation,

Mathematical Induction

INDUCTIVE DEFINITION

CS Lecture 19: Logic To Truth through Proof. Prof. Clarkson Fall Today s music: Theme from Sherlock

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Linear two-sorted constructive arithmetic

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2017

CSCE 222 Discrete Structures for Computing. Review for Exam 2. Dr. Hyunyoung Lee !!!

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

CSI Mathematical Induction. Many statements assert that a property of the form P(n) is true for all integers n.

CSC 344 Algorithms and Complexity. Proof by Mathematical Induction

CSC236H Lecture 2. Ilir Dema. September 19, 2018

13 Dynamic Programming (3) Optimal Binary Search Trees Subset Sums & Knapsacks

What is this course about?

COE428 Notes Week 4 (Week of Jan 30, 2017)

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Dynamic Programming II Date: 10/12/17

3 Propositional Logic

problem score possible Total 60

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

CS473 - Algorithms I

Carmen s Core Concepts (Math 135)

CIS 500 Software Foundations Fall October. Review. Administrivia. Subtyping

Lecture 2: Proof of Switching Lemma

MATH The Chain Rule Fall 2016 A vector function of a vector variable is a function F: R n R m. In practice, if x 1, x n is the input,

CSC236 Week 3. Larry Zhang

Discrete Mathematics Review

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

Fall 2017 Test II review problems

CS173 Strong Induction and Functions. Tandy Warnow

Design and Analysis of Algorithms

Mathematical Induction. Rosen Chapter 4.1,4.2 (6 th edition) Rosen Ch. 5.1, 5.2 (7 th edition)

Representations of Boolean Functions in Constructive Type Theory

Decision Trees. Gavin Brown

On the mean connected induced subgraph order of cographs

Mathematical Induction Again

Example. Lemma. Proof Sketch. 1 let A be a formula that expresses that node t is reachable from s

Tree-width and algorithms

Mathematical Induction

We now introduce a powerful method for proving claims about inductively-defined sets...

CIS 500: Software Foundations

Analyse et Conception Formelle. Lesson 4. Proofs with a proof assistant

CVO103: Programming Languages. Lecture 2 Inductive Definitions (2)

6.854 Advanced Algorithms

Mathematical Induction Again

Theory of Computation

Midterm 2 for CS 170

1) Synthetic Division: The Process. (Ruffini's rule) 2) Remainder Theorem 3) Factor Theorem

1 Introduction. 2 Recap The Typed λ-calculus λ. 3 Simple Data Structures

Data Structures and Algorithms

CSE 151 Machine Learning. Instructor: Kamalika Chaudhuri

Higher Inductive Types: The circle and friends, axiomatically

Induction on Failing Derivations

Applied Logic for Computer Scientists. Answers to Some Exercises

Context-Free languages (part II)

Introduction to type theory and homotopy theory

1 / A bird s-eye view of type theory. 2 A bird s-eye view of homotopy theory. 3 Path spaces and identity types. 4 Homotopy type theory

Midterm Exam Types and Programming Languages Frank Pfenning. October 18, 2018

CS 380: ARTIFICIAL INTELLIGENCE

Learning from Observations. Chapter 18, Sections 1 3 1

COSE212: Programming Languages. Lecture 1 Inductive Definitions (1)

Divide and Conquer Algorithms

Mathematical Induction. Defining Functions. Overview. Notation for recursive functions. Base case Sn(0) = 0 S(n) = S(n 1) + n for n > 0

COSE212: Programming Languages. Lecture 1 Inductive Definitions (1)

Interface for module Ptset

Note that r = 0 gives the simple principle of induction. Also it can be shown that the principle of strong induction follows from simple induction.

Induction and Recursion

1 Circuit Complexity. CS 6743 Lecture 15 1 Fall Definitions

Introduction to Machine Learning CMU-10701

Lecture 1 : Data Compression and Entropy

CS173 Lecture B, September 10, 2015

Discrete Mathematics. Spring 2017

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

CS 380: ARTIFICIAL INTELLIGENCE MACHINE LEARNING. Santiago Ontañón

SOLUTIONS OF SEMILINEAR WAVE EQUATION VIA STOCHASTIC CASCADES

MATH 2400: PRACTICE PROBLEMS FOR EXAM 1

Models of Computation. by Costas Busch, LSU

Digital Logic Design: a rigorous approach c

Mathematical Induction. EECS 203: Discrete Mathematics Lecture 11 Spring

Transformational programming and forests

Iris: Higher-Order Concurrent Separation Logic. Lecture 4: Basic Separation Logic: Proving Pointer Programs

THE ISLAMIC UNIVERSITY OF GAZA ENGINEERING FACULTY DEPARTMENT OF COMPUTER ENGINEERING DISCRETE MATHMATICS DISCUSSION ECOM Eng. Huda M.

Quiz 3 Reminder and Midterm Results

Size-Depth Tradeoffs for Boolean Formulae

Divide and Conquer Algorithms

CSE 4502/5717: Big Data Analytics

CSE 311: Foundations of Computing I Autumn 2014 Practice Final: Section X. Closed book, closed notes, no cell phones, no calculators.

COM S 330 Homework 08 Solutions. Type your answers to the following questions and submit a PDF file to Blackboard. One page per problem.

Formalizing Kruskal s Tree Theorem in Isabelle/HOL

Logic as a Tool Chapter 1: Understanding Propositional Logic 1.1 Propositions and logical connectives. Truth tables and tautologies

Analysis of Algorithms - Using Asymptotic Bounds -

Inductive Definitions

SLD-Resolution And Logic Programming (PROLOG)

Solutions to EoPL3 Exercises

Minimally Strict Polymorphic Functions

Math 152: Applicable Mathematics and Computing

Transcription:

Induction and Recursion Prof. Clarkson Fall 2016 Today s music: Dream within a Dream from the soundtrack to Inception by Hans Zimmer

Review Previously in 3110: Behavioral equivalence Proofs of correctness by induction on naturals Today: Induction on lists Induction on trees

Review: Induction on natural numbers Theorem: for all natural numbers n, P(n). Proof: by induction on n Case: n = 0 Show: P(0) Case: n = k+1 IH: P(k) Show: P(k+1) QED

Induction principle for all properties P of natural numbers, if P(0) and (for all n, P(n) implies P(n+1)) then (for all n, P(n))

Induction principle for all properties P of lists, if P([]) and (for all x and xs, P(xs) implies P(x::xs)) then (for all xs, P(xs))

Induction on lists Theorem: for all lists lst, P(lst). Proof: by induction on lst Case: lst = [] Show: P([]) Case: lst = h::t IH: P(t) Show: P(h::t) QED

Append let rec length = function [] -> 0 _::xs -> 1 + length xs let rec append xs1 xs2 = match xs1 with [] -> xs2 h::t -> h :: append t xs2 Theorem. for all lists xs and ys, length (append xs ys) ~ length xs + length ys.

Append let rec length = function [] -> 0 _::xs -> 1 + length xs let rec append xs1 xs2 = match xs1 with [] -> xs2 h::t -> h :: append t xs2 Theorem. for all lists xs and ys, length (append xs ys) ~ length xs + length ys. Proof: by induction on xs Case: xs = [] Show: for all ys, length (append [] ys) ~ length [] + length ys length (append [] ys) ~ length ys (eval) ~ 0 + length ys (math) ~ length [] + length ys (eval,symm.)

Append let rec length = function [] -> 0 _::xs -> 1 + length xs let rec append xs1 xs2 = match xs1 with [] -> xs2 h::t -> h :: append t xs2 Theorem. for all lists xs and ys, length (append xs ys) ~ length xs + length ys. Proof: by induction on xs Case: xs = h::t Show: for all ys, length (append (h::t) ys) IH:?? ~ length (h::t) + length ys

Question If we're trying to prove for all lists xs and ys, length (append xs ys) ~ length xs + length ys. by induction on xs, in the case where xs = h::t, what is the inductive hypothesis? A. for all ys, length (append xs ys) ~ length xs + length ys B. for all ys, length (append t ys) ~ length t + length ys C. for all ys, length (append (h::t) ys) ~ length (h::t) + length ys D. for all h' and t', length (append (h::t) (h'::t')) ~ length (h::t) + length (h'::t') E. for all xs, length (append xs t) ~ length xs + length t

Question If we're trying to prove for all lists xs and ys, length (append xs ys) ~ length xs + length ys. by induction on xs, in the case where xs = h::t, what is the inductive hypothesis? A. for all ys, length (append xs ys) ~ length xs + length ys B. for all ys, length (append t ys) ~ length t + length ys C. for all ys, length (append (h::t) ys) ~ length (h::t) + length ys D. for all h' and t', length (append (h::t) (h'::t')) ~ length (h::t) + length (h'::t') E. for all xs, length (append xs t) ~ length xs + length t

Append let rec length = function [] -> 0 _::xs -> 1 + length xs let rec append xs1 xs2 = match xs1 with [] -> xs2 h::t -> h :: append t xs2 Theorem. for all lists xs and ys, length (append xs ys) ~ length xs + length ys. Proof: by induction on xs Case: xs = h::t Show: for all ys, length (append (h::t) ys) IH: ~ length (h::t) + length ys for all ys, length (append t ys) ~ length t + length ys

Append Case: xs is h::t Show: for all ys, length (append (h::t) ys) ~ length (h::t) + length ys IH: let rec length = function [] -> 0 _::xs -> 1 + length xs for all ys, length (append t ys) let rec append xs1 xs2 = match xs1 with [] -> xs2 h::t -> h :: append t xs2 ~ length t + length ys length (append (h::t) ys) ~ length (h :: append t ys) (eval) ~ 1 + length (append t ys) (eval) ~ 1 + length t + length ys (IH,congr.) ~ length (h::t) + length ys (eval,symm.,congr.) QED From now on, omit many uses of symm., trans., congr.

Higher-order functions Proofs about higher-order functions sometimes need an additional axiom: Extensionality: if (for all x, (f x) ~ (g x)) then f ~ g

Compose let (@@) f g x = f (g x) let map = List.map Theorem: for all functions f and g, (map f) @@ (map g) ~ map (f @@ g). Proof: By extensionality, we need to show that for all xs, ((map f) @@ (map g)) xs ~ map (f @@ g) xs. By eval, ((map f) @@ (map g)) xs ~ map f (map g xs). So by transitivity, it suffices to show that map f (map g xs) ~ map (f @@ g) xs.

let (@@) f g x = f (g x) let map = List.map Compose Show: map f (map g xs) ~ map (f @@ g) xs. Proof: by induction on xs Case: xs = [] Show: map f (map g []) ~ map (f @@ g) [] map f (map g []) ~ [] (eval) ~ map (f @@ g) [] (eval)

let (@@) f g x = f (g x) let map = List.map Compose Show: map f (map g xs) ~ map (f @@ g) xs. Proof: by induction on xs Case: xs = h::t Show: map f (map g (h::t)) ~ map (f @@ g) (h::t) IH: map f (map g t) ~ map (f @@ g) t map f (map g (h::t)) ~ map f ((g h)::map g t) (eval map) ~ (f (g h))::map f (map g t) (eval map) ~ ((f @@ g) h)::map f (map g t) (eval @@) ~ ((f @@ g) h)::map (f @@ g) t (IH) ~ map (f @@ g) (h::t) (eval map) Helpful to identify what is being evaluated

Compose let (@@) f g x = f (g x) let map = List.map Theorem: for all functions f and g, (map f) @@ (map g) ~ map (f @@ g). Proof: By extensionality, we need to show that for all xs, ((map f) @@ (map g)) xs ~ map (f @@ g) xs. By eval, ((map f) @@ (map g)) xs ~ map f (map g xs). So by transitivity, it suffices to show that map f (map g xs) ~ map (f @@ g) xs. We have. QED.

Compose let (@@) f g x = f (g x) let map = List.map Theorem: for all functions f and g, (map f) @@ (map g) ~ map (f @@ g). Comment: this theorem would be the basis for a nice compiler optimization in a pure language. Replace an operation that processes list twice with an operation that processes list only once.

Trees type 'a tree = Leaf Branch of 'a * 'a tree * 'a tree let rec reflect = function Leaf -> Leaf Branch(x,l,r) -> Branch(x, reflect r, reflect l)

Trees reflection of is 1 / \ 2 3 / \ / \ 4 5 6 7 1 / \ 3 2 / \ / \ 7 6 5 4

Trees type 'a tree = Leaf Branch of 'a * 'a tree * 'a tree let rec reflect = function Leaf -> Leaf Branch(x,l,r) -> Branch(x, reflect r, reflect l) Theorem: for all trees t, reflect(reflect t) ~ t. Proof: by induction on t.

Induction principle for all properties P of trees, if P(Leaf) and (for all x and l and r, P(l) and P(r) implies P(Branch(x,l,r)) then (for all t, P(t))

Induction on trees Theorem: for all trees t, P(t). Proof: by induction on t Case: n = Leaf Show: P(Leaf) Case: n = Branch(x,l,r) IH: P(l) and P(r) Show: P(Branch(x,l,r)) QED

Trees let rec reflect = function Leaf -> Leaf Branch(x,l,r) -> Branch(x, reflect r, reflect l) Theorem: for all trees t, reflect(reflect t) ~ t. Proof: by induction on t. Case: t = Leaf Show: reflect(reflect Leaf) ~ Leaf reflect(reflect Leaf) ~ Leaf (eval)

Trees let rec reflect = function Leaf -> Leaf Branch(x,l,r) -> Branch(x, reflect r, reflect l) Theorem: for all trees t, reflect(reflect t) ~ t. Proof: by induction on t. Case: t = Branch(x,l,r) Show: reflect(reflect(branch(x,l,r))) ~ Branch(x,l,r) IH:???

Question How many formulas in inductive hypothesis i.e., how many inductive hypotheses? A. 1 (for the Branch constructor) B. 2 (for the two subtrees) C. 3 (for the two subtrees and the node's label)

Question How many formulas in inductive hypothesis i.e., how many inductive hypotheses? A. 1 (for the Branch constructor) B. 2 (for the two subtrees) C. 3 (for the two subtrees and the node's label)

Trees let rec reflect = function Leaf -> Leaf Branch(x,l,r) -> Branch(x, reflect r, reflect l) Theorem: for all trees t, reflect(reflect t) ~ t. Proof: by induction on t. Case: t = Branch(x,l,r) Show: reflect(reflect(branch(x,l,r))) ~ Branch(x,l,r) IH: 1. reflect(reflect l) ~ l 2. reflect(reflect r) ~ r

Trees let rec reflect = function Leaf -> Leaf Branch(x,l,r) -> Branch(x, reflect r, reflect l) Show: reflect(reflect(branch(x,l,r))) ~ Branch(x,l,r) IH: 1. reflect(reflect l) ~ l 2. reflect(reflect r) ~ r reflect(reflect(branch(x,l,r))) ~ reflect(branch(x, reflect r, reflect l)) (eval) ~ Branch(x, reflect(reflect l), reflect(reflect r)) (eval) ~ Branch(x, l, reflect(reflect r)) (IH 1) ~ Branch(x, l, r) (IH 2) QED

Inductive proofs on variants type t = C1 of t1... Cn of tn Theorem: for all x:t, P(x) Proof: by induction on x... Case: x = Ci y IH: P(v) for any components v:t of y Show: P(Ci y)... QED

General induction principle type t =... Ci of ti... for all properties P of values of type t, if then (for all Ci, (for all y:ti, (for all components z:t of y, P(z)) implies P(Ci y))) (for all v:t, P(v))

Naturals (* unary representation *) type nat = Z S of nat Theorem: for all n:nat, P(n) Proof: by induction on n Case: n = Z Show: P(Z) Theorem: for all naturals n, P(n) Proof: by induction on n Case: n = 0 Show: P(0) Case: n = S k IH: P(k) Show: P(S k) Case: x = k+1 IH: P(k) Show: P(k+1) QED QED

Induction The kind of induction we've done today is called structural induction Induct on the structure of a data type Widely used in programming languages theory When naturals are coded up as variants, weak induction becomes structural induction Both structural induction and weak induction (and strong induction) are instances of a very general kind of induction called well-founded induction

Induction and recursion Intense similarity between inductive proofs and recursive functions on variants In proofs: one case per constructor In functions: one pattern-matching branch per constructor In proofs: uses IH on "smaller" value In functions: uses recursive call on "smaller" value Inductive proofs truly are a kind of recursive programming (see Curry-Howard isomorphism)

Upcoming events [next Wednesday] MS1 due This is inductive. THIS IS 3110