Built with Alectryon, running Coq+SerAPI v8.13.0+0.13.0. Bubbles () indicate interactive fragments: hover for details, tap to reveal contents. Use Ctrl+↑ Ctrl+↓ to navigate, Ctrl+🖱️ to focus. On Mac, use instead of Ctrl.
(******************************************************************************)
(*                                                                            *)
(*                              Triangular number                             *)
(*                                                                            *)
(******************************************************************************)
(*                                                                            *)
(*     delta n = the n^th triangular number                                   *)
(*     troot n = the triangular root of n                                     *)
(*      tmod n = the triangular modulo of n                                   *)
(*                                                                            *)
(*                                                                            *)
(******************************************************************************)

Notation "[ rel _ _ | _ ]" was already used in scope fun_scope. [notation-overridden,parsing]
Notation "[ rel _ _ : _ | _ ]" was already used in scope fun_scope. [notation-overridden,parsing]
Notation "[ rel _ _ in _ & _ | _ ]" was already used in scope fun_scope. [notation-overridden,parsing]
Notation "[ rel _ _ in _ & _ ]" was already used in scope fun_scope. [notation-overridden,parsing]
Notation "[ rel _ _ in _ | _ ]" was already used in scope fun_scope. [notation-overridden,parsing]
Notation "[ rel _ _ in _ ]" was already used in scope fun_scope. [notation-overridden,parsing]
Notation "_ + _" was already used in scope nat_scope. [notation-overridden,parsing]
Notation "_ - _" was already used in scope nat_scope. [notation-overridden,parsing]
Notation "_ <= _" was already used in scope nat_scope. [notation-overridden,parsing]
Notation "_ < _" was already used in scope nat_scope. [notation-overridden,parsing]
Notation "_ >= _" was already used in scope nat_scope. [notation-overridden,parsing]
Notation "_ > _" was already used in scope nat_scope. [notation-overridden,parsing]
Notation "_ <= _ <= _" was already used in scope nat_scope. [notation-overridden,parsing]
Notation "_ < _ <= _" was already used in scope nat_scope. [notation-overridden,parsing]
Notation "_ <= _ < _" was already used in scope nat_scope. [notation-overridden,parsing]
Notation "_ < _ < _" was already used in scope nat_scope. [notation-overridden,parsing]
Notation "_ * _" was already used in scope nat_scope. [notation-overridden,parsing]
Set Implicit Arguments. Unset Strict Implicit. Unset Printing Implicit Defensive.
m, n:nat

0 < m -> (m + n).-1 = m.-1 + n
2
by case: m. Qed.
4
0 < n -> (m + n).-1 = m + n.-1
9
by case: n => // n; rewrite addnS. Qed.
a, b, c:nat

a - b - (c - b) <= a - c
e
11
bLa:a < b

12
11
aLb:b <= a
12
17
a - b = 0
19
1b
12
111c
bLc:b < c

12
111c
cLb:c <= b
12
2b
12
2b
a - b - 0 <= a - c
by rewrite subn0 leq_sub2l. Qed.
b, a, c:nat

a - c <= a - b + (b - c)
35
37
a <= a - b + (b - c + c)
37
a <= a - b + b
38
H:b < c
3e
38
H:a < b

42
43
4a
b <= a - b + b
43
45
3e
45
b <= c -> a <= a - b + (b - c + c)
45
a <= a - b + (0 + c)
45
a - (0 + c) <= a - b
by apply: leq_sub2l; apply: ltnW. Qed. (******************************************************************************) (* *) (* Triangular number *) (* *) (******************************************************************************) Definition delta n := (n.+1 * n)./2. Notation "'Δ' n " := (delta n) (format "'Δ' n", at level 10).
= [:: (1, 1); (2, 3); (3, 6); (4, 10); (5, 15); (6, 21); (7, 28); (8, 36); (9, 45); (10, 55); (11, 66); (12, 78); (13, 91); (14, 105); (15, 120); (16, 136); (17, 153); (18, 171); (19, 190); (20, 210)] : seq (nat * nat)
n:nat

