File size: 6,850 Bytes
cdec422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import OAI.LinearAlgebra.MatrixMultiplication.FieldHistory.Counts

namespace OAI

/-! Finite extraction histories, inherited masks and recovery bounds. -/

noncomputable section
namespace MatrixMultiplication.AllFieldHistory

open AllFieldParameters
open scoped BigOperators
attribute [local instance] Classical.propDecidable Classical.decEq

def due {K : ℕ} (tick : ℕ) : CanonicalHistory K → Prop
  | .initial h => initialShape h ∈ positiveInitial ∧ tick = h.1.val
  | .afterA h => aShape h ∈ positiveSecond ∧ tick = h.1.val.1.val + 1
  | .afterB _ => False
  | .partC h => tick = h.1.val.1.val.1.val.1.val + 2
  | .afterC _ => False

def produced {K : ℕ} (tick : ℕ) : CanonicalHistory K → Prop
  | .initial _ => False
  | .afterA h => tick = h.1.val.1.val
  | .afterB h => tick = h.1.val.1.val.1.val + 1 ∧ positive (bShape h) ≠ true
  | .partC h => tick = h.1.val.1.val.1.val.1.val + 1
  | .afterC h => tick = h.1.1.val.1.val.1.val.1.val + 2

abbrev Carried (K tick : ℕ) := {h : State K tick // ¬ due tick h.val.1}
abbrev Produced (K tick : ℕ) := {h : History K // produced tick h.1}

theorem produced_not_resident {K tick : ℕ} (h : CanonicalHistory K)
    (hp : produced tick h) : ¬ resident tick h := by
  cases h with
  | initial h => exact False.elim hp
  | afterA h => simp only [produced] at hp; simp only [resident]; omega
  | afterB h => simp only [produced] at hp; simp only [resident]; omega
  | partC h => simp only [produced] at hp; simp only [resident]; omega
  | afterC h => simp only [produced] at hp; simp only [resident]; omega

theorem resident_succ_iff {K tick : ℕ} (h : CanonicalHistory K) :
    resident (tick + 1) h ↔ (resident tick h ∧ ¬ due tick h) ∨ produced tick h := by
  cases h with
  | initial h =>
      by_cases hp : initialShape h ∈ positiveInitial
      · simp only [resident, due, produced, hp, true_implies, true_and, or_false]
        omega
      · simp [resident, due, produced, hp]
  | afterA h =>
      by_cases hp : aShape h ∈ positiveSecond
      · simp only [resident, due, produced, hp, true_implies, true_and]
        omega
      · simp only [resident, due, produced, hp, false_implies, and_true, false_and,
          not_false_eq_true]
        omega
  | afterB h =>
      by_cases hp : positive (bShape h) = true
      · simp [resident, due, produced, hp]
      · simp [resident, due, produced, hp, ne_eq]
        omega
  | partC h => simp only [resident, due, produced]; omega
  | afterC h => simp only [resident, due, produced, not_false_eq_true, and_true]; omega

def stateTransitionForward {K tick : ℕ} (h : State K (tick + 1)) :
    Carried K tick ⊕ Produced K tick := by
  by_cases hr : resident tick h.val.1
  · refine Sum.inl ⟨⟨h.val, hr⟩, ?_⟩
    rcases (resident_succ_iff h.val.1).mp h.property with hc | hp
    · exact hc.2
    · exact False.elim (produced_not_resident _ hp hr)
  · refine Sum.inr ⟨h.val, ?_⟩
    rcases (resident_succ_iff h.val.1).mp h.property with hc | hp
    · exact False.elim (hr hc.1)
    · exact hp

def stateTransitionBackward {K tick : ℕ} : Carried K tick ⊕ Produced K tick →
    State K (tick + 1)
  | .inl h => ⟨h.val.val, (resident_succ_iff _).mpr (.inl ⟨h.val.property, h.property⟩)⟩
  | .inr h => ⟨h.val, (resident_succ_iff _).mpr (.inr h.property)⟩

def stateTransition {K tick : ℕ} : State K (tick + 1) ≃ Carried K tick ⊕ Produced K tick where
  toFun := stateTransitionForward
  invFun := stateTransitionBackward
  left_inv h := by
    unfold stateTransitionForward
    split_ifs <;> rfl
  right_inv h := by
    cases h with
    | inl h => simp [stateTransitionForward, stateTransitionBackward, h.val.property]
    | inr h =>
        have hn := produced_not_resident h.val.1 h.property
        simp [stateTransitionForward, stateTransitionBackward, hn]

def productionParent {K tick : ℕ} (p : Produced K tick) : Active K tick := by
  rcases p with ⟨⟨h, phi⟩, hp⟩
  cases h with
  | initial h => exact False.elim hp
  | afterA h =>
      refine ⟨(.stageA h.1, phi), ?_⟩
      exact hp.symm
  | afterB h =>
      refine ⟨(.stageB h.1, phi), ?_⟩
      exact hp.1.symm
  | partC h =>
      refine ⟨(.stageB h.1.val.1, phi), ?_⟩
      exact hp.symm
  | afterC h =>
      refine ⟨(.stageC h.1, phi), ?_⟩
      exact hp.symm

abbrev DueState (K tick : ℕ) := {h : State K tick // due tick h.val.1}

def activeToDue {K tick : ℕ} (w : Active K tick) : DueState K tick := by
  refine ⟨⟨(w.val.1.source, w.val.2), ?_⟩, ?_⟩
  · exact Eq.mp (congrArg (fun n => resident n w.val.1.source) w.property)
      (work_source_resident w.val.1)
  · rcases w with ⟨⟨w, phi⟩, hw⟩
    cases w with
    | stageA h => exact ⟨h.property, hw.symm⟩
    | stageB h => exact ⟨h.property, hw.symm⟩
    | stageC h => exact hw.symm

def dueToActive {K tick : ℕ} (h : DueState K tick) : Active K tick := by
  rcases h with ⟨⟨⟨h, phi⟩, hr⟩, hd⟩
  cases h with
  | initial h => exact ⟨(.stageA ⟨h, hd.1⟩, phi), hd.2.symm⟩
  | afterA h => exact ⟨(.stageB ⟨h, hd.1⟩, phi), hd.2.symm⟩
  | afterB h => exact False.elim hd
  | partC h => exact ⟨(.stageC h, phi), hd.symm⟩
  | afterC h => exact False.elim hd

def activeSourceEquiv {K tick : ℕ} : Active K tick ≃ DueState K tick where
  toFun := activeToDue
  invFun := dueToActive
  left_inv w := by
    rcases w with ⟨⟨w, phi⟩, hw⟩
    cases w <;> rfl
  right_inv h := by
    rcases h with ⟨⟨⟨h, phi⟩, hr⟩, hd⟩
    cases h with
    | initial h => rfl
    | afterA h => rfl
    | afterB h => exact False.elim hd
    | partC h => rfl
    | afterC h => exact False.elim hd

def bSubdivisionPositionEquiv {K : ℕ} (allocation : Allocation) (dilation : ℕ)
    (h : BPositive K) (phi : Placement) :
    Fin (population allocation dilation (.afterB h.val, phi)) ≃
      (Σ i : Fin 3, Fin (population allocation dilation (.partC (h, i), phi))) :=
  Fintype.equivOfCardEq (by
    simp only [Fintype.card_fin, Fintype.card_sigma]
    exact (part_population_transition allocation dilation h phi).symm)

theorem final_state_terminal {K : ℕ} (h : State K (K + 2)) : terminal h.val.1 := by
  have hr := h.property
  have hlot := (initialAncestor h.val.1).1.isLt
  cases hh : h.val.1 with
  | initial g =>
      simp only [hh, resident] at hr
      simp only [hh, initialAncestor] at hlot
      intro hp
      have := hr hp
      omega
  | afterA a =>
      simp only [hh, resident] at hr
      simp only [hh, initialAncestor] at hlot
      intro hp
      have := hr.2 hp
      omega
  | afterB b =>
      simp only [hh, resident] at hr
      exact hr.2
  | partC c =>
      simp only [hh, resident] at hr
      simp only [hh, initialAncestor] at hlot
      omega
  | afterC c => trivial

end MatrixMultiplication.AllFieldHistory

end

end OAI