Program Calculus Calculational Programming

Similar documents
Mathematical Structures in Programming Introduction

FUNCTIONAL PEARLS The countdown problem

Lecture 4: Higher Order Functions

Shell CSCE 314 TAMU. Haskell Functions

CSCC24 Functional Programming Scheme Part 2

Programming Languages 3. Definition and Proof by Induction

1 Problem Description

CS 440: Programming Languages and Translators, Spring 2019 Mon

Programming Languages Third Edition

Lecture Notes on Data Representation

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Lecture 19: Functions, Types and Data Structures in Haskell

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

LECTURE 16. Functional Programming

LOGIC AND DISCRETE MATHEMATICS

1.3. Conditional expressions To express case distinctions like

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

Lecture 6: Sequential Sorting

Contents. Chapter 1 SPECIFYING SYNTAX 1

Week 5 Tutorial Structural Induction

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

Continuation Passing Style. Continuation Passing Style

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

Lists. Michael P. Fourman. February 2, 2010

Reasoning about programs. Chapter 9 of Thompson

Natural Numbers. We will use natural numbers to illustrate several ideas that will apply to Haskell data types in general.

3.7 Denotational Semantics

Exercise 1 ( = 22 points)

How invariants help writing loops Author: Sander Kooijmans Document version: 1.0

Denotational Semantics. Domain Theory

Discrete mathematics

CSCE 314 Programming Languages

Shell CSCE 314 TAMU. Higher Order Functions

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Accurate Step Counting

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

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS522 Programming Language Semantics

Functional Programming

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh

Scheme Quick Reference

Introduction to Functional Programming in Haskell 1 / 56

CSE505, Fall 2012, Midterm Examination October 30, 2012

An introduction introduction to functional functional programming programming using usin Haskell

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Semantics

n λxy.x n y, [inc] [add] [mul] [exp] λn.λxy.x(nxy) λmn.m[inc]0 λmn.m([add]n)0 λmn.n([mul]m)1

MPRI course 2-4 Functional programming languages Exercises

A Small Interpreted Language

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

Z Notation. June 21, 2018

Recursion. Lars-Henrik Eriksson. Functional Programming 1. Based on a presentation by Tjark Weber and notes by Sven-Olof Nyström

Derivation of a Pattern-Matching Compiler

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

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

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

CS 671, Automated Reasoning

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

Reverse Polish notation in constructing the algorithm for polygon triangulation

Complexity Theory. Compiled By : Hari Prasad Pokhrel Page 1 of 20. ioenotes.edu.np

Theorem Proving Principles, Techniques, Applications Recursion

Functional programming with Common Lisp

Introduction to Programming, Aug-Dec 2006

[Ch 6] Set Theory. 1. Basic Concepts and Definitions. 400 lecture note #4. 1) Basics

Programming Languages

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

Handout 9: Imperative Programs and State

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Lecture 1. 1 Notation

SAT-CNF Is N P-complete

15 212: Principles of Programming. Some Notes on Continuations

Chapter S:V. V. Formal Properties of A*

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

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

A GRAPH FROM THE VIEWPOINT OF ALGEBRAIC TOPOLOGY

Structural polymorphism in Generic Haskell

Computation with No Memory, and Rearrangeable Multicast Networks

9/19/12. Why Study Discrete Math? What is discrete? Sets (Rosen, Chapter 2) can be described by discrete math TOPICS

Part I Basic Concepts 1

n 1 x i = xn 1 x 1. i= (2n 1) = n 2.

STABILITY AND PARADOX IN ALGORITHMIC LOGIC

Formally-Proven Kosaraju s algorithm

THREE LECTURES ON BASIC TOPOLOGY. 1. Basic notions.

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Introduction to the Lambda Calculus

7. The Gauss-Bonnet theorem

Recursive Data and Recursive Functions

CMSC 330: Organization of Programming Languages. Operational Semantics

Lecture 6,

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

Discrete Mathematics Lecture 4. Harper Langston New York University

3.4 Deduction and Evaluation: Tools Conditional-Equational Logic

Foundations of Computation

Functional Programming in Haskell Part I : Basics

CS 310 Advanced Data Structures and Algorithms

Functional Programming in Haskell for A level teachers

CSE-321 Programming Languages 2012 Midterm

SOFTWARE ENGINEERING DESIGN I

Functional Programming Languages (FPL)

Transcription:

Program Calculus Calculational Programming National Institute of Informatics June 21 / June 28 / July 5, 2010 Program Calculus Calculational Programming

What we will learn? Discussing the mathematical structures in programs, and Program Calculus Calculational Programming

What we will learn? Discussing the mathematical structures in programs, and explaining how mathematical reasoning plays an important role in designing efficient and correct algorithms (programs). Program Calculus Calculational Programming

What we will learn? Discussing the mathematical structures in programs, and explaining how mathematical reasoning plays an important role in designing efficient and correct algorithms (programs). A new programming style: calculational programming Program Calculus Calculational Programming

Calculation is widely used in solving our daily problems, but its importance in programming has not been fully recognized. Program Calculus Calculational Programming

