This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
/
QcertData.v
542 lines (502 loc) · 19.6 KB
/
QcertData.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
(*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)
Require Import List.
Require Import ZArith.
Require Import EquivDec.
Require Import RelationClasses.
Require Import Equivalence.
Require Import String.
Require Import Qcert.Utils.Utils.
Require Import Qcert.Data.DataSystem.
Require Import Qcert.Compiler.Component.UriComponent.
Require Import LogComponent.
Require Import MathComponent.
Require Import DateTimeComponent.
Require Import MonetaryAmountComponent.
Import ListNotations.
Local Open Scope list_scope.
Inductive enhanced_data : Set
:=
| enhanceddateTimeformat : DATE_TIME_FORMAT -> enhanced_data
| enhanceddateTime : DATE_TIME -> enhanced_data
| enhanceddateTimeduration : DATE_TIME_DURATION -> enhanced_data
| enhanceddateTimeperiod : DATE_TIME_PERIOD -> enhanced_data
.
Definition enhanceddateTime_now := DATE_TIME_now.
Existing Instance date_time_format_foreign_data.
Existing Instance date_time_foreign_data.
Existing Instance date_time_duration_foreign_data.
Existing Instance date_time_period_foreign_data.
Program Instance enhanced_foreign_data : foreign_data
:= mk_foreign_data enhanced_data _ _ _ _ _ _.
Next Obligation.
red.
unfold equiv, complement.
destruct x; destruct y; simpl; try solve [right; inversion 1].
- destruct (@equiv_dec _ _ _ (@foreign_data_dec date_time_format_foreign_data) d d0).
+ left; congruence.
+ right; congruence.
- destruct (@equiv_dec _ _ _ (@foreign_data_dec date_time_foreign_data) d d0).
+ left; congruence.
+ right; congruence.
- destruct (@equiv_dec _ _ _ (@foreign_data_dec date_time_duration_foreign_data) d d0).
+ left; congruence.
+ right; congruence.
- destruct (@equiv_dec _ _ _ (@foreign_data_dec date_time_period_foreign_data) d d0).
+ left; congruence.
+ right; congruence.
Defined.
Next Obligation.
(* normalized? *)
destruct a.
- exact True.
- exact (@foreign_data_normalized date_time_foreign_data d).
- exact (@foreign_data_normalized date_time_duration_foreign_data d).
- exact (@foreign_data_normalized date_time_period_foreign_data d).
Defined.
Next Obligation.
destruct a.
- simpl; trivial.
- exact (@foreign_data_normalize_normalizes date_time_foreign_data d).
- exact (@foreign_data_normalize_normalizes date_time_duration_foreign_data d).
- exact (@foreign_data_normalize_normalizes date_time_period_foreign_data d).
Defined.
Next Obligation.
constructor.
destruct 1.
- exact (@toString _ (@foreign_data_tostring date_time_format_foreign_data) d).
- exact (@toString _ (@foreign_data_tostring date_time_foreign_data) d).
- exact (@toString _ (@foreign_data_tostring date_time_duration_foreign_data) d).
- exact (@toString _ (@foreign_data_tostring date_time_period_foreign_data) d).
Defined.
Definition denhanceddateTimeformat td := dforeign (enhanceddateTimeformat td).
Definition denhanceddateTime td := dforeign (enhanceddateTime td).
Definition denhanceddateTimeduration td := dforeign (enhanceddateTimeduration td).
Definition denhanceddateTimeperiod td := dforeign (enhanceddateTimeperiod td).
Inductive enhanced_unary_op :=
| enhanced_unary_uri_op : uri_unary_op -> enhanced_unary_op
| enhanced_unary_log_op : log_unary_op -> enhanced_unary_op
| enhanced_unary_math_op : math_unary_op -> enhanced_unary_op
| enhanced_unary_date_time_op : date_time_unary_op -> enhanced_unary_op
.
Definition onddateTime {A} (f : DATE_TIME -> A) (d : data) : option A :=
match d with
| dforeign (enhanceddateTime fd) => Some (f fd)
| _ => None
end.
Definition lift_dateTimeList (l:list data) : option (list DATE_TIME) :=
lift_map
(fun d : data =>
match d with
| dforeign (enhanceddateTime fd) => Some fd
| _ => None
end) l.
Definition onddateTimeList (f : list DATE_TIME -> DATE_TIME) (d : data) : option DATE_TIME :=
let odates :=
match d with
| dcoll c => lift_dateTimeList c
| _ => None
end
in
lift f odates.
Definition onddateTimeduration {A} (f : DATE_TIME_DURATION -> A) (d : data) : option A :=
match d with
| dforeign (enhanceddateTimeduration fd) => Some (f fd)
| _ => None
end.
Definition onddateTimeDurationNat {A} (f : Z -> A) (d : data) : option A :=
match d with
| dnat z => Some (f z)
| _ => None
end.
Definition onddateTimePeriodNat {A} (f : Z -> A) (d : data) : option A :=
match d with
| dnat z => Some (f z)
| _ => None
end.
Definition ondstring {A} (f : String.string -> A) (d : data) : option A :=
match d with
| dstring s => Some (f s)
| _ => None
end.
Definition ondstringfloatopt (f : String.string -> option float) (d : data) : option data :=
match d with
| dstring s =>
match f s with
| None => Some dnone
| Some n => Some (dsome (dfloat n))
end
| _ => None
end.
Definition ondstringunit (f : String.string -> unit) (d : data) : option data :=
match d with
| dstring s =>
match f s with (* Call log *)
| y => if unit_eqdec y tt then Some dunit else None (* Return unit *)
end
| _ => None
end.
Definition ondstringstring (f : String.string -> string) (d : data) : option data :=
match d with
| dstring s =>
Some (dstring (f s))
| _ => None
end.
Definition ondfloat {A} (f : float -> A) (d : data) : option A :=
match d with
| dfloat s => Some (f s)
| _ => None
end.
Definition uri_unary_op_interp (op:uri_unary_op) (d:data) : option data :=
match op with
| uop_uri_encode => ondstringstring URI_encode d
| uop_uri_decode => ondstringstring URI_decode d
end.
Definition log_unary_op_interp (op:log_unary_op) (d:data) : option data :=
match op with
| uop_log_string => ondstringunit LOG_string d
end.
Definition math_unary_op_interp (op:math_unary_op) (d:data) : option data :=
match op with
| uop_math_float_of_string => ondstringfloatopt FLOAT_of_string d
| uop_math_acos => lift dfloat (ondfloat FLOAT_acos d)
| uop_math_asin => lift dfloat (ondfloat FLOAT_asin d)
| uop_math_atan => lift dfloat (ondfloat FLOAT_atan d)
| uop_math_cos => lift dfloat (ondfloat FLOAT_cos d)
| uop_math_cosh => lift dfloat (ondfloat FLOAT_cosh d)
| uop_math_sin => lift dfloat (ondfloat FLOAT_sin d)
| uop_math_sinh => lift dfloat (ondfloat FLOAT_sinh d)
| uop_math_tan => lift dfloat (ondfloat FLOAT_tan d)
| uop_math_tanh => lift dfloat (ondfloat FLOAT_tanh d)
end.
Definition date_time_unary_op_interp (op:date_time_unary_op) (d:data) : option data :=
match op with
| uop_date_time_get_seconds =>
lift dnat (onddateTime DATE_TIME_get_seconds d)
| uop_date_time_get_minutes =>
lift dnat (onddateTime DATE_TIME_get_minutes d)
| uop_date_time_get_hours =>
lift dnat (onddateTime DATE_TIME_get_hours d)
| uop_date_time_get_days =>
lift dnat (onddateTime DATE_TIME_get_days d)
| uop_date_time_get_weeks =>
lift dnat (onddateTime DATE_TIME_get_weeks d)
| uop_date_time_get_months =>
lift dnat (onddateTime DATE_TIME_get_months d)
| uop_date_time_get_quarters =>
lift dnat (onddateTime DATE_TIME_get_quarters d)
| uop_date_time_get_years =>
lift dnat (onddateTime DATE_TIME_get_years d)
| uop_date_time_start_of_day =>
lift denhanceddateTime (onddateTime DATE_TIME_start_of_day d)
| uop_date_time_start_of_week =>
lift denhanceddateTime (onddateTime DATE_TIME_start_of_week d)
| uop_date_time_start_of_month =>
lift denhanceddateTime (onddateTime DATE_TIME_start_of_month d)
| uop_date_time_start_of_quarter =>
lift denhanceddateTime (onddateTime DATE_TIME_start_of_quarter d)
| uop_date_time_start_of_year =>
lift denhanceddateTime (onddateTime DATE_TIME_start_of_year d)
| uop_date_time_end_of_day =>
lift denhanceddateTime (onddateTime DATE_TIME_end_of_day d)
| uop_date_time_end_of_week =>
lift denhanceddateTime (onddateTime DATE_TIME_end_of_week d)
| uop_date_time_end_of_month =>
lift denhanceddateTime (onddateTime DATE_TIME_end_of_month d)
| uop_date_time_end_of_quarter =>
lift denhanceddateTime (onddateTime DATE_TIME_end_of_quarter d)
| uop_date_time_end_of_year =>
lift denhanceddateTime (onddateTime DATE_TIME_end_of_year d)
| uop_date_time_format_from_string =>
lift denhanceddateTimeformat (ondstring DATE_TIME_FORMAT_from_string d)
| uop_date_time_from_string =>
lift denhanceddateTime (ondstring DATE_TIME_from_string d)
| uop_date_time_max =>
lift denhanceddateTime (onddateTimeList DATE_TIME_max d)
| uop_date_time_min =>
lift denhanceddateTime (onddateTimeList DATE_TIME_min d)
| uop_date_time_duration_amount =>
lift dnat (onddateTimeduration DATE_TIME_DURATION_amount d)
| uop_date_time_duration_from_string =>
lift denhanceddateTimeduration (ondstring DATE_TIME_DURATION_from_string d)
| uop_date_time_duration_from_seconds =>
lift denhanceddateTimeduration (onddateTimeDurationNat DATE_TIME_DURATION_from_seconds d)
| uop_date_time_duration_from_minutes =>
lift denhanceddateTimeduration (onddateTimeDurationNat DATE_TIME_DURATION_from_minutes d)
| uop_date_time_duration_from_hours =>
lift denhanceddateTimeduration (onddateTimeDurationNat DATE_TIME_DURATION_from_hours d)
| uop_date_time_duration_from_days =>
lift denhanceddateTimeduration (onddateTimeDurationNat DATE_TIME_DURATION_from_days d)
| uop_date_time_duration_from_weeks =>
lift denhanceddateTimeduration (onddateTimeDurationNat DATE_TIME_DURATION_from_weeks d)
| uop_date_time_period_from_string =>
lift denhanceddateTimeperiod (ondstring DATE_TIME_PERIOD_from_string d)
| uop_date_time_period_from_days =>
lift denhanceddateTimeperiod (onddateTimePeriodNat DATE_TIME_PERIOD_from_days d)
| uop_date_time_period_from_weeks =>
lift denhanceddateTimeperiod (onddateTimePeriodNat DATE_TIME_PERIOD_from_weeks d)
| uop_date_time_period_from_months =>
lift denhanceddateTimeperiod (onddateTimePeriodNat DATE_TIME_PERIOD_from_months d)
| uop_date_time_period_from_quarters =>
lift denhanceddateTimeperiod (onddateTimePeriodNat DATE_TIME_PERIOD_from_quarters d)
| uop_date_time_period_from_years =>
lift denhanceddateTimeperiod (onddateTimePeriodNat DATE_TIME_PERIOD_from_years d)
end.
Definition enhanced_unary_op_interp
(br:brand_relation_t)
(op:enhanced_unary_op)
(d:data) : option data :=
match op with
| enhanced_unary_uri_op f => uri_unary_op_interp f d
| enhanced_unary_log_op f => log_unary_op_interp f d
| enhanced_unary_math_op f => math_unary_op_interp f d
| enhanced_unary_date_time_op f => date_time_unary_op_interp f d
end.
Section toString. (* XXX Maybe to move as a component ? *)
Fixpoint enumToString (b:brands) (d:data) : string :=
match d with
| dleft (dstring s) =>
append "~"
(append (@toString _ ToString_brands b)
(append "."
(stringToString s)))
| dright d => enumToString b d
| _ => "<BOGUS ENUM>"
end.
Fixpoint dataToString (d:data) : string :=
match d with
| dunit => "unit"%string
| dnat n => toString n
| dfloat n => toString n
| dbool b => toString b
| dstring s => string_bracket """"%string (stringToString s) """"%string
| dcoll l => string_bracket
"["%string
(concat ", "
(map dataToString l))
"]"%string
| drec lsd => string_bracket
"{"%string
(concat ","
(map (fun xy => let '(x,y):=xy in
(append (stringToString x) (append ":"%string
(dataToString y)))
) lsd))
"}"%string
| dleft d => string_bracket
"Left("%string
(dataToString d)
")"%string
| dright d => string_bracket
"Right("%string
(dataToString d)
")"%string
| dbrand b d =>
match d with
| drec _ =>
append "~"
(append (@toString _ ToString_brands b)
(dataToString d))
| dleft _
| dright _ =>
enumToString b d
| _ =>
"<BOGUS OBJECT>"
end
| dforeign fd => toString fd
end.
Definition dataToText (d:data) : string := dataToString d.
End toString.
Inductive enhanced_binary_op :=
| enhanced_binary_math_op : math_binary_op -> enhanced_binary_op
| enhanced_binary_date_time_op : date_time_binary_op -> enhanced_binary_op
| enhanced_binary_monetary_amount_op : monetary_amount_binary_op -> enhanced_binary_op
.
Definition ondfloat2 {A} (f : float -> float -> A) (d1 d2 : data) : option A :=
match d1, d2 with
| dfloat fd1, dfloat fd2 => Some (f fd1 fd2)
| _, _ => None
end.
Definition onddateTime2 {A} (f : DATE_TIME -> DATE_TIME -> A) (d1 d2 : data) : option A :=
match d1, d2 with
| dforeign (enhanceddateTime fd1), dforeign (enhanceddateTime fd2) => Some (f fd1 fd2)
| _, _ => None
end.
Definition rondbooldateTime2 (f: DATE_TIME -> DATE_TIME -> bool) (d1 d2:data) : option data :=
lift dbool (onddateTime2 f d1 d2).
Definition math_binary_op_interp
(op:math_binary_op) (d1 d2:data) : option data :=
match op with
| bop_math_atan2 => lift dfloat (ondfloat2 FLOAT_atan2 d1 d2)
end.
Definition date_time_binary_op_interp
(op:date_time_binary_op) (d1 d2:data) : option data :=
match op with
| bop_date_time_format
=> match d1, d2 with
| dforeign (enhanceddateTime tp), dforeign (enhanceddateTimeformat td)
=> Some (dstring (DATE_TIME_format tp td))
| _,_ => None
end
| bop_date_time_add
=> match d1, d2 with
| dforeign (enhanceddateTime tp), dforeign (enhanceddateTimeduration td)
=> Some (denhanceddateTime (DATE_TIME_add tp td))
| _,_ => None
end
| bop_date_time_subtract
=> match d1, d2 with
| dforeign (enhanceddateTime tp), dforeign (enhanceddateTimeduration td)
=> Some (denhanceddateTime (DATE_TIME_subtract tp td))
| _,_ => None
end
| bop_date_time_add_period
=> match d1, d2 with
| dforeign (enhanceddateTime tp), dforeign (enhanceddateTimeperiod td)
=> Some (denhanceddateTime (DATE_TIME_add_period tp td))
| _,_ => None
end
| bop_date_time_subtract_period
=> match d1, d2 with
| dforeign (enhanceddateTime tp), dforeign (enhanceddateTimeperiod td)
=> Some (denhanceddateTime (DATE_TIME_subtract_period tp td))
| _,_ => None
end
| bop_date_time_is_same => rondbooldateTime2 DATE_TIME_eq d1 d2
| bop_date_time_is_before => rondbooldateTime2 DATE_TIME_is_before d1 d2
| bop_date_time_is_after => rondbooldateTime2 DATE_TIME_is_after d1 d2
| bop_date_time_diff => lift denhanceddateTimeduration (onddateTime2 DATE_TIME_diff d1 d2)
end.
Definition monetary_amount_binary_op_interp
(op:monetary_amount_binary_op) (d1 d2:data) : option data
:= match op with
| bop_monetary_amount_format =>
match d1, d2 with
| dfloat f1, dstring s2 =>
Some (dstring (MONETARY_AMOUNT_format f1 s2))
| _, _ =>
None
end
| bop_monetary_code_format =>
match d1, d2 with
| dstring s1, dstring s2 =>
Some (dstring (MONETARY_CODE_format s1 s2))
| _, _ =>
None
end
end.
Definition enhanced_binary_op_interp
(br:brand_relation_t)
(op:enhanced_binary_op)
(d1 d2:data) : option data :=
match op with
| enhanced_binary_math_op f => math_binary_op_interp f d1 d2
| enhanced_binary_date_time_op f => date_time_binary_op_interp f d1 d2
| enhanced_binary_monetary_amount_op f => monetary_amount_binary_op_interp f d1 d2
end.
Program Instance enhanced_foreign_operators : foreign_operators
:= { foreign_operators_unary := enhanced_unary_op
; foreign_operators_unary_interp := enhanced_unary_op_interp
; foreign_operators_unary_data_tostring := dataToString
; foreign_operators_unary_data_totext := dataToText
; foreign_operators_binary := enhanced_binary_op
; foreign_operators_binary_interp := enhanced_binary_op_interp }.
Next Obligation.
red; unfold equiv; intros.
change ({x = y} + {x <> y}).
repeat (decide equality).
Defined.
Next Obligation.
constructor; intros op.
destruct op.
- exact (uri_unary_op_tostring u).
- exact (log_unary_op_tostring l).
- exact (math_unary_op_tostring m).
- exact (date_time_unary_op_tostring d).
Defined.
Next Obligation.
destruct op; simpl in H.
- destruct u; simpl in H; unfold ondstringunit, lift in H; simpl in H;
destruct d; simpl in H; try discriminate; invcs H; repeat constructor.
- destruct l; simpl in H; unfold ondstringunit, lift in H; simpl in H;
destruct d; simpl in H; try discriminate; invcs H; repeat constructor.
- destruct m; simpl in H; unfold ondstring, ondfloat, lift in H; simpl in H;
destruct d; simpl in H; try discriminate; invcs H; repeat constructor.
destruct (FLOAT_of_string s); try discriminate; invcs H2; repeat constructor.
- destruct d0; simpl in H;
unfold onddateTime, onddateTimeList, onddateTimeduration,
denhanceddateTime, denhanceddateTimeduration,
denhanceddateTimeperiod, lift in H;
simpl in H;
destruct d; simpl in H; try discriminate; try (try (destruct f); invcs H; repeat constructor).
+ case_eq (match lift_dateTimeList l with
| Some a' => Some (DATE_TIME_max a')
| None => None
end); intros;
rewrite H in H2.
inversion H2.
invcs H3; repeat constructor.
congruence.
+ case_eq (match lift_dateTimeList l with
| Some a' => Some (DATE_TIME_min a')
| None => None
end); intros;
rewrite H in H2.
inversion H2.
invcs H3; repeat constructor.
congruence.
Defined.
Next Obligation.
red; unfold equiv; intros.
change ({x = y} + {x <> y}).
repeat (decide equality).
Defined.
Next Obligation.
constructor; intros op.
destruct op.
- exact (math_binary_op_tostring m).
- exact (date_time_binary_op_tostring d).
- exact (monetary_amount_binary_op_tostring m).
Defined.
Next Obligation.
destruct op; simpl in H.
- destruct m; simpl in H;
unfold ondfloat2, lift in H
; destruct d1; simpl in H; try discriminate
; destruct d2; simpl in H; try discriminate
; try (destruct f; simpl in H; try discriminate)
; invcs H
; repeat constructor.
- destruct d; simpl in H;
unfold rondbooldateTime2, onddateTime2, denhanceddateTime, lift in H
; destruct d1; simpl in H; try discriminate
; destruct f; simpl in H; try discriminate
; destruct d2; simpl in H; try discriminate
; try (destruct f; simpl in H; try discriminate)
; invcs H
; repeat constructor.
- destruct m; simpl in H;
unfold ondfloat2, lift in H
; destruct d1; simpl in H; try discriminate
; try (destruct f; simpl in H; try discriminate)
; destruct d2; simpl in H; try discriminate
; try (destruct f; simpl in H; try discriminate)
; invcs H
; repeat constructor.
Defined.
Instance enhanced_foreign_runtime :
foreign_runtime
:= mk_foreign_runtime
enhanced_foreign_data
enhanced_foreign_operators.