Coinduction

From HandWiki - Reading time: 7 min

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.[citation needed] Coinductively defined 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 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 [math]\displaystyle{ U }[/math] be a set and [math]\displaystyle{ F }[/math] be a monotone function [math]\displaystyle{ 2^U \rightarrow 2^U }[/math], that is:

[math]\displaystyle{ X \subseteq Y \Rightarrow F(X) \subseteq F(Y) }[/math]

Unless otherwise stated, [math]\displaystyle{ F }[/math] will be assumed to be monotone.

X is F-closed if [math]\displaystyle{ F(X) \subseteq X }[/math]
X is F-consistent if [math]\displaystyle{ X \subseteq F(X) }[/math]
X is a fixed point if [math]\displaystyle{ X = F(X) }[/math]

These terms can be intuitively understood in the following way. Suppose that [math]\displaystyle{ X }[/math] is a set of assertions, and [math]\displaystyle{ F(X) }[/math] is the operation which takes the implications of [math]\displaystyle{ X }[/math]. Then [math]\displaystyle{ X }[/math] is F-closed when you cannot conclude anymore than you've already asserted, while [math]\displaystyle{ X }[/math] 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 [math]\displaystyle{ F }[/math] (denoted [math]\displaystyle{ \mu F }[/math]) is given by the intersection of all F-closed sets, while the greatest fixed-point (denoted [math]\displaystyle{ \nu F }[/math]) 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 [math]\displaystyle{ X }[/math] is F-closed, then [math]\displaystyle{ \mu F \subseteq X }[/math]
Principle of coinduction: If [math]\displaystyle{ X }[/math] is F-consistent, then [math]\displaystyle{ X \subseteq \nu F }[/math]

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 [math]\displaystyle{ \mu F }[/math]. By the principle of induction, it suffices to exhibit an F-closed set [math]\displaystyle{ X }[/math] for which the property holds. Dually, suppose you wish to show that [math]\displaystyle{ x \in \nu F }[/math]. Then it suffices to exhibit an F-consistent set which [math]\displaystyle{ x }[/math] is known to be a member of.

Examples

Defining a set of datatypes

Consider the following grammar of datatypes:

[math]\displaystyle{ T = \bot \;|\;\top \;|\; T \times T }[/math]

That is, the set of types includes the "bottom type" [math]\displaystyle{ \bot }[/math], the "top type" [math]\displaystyle{ \top }[/math], and (non-homogenous) lists. These types can be identified with strings over the alphabet [math]\displaystyle{ \Sigma = \{\bot, \top, \times\} }[/math]. Let [math]\displaystyle{ \Sigma^* }[/math] denote all strings over [math]\displaystyle{ \Sigma }[/math]. Consider the function [math]\displaystyle{ F: 2^{\Sigma^*} \rightarrow 2^{\Sigma^*} }[/math]:

[math]\displaystyle{ F(X) = \{\bot, \top\} \cup \{ x \times y : x,y \in X \} }[/math]

In this context, [math]\displaystyle{ x \times y }[/math] means "the concatenation of string [math]\displaystyle{ x }[/math], the symbol [math]\displaystyle{ \times }[/math], and string [math]\displaystyle{ y }[/math]." We should now define our set of datatypes as a fixpoint of [math]\displaystyle{ F }[/math], but it matters whether we take the least or greatest fixpoint.

Suppose we take [math]\displaystyle{ \mu F }[/math] as our set of datatypes. Using the principle of induction, we can prove the following claim:

All datatypes in [math]\displaystyle{ \mu F }[/math] are finite

To arrive at this conclusion, consider the set of all finite strings over [math]\displaystyle{ \Sigma }[/math]. Clearly [math]\displaystyle{ F }[/math] cannot produce an infinite string, so it turns out this set is F-closed and the conclusion follows.

Now suppose that we take [math]\displaystyle{ \nu F }[/math] as our set of datatypes. We would like to use the principle of coinduction to prove the following claim:

The type [math]\displaystyle{ \bot \times \bot \times \cdots \in \nu F }[/math]

Here [math]\displaystyle{ \bot \times \bot \times \cdots }[/math] denotes the infinite list consisting of all [math]\displaystyle{ \bot }[/math]. To use the principle of coinduction, consider the set:

[math]\displaystyle{ \{\bot \times \bot \times \cdots \} }[/math]

This set turns out to be F-consistent, and therefore [math]\displaystyle{ \bot \times \bot \times \cdots \in \nu F }[/math]. This depends on the suspicious statement that

[math]\displaystyle{ \bot \times \bot \times \cdots = \bot \times \bot \times \cdots \times \bot \times \bot \times \cdots }[/math]

The formal justification of this is technical and depends on interpreting strings as sequences, i.e. functions from [math]\displaystyle{ \mathbb{N} \rightarrow \Sigma }[/math]. Intuitively, the argument is similar to the argument that [math]\displaystyle{ 0.\bar{0}1 = 0 }[/math] (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) = a
tail (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]

