Week 5 Tutorial Structural Induction

Similar documents
Foundations of Computation

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Induction for Data Types

Programming Languages 3. Definition and Proof by Induction

Shell CSCE 314 TAMU. Functions continued

MoreIntro_annotated.v. MoreIntro_annotated.v. Printed by Zach Tatlock. Oct 04, 16 21:55 Page 1/10

Reasoning about programs. Chapter 9 of Thompson

Logic and Computation Lecture 20 CSU 290 Spring 2009 (Pucella) Thursday, Mar 12, 2009

CSCE 314 Programming Languages

Shell CSCE 314 TAMU. Higher Order Functions

Theorem Proving Principles, Techniques, Applications Recursion

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Software Properties as Axioms and Theorems

15 212: Principles of Programming. Some Notes on Induction

The Worker/Wrapper Transformation

MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

Lecture 6: Sequential Sorting

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Lecture 6: Arithmetic and Threshold Circuits

AXIOMS FOR THE INTEGERS

Thinking Induc,vely. COS 326 David Walker Princeton University

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

An introduction introduction to functional functional programming programming using usin Haskell

Haskell Overview II (2A) Young Won Lim 8/9/16

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh

Introduction to Programming, Aug-Dec 2006

Topic 5: Examples and higher-order functions

Haskell Types, Classes, and Functions, Currying, and Polymorphism

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

FUNCTIONAL PEARLS The countdown problem

RECURSION. Week 6 Laboratory for Introduction to Programming and Algorithms Uwe R. Zimmer based on material by James Barker. Pre-Laboratory Checklist

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

CSc 372 Comparative Programming Languages

To illustrate what is intended the following are three write ups by students. Diagonalization

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

List Functions, and Higher-Order Functions

CS 3512, Spring Instructor: Doug Dunham. Textbook: James L. Hein, Discrete Structures, Logic, and Computability, 3rd Ed. Jones and Barlett, 2010

Testing. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 2. [Faculty of Science Information and Computing Sciences]

Program Calculus Calculational Programming

Reasoning About Programs Panagiotis Manolios

Simon Peyton Jones (Microsoft Research) Max Bolingbroke (University of Cambridge)

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

Functional Programming Mid-term exam Tuesday 3/10/2017

Informatics 1 Functional Programming Lecture 4. Lists and Recursion. Don Sannella University of Edinburgh

Fall 2018 Lecture 1

CS 320: Concepts of Programming Languages

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

QuickCheck, SmallCheck & Reach: Automated Testing in Haskell. Tom Shackell

Lecture 19: Functions, Types and Data Structures in Haskell

Informatics 1 Functional Programming Lecture 12. Data Abstraction. Don Sannella University of Edinburgh

MoreIntro.v. MoreIntro.v. Printed by Zach Tatlock. Oct 07, 16 18:11 Page 1/10. Oct 07, 16 18:11 Page 2/10. Monday October 10, 2016 lec02/moreintro.

Exercises on the Fundamentals of Prolog

More fun with recursion

Intro. Scheme Basics. scm> 5 5. scm>

To prove something about all Boolean expressions, we will need the following induction principle: Axiom 7.1 (Induction over Boolean expressions):

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Induction and Semantics in Dafny

Stacks and queues (chapters 6.6, 15.1, 15.5)

Programming Languages Fall 2014

Reasoning About Programs Panagiotis Manolios

COMP 250 Fall Recursive algorithms 1 Oct. 2, 2017

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

To prove something about all Boolean expressions, we will need the following induction principle: Axiom 7.1 (Induction over Boolean expressions):

Applicative, traversable, foldable

Informatics 1 Functional Programming Lecture 5. Function properties. Don Sannella University of Edinburgh

by the evening of Tuesday, Feb 6

Functional Programming. Overview. Topics. Recall λ-terms. Examples

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Fall Recursion and induction. Stephen Brookes. Lecture 4

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

CS70 - Lecture 6. Graphs: Coloring; Special Graphs. 1. Review of L5 2. Planar Five Color Theorem 3. Special Graphs:

Logic - CM0845 Introduction to Haskell

Applicative, traversable, foldable

The Worker/Wrapper Transformation

Higher Order Functions in Haskell

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Haskell Overview II (2A) Young Won Lim 8/23/16

(Refer Slide Time: 01.26)

CS 209 Functional Programming

Today. Types of graphs. Complete Graphs. Trees. Hypercubes.

CSE505, Fall 2012, Midterm Examination October 30, 2012

Shell CSCE 314 TAMU. Haskell Functions

Type families and data kinds

15 212: Principles of Programming. Some Notes on Continuations

1 The smallest free number

Induction Schemes. Math Foundations of Computer Science

Principles of Programming Languages

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

EDAA40 At home exercises 1

Homework: More Abstraction, Trees, and Lists

Introduction to Programming: Lecture 6

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

APCS-AB: Java. Recursion in Java December 12, week14 1

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter 12

EECS 140 Laboratory Exercise 5 Prime Number Recognition

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

Transcription:

