File size: 8,589 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import OAI.MathematicalPhysics.AlternatingFlow.MixedPrograms

namespace OAI

section CompilersDevelopment

open scoped BigOperators Topology ContDiff
namespace AlternatingNS.Effective
attribute [local instance] Arithmetic.rationalCoding

variable {A : Type*} [Primcodable A]

lemma space_norm_le_sum (v : Space) : ‖v‖ ≤ ∑ i : Fin 3, |v i| := by
  have he : (∑ i : Fin 3, v i • e i) = v := by
    ext j
    simp [e, Finset.sum_apply, Pi.single_apply]
  calc
    ‖v‖ = ‖∑ i : Fin 3, v i • e i‖ := congrArg norm he.symm
    _ ≤ ∑ i : Fin 3, ‖v i • e i‖ := norm_sum_le _ _
    _ = _ := by simp only [norm_smul, Bounds.norm_e, mul_one, Real.norm_eq_abs]

lemma rationalVector_error (q : RationalVector) (v : Space) (k : ℕ)
    (h : ∀ i, |v i - rationalVector q i| ≤ (tolerance (k+2) : ℝ)) :
    ‖rationalVector q - v‖ ≤ dyadicError k := by
  apply (space_norm_le_sum _).trans
  have hi (i : Fin 3) : |(rationalVector q-v) i| ≤ (tolerance (k+2) : ℝ) := by
    change |rationalVector q i-v i| ≤ _
    rw [abs_sub_comm]
    exact h i
  have hsum : (∑ i : Fin 3, |(rationalVector q-v) i|) ≤ 3 * (tolerance (k+2) : ℝ) := by
    calc
      _ ≤ ∑ _i : Fin 3, (tolerance (k+2) : ℝ) := Finset.sum_le_sum (fun i _ => hi i)
      _ = _ := by simp
  apply hsum.trans
  simp only [tolerance_cast, dyadicError, pow_add]
  nlinarith [pow_pos (by norm_num : (0:ℝ)<1/2) k]

lemma mul_precision (C k : ℕ) : (C : ℝ) * (tolerance (k+C) : ℝ) ≤ tolerance k := by
  rw [tolerance_add, mul_left_comm]
  exact mul_le_of_le_one_right (tolerance_pos _).le (nat_mul_tolerance C)

abbrev EvalInput := ℕ × IndexCode × RationalPoint × ℕ
abbrev ModInput := ℕ × IndexCode × ℕ × ℕ
abbrev BoundInput := ℕ × ℕ × IndexCode

def sample (b : (((A × Fin 3) × List (Fin 4)) × RationalPoint) × ℕ → ℚ)
    (a : A) (q : EvalInput) (i : Fin 3) : ℚ :=
  b ((((a,i),mixedWord q.1 q.2.1),q.2.2.1),q.2.2.2+2)

def values (b : (((A × Fin 3) × List (Fin 4)) × RationalPoint) × ℕ → ℚ)
    (a : A) (q : EvalInput) : RationalVector :=
  (sample b a q 0,sample b a q 1,sample b a q 2)

lemma sample_computable (b : (((A × Fin 3) × List (Fin 4)) × RationalPoint) × ℕ → ℚ)
    (hb : Computable b) (i : Fin 3) : Computable₂ (fun a : A => fun q : EvalInput => sample b a q i) := by
  have hl : Computable (fun z : A × EvalInput => z.2.1) := Computable.fst.comp Computable.snd
  have hα : Computable (fun z : A × EvalInput => z.2.2.1) := Computable.fst.comp (Computable.snd.comp Computable.snd)
  have hq : Computable (fun z : A × EvalInput => z.2.2.2.1) := Computable.fst.comp (Computable.snd.comp (Computable.snd.comp Computable.snd))
  have hk : Computable (fun z : A × EvalInput => z.2.2.2.2) := Computable.snd.comp (Computable.snd.comp (Computable.snd.comp Computable.snd))
  exact hb.comp ((((Computable.fst.pair (Computable.const i)).pair
    (mixedWord_computable.comp hl hα)).pair hq).pair
    (Primrec.nat_add.to_comp.comp hk (Computable.const 2)))

