Outline. A recursive function follows the structure of inductively-defined data.

Similar documents
Induction. Induction. Induction. Induction. Induction. Induction 2/22/2018

The natural numbers. Definition. Let X be any inductive set. We define the set of natural numbers as N = C(X).

Structure. Background. them to their higher order equivalents. functionals in Standard ML source code and converts

Inductive Definitions with Inference Rules 1 / 27

2.7.1 Foundations of Proof Systems

Math 324 Summer 2012 Elementary Number Theory Notes on Mathematical Induction

Notes on Inductive Sets and Induction

Fall Lecture 5

Iris: Higher-Order Concurrent Separation Logic. Lecture 6: Case Study: foldr

Logic, Sets, and Proofs

Writing proofs for MATH 51H Section 2: Set theory, proofs of existential statements, proofs of uniqueness statements, proof by cases

Homework #2 Solutions Due: September 5, for all n N n 3 = n2 (n + 1) 2 4

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

Notes on Systems of Linear Congruences

Programming Language Concepts, cs2104 Tutorial 1. Answers

BASIC MATHEMATICAL TECHNIQUES

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

Proofs Regarding Specification of Spark Aggregation

Extending the Lambda Calculus: An Eager Functional Language

Solutions to Practice Final

CMSC 631 Program Analysis and Understanding Fall Type Systems

Outline. We will cover (over the next few weeks) Induction Strong Induction Constructive Induction Structural Induction

Recursion: Introduction and Correctness

CS411 Notes 3 Induction and Recursion

Course 311: Michaelmas Term 2005 Part III: Topics in Commutative Algebra

Mathematical Induction. How does discrete math help us. How does discrete math help (CS160)? How does discrete math help (CS161)?

BOREL SETS ARE RAMSEY

Consequences of Continuity

However another possibility is

Roy L. Crole. Operational Semantics Abstract Machines and Correctness. University of Leicester, UK

Any Wizard of Oz fans? Discrete Math Basics. Outline. Sets. Set Operations. Sets. Dorothy: How does one get to the Emerald City?

The Real Number System

This chapter contains a very bare summary of some basic facts from topology.

Discrete Mathematics Review

MAT246H1S - Concepts In Abstract Mathematics. Solutions to Term Test 1 - February 1, 2018

Accumulators. A Trivial Example in Oz. A Trivial Example in Prolog. MergeSort Example. Accumulators. Declarative Programming Techniques

Polynomial Factorization

COMP 204. Object Oriented Programming (OOP) Mathieu Blanchette

The integers. Chapter 3

Assignment 4 - Solution

Discrete Math in Computer Science Solutions to Practice Problems for Midterm 2

Grammars (part II) Prof. Dan A. Simovici UMB

Automated Reasoning Lecture 17: Inductive Proof (in Isabelle)

Show Your Work! Point values are in square brackets. There are 35 points possible. Some facts about sets are on the last page.

CmSc 250 Intro to Algorithms. Mathematical Review. 1. Basic Algebra. (a + b) 2 = a 2 + 2ab + b 2 (a - b) 2 = a 2-2ab + b 2 a 2 - b 2 = (a + b)(a - b)

Comment: The induction is always on some parameter, and the basis case is always an integer or set of integers.

Free Groups. Joachim Breitner. April 17, 2016

Abstract Measure Theory

Isomorphism of Finitary Inductive Types

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

We are going to discuss what it means for a sequence to converge in three stages: First, we define what it means for a sequence to converge to zero

Automata Theory and Formal Grammars: Lecture 1

Equations of Predicate Calculus

n CS 160 or CS122 n Sets and Functions n Propositions and Predicates n Inference Rules n Proof Techniques n Program Verification n CS 161

Generic Accumulations for Program Calculation

Math 127 Homework. Mary Radcliffe. Due 29 March Complete the following problems. Fully justify each response.

Chapter 1 The Real Numbers

2 Elementary number theory

Chapter 3. The Logic of Quantified Statements

Induction and recursion. Chapter 5

