# Coinduction

In computer science, coinduction is a technique for defining and proving properties of systems of concurrent interacting objects.
Coinduction is the mathematical dual to structural induction. Coinductively defined data types are known as codata and are typically infinite data structures, such as streams.
As a definition or specification, coinduction describes how an object may be "observed", "broken down" or "destructed" into simpler objects. As a proof technique, it may be used to show that an equation is satisfied by all possible implementations of such a specification.
To generate and manipulate codata, one typically uses corecursive functions, in conjunction with lazy evaluation. Informally, rather than defining a function by pattern-matching on each of the inductive constructors, one defines each of the "destructors" or "observers" over the function result.
In programming, co-logic programming (co-LP for brevity) "is a natural generalization of logic programming and coinductive logic programming, which in turn generalizes other extensions of logic programming, such as infinite trees, lazy predicates, and concurrent communicating predicates. Co-LP has applications to rational trees, verifying infinitary properties, lazy evaluation, concurrent logic programming, model checking, bisimilarity proofs, etc." Experimental implementations of co-LP are available from the University of Texas at Dallas and in the language Logtalk (for examples see) and SWI-Prolog.

## Description

In his book Types and Programming Languages, Benjamin C. Pierce gives a concise statement of both the principle of induction and the principle of coinduction. While this article is not primarily concerned with induction, it is useful to consider their somewhat generalized forms at once. In order to state the principles, a few preliminaries are required.

### Preliminaries

Let $U$ be a set and $F$ be a monotone function $2^{U}\rightarrow 2^{U}$, that is:
$X\subseteq Y\Rightarrow F(X)\subseteq F(Y)$
Unless otherwise stated, $F$ will be assumed to be monotone.
* X is F-closed if $F(X)\subseteq X$
* X is F-consistent if $X\subseteq F(X)$
* X is a fixed point if $X=F(X)$
These terms can be intuitively understood in the following way. Suppose that $X$ is a set of assertions, and $F(X)$ is the operation that yields the consequences of $X$. Then $X$ is F-closed when one cannot conclude anymore than has already been asserted, while $X$ is F-consistent when all of the assertions are supported by other assertions (i.e. there are no "non-F-logical assumptions").
The Knaster–Tarski theorem tells us that the least fixed-point of $F$ (denoted $\mu F$) is given by the intersection of all F-closed sets, while the greatest fixed-point (denoted $\nu F$) is given by the union of all F-consistent sets. We can now state the principles of induction and coinduction.

### Definition

* Principle of induction: If $X$ is F-closed, then $\mu F\subseteq X$
* Principle of coinduction: If $X$ is F-consistent, then $X\subseteq \nu F$

## Discussion

The principles, as stated, are somewhat opaque, but can be usefully thought of in the following way. Suppose you wish to prove a property of $\mu F$. By the principle of induction, it suffices to exhibit an F-closed set $X$ for which the property holds. Dually, suppose you wish to show that $x\in \nu F$. Then it suffices to exhibit an F-consistent set that $x$ is known to be a member of.

## Examples
### Defining a set of data types

Consider the following grammar of datatypes:
$T=\bot \;|\;\top \;|\;T\times T$
That is, the set of types includes the "bottom type" $\bot$, the "top type" $\top$, and (non-homogenous) lists. These types can be identified with strings over the alphabet $\Sigma =\{\bot ,\top ,\times \}$. Let $\Sigma ^{\leq \omega }$ denote all (possibly infinite) strings over $\Sigma$. Consider the function $F:2^{\Sigma ^{\leq \omega }}\rightarrow 2^{\Sigma ^{\leq \omega }}$:
$F(X)=\{\bot ,\top \}\cup \{x\times y:x,y\in X\}$
In this context, $x\times y$ means "the concatenation of string $x$, the symbol $\times$, and string $y$." We should now define our set of datatypes as a fixpoint of $F$, but it matters whether we take the least or greatest fixpoint.
Suppose we take $\mu F$ as our set of datatypes. Using the principle of induction, we can prove the following claim:
$\mu F$
To arrive at this conclusion, consider the set of all finite strings over $\Sigma$. Clearly $F$ cannot produce an infinite string, so it turns out this set is F-closed and the conclusion follows.
Now suppose that we take $\nu F$ as our set of datatypes. We would like to use the principle of coinduction to prove the following claim:
$\bot \times \bot \times \cdots \in \nu F$
Here $\bot \times \bot \times \cdots$ denotes the infinite list consisting of all $\bot$. To use the principle of coinduction, consider the set:
$\{\bot \times \bot \times \cdots \}$
This set turns out to be F-consistent, and therefore $\bot \times \bot \times \cdots \in \nu F$. This depends on the suspicious statement that
$\bot \times \bot \times \cdots =(\bot \times \bot \times \cdots )\times (\bot \times \bot \times \cdots )$
The formal justification of this is technical and depends on interpreting strings as sequences, i.e. functions from $\mathbb {N} \rightarrow \Sigma$. Intuitively, the argument is similar to the argument that $0.{\bar {0}}1=0$ (see Repeating decimal).

