The continuity of monadic stream functions

Brouwer's continuity principle states that all functions from infinite sequences of naturals to naturals are continuous, that is, for every sequence the result depends only on a finite initial segment. It is an intuitionistic axiom that is incompatible with classical mathematics. Recently Martín Escardó proved that it is also inconsistent in type theory.


I. INTRODUCTION
Brouwer's continuity principle is a non-standard intuitionistic postulate, incompatible with classical mathematics.It states that every function from infinite sequences of natural numbers to natural numbers is continuous.Continuity, in this setting, means that the value of the function on each specific input depends only on a finite initial segment of the sequence.
We denote an arbitrary sequence by α = a 0 a 1 a 2 • • • .We write α| n for the initial segment consisting of the first n elements: α| n = a 0 :: a 1 :: . . .:: a n−1 :: nil.(We use the type theoretic notation that we introduce formally later: infinite sequences, or streams, form a coinductive type with constructor ; finite sequences, or lists, form an inductive type with constructor :: and an empty list base case nil.) We say that two streams α and α are n-equal if their first n elements are the same: α = n α if α | n = α| n .Brouwer's principle is expressed by the following formula: ∀α, ∃n, ∀α , α = n α ⇒ f α = f α.
It states that the value of f on any input α depends only on an initial segment α| n , so that on any other sequence α with the same initial n elements, f will produce the same result.
The justification that Brouwer gave for the principle rests on his philosophy of mathematics, specifically on his interpretation of the meaning of infinite sequence and function.The objects on which the functions operate are choice sequences, progressions of values that are completely free and not governed to a generating rule.They may be produced by a creative subject and are not necessarily algorithmic.On the contrary, functions are effective procedures, consisting of precise mental steps.A function can consult its sequence argument one element at a time and must algorithmically compute a result in a finite time.It follows that a function can only obtain a finite number of sequence elements in the time it takes it to produce the result.Hence, it must be continuous.
The relevance of intuitionistic mathematics to modern computer science rests on the parallel between the philosophical apprehension of mathematical objects as mental constructions and their computational realization as data structures and programs.A function is now a computational procedure.Brouwer's choice sequences can be reinterpreted as input streams.These need not be data structures implemented on a computer, but can be progressions of input values read from some device.
The setup of Brouwer's principle can be reformulated thus: We have an interactive program that can ask the user to insert a value at any point of its computation; after a finite number of steps, the program must end and produce a result.It is not essential to think that the sequence is provided by a user; in scientific and real-world applications we may think of the sequence as produced by a measuring device or by any other signalling process.There is no predefined limit to the number of input values that the program will ask for, but it can only get a finite number of them if it needs to terminate.Therefore the program is the realization of a continuous function in Brouwer's sense.
The most coherent and complete realization of the correspondence between intuitionistic mathematics and computer science is in Martin-Löf's Type Theory.It is, at the same time, a programming language and a formal system for the foundations of mathematics.It is compatible with both intuitionistic and classical mathematics.It has been very successful and led to concrete implementations, notably the systems Coq [26] and Agda [23], and useful applications.
We may now ask if the theory can be extended with stronger constructive principles, specifically if we can add the continu-978-1-5090-3018-7/17/$31.00 c 2017 IEEE ity principle to it.Unfortunately not: Recently Martín Escardó discovered that the straightforward addition of the continuity principle to type theory leads to a contradiction [11].In the aftermath of the discovery, discussion focused on analyzing the source of the problem and investigating alternative formulations of the principle that are not lethal.One solution, proposed by Escardó himself, is to adopt a weaker notion of existential quantifier.One crucial point in his paradox was that we can use the constructive content of the existential quantification on the length n of the initial segment to construct a function that turns out not to be continuous.By weakening the existential quantifier, so that no extraction of a witness is allowed, we prevent the construction of such an evil function.
Here we propose an alternative formulation, based on a different diagnosis of the paradox.The construction of the evil function has an input sequence as a parameter.But in Brouwer's conception there is a clear distinction between sequences, which are non-computable, and functions, which must be effectively computable.Therefore we should not allow the definition of a function to depend on a sequence.However, in type theory all objects are intended to be internal to the theory itself and we are authorized to use any object in the definition of another.Specifically, infinite sequences are realized as functions on the natural numbers.A sequence of natural numbers is just a function α : N → N. If we construe functions as computable, which is essential for the justification of the continuity principle, then so are infinite sequences, contrary to the spirit of the principle.
We may substitute the representation of sequences as functions N → N with a coinductive type of streams S A .An element α : S A is built by using the constructor an infinite number of times.(Corecursion patterns tell us how we can generate the infinite sequence by a finite process.) There is a correspondence between S A and N → A, which is one-to-one if we assume extensionality of functions and bisimilarity of streams: functions are equal if they are pointwise equal, streams are equal if they can simulate each other.Therefore, the change of data type does not in itself solve the issue.But it affords a way to generalize the notions.
A stream, as defined above, is still an internal object of type theory.It is meant to be defined by some computational criterion: it could be generated by a coalgebra [18], characterized as the fixed point of a guarded equation [8], [16], or produced by a guarded-by-destructors pattern [1].
We look for a notion of stream that comprises other ways of generating the sequence of elements, in particular allowing them to be given interactively.In functional programming, specifically in the language Haskell, interactive programming is realized by using the IO monad.While a basic type A contains pure elements which are immutable data structures, when we put it inside the IO monad we obtain an interactive type (IO A) whose elements are values of A produced through interaction and producing side effects.Other monads characterize different kinds of side effects.
We define a notion of stream that embodies the possibility of its elements being produced through monadic actions.The coinductive type S M,A , for a given monad M , has elements of the form mcons m, where m is an M -action, m : M (A × S M,A ).This action, when executed, will produce some side effects and give results consisting of an element of A and a new monadic stream.For example, if M is the IO monad, there will be some interaction with the user to obtain the first element of the stream and the tail, which is again a monadic stream that results in a new IO action.Other monads result in different stream behaviours: the Maybe monad admits the possibility of the action not giving a value, thus allowing the sequence to terminate; the list monad allows the stream to branch into many possible continuations; the state monad allows the elements to depend on and modify a changing state, the writer monad gives streams that, when read, produce some output; and so on.Now we come to the characterization of functions on streams.We want a function to operate independently of how the stream is produced.It shouldn't matter if the stream is a pure internal procedure or if it is an interactive process or if it produces any other side effects.In other words, functions should apply to monadic streams and be polymorphic on the monad.Therefore, the type of functions we are interested in is The variable M ranges over monads.A function f of this type should operate in a uniform way independently of the monad.We make this notion precise through a naturality condition.A different way to characterize uniformity is through parametricity [3].Naturality is weaker.Its advantages are that it has a more abstract and clear formulation and results in stronger properties about the function.Every natural monadic stream function is continuous: Brouwer's Principle becomes a theorem.

