-
Notifications
You must be signed in to change notification settings - Fork 3
/
geometry.v
407 lines (325 loc) · 12.2 KB
/
geometry.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
Require Export CRsign.
Require Export CRln.
Require Import Morphisms.
Require Export c_util.
Require Export util.
Set Implicit Arguments.
Open Local Scope CR_scope.
Definition QRange: Set := sig (uncurry Qle).
Definition Range: Type := sig (fun r: CR * CR => fst r <= snd r).
Program Coercion unqrange (r: QRange): Range := ('(fst r), '(snd r)).
Next Obligation.
apply <- CRle_Qle.
destruct r.
assumption.
Qed.
Hint Immediate CRle_refl.
Program Definition unit_qrange (c: Q): QRange := (c, c).
Next Obligation. apply Qle_refl. Qed.
Program Definition unit_range (c: CR): Range := (c, c).
Section option_setoid.
Variable s: CSetoid.
Let cmp (a b: option s): Prop :=
match a, b with
| Some x, Some y => x [=] y
| None, None => True
| _, _ => False
end.
Let ap (a b: option s): CProp :=
match a, b with
| Some x, Some y => cs_ap x y
| None, None => False
| _, _ => True
end.
Let is: is_CSetoid (option s) cmp ap.
Proof with auto.
destruct s.
apply Build_is_CSetoid; intro.
destruct x.
simpl.
apply (ax_ap_irreflexive cs_crr _ _ cs_proof).
repeat intro...
destruct x; destruct y; simpl; auto.
apply (ax_ap_symmetric cs_crr _ cs_ap cs_proof).
destruct x; destruct y; destruct z; simpl; auto.
apply (ax_ap_cotransitive cs_crr _ cs_ap cs_proof)...
repeat intro.
split; intro.
destruct x; destruct y; simpl in *; auto.
apply (ax_ap_tight cs_crr _ cs_ap cs_proof s0 s1)...
destruct x; destruct y; simpl in *; try intro; auto.
apply (ax_ap_tight cs_crr _ cs_ap cs_proof s0 s1)...
Qed.
Definition option_setoid: CSetoid := Build_CSetoid (option s) cmp ap is.
End option_setoid.
Definition optCR: CSetoid := option_setoid CRasCSetoid.
Definition OCRle (x y: optCR): Prop :=
match x, y with
| Some l, Some u => l <= u
| _, _ => True
end.
Program Definition overestimate_OCRle eps x y: overestimation (OCRle x y) :=
match x, y with
| Some l, Some u => overestimate_CRle eps l u
| _, _ => true
end.
Definition OQle (r: option Q * option Q): Prop :=
match r with
| (Some l, Some u) => (l <= u)%Q
| _ => True
end.
Definition CRmin_of_upper_bounds (a b: option CR): option CR :=
match a, b with
| None, _ => b
| _, None => a
| Some a, Some b => Some (CRmin a b)
end.
Definition OpenQRange: Set := sig OQle.
Definition OpenRange: Type := sig (uncurry OCRle).
Definition above (c: CR): OpenRange := exist _ (Some c, None) I.
Definition below (c: CR): OpenRange := exist _ (None, Some c) I.
Definition qabove (q: Q): OpenQRange := exist _ (Some q, None) I.
Definition qbelow (q: Q): OpenQRange := exist _ (None, Some q) I.
Program Definition unoqrange (r: OpenQRange): OpenRange
:= (option_map inject_Q_CR (fst r), option_map inject_Q_CR (snd r)).
Next Obligation.
destruct r as [[[x|] [y|]] H]; auto.
unfold OCRle, uncurry.
simpl. apply <- CRle_Qle. assumption.
Qed.
Coercion unoqrange: OpenQRange >-> OpenRange.
(* Should be a [Program Coercion], but that's broken in 8.2
(it has since been fixed). *)
Coercion open_range (r: Range): OpenRange :=
match r with
| exist (x, y) H => exist _ (Some x, Some y) H
end.
Coercion open_qrange (r: QRange): OpenQRange :=
match r with
| exist (x, y) H => exist _ (Some x, Some y) H
end.
Program Definition unbounded_qrange: OpenQRange := (None, None).
Program Definition unbounded_range: OpenRange := (None, None).
Next Obligation. exact I. Qed.
Definition qrange_left (r: QRange): Q := fst (proj1_sig r).
Definition qrange_right (r: QRange): Q := snd (proj1_sig r).
Definition range_left (r: Range): CR := fst (proj1_sig r).
Definition range_right (r: Range): CR := snd (proj1_sig r).
Definition oqrange_left (r: OpenQRange): option Q := fst (proj1_sig r).
Definition oqrange_right (r: OpenQRange): option Q := snd (proj1_sig r).
Definition orange_left (r: OpenRange): option CR := fst (proj1_sig r).
Definition orange_right (r: OpenRange): option CR := snd (proj1_sig r).
Definition in_range (r: Range) (x: CR): Prop :=
range_left r <= x /\ x <= range_right r.
Definition in_orange (r: OpenRange) (x: CR): Prop :=
opt_prop (orange_left r) (flip CRle x) /\
opt_prop (orange_right r) (CRle x).
Instance in_orange_mor: Proper (proj1_sig_relation (product_conj_relation (@st_eq _) (@st_eq _)) ==> @st_eq _ ==> iff) in_orange.
Proof with auto.
intros [[x y] a] [[x' y'] a'] [H H0] x0 y0 e.
unfold in_orange, orange_left, orange_right.
simpl @fst in *. simpl @snd in *.
unfold opt_prop, flip.
destruct x; destruct y; destruct x'; destruct y';
simpl in H, H0; try (elimtype False; assumption);
try rewrite H; try rewrite H0; try rewrite e; split...
Qed.
Instance in_orange_mor' r: Proper (@st_eq _ ==> iff) (in_orange r).
Proof with auto. revert r. intros [[a b] e] x y H. apply in_orange_mor... split... Qed.
Lemma in_unbounded_range x: in_orange unbounded_range x.
Proof with auto. intros. split; simpl; auto. Qed.
Hint Immediate in_unbounded_range.
Definition overestimate_in_range eps r x: overestimation (in_range r x) :=
overestimate_conj
(overestimate_CRle eps (range_left r) x)
(overestimate_CRle eps x (range_right r)).
Definition overestimate_in_orange eps r x: overestimation (in_orange r x) :=
overestimate_conj
(opt_overestimation _ (dep_flip _ (overestimate_CRle eps) x) (orange_left r))
(opt_overestimation _ (overestimate_CRle eps x) (orange_right r)).
Definition Square: Type := (Range * Range)%type.
Definition OpenQSquare: Set := (OpenQRange * OpenQRange)%type.
Definition OpenSquare: Type := (OpenRange * OpenRange)%type.
Coercion open_square (s: Square): OpenSquare
:= (fst s: OpenRange, snd s: OpenRange).
Coercion unoqsquare (s: OpenQSquare): OpenSquare :=
(fst s: OpenRange, snd s: OpenRange).
Definition unbounded_square: OpenSquare
:= (unbounded_range, unbounded_range).
Definition Point: CSetoid := ProdCSetoid CRasCSetoid CRasCSetoid.
Definition in_square (p : Point) (s : Square) : Prop :=
in_range (fst s) (fst p) /\ in_range (snd s) (snd p).
Definition in_osquare (p : Point) (s : OpenSquare) : Prop :=
in_orange (fst s) (fst p) /\ in_orange (snd s) (snd p).
Lemma in_unbounded_square p: in_osquare p unbounded_square.
Proof with auto. intros. split; apply in_unbounded_range. Qed.
Definition overestimate_in_square eps p s: overestimation (in_square p s) :=
overestimate_conj
(overestimate_in_range eps (fst s) (fst p))
(overestimate_in_range eps (snd s) (snd p)).
Definition overestimate_in_osquare eps p s: overestimation (in_osquare p s) :=
overestimate_conj
(overestimate_in_orange eps (fst s) (fst p))
(overestimate_in_orange eps (snd s) (snd p)).
Definition ranges_overlap (a b: Range): Prop :=
range_left a <= range_right b /\ range_left b <= range_right a.
Definition oranges_overlap (a b: OpenRange): Prop :=
match orange_left a, orange_right b with
| Some l, Some r => l <= r
| _, _ => True
end /\
match orange_left b, orange_right a with
| Some l, Some r => l <= r
| _, _ => True
end.
Hint Immediate CRmax_ub_l.
Hint Immediate CRmax_ub_r.
Hint Immediate CRmin_lb_l.
Hint Immediate CRmin_lb_r.
Hint Immediate CRle_refl.
Hint Resolve CRmax_lub.
Lemma overlapping_oranges_share_point (a b: OpenRange):
oranges_overlap a b -> exists p, in_orange a p /\ in_orange b p.
Proof with auto.
revert a b.
intros [[a b] e] [[c d] f] [H H0].
unfold in_orange.
unfold orange_left, orange_right in *.
simpl @fst in *. simpl @snd in *.
unfold opt_prop, flip.
destruct a.
destruct c; [| eauto].
exists (CRmax s s0).
destruct b; destruct d...
destruct c. eauto.
destruct b.
destruct d; [| eauto].
exists (CRmin s s0)...
destruct d. eauto.
exists 0...
Qed.
(* Because of the following Coq bug, we cannot use "overestimator" below:
http://coq.inria.fr/bugs/show_bug.cgi?id=2122 *)
Definition overestimate_ranges_overlap eps a b: overestimation (ranges_overlap a b) :=
overestimate_conj
(overestimate_CRle eps (range_left a) (range_right b))
(overestimate_CRle eps (range_left b) (range_right a)).
Definition overestimate_oranges_overlap eps a b: overestimation (oranges_overlap a b) :=
overestimate_conj
(overestimate_OCRle eps (orange_left a) (orange_right b))
(overestimate_OCRle eps (orange_left b) (orange_right a)).
Definition squares_overlap (a b: Square): Prop :=
ranges_overlap (fst a) (fst b) /\ ranges_overlap (snd a) (snd b).
Definition osquares_overlap (a b: OpenSquare): Prop :=
oranges_overlap (fst a) (fst b) /\
oranges_overlap (snd a) (snd b).
Definition overestimate_osquares_overlap eps a b: overestimation (osquares_overlap a b) :=
overestimate_conj
(overestimate_oranges_overlap eps (fst a) (fst b))
(overestimate_oranges_overlap eps (snd a) (snd b)).
Definition overestimate_squares_overlap eps (a b: Square): overestimation (squares_overlap a b) :=
overestimate_conj
(overestimate_ranges_overlap eps (fst a) (fst b))
(overestimate_ranges_overlap eps (snd a) (snd b)).
Lemma ranges_share_point a b p: in_range a p -> in_range b p ->
ranges_overlap a b.
Proof.
intros [c d] [e f]. split; eapply CRle_trans; eauto.
Qed.
Lemma oranges_share_point a b p: in_orange a p -> in_orange b p ->
oranges_overlap a b.
Proof with auto.
revert a b p.
intros [[a b] c] [[d e] f] p [g h] [i j].
unfold oranges_overlap, orange_left, orange_right in *.
simpl @fst in *. simpl @snd in *.
split.
destruct a...
destruct e...
eapply CRle_trans; eauto.
destruct d...
destruct b...
eapply CRle_trans; eauto.
Qed.
Lemma squares_share_point a b p: in_square p a -> in_square p b ->
squares_overlap a b.
(* todo: this also holds in reverse *)
Proof.
revert a b p.
intros [a b] [c d] [e f] [g h] [i j].
split; eapply ranges_share_point; eauto.
Qed.
Lemma osquares_share_point a b p: in_osquare p a -> in_osquare p b ->
osquares_overlap a b.
Proof.
revert a b p.
intros [a b] [c d] [e f] [g h] [i j].
split; eapply oranges_share_point; eauto.
Qed.
Program Definition map_range (f: CR -> CR) (fi: increasing f) (r: Range): Range :=
(f (fst (proj1_sig r)), f (snd (proj1_sig r))).
Hint Unfold OCRle.
Program Definition map_orange (f: CR -> CR) (fi: increasing f) (r: OpenRange): OpenRange
:= (option_map f (fst (`r)), option_map f (snd (`r))).
Next Obligation. unfold uncurry. destruct r as [[[a|] [b|]] m]; simpl; auto. Qed.
Definition in_map_range p r (f: CR -> CR) (i: increasing f): in_range r p ->
in_range (map_range i r) (f p).
Proof with auto with real.
unfold in_range, range_left, range_right.
destruct r. simpl. intros. destruct H...
Qed.
Definition in_map_orange p r (f: CR -> CR) (i: increasing f): in_orange r p ->
in_orange (map_orange i r) (f p).
Proof.
intros.
destruct r.
unfold in_orange, orange_left, orange_right, opt_prop, flip in *.
destruct x. destruct H.
destruct s; destruct s0; simpl; auto.
Qed.
Section scaling.
Variables (s: CR) (H: 0 <= s).
Hint Resolve CRle_mult.
Program Definition scale_orange (r: OpenRange): OpenRange :=
(option_map (CRmult s) (fst (`r)), option_map (CRmult s) (snd (`r))) .
Next Obligation. unfold uncurry. destruct r as [[[a|] [b|]] h]; simpl; auto. Qed.
Lemma in_scale_orange p r : in_orange r p ->
in_orange (scale_orange r) (s * p).
Proof with simpl; auto.
intros.
destruct r.
unfold in_orange, orange_left, orange_right in *.
unfold scale_orange.
destruct x.
simpl in *.
unfold opt_prop, flip.
destruct H0.
destruct s0; destruct s1...
Qed.
End scaling.
Section mapping.
Variables (fx: CR -> CR) (fy: CR -> CR)
(fxi: increasing fx) (fyi: increasing fy).
Definition map_square (s: Square): Square :=
(map_range fxi (fst s), map_range fyi (snd s)).
Definition map_osquare (s: OpenSquare): OpenSquare :=
(map_orange fxi (fst s), map_orange fyi (snd s)).
Definition in_map_square p s: in_square p s ->
in_square (fx (fst p), fy (snd p)) (map_square s).
Proof with auto.
unfold in_square.
intros. destruct p. destruct s.
destruct H.
simpl in *.
split; apply in_map_range...
Qed.
Definition in_map_osquare p s: in_osquare p s ->
in_osquare (fx (fst p), fy (snd p)) (map_osquare s).
Proof with auto.
unfold in_osquare.
intros. destruct p. destruct s.
destruct H.
simpl in *.
split; apply in_map_orange...
Qed.
End mapping.