Well-definedness of Streams by Transformation and Termination

Streams are infinite sequences over a given data type. A stream specification is a set of equations intended to define a stream. We propose a transformation from such a stream specification to a term rewriting system (TRS) in such a way that termination of the resulting TRS implies that the stream specification is well-defined, that is, admits a unique solution. As a consequence, proving well-definedness of several interesting stream specifications can be done fully automatically using present powerful tools for proving TRS termination. In order to increase the power of this approach, we investigate transformations that preserve semantics and well-definedness. We give examples for which the above mentioned technique applies for the ransformed specification while it fails for the original one.


Introduction
Streams are among the simplest data types in which the objects are infinite. We consider streams to be maps from the natural numbers to some data type D. Streams have been studied extensively, e.g., in [1]. The basic constructor for streams is the operator ':' mapping a data element d and a stream s to a new stream d : s by putting d in front of s. Using this operator we can define streams by equations. For instance, the stream zeros only consisting of 0's can be defined by the single equation zeros = 0 : zeros. More complicated streams are defined using stream functions. For instance, the boolean Fibonacci stream Fib is defined 1 as the limit of the strings φ i where φ 1 = 1, φ 2 = 0, φ i+2 = φ i+1 φ i for i ≥ 1, showing the relationship with Fibonacci numbers. For f being the function replacing every 0 by 1 and every 1 by 01, one easily proves by induction on n that f (φ n ) = φ n+1 for all n ≥ 1. As Fib is the limit of these strings, Fib is a fix point of this function f on boolean streams. So the function f and Fib satisfy the three equations f (0 : σ) = 0 : 1 : f (σ), f (1 : σ) = 0 : f (σ), for all boolean streams σ. In this paper we consider stream specifications consisting of such a set of equations. We address the most fundamental question one can think of: is the intended stream uniquely defined by these equations? More precisely, does such a set of equations admit a unique solution as constants and functions on streams? So in particular for Fib: is the boolean stream Fib uniquely defined by the three equations we gave? We will call this well-defined, and we will show that for the equations for Fib this indeed holds. Although our specification of Fib only consists of a few very simple equations, the resulting stream is non-periodic and has remarkable properties. For instance, one can make a turtle visualization as follows. Choose an initial drawing direction and traverse the elements of the stream Fib as follows: if the symbol 0 is read then the drawing direction is moved 120 degrees to the right; if the symbol 1 is read then the drawing direction is moved 30 degrees to the left. In both cases after doing so a line of unit length is drawn. Then after 100.000 steps the following picture is obtained.
Another turtle visualization of Fib with different parameters was given in [19]. For turtle visualizations of similar stream specifications we refer to http://www.win.tue.nl/~hzantema/str.html. To show that well-definedness does not always hold, observe that the function f defined has exactly two fixpoints: the Thue-Morse stream and its inverse.
Our approach to prove well-definedness of stream specifications is based on the following idea. Derive rewrite rules from the equations in such a way that by these rules the n-th element of the stream can be computed for every n. The term rewriting systems (TRS) consisting of these rules will be orthogonal by construction, so if the computation yields a result, this result will be unique. So the remaining key point is to show that the computation always yields a result, which is the case if the TRS is terminating. The past ten years showed up a remarkable progress in techniques and implementations for proving termination of TRSs [2,7,14]. One of the objectives of this paper is to exploit this power for proving welldefinedness of stream specifications. In our approach we introduce fresh operators head and tail intended to observe streams. We present a transformation of the specification to its observational variant. This is a TRS mimicking the stream specification in such a way that head or tail applied on any stream constant or stream function can always be rewritten. In particular for a stream term t it serves for computing head(tail n−1 (t)), representing the n-th element of t. So not only a proof of well-definedness is provided, our approach also yields an algorithm to compute the n-th element of any stream term, for any n.
This transformation is straightforward and easy to implement; an implementation for boolean stream specifications is discussed in Section 6.
The main result of this paper states that if the observational variant of a stream specification is terminating, then the stream specification is well-defined. It turns out that for several interesting cases termination of the observational variant of a specification can be proved by termination tools like AProVE [6] or TTT2 [10]. This provides a new technique to prove well-definedness of stream specifications fully automatically, applying for cases where earlier approaches fail. Our main result appears in two variants: • a variant restricting to ground terms for general stream specifications (Theorem 5.1), and • a variant generalizing to all streams for stream specifications not depending on particular data elements (Theorem 7.1). By an example we show that the approach does not work for general stream specifications and functions applied on all streams. Moreover, we show that our technique is not complete: the fix point definition of the Fibonacci stream Fib as we just gave is a well-defined stream specification for which the observational variant is non-terminating. However, we will also investigate transformations from stream specifications to stream specifications preserving semantics, so also preserving well-definedness. Applying such a transformation to our specification of Fib gives an alternative specification specifying the same stream Fib, but for which the observational variant is terminating, to be proved automatically by a termination tool. In this way we prove well-definedness of Fib with respect to the original stream specification. More general, applying such semantics preserving transformations increases the power of our approach to prove well-definedness of stream specifications.
Proving well-definedness in stream specification is closely related to proving equality of streams. A standard approach for this is co-induction [16]: two streams or stream functions are equal if a bisimulation can be found between them. Finding such an arbitrary bisimulation is a hard problem in the general setting, but restricting to circular co-induction [8,13] finding this automatically is tractable. A strong tool doing so is Circ [12,11]. The tool Circ focuses on proving equality, but proving well-definedness of a function f can also be proved by equality as long as the equations for f are orthogonal: take a copy f of f with the same equations, and prove f = f . Here orthogonality is essential: if for instance a stream c has two rules c = 0 : c and c = 1 : c, then the system is non-orthogonal and admits every boolean stream as a solution, while by having a copy c with the same rules one can prove c = c by only using the rules c = 0 : c and c = 0 : c .
The input format of Circ differs from what we call stream specifications: in order to fit in the co-induction approach head and tail are already building blocks and the Circ input is essentially the same as what we call the observational variant. Our implementation as discussed in Section 6 offers the facility to transform a stream specification to Circ format, and also generate the equalities representing well-definedness in Circ format. For very simple examples the equalities can be proved automatically by Circ, but for several small stream specifications Circ fails while our approach succeeds in proving well-definedness. Conversely our approach can be helpful to prove equality of two streams: if one stream satisfies the specification of the other one, and both specifications are well-defined, then the streams are equal.
Another closely related topic is productivity of stream specifications, as studied by [4]. Productive stream specifications are always well-defined. Conversely we will give an example (Example 4) of a stream specification that is well-defined, but not productive. Our format of stream specifications is strongly inspired by [4]. In [4] a technique has been developed for establishing productivity of single ground terms fully automatically for a restricted class of stream specifications. In particular, only a mild type of nesting in the right-hand sides of the equation is allowed. If these restrictions hold, then the approach yields a full decision procedure for productivity, and provides a corresponding implementation by which for a wide range of examples productivity can be proved fully automatically. Productivity of a single ground term implies well-definedness of that single term. On the other hand, our technique often applies where their restrictions do not hold, or for proving well-definedness for systems that are not productive. Apart from the technique from [4] there are more results on productivity. An approach to prove productivity by means of outermost termination has been presented in [21]; a more recent approach using transformations and context-sensitive termination is presented in [20]. For both these approaches the power of present termination provers is exploited for proving productivity automatically, similar to what we do in this paper for proving well-definedness.
In [9] well-definedness of a stream specification is claimed if some particular syntactic conditions hold, like all right-hand sides of the equations have ":" as its root. Their result both follows from our main theorem and from the productivity analysis of [4].
Both stream equality [15] and productivity [17] have been proved to be Π 0 2 -complete, hence undecidable. By a similar Turing machine construction the same is expected to hold for stream well-definedness.
This paper is an extension of the RTA conference paper [19] and the corresponding tool description [18]. Compared to these papers • some definitions have been slightly modified in order to cover a more general setting, • the process of unfolding and other transformations preserving the semantics have been worked out in detail in Section 3 and Section 8, while in [19] only some of the ideas were sketched by examples, • more examples are given, in particular specifying the paper folding stream and the Kolakoski stream. The paper is structured as follows. In Section 2 we present the basics of stream specifications and their models. In Section 3 we show how a non-proper stream specification can be unfolded to a proper stream specification preserving semantics and well-definedness. In Section 4 we define the transformation of a proper stream specification to its observational variant. In Section 5 we present and prove the main theorem: if the observational variant is terminating then the specification is well-defined, that is, restricted to ground terms it has a unique model. In Section 6 we describe our implementation. In Section 7 we show that the restriction to ground terms in the main theorem may be removed in case the stream specification is data independent, that is, left-hand sides of equations do not contain data values. In Section 8 we present requirements on transformations on stream specifications for preserving semantics and well-definedness. In case the observational variant of a stream specification is not terminating, or the tools fail to prove termination, then we can apply such transformations. Often then the observational variant is terminating, proving not only well-definedness of the transformed specification, but also of the original one. One of the corresponding examples serves for proving incompleteness of our main theorem. We conclude in Section 9.