lemma values_computable (b : (((A × Fin 3) × List (Fin 4)) × RationalPoint) × ℕ → ℚ)
    (hb : Computable b) : Computable₂ (values b) :=
  (sample_computable b hb 0).pair ((sample_computable b hb 1).pair (sample_computable b hb 2))

def modulus (B : A × ℕ × ℕ → ℕ) (a : A) (q : ModInput) : ℕ :=
  q.2.2.2 + B (a,0,(mixedWord q.1 q.2.1).length+1)

lemma modulus_computable (B : A × ℕ × ℕ → ℕ) (hB : Computable B) : Computable₂ (modulus B) := by
  have hl : Computable (fun z : A × ModInput => z.2.1) := Computable.fst.comp Computable.snd
  have hα : Computable (fun z : A × ModInput => z.2.2.1) := Computable.fst.comp (Computable.snd.comp Computable.snd)
  have hk : Computable (fun z : A × ModInput => z.2.2.2.2) := Computable.snd.comp (Computable.snd.comp (Computable.snd.comp Computable.snd))
  exact Primrec.nat_add.to_comp.comp hk (hB.comp (Computable.fst.pair
    ((Computable.const 0).pair (Computable.succ.comp (Computable.list_length.comp
      (mixedWord_computable.comp hl hα))))))

def bound (B : A × ℕ × ℕ → ℕ) (a : A) (q : BoundInput) : ℕ :=
  B (a,q.1,(mixedWord q.2.1 q.2.2).length)

lemma bound_computable (B : A × ℕ × ℕ → ℕ) (hB : Computable B) : Computable₂ (bound B) := by
  have hJ : Computable (fun z : A × BoundInput => z.2.1) := Computable.fst.comp Computable.snd
  have hl : Computable (fun z : A × BoundInput => z.2.2.1) := Computable.fst.comp (Computable.snd.comp Computable.snd)
  have hα : Computable (fun z : A × BoundInput => z.2.2.2) := Computable.snd.comp (Computable.snd.comp Computable.snd)
  exact hB.comp (Computable.fst.pair (hJ.pair
    (Computable.list_length.comp (mixedWord_computable.comp hl hα))))