Today s Topics. Methods of proof Relationships to logical equivalences. Important definitions Relationships to sets, relations Special functions

Principles of Real Analysis I Fall I. The Real Number System

To hand in: (a) Prove that a group G is abelian (= commutative) if and only if (xy) 2 = x 2 y 2 for all x, y G.

Module 9: Mathematical Induction

USA Mathematical Talent Search Round 4 Solutions Year 20 Academic Year

Consequences of Continuity

1 Sequences and Summation

MATH 324 Summer 2011 Elementary Number Theory. Notes on Mathematical Induction. Recall the following axiom for the set of integers.

Examples for program extraction in Higher-Order Logic

Induction and Recursion

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

1. Problem I calculated these out by hand. It was tedious, but kind of fun, in a a computer should really be doing this sort of way.

UNIVERSITY OF MICHIGAN UNDERGRADUATE MATH COMPETITION 31 MARCH 29, 2014

Theorems for free : a (calculational) introduction

MATH 251, Handout on Sylow, Direct Products, and Finite Abelian Groups

Executable Transitive Closures of Finite Relations

Lecture Overview. 2 Weak Induction

Non-Axiomatic Logic (NAL) Specification. Pei Wang

CSE 20 DISCRETE MATH WINTER

Math 210 Exam 2 - Practice Problems. 1. For each of the following, determine whether the statement is True or False.

Types and Programming Languages (15-814), Fall 2018 Assignment 4: Data Representation (Sample Solutions)

Rectangles as Sums of Squares.

CS632 Notes on Relational Query Languages I

Review 3. Andreas Klappenecker

Automated Reasoning Lecture 5: First-Order Logic

Products of Factorials Modulo p

Clock Arithmetic. 1. If it is 9 o clock and you get out of school in 4 hours, when do you get out of school?

The Importance of Being Formal. Martin Henz. February 5, Propositional Logic

The Tychonoff Theorem

Math 40510, Algebraic Geometry

UNIVERSITY OF VICTORIA DECEMBER EXAMINATIONS MATH 122: Logic and Foundations

Math 105A HW 1 Solutions

NOTE: You have 2 hours, please plan your time. Problems are not ordered by difficulty.

Mathematical Induction

Minimally Strict Polymorphic Functions

Measures and Measure Spaces

Homework 1 Submission

What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency Asympt

Reasoning about Programs

Section 11.1 Sequences

Transcription:

Outline A recursive function follows the structure of inductively-defined data. With lists as our example, we shall study 1. inductive definitions (to specify data) 2. recursive functions (to process data) 3. frequent function templates

Specifying Types/Sets Extensional {n n is a multiple of 3} {p p has red hair} defined by giving characteristics no info about how to generate elements Intensional Let S be the smallest set of natural numbers satisfying 1. 0 S, 2. x + 3 S whenever x S. defined inductively describes how to generate elements

Why require smallest solution? Let S be a set of natural numbers satisfying 1. 0 S, 2. x + 3 S whenever x S. Which sets satisfy this specification? {0,3,6,9,...} {0,1,3,4,6,7,9,10,...}... By choosing the smallest solution, we get exactly those elements explicitly generated by the specification we can give a derivation showing why each element belongs in the set.

Derivation of Set Elements Let S be the smallest set of natural numbers satisfying 1. 0 S, 2. x + 3 S whenever x S. Example: 0 S (by rule 1) 3 S (by rule 2) 6 S (by rule 2) 9 S (by rule 2) Non-example: 10 Letting set be defined as the smallest gives us constructive information about the set.

BNF Inductive Integer lists: <i n t l i s t > ::= n i l <i n t > : : <i n t l i s t > Example: Derivation: 1 :: 2 :: 3 :: nil [1, 2, 3] nil is an <int-list> (by rule 1) 3 :: nil is an <int-list> (by rule 2) 2 :: 3 :: nil is an <int-list> (by rule 2) 1 :: 2 :: 3 :: nil is an <int-list> (by rule 2) Note: recursion in grammar each use of :: increases list length by 1

