Skip to content

Commit 581cc9c

Browse files
committed
collatz conj category stuff
1 parent ea346f2 commit 581cc9c

File tree

1 file changed

+184
-8
lines changed

1 file changed

+184
-8
lines changed

RecMath/Collatz.lean

Lines changed: 184 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import Mathlib.Tactic
22
import Mathlib.Logic.Function.Basic
33
import Mathlib.Data.Nat.Basic
44
import Mathlib.Data.Nat.ModEq
5-
-- import Mathlib.CategoryTheory.Category.Basic
6-
-- import Mathlib.Combinatorics.Quiver.Basic
5+
import Mathlib.CategoryTheory.Category.Basic
76
import Mathlib.Dynamics.FixedPoints.Basic
87
import Mathlib.Dynamics.PeriodicPts.Defs
98
import Mathlib.Dynamics.Flow
109
import Mathlib.Topology.Defs.Basic
1110

1211
-- import Mathlib.LinearAlgeba.AffineSpace.AffineMap
1312

13+
14+
1415
@[grind =]
1516
def step (n : Nat) : Nat :=
1617
if Even n then
@@ -83,14 +84,13 @@ theorem stepT.even {n} (h : Even n) : stepT n = n / 2 := by
8384
theorem stepT.odd {n} (h : Odd n) : stepT n = (3 * n + 1) / 2 := by
8485
simp! only [stepT, h, Nat.not_even_iff_odd.mpr h, ↓reduceIte]
8586

87+
@[grind =]
8688
theorem stepT.even_step {n} (h : Even n) : stepT n = step n := by
8789
simp! only [stepT, step, h, ↓reduceIte]
8890

91+
@[grind =]
8992
theorem stepT.odd_step {n} (h : Odd n) : stepT n = step (step n) := by
90-
-- grind? [= stepT, = step, = Nat.not_odd_iff_even, = Nat.even_mul, = Nat.even_add ]
91-
have : Even (3*n + 1) := by
92-
grind only [= Nat.not_odd_iff_even, = Nat.even_add, = Nat.even_iff, = Nat.odd_iff]
93-
simp! only [stepT, step, Nat.not_even_iff_odd.mpr h, ↓reduceIte, this]
93+
grind only [= Nat.even_iff, = Nat.odd_iff, stepT, step]
9494

9595

9696
theorem stepT.pattern1 {k m} : (1 <= k) -> (k <= m) -> stepT^[k] (2^m - 1) = 3^k * 2^(m-k) - 1 := by
@@ -109,13 +109,58 @@ theorem stepT.pattern1 {k m} : (1 <= k) -> (k <= m) -> stepT^[k] (2^m - 1) = 3^k
109109

110110
end stepT
111111

