forked from coq-community/corn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Picard.v
476 lines (375 loc) · 16.6 KB
/
Picard.v
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
Require Import
Unicode.Utf8 Program
CRArith CRabs
Qauto Qround Qmetric
(*stdlib_omissions.P
stdlib_omissions.Z
stdlib_omissions.Q
stdlib_omissions.N*).
Require Qinf QnonNeg QnnInf CRball.
Import
QnonNeg Qinf.notations QnonNeg.notations QnnInf.notations CRball.notations
Qabs propholds.
Require Import metric FromMetric2 AbstractIntegration SimpleIntegration BanachFixpoint.
Require Import canonical_names decision setoid_tactics util.
Close Scope uc_scope. (* There is a leak in some module *)
Open Scope signature_scope. (* To interpret "==>" *)
Bind Scope mc_scope with Q.
Local Notation ball := mspc_ball.
Lemma Qinf_lt_le (x y : Qinf) : x < y → x ≤ y.
Proof.
destruct x as [x |]; destruct y as [y |]; [| easy..].
change (x < y -> x ≤ y). intros; solve_propholds.
Qed.
Instance Q_nonneg (rx : QnonNeg) : PropHolds (@le Q _ 0 rx).
Proof. apply (proj2_sig rx). Qed.
Instance Q_nonempty : NonEmpty Q := inhabits 0.
Program Instance sig_nonempty `{ExtMetricSpaceClass X}
(r : QnonNeg) (x : X) : NonEmpty (sig (ball r x)) := inhabits x.
Next Obligation. apply mspc_refl; solve_propholds. Qed.
Instance prod_nonempty `{NonEmpty X, NonEmpty Y} : NonEmpty (X * Y).
Proof.
(* In order not to refer to the name of the variable that has type NonEmpty X *)
match goal with H : NonEmpty X |- _ => destruct H as [x] end.
match goal with H : NonEmpty Y |- _ => destruct H as [y] end.
exact (inhabits (x, y)).
Qed.
(* The following instances are needed to show that Lipschitz functions are
uniformly continuous: metric.lip_uc *)
Global Instance Qmsd : MetricSpaceDistance Q := λ x y, abs (x - y).
Global Instance Qmsc : MetricSpaceClass Q.
Proof. intros x1 x2; apply gball_Qabs; reflexivity. Qed.
(*Instance Q_nonempty : NonEmpty Q := inhabits 0%Q.*)
Section Extend.
Context `{ExtMetricSpaceClass Y} (a : Q) (r : QnonNeg).
(* Sould [r] be [Q] or [QnonNeg]? If [r : Q], then [extend] below is not
necessarily continuous. This may be OK because we could add the premise [0
≤ r] to the lemma that says that [extend] is Lipschitz. However, the
definition below is not well-typed because if [r < 0], then [ball r a (a -
r)] is false, so we can't apply [f] to [a - r]. So we assume [r : QnonNeg]. *)
Lemma mspc_ball_edge_l : ball r a (a - `r).
Proof.
destruct r as [e ?]; simpl.
apply gball_Qabs. mc_setoid_replace (a - (a - e)) with e by ring.
change (abs e ≤ e). rewrite abs.abs_nonneg; [reflexivity | trivial].
Qed.
Lemma mspc_ball_edge_r : ball r a (a + `r).
Proof.
destruct r as [e ?]; simpl.
apply Qmetric.gball_Qabs. mc_setoid_replace (a - (a + e)) with (-e) by ring.
change (abs (-e) ≤ e). rewrite abs.abs_negate, abs.abs_nonneg; [reflexivity | trivial].
Qed.
Context (f : sig (ball r a) -> Y).
(* Since the following is a Program Definition, we could write [f (a - r)]
and prove the obligation [mspc_ball r a (a - r)]. However, this obligation
would depend on x and [H1 : x ≤ a - r] even though they are not used in the
proof. So, if [H1 : x1 ≤ a - r] and [H2 : x2 ≤ a - r], then [extend x1] would reduce
to [f ((a - r) ↾ extend_obligation_1 x1 H1)] and [extend x2] would reduce
to [f ((a - r) ↾ extend_obligation_1 x2 H2)]. To apply mspc_refl, we would
need to prove that these applications of f are equal, i.e., f is a morphism
that does not depend on the second component of the pair. So instead we
prove mspc_ball_edge_l and mspc_ball_edge_r, which don't depend on x. *)
Program Definition extend : Q -> Y :=
λ x, if (decide (x ≤ a - r))
then f ((a - r) ↾ mspc_ball_edge_l)
else if (decide (a + r ≤ x))
then f ((a + r) ↾ mspc_ball_edge_r)
else f (x ↾ _).
Next Obligation.
apply Qmetric.gball_Qabs, Q.Qabs_diff_Qle.
apply orders.le_flip in H1; apply orders.le_flip in H2.
split; trivial.
Qed.
(*
Global Instance extend_lip `{!IsLipschitz f L} : IsLipschitz extend L.
Proof with (assumption || (apply orders.le_flip; assumption) || reflexivity).
constructor; [apply (lip_nonneg f L) |].
intros x1 x2 e A.
assert (0 ≤ e) by now apply (radius_nonneg x1 x2).
assert (0 ≤ L) by apply (lip_nonneg f L).
assert (a - to_Q r ≤ a + to_Q r) by
(destruct r; simpl; transitivity a;
[apply rings.nonneg_minus_compat | apply semirings.plus_le_compat_r]; (easy || reflexivity)).
unfold extend.
destruct (decide (x1 ≤ a - to_Q r)); destruct (decide (x2 ≤ a - to_Q r)).
* apply mspc_refl; solve_propholds.
* destruct (decide (a + to_Q r ≤ x2)); apply (lip_prf f L).
+ apply (nested_balls A)...
+ apply (nested_balls A)...
* destruct (decide (a + to_Q r ≤ x1)); apply (lip_prf f L).
+ apply mspc_symm; apply mspc_symm in A. apply (nested_balls A)...
+ apply mspc_symm; apply mspc_symm in A. apply (nested_balls A)...
* destruct (decide (a + to_Q r ≤ x1)); destruct (decide (a + to_Q r ≤ x2));
apply (lip_prf f L).
+ apply mspc_refl; solve_propholds.
+ apply mspc_symm; apply mspc_symm in A. apply (nested_balls A)...
+ apply (nested_balls A)...
+ apply A.
Qed.
*)
Global Instance extend_uc `{!IsUniformlyContinuous f mu_f} : IsUniformlyContinuous extend mu_f.
Proof with (assumption || (apply orders.le_flip; assumption) || reflexivity).
constructor; [apply (uc_pos f mu_f) |].
intros e x1 x2 e_pos A.
assert (a - to_Q r ≤ a + to_Q r) by
(destruct r; simpl; transitivity a;
[apply rings.nonneg_minus_compat | apply semirings.plus_le_compat_r]; (easy || reflexivity)).
unfold extend.
destruct (decide (x1 ≤ a - to_Q r)); destruct (decide (x2 ≤ a - to_Q r)).
* apply mspc_refl; solve_propholds.
* destruct (decide (a + to_Q r ≤ x2)); apply (uc_prf f mu_f); trivial.
+ apply (nested_balls _ _ A)...
+ apply (nested_balls _ _ A)...
* destruct (decide (a + to_Q r ≤ x1)); apply (uc_prf f mu_f); trivial.
+ apply mspc_symm; apply mspc_symm in A. apply (nested_balls _ _ A)...
+ apply mspc_symm; apply mspc_symm in A. apply (nested_balls _ _ A)...
* destruct (decide (a + to_Q r ≤ x1)); destruct (decide (a + to_Q r ≤ x2));
apply (uc_prf f mu_f); trivial.
+ apply mspc_refl'. now apply Qinf_lt_le, (uc_pos f mu_f).
+ apply mspc_symm; apply mspc_symm in A. apply (nested_balls _ _ A)...
+ apply (nested_balls _ _ A)...
Qed.
Lemma extend_inside (x : Q) (A : ball r a x) : extend x = f (x ↾ A).
Proof.
Admitted.
(*apply mspc_ball_Qabs in A.*)
End Extend.
Section Bounded.
Class Bounded {X : Type} (f : X -> CR) (M : Q) := bounded : forall x, abs (f x) ≤ 'M.
Global Instance comp_bounded {X Y : Type} (f : X -> Y) (g : Y -> CR)
`{!Bounded g M} : Bounded (g ∘ f) M.
Proof. intro x; unfold Basics.compose; apply bounded. Qed.
Global Instance extend_bounded {a : Q} {r : QnonNeg} (f : {x | ball r a x} -> CR)
`{!Bounded f M} : Bounded (extend a r f) M.
Proof.
intro x. unfold extend.
destruct (decide (x ≤ a - to_Q r)); [| destruct (decide (a + to_Q r ≤ x))]; apply bounded.
Qed.
Lemma bounded_nonneg {X : Type} (f : X -> CR) `{!Bounded f M} `{NonEmpty X} :
(*PropHolds*) (0 ≤ M).
Proof.
match goal with H : NonEmpty X |- _ => destruct H as [x] end.
apply CRle_Qle. change (@zero CR _ ≤ 'M). transitivity (abs (f x)).
+ apply CRabs_nonneg.
+ apply bounded.
Qed.
End Bounded.
Global Existing Instance luc_prf.
Lemma int_plus (f g : Q -> CR)
`{!IsUniformlyContinuous f f_mu, !IsUniformlyContinuous g g_mu} (a b : Q) :
int (f + g) a b = int f a b + int g a b.
Admitted.
Lemma int_negate (f : Q -> CR) `{!IsUniformlyContinuous f f_mu} (a b : Q) :
int (- f) a b = - int f a b.
Admitted.
Lemma int_minus (f g : Q -> CR)
`{!IsUniformlyContinuous f f_mu, !IsUniformlyContinuous g g_mu} (a b : Q) :
int (f - g) a b = int f a b - int g a b.
Proof. rewrite int_plus, int_negate; reflexivity. Qed.
Global Instance bounded_int_uc {f : Q -> CR} {M : Q}
`{!Bounded f M} `{!IsUniformlyContinuous f mu_f} (x0 : Q) :
IsUniformlyContinuous (λ x, int f x0 x) (lip_modulus M).
Proof.
constructor.
+ intros. apply lip_modulus_pos; [apply (bounded_nonneg f) | easy].
+ intros e x1 x2 e_pos A. apply mspc_ball_CRabs. rewrite int_diff; [| apply _].
transitivity ('(abs (x1 - x2) * M)).
- apply int_abs_bound; [apply _ |]. intros x _; apply bounded.
- apply CRle_Qle. change (abs (x1 - x2) * M ≤ e).
unfold lip_modulus in A. destruct (decide (M = 0)) as [E | E].
rewrite E, rings.mult_0_r. now apply orders.lt_le. (* why does [solve_propholds] not work? *)
apply mspc_ball_Qabs in A. assert (0 ≤ M) by apply (bounded_nonneg f).
apply (orders.order_preserving (.* M)) in A.
now mc_setoid_replace (e / M * M) with e in A by (field; solve_propholds).
Qed.
Section Picard.
Context (x0 : Q) (y0 : CR) (rx ry : QnonNeg).
Notation sx := (sig (ball rx x0)).
Notation sy := (sig (ball ry y0)).
Context (v : sx * sy -> CR) `{!Bounded v M} `{!IsUniformlyContinuous v mu_v} (L : Q).
Hypothesis v_lip : forall x : sx, IsLipschitz (λ y, v (x, y)) L.
Hypothesis L_rx : L * rx < 1.
Hypothesis rx_ry : `rx * M ≤ ry.
Instance L_nonneg : PropHolds (0 ≤ L).
Proof.
assert (B : ball rx x0 x0) by (apply mspc_refl; solve_propholds).
apply (lip_nonneg (λ y, v ((x0 ↾ B), y)) L).
Qed.
(* Needed to apply Banach fixpoint theorem, which requires a finite
distance between any two points *)
Instance uc_msd : MetricSpaceDistance (UniformlyContinuous sx sy) := λ f1 f2, 2 * ry.
Instance uc_msc : MetricSpaceClass (UniformlyContinuous sx sy).
Proof.
intros f1 f2. unfold msd, uc_msd. intro x. apply (mspc_triangle' ry ry y0).
+ change (to_Q ry + to_Q ry = 2 * (to_Q ry)). ring.
+ apply mspc_symm; apply (proj2_sig (func f1 x)).
+ apply (proj2_sig (func f2 x)).
Qed.
(*Check _ : MetricSpaceClass sx.
Check _ : IsUniformlyContinuous v _.
Context (f : sx -> sy) `{!IsUniformlyContinuous f mu_f}.
Check _ : IsUniformlyContinuous ((@Datatypes.id sx) ∘ (@Datatypes.id sx)) _.
Check _ : IsUniformlyContinuous (extend x0 rx (v ∘ (together Datatypes.id f) ∘ diag)) _.
Check _ : IsLocallyUniformlyContinuous (extend x0 rx (v ∘ (together Datatypes.id f) ∘ diag)) _.*)
Definition picard' (f : sx -> sy) `{!IsUniformlyContinuous f mu_f} : Q -> CR :=
λ x, y0 + int (extend x0 rx (v ∘ (together Datatypes.id f) ∘ diag)) x0 x.
(*
Variable f : UniformlyContinuous sx sy.
Check _ : IsUniformlyContinuous f _.
Check _ : IsLocallyLipschitz (extend x0 rx (v ∘ (together Datatypes.id f) ∘ diag)) _.
Check _ : Integral (extend x0 rx (v ∘ (together Datatypes.id f) ∘ diag)).
Check _ : Integrable (extend x0 rx (v ∘ (together Datatypes.id f) ∘ diag)).
Check _ : IsLocallyLipschitz (λ x : Q, int (extend x0 rx (v ∘ (together Datatypes.id f) ∘ diag)) x0 x) _.
Check _ : IsLocallyLipschitz (picard' f) _. Goal True.
assert (0 ≤ to_Q rx). apply (proj2_sig rx).
Check _ : PropHolds (0 ≤ to_Q rx).
Check _ : IsLipschitz (restrict (picard' f) x0 rx) _.
*)
Definition picard'' (f : UniformlyContinuous sx sy) : UniformlyContinuous sx CR.
apply (Build_UniformlyContinuous (restrict (picard' f) x0 rx) _ _).
Defined.
(* Needed below to be able to apply (order_preserving (.* M)) *)
Instance M_nonneg : PropHolds (0 ≤ M).
Proof. apply (bounded_nonneg v). Qed.
Lemma picard_sy (f : UniformlyContinuous sx sy) (x : sx) : ball ry y0 (picard'' f x).
Proof.
destruct x as [x x_sx]. unfold picard''; simpl.
unfold restrict, Basics.compose; simpl.
unfold picard'. apply mspc_ball_CRabs.
rewrite rings.negate_plus_distr, plus_assoc, rings.plus_negate_r, rings.plus_0_l, CRabs_negate.
transitivity ('(abs (x - x0) * M)).
+ apply int_abs_bound; [apply _ |]. (* Should not be required *)
intros t A.
assert (A1 : mspc_ball rx x0 t) by
(apply (mspc_ball_convex x0 x); [apply mspc_refl, (proj2_sig rx) | |]; trivial).
(* [(extend_inside (A:= A1))]: "Wrong argument name: A" *)
rewrite (extend_inside _ _ _ _ A1). apply bounded.
+ apply CRle_Qle. change (abs (x - x0) * M ≤ ry). transitivity (`rx * M).
- now apply (orders.order_preserving (.* M)), mspc_ball_Qabs_flip.
- apply rx_ry.
Qed.
(*Require Import Integration.*)
Definition picard (f : UniformlyContinuous sx sy) : UniformlyContinuous sx sy.
set (g := picard'' f).
set (h x := exist _ (g x) (picard_sy f x)).
assert (C : IsUniformlyContinuous h (uc_mu g)) by admit.
exact (Build_UniformlyContinuous _ _ C).
Defined.
Instance picard_contraction : IsContraction picard (L * rx).
Proof.
constructor; [| exact L_rx].
constructor; [solve_propholds |].
intros f1 f2 e A [x ?].
change (ball (L * rx * e) (picard' f1 x) (picard' f2 x)).
unfold picard'. apply mspc_ball_CRplus_l, mspc_ball_CRabs.
rewrite <- int_minus. transitivity ('(abs (x - x0) * (L * e))).
+ apply int_abs_bound; [apply _ |]. (* remove [apply _] *)
intros x' B. assert (B1 : ball rx x0 x') by
(apply (mspc_ball_convex x0 x); [apply mspc_refl | |]; solve_propholds).
unfold plus, negate, ext_plus, ext_negate.
rewrite !(extend_inside x0 rx _ x' B1).
apply mspc_ball_CRabs. unfold diag, together, Datatypes.id, Basics.compose; simpl.
apply (lip_prf (λ y, v (_, y)) L), A.
+ apply CRle_Qle. mc_setoid_replace (L * rx * e) with ((to_Q rx) * (L * e)) by ring.
assert (0 ≤ e) by apply (radius_nonneg f1 f2 e A).
change ((abs (x - x0) * (L * e)) ≤ ((to_Q rx) * (L * e))).
apply (orders.order_preserving (.* (L * e))).
now apply mspc_ball_Qabs_flip.
Qed.
Program Definition f0 : UniformlyContinuous sx sy :=
Build_UniformlyContinuous (λ x, y0) (λ e, Qinf.infinite) _.
Next Obligation. apply mspc_refl; solve_propholds. Qed.
Next Obligation.
constructor.
+ intros; easy.
+ intros e x1 x2 e_pos B. change (ball e y0 y0). apply mspc_refl; solve_propholds.
Qed.
(*Let emsc := _ : ExtMetricSpaceClass (UniformlyContinuous sx sy).
Check _ : MetricSpaceClass (UniformlyContinuous sx sy).*)
Lemma ode_solution : let f := fp picard f0 in picard f = f.
Proof. apply banach_fixpoint. Qed.
End Picard.
Section Computation.
Definition x0 : Q := 0.
Definition y0 : CR := 1.
Definition rx : QnonNeg := (1 # 2)%Qnn.
Definition ry : QnonNeg := 2.
Notation sx := (sig (ball rx x0)).
Notation sy := (sig (ball ry y0)).
Definition v (z : sx * sy) : CR := proj1_sig (snd z).
Definition M : Q := 2.
Definition mu_v (e : Q) : Qinf := e.
Instance : Bounded v M.
Admitted.
Instance : IsUniformlyContinuous v mu_v.
Admitted.
Program Definition f0 : UniformlyContinuous sx sy :=
Build_UniformlyContinuous (λ x, y0) _ _.
(* [Admit Obligations] causes uncaught exception *)
Next Obligation. (*apply mspc_refl; Qauto_nonneg. Qed.*)
Next Obligation. exact Qinf.infinite. Defined.
Next Obligation. admit. Qed.
Definition picard_iter (n : nat) := nat_iter n (picard x0 y0 rx ry v) f0.
Definition answer (n : positive) (r : CR) : Z :=
let m := (iter_pos n _ (Pmult 10) 1%positive) in
let (a,b) := (approximate r (1#m)%Qpos)*m in
Zdiv a b.
Program Definition half : sx := 1 # 2.
Next Obligation. admit. Qed.
Time Compute answer 2 (proj1_sig (picard_iter 2 half)).
End Computation.
(*
Require Import CRArith CRtrans CRconst Qmetric Utf8.
Require Import ProductMetric CompleteProduct (*CPoly_Newton*).
Require Import (*metric2.*)Classified.
Notation "X × Y" := (ProductMS X Y) (at level 40).
Notation "f >> g" := (Cbind_slow f ∘ g) (at level 50).
Notation "x >>= f" := (Cbind_slow f x) (at level 50).
Notation "( f , g )" := (together f g).
Section ODE.
Open Scope uc_scope.
Variable v: (Q_as_MetricSpace × Q_as_MetricSpace) --> CR.
Variable f: Q_as_MetricSpace --> CR.
Definition vxfx := (v >> Couple ∘ (Cunit, f) ∘ diag _).
End ODE.
Section Picard_op.
Definition k := (1#2).
Variable f: Q_as_MetricSpace --> CR.
Require SimpsonIntegration Qpossec.
(* Picard operator, ∫ f, from 0 to t *)
Definition Picard_raw (t:Q_as_MetricSpace) : CR :=
let f' := uc_compose (scale k) f in
(1 + (SimpsonIntegration.simpson_integral f' 1 0 (QabsQpos t)))%CR.
Lemma Picard_uc: (is_UniformlyContinuousFunction Picard_raw (λ (ε:Qpos), ε)).
admit.
Qed.
(* locally lipschitz *)
Definition Picard := (Cbind QPrelengthSpace (Build_UniformlyContinuousFunction Picard_uc)).
End Picard_op.
Section Banach_iter.
(* Iterate operator L, n times *)
Variable L:CR-->CR.
Fixpoint Picard_seq (n : nat) : Q_as_MetricSpace --> CR :=
match n with
| O => L ∘ Cunit
| S m => (Picard (Picard_seq m) ) ∘ Cunit
end.
End Banach_iter.
Section example.
Definition g : CR --> CR := Cbind QPrelengthSpace (const_uc (1:Q_as_MetricSpace)).
Definition picard (n:nat) := (Picard_seq g n).
Definition eval (n:positive) (r:CR) : Z :=
let m := (iter_pos n _ (Pmult 10) 1%positive) in
let (a,b) := (approximate r (1#m)%Qpos)*m in
Zdiv a b.
Definition h := const_uc (5#7:Q_as_MetricSpace).
Definition h' := uc_compose (scale (11#13)) h.
Require Import Integration.
Require Import SimpsonIntegration.
Time Eval vm_compute in (eval 3 (1 + (Integrate h' 0 (1#2)))%CR).
Time Eval vm_compute in (eval 3 (1 + (simpson_integral h' 1 0 (1#2)))%CR).
Time Eval vm_compute in (eval 3 (Picard_raw (@const_uc Q_as_MetricSpace (1#1)) 1)).
Time Eval vm_compute in (eval 3 (picard 1 1)).
Time Eval vm_compute in (eval 2 (picard 2 1)).
End example.
*)