Consider the endofunctor [math]\displaystyle{ F }[/math] in the category of sets:

[math]\displaystyle{ F(x) = A \times x }[/math]
[math]\displaystyle{ F(f) = \langle \mathrm{id}_A, f \rangle }[/math]

The final F-coalgebra [math]\displaystyle{ \nu F }[/math] has the following morphism associated with it:

[math]\displaystyle{ \mathrm{out}: \nu F \rightarrow F(\nu F) = A \times \nu F }[/math]

This induces another coalgebra [math]\displaystyle{ F(\nu F) }[/math] with associated morphism [math]\displaystyle{ F(\mathrm{out}) }[/math]. Because [math]\displaystyle{ \nu F }[/math] is final, there is a unique morphism

[math]\displaystyle{ \overline{F(\mathrm{out})}: F(\nu F) \rightarrow \nu F }[/math]

such that

[math]\displaystyle{ \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) }[/math]

The composition [math]\displaystyle{ \overline{F(\mathrm{out})} \circ \mathrm{out} }[/math] induces another F-coalgebra homomorphism [math]\displaystyle{ \nu F \rightarrow \nu F }[/math]. Since [math]\displaystyle{ \nu F }[/math] is final, this homomorphism is unique and therefore [math]\displaystyle{ \mathrm{id}_{\nu F} }[/math]. Altogether we have:

[math]\displaystyle{ \overline{F(\mathrm{out})} \circ \mathrm{out} = \mathrm{id}_{\nu F} }[/math]
[math]\displaystyle{ \mathrm{out} \circ \overline{F(\mathrm{out})} = F\left(\overline{F(\mathrm{out})}\right) \circ \mathrm{out}) = \mathrm{id}_{F(\nu F)} }[/math]

This witnesses the isomorphism [math]\displaystyle{ \nu F \simeq F(\nu F) }[/math], which in categorical terms indicates that [math]\displaystyle{ \nu F }[/math] is a fixpoint of [math]\displaystyle{ F }[/math] and justifies the notation.

Stream as a final coalgebra

We will show that

Stream A

is the final coalgebra of the functor [math]\displaystyle{ F(x) = A \times x }[/math]. 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.

Relationship with mathematical induction

We will demonstrate how the principle of induction subsumes mathematical induction. Let [math]\displaystyle{ P }[/math] be some property of natural numbers. We will take the following definition of mathematical induction:

[math]\displaystyle{ P(0) \and (P(n) \Rightarrow P(n+1)) \Rightarrow P(\mathbb{N}) }[/math]

Now consider the function [math]\displaystyle{ F: 2^{\mathbb{N}} \rightarrow 2^{\mathbb{N}} }[/math]:

[math]\displaystyle{ F(X) = \{0\} \cup \{x + 1 : x \in X \} }[/math]

It should not be difficult to see that [math]\displaystyle{ \mu F = \mathbb{N} }[/math]. Therefore, by the principle of induction, if we wish to prove some property [math]\displaystyle{ P }[/math] of [math]\displaystyle{ \mathbb{N} }[/math], it suffices to show that [math]\displaystyle{ P }[/math] is F-closed. In detail, we require:

[math]\displaystyle{ F(P) \subseteq P }[/math]

That is,

[math]\displaystyle{ \{0\} \cup \{x + 1 : x \in P \} \subseteq P }[/math]

This is precisely mathematical induction as stated.

See also

References

  1. "Co-Logic Programming | Lambda the Ultimate". http://lambda-the-ultimate.org/node/2513. 
  2. "Gopal Gupta's Home Page". http://www.utdallas.edu/~gupta/. 
  3. "Logtalk3/Examples/Coinduction at master · LogtalkDotOrg/Logtalk3". https://github.com/LogtalkDotOrg/logtalk3/tree/master/examples/coinduction. 
  4. Benjamin Pierce. "Types and Programming Languages". https://mitpress.mit.edu/9780262303828/types-and-programming-languages/. 
  5. Dexter Kozen , Alexandra Silva. "Practical Coinduction". CiteSeerX 10.1.1.252.3961.
  6. Ralf Hinze (2012). "Generic Programming with Adjunctions". Generic and Indexed Programming. Lecture Notes in Computer Science. 7470. Springer. pp. 47–129. doi:10.1007/978-3-642-32202-0_2. ISBN 978-3-642-32201-3. https://link.springer.com/chapter/10.1007/978-3-642-32202-0_2. 

Further reading

Textbooks
  • Davide Sangiorgi (2012). Introduction to Bisimulation and Coinduction. Cambridge University Press.
  • Davide Sangiorgi and Jan Rutten (2011). Advanced Topics in Bisimulation and Coinduction. Cambridge University Press.
Introductory texts
History
Miscellaneous




Licensed under CC BY-SA 3.0 | Source: https://handwiki.org/wiki/Coinduction
11 views | Status: cached on July 14 2024 00:30:57
↧ Download this article as ZWI file
Encyclosphere.org EncycloReader is supported by the EncyclosphereKSF