II. MONADIC STREAMS
Coinductive types are data structures that contain non-wellfounded elements (See Chapter 13 of the book by Bertot and Casteran [4] for a good introduction).They have their roots in the categorical theory of final coalgebras and have been implemented in the major type theoretic systems, Coq, Agda and Idris [5].At present, their understanding still suffers from tension between the abstract theory of coalgebras, with its simple characterization by finality, and the formal implementation, with its syntactic conditions [21].The best synthesis so far is probably in the technique of copatterns [1], which offers an easy syntactic format that transparently mirrors coalgebraic definitions.For our purposes, we skip over syntactic details and issues of unicity, decidability and extensionality.
We use an Agda-style notation, with the key words data and codata marking inductive and coinductive types.As simple examples of both, here are the definitions of the types of lists and of pure streams.
data List(A) : Set nil : List(A) (:: Elements of data types are built bottom-up using constructors in a well-founded way.It is necessary to have a nonrecursive constructor, nil in this case, to provide a basis for the manufacture of lists.We can define them by directly giving their structure, for example 2::3::5::7::nil.We also use typical list notation e.g. the above list can be written [2,3,5,7]. Elements of codata types are built top-down, and there may not be a bottom.We can apply the constructors in a non-well-founded way.We do not need a non-recursive base constructor (but we may have one).Since the structure of coinductive objects can be infinite, we cannot usually define them by directly giving their components.Instead, we use recursive definitions that generate the streams step by step when we unfold them.
Both inductive and coinductive types are fixed points of functors.For the definition to make constructive sense in type theory, the functor must be strictly positive, that is, in its syntactic form the argument type must occur only on the right-hand side of functional type formers.A more elegant, less syntax-bound characterization is the notion of container [2] (or dependent polynomial functor [12] in the categorical literature).Definition 1.A container is a pair S, P with S : Set, a set of shapes, and P : S → Set, a family giving a set of positions for every shape.Every container defines a functor: (S P ) : Set → Set (S P ) X = Σs : S. P s → X.
An element of (S P ) X is a pair s, xs where s : S is a shape and xs : P s → X is a function assigning an element of X to every position in the shape s.
The carrier of the final coalgebra of a container is a type ν(S P ) inhabited by trees with nodes labelled by shapes s : S and branches labelled by the positions (P s) of the node shape.So every element of t : ν(S P ) is uniquely given by a shape, shape t : S, and a family of sub-elements, subs t : P (shape t) → ν(S P ).
The actual final coalgebra is the function out ν : ν(S P ) → (S P ) (ν(S P )) out ν t = shape t, subs t .
Using this terminology and notation, streams can be defined by, S A = ν(A λa.1).So streams are the final coalgebra of the container with a shape for every element of A and a single position in each shape.We will continue to use the more intuitive codata formalism.The correspondence with the ν formalism should be evident in every case.
Once we defined the coinductive type, we need a formalism to program with it.Categorically, coinductive types are final coalgebras.We can use their universal property as a definitional scheme: Every coalgebra c : X → (S P ) X has a unique anamorphism ĉ : X → ν(S P ) such that out ν • ĉ = (S P ) ĉ • c. ν(S P ) outν / / (S P ) (ν(S P )) Since (S P ) X = Σs : S. P s → X, the coalgebra c has two components c = c S , c P with c S : X → S and c P : (x : X) → (P (c S x)) → X.The commutativity of the diagram can then be expressed by the two equations shape (ĉ x) = c S x subs (ĉ x) p = ĉ (c P x p).
We will continue to use the more intuitive recursive formalism.The correspondence with coalgebraic definitions should be evident in every case.
The only coinductive structure we are interested in here is that of streams.However, we want to generalize it to monadic streams, in which the constructor shields the head and tail behind a monadic action.The justification of such data formation requires the full range of final coalgebras for containers.Given a monad M , the type of monadic streams on M is defined as follows: Categorically, we can see this type as the final coalgebra of the functor F X = M (A × X).For it to make constructive sense, it should be strictly positive, so we must put it in container form.This is possible only if M itself is a container.Not all monads are; for example the continuation monad is not strictly positive.If M is itself a container, M = S M P M , the above functor F can also be presented as a container with shapes S F = Σs : S M .P M → A and positions P F s, h = P M s.The shapes of F are shapes of M with the positions ornamented [22] by elements of A. From now on we always assume that the monad M is a container.(Thorsten Altenkirch, in a personal communication, showed that monad containers are exactly type universes closed under lax sum types.)Pure streams are monadic streams for the identity monad Id: the type of mcons Id is isomorphic to that of ( ) by currying: Interesting instantiations are obtained by using other monads.Some of them are important in later sections.If we choose the Maybe monad, we obtain co-lists, sequences of elements that may or may not be finite.
The elements of Maybe X are copies of each element x : X, Just x, and an error element Nothing.When we instantiate the definition of monadic streams with Maybe we obtain the type S Maybe,A , with two distinct ways to construct streams (although there is only one constructor) according to the monadic action.If the monadic action is Nothing, we get an empty stream object: nil = mcons Maybe Nothing.If the monadic action is Just, we get a head element and a tail: a α = mcons Maybe (Just a, α ).Both finite lists and pure streams can be injected in S Maybe,A .A list [a 0 , • • • , a n ] is represented as mcons (Just a 0 , . . .mcons (Just a n , mcons Nothing ) . . .).
The list constructor is itself a monad, so it makes sense to consider S List,A .This turns out to be the set of non-well-founded finitely branching trees with edges labelled by elements of A. An element of it has the form mcons [ a 0 , α 0 , a 1 , α 1 , a 2 , α 2 , • • • ] where a 0 , a 1 , a 2 are elements of A and α 0 , α 1 , α 2 are monadic streams in S List,A .
Another interesting instantiation uses the state monad.This characterizes computations whose side effects consist in reading and modifying a state value in some type S: State S X = S → X × S. A monadic action of type X reads the present state and produces a result in X and a new state.A monadic stream in S State S ,A is an infinite sequence of values such that the evaluation of each component depends on and modifies the current state.An element of it has the form mcons h, where h : S → A × S State,A × S. As a simple example, here is the state-monadic stream of Fibonacci numbers.
fib gen : This is in fact a constant stream, in the sense that it recursively calls itself with no variation.It generates a dynamically changing stream when we execute it with a varying state containing the pair of the last two Fibonacci numbers we computed.
runstr : in a (runstr α s ) fib : S N fib = runstr fib gen 0, 1 A special case of the state monad is the writer monad.It is a state monad in which the monadic actions only write into the state, they never read it.The state space itself is a monoid I, e, * : the initial state is the unit e and each action produces a value in I that is inserted into the state by the operation * .Formally, Writer I X = X × I. Elements of S Writer I ,A are essentially streams of pairs a 0 , i 0 a 1 , i 1 a 2 , i 2 • • • where a n : A and i n : I for every n.(Formally, the order of the arguments is different, because of the way the products associate: α = mcons a 0 , (mcons a 1 , (mcons a 2 , . . ., i 2 ) , i 1 ) , i 0 ).The intuitive idea is that the evaluation of consecutive elements of the stream will generate successive states i 0 , i 0 * i 1 , i 0 * i 1 * i 2 , and so on.
Remark on Monad Notation: We use return/bind notation for monads.The operation return lifts a pure value a : A into the monad, (return a) : M A, and the bind operation takes a monadic action m : M A along with a function f : A → M B and binds the results of the action to the function (m > >= f ) : M B. We also use do notation which is a convenient syntax for expressing bind operations.A do block contains a sequence of bindings and expressions, resulting in a monadic value.So, for example:

III. PURE FUNCTIONS
The main subject of this article is the study of pure functions on streams, where pure means that the operations of the function do not depend on how the stream is produced.Therefore we require these functions to operate on monadic streams and be polymorphic on the monad.We impose a naturality condition that specifies that the function works in the same way independently of the monad: it must be the same function instantiated to each monad, rather than a collection of different functions, each specifically defined for a particular monad.
Definition 2. Let M 0 , M 1 be two monads with respective operators return 0 , > >= 0 and return 1 , > >= 1 .A monad morphism is a natural transformation, φ : M 0 .→ M 1 , that respects the monad operations by satisfying the following laws: We want to extend the notion of naturality to monadic stream functions.In order to do this, we must lift monad morphisms to streams, by applying them to every action in the stream.Definition 3. Given a monad morphism φ : M 0 .→ M 1 , its lifting to streams is a family of morphisms on monadic streams: where φA : This is a typical corecursive definition: we give equations for a function S φ,A that generates a coinductive object in S M1,A .Circularly, S φ,A depends on φ which in turn calls S φ,A .Careful analysis of the structure of the equations shows that the recursive call generates subobjects of the output streams, while the top structure is given directly.This guarantees that the equation is guarded and the definition productive.
The definition of lifting is also applicable more generally when φ is a natural transformation.We use this more general version later but are explicit when we do so.
We have now the conceptual framework to specify exactly what it means for a polymorphic function to be uniform on all monads.
Notice that naturality of f implicitly states that all the monadic side effects returned in the output must have been generated by evaluating the monadic actions in the input stream.Only in this way, changing the monadic actions in the input stream (with S φ,A ) can cause a corresponding change in the result (with φ B ). Definition 5. A function g : S A → B is pure if there exists a monadic stream function f : ∀M, S M,A → M B natural in M , such that g = f Id .We say g instantiates f .
As an example, we define the function on streams that returns the index of the first non-decreasing element: This function is pure: we can easily generalize it to all monadic streams: When instantiated with the identity monad, this function is equal to the previous definition on pure streams.If we instantiate it to the Maybe monad, it becomes a function on colists: it returns Just of the index of the first non-decreasing element, if such element exists; if the colist is finite and decreasing, it returns Nothing.
If we instantiate nodec with the list monad, it becomes a function on (non-well-founded) finitely branching trees, returning a list of values.The function follows all the paths in the tree in a left-to-right lexicographic order, it returns the indices of the first non-decreasing element of each path, when they exist (some paths may be finite and decreasing).
One might ask why we didn't define nodec as follows: The flipper stream returns the current state, irrespective of the position in the stream, and then repeatedly flips the state between 0 and 1.If we consider (noend flipper 1), we find it does not terminate.Each call to noend reads two decreasing values, 1 and 0, and the state returns to its initial value of 1.
The recursive call is applied to the same stream, flipper, and therefore the evaluation loops.
The problem arises because we evaluate a monadic action twice: while this returns the same value for some monads, for example Id and List, for others, for example State, we may get different results.The monadic nodec avoids this problem by only reading each monadic action once and then passing the value as an argument for further comparisons.
In the introduction we mentioned a different characterization of pure functions which is defined using parametricity as opposed to naturality [3].Our characterization is weaker and therefore results in stronger properties.
That naturality of monadic stream functions is weaker than parametricity stems from the relation between parametricity and naturality.Although the exact details of this relation are unclear, it is known that parametricity for certain types, including monadic stream functions, entails strong dinaturality [17].Strong dinaturality for a monadic stream function is a stronger condition than naturality in M. Particularly, strong dinaturality implies that the monadic stream function should behave uniformly across all natural transformations, not just monad morphisms.As this strong dinaturality condition is implied by parametricity, it follows that parametricity is stronger than naturality for monadic stream functions.
IV. MONADIC CONTINUITY Now we would like to prove that pure functions are necessarily continuous.
We begin with a more modest task: Can we test when a pure function is (syntactically) constant?Deciding the constancy of a stream function is in general undecidable, but we are interested in detecting functions that return a value without even reading any of their input.Proof.There is no general monad morphism between Maybe and a generic monad M .To bridge the gap between the instantiation of f for Maybe and for M , we use an intermediate instantiation of f with an error monad, Error M , whose error values are the monadic actions of M : with the following return and > >= operators: The merge M,A function is a monad morphism between Error M and M that merges the Throw and Pure values into the monad M .This is used in the bind operation for Error M but also provides the link in our proof between the bridging monad Error M and the monad M .The second link, between Error M and Maybe, is provided by a second monad morphism that forgets the monadic value in Throw: Naturality of f in the monad tells us that the following two squares commute: We can lift a monadic stream into the Error M monad with the stream function S Throw,A (note that Throw is a natural transformation but not a monad morphism; however, as mentioned in Section III, it can be lifted to streams in the same way).Observe that since forget (Throw m) = Nothing for every m, then S forget,A (S Throw,A α) = mcons Nothing for every α.
It is immediate that merge • Throw = id and subsequently S merge,A •S Throw,A = id.By commutativity of the upper square we have: where the last step is the assumption.Since forget B only returns a Just value for Pure inputs, it follows that f Error M (S Throw,A α) = Pure b.Commutativity of the lower square then gives: This gives us an effective test for syntactic constancy of pure functions.A similar idea helps us to prove the main result of the paper that a pure function, f , must be continuous.(In fact, continuity can probably be obtained as a direct consequence of decidability of syntactic constancy [6, Section 7].)We can find a monadic instantiation that forces f to compute the modulus of continuity at every stream.
To begin with we define the modulus of continuity: Definition 6.The modulus of continuity of a stream function, g : S A → B, is a function, mod g : S A → N, such that: Here the relation α = n α means that α and α have the same initial n elements.
To calculate the modulus of continuity of a pure function, we use the Writer monad with the monoid N, 0, max .A monadic stream for this monad, α : S Writer N ,A is essentially a stream of pairs of elements of A and natural numbers: When we evaluate the elements of the stream, we write out the maximum value of the n i s of all the read elements.
We lift each pure stream α = a 0 a 1 a 2 • • • : S A to a Writer-monadic stream that decorates each element with the successor of its index: Formally we can define it as follows: where index : N → S A → S Writer N ,A index n (a 0 α) = mcons a 0 , index (n + 1) α , n According to the intuitive explanation, when we evaluate a monadic stream inside this Writer monad, we should obtain the successor of the maximum index of the elements that were actually read.This gives an immediate suggestion for the computation of the continuity modulus.
A variant of the proof of Theorem 1 shows that this function indeed computes a correct modulus of continuity.
Theorem 2. Let g : S A → B be a pure function that instantiates f : ∀M, S M,A → M B. Then (modulus f ) is the modulus of continuity of g.
Proof.We can formulate this continuity conjecture in a slightly different way by extending the claim about Maybemonadic streams.We use now the Writer N monad in place Error M in Theorem 1.There we used it as a mediation step from M -streams to Maybe-streams, injecting all M -actions into Throw elements to force its truncation to the empty list.Now we can do a similar operation, but truncating a stream α = a 0 a 1 a 2 • • • at the m-th element, where m = (modulus f α) = π 1 (f Writer N α ι ).For a given α define two monad morphisms from the Writer monad to Maybe and Id: Note that φ, which truncates the stream, is dependent on the modulus of continuity of α.Naturality of f in the monad tells us that the following two squares commute: Starting with α ι : S Writer N ,A , we know that (f Writer N α ι ) = b, m for some b : B. Using the commutativity of the lower rectangle, we discover that Following the upper route, by the definition of φ and of its lifting to streams, we have that (S φ,A α ι ) = a 0 . . .a m−1 nil; so we discover that (f Maybe (a 0 . . . If we now take another pure stream α such that α = m α, we also have (S φ,A α ι ) = a 0 . . .a m−1 nil.This forces (f Writer N α ι ) = b, m for some m (we can easily prove that m = m) and consequently (f Id α ) = b.

V. MONADIC APPROXIMATIONS
Theorem 2 shows that a pure function has a modulus of continuity.We may ask if this is true at a more abstract level: is there a general notion of "modulus" that makes sense for any monad and can we construct it for every polymorphic function on monadic streams?Continuity is achieved by showing that a initial segment α| n of the stream is sufficient to determine the value of (f Id α).For a general monad, we may look at well-founded approximations of the monadic stream.For example, streams with respect to the list monad are finitely branching non-well-founded trees; an approximation is obtained by cutting each branch at a finite depth.This, however, does not work in general: for the list monad we get indeed a version of Theorem 2 stating that the value of f List α depends only on a finite approximation; however, for other non-discrete monads, for example the state monad, this is not true: the result of f may indeed depend on an infinite amount of information from α.
The definition of the modulus function uses the Writer monad to produce the index of the furthest read element.The first step in that direction was α ι , where we decorated each entry in the stream with its index.With a small variation of the definition, we can decorate each entry with the list of elements of the stream up to that point.We use the writer monad with the list monoid List(A), * , nil , taking as monoid operation the longer of two lists, giving preference to the second: nil * l = l l * nil = l (a 0 :: as) * (b 0 :: bs) = b 0 :: (as * bs).
We lift every stream to one inside S Writer List(A) ,A by decorating every element with the initial segment of the stream leading to it.
where decorate : Intuitively we have that We can directly extract the prefix sufficient for the computation of a monadic function with a modification of the modulus: Let us write α l to state that the list l is an initial segment of the stream α, that is, l approximates α (notation inspired by formal topology [9], [25]).An alternative formulation of continuity states that this approximation is sufficient to determine the result.
Proof.By a modification of the proof of Theorem 2.
Let us generalize the definition of approximations to any monad.For container-monad, S M,A is a set of non-wellfounded trees.An approximation consists of a well-founded tree with leaves representing unknown information.
The approximation relation between S M,A and L M,A is defined inductively by the following rules.
α unknown m M l mcons m lcons l where M is the lifting of to the monad, a relation between M (A × S M,A ) and M (A × L M,A ).If M is a container, M = S, P , the relation states that the elements must have the same shape and related components in corresponding positions (with the same A-elements): if m = s m , h m with s m : S and h m : P s m → A × S M,A and if l = s l , h l with s l : S and h l : This allows us to formulate a continuity principle for every monad.
Definition 7. Let f : ∀M, S M,A → M B be natural in M and let M be a specific monad, the continuity principle for M states that Although it may be possible, for finitary monads (containers for which every shape has a finite number of positions), to generalize the definition of approx and the proof of Theorem 2, the continuity principle is not true in general.As a counterexample, consider the following function and observe what happens when we apply it to a stream in the state monad.
The function misTake reads the first element of the stream, n, and returns the first n elements of the stream, including the head n itself.Now consider the following set of streams in the state monad.
where atleft : S Id,N → S State N ,N atleft (a :: α) = mcons (λn.ifzero n a, atleft α, 0 0, 0, n ) 0 : ∀M.S M,N 0M = mcons (return 0, 0M ) The first monadic state action of goleft α returns the initial state and changes the state to 0. The tail depends on the value of the state: if it is non-zero, we return the zero stream; if it is zero, we return the first element of α and recurse.If we picture an element of S State N ,N as a tree in which every node branches according to the state value, then goleft α has the value of the state in the the nodes at depth one and at depths greater than one is zero everywhere except on the leftmost spine, where it contains the elements of α.
The counterexample is made by applying misTake to a stream goleft α.The result of an application is a function which when applied to initial state n returns the first n elements of the left spine of goleft α.Equationally: misTake (goleft α) n = 0 :: This contradicts the continuity principle because an arbitrarily large amount of the left spine can be read depending on the initial state -any well-founded approximation of goleft α would have to truncate this spine.We note that it is important for the counterexample that misTake re-evaluates the first monadic action, as otherwise the left-most spine would not be returned.
Next we give two important relations between the monadic stream goleft α and its approximations, before giving a proof of the contradiction.First we define leftof which takes the left spine of an approximation: Firstly, for any approximation of a goleft stream, leftof gives us a truncation of the left-most spine: Secondly, two goleft streams only differ on their left spine.This can be stated using approximations as so: We are now ready to prove that the continuity principle does not hold for State N .Suppose, towards a contradiction, that l is an approximation to goleft 0 which satisfies the continuity principle for misTake.Using equation 2 we have n such that: Take α = 0| n−1 + + 1 then it follows directly that 0 α leftof l.Using equation 3 we have that goleft α is approximated by l, i.e. goleftα l.We can now apply the continuity principle to give: misTake (goleft α) = misTake (goleft 0) However using (1) we have misTake (goleft 0)(n + 1) = 0 :: 0| n but misTake (goleft α)(n + 1) = 0 :: α| n = 0 :: 0| n−1 + + [1] which contradicts the equality given by the continuity principle.

VI. DIALOGUE TREES
In their characterization of continuous functions on streams Ghani, Hancock and Pattinson [13], [14] define a type of dialogue trees whose paths represent the successive calls that an effective procedure makes on the elements of a stream.The idea of a data structure representing interaction goes back to Brouwer and was used by Kleene in his article about higherorder functionals [20].Martín Escardó used them to give a concise proof of continuity of the functionals of system T [10].They are called strategy trees by Bauer et al. [3], who use them to characterize pure stream programs in ML-like languages.
A dialogue tree has nodes representing a single interaction with the input stream or the return of an output result.Since the procedure must be terminating, the tree must be well-founded.
A tree can have two forms: an immediate answer (Answer b) corresponding to a constant function returning b without reading any element of the input stream, or a branching construction (Ask h) with h : A → Dialogue A,B corresponding to a function that reads the next element of input stream, a, and then continues as (h a).This leads to the definition of the evaluation function that associates a continuous function to a tree, by structural recursion on the tree: The function (eval t) is continuous for every tree t, i.e. it has a modulus of continuity.The converse is challenging.In their paper, Ghani et al. prove it negatively: If a function has no representation as a dialogue tree then it is not continuous.In recent work, Tarmo Uustalu and one of us [7] gave a constructive proof based on Brouwer's Bar Induction Principle.The evaluation operation is readily generalized to monadic streams: For any dialogue tree t, (eval t) instantiates (meval t) with the identity monad, which provides an alternative proof for the continuity of (eval t).To verify this, we need to show that (eval t) = (meval t) Id and that (meval t) M is natural in M ; both these facts follow from the definition of meval (by substituting Id and by construction, respectively).Also in this case, the converse is the challenging direction of the correspondence.How can we construct a dialogue tree from a monadic stream function?We can observe that the dialogue tree type former Dialogue A is itself a monad on B: So it makes sense to consider streams on it, S Dialogue A ,A .These streams actually correspond to the representation of stream processors in another work by Ghani, Hancock and Pattinson [15].In particular, we can represent the identity processor that repeatedly reads an element from the input stream and immediately returns it to the output streams: Using ι A we can define a tabulation function, which converts a monadic stream function into a dialogue tree: The tabulate function however is not the right inverse of meval, as meval(tabulate f ) is not necessarily the same function as f .This can be seen already on pure streams α, eval (f Dialogue A ι A ) α is not always equal to f Id α, as we can see from the following counterexample by Paolo Capriotti: This function executes the monadic action of the stream twice, so it reads the head of the stream twice, then returns the second value that it obtains.On pure streams this simply means that it reads the first value twice, a 0 and a 1 are the same: asktwice Id (a 0 α) = a 0 .So the behaviour is the same as that of a function that returns the first input it gets: But if we instantiate it with monads that have side effects, the head of the stream might have changed after the first reading.For example, if we instantiate asktwice and askonce with the state transformer monad and apply it to a simple state increment stream, we get different results.
incr : S State N ,N incr = mcons (λs.s, incr, s + 1 ) runstr (askonce incr) 0 = 0 runstr (asktwice incr) 0 = 1 Now, if we generate a dialogue tree by applying asktwice to the identity stream processor and then we evaluate this tree, we obtain a function that reads the first two element of the stream and returns the second.

meval (asktwice Dialogue
When applied to a pure stream, this function returns the second element, not the first as asktwice does: eval (asktwice Dialogue A ι A ) (a 0 a 1 α) = a 1 .We can note that this non-correspondence is caused by the fact that asktwice evaluates the input monadic action m twice.In the original function, this means that we read the same stream element twice, but in the dialogue tree it results in two consecutive requests for input.We see that there is a discrepancy between two different ways of obtaining the next element of the stream, which the evaluation to a dialogue tree conflates.A monadic action m : M A can be seen as a channel through which values of type A are transmitted; every time we evaluate it, we request a new transmission.On the other hand, evaluating a richer action m 0 : M (A × S M,A ) returns a value of type A and a new channel m 1 ; at this point we have the choice of which channel we use next.
This observation tells us that a simple action M A can already be used to produce a sequence of values of type A. Indeed Jaskelioff and O'Connor [19] show that there is a oneto-one correspondence between dialogue trees in Dialogue A,B and monadic functions of type ∀M, M A → M B.

VII. RELATED AND FUTURE WORK
Monadic streams are a powerful abstraction that already found concrete applications.Perez, Bärenz and Nilsson [24] used them to give a mathematically coherent and practical implementation of Functional Reactive Programming.Thus, aside from the theoretical interest in the foundations of mathematics and computer science, the study of monadic stream functions has important applications.
Three recent articles ([3], [19], [10]) studied the closely related topic of pure functions on a type of streams encoded by the type N → M A, its relation to dialogue trees and its use to prove the continuity of functional programs.
Intuitively, the difference with out work is that a function N → M A gives an "infinite number of channels", each of which can be consulted to obtain an element of A. In our formalization, a stream of type S M,A is a single channel that can be consulted to get an element of A and a second channel (or a modification of the initial channel) that will produce future elements of A.
Bauer, Hofmann and Karbyshev [3] give a precise definition of purity for higher-order functionals in languages with side effects, for example ML.A function f : (int → int) → int is called pure if the only side effects that it generates are due to the evaluation of its argument.This is done by interpreting such functions as polymorphic monadic functionals of type The formal definition of purity is formulated on the basis of a relational semantics.A functional is pure (or monadically parametric) if its instantiations by different monads are related by this semantics.This is an abstract way of specifying that the functional behaves in the same way on all monads.
They use dialogue trees, which they call strategy trees, as a representation of functionals.Their representation is a slight generalization of the dialogue trees used by Ghani et al.'s ( [13], [14]) that we also use in the present paper.The type Tree is inductively defined by two constructors: Ans : C → Tree, that specifies a constant; Que : A → (B → Tree) → Tree, that specifies an interaction in which the function makes a query of type A, receives a response of type B, and produces a new tree to continue the computation.It is easy to show how every tree can be interpreted as a pure functional in Func.Conversely, a pure functional F : Func gives a tree by instantiation to the continuation monad with Tree as result type: fun2tree F = F Cont Tree Que Ans.Notice how the argument of F , when instantiated to the continuation monad, has type which is exactly the type of Que.The result type is Cont Tree C = (C → Tree) → Tree, so we can apply it to Ans.
The authors prove that purity implies that the transformations from trees to functionals and vice versa are inverse of each other (Theorem 12).
Among the applications, the authors show how to compute a modulus of continuity for functionals on the Baire space B = N → N.Call such a functional F : B → N pure if it is the instantiation of a monadically parametric function to the identity monad: F : T ∈Monad (N → T N) → T N, F = FId .The authors prove that such a functional is continuous (for every f : B, the computation of F f depends only on an initial segment of f ).They give an explicit calculation of the modulus of continuity: where instr f : This is similar to our technique: By using the state monad, they force f to return a list of all the indices on which the input function has been applied during the computation.
Jaskelioff and O'Connor [19] give representation theorems for polymorphic higher order functionals.Their general type is: ∀F.(A → F B) → F C where F ranges over a class of functors.Depending on the class of functors considered, we get different concrete representations of the functionals.For example, if F ranges over all functors, then the functionals are represented exactly by the type A × (B → C): a functional h is necessarily of the form h = λg.F (k) (g a) for a : A and k : B → C. For pointed functors (with a natural transformation η X : X → F X) the representation is A × (B → C) + C. For applicative functors (having in addition a natural operation X,Y : (F X × F Y ) → F (X × Y )) the representation is Σ n:N (A n × (B n → C)).
These representation results have a common structure: a general Representation Theorem formulated in categorical language.First they establish that for any small category F of endofunctors on sets that contains at least R A,B X = A×(B → X), we have: F ∈F (A → F B) → F X ∼ = R A,B X which corresponds to the isomorphism for functors: ∀F.(A → F B) → F X ∼ = A×(B → X).For categories of functors with extra structure (pointed functors, applicatives, monads), the representation theorem can be applied through an adjunction.Suppose that F is such a categoty of functors with structure, E a small subcategory of endofunctors on sets.Assume that there is an adjunction between a forgetful functor F → E and a free functor * : E → F, so that ( * ) U .Then the representation theorem is F ∈F (A → U F B) → U F X ∼ = U R * A,B X.When we instantiate the representation theorem to monads, we obtain that the type of polymorphic monadic higher order functions is isomorphic to the free monad on The type Free (PStore a b) x is once again a presentation of dialogue/strategy trees.
Escardó [10] gives a new short and simple proof of the known result that any function f : (N → N) → N definable in Gödel's system T is continuous.The proof utilizes an auxiliary interpretation of system T in which natural numbers are interpreted as well-founded dialogue trees.
His definition of dialogue trees is more general than ours and is essentially the same as in the two previously considered articles.A dialogue has three type parameters X, Y, Z: X is the type of queries that can be asked by a function, Y is the type of possible answers to a query, Z is the type of results that the function can return.If X = N then we represent functions on streams of elements of Y (of type N → Y ) which return a result of type Z.The dialogue trees have internal nodes decorated with a query of type X, branching according to the response type Y , and leaves of result type Z: Of specific interest are dialogues over the Baire type N → N, represented by the type former B = D N N.
A system T higher-order functional f : (N → N) → N is given a non-standard interpretation by replacing the plain natural numbers N with dialogue trees that return naturals, Ñ = B N = D N N N.The interpretation utilizes the monadic operations of dialogue trees: zero becomes just returning 0, the successor operator is lifted by the monadic functor; primitive recursion is interpreted by the use of the bind operation.The continuity of the dialogue tree interpretation is established.
We obtain an operator on functions between dialogue trees f : ( Ñ → Ñ) → Ñ.We can then apply it to a generic sequence of type Ñ → Ñ to obtain a dialogue tree that will turn out to be exactly a representation of the original f .The generic sequence is constructed so as to make the following diagram commute: This is achieved simply by adding an extra step of interaction to the dialogue: generic replaces every leaf of the form (η m) with B (λn.η n) m.This idea is analogous, but not the same, as our definition of tabulate, which also instantiates the monadic function with the dialogue tree monad and applies it to the identity processor ι A .
Finally, the interpretation is shown to be equivalent to the standard interpretation of system T.
Future Work.An important next step in this line of work is to extend Escardó's characterization of functionals of system T to richer type systems.Does every function definable in some form of (dependent) type theory have a representation as a monadic function or dialogue tree?
Another avenue of exploration is the application to concrete functional programming.Already the work of Perez, Bärenz and Nilsson [24] shows that programming with monadic streams is very effective to create functional interactive applications.A complete understanding of the computation power and logical properties of these functions will be useful in practice.

Theorem 1 .
Let f : ∀M, S M,A → M B be natural in M .If there exists b : B such that f Maybe (mcons Nothing) = Just b, then for every monad M and every α : S A , f M α = return M b.
R A,B .In Haskell notation this becomes ∀m.Monad m ⇒ (a → m b) → m x ∼ = Free (PStore a b) x where Free and PStore are the Haskell implementation of the free monad construction and of R A,B .
data D (X, Y, Z : Set) : Set η : Z → D X Y Z B : (Y → D X Y Z) → X → D X Y Z.
Id monad it behaves as the original nodec.However, noend is actually not well-founded: while the definition of nodec is clearly recursive on its first argument, no such recursion justifies noend.To see this consider the following stream on the State monad: flipper : S State N ,N flipper = mcons (λn.n, flipper , mod (n + 1) 2 ) This new function noend is directly recursive, taking the tail of the monadic stream as its argument.If we instantiate noend with the