Approximating Recursion Grammar: <i n t l i s t > ::= n i l <i n t > : : <i n t l i s t > We write a family of functions list sum i, with i the length of the argument: fun l i s t s u m 0 ( l s ) = 0 ; fun l i s t s u m 1 ( l s ) = hd ( l s ) + l i s t s u m 0 ( t l ( l s ) ) ; fun l i s t s u m 2 ( l s ) = hd ( l s ) + l i s t s u m 1 ( t l ( l s ) ) ; fun l i s t s u m 3 ( l s ) = hd ( l s ) + l i s t s u m 2 ( t l ( l s ) ) ;... l i s t s u m 3 ( [ 1, 2, 3 ] ) ; v a l i t = 6 : i n t

Putting It Together We had fun l i s t s u m 0 ( l s ) = 0 ; fun l i s t s u m 1 ( l s ) = hd ( l s ) + l i s t s u m 0 ( t l ( l s ) ) ; fun l i s t s u m 2 ( l s ) = hd ( l s ) + l i s t s u m 1 ( t l ( l s ) ) ; fun l i s t s u m 3 ( l s ) = hd ( l s ) + l i s t s u m 2 ( t l ( l s ) ) ;... Recursive function: fun l i s t s u m ( l s ) = i f l s = n i l then 0 e l s e hd ( l s ) + l i s t s u m ( t l ( l s ) ) ;

Using For the grammar <i n t l i s t > ::= n i l <i n t > : : <i n t l i s t > we wrote fun l i s t s u m ( l s ) = i f l s = n i l then 0 e l s e hd ( l s ) + l i s t s u m ( t l ( l s ) ) ; but the correspondence is clearer by the ML patterns fun l i s t s u m ( l s ) = case l s of n i l => 0 ( n : : ns ) => n + l i s t s u m ( ns ) ; or even better fun l i s t s u m ( n i l ) = 0 l i s t s u m ( n : : ns ) = n + l i s t s u m ( ns ) ;

Recursion Template Grammar: Data Structure directs Function Structure <i n t l i s t > ::= n i l <i n t > : : <i n t l i s t > Template: fun l i s t r e c ( n i l ) =.... l i s t r e c ( n : : ns ) =.... l i s t r e c ( ns )..... ; Key points: for each case in BNF there is a case in function recursion occurs in function exactly where recursion occurs in BNF we may assume function works for sub-structures of the same type

More Examples Add one to each element of list: fun l i s t i n c ( n i l ) = n i l l i s t i n c ( n : : ns ) = ( n + 1 ) : : l i s t i n c ( ns ) ; Select those elements greater than five: fun g t f i v e ( n i l ) = n i l g t f i v e ( n : : ns ) = i f n > 5 then n : : g t f i v e ( ns ) e l s e g t f i v e ( ns ) ; Append two lists: fun append ( n i l, l 2 ) = l 2 append ( n : : ns, l 2 ) = n : : append ( ns, l 2 ) ;

Adding one to each element of list: fun l i s t i n c ( n i l ) = n i l l i s t i n c ( n : : ns ) = ( n + 1 ) : : l i s t i n c ( ns ) ; Generalization: apply arbitrary function to each element fun l i s t m a p f n i l = n i l l i s t m a p f ( n : : ns ) = f ( n ) : : l i s t m a p f ns ; Type of list map: fn : ( a -> b) -> a list -> b list Instantiation: add one to each element v a l m y l i s t i n c = l i s t m a p ( fn x => x + 1 ) ; Instantiation: square each element v a l s q u a r e l i s t = l i s t m a p ( fn x => x x ) ;

Selecting only the elements greater than five: fun g t f i v e ( n i l ) = n i l g t f i v e ( n : : ns ) = i f n > 5 then n : : g t f i v e ( ns ) e l s e g t f i v e ( ns ) ; Generalization: select using arbitrary predicate fun l i s t f i l t e r p n i l = n i l l i s t f i l t e r p ( n : : ns ) = i f p ( n ) then n : : l i s t f i l t e r p ns e l s e l i s t f i l t e r p ns ; Type of list filter: ( a -> bool) -> a list -> a list Instantiation: select those greater than five v a l m y g t f i v e = l i s t f i l t e r ( fn n => n > 5 ) ; Instantiation: select the even elements v a l e v e n s = l i s t f i l t e r ( fn n => n mod 2 = 0 ) ;