### Coinductive datatypes in programming languages

Consider the following definition of a stream:
This would seem to be a definition that is not well-founded, but it is nonetheless useful in programming and can be reasoned about. In any case, a stream is an infinite list of elements from which you may observe the first element, or place an element in front of to get another stream.

### Relationship with F-coalgebras

Consider the endofunctor $F$ in the category of sets:
${\begin{aligned}F(x)&=A\times x\\F(f)&=\langle \mathrm {id} _{A},f\rangle \end{aligned}}$
The final F-coalgebra $\nu F$ has the following morphism associated with it:
$\mathrm {out} :\nu F\rightarrow F(\nu F)=A\times \nu F$
This induces another coalgebra $F(\nu F)$ with associated morphism $F(\mathrm {out} )$. Because $\nu F$ is final, there is a unique morphism
${\overline {F(\mathrm {out} )}}:F(\nu F)\rightarrow \nu F$
such that
$\mathrm {out} \circ {\overline {F(\mathrm {out} )}}=F\left({\overline {F(\mathrm {out} )}}\right)\circ F(\mathrm {out} )=F\left({\overline {F(\mathrm {out} )}}\circ \mathrm {out} \right)$
The composition ${\overline {F(\mathrm {out} )}}\circ \mathrm {out}$ induces another F-coalgebra homomorphism $\nu F\rightarrow \nu F$. Since $\nu F$ is final, this homomorphism is unique and therefore $\mathrm {id} _{\nu F}$. Altogether we have:
${\begin{aligned}{\overline {F(\mathrm {out} )}}\circ \mathrm {out} &=\mathrm {id} _{\nu F}\\\mathrm {out} \circ {\overline {F(\mathrm {out} )}}=F\left({\overline {F(\mathrm {out} )}}\right)\circ \mathrm {out} )&=\mathrm {id} _{F(\nu F)}\end{aligned}}$
This witnesses the isomorphism $\nu F\simeq F(\nu F)$, which in categorical terms indicates that $\nu F$ is a fixed point of $F$ and justifies the notation.

#### Stream as a final coalgebra

We will show that Stream A is the final coalgebra of the functor $F(x)=A\times x$. Consider the following implementations:
These are easily seen to be mutually inverse, witnessing the isomorphism. See the reference for more details.

### Relationship with mathematical induction

We will demonstrate how the principle of induction subsumes mathematical induction. Let $P$ be some property of natural numbers. We will take the following definition of mathematical induction:
$0\in P\land (n\in P\Rightarrow n+1\in P)\Rightarrow P=\mathbb {N}$
Now consider the function $F:2^{\mathbb {N} }\rightarrow 2^{\mathbb {N} }$:
$F(X)=\{0\}\cup \{x+1:x\in X\}$
It should not be difficult to see that $\mu F=\mathbb {N}$. Therefore, by the principle of induction, if we wish to prove some property $P$ of $\mathbb {N}$, it suffices to show that $P$ is F-closed. In detail, we require:
$F(P)\subseteq P$
That is,
$\{0\}\cup \{x+1:x\in P\}\subseteq P$
This is precisely mathematical induction as stated.