Streams: Specifications and Models
In stream specifications we have two sorts: s (stream) and d (data). We assume the set D of data elements to consist of the unique normal forms of ground terms over some signature Σ d with respect to some terminating orthogonal TRS R d over Σ d . Here all symbols of Σ d are of type d n → d for some n ≥ 0. We assume a particular symbol : having type d × s → s. For giving the actual stream specification we need a set Σ s of stream symbols, each being of type d n × s m → s for n, m ≥ 0. Now terms of sort s are defined inductively as follows: • a variable of sort s is a term of sort s, • if f ∈ Σ s is of type d n × s m → s, u 1 , . . . , u n are terms over Σ d and t 1 , . . . , t m are terms of sort s, then f (u 1 , . . . , u n , t 1 , . . . , t m ) is a term of sort s, • if u is a term over Σ d and t is a term of sort s, then u : t is a term of sort s. Note that we do not allow function symbols with output sort d and input containing sort s. One reason for this is that we do not want that distinct data elements are made equal by stream equations.
An equation of sort s is a pair ( , r) of terms of sort s, usually written as = r. An equation can also be considered as a rule in a TRS. For basic properties of TRSs we refer to [3]. In particular, an orthogonal TRS is always confluent, from which it can be concluded that every term has at most one normal form. Here orthogonal means that the left-hand sides of the rules are non-overlapping, and every variable occurs at most once in any left-hand side.
As a notational convention variables of sort d will be denoted by x, y, terms of sort d by u, u i , variables of sort s by σ, τ , and terms of sort s by t, t i .
• for every i = 1, . . . , m the term t i is either a variable of sort s, or t i = x : σ where x is a variable of sort d and σ is a variable of sort s, • t is any term of sort s, • Every term of the shape f (u 1 , . . . , u n , u n+1 : t 1 , . . . , u n+m : t m ) for f ∈ Σ s of type d n × s m → s, and u 1 , . . . , u n+m ∈ D matches with the left-hand side of an equation from R s .
Some parts in this definition allow modification, but for being a basis for the rest of our theory we fix this choice. All of our examples are on boolean streams, but by allowing data to be ground normal forms of a data TRS, the setting is much more general. Sometimes we call R s a stream specification: in that case Σ d , Σ s consist of the symbols of sort d, s, respectively, occurring in R s , and R d = ∅.
Example 1. For specifying the Thue-Morse sequence the data elements are 0, 1, and a data operation not is used. The data rewrite system R d consists of the two rules not(0) → 1 and not(1) → 0. The set R s consists of the equations This is a proper stream specification.
Definition 2.1 is closely related to the definition of stream specification in [4]. In fact there are two differences: • We want to specify streams for every ground term of sort s, while in [4] there is a designated constant to be specified. • Our restriction on left-hand sides of R s in a proper stream specification is stronger than the exhaustiveness from [4]. However, by introducing fresh symbols and equations for defining these fresh symbols, every stream specification in the format of [4] can be unfolded to a proper stream specification in our format. This is worked out in Section 3.
Stream specifications are intended to specify streams for the constants in Σ s , and stream functions for the other elements of Σ s . The combination of these streams and stream functions is what we will call a stream model.
More precisely, a stream over D is a map from the natural numbers to D. Write D ω for the set of all streams over D. In case of D = ∅ we have D ω = ∅; in case of #D = 1 we have #D ω = 1. So in non-degenerate cases we have #D ≥ 2.
It seems natural to require that stream functions in a stream model are defined on all streams. However, it turns out that several desired properties do not hold when requiring this. Therefore we allow stream functions to be defined on some set S ⊆ D ω for which every ground term can be interpreted in S.