Δn.+1 = Δn + n.+1
61
63
odd (n.+1 * n) && odd (2 * n.+1) + ((n.+1 * n)./2 + (2 * n.+1)./2) = (n.+1 * n)./2 + n.+1
63
(n.+1 * n)./2 + ((n.+1).*2)./2 = (n.+1 * n)./2 + n.+1
by rewrite -{4}(half_bit_double n.+1 false). Qed.
63
0 < n -> 0 < Δn
70
by case: n => // n _; rewrite deltaS addnS ltnS leq_addr. Qed.
63
Δn = \sum_(i < n.+1) i
75
64
IH:Δn = \sum_(i < n.+1) i

Δn.+1 = \sum_(i < n.+2) i
by rewrite big_ord_recr /= -IH deltaS. Qed.
= [:: (0, 0); (1, 1); (2, 3); (3, 6); (4, 10); (5, 15); (6, 21); (7, 28); (8, 36); (9, 45); (10, 55)] : seq (nat * nat)
4
m <= n -> Δm <= Δn
81
by move=> H; apply/half_leq/leq_mul. Qed.
63
(8 * Δn).+1 = n.*2.+1 ^ 2
86
64
IH:(8 * Δn).+1 = n.*2.+1 ^ 2

(8 * Δn.+1).+1 = (n.+1).*2.+1 ^ 2
8d
n.*2.+1 ^ 2 + 8 * n.+1 = (n.+1).*2.+1 ^ 2
8d
n.*2.+1 ^ 2 + 8 * n.+1 = (n.*2.+1 + 2) ^ 2
8d
n.*2.+1 ^ 2 + 8 * n.+1 = n.*2.+1 ^ 2 + (2 ^ 2 + 2 * (n.*2.+1 * 2))
8d
8 * n.+1 = 2 ^ 2 + 2 * (n.*2.+1 * 2)
8d
8 + 8 * n = 2 ^ 2 + 2 * (n.*2.+1 * 2)
8d
8 + 8 * n = 2 ^ 2 + 2 * 2 + 2 * (n.*2 * 2)
8d
8 * n = 2 * (n.*2 * 2)
by rewrite mulnCA -muln2 -!mulnA mulnC. Qed.
63
n <= Δn
ad
by case: n => // n; rewrite deltaS addnS ltnS leq_addl. Qed. (******************************************************************************) (* *) (* Triangular root *) (* *) (******************************************************************************) Definition troot n := let l := iota 0 n.+2 in (find (fun x => n < delta x) l).-1. Notation "∇ n" := (troot n) (format "∇ n", at level 10).
= [:: (0, 0); (1, 1); (2, 1); (3, 2); (4, 2); (5, 2); (6, 3); (7, 3); (8, 3); (9, 3); (10, 4)] : seq (nat * nat)
63
0 < n -> 0 < ∇n
b3
by case: n. Qed.
m:nat

Δ(∇m) <= m
b8
ba
~~ (m < Δ(find (fun x : nat => m < Δx) (iota 0 m.+2)).-1)
bb
l:=iota 0 m.+2:seq nat
f:=fun H : nat => m < ΔH:nat -> bool

~~ (m < Δ(find f l).-1)
bbc6c764
E:(find f l).-1 = n.+1

~~ (m < Δn.+1)
cc
f (nth 0 l (find f l).-1) = false -> ~~ (m < Δn.+1)
cc
n.+1 < m.+2
cc
0 < find f l
by case: find E. Qed.
ba
m < Δ(∇m).+1
dc
ba
~~ (Δ(find (fun x : nat => m < Δx) (iota 0 m.+2)).-1.+1 < m.+1)
c5
~~ (Δ(find f l).-1.+1 < m.+1)
c5
has f l
bbc6c7
Hfl:has f l
e7
c5
f m.+1
ec
c5
(false + (m.+1).*2)./2 <= (m.+2 * m.+1)./2
ec
ee
e7
ee
m < Δ(nth 0 l (find f l)) -> ~~ (Δ(find f l).-1.+1 < m.+1)
bbc6c7ef
E:(find f l).-1 = 0

