|
| 1 | +Require Import init.imports. |
| 2 | + |
| 3 | +(* |
| 4 | +
|
| 5 | +This file contains the definition of embedding functions, which form a bijective mapping from nat × nat to nat, together with its inverse unembed. |
| 6 | +
|
| 7 | +Together with the two definitions, there also are proofs that the functions are inverses of each other. |
| 8 | +
|
| 9 | +The embedding is done using diagonalization. |
| 10 | +
|
| 11 | +0/0 0/1 0/2 0/3 0/4 0/5 |
| 12 | +1/0 1/1 1/2 1/3 1/4 1/5 |
| 13 | +2/0 2/1 2/2 2/3 2/4 2/5 |
| 14 | +3/0 3/1 3/2 3/3 3/4 3/5 |
| 15 | +4/0 4/1 4/2 4/3 4/4 4/5 |
| 16 | +5/0 5/1 5/2 5/3 5/4 5/5 |
| 17 | +6/0 6/1 6/2 6/3 6/4 6/5 |
| 18 | +*) |
| 19 | + |
| 20 | +Section EmbedNaturals. |
| 21 | + |
| 22 | + (* Bijective maps from pairs of natural numbers to natural numbers.*) |
| 23 | + |
| 24 | + Definition gauss_sum (n : nat) : nat := (nat_rec _ 0 (λ (m s : nat), (S m) + s) (n)). |
| 25 | + |
| 26 | + Definition embed (p : nat×nat) : nat := (pr2 p) + (gauss_sum ((pr2 p) + (pr1 p))). |
| 27 | + |
| 28 | + Definition unembed (n : nat) : nat × nat := |
| 29 | + nat_rec _ (0,, 0) (λ _ a, match (pr1 a) with S x => (x,, S (pr2 a)) | _ => (S (pr2 a),, 0) end) n. |
| 30 | + |
| 31 | + Section Tests. |
| 32 | + Fact embed00 : (embed (0,, 0)) = 0. apply idpath. Defined. |
| 33 | + Fact embed01 : (embed (0,, 1)) = 2. apply idpath. Defined. |
| 34 | + Fact embed33 : (embed (3,, 3)) = 24. apply idpath. Defined. |
| 35 | + |
| 36 | + Fact unembed0 : (unembed 0) = (0,, 0). apply idpath. Defined. |
| 37 | + Fact unembed2 : (unembed 2) = (0,, 1). apply idpath. Defined. |
| 38 | + Fact unembed24 : (unembed 24) = (3,, 3). apply idpath. Defined. |
| 39 | + End Tests. |
| 40 | + |
| 41 | + (*Proofs that embed and unembed are inverses of each other. *) |
| 42 | + |
| 43 | + Lemma gauss_sum_sn (n : nat) : (gauss_sum (S n)) = ((S n) + gauss_sum n). |
| 44 | + Proof. |
| 45 | + apply idpath. |
| 46 | + Qed. |
| 47 | + |
| 48 | + Lemma natplusS (n m : nat) : n + S m = S (n + m). |
| 49 | + Proof. |
| 50 | + induction (pathsinv0 (natpluscomm n (S m))). |
| 51 | + induction (pathsinv0 (natpluscomm n m)). |
| 52 | + apply idpath. |
| 53 | + Qed. |
| 54 | + |
| 55 | + |
| 56 | + Lemma embedsn (m : nat) : (embed (0,, S m)) = ((S (S m)) + embed (0,, m)). |
| 57 | + Proof. |
| 58 | + induction m. |
| 59 | + - apply idpath. |
| 60 | + - unfold embed. simpl. |
| 61 | + induction (pathsinv0 (natplusr0 m)). |
| 62 | + induction (pathsinv0 (natplusS m (S (m + S (m + gauss_sum (m)))))). |
| 63 | + apply idpath. |
| 64 | + Qed. |
| 65 | + |
| 66 | + Lemma embedmsn (n m : nat) : (embed (n,, S m)) = ((S (S (n + m)) + embed (n,, m))). |
| 67 | + Proof. |
| 68 | + unfold embed. |
| 69 | + simpl. |
| 70 | + induction (pathsinv0 (natplusS m (m + n + gauss_sum (m + n)))). |
| 71 | + repeat apply maponpaths. |
| 72 | + induction (pathsinv0 (natpluscomm n m)). |
| 73 | + induction ( (natplusassoc m (m + n) (gauss_sum (m + n)))). |
| 74 | + induction (natplusassoc m m n). |
| 75 | + induction (natplusassoc (m + n) m (gauss_sum (m + n))). |
| 76 | + apply (maponpaths (λ x, add x (gauss_sum (m + n)))). |
| 77 | + induction (pathsinv0 (natplusassoc m m n)), (pathsinv0 (natplusassoc m n m)). |
| 78 | + apply (maponpaths (add m)). |
| 79 | + apply natpluscomm. |
| 80 | + Qed. |
| 81 | + |
| 82 | + Lemma natnmto0 (n m : nat) : n + m = 0 → n = 0. |
| 83 | + Proof. |
| 84 | + intros eq. |
| 85 | + induction n. |
| 86 | + - apply idpath. |
| 87 | + - apply fromempty. |
| 88 | + exact (negpathssx0 _ eq). |
| 89 | + Qed. |
| 90 | + |
| 91 | + Lemma embed0 (n : nat × nat) : (embed n) = 0 → n = (0,, 0). |
| 92 | + Proof. |
| 93 | + induction n as [[|?] [|?]]. |
| 94 | + - intros. apply idpath. |
| 95 | + - unfold embed. simpl. intros. apply fromempty. exact (negpathssx0 _ X). |
| 96 | + - unfold embed. simpl. intros. apply fromempty. exact (negpathssx0 _ X). |
| 97 | + - unfold embed. simpl. intros. apply fromempty. exact (negpathssx0 _ X). |
| 98 | + Defined. |
| 99 | + |
| 100 | + Lemma embedinv (n : nat) : (embed (unembed n)) = n. |
| 101 | + Proof. |
| 102 | + (*TODO*) |
| 103 | + Admitted. |
| 104 | + |
| 105 | + Lemma unembedinv (n : nat × nat) : (unembed (embed n)) = n. |
| 106 | + Proof. |
| 107 | + (* TODO *) |
| 108 | + Admitted. |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +End EmbedNaturals. |
0 commit comments