I was playing around with the Peano numerals in Haskell (as I often do), because I remember briefly reading about a generalisation of exponentiation in a paper once when I didn’t have time to read it. I decided I’d quickly look at it again now that I was in a mood to procrastinate.
> data Nat = Z | S Nat deriving Show
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 (Sn 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)
A while ago I wrote a post about how one can use coq to make a proper monad module. I was just thinking today that it would be nice to have a tool for haskell that would allow one to write down conjectures and discharge them automatically with a theorem prover. Supercompilation makes a nice clean theorem prover for haskell since one could express the equations of interest in haskell itself. Below is an example of the list monad, and then the 3 monad laws written as conj1,conj2 and conj3. I prove the first law by manual supercompilation. The next two are left as an exercise for the interested reader.
equal xs ys = case xs of
[] -> case ys of
[] -> True
_ -> False
(x:xs') -> case ys of
[] -> False
(y:ys') -> case (eq x y) of
False -> False
True -> equal xs' ys'
bind [] f = []
bind (h:t) f = (f h)++(bind t f)
ret a = [a]
conj1 a f = equal (bind (ret a) f) (f a)
conj2 m = equal (bind m (\a -> ret a)) m
conj3 m f g = equal (bind (bind m f) g) (bind m (\x -> (bind (f x) g)))
In order to define equality on lists we had to make reference to a different equality predicate on the elements of the list. We will make the assumption that this equality is decidable and supercompilation can prove the reflexive property for this equality predicate, that is “eq x x = True” will be taken as an assumption. It is somewhat hard to imagine a case where supercompilation would have a hard time with this because of the case substitution rule.
Now we take conj1 and attempt to prove it by semantics preserving transformations of the source code [1]. I’ve been brutally explicit in the steps used so that people who are interested can see exactly how to do these sorts of things themselves. I’ve found that program transformation techniques can be extremely useful in reasoning about code. Of course, it helps to assume that all functions are total, and I’ll be doing just that. I use the notation M[x:=y] to mean the substitution of y for x in M. Aside from that everything is just Haskell.
conj1 a f = equal (bind (ret a) f) (f a)
{- unfold equal -}
conj1 a f = case (bind (ret a) f) of
[] -> case (f a) of
[] -> True
_ -> False
(x:xs') -> case (f a) of
[] -> False
(y:ys') -> case eq x y of
False -> False
True -> equal xs' ys'
{- unfold bind -}
conj1 a f = case (case (ret a) of
[] -> []
(z:zs) -> (f h)++(bind zs f))
[] -> case (f a) of
[] -> True
_ -> False
(x:xs') -> case (f a) of
[] -> False
(y:ys') -> case eq x y of
False -> False
True -> equal xs' ys'
{- case distribution -}
conj1 a f = case (ret a) of
[] -> case (f a) of
[] -> True
_ -> False
(z:zs) -> case ((f h)++(bind zs f)) of
[] -> case (f a) of
[] -> True
_ -> False
(x:xs') -> case (f a) of
[] -> False
(y:ys') -> case eq x y of
False -> False
True -> equal xs' ys'
{- unfold ret -}
conj1 a f = case [a] of
[] -> case (f a) of
[] -> True
_ -> False
(z:zs) -> case ((f h)++(bind zs f)) of
[] -> case (f a) of
[] -> True
_ -> False
(x:xs') -> case (f a) of
[] -> False
(y:ys') -> case eq x y of
False -> False
True -> equal xs' ys'
{- case selection -}
conj1 a f = (case ((f z)++(bind zs f)) of
[] -> case (f a) of
[] -> True
_ -> False
(x:xs') -> case (f a) of
[] -> False
(y:ys') -> case eq x y of
False -> False
True -> equal xs' ys')
[z := a, zs := []]
{- substitution -}
conj1 a f = case ((f a)++(bind [] f)) of
[] -> case (f a) of
[] -> True
_ -> False
(x:xs') -> case (f a) of
[] -> False
(y:ys') -> case eq x y of
False -> False
True -> equal xs' ys'
{- unfold bind -}
conj1 a f = case (case (f a) of
[] -> []
(z:zs') -> z:(zs'++[])) of
[] -> case (f a) of
[] -> True
_ -> False
(x:xs') -> case (f a) of
[] -> False
(y:ys') -> case eq x y of
False -> False
True -> equal xs' ys'
{- case selection, substitution -}
conj1 a f = case (f a) of
[] -> True
(z:zs') -> case eq z z of
False -> False
True -> equal (zs'++[]) zs'
{- Assumption: eq z z = True -}
conj1 a f = case (f a) of
[] -> True
(z:zs') -> equal (zs'++[]) zs'
{- unfold of equal and ++ -}
conj1 a f = case (f a) of
[] -> True
(z:zs') -> case (case zs' of
[] -> []
(w:ws) -> w:(ws++[]) of
[] -> case zs' of
[] -> True
(y:ys) -> False
(x:xs) -> case zs' of
[] -> False
(y:ys) -> case (eq x y) of
False -> False
True -> equal xs ys
{- case selection -}
conj1 a f = case (f a) of
[] -> True
(z:zs') -> case zs' of
[] -> True
(w:ws) -> case (eq w w) of
False -> False
True -> equal (ws++[]) ws
{- Assumption: eq w w = True -}
conj1 a f = case (f a) of
[] -> True
(z:zs') -> case zs' of
[] -> True
(w:ws) -> equal (ws++[]) ws
{- fold, (we encountered a repeated instance of 'equal (ws++[]) ws') -}
conj1 a f = case (f a) of
[] -> True
(z:zs') -> let g = \ xs ->
case xs of
[] -> True
(y:ys') -> g ys'
in g zs'
Now we have a function that can only return true, regardless of the value of (f a) and assuming that f is total, we can replace this term with True. The proof of this is simply by induction on (f a), but the principle can be built into a checker and is the principle used in the Poitín [3] theorem prover.
Something along these lines would be very light-weight in comparison to using a full theorem prover and could just issue a warning if some laws couldn’t be proved. The proof of this in Coq is actually more work and requires lemmas about the list type to be proven. I did in fact fudge the ((f a)++[]) situation there by replacing with (f a), and I’m not entirely sure what supercompilation does with it if you leave it in. I’ll have to try it later. UPDATE: This is fixed in the code above.
If we want to prove this result by hand we can also derive it in a much simpler way by not following the supercompilation method verbatim:
conj1 a f = equal (bind [a] f) (f a)
conj1 a f = equal (append (f a) (bind [] f)) (f a)
conj1 a f = equal (append (f a) []) (f a)
conj1 a f = equal (f a) (f a)
The advantage of the former is of course that it is entirely mechanical.
UPDATE: I went ahead and incorporated the definition of append and supercompiled it (by hand) with this definition and it reduces to exactly the same term, so in fact supercompilation, without any special knowledge, is capable of proving the conjecture. This is a real win over most proof assistants which would require this append lemma to be proved separately, or incorporated into the knowledge base. For a more visual and more compact depiction of the derivation we can draw a partial process tree with the append function definition included in the derivation.

And indeed the second monad law:

Is the third monad law for the list monad provable using supercompilation? I expect it is, but since I don’t have a supercompiler, and it is a bit of a bear to do by hand, I’m not sure.
Incidently, there is already a supercompiler being developed for haskell called Supero [2]. It probably wouldn’t be too much work to extend this with theorem proving capabilities using ideas from [3].
[1] A Transformation System for Developing Recursive Programs
[2] A Supercompiler for Core Haskell
[3] Poitın: Distilling Theorems From Conjectures