We can reason, using induction, over any inductively

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 13 - induction on lists.
Advertisements

Satisfiability Modulo Computable Functions Philippe Suter, Ali Sinan Köksal, and Viktor Kuncak ÉCOLE POLYTECHNIQUE FÉDÉRALE DE LAUSANNE, SWITZERLAND Photo:
Lecture 8 From NFA to Regular Language. Induction on k= # of states other than initial and final states K=0 a a* b a c d c*a(d+bc*a)*
Recursion Breaking down problems into solvable subproblems Chapter 7.
ECE 2110: Introduction to Digital Systems Combinational Logic Design Principles.
© M. Winter COSC 4P41 – Functional Programming Testing vs Proving Testing –uses a set of “typical” examples, –symbolic testing, –may find errors,
Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.
5/10/20151 GC16/3011 Functional Programming Lecture 13 Example Programs 1. Evaluating arithmetic expressions 2. lists as functions.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 39: Lambda Calculus.
Termination Analysis Math Foundations of Computer Science.
Transparency No. 4-1 Formal Language and Automata Theory Chapter 4 Patterns, Regular Expressions and Finite Automata (include lecture 7,8,9) Transparency.
Discussion #131/18 Discussion #13 Induction (the process of deriving generalities from particulars) Mathematical Induction (deductive reasoning over the.
Transparency No. P2C5-1 Formal Language and Automata Theory Part II Chapter 5 The Pumping Lemma and Closure properties for Context-free Languages.
1 Finite Model Theory Lecture 13 FO k, L k 1, ,L  1, , and Pebble Games.
Arithmetic Expression Consider the expression arithmetic expression: (a – b) + ((c + d) + (e * f)) that can be represented as the following tree.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
1 Semantics Q S EMANTICS (Q1,’07) Week 4 Jacob Andersen PhD student
Random Walks Great Theoretical Ideas In Computer Science Steven Rudich, Anupam GuptaCS Spring 2004 Lecture 24April 8, 2004Carnegie Mellon University.
Lecture 8 Sept 29, 2011 Regular expressions – examples Converting DFA to regular expression. (same works for NFA to r.e. conversion.) Converting R.E. to.
Transparency No. P2C5-1 Formal Language and Automata Theory Part II Chapter 5 The Pumping Lemma and Closure properties for Context-free Languages.
Advanced Programming Handout 5 Recursive Data Types (SOE Chapter 7)
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Equational Reasoning Math Foundations of Computer Science.
Sequences and Series (T) Students will know the form of an Arithmetic sequence.  Arithmetic Sequence: There exists a common difference (d) between each.
Lecture 9. Arithmetic and geometric series and mathematical induction
Inductive Proof (the process of deriving generalities from particulars) Mathematical Induction (reasoning over the natural numbers)
Mathematical Induction. F(1) = 1; F(n+1) = F(n) + (2n+1) for n≥ F(n) n F(n) =n 2 for all n ≥ 1 Prove it!
College Algebra Fifth Edition James Stewart Lothar Redlin Saleem Watson.
CSE373: Data Structures and Algorithms Lecture 2b: Proof by Induction and Powers of Two Nicki Dell Spring 2014.
Factoring Expressions Completely Some algebraic expressions will need more than one step or method to completely factor them. Use you factoring tree to.
Random Walks Great Theoretical Ideas In Computer Science Steven Rudich, Anupam GuptaCS Spring 2005 Lecture 24April 7, 2005Carnegie Mellon University.
Methods of Proof Lecture 3: Sep 9. This Lecture Now we have learnt the basics in logic. We are going to apply the logical rules in proving mathematical.
1 Binomial Coefficients CS 202 Epp, section ??? Aaron Bloomfield.
CS 473Lecture X1 CS473-Algorithms I Lecture X1 Properties of Ranks.
The Tutte Polynomial Graph Polynomials winter 05/06.
Type Safety Kangwon National University 임현승 Programming Languages.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
Chapter 4 Pumping Lemma Properties of Regular Languages Decidable questions on Regular Languages.
(Finite) Mathematical Induction In our first lesson on sequences and series, you were told that How can we be certain that this will be true for all counting.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
Context Free Pumping Lemma. CFL Pumping Lemma A CFL pump consists of two non-overlapping substrings that can be pumped simultaneously while staying in.
Classifications LanguageGrammarAutomaton Regular, right- linear Right-linear, left-linear DFA, NFA Context-free PDA Context- sensitive LBA Recursively.
Compiler Principles Fall Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Boolean Expression Evaluation CS 270: Math Foundations of CS Jeremy Johnson.
Using Properties of Parallel Lines Sec. 3.5 p. 157 GOAL: To use properties of parallel lines.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Mathematical Induction Prudence Wong
6/12/2016 Prepared by Dr.Saad Alabbad1 CS100 : Discrete Structures Proof Techniques(2) Mathematical Induction & Recursion Dr.Saad Alabbad Department of.
1 Proving Properties of Recursive Functions and Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
Mathematical Induction. The Principle of Mathematical Induction Let S n be a statement involving the positive integer n. If 1.S 1 is true, and 2.the truth.
Notes Over 1.2.
Inductive Proof (the process of deriving generalities from particulars) Mathematical Induction (reasoning over the natural numbers)
COMP108 Algorithmic Foundations Mathematical Induction
14:332:231 DIGITAL LOGIC DESIGN Boolean Algebra
Lecture 12 CS 1813 – Discrete Mathematics
Proving Properties of Recursive Functions and Data Structures
Context Free Pumping Lemma Some languages are not context free!
Infinite Geometric Series
Lecture 11 CS 1813 – Discrete Mathematics
Simplification of Boolean Expressions
Lazy Programming Lazy evaluation:
Sullivan Algebra and Trigonometry: Section 13.4
Linear Algebra Lecture 24.
Testing vs Proving Testing uses a set of “typical” examples,
Mathematical Induction
2.5 Proving Statements about Segments
Chapter 2: Geometric Reasoning
Evaluating an expression with two variable
COMP108 Algorithmic Foundations Mathematical Induction
Chapter 2 Reasoning and Proof.
Presentation transcript:

