File: StepQsec.v

package info (click to toggle)
coq-corn 8.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,216 kB
  • sloc: python: 112; haskell: 69; makefile: 39; sh: 4
file content (372 lines) | stat: -rw-r--r-- 9,985 bytes parent folder | download | duplicates (3)
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
Require Import CoRN.algebra.RSetoid.
Require Import CoRN.model.metric2.Qmetric.
From Coq Require Export QArith.
Require Export CoRN.metric2.StepFunctionSetoid.
From Coq Require Import Qabs.
From Coq Require Import Bool.
Require Import CoRN.tactics.CornTac.
Require Import CoRN.logic.CornBasics.
Require Import CoRN.algebra.RSetoid.

Set Implicit Arguments.

Local Open Scope setoid_scope.
Local Open Scope sfstscope.

Section QS.

Definition QS : RSetoid := Build_RSetoid Q_Setoid.

Definition QabsS : QS-->QS.
Proof.
 exists Qabs.
 abstract( simpl; intros x1 x2 Hx; rewrite -> Hx; reflexivity).
Defined.

Definition Qplus0 : QS -> QS --> QS.
Proof.
 intros q.
 exists (Qplus q).
 abstract ( simpl; intros x1 x2 Hx; rewrite -> Hx; reflexivity).
Defined.

Definition QplusS : QS --> QS --> QS.
Proof.
 exists (Qplus0).
 abstract ( intros x1 x2 Hx y; simpl in *; rewrite -> Hx; reflexivity).
Defined.

Definition QoppS : QS --> QS.
Proof.
 exists (Qopp).
 abstract ( simpl; intros x1 x2 Hx; simpl in *; rewrite -> Hx; reflexivity).
Defined.

Definition Qminus0 : QS -> QS --> QS.
Proof.
 intros q.
 exists (Qminus q).
 abstract ( simpl; intros x1 x2 Hx; rewrite -> Hx; reflexivity).
Defined.

Definition QminusS : QS --> QS --> QS.
Proof.
 exists (Qminus0).
 abstract ( intros x1 x2 Hx y; simpl in *; rewrite -> Hx; reflexivity).
Defined.

Definition QscaleS : QS -> QS --> QS.
Proof.
 intros q.
 exists (Qmult q).
 abstract ( intros x1 x2 Hx; simpl in *; rewrite -> Hx; reflexivity).
Defined.

Definition QmultS : QS --> QS --> QS.
Proof.
 exists (QscaleS).
 abstract ( intros x1 x2 Hx y; simpl in *; rewrite -> Hx; reflexivity).
Defined.

Definition Qle0 : QS -> QS --> iffSetoid.
Proof.
 intros q.
 exists (Qle q).
 abstract ( simpl; intros x1 x2 Hx; rewrite -> Hx; reflexivity).
Defined.

Definition QleS : QS --> QS --> iffSetoid.
Proof.
 exists (Qle0).
 abstract ( intros x1 x2 Hx y; simpl in *; rewrite -> Hx; reflexivity).
Defined.

End QS.

Notation "'StepQ'" := (StepF QS) : StepQ_scope.
#[global]
Instance StepQ_default : @DefaultRelation (StepF QS) (@StepF_eq QS) | 2 := {}.

Delimit Scope StepQ_scope with SQ.
Bind Scope StepQ_scope with StepF.

Local Open Scope StepQ_scope.

Definition StepQplus (s t:StepQ) : StepQ := QplusS ^@> s <@> t.
Definition StepQopp (s:StepQ) : StepQ := QoppS ^@> s.
Definition StepQminus (s t:StepQ) : StepQ := QminusS ^@> s <@> t.
Definition StepQmult (s t:StepQ) : StepQ := QmultS ^@> s <@> t.
Notation "x + y" := (StepQplus x y) : StepQ_scope.
Notation "- x" := (StepQopp x) : StepQ_scope.
Notation "x - y" := (StepQminus x y) : StepQ_scope.
Notation "x * y" := (StepQmult x y) : StepQ_scope.


Add Morphism StepQplus with signature (@StepF_eq QS) ==> (@StepF_eq QS) ==> (@StepF_eq QS) as StepQplus_wd.
Proof.
 intros.
 unfold StepQplus.
 rewrite -> H.
 rewrite -> H0.
 reflexivity.
Qed.

Add Morphism StepQopp with signature (@StepF_eq QS) ==> (@StepF_eq QS) as StepQopp_wd.
Proof.
 intros.
 unfold StepQopp.
 rewrite -> H.
 reflexivity.
Qed.

Add Morphism StepQminus with signature (@StepF_eq QS) ==> (@StepF_eq QS) ==> (@StepF_eq QS) as StepQminus_wd.
Proof.
 intros.
 unfold StepQminus.
 rewrite -> H.
 rewrite -> H0.
 reflexivity.
