230 likes | 473 Views
HOW TO BE MORE PRODUCTIVE. Graham Hutton and Mauro Jaskelioff. Streams. A stream is an infinite sequence of values:. 0 1 2 3 4 . The type of streams is co-inductively defined:. codata Stream A = A Stream A . Defining Streams.
E N D
HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff
Streams A stream is an infinite sequence of values: 0 1 2 3 4 ... The type of streams is co-inductively defined: codata Stream A = A Stream A
Defining Streams Streams can be defined by recursive equations: ones :: Stream Nat ones = 1 ones nats :: Stream Nat nats = 0 map (+1) nats
This Talk • How do we ensure such equations make sense, i.e. that they produce well-defined streams? loop :: Stream A loop = tail loop • A new approach, based upon a representation theorem for contractive functions.
Fixed Points The starting point for our approach is the use of explicit fixed points. For example: ones = 1 ones can be rewritten as: ones = fix body body xs = 1 xs fix f = f (fix f)
The Problem Given a function on streams f :: Stream A Stream A when does fix f :: Stream A makes sense, i.e. produce a well-defined stream?
Contractive Functions Adapting an idea from topology, let us say that a function f on streams is contractive iff: xs =n ys f xs =n+1 f ys Equal for the first n elements. Equal for one further element.
Banach’s Theorem Every contractive function f :: Stream A cStream A has a unique fixed point fix f :: Stream A and hence produces a well-defined stream.
This theorem provides a semantic means of ensuring that stream definitions are valid.
Example The function (1 ) is contractive: 1 xs =n+1 1 ys xs =n ys Hence, it has a unique fixed point, and ones = fix (1 ) is a valid definition for a stream.
Example The function tail is not contractive: tail xs =n+1 tail ys xs =n ys Hence, Banach’s theorem does not apply, and loop = fix tail is rejected as an invalid definition.
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?
Key Idea If we view a stream as a time-varying value x0 x1 x2 x3 x4... then a function on streams is contractive iff Its output value at any time only depends on input values at strictly earlier times.
This result simplifies the process of deciding if a function is contractive.
Examples Each output depends on the input one step earlier in time. (1 ) Each output depends on the input one step later in time. tail
Generating Functions This idea is formalised using generating functions, which map finite lists to single values: [A] B The next output value. All earlier input values.
Representation Theorem Every contractive function can be represented by a generating function, and vice versa: rep Stream A cStream B [A] B gen Moreover, rep and gen form an isomorphism.
This theorem provides a practical means of producing streams that are well-defined.
Example g :: [Nat] Nat g [] = 1 g (x:xs) = x Generator for ones. Guaranteed to be well-defined. ones :: Stream Nat ones = fix (gen g)
Example g :: [Nat] Nat g [] = 0 g (x:xs) = x+1 Generator for nats. Guaranteed to be well-defined. nats :: Stream Nat nats = fix (gen g)
Example g :: [Nat] Nat g [] = 0 g [x] = 1 g (x:y:xs) = x+y Generator for fibs. Guaranteed to be well-defined. fibs :: Stream Nat fibs = fix (gen g)
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.
Ongoing and Further Work • Generalisation to final co-algebras; • Other kinds of generating functions; • Relationship to other techniques; • Improving efficiency.