File size: 5,993 Bytes
f18e7de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import Mathlib.Data.Fintype.EquivFin
import Mathlib.Data.Fintype.BigOperators
import Mathlib.Logic.Equiv.Sum
import Mathlib.Algebra.Order.BigOperators.Ring.Finset
import Mathlib.Algebra.BigOperators.Group.Finset.Piecewise
import Mathlib.Combinatorics.Additive.AP.Three.Defs
import Mathlib.Basic.Real.Basic
import Mathlib.Data.Rat.Lemmas
import Mathlib.Tactic.FieldSimp
import Mathlib.Tactic.Linarith
import Mathlib.Tactic.Ring

namespace OAI

/-! Finite type counts, hierarchy separation and tensor execution bounds. -/

namespace MatrixMultiplication.Foundation
namespace FiniteControl

open scoped BigOperators

theorem every_path_safe {State : Type*} (Safe : ℕ → State → Prop)
    (Allows : ℕ → State → State → Prop) (n : ℕ) (path : ℕ → State)
    (initial : Safe 0 (path 0))
    (closed : ∀ t < n, ∀ s s', Safe t s → Allows t s s' → Safe (t + 1) s')
    (steps : ∀ t < n, Allows t (path t) (path (t + 1))) :
    ∀ t ≤ n, Safe t (path t) := by
  intro t
  induction t with
  | zero => intro _; exact initial
  | succ t ih =>
      intro ht
      have htn : t < n := Nat.lt_of_succ_le ht
      exact closed t htn (path t) (path (t + 1))
        (ih (Nat.le_of_lt htn)) (steps t htn)

theorem exists_population_preserving_equiv {I J Node : Type*}
    [Fintype I] [Fintype J] [DecidableEq Node]
    (f : I → Node) (g : J → Node)
    (counts : ∀ node, Fintype.card {i // f i = node} =
      Fintype.card {j // g j = node}) :
    ∃ e : I ≃ J, ∀ i, g (e i) = f i := by
  classical
  let ef : ∀ node, {i // f i = node} ≃ {j // g j = node} :=
    fun node => Fintype.equivOfCardEq (counts node)
  refine ⟨(Equiv.sigmaFiberEquiv f).symm.trans
    ((Equiv.sigmaCongrRight ef).trans (Equiv.sigmaFiberEquiv g)), ?_⟩
  intro i
  exact (ef (f i) ⟨i, rfl⟩).property

theorem population_product_eq {I J Node M : Type*}
    [Fintype I] [Fintype J] [DecidableEq Node] [CommMonoid M]
    (f : I → Node) (g : J → Node)
    (counts : ∀ node, Fintype.card {i // f i = node} =
      Fintype.card {j // g j = node}) (cost : Node → M) :
    (∏ i, cost (f i)) = ∏ j, cost (g j) := by
  obtain ⟨e, he⟩ := exists_population_preserving_equiv f g counts
  calc
    (∏ i, cost (f i)) = ∏ i, cost (g (e i)) := by
      apply Finset.prod_congr rfl
      intro i _
      rw [he i]
    _ = ∏ j, cost (g j) := e.prod_comp (fun j => cost (g j))

theorem exists_common_bank_multiple {Node : Type*} [Fintype Node]
    (denominator blockSize : Node → ℕ)
    (denominator_pos : ∀ node, 0 < denominator node)
    (blockSize_pos : ∀ node, 0 < blockSize node) :
    ∃ bank : ℕ, 0 < bank ∧
      ∀ node, denominator node * blockSize node ∣ bank := by
  classical
  refine ⟨∏ node, denominator node * blockSize node, ?_, ?_⟩
  · exact Finset.prod_pos fun node _ =>
      Nat.mul_pos (denominator_pos node) (blockSize_pos node)
  · intro node
    exact Finset.dvd_prod_of_mem (fun node => denominator node * blockSize node)
      (Finset.mem_univ node)

theorem exists_exact_rational_tiling {Node : Type*} [Fintype Node]
    (weight : Node → ℚ) (nonnegative : ∀ node, 0 ≤ weight node)
    (blockSize : Node → ℕ) (blockSize_pos : ∀ node, 0 < blockSize node) :
    ∃ bank : ℕ, 0 < bank ∧ ∀ node, ∃ copies : ℕ,
      (bank : ℚ) * weight node = (copies * blockSize node : ℕ) := by
  obtain ⟨bank, bank_pos, divisible⟩ := exists_common_bank_multiple
    (fun node => (weight node).den) blockSize
    (fun node => (weight node).den_pos) blockSize_pos
  refine ⟨bank, bank_pos, ?_⟩
  intro node
  obtain ⟨multiple, hmultiple⟩ := divisible node
  refine ⟨multiple * (weight node).num.toNat, ?_⟩
  have hnum : ((weight node).num.toNat : ℚ) = ((weight node).num : ℚ) := by
    exact_mod_cast Int.toNat_of_nonneg (Rat.num_nonneg.mpr (nonnegative node))
  have hden : ((weight node).den : ℚ) ≠ 0 := by
    exact_mod_cast (weight node).den_ne_zero
  calc
    (bank : ℚ) * weight node =
        (bank : ℚ) * (((weight node).num : ℚ) / ((weight node).den : ℚ)) := by
      rw [(weight node).num_div_den]
    _ = ((multiple * (weight node).num.toNat) * blockSize node : ℕ) := by
      rw [hmultiple]
      push_cast
      rw [hnum]
      field_simp [hden]

theorem target_relation_of_windows (a b c : ℤ) (x y z d : ℝ)
    (hd : 0 < d) (tight : x + y + z = 0)
    (hx : |x - (a : ℝ) * d| ≤ d / 10)
    (hy : |y - (b : ℝ) * d| ≤ d / 10)
    (hz : |z + 2 * (c : ℝ) * d| ≤ d / 10) :
    a + b = c + c := by
  obtain ⟨hx₀, hx₁⟩ := abs_le.mp hx
  obtain ⟨hy₀, hy₁⟩ := abs_le.mp hy
  obtain ⟨hz₀, hz₁⟩ := abs_le.mp hz
  let k : ℤ := a + b - 2 * c
  have hkcast : (k : ℝ) = (a : ℝ) + (b : ℝ) - 2 * (c : ℝ) := by
    simp [k]
  have hlow : -(1 : ℝ) < (k : ℝ) := by
    by_contra hn
    have hn' : (k : ℝ) ≤ -1 := le_of_not_gt hn
    have hm := mul_le_mul_of_nonneg_right hn' (le_of_lt hd)
    rw [hkcast] at hm
    nlinarith
  have hhigh : (k : ℝ) < 1 := by
    by_contra hn
    have hn' : 1 ≤ (k : ℝ) := le_of_not_gt hn
    have hm := mul_le_mul_of_nonneg_right hn' (le_of_lt hd)
    rw [hkcast] at hm
    nlinarith
  have hsmall : |(k : ℝ)| < 1 := abs_lt.mpr ⟨hlow, hhigh⟩
  have hsmallInt : |k| < 1 := by exact_mod_cast hsmall
  have hk : k = 0 := Int.abs_lt_one_iff.mp hsmallInt
  dsimp [k] at hk
  linarith

theorem synchronized_targets_of_windows (targets : Set ℤ)
    (free : ThreeAPFree targets) (a b c : ℤ)
    (ha : a ∈ targets) (hb : b ∈ targets) (hc : c ∈ targets)
    (x y z d : ℝ) (hd : 0 < d) (tight : x + y + z = 0)
    (hx : |x - (a : ℝ) * d| ≤ d / 10)
    (hy : |y - (b : ℝ) * d| ≤ d / 10)
    (hz : |z + 2 * (c : ℝ) * d| ≤ d / 10) :
    a = b ∧ a = c := by
  have hrelation := target_relation_of_windows a b c x y z d hd tight hx hy hz
  have hac : a = c := free ha hc hb hrelation
  exact ⟨by linarith, hac⟩

end FiniteControl
end MatrixMultiplication.Foundation

end OAI