Qed.

Add Morphism StepQmult with signature (@StepF_eq QS) ==> (@StepF_eq QS) ==> (@StepF_eq QS) as StepQmult_wd.
Proof.
 intros.
 unfold StepQmult.
 rewrite -> H.
 rewrite -> H0.
 reflexivity.
Qed.

Definition StepQsrt : (@ring_theory (StepQ) (constStepF (0:QS)) (constStepF (1:QS)) StepQplus StepQmult StepQminus StepQopp (@StepF_eq QS)).
Proof.
 constructor; intros; unfold StepF_eq, StepQplus, StepQminus, StepQopp, StepQmult; rewriteStepF;
   set (g:=@st_eqS QS).
         set (z:=QplusS 0).
         set (f:=(join (compose g z))).
         cut (StepFfoldProp (f ^@> x)).
          unfold f; evalStepF; tauto.
         apply StepFfoldPropForall_Map.
         intros a.
         unfold f; simpl; ring.
        set (f:=ap (compose (@ap _ _ _) (compose (compose g) QplusS)) (flip (QplusS))).
        cut (StepFfoldProp (f ^@> x <@> y)).
         unfold f; evalStepF; tauto.
        apply StepFfoldPropForall_Map2.
        intros a b.
        change (a + b == b + a)%Q.
        ring.
       set (f:=ap
         (compose (@ap _ _ _) (compose (compose (compose (compose (@ap _ _ _)) (@compose _ _ _) g)) (compose (flip (@compose _ _ _) QplusS) (compose (@compose _ _ _) QplusS))))
           (compose (compose QplusS) QplusS)).
       cut (StepFfoldProp (f ^@> x <@> y <@> z)).
        unfold f; evalStepF; tauto.
       apply StepFfoldPropForall_Map3.
       intros a b c.
       change (a + (b + c) == a + b + c)%Q.
       ring.
      set (z:=(QmultS 1)).
      set (f:=(join (compose g z))).
      cut (StepFfoldProp (f ^@> x)).
       unfold f; evalStepF; tauto.
      apply StepFfoldPropForall_Map.
      intros a.
      unfold f; simpl; ring.
     set (f:=ap (compose (@ap _ _ _) (compose (compose g) QmultS)) (flip (QmultS))).
     cut (StepFfoldProp (f ^@> x <@> y)).
      unfold f; evalStepF; tauto.
     apply StepFfoldPropForall_Map2.
     intros a b.
     change (a * b == b * a)%Q.
     ring.
    set (f:=ap
      (compose (@ap _ _ _) (compose (compose (compose (compose (@ap _ _ _)) (@compose _ _ _) g)) (compose (flip (@compose _ _ _) QmultS) (compose (@compose _ _ _) QmultS))))
        (compose (compose QmultS) QmultS)).
    cut (StepFfoldProp (f ^@> x <@> y <@> z)).
     unfold f; evalStepF; tauto.
    apply StepFfoldPropForall_Map3.
    intros a b c.
    change (a * (b * c) == a * b * c)%Q.
    ring.
   set (f:= ap
     (compose (@ap _ _ _) (compose (compose (compose (@ap _ _ _) (compose (compose g) QmultS))) QplusS))
       (compose (flip (@compose _ _ _) QmultS) (compose (@ap _ _ _) (compose (compose QplusS) QmultS)))).
   cut (StepFfoldProp (f ^@> x <@> y <@> z)).
    unfold f; evalStepF; tauto.
   apply StepFfoldPropForall_Map3.
   intros a b c.
   change ((a + b) * c == a*c + b*c)%Q.
   ring.
  set (f:= ap (compose (@ap _ _ _) (compose (compose g) QminusS))
    (compose (flip (@compose _ _ _) QoppS) QplusS)).
  cut (StepFfoldProp (f ^@> x <@> y)).
   unfold f; evalStepF; tauto.
  apply StepFfoldPropForall_Map2.
  intros a b.
  change (a - b == a + - b)%Q.
  ring.
 set (z:=(0:QS)).
 set (f:= compose (flip g z) (ap QplusS QoppS)).
 cut (StepFfoldProp (f ^@> x)).
  unfold f; evalStepF; tauto.
 apply StepFfoldPropForall_Map.
 intros a.
 change (a + - a == 0)%Q.
 ring.
Qed.

Definition StepQisZero:(StepQ)->bool:=(StepFfold (fun (x:QS) => Qeq_bool x 0) (fun _  x y => x && y)).

Definition StepQeq_bool (x y:StepQ) : bool := StepQisZero (x-y).

