Search code examples
coq

Stuck with a proof


I'm fairly new to Coq, and was doing some Katas on CodeWars for fun and learning.

I'm stuck with one of them and want to hear some ideas from you.

So, I have:

Record iso (A B : Set) : Set :=

bijection {

A_to_B : A -> B;

B_to_A : B -> A;

A_B_A : forall a : A, B_to_A (A_to_B a) = a;

B_A_B : forall b : B, A_to_B (B_to_A b) = b

}.


(* nat_plus_nat : a set having size(nat) more elements than nat. (provided in preloaded) *)
Inductive nat_plus_nat : Set := left (n : nat) | right (n : nat).


Theorem nat_iso_natpnat : iso nat nat_plus_nat.

I have and idea, but I can't implement it, and I don't know if it's feasible. Basically, I want to map every odd nat to one constructor(left, for example) and every even nat to another(right, for example). Will this work? If no, how can it be done?

Right now I'm stuck with the fact, that A_to_B defined as fun n => if odd n then left n else right n and B_to_A defined as fun n => match n with | left n' => n' | right n' => n' end won't give me enough facts to eliminate some cases.


Solution

  • You need to do the math correctly first: find two functions that are inverse of each other.

    You initial intent is correct: odd numbers to one side, even numbers to the other side, but what you store on each side should cover all the natural numbers, so you will probably have to divide by 2 somewhere.

    For Coq usage, You should load the Arith package, by starting with the following line:

    Require Import Arith.
    

    This way, you can benefit from existing functions, like Nat.div2 and Nat.even and all the existing theorems about them. To find the relevant theorems, I suggest commands like:

    Search Nat.even 2.
    Search Nat.div2.
    

    Last hint: proving properties of Nat.div2 by induction is rather difficult for beginners. Try to use the existing theorems as much as possible. If you choose to perform a proof by induction concerning div2, go look in the sources in file theories/Arith/Div2.v : the author of that file designed a specific induction theorem called ìnd_0_1_SS just for that purpose.