m < Δ(nth 0 l (find f l)) -> ~~ (Δ1 < m.+1)
bbc6c7ef64cd
m < Δ(nth 0 l (find f l)) -> ~~ (Δn.+2 < m.+1)
ee
1.-1 = 0 -> m < Δ(nth 0 l 1) -> ~~ (Δ1 < m.+1)
105
107
108
107
m < Δ(0 + find f l) -> ~~ (Δn.+2 < m.+1)
107
find f l < m.+2
107
116
by rewrite -(size_iota 0 m.+2) -has_find. Qed. (* Galois connection *)
4
(n <= ∇m) = (Δn <= m)
11b
4
bc
5
dmLn:∇m < n
false = (Δn <= m)
124
126
124
~~ (Δn <= m)
124
m < Δn
by apply: leq_trans (delta_root_gt _) (delta_le dmLn). Qed.
4
(∇m < n) = (m < Δn)
133
by rewrite ltnNge root_delta_le -ltnNge. Qed.
4
m <= n -> ∇m <= ∇n
138
by move=> mLn; rewrite root_delta_le (leq_trans (delta_root_le _)). Qed.
bb
n:nat_eqType

(∇m == n) = (Δn <= m < Δn.+1)
13d
13f
(∇m == n) = (n <= ∇m < n.+1)
by rewrite ltnS -eqn_leq. Qed.
63
∇(Δn) = n
148
by apply/eqP; rewrite trootE leqnn deltaS -addn1 leq_add2l. Qed.
63
∇n <= n
14d
by rewrite -{2}[n]troot_delta troot_le // geq_deltann. Qed. (******************************************************************************) (* *) (* Triangular modulo *) (* *) (******************************************************************************) Definition tmod n := n - delta (troot n).
63
tmod (Δn) = 0
152
by rewrite /tmod troot_delta subnn. Qed.
63
n = Δ(∇n) + tmod n
157
by rewrite addnC (subnK (delta_root_le _)). Qed.
63
tmod n <= ∇n
15c
by rewrite leq_subLR -ltnS -addnS -deltaS delta_root_gt. Qed.
4
∇m < ∇n -> m < n
161
4
m < Δ(∇m) + (∇m).+1
by rewrite {1}[m]tmodE ltn_add2l ltnS tmod_le. Qed.
4
∇m = ∇n -> (tmod m <= tmod n) = (m <= n)
16a
by move=> tmEtn; rewrite {2}[m]tmodE {2}[n]tmodE tmEtn leq_add2l. Qed.
4
∇m = ∇n -> (tmod m < tmod n) = (m < n)
16f
by move=> tmEtn; rewrite {2}[m]tmodE {2}[n]tmodE tmEtn ltn_add2l. Qed.
ba
(∇m.+1 == ∇m) && (tmod m.+1 == (tmod m).+1) || [&& ∇m.+1 == (∇m).+1, tmod m.+1 == 0 & tmod m == ∇m]
174
ba
∇m <= ∇m.+1 -> (∇m.+1 == ∇m) && (tmod m.+1 == (tmod m).+1) || [&& ∇m.+1 == (∇m).+1, tmod m.+1 == 0 & tmod m == ∇m]
bb
He:∇m = ∇m.+1

176
bb
He:∇m < ∇m.+1
176
183
176
183
[&& ∇m.+1 == (∇m).+1, tmod m.+1 == 0 & tmod m == ∇m] || (∇m.+1 == ∇m) && (tmod m.+1 == (tmod m).+1)
183
∇m.+1 == (∇m).+1
183
∇m.+1 == (∇m).+1 -> [&& ∇m.+1 == (∇m).+1, tmod m.+1 == 0 & tmod m == ∇m] || (∇m.+1 == ∇m) && (tmod m.+1 == (tmod m).+1)
183
Δ(∇m.+1) <= m.+1
183
true && (m.+1 < Δ(∇m).+2)
191
183
199
190
183
true && (Δ(∇m) + (tmod m).+2 <= Δ(∇m) + ((∇m).+1 + (∇m).+2))
190
183
192
bb184
He1:∇m.+1 = (∇m).+1

18b
1a7
[&& true, tmod m.+1 == 0 & tmod m == ∇m] || ((∇m).+1 == ∇m) && (tmod m.+1 == (tmod m).+1)
1a7
m.+1 == m.+1 -> [&& true, tmod m.+1 == 0 & tmod m == ∇m] || ((∇m).+1 == ∇m) && (tmod m.+1 == (tmod m).+1)
1a7
Δ(∇m) + (tmod m).+1 == Δ(∇m) + (∇m).+1 + tmod m.+1 -> [&& true, tmod m.+1 == 0 & tmod m == ∇m] || ((∇m).+1 == ∇m) && (tmod m.+1 == (tmod m).+1)
bb1841a8
He2:tmod m = ∇m + tmod m.+1