Tsuru-Kame-Zan An Overview The Tsuru-Kame Problem Some cranes (tsuru) and tortoises (kame) are mixed in a cage. Known is that there are 5 heads and 14 legs. Find out the numbers of cranes and tortoises. Program Calculus Calculational Programming

Tsuru-Kame-Zan An Overview The Tsuru-Kame Problem Some cranes (tsuru) and tortoises (kame) are mixed in a cage. Known is that there are 5 heads and 14 legs. Find out the numbers of cranes and tortoises. The kindergarten approach: plain simple enumeration! Crane 0, Tortoise 5... No. Crane 1, Tortoise 4... No. Crane 2, Tortoise 3... No. Crane 3, Tortoise 2... Yes! Crane 4, Tortoise 1... No. Crane 5, Tortoise 0... No. Program Calculus Calculational Programming

Tsuru-Kame-Zan An Overview The Tsuru-Kame Problem Some cranes (tsuru) and tortoises (kame) are mixed in a cage. Known is that there are 5 heads and 14 legs. Find out the numbers of cranes and tortoises. Elementary school: let s do some reasoning... If all 5 animals were cranes, there ought to be 5 2 = 10 legs. However, there are in fact 14 legs. The extra 4 legs must belong to some tortoises. There must be (14 10)/2 = 2 tortoises. So there must be 5 2 = 3 cranes. It generalises to larger numbers of heads and legs. Program Calculus Calculational Programming

Tsuru-Kame-Zan An Overview The Tsuru-Kame Problem Some cranes (tsuru) and tortoises (kame) are mixed in a cage. Known is that there are 5 heads and 14 legs. Find out the numbers of cranes and tortoises. Junior high school: algebra (theory of equation)! x + y = 5 2x + 4y = 14 It s a general approach applicable to many other problems and perhaps easier. Program Calculus Calculational Programming

Tsuru-Kame-Zan An Overview The Tsuru-Kame Problem Some cranes (tsuru) and tortoises (kame) are mixed in a cage. Known is that there are 5 heads and 14 legs. Find out the numbers of cranes and tortoises. rules and theories are useful for solving problems easily and systematically. Problems are declaratively specified. Implementation is hidden. Program Calculus Calculational Programming

Can we have a set of useful rules and theories in programming for developing correct and efficient algorithms (programs)? Program Calculus Calculational Programming

A Programming Problem Can you develop a correct linear-time program for solving the following problem? Maximum Segment Sum Problem Given a list of numbers, find the maximum of sums of all consecutive sublists. [ 1, 3, 3, 4, 1, 4, 2, 1] = 7 [ 1, 3, 1, 4, 1, 4, 2, 1] = 6 [ 1, 3, 1, 4, 1, 1, 2, 1] = 4 Program Calculus Calculational Programming

A Simple Solution An Overview 1 Enumerating all segments (segs); Program Calculus Calculational Programming

A Simple Solution An Overview 1 Enumerating all segments (segs); 2 Computing sum for each segment(sums); Program Calculus Calculational Programming

A Simple Solution An Overview 1 Enumerating all segments (segs); 2 Computing sum for each segment(sums); 3 Calculating the maximum of all the sums (max). Program Calculus Calculational Programming

A Simple Solution An Overview 1 Enumerating all segments (segs); 2 Computing sum for each segment(sums); 3 Calculating the maximum of all the sums (max). Program Calculus Calculational Programming

A Simple Solution An Overview 1 Enumerating all segments (segs); 2 Computing sum for each segment(sums); 3 Calculating the maximum of all the sums (max). Exercise How many segments does a list of length n have? Program Calculus Calculational Programming

A Simple Solution An Overview 1 Enumerating all segments (segs); 2 Computing sum for each segment(sums); 3 Calculating the maximum of all the sums (max). Exercise How many segments does a list of length n have? Exercise What is the time complexity of this simple solution? Program Calculus Calculational Programming

There indeed exists a clever solution! mss=0; s=0; for(i=0;i<n;i++){ s += x[i]; if(s<0) s=0; if(mss<s) mss= s; } x[i] 3 1 4 1 1 2 1 s 0 3 4 0 0 1 3 2 mss 0 3 4 4 4 4 4 4 Program Calculus Calculational Programming

There is a big gap between the simple and clever solutions! Program Calculus Calculational Programming

There is a big gap between the simple and clever solutions! Can we calculate the clever solution from the simple solution? Program Calculus Calculational Programming

There is a big gap between the simple and clever solutions! Can we calculate the clever solution from the simple solution? What rules and theorems are necessary to do so? Program Calculus Calculational Programming

There is a big gap between the simple and clever solutions! Can we calculate the clever solution from the simple solution? What rules and theorems are necessary to do so? How to apply the rules and theorems to do so? Program Calculus Calculational Programming

There is a big gap between the simple and clever solutions! Can we calculate the clever solution from the simple solution? What rules and theorems are necessary to do so? How to apply the rules and theorems to do so? Program Calculus Calculational Programming

Transformational Programming One starts by writing clean and correct programs, and then use program transformation techniques to transform them step-by-step to more efficient equivalents. Specificaiton: Clean and Correct programs Folding/Unfolding Program Transformation Efficient Programs Program Calculus Calculational Programming

