Corecursion - Discussion

Discussion

This section may require cleanup to meet Wikipedia's quality standards. The specific problem is: mix of discussion, examples, and related concepts. Please help improve this section if you can.
This section needs attention from an expert in Computer science. Please add a reason or a talk parameter to this template to explain the issue with the section. WikiProject Computer science or the Computer science Portal may be able to help recruit an expert.

The rule for primitive corecursion on codata is the dual to that for primitive recursion on data. Instead of descending on the argument by pattern-matching on its constructors (that were called up before, somewhere, so we receive a ready-made datum and get at its constituent sub-parts, i.e. "fields"), we ascend on the result by filling-in its "destructors" (or "observers", that will be called afterwards, somewhere - so we're actually calling a constructor, creating another bit of the result to be observed later on). Thus corecursion creates (potentially infinite) codata, whereas ordinary recursion analyses (necessarily finite) data. Ordinary recursion might not be applicable to the codata because it might not terminate. Conversely, corecursion is not strictly necessary if the result type is data, because data must be finite.

Here is an example in Haskell. The following definition produces the list of Fibonacci numbers in linear time:

fibs = 0 : 1 : next fibs where next (a: t@(b:_)) = (a+b):next t

This infinite list depends on lazy evaluation; elements are computed on an as-needed basis, and only finite prefixes are ever explicitly represented in memory. This feature allows algorithms on parts of codata to terminate; such techniques are an important part of Haskell programming.

This example employs a self-referential data structure. Ordinary recursion makes use of self-referential functions, but does not accommodate self-referential data. However, this is not essential to the Fibonacci example. It can be rewritten as follows:

fibs = fibgen (0,1) fibgen (x,y) = x : fibgen (y,x+y)

This employs only self-referential function to construct the result. If it were used with strict list constructor it would be an example of runaway recursion, but with non-strict list constructor it (corecursively) produces an indefinitely defined list.

This shows how it can be done in Python:

from itertools import tee, chain, islice, imap def add(x,y): return (x + y) def fibonacci: def deferred_output: for i in output: yield i result, c1, c2 = tee(deferred_output, 3) paired = imap(add, c1, islice(c2, 1, None)) output = chain(, paired) return result for i in islice(fibonacci,20): print i

Corecursion need not produce an infinite object; a corecursive queue is a particularly good example of this phenomenon. The following definition produces a breadth-first traversal of a binary tree in linear time:

data Tree a b = Leaf a | Branch b (Tree a b) (Tree a b) bftrav :: Tree a b -> bftrav tree = queue where queue = tree : trav 1 queue trav 0 q = trav len (Leaf _ : q) = trav (len-1) q trav len (Branch _ l r : q) = l : r : trav (len+1) q

This definition takes an initial tree and produces list of subtrees. This list serves a dual purpose as both the queue and the result. It is finite if and only if the initial tree is finite. The length of the queue must be explicitly tracked in order to ensure termination; this can safely be elided if this definition is applied only to infinite trees. Even if the result is finite, this example depends on lazy evaluation due to the use of self-referential data structures.

Another particularly good example gives a solution to the problem of breadth-first labelling. The function label visits every node in a binary tree in a breadth first fashion, and replaces each label with an integer, each subsequent integer is bigger than the last by one. This solution employs a self-referential data structure, and the binary tree can be finite or infinite.

label :: Tree a b -> Tree Int Int label t = t' where (t',ns) = label' t (1:ns) label' :: Tree a b -> -> (Tree Int Int, ) label' (Leaf _ ) (n:ns) = (Leaf n, n+1 : ns ) label' (Branch _ l r) (n:ns) = (Branch n l' r', n+1 : ns'') where (l',ns' ) = label' l ns (r',ns'') = label' r ns'

An apomorphism (such as an anamorphism, such as unfold) is a form of corecursion in the same way that a paramorphism (such as an catamorphism, such as fold) is a form of recursion.

The Coq proof assistant supports corecursion and coinduction using the CoFixpoint command.

Read more about this topic:  Corecursion

Famous quotes containing the word discussion:

    Bigotry is the disease of ignorance, of morbid minds; enthusiasm of the free and buoyant. Education and free discussion are the antidotes of both.
    Thomas Jefferson (1743–1826)

    We should seek by all means in our power to avoid war, by analysing possible causes, by trying to remove them, by discussion in a spirit of collaboration and good will. I cannot believe that such a programme would be rejected by the people of this country, even if it does mean the establishment of personal contact with the dictators.
    Neville Chamberlain (1869–1940)

    We cannot set aside an hour for discussion with our children and hope that it will be a time of deep encounter. The special moments of intimacy are more likely to happen while baking a cake together, or playing hide and seek, or just sitting in the waiting room of the orthodontist.
    Neil Kurshan (20th century)