It is natural to consider an interpretation of coinductive types where the elements of a coinductive type are possibly infinite terms. Each finite term of type containing fixpoint operators then “unfolds” to a possibly infinite term without fixpoint operators in the interpretation of . For instance, one would interpret the type of binary streams as the set of infinite terms of the form where and is an infix notation for the stream constructor. Then any fixpoint definition of a term of this type should “unfold” to such an infinite term. We believe this kind of interpretation corresponds closely to a naive understanding of infinite objects and coinductive types, and is thus worth investigating in its own right.
This paper is devoted to a study of such an interpretation in the context of infinitary rewriting. Infinitary rewriting extends term rewriting by infinite terms and transfinite reductions. This enables the consideration of “limits” of terms under infinite reduction sequences.
We consider a combination of simple function types with strictly positive nested higher-order inductive and coinductive types. An example of a higher-order coinductive type is the type of trees with potentially infinite branches and two kinds of nodes: nodes with a list of finitely many children and nodes with infinitely many children specified by a function on natural numbers. In our notation this type may be represented as the coinductive definition which intuitively specifies that each element of is a possibly infinite term which has one of the forms:
where each is an element of and is the finite list constructor, or
where is a term which represents a function from to .
We interpret each type as a subset of the set of finite and infinite terms. This interpretation may be seen as a refinement and generalization of the notion of productivity in term rewriting to a setting with higher-order functions and more complex (co)inductive data structures.
For example, the interpretation of the coinductive type of streams of natural numbers with a single constructor consists of all infinite terms of the form where for . The interpretation of an arrow type is the set of all terms such that for every there is with , where denotes the infinitary reduction relation. This means that is productive – it computes (in the limit) a stream when given a stream as an argument, producing any initial finite segment of the result using only an initial finite segment of the argument. Note that the argument is just any infinite stream of natural numbers – it need not even be computable. This corresponds with the view that arguments to a function may come from an outside “environment” about which nothing is assumed, e.g., the argument may be a stream of requests for an interactive program.
One could informally argue that including infinite objects explicitly is not necessary, because it suffices to consider finite “approximations” of “size” of an infinite argument object (which itself is possibly not computable), and if reduces to progressively larger approximations of an infinite object for progressively larger , then this “defines” the application of to , because to compute any finite part of the result it suffices to take a sufficiently large approximation as an argument. We actually make this intuition precise in the framework of infinitary rewriting. We show that if for every approximation of size of an infinite object the application reduces to an approximation of an infinite object of the right type, with the result approximations getting larger as gets larger, then there is a reduction starting from which “in the limit” produces an infinite object of the right type. For nested higher-order (co)inductive types this result turns out to be surprisingly non-trivial.
The result mentioned above actually follows from the approximation theorem which is the central technical result of this paper. It may be stated as follows: if for each then there is with , where is a coinductive type and is the set of approximations of size of the (typically infinite) objects of type (i.e. of the terms in ).
In the second part of the paper we consider finite terms decorated with type annotations. We present a type system which gives a sufficient but incomplete syntactic correctness criterion for such terms. The system enables reasoning about sizes of (co)inductive types, similarly as in systems with sized types. Using the approximation theorem we show soundness: if a finite decorated term may be assigned type in our type system, then there is such that , where denotes the term with type decorations erased. This means that every typable term has a well-defined interpretation in the corresponding type, which may be obtained as a limit of a reduction sequence starting from .
Our definition of the rewriting semantics is relatively straightforward and it is not difficult to prove it sound for a restricted form of non-nested first-order (co)inductive types. However, once we allow parameterized nested higher-order inductive and coinductive types significant complications occur because of the alternation of least and greatest fixpoints in the definitions. Our main technical contribution is the proof of the approximation theorem.
We stress again that the focus of the present paper is on the rewriting semantics, the approximation theorem and the soundness proof. With nested higher-order (co)inductive types the proof of the approximation theorem becomes fairly involved. The type system itself presented in the second part of the paper is not a significant improvement over the state-of-the-art in type systems based on sized types. It is mostly intended as an illustration of a system for which our rewriting semantics is particularly perspicuous. In other words, the main purpose of this paper is to define an infinitary rewriting semantics, to precisely state and prove the approximation theorem, and to show that the approximation theorem may be used to derive soundness of the rewriting semantics for systems based on sized types. We believe the rewriting semantics presented here provides a natural generalization of the notion of productivity in first-order term rewriting, and is thus worth studying on its own merits.
1.1. Related work
The notion of productivity dates back to the work of Dijkstra [Dijkstra1980], and the later work of Sijtsma [Sijtsma1989]. Our rewriting semantics may be considered a generalization of Isihara’s definition of productivity in algorithmic systems [Isihara2008], of Zantema’s and Raffelsieper’s definition of productivity in infinite data structures [ZantemaRaffelsieper2010], and of the definition of stream productivity [Endrullis2010, EndrullisGrabmayerHendriks2008, EndrullisHendriks2011]. In comparison to our setting, the infinite data structures considered before in term rewriting literature are very simple. None of the papers mentioned allow higher-order functions or higher-order (co)inductive types. The relative difficulty of our main results stems from the fact that the data structures we consider may be much more complex.
Infinitary rewriting was introduced in [KennawayKlopSleepVries1995, KennawayKlopSleepVries1995b, KennawayKlopSleepVries1997]. See [KennawayVries2003] for more references and a general introduction.
In the context of type theory, infinite objects were studied by Martin-Löf [MartinLof1988] and Coquand [Coquand1993]. Gimenez [Gimenez1994] introduced the guardedness condition to incorporate coinductive types and corecursion into dependent type theory, which is the approach currently used in Coq. Sized types are a long-studied approach for ensuring termination and productivity in type theories [HughesParetoSabry1996, BartheFadeGimenezPintoUustalu2004, Abel2006, AbelPientka2016]
. In comparison to previous work on sized types, the type system introduced in the second part of this paper is not a significant advance, but as mentioned before this is not the point of the present work. In order to justify the correctness of systems with sized types, usually strong normalization on typable terms is shown for a restriction of the reduction relation. We provide an infinitary rewriting semantics. Our approach may probably be extended to provide an infinitary rewriting semantics for at least some of the systems from the type theory literature. This semantics is interesting in its own right.
In [SeveriVries2012a] infinitary weak normalization is proven for a broad class of Pure Type Systems extended with corecursion on streams (CoPTSs), which includes Krishnaswami and Benton’s typed -calculus of reactive programs [KrishnaswamiBenton2011]. This is related to our work in that it provides some infinitary rewriting interpretation for a class of type systems. The formalism of CoPTSs is not based on sized types, but on a modal next operator, and it only supports the coinductive type of streams.
Our work is also related to the work on computability at higher types [Longley2000], but we have not yet investigated the precise relationships.
Coinduction has been studied from a more general coalgebraic perspective [JacobsRutten2011]. In this paper we use a few simple proofs by coinduction and one definition by corecursion. Formally, they could be justified as in e.g. [KozenSilva2017, Sangiorgi2012, JacobsRutten2011, Czajka2018]. Our use of coinduction in this paper is not very involved, and there are no implicit corecursive function definitions like in [Czajka2018].
2. Infinitary rewriting
In this section we define infinitary terms and reductions. We assume familiary with the lambda calculus [Barendregt1984] and basic notions such as -conversion, substitution, etc. Prior familiarity with infinitary rewriting or infinitary lambda calculus [KennawayVries2003, KennawayKlopSleepVries1997] is not strictly necessary but nonetheless helpful.
We assume a countable set of variables, and a countable set of constructors. The set of all finite and infinite terms is given by
where and . We use the notation (resp. ) to denote a sequence of terms (resp. variables) of an unspecified length.
More precisely, the set is defined as an appropriate metric completion (analogously to [KennawayVries2003]), but the above specification is clear and the details of the definition are not significant for our purposes. We consider terms modulo -conversion. Below (Definition 2) we will present the terms together with the rewrite rules as an iCRS [KetemaSimonsen2011], which may be considered a formal definition of our rewrite system.
There are the following reductions:
In the -rule we require that the appropriate sequences and have the same lengths, all variables in each are pairwise distinct, and the constructors are all distinct. For instance, (assuming ), but , and do not have -reducts (assuming ). We usually write to denote a finitary reduction .
Following [EndrullisPolonsky2011, EndrullisHansenHendriksPolonskySilva2015, EndrullisHansenHendriksPolonskySilva2018], we define infinitary reduction coinductively.
We define the relation analogously to , but replacing with and with in the above.
A term is root-active if for every with there is a -redex such that . The set of root-active, or meaningless, terms is denoted by . By we denote equality of terms modulo equivalence of meaningless subterms. Meaningless terms are a technical notion needed in the proofs, because for infinitary rewriting confluence holds only modulo . Intuitively, meaningless terms have no “meaningful” interpretation and may all be identified. An example of a meaningless term is . Various other sets of meaningless terms have been considered in the infinitary lambda calculus [KennawayVries2003, Vries2016, SeveriVries2011, SeveriVries2011b, SeveriVries2005, KennawayOostromVries1999]. The set of root-active terms is a subset of each of them.
Note that if then to produce any finite prefix of only a finitary reduction from is necessary, i.e., any finite prefix of becomes fixed after finitely many reduction steps and afterwards all reductions occur only at higher depths. We may consider (resp. ) as defining a strongly convergent ordinal-indexed reduction sequence [KennawayVries2003] of length at most (resp. ), obtained by concatenating the finite reductions occurring in the coinductive derivation.
The proofs of the next three lemmas follow the pattern from [EndrullisPolonsky2011, Lemma 4.3-4.5].
If and then .
Coinduction with case analysis on , using that implies . ∎
If then .
Induction on , using Lemma 2. ∎
If then .
By coinduction, analyzing and using Lemma 2. ∎
The next lemma may be seen as a kind of compression lemma.
If then .
By coinduction, using Lemma 2. See for example [Czajka2018, Lemma 6.3] for details. ∎
The system of -reductions on infinitary terms may be presented as a fully-extended infinitary Combinatory Reduction System (iCRS) [KetemaSimonsen2011]. One checks that this iCRS is orthogonal. A reader not familiar with the iCRS formalism may skip the following definition.
The signature of the iCRS contains:
a distinct nullary symbol for each constructor,
a binary symbol denoting application,
a unary symbol denoting lambda abstraction, and
for each and each sequence of distinct constructors and each sequence of natural numbers , a symbol of arity .
The iCRS has the following rewrite rules:
for each symbol and each :
We assume to be pairwise distinct, for .
One sees that this iCRS corresponds to our informal presentation of terms and reductions, and that it is fully-extended and orthogonal.
Our coinductive definition of the infinitary reduction relation corresponds to, in the sense of existence, to the well-established notion of strongly convergent reduction sequences [KetemaSimonsen2011]. This is made precise in the next lemma.
iff there exists a strongly convergent reduction sequence from to .
This follows by a proof completely analogous to [Czajka2018, Theorem 6.4], [Czajka2015a, Theorem 48] or [EndrullisPolonsky2011, Theorem 3]. The technique originates from [EndrullisPolonsky2011]. Lemma 2 is needed in the proof. ∎
Because the iCRS is fully-extended and orthogonal, the following are consequences of some results in [KetemaSimonsen2009] and the previous lemma. Note that because all rules are collapsing, in our setting root-active terms are the same as the hypercollapsing terms from [KetemaSimonsen2009].
If then .
Follows from [KetemaSimonsen2009, Proposition 4.12]. ∎
If and then there is with and .
Follows from [KetemaSimonsen2009, Lemma 4.14]. ∎
The relation of infinitary reduction is confluent modulo , i.e., if and and then there exist such that and and .
Follows from [KetemaSimonsen2009, Theorem 4.17]. ∎
In this section we define the types for which we will provide an interpretation in our rewriting semantics. Some types will be decorated with sizes of (co)inductive types, indicating the type of approximations of a (co)inductive type of a given size.
Size expressions are given by the following grammar:
where is a size variable. We denote the set of size variables by . We use obvious abbreviations for size expressions, e.g., for , or for , or for , etc. Substitution of for the size variable in the size expression is defined in the obvious way.
We assume an infinite set of (co)inductive definition names . Types are defined by:
where is a type variable, is a size expression, is a size variable, and is a (co)inductive definition name.
A type is strictly positive if one of the following holds:
is closed (i.e. it contains no type variables),
is a type variable,
and is closed and is strictly positive,
and is strictly positive,
and each is strictly positive.
By (resp. ) we denote the set of all size variables occurring in (resp. ). By we denote the set of all type variables occurring in . By we denote the set of all free size variables occuring in (i.e. those not bound by any ).
Substitution , , is defined in the obvious way, avoiding size variable capture. We abbreviate simultaneous substitution to .
To each (co)inductive definition name we associate a unique (co)inductive definition. Henceforth, we will use (co)inductive definitions and their names interchangeably. Remember, however, that strictly speaking (co)inductive definitions do not occur in types, only their names do.
A coinductive definition for is specified by a defining equation of the form
where is the recursive type variable, and are the parameter type variables, and , and is the th constructor, and is the th constructor’s th argument type, and the following is satisfied:
are all strictly positive,
An inductive definition is specified analogously, but using instead of .
We assume that each constructor is associated with a unique (co)inductive definition .
We assume there is a well-founded order on (co)inductive definitions such that for every (co)inductive definition , each (co)inductive definition occurring in a constructor argument type of satisfies . The type variable is used as a placeholder for recursive occurrences of . We often write to denote : the argument types of the -th constructor. We usually present (co)inductive definitions in a bit more readable format by replacing the recursive type variable with the type being defined, presenting the constructor argument types in a chain of arrow types, and adding the type being defined as the target type of constructors. For instance, the inductive definition of lists is specified by
Formally, here , , and .
The inductive definition of natural numbers is specified by:
The coinductive definition of streams of natural numbers is specified by:
An expression of the form is a (co)inductive type, depending on whether is an inductive or coinductive definition. A type of the form is a decorated (co)inductive type. We drop the designator “decorated” when clear from the context. We write to denote that is a constructor for a (decorated) (co)inductive type or definition .
In a (co)inductive type , the types denote the parameters. Intuitively, we substitute for the parameter type variables of the (co)inductive definition .
By default, denotes a coinductive and an inductive definition. We use for inductive and for coinductive types, and for (co)inductive types when it is not important if it is inductive or coinductive. Analogously, we use , , for decorated (co)inductive types (with size ). We often omit the superscript in , overloading the notation.
Intuitively, denotes the type of objects of an inductive type which have size at most , and denotes the type of objects of a coinductive type which have size at least , i.e., considered up to depth they represent a valid object of type . For a stream , the type is the type of terms which produce (under a sufficiently long reduction sequence) at least initial elements of a stream. The type e.g. is the type of functions which when given as argument a stream of size (i.e. with at least initial elements well-defined) produce at least initial elements of a stream, where may occur in .
Note that the parameters to (co)inductive definitions may be other (co)inductive types with size constraints. For instance denotes the type of lists (of any length) whose elements are lists of length at most with elements of type . Note also that the recursive type variable may occur as a parameter of a (co)inductive type in the type of one of the constructors. For these two reasons we need to require that the parameter type variables occur only strictly positively in the types of the arguments of constructors. One could allow non-positive occurrences of parameter type variables in general and restrict the occurrences to strictly positive only for instantiations with types containing free size variables or recursive type variables. This would, however, introduce some tedious but straightforward technicalities in the proofs.
Infinite binary trees storing natural numbers in nodes may be specified by:
Trees with potentially infinite branches but finite branching at each node are specified by:
Here the type itself (formally, the recursive type variable ) occurs as a parameter of in the type of the constructor .
Infinite trees with infinite branching are specified by:
Here infinite branching is specified by a function from to .
Recall the coinducutive definition of the type from the introduction:
In this definition both finite branching via the constructor and infinite branching via are possible. In contrast to , and , the nodes of do not store any natural number values.
As an example of a nested higher-order (co)inductive type we consider stream processors from [HancockPattinsonGhani2009]. See also [AbelPientka2013, Section 2.3]. We define two types:
The type is a type of stream processors. A stream processor can either read the first element from the input stream and enter a new state depending on the read value (the constructor), or it can write an element to the output stream and enter a new state (the constructor). To ensure productivity, a stream processor may read only finitely many elements from the input stream before writing a value to the output stream. This is achieved by nesting the inductive type inside the coinductive type of stream processors.
The well-founded order on (co)inductive definitions essentially disallows mutual (co)inductive types. They may still be represented indirectly thanks to type parameters.
The types and
of odd and even natural numbers may be defined as mutual inductive types:
These are not valid inductive definitions in our formalism, but they may be reformulated as follows:
Now the type is represented by .
In the rest of this paper by “induction on a type ” we mean induction on the lexicographic product of:
the multiset extension of the well-founded order on (co)inductive definitions occurring in the type, and
the size of the type.
In this order, if with then each is smaller than .
4. Rewriting semantics
In this section we define our rewriting semantics. More precisely, we define an interpretation for each type .
By we denote a sufficiently large ordinal (see Definition 4), and by we denote the set of all ordinals not greater than . A size variable valuation is a function . Any size variable valuation extends in a natural way to a function from size expressions to . More precisely, we define: , , , , . To save on notation we identify ordinals larger than with , e.g., denotes the ordinal .
We interpret types as subsets of . By we denote an ordinal large enough so that any monotone function on (the powerset of ) reaches its least and greatest fixpoint in iterations. This ordinal exists, as we may take any ordinal larger than the cardinality of .
Given a type variable valuation , a size variable valuation , and a strictly positive type , we define a type valuation . This is done by induction on . We simultaneously also define valuation approximations and .
Let be a (co)inductive definition. We define a function so that for contains all terms of the form such that for .
For a coinductive definition and an ordinal we define the valuation approximation as follows:
if is a limit ordinal.
For an inductive definition and an ordinal we define the valuation approximation by:
if is a limit ordinal.
where is a (co)inductive type, , and are the parameter type variables of .
if and for every there is with .
if for every there is with .
For a closed type the valuation does not depend on , so we simply write instead. Whenever we omit the type variable valuation we implicitly assume the type to be closed.
In general, the interpretation of a type may contain terms which are not in normal form. This is because of the interpretation of function types and quantification over size variables (). If is a simple first-order (co)inductive type whose constructor argument types contain neither function types () nor quantification over size variables (), then contains only normal forms.
Thus, we do not show infinitary weak normalization for terms having function types. Nonetheless, our interpretation of is very natural and ensures productivity of regarded as a function: we require that for there is with . Moreover, it is questionable in the first place how sensible infinitary normalization is as a “correctness” criterion for terms of function types.
Recall the definitions of the types and from Example 3:
The elements of are the terms: . We use common number notation, e.g. for , etc. We usually write e.g. instead of . The elements of are all infinite terms of the form where .
Consider the term
We have . Indeed, let . Then with and . Thus .
Recall the definitions of , and form Example 3:
The interpretation consists of all infinite terms of the form
where . The interpretation consists of all potentially infinite terms of the form where . Finally, consists of all terms of the form where for every there is such that .
Recall the definition of stream processors from Example 3:
An example stream processor, i.e., an example element of is an infinite term satisfying the identity:
The stream processor odd drops every second element of a stream, e.g., it transforms the stream into . But e.g. the infinite term
is not in , because it nests infinitely many s.
If for every then . Moreover, for any .
Follows by induction on , using the fact for a constructor argument type as in Definition 3. ∎
If for then .
If for each parameter type variable of , then .
Induction on , generalizing over , and . ∎
If for each parameter type variable of , then .
Assume , i.e., for all type variables .
If is strictly positive then .
If is a (co)inductive definition then .
If then . In particular, the function is monotone.
Induction on , generalizing over . ∎
From the third point in the above lemma it follows that for , and for . Also, for a (co)inductive definition , by the Knaster-Tarski fixpoint theorem [Tarski1955], the function has the least and greatest fixpoints, which may be obtained by “iterating” starting with the empty or the full set, respectively, as in the definition of valuation approximations. For an inductive definition , the least fixpoint of is then , by how we defined . Analogously, for a coinductive definition the greatest fixpoint of is . Note that for we have .
A set is stable when:
if and then ,
if and then .
A type variable valuation is stable if is stable for each type variable . The following lemma implies that the interpretations of closed types are in fact stable.
Assume are strictly positive.
If is stable then so is .
If is stable then so is .
If and are stable then so is .
We show the first point by induction on , generalizing over . The remaining two points will follow directly from this proof.
First assume with . Then where and each is strictly positive. By the inductive hypothesis each is stable. Hence is also stable. We show that if is stable then so is . From this it follows by induction that is stable for any , and thus is stable. Let . Then where and and . Note that is stable, because is. Hence is stable by the inductive hypothesis.
Assume . Then with . We have because is stable. Thus .
Assume . Then with . We have because is stable. Thus .
If is a type variable then is stable because is.
Assume . Let .