Functional Programming Lecture 14 - induction on algebraic data types and lazy evaluation

We can reason, using induction, over any inductively defined set. That includes algebraic data types.

Proof by induction on Trees data Tree a = Nil | Node a (Tree a) (Tree a) To prove a property P of Tree a, we use the Principle of (Structural) Induction: base case: prove P(Nil), ind. case: Prove P(Node x t1 t2), assuming P(t1) and P(t2).

size Nil = 0 s.0 size (Node x t1 t2) = 1+size t1 + size t2 s.1 depth Nil = 0 d.0 depth (Node x t1 t2) =1+max (depth t1) (depth t2) d.1 Theorem: For all finite trees. size t < 2depth t. Proof: By induction on t. Base Case: Prove (size Nil) < 2depth Nil. l.h.s. size Nil = 0 by s.0 0 < (20 = 1) by d.0, arith. Ind Case: Assume size t1 < 2depth t1, size t2 < 2depth t2. Show size (Node x t1 t2) < 2depth (Node x t1 t2). size (Node x t1 t2) = 1+size t1 + size t2. by s.1 Case depth t1>depth t2: 2depth (Node x t1 t2) = 21 + depth t1 by d.1

1 + size t1 + size t2 << 1+ 2depth t1 + 2depth t2 by ass. and << << 1 + 2depth t1 + 2depth t1 by depth t1>=depth t2 << 1 + 21+ depth t1 by 2n+2n=21+n < < 1 + 2depth (Node x t1 t2) by d.1 < 2depth (Node x t1 t2) by arith QED Note: if x<y and u<v, then x+u << y+v, i.e. x+u < y+v-1 (e.g. 4<5 and 6<7, so 10<<12 and 10<11)

Lazy evaluation Recall that expressions are evaluated. But does the order matter? f x y = x + y f (9-3) (f 34 3) => f 6 37 => 43 or => (9-3) + (f 34 3) => 6 + 37 When do we evaluate arguments fully? f x y = x + x f (9-3) (f 34 3) => f 6 37 => 6 + 6 => 12 => (9-3) + (9-3) The second is more efficient because it only evaluates arguments when it needs them. Moreover, operands can be shared. . + . . + . (9-3) (9-3) (9-3)

Lazy evaluation ensures that an argument is never evaluated more than once. - arguments to functions are only evaluated when it is necessary for evaluation to continue, - an argument is not necessarily evaluated fully; only the parts that are needed are examined, - an argument is evaluated at most only once. Evaluation order - from the outside in, and then left to right. E.g. f (h e1) (g e2 17) => (h e1) + (g e2 17) => ...

Lazy evaluation means that we can have infinite data structures! Infinite lists: [3 ..] = [3,4,5,6,7,...] [3,5 ..] = [3,5,7,9,…] ones = 1:ones = [1,1,1,1, …] powers :: Int->[Int] powers n = [n^x| x <- [0..]] We can evaluate head [3 ..] => 3 firsttwo (x:y:zs) = x + y firsttwo [3 .. ] => 3+4 => 7 Note that induction is only defined over finite lists and data structures.