File size: 8,519 Bytes
760f90e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import OAI.Computability.RationalHitting.MatrixComputation

namespace OAI

namespace RationalHitting.StackLanguage

lemma range_flatMap_to_finRange {A : Type} (n : ℕ) (f : ℕ → List A) :
    (List.range n).flatMap f=(List.finRange n).flatMap (fun i => f i.val) := by
  rw [←finRange_map_val,List.flatMap_map]

namespace Poly
variable {A B : Type} [Coding A] [Coding B]

theorem tabulateUp {n : A → ℕ} {f : A×Unary → B}
    (hn : Poly (fun a => Unary.mk (n a))) (hf : Poly f) :
    Poly (fun a => (List.range (n a)).map (fun i => f (a,⟨i⟩))) :=
  hf.tabulate.comp (hn.pair (show Poly (fun a : A => a) from id))

theorem flatMapUp {n : A → ℕ} {f : A×Unary → List B}
    (hn : Poly (fun a => Unary.mk (n a))) (hf : Poly f) :
    Poly (fun a => (List.range (n a)).flatMap (fun i => f (a,⟨i⟩))) :=
  (listFlatten.comp (tabulateUp hn hf)).of_eq (by intro a;rw [List.flatMap_def])

theorem wordRange {n : A → ℕ} {f : A×Unary → BitWord}
    (hn : Poly (fun a => Unary.mk (n a))) (hf : Poly f) :
    Poly (fun a => BitWord.mk ((List.range (n a)).flatMap (fun i => (f (a,⟨i⟩)).val))) :=
  (wordConcat.comp (tabulateUp hn hf)).of_eq (by intro a;simp [List.flatMap_map])
end Poly
end RationalHitting.StackLanguage


namespace RationalHitting.StackLanguage
namespace Runtime

abbrev MatrixWordArgs := (DenseSquare×DenseSquare)×ℤ

def matrixWord (A B : DenseSquare) (den : ℤ) : BitWord :=
  ⟨(List.range A.dim).flatMap fun i =>
    (List.range A.dim).flatMap fun j =>
      encodeRat (Rat.divInt (cramer A B i j).val.det den)⟩

@[fun_prop] theorem matrixWord_poly : Poly (fun a : MatrixWordArgs => matrixWord a.1.1 a.1.2 a.2) := by
  have hc : Poly (fun a : (MatrixWordArgs×Unary)×Unary =>
      cramer a.1.1.1.1 a.1.1.1.2 a.1.2.val a.2.val) :=
    cramer_poly.comp (show Poly (fun a : (MatrixWordArgs×Unary)×Unary =>
      (a.1.1.1,(a.1.2,a.2))) by fun_prop)
  have hv := Poly.rationalEncode.comp ((Poly.denseDet.comp hc).pair
    (show Poly (fun a : (MatrixWordArgs×Unary)×Unary => a.1.1.2) by fun_prop))
  have hr := Poly.wordRange (Poly.denseDim.comp (show Poly (fun a : MatrixWordArgs×Unary => a.1.1.1) by fun_prop)) hv
  exact Poly.wordRange (Poly.denseDim.comp (show Poly (fun a : MatrixWordArgs => a.1.1) by fun_prop)) hr

lemma matrixWord_of {d : ℕ} (A B : Matrix (Fin d) (Fin d) ℤ) (den : ℤ) :
    (matrixWord (DenseSquare.of A) (DenseSquare.of B) den).val=
      (List.finRange d).flatMap (fun i => (List.finRange d).flatMap (fun j =>
        encodeRat (Rat.divInt (A.updateCol i (fun r => B r j)).det den))) := by
  unfold matrixWord
  change (List.range d).flatMap _ = _
  rw [range_flatMap_to_finRange]
  apply List.flatMap_congr
  intro i hi
  rw [range_flatMap_to_finRange]
  apply List.flatMap_congr
  intro j hj
  change Fin d at j
  exact cramer_encode_of A B den i j

abbrev TupleWordArgs := (Params×GridPoint)×DenseSquare×ℤ

def tupleWord (p : Params) (x : GridPoint) (A : DenseSquare) (den : ℤ) : BitWord :=
  ⟨(List.range p.n).flatMap fun i => (matrixWord A (matrix p x ⟨i+1⟩) den).val⟩

@[fun_prop] theorem tupleWord_poly : Poly (fun a : TupleWordArgs => tupleWord a.1.1 a.1.2 a.2.1 a.2.2) := by
  have hm : Poly (fun a : TupleWordArgs×Unary => matrix a.1.1.1 a.1.1.2 (a.2+1)) := by fun_prop
  have hv := matrixWord_poly.comp
    (((show Poly (fun a : TupleWordArgs×Unary => a.1.2.1) by fun_prop).pair hm).pair
      (show Poly (fun a : TupleWordArgs×Unary => a.1.2.2) by fun_prop))
  exact Poly.wordRange (show Poly (fun a : TupleWordArgs => a.1.1.1) by fun_prop) hv