112+
113+
114+
abbrev IteratesTo {α : Type*} (F : α -> α) (a b : α) :=
115+
{ i : Nat // F^[i] a = b }
116+
117+
/-- `X ~>[F] Y` is a natural number i such that F^[i] X = Y. -/
118+
notation:25 X " ~>[" F:25 "] " Y:0 => IteratesTo F X Y
119+
120+
@[simp, grind =] def IteratesTo.id {α} {F : α -> α} (x : α) : x ~>[F] x :=
121+
0, Function.iterate_zero_apply F x⟩
122+
123+
@[grind]
124+
theorem IteratesTo.zero_eq {α} {X Y : α} {F} {i : X ~>[F] Y} : i.val = 0 -> X = Y := by
125+
grind only [= Function.iterate_zero_apply]
126+
127+
@[simp, grind =]
128+
def IteratesTo.comp {α} {F : α -> α} {X Y Z : α} (f : X ~>[F] Y) (g : Y ~>[F] Z) : X ~>[F] Z :=
129+
⟨g.val + f.val, by grind only [=_ Function.iterate_add_apply]⟩
130+
131+
@[simp, grind =]
132+
def IteratesTo.iterate_n {α} {x : α} {F} (n : Nat) : x ~>[F] (F^[n] x) := ⟨n, rfl⟩
133+
134+
-- @[grind]
135+
def IteratesToCat {α} (_ : α -> α) := α
136+
137+
instance IteratesTo.category {α} (F : α -> α) : CategoryTheory.Category (IteratesToCat F) where
138+
Hom := IteratesTo F
139+
id := IteratesTo.id
140+
comp := IteratesTo.comp
141+
id_comp := by grind only [= id, = comp]
142+
comp_id := by grind only [= id, = comp]
143+
assoc := by grind only [= comp]
144+
145+
abbrev step.iteratesTo := IteratesTo step
146+
abbrev stepT.iteratesTo := IteratesTo stepT
147+
148+
112149
section GoesTo
113150

114151
variable {n m : Nat}
115152

116-
def GoesTo (n m : Nat) : Prop :=
117-
∃i, step^[i] n = m
153+
def step.iteratesTo.even_path {n} : n * 2 ~>[step] n := ⟨1, by simp! [step]⟩
154+
155+
156+
157+
158+
159+
def GoesTo (n m : Nat) : Prop := ∃i, step^[i] n = m
118160

161+
theorem GoesTo.iff_nonempty_iteratesTo {n m : Nat} :
162+
GoesTo n m <-> Nonempty (step.iteratesTo n m) := by
163+
exact nonempty_subtype.symm
119164

120165
infixr:50 " |=> " => GoesTo
121166
infixr:50 " ⤇ " => GoesTo
@@ -208,3 +253,134 @@ theorem GoesTo.odd_family2 : n ≡ 5 [MOD 6] -> ∀k, (n * 2^(2*k+1) - 1)/3 |=>
208253

209254
end GoesTo
210255

256+
section Cats
257+
258+
def stepT.iteratesTo.to_step {x y : Nat} (i : x ~>[stepT] y) : x ~>[step] y :=
259+
match i with
260+
| ⟨0, hi⟩ => ⟨0, hi⟩
261+
| ⟨n + 1, hi⟩ =>
262+
if heven : Even x then
263+
IteratesTo.comp (IteratesTo.iterate_n 1) (to_step ⟨n, by
264+
change stepT^[n] (step x) = y
265+
grind only [= even_step, = Function.iterate_succ_apply]⟩)
266+
else
267+
have hodd := Nat.not_even_iff_odd.mp heven
268+
IteratesTo.comp (IteratesTo.iterate_n 2) (to_step ⟨n, by
269+
change stepT^[n] (step (step x)) = y
270+
grind only [= odd_step, = Function.iterate_succ_apply]⟩)
271+
272+
@[simp, grind =]
273+
theorem stepT.iteratesTo.to_step_zero {x y : Nat} {h : stepT^[0] x = y} : (to_step ⟨0, h⟩).val = 0 := by
274+
simp! only [to_step]
275+
276+
theorem stepT.iteratesTo.to_step_ge {x y : Nat} {i : x ~>[stepT] y} : i.val ≤ (to_step i).val := by
277+
unfold to_step
278+
rcases i with ⟨ni, hi⟩
279+
induction ni generalizing x with
280+
| zero => rfl
281+
| succ ni hn =>
282+
simp_all
283+
rw [Function.iterate_succ_apply] at hi
284+
split
285+
· rename_i heven
286+
rw [stepT.even_step heven] at hi
287+
specialize hn hi
288+
sorry
289+
· have hodd : Odd x := by grind
290+
rw [stepT.odd_step hodd] at hi
291+
specialize hn hi
292+
sorry
293+
294+
#eval stepT.iteratesTo.to_step (⟨13, by decide⟩ : 9 ~>[stepT] 1)
295+
296+
attribute [grind =] Function.iterate_zero Function.iterate_succ
297+
298+
-- set_option maxHeartbeats 800000
299+
300+
-- @[simp]
301+
-- theorem IteratesTo.coe_cast {α} {F} {X Y : α} {n : Nat} {h₁} :
302+
-- ↑(⟨n, h₁⟩ : X ~>[F] Y) = n := by grind only
303+
304+
-- theorem IteratesTo.mk_coe : ⟨
305+
306+
def stepT.iteratesTo.to_step.da_funky_functor : CategoryTheory.Functor (IteratesToCat stepT) (IteratesToCat step) where
307+
obj := id
308+
map {X Y : Nat} (f : X ~>[stepT] Y) : X ~>[step] Y := stepT.iteratesTo.to_step f
309+
map_comp {X Y Z} (f : X ~>[stepT] Y) (g : Y ~>[stepT] Z) := by
310+
rcases f with ⟨nf, hf⟩
311+
rcases g with ⟨ng, hg⟩
312+
simp [CategoryTheory.CategoryStruct.comp]
313+
induction nf with
314+
| zero => bound
315+
| succ nf hnf =>
316+
induction ng with
317+
| zero => bound
318+
| succ ng hng =>
319+
ring_nf
320+
321+
sorry
322+
323+
-- split
324+
-- · grind only
325+
-- · split
326+
-- · split
327+
-- · split
328+
-- · grind only
329+
-- ·
330+
-- rename_i hi h i_1 hi_1 i_2 n_1 hi_2 heq
331+
-- subst hi hi_2
332+
-- simp at heq
333+
-- subst heq
334+
-- grind [= Function.iterate_zero, IteratesTo.comp, IteratesTo.id, = Nat.even_iff,
335+
-- cases eager Subtype, cases Or]
336+
-- · split
337+
-- · split
338+
-- ·
339+
-- rename_i hi_1 h_1 i_2 hi_2 heq
340+
-- subst hi_2 hi_1
341+
-- grind only [= Function.iterate_zero, IteratesTo.comp, IteratesTo.id, = Nat.even_iff,
342+
-- cases eager Subtype, cases Or]
343+
-- · simp
344+
345+
346+
-- -- hint timeout
347+
348+
-- sorry
349+
-- · split
350+
-- ·
351+
-- sorry
352+
-- -- simp
353+
-- · -- hint gave no good results
354+
355+
-- sorry
356+
-- · split
357+
-- · split
358+
-- · grind only
359+
-- ·
360+
-- rename_i hi h i_1 hi_1 i_2 n_1 hi_2 heq
361+
-- subst hi hi_2
362+
-- simp_all
363+
-- subst heq
364+
-- simp_all only [cast_eq, zero_add]
365+
-- · split
366+
-- · split
367+
-- ·
368+
-- sorry
369+
-- ·
370+
-- sorry
371+
-- · split
372+
-- · rename_i hi_1 h_1 i_2 hi_2 heq
373+
-- subst hi_1 hi_2
374+
-- grind [= Function.iterate_zero, = Function.iterate_succ]
375+
-- ·
376+
-- sorry
377+
map_id x := by
378+
simp! only [id_eq, CategoryTheory.CategoryStruct.id, IteratesTo.id, stepT.iteratesTo.to_step,
379+
cast_eq]
380+
381+
382+
383+
384+
385+
386+
end Cats

0 commit comments

Comments
 (0)