23
HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

Embed Size (px)

Citation preview

Page 1: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

HOW TO BEMORE PRODUCTIVE

Graham Hutton and Mauro Jaskelioff

Page 2: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

2

A stream is an infinite sequence of values:

The type of streams is co-inductively defined:

Streams

codata Stream A = A Stream A

0 1 2 3 4 ...

Page 3: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

3

Streams can be defined by recursive equations:

Defining Streams

ones :: Stream Nat

ones = 1 ones

nats :: Stream Nat

nats = 0 map (+1) nats

Page 4: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

4

This Talk

How do we ensure such equations make sense, i.e. that they produce well-defined streams?

A new approach, based upon a representation theorem for contractive functions.

loop :: Stream A

loop = tail loop

Page 5: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

5

Fixed Points

ones = 1 ones

ones = fix body

body xs = 1 xs

can be rewritten as:

fix f = f (fix f)

The starting point for our approach is the use ofexplicit fixed points. For example:

Page 6: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

6

The Problem

Given a function on streams

when does

makes sense, i.e. produce a well-defined stream?

f :: Stream A Stream A

fix f :: Stream A

Page 7: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

7

Adapting an idea from topology, let us say that afunction f on streams is contractive iff:

Equal for one

further element.

Equal for the first n elements

.

xs =n ys f xs =n+1 f ys

Contractive Functions

Page 8: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

8

Banach’s Theorem

Every contractive function

has a unique fixed point

f :: Stream A c Stream A

fix f :: Stream A

and hence produces a well-defined stream.

Page 9: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

9

This theorem provides a

semantic means of ensuring that

stream definitions are

valid.

Page 10: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

10

Example

The function (1 ) is contractive:

Hence, it has a unique fixed point, and

is a valid definition for a stream.

xs =n ys 1 xs =n+1 1 ys

ones = fix (1 )

Page 11: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

11

Example

The function tail is not contractive:

Hence, Banach’s theorem does not apply, and

is rejected as an invalid definition.

xs =n ys tail xs =n+1 tail ys

loop = fix tail

Page 12: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

12

Questions

Does the converse also hold - every function with a unique fixed point is contractive?

What does contractive actually mean?

What kind of functions are contractive?

Page 13: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

13

Key Idea

If we view a stream as a time-varying value

then a function on streams is contractive iff

x0 x1 x2 x3 x4 ...

Its output value at any time only dependson input values at strictly earlier times.

Page 14: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

14

This result simplifies the process of

deciding if a function is contractive.

Page 15: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

15

Examples

(1 )

tail

Each output depends on the input one step earlier in time.

Each output depends on the input one step later in time.

Page 16: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

16

Generating Functions

This idea is formalised using generating functions, which map finite lists to single values:

[A] B

All earlier input values.

The next output value.

Page 17: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

Representation Theorem

17

Every contractive function can be represented by a generating function, and vice versa:

Stream A c Stream B [A] B

gen

rep

Moreover, rep and gen form an isomorphism.

Page 18: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

18

This theorem provides a

practical means of producing

streams that are well-defined.

Page 19: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

Example

19

g :: [Nat] Nat

g [] = 1

g (x:xs) = x

ones :: Stream Nat

ones = fix (gen g)

Generator for ones.

Guaranteed to be well-defined.

Page 20: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

Example

20

g :: [Nat] Nat

g [] = 0

g (x:xs) = x+1

nats :: Stream Nat

nats = fix (gen g)

Generator for nats.

Guaranteed to be well-defined.

Page 21: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

Example

21

g :: [Nat] Nat

g [] = 0

g [x] = 1

g (x:y:xs) = x+y

fibs :: Stream Nat

fibs = fix (gen g)

Generator for fibs.

Guaranteed to be well-defined.

Page 22: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

22

Summary

Generating functions are a sound and complete representation of contractive functions;

Gives a precise characterisation of the class of functions that are contractive;

Provides a simple but rather general means of producing well-defined streams.

Page 23: HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff

23

Ongoing and Further Work

Generalisation to final co-algebras;

Other kinds of generating functions;

Relationship to other techniques;

Improving efficiency.