lemma tupleWord_eq (n s : ℕ) (x : GridPoint) (den : ℤ) :
    (tupleWord (Params.of n s) x
      (matrix (Params.of n s) x 0) den).val =
      encodeTuple (ExecutableGenerator.tuple (ExecutableGenerator.normalized
        (ExecutableGenerator.inputTables n (Parameters.N n s) (Parameters.exponent n s) (gridFn x)) den)) := by
  unfold tupleWord encodeTuple
  change (List.range n).flatMap _ = _
  rw [range_flatMap_to_finRange]
  apply List.flatMap_congr
  intro a ha
  let U := Generator.U n (Parameters.N n s) (Parameters.exponent n s) (gridFn x)
  have hzero : matrix (Params.of n s) x 0=DenseSquare.of (U 0) := matrix_eq n s x 0
  have hsucc : matrix (Params.of n s) x ⟨a.val+1⟩=DenseSquare.of (U a.succ) := matrix_eq n s x a.succ
  trans (matrixWord (DenseSquare.of (U 0)) (DenseSquare.of (U a.succ)) den).val
  · exact congrArg₂ (fun A B => (matrixWord A B den).val) hzero hsucc
  rw [matrixWord_of]
  apply List.flatMap_congr
  intro i hi
  apply List.flatMap_congr
  intro j hj
  congr 1
  rw [ExecutableGenerator.tuple_normalized,ExecutableGenerator.normalizedEntry,
    DeterminantAlgorithm.determinant_eq,DeterminantAlgorithm.denote_tabulate,
    ExecutableGenerator.inputTables_denote,ExecutableGenerator.inputTables_denote]

end Runtime
end RationalHitting.StackLanguage


namespace RationalHitting.StackLanguage
namespace Runtime

def point (p : Params) (x : GridPoint) : List BitWord :=
  let A := matrix p x 0
  let den := A.val.det
  if den=0 then [] else [tupleWord p x A den]

@[fun_prop] theorem point_poly : Poly (fun a : Params×GridPoint => point a.1 a.2) := by
  have hf : Poly (fun a : TupleWordArgs =>
    if a.2.2=0 then ([] : List BitWord) else [tupleWord a.1.1 a.1.2 a.2.1 a.2.2]) := by
    apply Poly.ite
    · exact Poly.intZero.comp (show Poly (fun a : TupleWordArgs => a.2.2) by fun_prop)
    · exact Poly.const []
    · exact Poly.listSingleton.comp tupleWord_poly
  have hm : Poly (fun a : Params×GridPoint => matrix a.1 a.2 0) := by fun_prop
  exact hf.comp ((show Poly (fun a : Params×GridPoint => a) from Poly.id).pair
    (hm.pair (Poly.denseDet.comp hm)))

lemma matrix_det_eq (n s : ℕ) (x : GridPoint) (a : Fin (n+1)) :
    (matrix (Params.of n s) x ⟨a.val⟩).val.det=
      (Generator.U n (Parameters.N n s) (Parameters.exponent n s) (gridFn x) a).det := by
  apply congrArg Matrix.det
  funext i j
  exact entry_eq n _ _ (gridFn x) a i j

lemma point_eq (n s : ℕ) (x : GridPoint) :
    point (Params.of n s) x=
      ((ExecutableGenerator.atPoint n (Parameters.N n s) (Parameters.exponent n s) (gridFn x)).toList.map
        (fun X => BitWord.mk (encodeTuple X))) := by
  unfold point ExecutableGenerator.atPoint
  simp only [DeterminantAlgorithm.determinant_eq,ExecutableGenerator.inputTables_denote]
  have hd : (matrix (Params.of n s) x 0).val.det=
      (Generator.U n (Parameters.N n s) (Parameters.exponent n s) (gridFn x) 0).det :=
    matrix_det_eq n s x 0
  rw [hd]
  split_ifs with hz
  · rfl
  · simp only [Option.toList_some,List.map_cons,List.map_nil,List.cons.injEq, and_true]
    apply congrArg BitWord.mk
    exact tupleWord_eq n s x _

end Runtime
end RationalHitting.StackLanguage


namespace RationalHitting.StackLanguage

lemma map_filterMap_toList {A B C : Type} (l : List A) (f : A → Option B) (g : B → C) :
    (l.filterMap f).map g=l.flatMap (fun a => (f a).toList.map g) := by
  induction l with
  | nil => rfl
  | cons a l ih => cases h : f a <;> simp [h,ih]

namespace Runtime

def words (p : Params) : List BitWord :=
  (List.range p.H).flatMap fun t => (List.range p.H).flatMap fun q =>
    (List.range p.H).flatMap fun r => point p (⟨t+1⟩,⟨q+1⟩,⟨r+1⟩)

@[fun_prop] theorem words_poly : Poly words := by
  have hp : Poly (fun a : ((Params×Unary)×Unary)×Unary =>
      point a.1.1.1 (a.1.1.2+1,a.1.2+1,a.2+1)) := by fun_prop
  have h1 := Poly.flatMapUp (show Poly (fun a : (Params×Unary)×Unary => a.1.1.2.2.2.2) by fun_prop) hp
  have h2 := Poly.flatMapUp (show Poly (fun a : Params×Unary => a.1.2.2.2.2) by fun_prop) h1
  exact Poly.flatMapUp (show Poly (fun a : Params => a.2.2.2.2) by fun_prop) h2

lemma gridFn_fin {H : ℕ} (t p r : Fin H) :
    gridFn (⟨t.val+1⟩,⟨p.val+1⟩,⟨r.val+1⟩)=fun i => (![t,p,r] i).val+1 := by
  funext i;fin_cases i <;> rfl

lemma words_eq (n s : ℕ) : words (Params.of n s)=
    (ExecutableGenerator.list n s).tuples.map (fun X => BitWord.mk (encodeTuple X)) := by
  unfold words
  rw [show (Params.of n s).H=Parameters.H n s from rfl]
  simp only [ExecutableGenerator.list,ExecutableGenerator.output]
  rw [map_filterMap_toList]
  simp only [Generator.grid,List.flatMap_assoc,List.flatMap_map]
  rw [range_flatMap_to_finRange]
  apply List.flatMap_congr
  intro t ht
  rw [range_flatMap_to_finRange]
  apply List.flatMap_congr
  intro p hp
  rw [range_flatMap_to_finRange]
  apply List.flatMap_congr
  intro r hr
  rw [point_eq,gridFn_fin]

end Runtime
end RationalHitting.StackLanguage

end OAI