1ac
1b8
tmod m <= ∇m -> [&& true, tmod m.+1 == 0 & tmod m == ∇m] || ((∇m).+1 == ∇m) && (tmod m.+1 == (tmod m).+1)
1b8
tmod m < ∇m -> [&& true, tmod m.+1 == 0 & tmod m == ∇m] || ((∇m).+1 == ∇m) && (tmod m.+1 == (tmod m).+1)
bb1841a81b9
He3:tmod m = ∇m
1ac
1c4
1ac
1c4
[&& true, tmod m == tmod m & ∇m == ∇m] || ((∇m).+1 == ∇m) && (tmod m.+1 == (∇m).+1)
by rewrite !eqxx. Qed.
4
(m <= n) = (∇m < ∇n) || (∇m == ∇n) && (tmod m <= tmod n)
1ce
5
dmGdn:∇m < ∇n

(m <= n) = true
4
∇n <= ∇m -> (m <= n) = (∇m == ∇n) && (tmod m <= tmod n)
1d5
m <= n
1d8
1d5
m <= Δ(∇m).+1
1d5
Δ(∇m).+1 <= n
1d9
1d5
1e5
1d8
1d5
Δ(∇m).+1 <= Δ(∇n)
1d5
Δ(∇n) <= n
1d9
1d5
1ef
1d8
4
1da
5
dnEdm:∇n = ∇m

(m <= n) = (∇m == ∇n) && (tmod m <= tmod n)
5
dmLdn:∇n < ∇m
1fb
1f9
(m <= n) = (tmod m <= tmod n)
1fc
1fe
1fb
1fe
(m <= n) = false
1fe
~~ (m <= n)
1fe
n < m
1fe
Δ(∇n).+1 <= m
1fe
Δ(∇n).+1 <= Δ(∇m)
by apply: delta_le. Qed.
4
(m < n) = (∇m < ∇n) || (∇m == ∇n) && (tmod m < tmod n)
21c
1d5
(m < n) = true
4
∇n <= ∇m -> (m < n) = (∇m == ∇n) && (tmod m < tmod n)
1d5
m < n
224
1e8224
1f2224
4
226
1f9
(m < n) = (∇m == ∇n) && (tmod m < tmod n)
1fe235
1f9
(m < n) = (tmod m < tmod n)
236
1fe
235
1fe
(m < n) = false
1fe
~~ (m < n)
210
213 217 by apply: delta_le. Qed. (******************************************************************************) (* *) (* Correspondence between N and N x N *) (* *) (******************************************************************************) (* An explicit definition of N <-> N * N *) Definition tpair n := (troot n - tmod n, tmod n).
= [:: (0, (0, 0)); (1, (1, 0)); (2, (0, 1)); (3, (2, 0)); (4, (1, 1)); (5, (0, 2)); (6, (3, 0)); (7, (2, 1)); (8, (1, 2)); (9, (0, 3)); (10, (4, 0)); (11, (3, 1)); (12, (2, 2)); (13, (1, 3)); (14, (0, 4)); (15, (5, 0)); (16, (4, 1)); (17, (3, 2)); (18, (2, 3)); (19, (1, 4))] : seq (nat * (nat * nat))
Definition pairt p := delta (p.1 + p.2) + p.2.
63
pairt (tpair n) = n
24a
63
Δ(∇n) + tmod n = n
by rewrite /tmod addnC subnK // delta_root_le. Qed.
p:(nat * nat)%type

tpair (pairt p) = p
253
a, b:nat

tpair (pairt (a, b)) = (a, b)
25c
(∇(Δ(a + b) + b) - (Δ(a + b) + b - Δ(∇(Δ(a + b) + b))), Δ(a + b) + b - Δ(∇(Δ(a + b) + b))) = (a, b)
25c
∇(Δ(a + b) + b) = a + b
25c
(a + b - (Δ(a + b) + b - Δ(a + b)), Δ(a + b) + b - Δ(a + b)) = (a, b)
25c
∇(Δ(a + b) + b) == a + b
267
25c
Δ(a + b) + b < Δ(a + b) + (a + b).+1
267
25c
269
by rewrite [delta _ + _]addnC !addnK. Qed.