Program Calculation Program calculation is a kind of program transformation based on Constructive Algorithmics, a framework for developing laws/rules/theories for manipulating programs. Program Calculus Calculational Programming

Program Calculation Program calculation is a kind of program transformation based on Constructive Algorithmics, a framework for developing laws/rules/theories for manipulating programs. Specificaiton: Clean and Correct programs Folding-free Program Transformation Efficient Programs Program Calculus Calculational Programming

References Richard Bird, Lecture Notes on Constructive Functional Programming, Technical Monograph PRG-69, Oxford University, 1988. Anne Kaldewaij, Programming: The Derivation of Algorithms, Prentice Hall, 1990. Richard Bird and Oege de Moor, The Algebra of Programming, Prentice-Hall, 1996. Roland Backhouse, Program Construction: Calculating Implementation from Specification, Wiley, 2003. Program Calculus Calculational Programming

National Institute of Informatics June 21 / June 28 / July 5, 2010

A specification describes what task an algorithm is to perform, expresses the programmers intent, should be as clear as possible.

A specification describes what task an algorithm is to perform, expresses the programmers intent, should be as clear as possible. An implementation describes how task is to perform, expresses an algorithm (an execution), should be efficiently done within the time and space available.

A specification describes what task an algorithm is to perform, expresses the programmers intent, should be as clear as possible. An implementation describes how task is to perform, expresses an algorithm (an execution), should be efficiently done within the time and space available. The link is that the implementation should be proved to satisfy the specification.

How to write a specification? By predicates: describing intended relationship between input and output of an algorithm.

How to write a specification? By predicates: describing intended relationship between input and output of an algorithm. By functions: describing straightforward functional mapping from input to output of an algorithm, which is executable but could be terribly inefficient.

Specifying Algorithms by Predicates (1/3) Specification: describing intended relationship between input and output of an algorithm.

Specifying Algorithms by Predicates (1/3) Specification: describing intended relationship between input and output of an algorithm. Example: increase The specification increase :: Int Int increase x > square x says that the result of increase should be strictly greater than the square of its input, where square x = x x.

Specifying Algorithms by Predicates (2/3) In this case, an Implementation is first given and then proved to satisfy the specification.

Specifying Algorithms by Predicates (2/3) In this case, an Implementation is first given and then proved to satisfy the specification. Example: increase (continue) One implementation is increase x = square x + 1 which can be proved by the following simple calculation. increase x = { definition of increase } square x + 1 > { arithmetic property } square x

Specifying Algorithms by Predicates (3/3) Exercise S1 Give another implementation of increase and prove that your implementation meets its specification.

Specifying Algorithms by Functions (1/3) Specification: describing straightforward functional mapping from input to output of an algorithm, which is executable but could be terribly inefficient.

Specifying Algorithms by Functions (1/3) Specification: describing straightforward functional mapping from input to output of an algorithm, which is executable but could be terribly inefficient. Example: quad The specification for computing quadruple of a number can be described straightforwardly by quad x = x x x x which is not efficient in the sense that multiplications are used three times.

Specifying Algorithms by Functions (2/3) With functional specification, we do not need to invent the implementation; just to improve specification via calculation.

Specifying Algorithms by Functions (2/3) With functional specification, we do not need to invent the implementation; just to improve specification via calculation. Example: quad (continue) We derive (develop) an efficient algorithm with only two multiplications by the following calcualtion. quad x = { specification } x x x x = { since x is associative } (x x) (x x) = { definition of square } square x square x = { definition of square } square (aquare x)

Specifying Algorithms by Functions (3/3) Exercise S2 Extend the idea in the derivation of efficient quad to develop an efficient algorithm for computing exp defined by exp(x, n) = x n.

Advantages of Functional Specification Functional specification is executable.

Advantages of Functional Specification Functional specification is executable. Functional specification is powerful to express intended mappings directly by functions or through their composition.

Advantages of Functional Specification Functional specification is executable. Functional specification is powerful to express intended mappings directly by functions or through their composition. Functional specification is suitable for reasoning, when functions used are well-structured with good algebraic properties.

Advantages of Functional Specification Functional specification is executable. Functional specification is powerful to express intended mappings directly by functions or through their composition. Functional specification is suitable for reasoning, when functions used are well-structured with good algebraic properties. In this course, we will consider functional specification.

Introduction to Bird Meertens Formalisms National Institute of Informatics June 21 / June 28 / July 5, 2010 Introduction to Bird Meertens Formalisms

Bird Meentens Formalisms (BMF) Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition BMF is a calculus of functions for people to derive programs from specifications: a range of concepts and notations for defining functions over lists; a set of algebraic laws for manipulating functions. Introduction to Bird Meertens Formalisms

A Problem An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Consider the following simple identity: (a 1 a 2 a 3 ) + (a 2 a 3 ) + a 3 + 1 = ((1 a 1 + 1) a 2 + 1) a 3 + 1 This equation generalizes in the obvious way to n variables a 1, a 2,..., a n, and we will refer to it as Horner e rule. Introduction to Bird Meertens Formalisms

A Problem An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Consider the following simple identity: (a 1 a 2 a 3 ) + (a 2 a 3 ) + a 3 + 1 = ((1 a 1 + 1) a 2 + 1) a 3 + 1 This equation generalizes in the obvious way to n variables a 1, a 2,..., a n, and we will refer to it as Horner e rule. How many are used in each side? Introduction to Bird Meertens Formalisms