Department of Computer Science, Australian National University COMP2600 / COMP6260 Formal Methods in Software Engineering Semester 2, 2016 Week 5 Tutorial Structural Induction You should hand in attempts to the questions indicated by (*) to your tutor at the start of each tutorial. Showing effort at answering the indicated questions will contribute to the 4% Tutorial Preparation component of the course; your attempts will not be marked for correctness. 1 Induction on Lists 1.1 An Easy One ( ) We are now all familiar with the append operator for joining two lists together. We would probably agree that it is associative: xs ++ (ys ++ zs) = (xs ++ ys) ++ zs Prove this property using structural induction. 1.2 Arguing by Cases The examples in the lecture all use a recipe where we simplify repeatedly using equations that come from the function definitions, until we prove the equality required. In the following example, you will need to do some case analysis in the proof. It is part of the exercise to work out what the cases are. elem z (xs ++ ys) = elem z xs elem z ys Prove this property of lists using structural induction. 1.3 A Really Hard One It may seem obvious, but when you define an operation reverse as follows, reverse [] = [] reverse (x : xs) = reverse xs ++ [x] it is hard to prove that: Structural Induction 1

reverse (reverse xs) = xs. Determine where the difficulty is. Can you find a way around the problem - a lemma, perhaps?.. or maybe a different definition of reverse. Does this definition of reverse contain more nested recursions than a more efficient definition? Will a proof about reverse correspondingly contain more nested inductions than a proof about a different definition of list reversal? Don t spend too long thinking about this one. 2 Induction on Trees ( ) 2.1 Reverse What does it mean to reverse a binary tree? The following is probably a definition that we could agree on: revt :: Tree a -> Tree a revt Nul = Nul -- T1 revt (Node x t1 t2) = Node x (revt t2) (revt t1) -- T2 Again we will expect that the following is true. So, prove it! Additionally, prove the following: revt (revt t) = t count(revt t) = count (t) 2.2 Flattening We can turn a tree into a list containing the same entries with the tail recursive function flat, where flat t acc returns the result of flattening the tree t, appended to the front of the list acc. Thus, for example, flat (Node 5 (Node 3 Nul Nul) (Node 6 Nul Nul)) [1,2] = [3,5,6,1,2] flat :: Tree a -> [a] -> [a] flat Nul acc = acc -- (F1) flat (Node a t1 t2) acc = flat t1 (a : flat t2 acc) -- (F2) We can get the sum of entries in a list by the function suml suml :: [Int] -> Int suml [] = 0 -- (S1) suml (x:xs) = x + suml xs -- (S2) Structural Induction 2

We can get the sum of entries in a tree by the function sumt sumt :: Tree Int -> Int sumt Nul = 0 -- (T1) sumt (Node n t1 t2) = n + sumt t1 + sumt t2 -- (T2) Prove by structural induction on the structure of the tree argument, that for all t and acc, suml (flat t acc) = sumt t + suml acc 3 Induction with Functions of Multiple Variables The two issues that often crop up when proving theorems about such functions are: It is often not clear what variable to do induction on. The beginner may not get the inductive hypothesis right. One has to remember that the other variables are still implicitly universally quantified. The following function is one that successively takes elements from the front of one list and puts them onto the front of a second list. slinky :: [a] -> [a] -> [a] slinky [] ys = ys -- S1 slinky (x:xs) ys = slinky xs (x:ys) -- S2 For example, (slinky [1,2] [3,4]) = [2,1,3,4]. Each of the following equations are theorems about the slinky function (a) slinky (slinky xs ys) zs = slinky ys (xs ++ zs) (b) slinky xs (slinky ys zs) = slinky (ys ++ xs) zs (c) slinky xs (ys ++ zs) = slinky xs ys ++ zs 3.1 Proving Property (a) Take it as given that we do induction on xs and check that this makes the base case is trivial. The step case is now slinky (slinky (x:xs) ys) zs = slinky ys ((x:xs) ++ zs) Attack the step case using an instance of ys, zs. slinky (slinky xs ys) zs = slinky ys (xs ++ zs) Structural Induction 3

3.2 Do one yourself Prove lemma (b). Prove lemma (c). 3.3 Reverse, again Can we use slinky to do the very hard Question 1.3? (Hint: describe, in words, what does slinky do?) Structural Induction 4

4 Appendix: Function definitions count :: Tree a -> Int count Nul = 0 -- C1 count (Node x t1 t2) = 1 + count t1 + count t2 -- C2 length :: [a] -> Int length [] = 0 -- L1 length (x:xs) = 1 + length xs -- L2 map :: (a -> b) -> [a] -> [b] map f [] = [] -- M1 map f (x:xs) = f x : map f xs -- M2 (++) :: [a] -> [a] -> [a] [] ++ ys = ys -- A1 (x:xs) ++ ys = x : (xs ++ ys) -- A2 elem :: Eq a => a -> [a] -> Bool elem y [] = False -- E1 elem y (x:xs) x == y = True -- E2 otherwise = elem y xs -- E3 ( ) :: Bool -> Bool -> Bool True _ = True -- O1 False x = x -- O2 reverse :: [a] -> [a] reverse [] = [] -- R1 reverse (x:xs) = reverse xs ++ [x] -- R2 Structural Induction 5