lemma Certified.programs {f : A → Velocity}
    (hf : Certified (fun a => Function.uncurry (f a)))
    (hn : Named (fun z : (A × Fin 3) × RationalPoint =>
      f z.1.1 (rationalPoint z.2).1 (rationalPoint z.2).2 z.1.2)) :
    ∃ compile : A → FieldProgram, Computable compile ∧ ∀ a, ProgramFor (compile a) (f a) := by
  obtain ⟨b,hb,hbError⟩ := hf.jetBound.named_component_words hn
  obtain ⟨sf,B,hB,hBound⟩ := hf
  obtain ⟨ce,hce,he⟩ := compiler_of_computable (values b) (values_computable b hb)
  obtain ⟨cm,hcm,hm⟩ := compiler_of_computable (modulus B) (modulus_computable B hB)
  obtain ⟨cb,hcb,hc⟩ := compiler_of_computable (bound B) (bound_computable B hB)
  refine ⟨fun a => (ce a,cm a,cb a), hce.pair (hcm.pair hcb), fun a => ⟨?_,?_,?_⟩⟩
  · intro l α q k
    refine ⟨values b a (l,α,q,k),?_,fun ht => ?_⟩
    · have hh := he a (l,α,q,k)
      rcases q with ⟨t,x,y,z⟩
      simpa only [Returns, values, Encodable.encode_prod_val, Arithmetic.rationalCoding_encode] using hh
    · rw [mixed_eq_wordJet (f a) (sf a) l α _ (by change 0 ≤ (q.1 : ℝ); exact_mod_cast ht)]
      apply rationalVector_error
      intro i
      have hh := hbError (((a,i),mixedWord l α),q) (k+2)
      have heq : rationalVector (values b a (l,α,q,k)) i = (sample b a (l,α,q,k) i : ℝ) := by
        fin_cases i <;> rfl
      rw [heq]
      exact hh
  · intro l α R k
    refine ⟨modulus B a (l,α,R,k),hm a (l,α,R,k),fun z z' ht ht' _ _ hd => ?_⟩
    rw [mixed_eq_wordJet (f a) (sf a) l α z.1 ht z.2,
      mixed_eq_wordJet (f a) (sf a) l α z'.1 ht' z'.2]
    let C := B (a,0,(mixedWord l α).length+1)
    have hC (y : ℝ × Space) : ‖iteratedFDeriv ℝ 1 (wordJet (Function.uncurry (f a)) (mixedWord l α)) y‖ ≤ C := by
      have hC' : ‖iteratedFDeriv ℝ (1+(mixedWord l α).length) (Function.uncurry (f a)) y‖ ≤ C := by
        rw [Nat.add_comm 1 (mixedWord l α).length]
        simpa only [C, pow_zero, one_mul] using hBound a 0 ((mixedWord l α).length+1) y
      exact (wordJet_bound (sf a) (mixedWord l α) 1 y).trans hC'
    have hLip := jet_lipschitz (wordJet_smooth (sf a) (mixedWord l α)) C hC z z'
    exact hLip.trans ((mul_le_mul_of_nonneg_left hd (Nat.cast_nonneg C)).trans (by
      simpa only [modulus, C, tolerance_cast] using mul_precision C k))
  · intro J l α
    refine ⟨bound B a (J,l,α),hc a (J,l,α),fun t ht x => ?_⟩
    rw [mixed_eq_wordJet (f a) (sf a) l α t ht x]
    have hnorm : ‖wordJet (Function.uncurry (f a)) (mixedWord l α) (t,x)‖ ≤
        ‖iteratedFDeriv ℝ (mixedWord l α).length (Function.uncurry (f a)) (t,x)‖ := by
      have hh := wordJet_bound (sf a) (mixedWord l α) 0 (t,x)
      rw [Nat.zero_add] at hh
      simpa only [norm_iteratedFDeriv_zero] using hh
    apply (mul_le_mul_of_nonneg_left hnorm (by positivity : (0:ℝ) ≤ (1+t+‖x‖)^J)).trans
    simpa only [bound, weight, abs_of_nonneg ht] using hBound a J (mixedWord l α).length (t,x)

end AlternatingNS.Effective

namespace AlternatingNS

lemma ProgramFor.congr_half {c : FieldProgram} {f g : Velocity}
    (hf : ProgramFor c f) (h : Bounds.HalfEq f g) : ProgramFor c g := by
  refine ⟨?_,?_,?_⟩
  · intro l α q k
    obtain ⟨v,hv,he⟩ := hf.1 l α q k
    refine ⟨v,hv,fun ht => ?_⟩
    rw [← h.mixed l (index α) _ (by change 0 ≤ (q.1 : ℝ); exact_mod_cast ht) _]
    exact he ht
  · intro l α R k
    obtain ⟨m,hm,he⟩ := hf.2.1 l α R k
    refine ⟨m,hm,fun z z' ht ht' hz hz' hd => ?_⟩
    rw [← h.mixed l (index α) _ ht _, ← h.mixed l (index α) _ ht' _]
    exact he z z' ht ht' hz hz' hd
  · intro J l α
    obtain ⟨B,hB,he⟩ := hf.2.2 J l α
    refine ⟨B,hB,fun t ht x => ?_⟩
    rw [← h.mixed l (index α) _ ht _]
    exact he t ht x

end AlternatingNS

end CompilersDevelopment

end OAI