HOW TO BEMORE 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 ...
3
Streams can be defined by recursive equations:
Defining Streams
ones :: Stream Nat
ones = 1 ones
nats :: Stream Nat
nats = 0 map (+1) nats
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
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:
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
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
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.
9
This theorem provides a
semantic means of ensuring that
stream definitions are
valid.
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 )
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
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?
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.
14
This result simplifies the process of
deciding if a function is contractive.
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.
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.
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.
18
This theorem provides a
practical means of producing
streams that are well-defined.
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.
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.
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.
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.
23
Ongoing and Further Work
Generalisation to final co-algebras;
Other kinds of generating functions;
Relationship to other techniques;
Improving efficiency.