Comments on: What the heck is coinduction?
http://ask.metafilter.com/42858/What-the-heck-is-coinduction/
Comments on Ask MetaFilter post What the heck is coinduction?Mon, 24 Jul 2006 19:58:39 -0800Mon, 24 Jul 2006 19:58:39 -0800en-ushttp://blogs.law.harvard.edu/tech/rss60Question: What the heck is coinduction?
http://ask.metafilter.com/42858/What-the-heck-is-coinduction
Can someone explain coinduction (mathematics, computer science) in English? <br /><br /> I'm studying a text book for a computer science class and I'm struggling with the concept as explained in the text. I'm perfectly comfortable with normal induction, but I don't know what a co-algebra or coinduction is.<br>
<br>
The book attempts to explain it in the form of abstract statements, with no concrete examples, and thus, I'm beginning to flounder. The web is even less helpful. Can anyone here make it clear?post:ask.metafilter.com,2006:site.42858Mon, 24 Jul 2006 15:08:36 -0800knavemathcomputerscienceinductioncoinductioncoalgebraBy: j.edwards
http://ask.metafilter.com/42858/What-the-heck-is-coinduction#658807
I'm not quite sure how to approach this. What's your background like? Which text are you using? Coinduction is not an exceedingly difficult topic, but it requires some unsightly machinery.comment:ask.metafilter.com,2006:site.42858-658807Mon, 24 Jul 2006 19:58:39 -0800j.edwardsBy: knave
http://ask.metafilter.com/42858/What-the-heck-is-coinduction#658828
Background: Bachelors in CS, minor in physics. Familiar with algebra, calculus, differential equations, linear algebra, discrete math, etc. etc.<br>
<br>
I should be able to get the material just fine, but I'm studying from a text book without actually taking a class, so no professor or lecturer to expand on what the book is saying.<br>
<br>
I just need someone to explain coinduction in english.comment:ask.metafilter.com,2006:site.42858-658828Mon, 24 Jul 2006 20:32:27 -0800knaveBy: knave
http://ask.metafilter.com/42858/What-the-heck-is-coinduction#658834
If it helps, my two insights thus far have been:<br>
<br>
1. Induction works on sets with a base case, or starting point, whereas coinduction works on streams, or sets with no beginning or end point.<br>
<br>
2. Induction works by starting with a base case and constructing a set from that point. Coinduction works by starting with the entire solution space, and stripping away the non-members of the solution set.<br>
<br>
If either or both are correct, it'd be nice just to hear that. If you can add to my current understanding, I'd be very happy. :)comment:ask.metafilter.com,2006:site.42858-658834Mon, 24 Jul 2006 20:35:57 -0800knaveBy: j.edwards
http://ask.metafilter.com/42858/What-the-heck-is-coinduction#658878
Sort of (bear in mind this isn't my field, but I'll say what I know). Coinduction as I learned it was a method of proving the equivalence of opaque structures by exploiting bisimilarity (simply put, given two (not necessarily finite) state automata with outputs in the same set, they are bisimilar if there exists a binary relation that "matches up" their state transitions and outputs). "The" bisimilarity relation on a given automaton is the union of all such binary relations from that automaton to itself. Coinduction is then the principle that given a map from states to output, that map distributes over the bisimilarity relation. The effective statement is that two bisimilar start states will arrive at the same output.<br>
<br>
As a method of proof, one would show that two states in a given automaton had the same output by demonstrating the existence of a bisimilarity relation between them.<br>
<br>
I'm hardly a pedagogue, so let me know if any of this is unclear.<br>
<br>
For the co-algebra connection, think of induction as a principle on minimal algebras -- the canonical example would be the algebra formed by zero and the successor function (the minimal algebra therefrom being the natural numbers). A "minimal" co-algebra likewise has no quotients under an equivalence operation -- so in this instance, one could perform coinduction on a coalgebra (say, of an automaton) that had no distinct elements (states) seperated by the equivalence relation (the bisimilarity relation). There's more content to this from a category-theoretic perspective, but I'm even more at a loss there and it sounds like that's not the angle you're going for.comment:ask.metafilter.com,2006:site.42858-658878Mon, 24 Jul 2006 21:16:35 -0800j.edwardsBy: jacobm
http://ask.metafilter.com/42858/What-the-heck-is-coinduction#659864
I've got a similar background to you — BA in computer science and sociology, MS in computer science, working on PhD. I've recently tried to figure out coinduction as well; my understanding is pretty shaky, but I'll try to explain what I know. I should preface all of this by saying that you really should just ignore me and read the best tutorial paper I've found on the subject, which is <a href="http://citeseer.ist.psu.edu/jacobs97tutorial.html">"A Tutorial on (Co)Algebras and (Co)Induction" by Bart Jacobs and Jan Rutten</a>. (But skip sections 4 through 6 at first, then come back and read them later.)<br>
<br>
My explanation in three parts (just like they do it): coinductive <i>data</i>, coinductive <i>functions</i>, and coinductive <i>proofs</i>. First part, coinductive data.<br>
<br>
So Jacobs and Rutten focus on infinite streams — like Haskell's lazy lists if that means anything to you. An infinite stream can be broken into its head (first element) and tail (another infinite stream), but it doesn't necessarily ever bottom out with empty, it might just go on forever. Think of the list [1,2,3,4,5, ...] which keeps ascending without bound. <br>
<br>
These data structures can't be defined inductively — in order for an inductive definition to be well-founded it's got to have a base case and these guys don't have one. But consider the inductive definition of lists: usually it's given as<br>
<ol><li>Nil is a list; and<li>If <i>xs</i> is a list then so is <i>cons(x,xs)</i> [where <i>x</i> is anything]</li></li></ol><br>
But if you think about it, there are lots sets of values that satisfy those constraints; the set of all possible values in the universe is one of them. Really when we give an inductive definition like that, we mean <i>the smallest set</i> that satisfies the given constraints; everything that's in the set has some justification, no random junk is thrown in there for fun.<br>
<br>
Coinductive definitions are the complement of that. Instead of taking the smallest set that satisfies the given constraints, we can take the <i>largest</i> set that is consistent with them, meaning that there is nothing in the set that couldn't possibly be produced by some number of applications (perhaps infinitely many) of the rules.<br>
<br>
Okay, it's probably not clear why those two things are different from each other. Let's have an example. Consider if we dropped the base case from our inductive definition of lists, and we just had "if <i>xs</i> is a list then so is <i>cons(x,xs)</i>". If it were an inductive definition, we'd be forced to say that there are no lists at all, because we have to interpret the statement as saying that the set <i>lists</i> is the smallest set such that if <i>xs</i> is in it then so is <i>cons(x,xs)</i> and the empty list satisfies that trivially. But as a <i>co</i>inductive definition, it gives you lots of lists: just start with everything you can imagine and carve away the things that couldn't possibly be produced by applications of the rule. Infinitely long lists will never get carved away like that, so they're all a part of the new definition.<br>
<br>
Alright, so that's what the data look like. Now how do we define functions on them? Just like we can define functions that process inductively-defined data by giving a case for every one of the constructors of the data (you know, like writing a case for the empty list and a case for cons that uses recursion), we can define functions that produce coinductively defined data by specifying what happens when the result is broken down (in the case of lists by saying what happens when someone tries to take the first or rest of the list). Jacobs and Rutten put it like this:<br>
<ul><li>In an <i>inductive definition</i> of a function <i>f</i>, one defines the value of <i>f</i> on all constructors.<br>
<li>In a <i>coinductive definition</i> of a function <i>f</i>, one defines the values of all destructors on each outcome <i>f(x)</i>.</li></li></ul><br>
Jacobs and Rutten give a couple examples of coinductively-defined functions. Here's one: say you want to write a function <i>odds</i> that takes an infinite stream and returns another stream that consists of every other element of the original, starting with the first. You can define that as follows:<br>
<ul><li>first(odds(xs)) = first(xs)<br>
<li>rest(odds(xs)) = odds(rest(rest(xs)))</li></li></ul><br>
Similarly, say you want to define <i>merge(xs,ys)</i> that consists of the first element of <i>xs</i>, the first element of <i>ys</i>, the second element of <i>xs</i>, the second element of <i>ys</i>, and so on. That's also easy:<br>
<ul><li>first(merge(xs,ys)) = first(xs)<br>
<li>rest(merge(xs,ys)) = merge(ys,rest(xs))</li></li></ul><br>
These definitions are a little funny; at first glance (at least to programmers in non-lazy languages) it might look like they can't possibly work because they would never end. There are two answers to that objection: the first is that you can think of this as pure mathematical specification, so it doesn't matter if these things can actually be computed out on a machine as long as you can write down a specification. But another answer is that you can represent these lists in a computer just fine — all you have to do is realize that there's no need to actually compute the next element of any given infinite stream until somebody actually asks for it. So read the definitions as, "<i>If</i> someone takes the rest of <i>odds(xs)</i>, <i>then</i> compute and return <i>odds(rest(rest(xs)))</i>." Of course that computation is another stream, which again is allowed to wait for somebody to actually ask for the rest before it actually computes anything, and so on.<br>
<br>
Alright, that's all I'm going to say about functions. The big question is, how do we prove things by coinduction? Well, again it's kind of analogous to proving things by regular induction. In regular induction, we say that some property holds for all elements in some inductively defined set if (a) it is true of all the base cases, and (b) for every inductively-defined case, the property being true of the smaller subpieces implies that the property is true for the larger construction. With coinduction you again get the complement: we can conclude that some property holds of some coinductively-defined structure if it holds for the observable "fringe" (the first element of the list, in our example) and if the property holding for entire structure implies that it holds for the substructures (the property holding for the list implies that it holds for the rest of the list in our example).<br>
<br>
That's abstract, so we'll have to actually do a proof to see how it works. First of all, let's define another function <i>evens(xs)</i> = <i>odds(rest(xs))</i>; it defines the stream consisting of every other element of the input stream <i>xs</i>, starting with the second (i.e., it contains all the elements that <i>odds(xs)</i> would drop and drops the ones <i>odds(xs)</i> would keep). Now it seems clear that for any stream <i>xs</i>, <i>xs = merge(odds(xs),evens(xs))</i>. We can prove this claim by coinduction.<br>
<br>
First (sort of our "base case"): we have to establish that <i>first(xs) = first(merge(odds(xs),evens(xs)))</i>. By definition <i>first(merge(odds(xs),evens(xs))) = first(odds(xs)) = first(xs)</i>, so that case is done.<br>
<br>
Second (our "inductive case"): assuming that <i>xs = merge(odds(xs),evens(xs))</i> (our "coinductive hypothesis") we have to establish that <i>rest(xs) = merge(odds(rest(xs)),evens(rest(xs)))</i>. By coinductive hypothesis we have that <i>rest(xs) = rest(merge(odds(xs),evens(xs)))</i>. By definition of <i>merge</i> this is equal to <i>merge(evens(xs),rest(odds(xs)))</i>. Using the definitions of <i>evens</i> and <i>odds</i> we get that this is equal to <i>merge(odds(rest(xs)),odds(rest(rest(xs))))</i>; by the definition of <i>evens</i> again we have that this is equal to <i>merge(odds(rest(xs)),evens(rest(xs)))</i> as required. That completes the proof.<br>
<br>
So there you have it, a quick intro to coinduction by somebody who really doesn't understand it too well himself. I do recommend you look at Jacobs and Rutten's paper, and also Andy Gordon's <a href="http://citeseer.ist.psu.edu/gordon94tutorial.html">"A Tutorial on Co-induction and Functional Programming"</a>. There is a lot more to say on this subject, and in particular on why all of this stuff works and what its deep connections to regular induction (and regular algebras) are; but from a pragmatic standpoint it seems like this is a subject where you need to understand it on its own terms before you can appreciate the analogy.<br>
<br>
Of course if you look into those you'll probably find that everything I've written here is seriously misguided and overly simplistic. If you do, please let me know what I'm getting wrong!comment:ask.metafilter.com,2006:site.42858-659864Tue, 25 Jul 2006 15:38:37 -0800jacobmBy: knave
http://ask.metafilter.com/42858/What-the-heck-is-coinduction#659993
Thanks to both of you, I'm obviously still fuzzy, but you both helped a great deal. The papers you point to look good, and in fact, I had found one of them in previous web searches. I'll have to spend more time with both of them before I am really comfortable with it, but I do have a better general sense of what coinduction means now.<br>
<br>
Jacob, that answer totally rocks.comment:ask.metafilter.com,2006:site.42858-659993Tue, 25 Jul 2006 17:21:42 -0800knaveBy: ijoshua
http://ask.metafilter.com/42858/What-the-heck-is-coinduction#660178
If you grok Haskell and Object-Oriented Programming, <cite><a href="http://www.mm.informatik.tu-darmstadt.de/~kuehne/fps/">A Functional Pattern System for <br>
Object-Oriented Design</a></cite> (Thomas Kühne,) may help somewhat with understanding some of the applications of coinduction. Kühne cites Jacobs & Rutton as well as Gordon.comment:ask.metafilter.com,2006:site.42858-660178Tue, 25 Jul 2006 19:47:56 -0800ijoshua