Lemma StepQeq_bool_correct : forall x y, StepQeq_bool x y = true -> x == y.
Proof.
 intros x y H.
 destruct StepQsrt.
 rewrite <- (Radd_0_l x).
 rewrite <- (Ropp_def y).
 transitivity (y + (constStepF (0:QS))).
  set (z:=constStepF (X:=QS) 0).
  rewrite <- (Radd_assoc).
  apply StepQplus_wd.
   reflexivity.
  rewrite -> Radd_comm.
  rewrite <- Rsub_def.
  unfold StepF_eq.
  revert H.
  unfold StepQeq_bool.
  generalize (x-y).
  intros s H.
  induction s.
   apply: Qeq_bool_eq;assumption.
  symmetry in H.
  destruct (andb_true_eq _ _ H) as [H1 H2].
  split.
   apply IHs1; symmetry; assumption.
  apply IHs2; symmetry; assumption.
 rewrite -> Radd_comm.
 apply Radd_0_l.
Qed.

Lemma StepQRing_Morphism : ring_eq_ext StepQplus StepQmult StepQopp (@StepF_eq QS).
Proof.
 split.
   apply: StepQplus_wd.
  apply: StepQmult_wd.
 apply: StepQopp_wd.
Qed.

Ltac isStepQcst t :=
  match t with
  | constStepF ?q => isQcst q
  | glue ?o ?l ?r =>
   match isStepQcst l with
   |true => match isStepQcst r with
            |true => isQcst o
            |false => false
            end
   |false => false
   end
  | _ => false
  end.

Ltac StepQcst t :=
  match isStepQcst t with
    true => t
    | _ => NotConstant
  end.

Add Ring StepQRing : StepQsrt
 (decidable StepQeq_bool_correct,
  setoid (StepF_Sth QS) StepQRing_Morphism,
  constants [StepQcst]).

Definition StepQabs (s:StepQ) : StepQ := QabsS ^@> s.

Add Morphism StepQabs with signature (@StepF_eq QS) ==> (@StepF_eq QS) as StepQabs_wd.
Proof.
 intros.
 unfold StepQabs.
 rewrite -> H.
 reflexivity.
Qed.

(**
** A Partial Order on Step Functions. *)
Definition StepQ_le x y := (StepFfoldProp (QleS ^@> x <@> y)).
(* begin hide *)
Add Morphism StepQ_le
  with signature (@StepF_eq QS) ==> (@StepF_eq QS) ==> iff
 as StepQ_le_wd.
Proof.
 unfold StepQ_le.
 intros x1 x2 Hx y1 y2 Hy.
 rewrite -> Hx.
 rewrite -> Hy.
 reflexivity.
Qed.
(* end hide *)
Notation "x <= y" := (StepQ_le x y) (at level 70) : sfstscope.

Lemma StepQ_le_refl:forall x, (x <= x).
Proof.
 intros x.
 unfold StepQ_le.
 cut (StepFfoldProp (join QleS ^@> x)).
  evalStepF.
  tauto.
 apply StepFfoldPropForall_Map.
 intros.
 simpl.
 auto with *.
Qed.

Lemma StepQ_le_trans:forall x y z,
 (x <= y)-> (y <= z) ->(x <= z).
Proof.
 intros x y z. unfold StepQ_le.
 intros H.
 apply StepF_imp_imp.
 revert H.
 apply StepF_imp_imp.
 unfold StepF_imp.
 pose (f:= ap (compose (@ap _ _ _) (compose (compose (compose (@compose _ _ _) imp)) QleS))
   (compose (flip (compose (@ap _ _ _) (compose (compose imp) QleS))) QleS)).
 cut (StepFfoldProp (f ^@> x <@> y <@> z)).
  unfold f.
  evalStepF.
  tauto.
 apply StepFfoldPropForall_Map3.
 intros a b c Hab Hbc.
 clear f.
 simpl in *.
 eauto with qarith.
Qed.

Lemma StepQabsOpp : forall x, StepQabs (-x) == StepQabs (x).
Proof.
 intros x.
 unfold StepF_eq.
 set (g:=(@st_eqS QS)).
 set (f:=(ap (compose g (compose QabsS QoppS)) QabsS)).
 cut (StepFfoldProp (f ^@> x)).
  unfold f.
  evalStepF.
  tauto.
 apply StepFfoldPropForall_Map.
 intros a.
 apply: Qabs_opp.
Qed.

Lemma StepQabs_triangle : forall x y, StepQabs (x+y) <= StepQabs x + StepQabs y.
Proof.
 intros x y.
 set (f:=(ap (compose ap (compose (compose (compose QleS QabsS)) QplusS))
   (compose (flip (@compose _ _ _) QabsS) (compose QplusS QabsS)))).
 cut (StepFfoldProp (f ^@> x <@> y)).
  unfold f.
  evalStepF.
  tauto.
 apply StepFfoldPropForall_Map2.
 intros a b.
 apply: Qabs_triangle.
Qed.