Coinduction Explained

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."[1] Experimental implementations of co-LP are available from the University of Texas at Dallas[2] and in the language Logtalk (for examples see [3]) and SWI-Prolog.

Description

In [4] a concise statement is given 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

2U2U

, that is:

X\subseteqYF(X)\subseteqF(Y)

Unless otherwise stated,

F

will be assumed to be monotone.

X is F-closed if

F(X)\subseteqX

X is F-consistent if

X\subseteqF(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 you cannot conclude anymore than you've already asserted, while

X

is F-consistent when all of your 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

\muF

) is given by the intersection of all F-closed sets, while the greatest fixed-point (denoted

\nuF

) 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

\muF\subseteqX

Principle of coinduction: If

X

is F-consistent, then

X\subseteq\nuF

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

\muF

. 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\nuF

. Then it suffices to exhibit an F-consistent set that

x

is known to be a member of.

Examples

Consider the following grammar of datatypes:

T=\bot|\top|T x 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, x \}

. Let

\Sigma\leq

denote all (possibly infinite) strings over

\Sigma

. Consider the function

F:

\Sigma\leq
2

\Sigma\leq
2
:

F(X)=\{\bot,\top\}\cup\{x x y:x,y\inX\}

In this context,

x x y

means "the concatenation of string

x

, the symbol

x

, 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

\muF

as our set of datatypes. Using the principle of induction, we can prove the following claim:

All datatypes in

\muF

are finite

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

\nuF

as our set of datatypes. We would like to use the principle of coinduction to prove the following claim:

The type

\bot x \bot x \in\nuF

Here

\bot x \bot x

denotes the infinite list consisting of all

\bot

. To use the principle of coinduction, consider the set:

\{\bot x \bot x \}

This set turns out to be F-consistent, and therefore

\bot x \bot x \in\nuF

. This depends on the suspicious statement that

\bot x \bot x =(\bot x \bot x ) x (\bot x \bot x )

The formal justification of this is technical and depends on interpreting strings as sequences, i.e. functions from

N\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:[5]

data Stream a = S a (Stream a)

-- Stream "destructors"head (S a astream) = atail (S a astream) = astream

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

Source:[6]

F

in the category of sets:

F(x)=A x x

F(f)=\langleidA,f\rangle

The final F-coalgebra

\nuF

has the following morphism associated with it:

out:\nuFF(\nuF)=A x \nuF

This induces another coalgebra

F(\nuF)

with associated morphism

F(out)

. Because

\nuF

is final, there is a unique morphism

\overline{F(out)}:F(\nuF)\nuF

such that

out\circ\overline{F(out)}=F\left(\overline{F(out)}\right)\circF(out)=F\left(\overline{F(out)}\circout\right)

The composition

\overline{F(out)}\circout

induces another F-coalgebra homomorphism

\nuF\nuF

. Since

\nuF

is final, this homomorphism is unique and therefore

id\nu

. Altogether we have:

\overline{F(out)}\circout=id\nu

out\circ\overline{F(out)}=F\left(\overline{F(out)}\right)\circout)=idF(\nu

This witnesses the isomorphism

\nuF\simeqF(\nuF)

, which in categorical terms indicates that

\nuF

is a fixpoint 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 x x

. Consider the following implementations:

out astream = (head astream, tail astream)out' (a, astream) = S a astream

These are easily seen to be mutually inverse, witnessing the isomorphism. See the reference for more details.

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\inP(n\inPn+1\inP)P=N

Now consider the function

F:2N2N

:

F(X)=\{0\}\cup\{x+1:x\inX\}

It should not be difficult to see that

\muF=N

. Therefore, by the principle of induction, if we wish to prove some property

P

of

N

, it suffices to show that

P

is F-closed. In detail, we require:

F(P)\subseteqP

That is,

\{0\}\cup\{x+1:x\inP\}\subseteqP

This is precisely mathematical induction as stated.

See also

Further reading

Textbooks
Introductory texts
History
Miscellaneous

Notes and References

  1. Web site: Co-Logic Programming | Lambda the Ultimate.
  2. Web site: Gopal Gupta's Home Page.
  3. Web site: Logtalk3/Examples/Coinduction at master · LogtalkDotOrg/Logtalk3. GitHub.
  4. Web site: Types and Programming Languages. Benjamin C. Pierce. The MIT Press.
  5. Practical Coinduction. . 10.1.1.252.3961 .
  6. Book: https://link.springer.com/chapter/10.1007/978-3-642-32202-0_2. Generic Programming with Adjunctions. Ralf Hinze. Generic and Indexed Programming . Springer. Lecture Notes in Computer Science . 2012 . 7470 . 47–129 . 10.1007/978-3-642-32202-0_2 . 978-3-642-32201-3 .