Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 22c8fae

Browse files
committed
feat(data/nat/pairing): ported data/nat/pairing.lean from Lean2
1 parent 7c67c29 commit 22c8fae

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

data/nat/pairing.lean

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/-
2+
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Leonardo de Moura
5+
6+
Elegant pairing function.
7+
-/
8+
import data.nat.sqrt
9+
open prod decidable
10+
11+
namespace nat
12+
13+
def mkpair (a b : nat) : nat :=
14+
if a < b then b*b + a else a*a + a + b
15+
16+
def unpair (n : nat) : nat × nat :=
17+
let s := sqrt n in
18+
if n - s*s < s then (n - s*s, s) else (s, n - s*s - s)
19+
20+
theorem mkpair_unpair (n : nat) : mkpair (unpair n).1 (unpair n).2 = n :=
21+
let s := sqrt n in
22+
by_cases
23+
(assume : n - s*s < s, by simp [unpair, mkpair, this, nat.add_sub_of_le sqrt_lower])
24+
(assume h₁ : ¬ n - s*s < s,
25+
have s ≤ n - s*s, from le_of_not_gt h₁,
26+
have s + s*s ≤ n - s*s + s*s, from add_le_add_right this (s*s),
27+
have h₂ : s*s + s ≤ n, by rwa [nat.sub_add_cancel sqrt_lower, add_comm] at this,
28+
29+
have n < (s + 1) * (s + 1), from sqrt_upper,
30+
have n < (s * s + s + s) + 1, by simp [mul_add, add_mul] at this; simp [this],
31+
have n - s*s ≤ s + s, from calc
32+
n - s*s ≤ (s*s + s + s) - s*s : nat.sub_le_sub_right (le_of_succ_le_succ this) (s*s)
33+
... = (s*s + (s+s)) - s*s : by rewrite add_assoc
34+
... = s + s : by rewrite nat.add_sub_cancel_left,
35+
have n - s*s - s ≤ s, from calc
36+
n - s*s - s ≤ (s + s) - s : nat.sub_le_sub_right this s
37+
... = s : by rewrite nat.add_sub_cancel_left,
38+
have h₃ : ¬ s < n - s*s - s, from not_lt_of_ge this,
39+
begin
40+
simp [h₁, h₃, unpair, mkpair, -add_comm, -add_assoc],
41+
rewrite [nat.sub_sub, add_sub_of_le h₂]
42+
end)
43+
44+
theorem unpair_mkpair (a b : nat) : unpair (mkpair a b) = (a, b) :=
45+
by_cases
46+
(assume : a < b,
47+
have a + b * b < (b + 1) * (b + 1),
48+
from calc a + b * b < b + b * b : add_lt_add_right this _
49+
... ≤ b + b * b + (b + 1) : le_add_right _ _
50+
... = (b + 1) * (b + 1) : by simp [mul_add, add_mul],
51+
have sqrt_mkpair : sqrt (mkpair a b) = b,
52+
by simp [mkpair, *]; exact sqrt_eq (le_add_left _ _) this,
53+
have mkpair a b - b * b = a, by simp [mkpair, ‹a < b›, -add_comm, nat.add_sub_cancel_left],
54+
by simp [unpair, sqrt_mkpair, this, ‹a < b›])
55+
(assume : ¬ a < b,
56+
have a + (b + a * a) < (a + 1) * (a + 1),
57+
from calc a + (b + a * a) ≤ a + (a + a * a) :
58+
add_le_add_left (add_le_add_right (le_of_not_gt this) _) _
59+
... < (a + 1) * (a + 1) : lt_of_succ_le $ by simp [mul_add, add_mul, succ_eq_add_one],
60+
have sqrt_mkpair : sqrt (mkpair a b) = a,
61+
by simp [mkpair, *]; exact sqrt_eq (le_add_of_nonneg_of_le (zero_le _) (le_add_left _ _)) this,
62+
have mkpair_sub : mkpair a b - a * a = a + b,
63+
by simp [mkpair, ‹¬ a < b›]; rw [←add_assoc, nat.add_sub_cancel],
64+
have ¬ a + b < a, from not_lt_of_ge $ le_add_right _ _,
65+
by simp [unpair, ‹¬ a < b›, sqrt_mkpair, mkpair_sub, this, nat.add_sub_cancel_left])
66+
67+
theorem unpair_lt_aux {n : nat} : n ≥ 1 → (unpair n).1 < n :=
68+
assume : n ≥ 1,
69+
or.elim (nat.eq_or_lt_of_le this)
70+
(assume : 1 = n, by subst n; exact dec_trivial)
71+
(assume : n > 1,
72+
let s := sqrt n in
73+
by_cases
74+
(assume h : n - s*s < s,
75+
have n > 0, from lt_of_succ_lt ‹n > 1›,
76+
have sqrt n > 0, from sqrt_pos_of_pos this,
77+
have sqrt n * sqrt n > 0, from mul_pos this this,
78+
by simp [unpair, h]; exact sub_lt ‹n > 0› ‹sqrt n * sqrt n > 0›)
79+
(assume : ¬ n - s*s < s, by simp [unpair, this]; exact sqrt_lt ‹n > 1›))
80+
81+
theorem unpair_lt : ∀ (n : nat), (unpair n).1 < succ n
82+
| 0 := dec_trivial
83+
| (succ n) :=
84+
have (unpair (succ n)).1 < succ n, from unpair_lt_aux dec_trivial,
85+
lt.step this
86+
87+
end nat

0 commit comments

Comments
 (0)