A Problem An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Consider the following simple identity: (a 1 a 2 a 3 ) + (a 2 a 3 ) + a 3 + 1 = ((1 a 1 + 1) a 2 + 1) a 3 + 1 This equation generalizes in the obvious way to n variables a 1, a 2,..., a n, and we will refer to it as Horner e rule. How many are used in each side? Can we generalize to, + to? What are the essential constraints for and? Introduction to Bird Meertens Formalisms

A Problem An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Consider the following simple identity: (a 1 a 2 a 3 ) + (a 2 a 3 ) + a 3 + 1 = ((1 a 1 + 1) a 2 + 1) a 3 + 1 This equation generalizes in the obvious way to n variables a 1, a 2,..., a n, and we will refer to it as Horner e rule. How many are used in each side? Can we generalize to, + to? What are the essential constraints for and? Do you have suitable notation for expressing the Horner s rule concisely? Introduction to Bird Meertens Formalisms

Functions An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition A function f that has source type α and target type β is denoted by f : α β We shall say that f takes arguments in α and returns results in β. Function application is written without brackets; thus f a means f (a). Function application is more binding than any other operation, so f a b means (f a) b. Functions are curried and applications associates to the left, so f a b means (f a) b (sometimes written as f a b. Introduction to Bird Meertens Formalisms

Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Function composition is denoted by a centralized dot ( ). We have (f g) x = f (g x) Exercise: Show the following equation state that functional compositon is associative. (f ) (g ) = ((f g) ) Introduction to Bird Meertens Formalisms

Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Binary operators will be denoted by,,, etc. Binary operators can be sectioned. This means that ( ), (a ) and ( a) all denote functions. The definitions are: ( ) a b = a b (a ) b = a b ( b) a = a b Exercise: If has type : α β γ, then what are the types for ( ), (a ) and ( b) for all a in α and b in β? Introduction to Bird Meertens Formalisms

Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition The identity element of : α α α, if it exists, will be denoted by id. Thus, a id = id a = a Exericise: What is the identity element of functional composition? The constant values function K : α β α is defined by the equation K a b = a Introduction to Bird Meertens Formalisms

Lists An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Lists are finite sequence of values of the same type. We use the notation [α] to describe the type of lists whose elements have type α. Examples: [1, 2, 1] : [Int] [[1], [1, 2], [1, 2, 1]] : [[Int]] [ ] : [α] Introduction to Bird Meertens Formalisms

List Data Constructors Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition [ ] : [α] constructs an empty list. [.] : α [α] maps elements of α into singleton lists. [.] a = [a] The primitive operator on lists is concatenation, denoted by +. [1] ++ [2] ++ [1] = [1, 2, 1] Concatenation is associative: x ++ (y ++ z) = (x ++ y) ++ z Exercise: What is the identity for concatenation? Introduction to Bird Meertens Formalisms

Algebraic View of Lists Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition ([α], +, [ ]) is a monoid. ([α], +, [ ]) is a free monoid generated by α under the assignment [.] : α [α]. ([α] +, + ) is a semigroup. Introduction to Bird Meertens Formalisms

List Functions: s Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition A function h defined in the following form is called homomorphism: h [ ] = id h [a] = f a h (x ++ y) = h x h y It defines a map from the monoid ([α], +, [ ]) to the monoid (β, : β β β, id : β). Introduction to Bird Meertens Formalisms

List Functions: s Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition A function h defined in the following form is called homomorphism: h [ ] = id h [a] = f a h (x ++ y) = h x h y It defines a map from the monoid ([α], +, [ ]) to the monoid (β, : β β β, id : β). Property: h is uniquely determined by f and. Introduction to Bird Meertens Formalisms

Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition An Example: the function returning the length of a list. # [ ] = 0 # [a] = 1 # (x ++ y) = # x + # y Note that (Int, +, 0) is a monoid. Introduction to Bird Meertens Formalisms

Bags and Sets An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition A bag is a list in which the order of the elements is ignored. Bags are constructed by adding the rule that + is commutative (as well as associative): x ++ y = y ++ x A set is a bag in which repetitions of elements are ignored. Sets are constructed by adding the rule that + is idempotent (as well as commutative and associative): x ++ x = x Introduction to Bird Meertens Formalisms

Map An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition The operator (pronounced map) takes a function on lts left and a list on its right. Informally, we have f [a 1, a 2,..., a n ] = [f a 1, f a 2,..., f a n ] Formally, (f ) (or sometimes simply written as f ) is a homomorphism: f [ ] = [ ] f [a] = [f a] f (x ++ y) = (f x) ++ (f y) Map Distributivity: (f g) = (f ) (g ) Introduction to Bird Meertens Formalisms

Reduce An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition The operator / (pronounced reduce) takes an associative binary operator on lts left and a list on its right. Informally, we have /[a 1, a 2,..., a n ] = a 1 a 2 a n Formally, / is a homomorphism: /[ ] = id { if id exists } /[a] = a /(x ++ y) = ( /x) ( /y) Introduction to Bird Meertens Formalisms

Examples: An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition max : [Int] Int max = / where a b = if a b then b else a head : [α] + α head = / where a b = a last : [α] + α last = / where a b = b Introduction to Bird Meertens Formalisms

Promotion An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition f and / can be expressed as identities between functions. Empty Rules One-Point Rules Join Rules f K [ ] = K [ ] / K [ ] = id f [ ] = [ ] f / [ ] = id f ++ / = ++ / (f ) / ++ / = /.( /) Introduction to Bird Meertens Formalisms

An Example of Calculation Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition / f ++ / g = { map promotion } / ++ / f g = { reduce promotion } / ( /) f g = { map distribution } / ( / f g) Introduction to Bird Meertens Formalisms

Directed Reductions Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition We introduce two more computation patterns / (pronounced left-to-right reduce) and / (right-to-left reduce) which are closely related to /. Informally, we have / e [a 1, a 2,..., a n ] = ((e a 1 ) ) a n / e [a 1, a 2,..., a n ] = a 1 (a 2 (a n e)) Formally, we can define / e on lists by two equations. / e [ ] = e / e (x ++ [a]) = ( / e x) a Exercise: Give a formal definition for / e. Introduction to Bird Meertens Formalisms

Directed Reductions without Seeds Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition / [a 1, a 2,..., a n ] = ((a 1 a 2 ) ) a n / [a 1, a 2,..., a n ] = a 1 (a 2 (a n 1 a n )) Properties: ( / ) ([a] ++ ) = / a ( / ) (++ [a]) = / a Introduction to Bird Meertens Formalisms

An Example Use of Left-Reduce Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Consider the right-hand side of Horner s rule: (((1 a 1 + 1) a 2 + 1) + 1) a n + 1 This expression can be written using a left-reduce: / 1 [a 1, a 2,..., a n ] where a b = (a b) + 1 Exercise: Give the definition of such that the following holds. / [a 1, a 2,..., a n ] = (((a 1 a 2 +a 2 ) a 3 +a 3 ) +a n 1 ) a n +a n Introduction to Bird Meertens Formalisms

Accumulations An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition With each form of directed reduction over lists there corresponds a form of computation called an accumulation. These forms are expressed with the operators / (pronounced left-accumualte) and / (right-accumulate) and are defined informally by / e [a 1, a 2,..., a n ] = [e, e a 1,..., ((e a 1 ) ) a n ] / e [a 1, a 2,..., a n ] = [a 1 (a 2 (a n e)),..., a n e, e] Introduction to Bird Meertens Formalisms

Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Formally, we can define / e on lists by two equations by / e [ ] = [e] / e ([a] ++ x) = [e] ++ ( / e a x), or / e [ ] = [e] / e (x ++ [a]) = ( / e x) ++ [b a] where b = last( / e x). Introduction to Bird Meertens Formalisms

Efficiency in Accumulate Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition / e [a 1, a 2,..., a n ]: can be evaluated with n 1 calculations of. Exercise: Consider computation of first n + 1 factorial numbers: [0!, 1!,..., n!]. How many calculations of are required for the following two programs? 1 / 1 [1, 2,..., n] 2 fact [0, 1, 2,, n] where fact 0 = 1 and fact k = 1 2 k. Introduction to Bird Meertens Formalisms

Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Relation between Reduce and Accumulate / e = last / e / e = / [e] where x a = x ++ [last x a] Introduction to Bird Meertens Formalisms

Segments An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition A list y is a segment of x if there exists u and v such that x = u ++ y ++ v. If u = [], then y is called an initial segment. If v = [], then y is called an final segment. An Example: segs [1, 2, 3] = [[], [1], [1, 2], [2], [1, 2, 3], [2, 3], [3]] Exercise: How many segments for a list [a 1, a 2,..., a n ]? Introduction to Bird Meertens Formalisms

inits An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition The function inits returns the list of initial segments of a list, in increasing order of a list. inits [a 1, a 2,..., a n ] = [[ ], [a 1 ], [a 1, a 2 ],..., [a 1, a 2,..., a n ]] inits = ( + / [] ) [ ] Introduction to Bird Meertens Formalisms

tails An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition The function tails returns the list of final segments of a list, in decreasing order of a list. tails [a 1, a 2,..., a n ] = [[a 1, a 2,..., a n ], [a 2,..., a n ],..., [a n ], [ ]] tails = ( + / [] ) [ ] Introduction to Bird Meertens Formalisms

segs An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition segs = ++ / tails inits Exercise: Show the result of segs [1, 2]. Introduction to Bird Meertens Formalisms

Accumulation Lemma Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition ( / e ) = ( / e ) inits ( / ) = ( / ) inits + The accumulation lemma is used frequently in the derivation of efficient algorithms for problems about segments. On lists of length n, evaluation of the LHS requires O(n) computations involving, while the RHS requires O(n 2 ) computations. Introduction to Bird Meertens Formalisms

The Problem: Revisit Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Consider the following simple identity: (a 1 a 2 a 3 ) + (a 2 a 3 ) + a 3 + 1 = ((1 a 1 + 1) a 2 + 1) a 3 + 1 This equation generalizes in the obvious way to n variables a 1, a 2,..., a 2, and we will refer to it as Horner e rule. Can we generalize to, + to? What are the essential constraints for and? Do you have suitable notation for expressing the Horner s rule concisely? Introduction to Bird Meertens Formalisms

Horner s Rule An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition The following equation / / tails = / e where e = id a b = (a b) e holds, provided that distributes (backwards) over : for all a, b, and c. (a b) c = (a c) (b c) Introduction to Bird Meertens Formalisms

Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Exercise: Prove the correctness of the Horner s rule. Hints: Show that is equivalent to Show that satisfies the equations (a b) c = (a c) (b c) ( c) / = / ( c). f = / / tails f [ ] = e f (x ++ [a]) = f x a Introduction to Bird Meertens Formalisms

Generalizations of Horner s Rule Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Generalization 1: / / tails + = / where a b = (a b) b Introduction to Bird Meertens Formalisms

Generalizations of Horner s Rule Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Generalization 1: / / tails + = / where a b = (a b) b Generalization 2: / ( / f ) tails = / e where e = id a b = (a f b) e Introduction to Bird Meertens Formalisms

Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition The Maximum Segment Sum (mss) Problem Compute the maximum of the sums of all segments of a given sequence of numbers, positive, negative, or zero. mss [3, 1, 4, 1, 5, 9, 2] = 6 Introduction to Bird Meertens Formalisms

A Direct Solution An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition mss = / +/ segs Introduction to Bird Meertens Formalisms

Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Calculating a Linear Algorithm using Horner s Rule mss = { definition of mss } / +/ segs = { definition of segs } / +/ ++ / tails inits = { map and reduce promotion } / ( / +/ tails) inits = { Horner s rule with a b = (a + b) 0 } / / 0 inits = { accumulation lemma } / / 0 Introduction to Bird Meertens Formalisms

A Program in Haskell Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Exercise: Code the derived linear algorithm for mss in your favorite programming language. Introduction to Bird Meertens Formalisms

Segment Decomposition Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition The sequence of calculation steps given in the derivation of the mss problem arises frequently. The essential idea can be summarized as a general theorem. Theorem (Segment Decomposition) Suppose S and T are defined by S = / f segs T = / f tails If T can be expressed in the form T = h / e, then we have S = / h / e Introduction to Bird Meertens Formalisms

Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition National Institute of Informatics June 21 / June 28 / July 5, 2010

Longest Even Segment Problem Given is a sequence x and a predicate p. Required is an efficient algorithm for computing some longest segment of x, all of whose elements satisfy p. lsp even [3, 1, 4, 1, 5, 9, 2, 6, 5] = [2, 6]

s An Overview A homomorphism from a monoid (α,, id ) to a monoid (β,, id ) is a function h satisfying the two equations: h id = id h (x y) = h x h y

Lemma (Promotion) h is a homomorphism if and only if the following holds. h / = / h

Lemma (Promotion) h is a homomorphism if and only if the following holds. h / = / h Proof Sketch. : simple. : by induction.

Lemma (Promotion) h is a homomorphism if and only if the following holds. h / = / h Proof Sketch. : simple. : by induction. So we have f ++ / = ++ / f / ++ / = / ( /)

Characterization of s Lemma h is a homomorphism from the list monoid if and only if there exists f and such that h = / f

Proof : h = { definition of id } h id = { identity lemma (can you prove it?) } h ++ / [ ] = { h is a homomorphism } / h [ ] = { map distributivity } / (h [ ]) = { definition of h on singleton } / f

Proof (Cont.) An Overview : We reason that h = / f is a homomorphism by calculating h ++ / = { given form for h } / f ++ / = { map and reduce promotion } / ( / f ) = { hypothesis } / h

Examples of s #: compute the length of a list. # = +/ K 1

Examples of s #: compute the length of a list. # = +/ K 1 reverse: reverses the order of the elements in a list. Here, x y = y x. reverse = + / [ ]

sort: reorders the elements of a list into ascending order. sort = / [ ] Here, (pronounced merge) is defined by the equations: x [ ] = x [ ] y = y ([a] ++ x) ([b] ++ y) = [a] ++ (x ([b] ++ y)), if a b = [b] ++ (([a] ++ x) y), otherwise

all p: returns True if every element of the input list satisfies the predicate p. all p = / p

all p: returns True if every element of the input list satisfies the predicate p. all p = / p some p: returns True if at least one element of the input list satisfies the predicate p. some p = / p

split: splits a non-empty list into its last element and the remainder. split : [α] + ([α], α) split [a] = ([ ], a) split (x ++ y) = split x split y where (x, a) (y, b) = (x ++ [a] ++ y, b)

split: splits a non-empty list into its last element and the remainder. split : [α] + ([α], α) split [a] = ([ ], a) split (x ++ y) = split x split y where (x, a) (y, b) = (x ++ [a] ++ y, b) Note: split is a homomorphism on the semigroup ([α] +, + ).

split: splits a non-empty list into its last element and the remainder. split : [α] + ([α], α) split [a] = ([ ], a) split (x ++ y) = split x split y where (x, a) (y, b) = (x ++ [a] ++ y, b) Note: split is a homomorphism on the semigroup ([α] +, + ). Exercise: Let init = π 1 split, where π 1 (a, b) = a. Show that init is not a homomorphism.

All applied to An Overview The operator o (pronounced all applied to) takes a sequence of functions and a value and returns the result of applying each function to the value. Formally, we have [f, g,..., h] o a = [f a, g a,..., h a] [ ] o a = [ ] [f ] o a = [f a] (fs ++ gs) o a = (fs o a) ++ (gs o a) so, ( o a) is a homomorphism. Exercise: Show that [ ] = [id] o.

Conditional Expressions The conditional notation h x = f x, if p x = g x, otherwise will be written by the McCarthy conditional form: h = (p f, g)

Conditional Expressions The conditional notation h x = f x, if p x = g x, otherwise will be written by the McCarthy conditional form: h = (p f, g) Laws on Conditional Forms h (p f, g) = (p h f, h g) (p f, g) h = (p h f h, g h) (p f, f ) = f (Note: all functions are assumed to be total.)

Filter The operator (pronounced filter) takes a predicate p and a list x and returns the sublist of x consisting, in order, of all those elements of x that satisfy p. p = ++ / (p [id] o, [ ] o )

Filter The operator (pronounced filter) takes a predicate p and a list x and returns the sublist of x consisting, in order, of all those elements of x that satisfy p. p = ++ / (p [id] o, [ ] o ) Exercise: Prove that the filter satisfies the filter promotion property: (p ) ++ / = ++ / (p )

Filter The operator (pronounced filter) takes a predicate p and a list x and returns the sublist of x consisting, in order, of all those elements of x that satisfy p. p = ++ / (p [id] o, [ ] o ) Exercise: Prove that the filter satisfies the filter promotion property: (p ) ++ / = ++ / (p ) Exercise: Prove that the filter satisfies the map-filter swap property: (p ) f = f (p f )

Cross-product An Overview X is a binary operator that takes two lists x and y and returns a list of values of the form a b for all a in x and b in y. [a, b]x [c, d, e] = [a c, b c, a d, b d, a e, b e] Formally, we define X by three equations: xx [ ] = [ ] xx [a] = ( a) x xx (y ++ z) = (xx y) ++ (xx z) Thus xx is a homomorphism.

Properties [ ] is the zero element of X : We have cross promotion rules: [ ]X x = xx [ ] = [ ] f X + / = X + / f / X + / = X / (X /)

Example Uses of Cross-product cp: takes a list of lists and returns a list of lists of elements, one from each component. cp [[a, b], [c], [d, e]] = [[a, c, d], [b, c, d], [a, c, e], [b, c, e]] cp = X + / ([id] o )

subs: computes all subsequences of a list. subs [a, b, c] = [[], [a], [b], [a, b], [c], [a, c], [b, c], [a, b, c]] subs = X + / [[ ] o, [id] o ] o

subs: computes all subsequences of a list. subs [a, b, c] = [[], [a], [b], [a, b], [c], [a, c], [b, c], [a, b, c]] subs = X + / [[ ] o, [id] o ] o Note: subs = cp [[] o, [id] o ].

(all p) : (all even) [[1, 3], [2, 4], [1, 2, 3]] = [[2, 4]] (all p) = ++ / (X + / (p [[id] o ] o, [ ] o ) )

Selection Operators An Overview Suppose f is a numeric valued function. We want to define an operator f such that 1 f is associative, commutative and idempotent; 2 f is selective in that 3 f is maximizing in that x f y = x or x f y = y f (x f y) = f x f y

Selection Operators An Overview Suppose f is a numeric valued function. We want to define an operator f such that 1 f is associative, commutative and idempotent; 2 f is selective in that 3 f is maximizing in that x f y = x or x f y = y f (x f y) = f x f y Condition: f should be injective.

An Example: # But if f is not injective, then x f y is not specified when x y but f x = f y. [1, 2] # [3, 4]

An Example: # But if f is not injective, then x f y is not specified when x y but f x = f y. [1, 2] # [3, 4] To solve this problem, we may refine f to an injective function f such that f x < f y f x < f y.

An Example: # But if f is not injective, then x f y is not specified when x y but f x = f y. [1, 2] # [3, 4] To solve this problem, we may refine f to an injective function f such that f x < f y f x < f y. So we may select the lexicographically least sequence as the value of x # y when #x = #y.

In this case, + distributes through # : x ++ (y # z) = (x ++ y) # (x ++ z) (x # y) ++ z = (x ++ z) # (y ++ z) That is, (x ++ ) # / = # / (x ++ ) (++ x) # / = # / (++ x). We assume ω = # /[ ], satisfying #ω =. (ω is the zero element of + )

A short calculation An Overview # / (all p) = { definition before } # / ++ / (X + / (p [[id] o ] o, [ ] o ) ) = { reduce promotion } # / ( # / X + / (p [[id] o ] o, [ ] o ) ) = { + distributes over # } # / (++ / ( # /) (p [[id] o ] o, [ ] o ) ) = { many steps... } # / (++ / (p [id] o, K ω ) )

Existence of Existence Lemma The list function h is a homomorphism iff the implication h v = h x h w = h y h (v ++ w) = h (x ++ y) holds for all lists v, w, x, y.

Existence of Existence Lemma The list function h is a homomorphism iff the implication h v = h x h w = h y h (v ++ w) = h (x ++ y) holds for all lists v, w, x, y. Proof Sketch. : obvious by assuming h = / f.

Existence of Existence Lemma The list function h is a homomorphism iff the implication h v = h x h w = h y h (v ++ w) = h (x ++ y) holds for all lists v, w, x, y. Proof Sketch. : obvious by assuming h = / f. : Define by t u = h (g t ++ g u). for some g such that h = h g h (such a g exisits!). Thus h (x ++ y) = h x h y.

Reference Lemma For every computatble total function h with enumerable domain, there is a computatble (but possibly partial) function g such that h g h = h.

Reference Lemma For every computatble total function h with enumerable domain, there is a computatble (but possibly partial) function g such that h g h = h. Proof. Here is one suitable definition of g. g t = head [x h x = t] If t is in the range of h then this process terminates.

Specification of the Problem Recall the problem of computing the longest segment of a list, all of whose elements satisfied some given property p. lsp = # / (all p) segs

Specification of the Problem Recall the problem of computing the longest segment of a list, all of whose elements satisfied some given property p. lsp = # / (all p) segs Property: lsp is not a homomorphism.

Specification of the Problem Recall the problem of computing the longest segment of a list, all of whose elements satisfied some given property p. lsp = # / (all p) segs Property: lsp is not a homomorphism. This is because: does not imply lsp [2, 1] = lsp [2] = [2] lsp [4] = lsp[4] = [4] lsp ([2, 1] ++ [4]) = lsp ([2] ++ [4]).

Calculating a Solution to the Problem # / (all p) segs = { segment decomposition } # / ( # / (all p) tails) inits = { result before } # / ( # / (++ / (p [id] o, K ω ) ) tails) inits = { Horner s rule with x a = (x ++ (p a [a], ω) # [ ] } # / [ ] inits = { accumulation lemma } # / [ ]

Calculating a Solution to the Problem # / (all p) segs = { segment decomposition } # / ( # / (all p) tails) inits = { result before } # / ( # / (++ / (p [id] o, K ω ) ) tails) inits = { Horner s rule with x a = (x ++ (p a [a], ω) # [ ] } # / [ ] inits = { accumulation lemma } # / [ ] Exercise: Show the final program is linear in the number of calculation of p.

National Institute of Informatics June 21 / June 28 / July 5, 2010

The Minimax Problem Given is a list of lists of numbers. Required is an efficient algorithm for computing the minimum of the maximum numbers in each list. More succinctly, we want to compute as efficiently as possible. minimax = / /

Three Views of Lists Monoid View: every list is either (i) the empty list; (ii) a singleton list; or (iii) the concatenation of two (non-empty) lists. Snoc View: every list is either (i) the empty list; or (ii) of the form x ++ [a] for some list x and value a. Cons View: every list is either (i) the empty list; or (ii) of the form [a] ++ [x] for some list x and value a.

Three General Computation Forms Monoid View: homomorphism Snoc View: left reduction (foldl) / e [ ] = e / e (x ++ [a]) = ( / e x) a Cons View: right reduction (foldr) / e [ ] = e / e ([a] ++ x) = a ( / e x)

Loops and Left Reductions A left reduction / e x can be translated into the following program in a conventional imperative language. [ var a; a := e; for b in x do a := a oplus b; return a ]

Left Zeros Left reductions require that the argument list be traversed in its entirety. Such a traversal can be cut short if we recognize the possibility that an operator may have left zeros. ω is a left zero of if ω a = ω for all a.

Left Zeros Left reductions require that the argument list be traversed in its entirety. Such a traversal can be cut short if we recognize the possibility that an operator may have left zeros. ω is a left zero of if ω a = ω for all a. Exercise: Prove that if ω is a zero left of then / ω x = ω for all x. (by induction on snoc list x.)

Specialization Lemma Lemma Specialization Every homomorphism on lists can be expressed as a left (or also a right) reduction. More precisely, / f = / e where e = id a b = a f b

Specialization Lemma Lemma Specialization Every homomorphism on lists can be expressed as a left (or also a right) reduction. More precisely, / f = / e where e = id a b = a f b Exercise: Prove the specialization lemma.

Specialization Lemma Every homomorphism on lists can be expressed as a left (or also a right) reduction. More precisely, / f = / e where e = id a b = a f b Exercise: Prove the specialization lemma.

Minimax Let us return to the problem of computing minimax = / / efficiently. Using the specialization lemma, we can write minimax = / where is the identity element of /, and a x = a ( /x)

Since distributes through we have a x = / ((a ) x) Using the specialization lemma a second time, we have a x = a / x where b a c = b (a c)

Since distributes through we have a x = / ((a ) x) Using the specialization lemma a second time, we have a x = a / x where b a c = b (a c) Exercise: What are left zeros for a and?

An Efficient Implementation of minimax xs [ var a; a := infinity; for x in xs while a <> \infinity do a := a odot x; return a ] where the assignment a := a odot x can be implemented by the loop: [ var b; b := -infinity; for c in x while c <> a do b := b max (a min c); a := b ]