>This data type is usually identified with the peano numerals. In fact as we will see, in haskell it isn't quite the natural numbers. We take Z to mean 0 and take (SdataNat = Z | S NatderivingShow

^{n}Z) or (S (S (S ... Z))) n times, to mean n. It's basically a unary number system where the length of the string gives us the number.

It is pretty straightforward to write the basic arithmetic operations on the peano numerals.

> add x Z = x > add x (S y) = S (add x y)

> mult x Z = Z > mult x (S y) = add x (mult x y)

> expt x Z = (S Z) > expt x (S y) = mult x (expt x y)Here we can see an extension of this series fairly easily. The only real question we might have is what to do with the base case. It turns out that (S Z) or 1 is a good choice.

> exptexpt x Z = (S Z) > exptexpt x (S y) = expt x (exptexpt x y)Playing with exptexpt got me thinking about the fact that I wasn't dealing with natural numbers and I was curious what I could prove about them. In order to see that we don't actually have the natural numbers we can define the following:

> inf :: Nat > inf = S (inf)This element is clearly not a natural number since it is not a valid recursive equation in the sense that there is no decreasing quantity. It is however a valid

**corecursive**equation. It is always

**productive**in the sense that it always has it's recursive reference inside of a constructor. We will always be able to "peel" something away from this function in order to reason about it. This will become obvious when we start looking at equational reasoning with infinity.

So this leads us to the question: what does inf+x equal? I'll prove it in pseudo-Coq. Let us make the following conjecture:

∀ x, inf + x = inf

Theorem : ∀ x, inf + x = inf. By coinduction: case x = 0: 1a) inf + 0 = inf 2a) inf = inf * true by the reflexive property of = case x = S x' 1b) inf + S x' = inf 2b) S (inf + x') = inf 3b) S (inf + x') = S inf 4b) inf + x' = inf * true as this is the coinductive hypothesis! Qed.

Now the equality that we are using here is actually not term based equality. It is a coinductive type of equality known as bisimulation. It also must follow the rule that we can never use recursion unless we are inside a constructor for the type of the function we are using. The recursion is the coinductive hypothesis. The constructor prior to the use of the coinductive hypothesis is from the definition of bisimulation. Namely:

∀ x y, x = y → S x = S y.

The full definition in Coq of the bisimulation which we denote above as "=" and below as "CoEq" is:

CoInductive CoEq : CoNat -> CoNat -> Prop := | coeq_base : forall x, CoEq Z Z | coeq_next : forall x y, CoEq x y -> CoEq (S x) (S y).

The constructor coeq_next, which is used between step 3b and 4b above ensures that we satisfy our

**guardedness**, or

**productivity**condition and can safely "call" the coinductive hypothesis. What do I mean "call" and how is the "constructor" being used? We can see exactly by printing out the term in Coq that proves inhabitation of the type for our proposition.

add_x_inf = cofix add_x_inf (x : CoNat) : x + inf ~= inf := eq_ind_r (fun c : CoNat => x + c ~= c) (eq_ind_r (fun c : CoNat => c ~= S inf) (coeq_next (x + inf) inf (add_x_inf x)) (add_x_s x inf)) (decomp_eql inf) : forall x : CoNat, x + inf ~= inf

Here we see in gory detail a corecursive function that allows us to show this type is inhabited along with the corecursive call shown embedded in the constructor coeq_next. Sorry if it isn't very readable, it was constructed automagically from the proof script.

To see how this is all done in detail, check out the Coq proof script for the co-naturals. This might be interesting to those of you who want to see how to use coinductive reasoning on simple examples and how setoids work in Coq. You'll notice that the reasoning is slightly more cumbersome then what I've used here. I think this mostly comes down to the "cofix" tactic being unwieldy in coq, and making automation a hassle. I'm going to think about ways to fix this since it really is unnecessarily inconvenient.

Here are some helper functions which convert from positive integers into our representation so you can play with the code more easily.

> to 0 = Z > to (n+1) = (S (to n))

> from Z = 0 > from (S x) = 1+(from x)