Definition 2.2.
A stream model is defined to consist of a set S ⊆ D ω and a set of functions For a ground term u over Σ d write NF(u) for its R d -normal form. We write T s for the set of ground terms of sort s over for all ground terms u, u i of sort d and all ground terms t, t i of sort s. So in a stream model: • every data operator is interpreted by its corresponding term constructor, after which the result is reduced to normal form, One can wonder why to restrict to S = {[t] | t ∈ T s }. Another option would be simply state S = D ω . However, sometimes restricting to ground terms yields a unique model, while functions applied on arbitrary streams are not unique. In Example 3 we will see an example of this phenomenon. By restricting to interpretations of ground terms and ignoring unreachable streams, we arrived at our definition of well-definedness.
Not every proper stream specification is well-defined: if #D > 1 and R s only consists of the equation c = c then every stream [c] satisfies the specification. Less trivial is the boolean stream specification in which [f ] can be chosen to be the tail function and [c] be any stream starting with 0, yielding several stream models. There are also proper stream specifications with no model,

Unfolding Stream Specifications
The specification of the function f in the introduction to define Fib does not meet the requirements of a proper stream specification since the argument 0 : σ in the left-hand side f (0 : σ) is not of the right shape. Introducing a fresh symbol g and unfolding yields satisfying the requirements of a proper stream specification. In this section we precisely define this unfolding and show that it does not influence well-definedness.
where f ∈ Σ s is of type d n × s m → s, and for some i ∈ {1, . . . , m} the term t i is of the shape t i = u : t where not both u and t are variables, so the stream specification is not proper. Then the unfolded stream specification on f on position i, denoted as is obtained by adding a fresh symbol g of type d n+1 × s m → s to Σ s , adding an equation to R s , where x n+1 : σ i is in the i-th stream position of f , and in which every equation in R s of the shape f (u 1 , . . . , u n , t 1 , . . . , u : t , . . . t m ) = t where u : t is on the i-th stream position of f , is replaced by where t is on the i-th stream position of g.
Applying Unf f,1 on the Fib stream specification from the introduction yields which is indeed a proper stream specification. In general, for every exhaustive stream specification in the sense of [4], by repeatedly applying Unf f,i for various f , i, as long as an equation of the shape f (u 1 , . . . , u n , t 1 , . . . , u : t , . . . t m ) = t exists for which not both u and t are variables, a proper stream specification in our sense can be obtained.
In order to justify this unfolding it remains to prove that the original stream specification is well-defined if and only if the unfolded variant is well-defined, and in case of well-definedness they define the same. More precisely, we prove that the transformation Unf f,i preserves semantics, defined as follows.
Obviously, preservation of semantics is closed under composition of such transformations.
We prove that Unf f,i preserves semantics in two steps: first we only add the equation for the fresh symbol g, and then we do the replacement of the f -equations by g-equations.
For each of these two steps we show by a more general lemma that semantics is preserved.
in which the symbol g does not occur in t, and t does not contain variables other than where in the latter T s stands for ground terms including the symbol g.
Conversely, assume we have a stream model (S, Then by ignoring g it is also a stream model satisfying (Σ d , Σ s , R d , R s ). Due to the shape of the equation containing g, for every ground term t containing g there is a ground term t not containing g satisfying and conversely, keeping the same set S and both satisfying S = {[t] | t ∈ T s }. This proves the first requirement of semantics preservation. The second requirement holds since the interpretations of ground terms are the same in both models.
In Lemma 3.2 the signature was extended by a fresh symbol, while except for adding one equation for this fresh symbol, the equations remained the same. In the next lemma it is the other way around: now the signature remains the same and the equations may be modified. For a set R of equations we write = R for the congruence generated by R, that is, the closure of R under substitutions, contexts, reflexivity, symmetry and transitivity.
= R s r for all = r in R s , and = Rs r for all = r in R s . Then transforming Proof. From the given connection between R s and R s it is immediate that a model satisfies From this the lemma follows.
Theorem 3.4. The transformation Unf f,i preserves semantics on stream specifications on which it is defined.
Proof. The operation Unf f,i consists of two steps: the addition of an equation generating g and the replacement of existing equations for f . The addition preserves semantics due to Lemma 3.2. For the replacement Lemma 3.3 applies, for both directions applying the equation As both transformations preserve semantics, the same holds for the composition Unf f,i .

The Observational Variant
We define a transformation Obs transforming the original set of equations R s in a proper stream specification to its observational variant Obs(R s ), being a TRS. The basic idea is that the streams are observed by two auxiliary operators head and tail, of which head picks the first element of the stream and tail removes the first element from the stream, and that for every t ∈ T s of type stream both head(t) and tail(t) can be rewritten by Obs(R s ).
The main result of this paper is that if Obs(R s ) ∪ R d is terminating for a given proper As a consequence, the specification uniquely defines a corresponding stream [t] for every t ∈ T s .
We define Obs(R s ) in two steps. First we define P(R s ) obtained from R s by modifying the equations as follows. By definition every equation of R s is of the shape f (u 1 , . . . , u n , t 1 , . . . , t m ) = t where for every i = 1, . . . , m the term t i is either a variable of sort s, or t i = x : σ where x is a variable of sort d and σ is a variable of sort s. In case t i = x : σ then in the left-hand side of the equation the subterm t i is replaced by σ, while in the right-hand side of the equation every occurrence of x is replaced by head(σ) and every occurrence of σ is replaced by tail(σ).
For example, the equation for zip in Example 1 will be replaced by Now we are ready to define Obs.
• for every rule in P(R s ) of the shape → r with root(r) = : the two rules head( ) → head(r), tail( ) → tail(r).
The reason for first transforming R s to P(R s ) is that for the validity of the main theorem we need the special shape of the rules of Obs(R s ) in which apart from the root symbol head or tail and one symbol from Σ s , every left-hand sides only consists of variables.
Example 2. For the set R s of equations given in Example 1 we rename the symbol tail by tail0 in order to keep the symbol tail for the fresh symbol introduced in the Obs construction. Then the TRS Obs(R s ) consists of the following rules: Together with the rules not(0) → 1 and not(1) → 0 from R d this TRS is terminating as can easily be proved fully automatically by AProVE [6] or TTT2 [10]. As a consequence, the result of this paper states that the specification uniquely defines a stream for every ground term of type s, in particular for morse.

The Main Theorem
We start this section by presenting our main theorem.
be a proper stream specification for which the TRS Obs(R s ) ∪ R d is terminating. Then the stream specification is well-defined.
Recall that a stream specification is defined to be well-defined if it admits a unique model (S, Before proving the theorem we show by an example why it is essential to restrict to S = {[t] | t ∈ T s } rather than choosing S = D ω . A degenerate example is obtained if there are no constants of sort s, and hence T s = ∅. More interesting is the following.
Example 3. Consider the proper boolean stream specification with R d = ∅ and R s consists of: The function f has been specified in such a way that it tries to remove all 0's from its argument. So for streams specified by terms like f (c) there is nothing to remove, and we expect well-definedness: the term f (c) will uniquely be defined to be the stream of only ones. However, for streams containing only finitely many 1's this may be problematic. Note that by the symbols c, :, 0 and 1 only the streams with finitely many 0's can be constructed, so for ground terms over the symbols occurring in the specification this problem does not arise. Indeed, it turns out that the TRS Obs(R s ) ∪ R d is terminating, so by Theorem 5.1 the specification is well-defined. It is interesting to remark that the approach from [4] fails to prove productivity, as this stream specification is not data-obliviously productive, i.e., the identity of the data is essential for productivity. Moreover, also Circ [11] fails to prove well-definedness of this stream specification.
We concluded that this example is well-defined: it admits a unique model tion. First we define [t] 1 ∈ D ω for any t ∈ T s . Since elements of D ω are functions from N to D, a function [t] 1 ∈ D ω is defined by defining [t] 1 (n) for every n ∈ N. Due to the assumption of the theorem the TRS Obs(R s ) ∪ R d is terminating. According to the definition of a proper stream specification the TRS R s ∪ R d is orthogonal, and by the construction Obs the TRS Obs(R s ) ∪ R d is orthogonal, too. So it is confluent. Since we assume termination, we conclude that every ground term of sort d has a unique normal form with respect to Assume such a normal form of sort d contains a symbol from Σ s ∪ {:}. Choose such a symbol with minimal position, that is, closest to the root. Since the term is of sort d, this symbol is not the root. Hence it has a parent. Due to minimality of position, this parent is either head or tail. Due to the shape of the rules of Obs(R s ), a rule of Obs(R s ) is applicable on this parent position, contradicting the normal form assumption. So the normal form only contains symbols from Σ d . Since it is also a normal form with respect to R d , such a normal form is an element of D. Now for t ∈ T s and n ∈ N we define [t] 1 (n) = the normal form of head(tail n (t)) with respect to Obs(R s ) ∪ R d , in this way defining [t] 1 ∈ D ω .  Proof. First we extend the definition of [·] 1 to all ground terms over Σ s ∪ Σ d ∪ {:, head, tail}. For ground terms t of sort s we define it by [t] 1 (n) = the normal form of head(tail n (t)) with respect to Obs(R s ) ∪ R d , and for ground terms u of sort d we define [u] 1 to be the normal form of u with respect to Obs(R s ) ∪ R d . We prove the following claim.
We prove the claim for every such term head(T ) by induction on >. Claim 1 is trivial if t = T , so we may assume that T = f (u 1 , . . . , u n , t 1 , . . . , t m ) such that t occurs in u 1 , . . . , u n , t 1 , . . . , t m , and either f ∈ Σ s ∪{:, tail}, and T = f (u 1 , . . . , u n , t 1 , . . . , t m ). For every subterm of u i of the shape head(· · · ) we may apply the induction hypothesis, In case the root of T is not tail we rewrite and then continue by the rule head(f (· · · )) → · · · in Obs(R s ), yielding a term U of sort d. As head is the only symbol of sort d having an argument of sort s, the only way such a term can contain t as a subterm is by which we had to prove.
In case the root of T is tail then write for f ∈ Σ s ∪ {:}. This can be rewritten by the rule tail(f (· · · )) → · · · in Obs(R s ), yielding V . Note that for applicability of this rule it is essential that the arguments of f in the left-hand side are variables, which was achieved by first applying the transformation P.
On the same position using the same rule we can rewrite T → Obs(Rs) V for V obtained from V by replacing one or more occurrences of t by t .  Next we will prove that it satisfies the specification, and essentially is the only one doing so. Lemma 5.3. Let → r ∈ R s and let ρ be a substitution. Then • there is a term t such that head( ρ) → * Obs(Rs) t and head(rρ) → * Obs(Rs) t, and • there is a term t such that tail( ρ) → * Obs(Rs) t and tail(rρ) → * Obs(Rs) t.
Proof. Let f be the root of . Define ρ by σρ = xρ : σρ for every argument of the shape x : σ of f in , and ρ coincides with ρ on all other variables. Then head( ρ) = ρ for some rule in → r in Obs(R s ). Now a common reduct t of r ρ and head(rρ) is obtained by applying the rule head(x : σ) → x zero or more times. This yields head( ρ) = ρ → Obs(Rs) r ρ → * Obs(Rs) t and head(rρ) → * Obs(Rs) t. The argument for tail( ρ) and tail(rρ) is similar.   for every → r ∈ E. From the definition of Obs(R s ) it is easily checked that any innermost step t → Obs(Rs) t on a ground term t is either an application of one of the first two rules of E, or it is of the shape t → * E · → Rs · → * E t where due to the innermost requirement the redex of the → Rs step does not contain the symbols head or tail so is in T s . Since the model is assumed to satisfy the specification (Σ d , Σ s , R d , R s ), we conclude that [t] = [t ] for every innermost ground step t → Obs(Rs) t .
For the lemma we have to prove that [t](i) = [t] 1 (i) for every i ∈ N. By definition [t] 1 (i) is the normal form with respect to Obs(R s ) ∪ R d of head(tail i (t)). Now consider an innermost Obs(R s ) ∪ R d -reduction of head(tail i (t)) to [t] 1 (i). By the above observation and the definitions of [head] and [tail] we conclude that We conclude this section by an example of a well-defined proper stream specification that is not productive. c = 1 : c f (x : σ) = g(f (σ)) g(x : σ) = c. This is a valid proper stream specification for which Obs(R s ) is terminating, as can be shown by AProVE [6] or TTT2 [10]. Hence by Theorem 5.1 it is well-defined. So the ground term f (c) has a unique interpretation: the stream only consisting of 1's. However, f (c) is not productive, as it only reduces to terms having f or g on top.
So the TRS R s uniquely defines f (c), but is not suitable to compute its interpretation.

Implementation
In http://www.win.tue.nl/~hzantema/str.zip we offer a prototype implementation automating proving well-definedness of boolean stream specifications by the approach we proposed. The main feature is to generate the observational variant for any given boolean stream specification. Being only a prototype, the focus is on testing simple examples as they occur in this paper. The default version runs under Windows with a graphical user interface and provides the following features: • Boolean stream specifications can be entered, loaded, edited and stored. The format is the same as given here, with the only difference that for the operator ':' a prefix notation is chosen, in order to be consistent with the user defined symbols. • By clicking a button the observational variant of the current stream specification is tried to be created. In doing so, all requirements of the definition of stream specification are checked. If they are not fulfilled, an appropriate error message is shown. • If all requirements hold, then the resulting observational variant is shown on the screen by which it can be entered by cut and paste in a termination tool. Alternatively, it can be stored. • Alternatively, the stream specification can be transformed to Circ format. This occurs in two variants: • a basic variant in which the Circ proof goal should be added manually, and • a version generating two copies of the specification and generating goals for these to be equivalent. Again it is shown on the screen with cut and paste facility, or the result can be stored, both for entering the result in the tool Circ.
• A term can be entered, and an initial part of the stream represented by this term can be computed. • For unary symbols the process of unfolding as described in Section 3 is supported.
• Several stream specifications, including the Fibonacci stream (the variant as we will present in Example 7), the Thue-Morse stream (Example 1), the paper folding stream (Example 5 below) and the Kolakoski stream (Example 9) are predefined. For all of these examples termination of the observational variant can be proved fully automatically both by AProVE [6] and TTT2 [10], proving well-definedness of the given stream specification. Apart from this graphical Windows version there is also a command line version to be run under Linux. This provides the main facility, that is, generates the observational variant in term rewriting format in case the syntax is correct, and generates an appropriate error message otherwise.
None of the actions require substantial computation: for all features the result shows up instantaneously. On the other hand, proving termination of a resulting observational variant by a tool like AProVE or TTT2 may take some computation time, although never more than a few seconds for the given examples. This was one of the objectives of the project: the transformation itself should be simple and direct, while the real work to be done makes use of the power of current termination provers.
We conclude this section by an interesting stream specification that can be dealt with by our implementation. Just like in the introduction for Fib, and later in Section 8 we also show a turtle visualization. These and others are made by a few lines of code traversing a boolean array containing the first N elements of a stream. These first N elements are determined by executing outermost rewriting with respect to R s starting in the constant representing the intended stream, until the first N elements have been computed. Example 5. Start by a ribbon of paper. Fold it half lengthwise. Next fold the folded ribbon half lengthwise again, and repeat this a number of times, every time folding in the same direction. Now by unfolding the ribbon one sees a sequence of top-folds and valleyfolds, and the question is what is the pattern in this sequence. A first observation is that this pattern is the first half of the pattern obtained when folding once more, so every such sequence is a proper prefix of the next sequence. As a consequence, we can take the limit, obtaining a boolean stream P , called the paper folding stream, in which top folds and valley folds are represented by 0 and 1, respectively.
Imagine what happens if we do an extra fold. Then all existing folds remain, but between every two consecutive folds a new fold is created. These new folds are alternately top folds and valley folds. So the effect of folding once more is that the new sequence is the zip of 010101 · · · and the old sequence. Taking the limit we obtain where for zip and alt we have the equations zip(x : σ, τ ) = x : zip(τ, σ), alt = 0 : 1 : alt.
One may wonder whether P is already fully defined by these three equations for P , zip and alt. It is, by Theorem 5.1, since the equations form a proper stream specification R s for which termination of Obs(R s ) is easily proved by TTT2 or AProVE.
Paper folding and many of its properties is folklore; we found this characterization of P independently. Turtle visualization of P is of particular interest, since the result is not just a visualization, but also the shape obtained if the ribbon is not fully unfolded, but only unfolded until the angles given as parameter of the turtle visualization. We only consider the case where the angles for 0 (top fold) and 1 (valley fold) are equal. In case this angle is 90 degrees, then the result is called the dragon curve; this curve touches itself, but does not intersect itself. Pictures are easily found on the Internet. When choosing turtle angles of less than 90 degrees, that is, the remaining paper fold is greater than 90 degrees, then the curve neither touches nor intersects itself. Doing this for 87 degrees and doing 15 folds, this yields the following turtle visualization of the first 2 15 − 1 = 32767 elements of the stream P :

Data Independent Stream Functions
The reason that in Theorem 5.1 we have to restrict to models satisfying S = {[t] | t ∈ T s }, as we saw in Example 3, is in the fact that computations may be guarded by data elements in left-hand sides of equations. Next we show that we also get well-definedness for stream functions defined on all streams in case the left-hand sides of the equations do not contain data elements. Proof. (sketch) We have to prove that for any f ∈ Σ s of type d n × s m → s the function [f ] : D n × (D ω ) m → D ω is uniquely defined. For doing so we introduce m fresh constants c 1 , . . . , c m of sort s. Let k ∈ N and u 1 , . . . , u n ∈ D. Due to termination and orthogonality of Obs(R s ) ∪ R d , the term head(tail k (f (u 1 , . . . , u n , c 1 , . . . , c m ))) has a unique normal form with respect to Obs(R s ) ∪ R d . Since it is of sort d, due to the shape of the rules it is a ground term of sort d over Σ d ∪ {head, tail, c 1 , . . . , c m }, that is, a ground term T composed from Σ d and terms of the shape head(tail i (c j )) for i ∈ N and j ∈ {1, . . . , m}. For this observation it is essential that left-hand sides do not contain non-variable terms of sort d: terms of the shape f (head(· · · ), · · · ) should be rewritten.
Observe that head(tail i (t j )) rewrites to s j (i) ∈ D.
So ([f ](u 1 , . . . , u n , s 1 , . . . , s m ))(k) has to be the R d -normal form of the ground term over Σ d obtained from T by replacing every subterm of the shape head(tail i (c j )) by s j (i) ∈ D. Since this fixes ([f ](u 1 , . . . , u n , s 1 , . . . , s m ))(k) for every k, this uniquely defines [f ]. Example 6. It is easy to see that for the standard stream functions zip, even and odd defined by even(x : σ) = x : odd(σ), odd(x : σ) = even(σ), zip(x : σ, τ ) = x : zip(τ, σ), there exists f : D ω → D ω for every data set D satisfying f (x : σ) = x : zip(f (even(σ)), f (odd(σ))), namely the identity. By Theorem 7.1 we can conclude it is the only one, since for R d = ∅ and R s consisting of the above four equations, the resulting TRS Obs(R s ) consisting of the rules head(even(σ)) → head(σ) head(odd(σ)) → head(even(tail(σ))) tail(even(σ)) → odd(tail(σ)) tail(odd(σ)) → tail(even(tail(σ))) head(f (σ)) → head(σ) tail(f (σ)) → zip(f (even(tail(σ))), f (odd(tail(σ)))) and the rules for ':' and zip as in Example 2, is terminating as can be proved by AProVE [6] or TTT2 [10]. Other approaches seem to fail: the technique from [16] fails to prove that the identity is the only stream function satisfying the equation for f , while productivity of stream specifications containing the rule for f cannot be proved to be productive by the technique from [4]. By essentially choosing Obs(R s ) as the input and adding information about special contexts, the tool Circ [11] is able to prove that f is the identity.

More Transformations Preserving Semantics
Unfolding the Fibonacci stream specification as given in the introduction yields the proper stream specification R s consisting of the equations However, the TRS Obs(R s ) is not terminating since it allows the infinite reduction tail(Fib) → tail(f (Fib)) → tail(g(head(Fib), tail(Fib) )) → · · · , so our method fails to prove well-definedness of Fib in a direct way. In Lemma 3.2 and Lemma 3.3 we already saw two ways to modify stream specifications while preserving their semantics. In this section we will extend these lemmas to more general semantics preserving transformations, in particular by making use of the equations E from the proof of Lemma 5.5 that hold in every model. As an example, we will apply such transformations to our original Fib specification. The observational variant of the resulting stream specification will be terminating, so proving well-definedness of the transformed Fib specification. But since the transformations are semantics preserving, this also proves well-definedness of the original Fib specification. In general we propose the following approach: in case for a stream specification the termination tools fail to prove termination of the observational variant, then try to apply semantics preserving transformations as discussed in Section 3 and this section until a transformed system has been found for which termination of the observational variant can be proved. If this succeeds, this not only proves well-definedness of the transformed specification, but also of the original one.
In this approach we have a symbol tail in several variants of the specification, while in the construction of observational variant a fresh symbol tail is required. So in the observational variant two versions of tail occur: the original symbol tail and the symbol tail created by Obs. However, if the observational variant happens to be terminating after identifying these two versions of tail, then it is also terminating if they are distinguished, so identifying them will not yield wrong results. But it may happen that termination holds if the two versions of tail are distinguished, and does not hold if they are identified. This is the case for Example 2.
Recall that mapping a stream specification In order not to change our definitions, throughout this section we assume the following extra assumptions to achieve this requirement: • the symbol tail is in Σ s , and • the corresponding equation tail(x : σ) = σ is in R s . Lemma 3.3 states that in keeping the same signature Σ s , replacing R s by R s preserves semantics as long as convertibility with respect to R s coincides with convertibility with respect to R s . But as we are interested in preservation of semantics, this syntactical convertibility requirement may be weakened to a more semantic version: if R s and R s do not have the same convertibility relation, but allow the same models, the same can be concluded. So now for a set R s of equations we will introduce a congruence ∼ Rs being weaker than = Rs , but still preserving semantics.
Recall the set E of equations For a set R s of equations of sort s we define the relation ∼ Rs on terms over Σ s ∪Σ d ∪{head} inductively by • if = r is in R s then ∼ Rs r, • ∼ Rs is reflexive, symmetric and transitive, • if C is a context and ρ is a substitution and t ∼ Rs t , then • if t, t are terms that may contain a fresh variable x of type d, and t[x := u] ∼ Rs t [x := u] for every u ∈ D, then t ∼ Rs t . Note that = Rs is defined by the first three items, so ∼ Rs generalizes = Rs by the additional last two items. Combining these observations we conclude by induction on the structure of ∼ Rs that if a model satisfies R s , and t ∼ Rs t , then the model satisfies the equation t = t too. Applying this both for ∼ Rs and ∼ R s , and using the conditions of the lemma we conclude that a model satisfies (Σ d , Σ s , R d , R s ) if and only if it satisfies (Σ d , Σ s , R d , R s ). From this the lemma follows. So the requirements of Lemma 8.1 are fulfilled and we conclude that transforming R s to R s preserves semantics. By tools like AProVE or TTT2 one proves that Obs(R s ) is terminating, so by Theorem 5.1 R s is well-defined. Due to preservation of semantics the same holds for R s , and for the original Fib specification.

HANS ZANTEMA
As a consequence, we can conclude incompleteness of Theorem 5.1: the stream specification R s is well-defined but Obs(R s ) is not terminating, due to the infinite reduction of Obs(R s ) we saw before.
The argument for the Fib example can be given in a more sloppy way as was done in [19] as follows. Identify ground terms with their interpretations in a model. The result of g always starts by 0, so we can write Fib = f (Fib) = g(· · · ) = 0 : c for some stream c. As R s satisfies our format and Obs(R s ) is terminating we conclude well-definedness of Fib.
For justifying the steps f (Fib) = g(· · · ) = 0 : c in this argument we need the last two items of the definition of ∼ Rs : • for the step f (Fib) = g(· · · ) we need E to rewrite Fib to a term with ":" on top, and • for the step g(· · · ) = 0 : c we need the case analysis on the data element in "· · · " as expressed by the last item in the definition of ∼ Rs , exactly as we did in our detailed proof. Note that the sloppy argument only shows that the new equations in R s are implied by original equations, and not the other way around. The following example shows that it is essential also to prove the other direction. So both for f and g its result on a stream is defined as follows. If a 1 is read, then a single symbol is produced, and if a 0 read, then two copies of a symbol are produced. This producing is done in such a way that the produced elements are alternately 0's and 1's, for f starting with 0 and for g starting with 1. Due to this procedure in some presentations instead of 0 the number 2 is written. Of course we have to prove that g has a unique fix point Kol. Similar to what we saw for Fib, the fix point equation Kol = g(Kol) causes non-termination in the observational variant so we cannot apply our approach directly. In order to prove well-definedness, we follow the same lines as we did for Fib, with the difference that now we do not start by unfolding, but postpone unfolding to the end. Start by the four equations for f and g, and the equations Kol = g(Kol) and tail(x : σ) = σ. According to Lemma 3.2 addition of 24 HANS ZANTEMA the equation K = tail(tail(Kol)) is semantics preserving. So let R s consist of all of these equations for f, g, Kol, tail, K. We will transform this to R s consisting of the equations for f, g, tail, and the two equations Kol = 1 : 0 : K, K = 0 : g(K).
Applying unfolding (Theorem 3.4) to R s yields a proper stream specification for which TTT2 and AProVE succeed in proving termination of the observational variant, so by Lemma 8.1 it remains to show that ∼ R s r for all = r in R s , and ∼ Rs r for all = r in R s . For doing so, first we show that head(g(0 : σ)) ∼ Rs 1 and head(g(1 : σ)) ∼ Rs 1, so head(g(x : σ)) ∼ Rs 1, and hence head(g(σ)) ∼ Rs head(g(head(σ) : tail(σ))) ∼ Rs 1.
For the other direction we have g(Kol) ∼ R s g(1 : 0 : K) ∼ R s 1 : f (0 : K) ∼ R s 1 : 0 : 0 : g(K) ∼ R s 1 : 0 : K ∼ R s Kol and K ∼ R s tail(tail(1 : 0 : K)) ∼ R s tail(tail(Kol)), concluding the proof. Although this stream Kol has a very simple and regular definition, the stream seems to behave remarkably irregular. In contrast to earlier streams we saw, turtle visualizations of Kol show up hardly any regular pattern: they seem to behave just like randomly generated boolean streams. For instance, by choosing the angle to be 90 degrees both for 0 and 1, taking the first 50000 elements of Kol yields the following turtle visualization:

Conclusions and Further Research
We presented a technique by which well-definedness of stream specifications like Example 3 can be proved fully automatically, where a tool like Circ [12,11] fails, and the productivity tool [4] fails to prove productivity. The main idea is to prove well-definedness by proving termination of a transformed system Obs(R s ), in this way exploiting the power of present termination provers.
We observed that productivity of the stream specification cannot be concluded from termination of Obs(R s ). Intuitively, productivity is closely related to termination; we leave as a challenge to further relate termination with productivity of stream specifications. A first step in this direction was made in [21]. There it was proved that productivity of a stream specification is equivalent to balanced outermost termination of the specification extended by an extra rule x : σ → overflow. Here an infinite reduction is called balanced outermost if only outermost redexes are reduced, and in the choice of them some fairness condition holds. As there are powerful techniques to prove outermost termination automatically [5], this can be used to prove productivity fully automatically. Unfortunately, as soon as binary operations like zip come in, typically the notions outermost termination and balanced outermost termination do not coincide: for many productive stream specifications the extension by the overflow rule is not outermost terminating, by which this approach fails. Instead in [20] some basic criteria for productivity have been investigated together with relationship with context-sensitive termination. Combined with a number of transformations and corresponding heuristics, this yields a powerful technique for proving productivity automatically, supported by an implementation. This approach exploits the power of present termination provers for proving productivity, just like we do in this paper for proving well-definedness.
We offer an implementation for computing Obs(R s ) automatically, by which proving well-definedness can be done fully automatically in case the approach applies directly. For cases for which the approach does not apply directly, in Section 3 and Section 8 we developed techniques to transform stream specifications in such a way that semantics and welldefinedness is preserved, and often our approach applies to the transformed specifications. Among these techniques only unfolding (Theorem 3.4) is supported by our implementation. For the other techniques some heuristics will be required. For the Fibonacci stream (Example 7) and the Kolakoski stream (Example 9) the following heuristics turned out to be successful: • Identify a non-productive constant c. In both mentioned examples this is the stream to be defined, for which the equation is of the shape c = f (c). Another approach of using the techniques of Section 3 and Section 8 is proving welldefinedness of a stream specification by proving productivity of all ground terms in a transformed specification, e.g., by the approach of [20]. Since productivity implies welldefinedness and the transformation preserves semantics, this implies well-definedness of the original specification.
In Section 8 we used the technical assumption that the model is closed under tail. This was forced by assuming the equation tail(x : σ) = σ. We conjecture that for the validity of the approach this is not essential. More precisely, we conjecture that a stream specification (Σ d , Σ s , R d , R s ) with tail ∈ Σ s is well-defined if and only if the extended specification (Σ d , Σ s ∪ {tail}, R d , R s ∪ {tail(x : σ) = σ}) is well-defined. This looks trivial as tail does not occur in the original specification, so is not expected to influence anything. However, giving a formal proof causes problems. The reason is that the model for (Σ d , Σ s , R d , R s ) may not be closed under [tail]. In fact we can even prove that in the model (S, [·]) for the Fib example satisfying S = {[t] | t ∈ T s }, the tail of Fib is not contained in S. A problem is how to lift [f ] defined on S to the larger model that is closed under tail. For the particular Fib example a solution can be given, but for the general setting we failed.
This paper purely focuses on streams over a fixed data set D; in all examples even D consists of the booleans. It is expected that the approach can be generalized to other infinite data types like infinite binary trees. A suitable format for this more general kind of infinite data structures has been given in [20]. In such a setting destructors can be defined as inverses of the constructors, just like in this paper we introduced (head, tail) as the inverse of ':'. Similar to what we did in this paper for streams, in this more general setting a specification consisting of equations on terms over constructors and user defined symbols will be transformed to an observational variant, being a rewrite system over destructors and the user defined symbols. Just like we did in this paper for the special case of streams, this rewrite system serves for observing data. It is orthogonal by construction, and welldefinedness can be concluded from termination. Although the agenda for this approach for other infinite data structures is similar to what we did in this paper, this has not been elaborated in detail.