r ing all elements up by adding them fun l i s t s u m ( n i l ) = 0 l i s t s u m ( n : : ns ) = n + l i s t s u m ( ns ) ; Generalization: fold in arbitrary way fun f o l d r f e n i l = e f o l d r f e ( x : : xs ) = f ( x, ( f o l d r f e xs ) ) Type of foldr: ( a * b -> b) -> b -> a list -> b Instantiation: my addlist fun m y a d d l i s t xs = f o l d r op+ 0 xs Instantiation: my identity fun m y i d e n t i t y xs = f o l d r op : : n i l xs Instantiation: my append fun my append xs ys = f o l d r op : : ys xs

l Recall foldr, processing input from right: fun f o l d r f e n i l = e f o l d r f e ( x : : xs ) = f ( x, ( f o l d r f e xs ) ) : ( a b > b ) > b > a l i s t > b Now consider foldl, processing input from left: fun f o l d l f e n i l = e f o l d l f e ( x : : xs ) = f o l d l f ( f ( x, e ) ) xs Type of foldl: ( a * b -> b) -> b -> a list -> b Example instantiation: foldl op:: nil xs which reverses a list.

List Representation of Sets Sets may be represented as lists + easy to code Testing membership:? with or without duplicates - not optimal for big sets member [ 3, 6, 8 ] 4 ; v a l i t = f a l s e : b o o l member [ 3, 6, 8 ] 6 ; v a l i t = t r u e : b o o l Coding member: fun member n i l x = f a l s e member ( y : : ys ) x = i f x = y then t r u e e l s e member ys x ;

fun member n i l x = f a l s e member ( y : : ys ) x = i f x = y then t r u e e l s e member ys x ; Type of member: member = fn : a l i s t > a > b o o l Here double primes denotes an equality type. member [ fn x => x+2, fn x => x +1] ( fn x => x +1);.. E r r o r : o p e r a t o r and operand don t a g r e e [ e q u a l i t y type r e q u i r e d ] o p e r a t o r domain : Z l i s t operand : ( i n t > i n t ) l i s t because functions cannot be tested for equality

Set Operations Intersection: fun i n t e r s e c t ( [ ], ys ) = [ ] i n t e r s e c t ( x : : xs, ys ) = i f member ys x then x : : i n t e r s e c t ( xs, ys ) e l s e i n t e r s e c t ( xs, ys ) ; Type of intersection: a list * a list -> a list Union, with type a list * a list -> a list fun union ( [ ], ys ) = ys union ( x : : xs, ys ) = i f member ys x then union ( xs, ys ) e l s e x : : union ( xs, ys ) ;

Removing Duplicates fun remove dups [ ] = [ ] remove dups ( x : : xs ) = i f member xs x then remove dups xs e l s e x : : remove dups xs ; with type a list -> a list

Association Lists We often want to associate keys with values. One way to do so is to maintain a list of pairs (key,value). + easy to code - not optimal for big sets We want to write a lookup function Input an association list, and a key Output the value corresponding to the key fun lookup ( ( y, v ) : : ds ) x = i f x = y then v e l s e lookup ds x lookup n i l x =???

Variants of Lookup We may need to go for some rather arbitrary value that signals unsuccessful search: fun lookup ( ( y, v ) : : ds ) x = i f x = y then v e l s e lookup ds x lookup n i l x = 1 Type of lookup: ( a * int) list -> a -> int We thus lose some polymorphism. Instead, we may write fun lookup n i l x = NONE lookup ( ( y, v ) : : ds ) x = i f x = y then SOME v e l s e lookup ds x Type of lookup: ( a * b) list -> a -> b option