-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogical.v
More file actions
3667 lines (3455 loc) · 86.7 KB
/
logical.v
File metadata and controls
3667 lines (3455 loc) · 86.7 KB
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Export ExtLib.Structures.Monads.
Export MonadNotation.
Open Scope monad_scope.
From Coq Require Import Lia.
From Coq Require Import Init.Nat.
From Coq Require Import Lists.List.
From Coq Require Import PeanoNat.
Import ListNotations.
Open Scope list_scope.
Instance optionMonad : Monad option :=
{
ret T x := Some x;
bind T U m f :=
match m with
| Some x => f x
| None => None
end;
}.
(* Return the nth item from the list if it exists *)
Fixpoint nth {X : Type} (lst : list X) (n : nat) : option X :=
match lst with
| [] => None
| (x :: xs) =>
match n with
| 0 => Some x
| S n' => nth xs n'
end
end.
Theorem nth_spec {X: Type} : forall (lst : list X) n,
n < length lst ->
exists x,
nth lst n = Some x.
Proof.
intros lst.
induction lst; intros.
- inversion H.
- destruct n.
+ simpl. exists a. auto.
+ simpl.
apply IHlst.
simpl in H.
Search lt.
apply Lt.lt_S_n.
auto.
Qed.
Definition tuple := list nat.
(* This is a database table *)
Record relation : Type :=
{
data : list tuple; (* Rows in the table *)
order : nat; (* number of columns in each row *)
}.
Example r_ex :=
{| data := [[1;1];[1]]; order := 3 |}.
Example good_r :=
{| data := [[1;1;1]]; order := 3 |}.
Definition coherent_relation (r : relation) : Prop :=
forall (t : tuple),
In t (data r) ->
length t = (order r).
Definition coherent_relations (r1 r2 : relation) : Prop :=
coherent_relation r1 /\ coherent_relation r2 /\ order r1 = order r2.
Definition boolean := bool.
Definition relname : Type := nat.
Definition select : Type := nat.
Definition database := list relation.
Definition get_relation (db : database) (n : relname) : option relation :=
nth db n.
Definition schema := list nat.
Definition coherent_database (db : database) : Prop :=
forall r,
In r db ->
coherent_relation r.
Definition compliant_relation (r : relation) (o : nat) : Prop :=
coherent_relation r /\ order r = o.
Definition compliant_database (db : database) (s : schema) : Prop :=
forall relname (o : nat),
nth s relname = Some o ->
exists (r : relation),
get_relation db relname = Some r /\ compliant_relation r o.
Lemma one_relation_per_name : forall (db : database) (name : relname) (r1 r2 : relation),
get_relation db name = Some r1 ->
get_relation db name = Some r2 ->
r1 = r2.
Proof.
intros.
rewrite H0 in H.
injection H.
auto.
Qed.
Example ex_r1 : relation :=
{| data := [[1;1;1];[1;1;1]]; order := 3; |}.
Example ex_r2 : relation :=
{| data := [[2;2]; [2;2]]; order := 2 |}.
Example ex_db : database := [ex_r1; ex_r2].
Example ex_schema : schema := [3; 2].
(* TODO make proving compliance easeir *)
Example ex_compiant : compliant_database ex_db ex_schema.
Proof.
simpl.
unfold compliant_database.
intros.
simpl in H.
destruct relname0.
- inversion H.
subst.
exists ex_r1.
split.
+ unfold get_relation.
simpl.
reflexivity.
+ unfold compliant_relation.
split.
* unfold coherent_relation.
intros.
inversion H0.
rewrite <- H1.
auto.
inversion H1.
rewrite <- H2.
auto.
inversion H2.
* auto.
- destruct relname0.
+ inversion H.
subst.
exists ex_r2.
split.
auto.
unfold compliant_relation.
unfold coherent_relation.
split.
intros.
inversion H0.
rewrite <- H1.
auto.
inversion H1.
rewrite <- H2.
auto.
inversion H2.
auto.
+ simpl.
discriminate H.
Qed.
Inductive quantifier : Type :=
| Forall
| Exists.
Inductive set_op : Type :=
| Union
| Intersection.
Inductive type : Type :=
| Number
| Boolean.
Definition type_eqb t1 t2 : boolean :=
match t1, t2 with
| Number, Number => true
| Boolean, Boolean => true
| _, _ => false
end.
Theorem type_eqb_eq : forall t1 t2,
type_eqb t1 t2 = true <-> t1 = t2.
Proof.
split;
intros;
destruct t1,t2;
auto;
simpl in H;
discriminate.
Qed.
Theorem type_eqb_refl : forall t1,
type_eqb t1 t1 = true.
Proof.
intros.
rewrite type_eqb_eq.
reflexivity.
Qed.
Inductive predicate : Type :=
| Literal : nat -> predicate
| Tru
| Fls
| And : predicate -> predicate -> predicate
| Or : predicate -> predicate -> predicate
| Equals : predicate -> predicate -> predicate
| Plus : predicate -> predicate -> predicate
| LessThan : predicate -> predicate -> predicate
| Field : nat -> predicate.
Inductive value : Type :=
| VTrue
| VFalse
| VNum : nat -> value.
Definition value_type (v : value) : type :=
match v with
| VTrue => Boolean
| VFalse => Boolean
| VNum _ => Number
end.
Definition value_eqb v1 v2 : boolean :=
match v1, v2 with
| VTrue, VTrue => true
| VFalse, VFalse => true
| VNum n1, VNum n2 => n1 =? n2
| _, _ => false
end.
Theorem value_eqb_eq : forall v1 v2,
value_eqb v1 v2 = true <-> v1 = v2.
Proof.
split; intros; destruct v1, v2; auto;
try (simpl in H; discriminate).
- simpl in H.
apply Nat.eqb_eq in H.
auto.
- simpl.
apply Nat.eqb_eq.
injection H.
auto.
Qed.
Lemma value_eqb_refl : forall v,
value_eqb v v = true.
Proof.
intros.
apply value_eqb_eq.
auto.
Qed.
Fixpoint compute_type (p : predicate) (order : nat) : option type :=
match p with
| Literal _ => Some Number
| Field n => if n <? order then Some Number else None
| Tru => Some Boolean
| Fls => Some Boolean
| Equals p1 p2 =>
t1 <- compute_type p1 order;;
t2 <- compute_type p2 order;;
if andb (type_eqb t1 t2) (type_eqb t1 Number) then
Some Boolean
else None
| Plus p1 p2 =>
t1 <- compute_type p1 order;;
t2 <- compute_type p2 order;;
if andb (type_eqb t1 t2) (type_eqb t1 Number) then
Some Number
else None
| LessThan p1 p2 =>
t1 <- compute_type p1 order;;
t2 <- compute_type p2 order;;
if andb (type_eqb t1 t2) (type_eqb t1 Number) then
Some Boolean
else None
| And p1 p2 =>
t1 <- compute_type p1 order;;
t2 <- compute_type p2 order;;
if andb (type_eqb t1 t2) (type_eqb t1 Boolean) then
Some Boolean
else None
| Or p1 p2 =>
t1 <- compute_type p1 order;;
t2 <- compute_type p2 order;;
if andb (type_eqb t1 t2) (type_eqb t1 Boolean) then
Some Boolean
else None
end.
Inductive well_typed : nat -> predicate -> type -> Prop :=
| WT_Lit : forall n o, well_typed o (Literal n) Number
| WT_Field : forall i o,
i < o ->
well_typed o (Field i) Number
| WT_True : forall o, well_typed o (Tru) Boolean
| WT_False : forall o, well_typed o (Fls) Boolean
| WT_Plus : forall o p1 p2,
well_typed o p1 Number ->
well_typed o p2 Number ->
well_typed o (Plus p1 p2) Number
| WT_Equals : forall o p1 p2,
well_typed o p1 Number ->
well_typed o p2 Number ->
well_typed o (Equals p1 p2) Boolean
| WT_LessThan : forall o p1 p2,
well_typed o p1 Number ->
well_typed o p2 Number ->
well_typed o (LessThan p1 p2) Boolean
| WT_And : forall o p1 p2,
well_typed o p1 Boolean ->
well_typed o p2 Boolean ->
well_typed o (And p1 p2) Boolean
| WT_Or : forall o p1 p2,
well_typed o p1 Boolean ->
well_typed o p2 Boolean ->
well_typed o (Or p1 p2) Boolean.
Theorem well_typed_computable : forall p o t,
well_typed o p t <-> compute_type p o = Some t.
Proof.
intros p.
induction p; split; intros; try (inversion H; subst); auto.
- apply WT_Lit.
- apply WT_True.
- apply WT_False.
- apply IHp1 in H3.
apply IHp2 in H5.
simpl.
rewrite H3.
rewrite H5.
simpl.
reflexivity.
- destruct t;
destruct (compute_type p1 o) eqn:Hp1;
destruct (compute_type p2 o) eqn:Hp2;
try discriminate.
+ destruct (type_eqb t t0) eqn:Hteq;
destruct (type_eqb t Boolean) eqn:Hbool;
try (simpl in H1; discriminate).
+ destruct (type_eqb t t0) eqn:Hteq;
destruct (type_eqb t Boolean) eqn:Hbool;
try (simpl in H1; discriminate).
apply type_eqb_eq in Hbool.
apply type_eqb_eq in Hteq.
subst.
apply IHp1 in Hp1.
apply IHp2 in Hp2.
apply WT_And; auto.
- simpl.
apply IHp1 in H3.
apply IHp2 in H5.
rewrite H3.
rewrite H5.
auto.
- destruct t;
destruct (compute_type p1 o) eqn:Hp1;
destruct (compute_type p2 o) eqn:Hp2;
try discriminate.
+ destruct (type_eqb t t0);
destruct (type_eqb t Boolean);
try discriminate.
+ destruct (type_eqb t t0) eqn:Heq1;
destruct (type_eqb t Boolean) eqn:Heq2;
try discriminate.
apply type_eqb_eq in Heq1.
apply type_eqb_eq in Heq2.
subst.
apply IHp1 in Hp1.
apply IHp2 in Hp2.
apply WT_Or; auto.
- apply IHp1 in H3.
apply IHp2 in H5.
simpl.
rewrite H3.
rewrite H5.
auto.
- destruct (compute_type p1 o) eqn:Hp1;
destruct (compute_type p2 o) eqn:Hp2;
try discriminate.
+ destruct (type_eqb t0 t1) eqn:Heq1;
destruct (type_eqb t0 Number) eqn:Heq2;
try discriminate.
apply type_eqb_eq in Heq1.
apply type_eqb_eq in Heq2.
subst.
apply IHp1 in Hp1.
apply IHp2 in Hp2.
destruct t; try (simpl in H; discriminate).
apply WT_Equals; auto.
- apply IHp1 in H3.
apply IHp2 in H5.
simpl.
rewrite H3.
rewrite H5.
auto.
- destruct (compute_type p1 o) eqn:Hp1;
destruct (compute_type p2 o) eqn:Hp2;
try discriminate.
destruct t;
destruct (type_eqb t0 t1) eqn:Heq1;
destruct (type_eqb t0 Number) eqn:Heq2;
try discriminate.
apply IHp1 in Hp1.
apply IHp2 in Hp2.
apply type_eqb_eq in Heq2.
apply type_eqb_eq in Heq1.
subst.
apply WT_Plus; auto.
- simpl.
apply IHp1 in H3.
apply IHp2 in H5.
rewrite H3.
rewrite H5.
auto.
- destruct (compute_type p1 o) eqn:Hp1;
destruct (compute_type p2 o) eqn:Hp2;
try discriminate.
destruct t;
destruct (type_eqb t0 t1) eqn:Heq1;
destruct (type_eqb t0 Number) eqn:Heq2;
try discriminate.
apply type_eqb_eq in Heq1.
apply type_eqb_eq in Heq2.
subst.
apply IHp1 in Hp1.
apply IHp2 in Hp2.
apply WT_LessThan; auto.
- simpl.
apply Nat.ltb_lt in H2.
rewrite H2.
reflexivity.
- destruct (n <? o) eqn:Hlt.
+ injection H1.
intros.
subst.
apply WT_Field.
apply Nat.ltb_lt in Hlt.
auto.
+ discriminate.
Qed.
Inductive valid_predicate : nat -> predicate -> Prop :=
| Valid : forall o p,
well_typed o p Boolean ->
valid_predicate o p.
Definition is_valid_predicate (o : nat) (p : predicate) : boolean :=
match compute_type p o with
| None => false
| Some t => type_eqb t Boolean
end.
Lemma valid_predicate_computable : forall o p,
valid_predicate o p <-> is_valid_predicate o p = true.
Proof.
split.
- intros.
inversion H.
subst.
apply well_typed_computable in H0.
unfold is_valid_predicate.
rewrite H0.
auto.
- intros.
unfold is_valid_predicate in H.
destruct (compute_type p o) eqn:Htype.
+ apply Valid.
apply well_typed_computable in Htype.
apply type_eqb_eq in H.
subst.
apply Htype.
+ discriminate.
Qed.
Definition extract_field (t : tuple) (i : nat) : option nat :=
nth_error t i.
Lemma extract_coherent_field : forall i (t : tuple) o,
length t = o ->
i < o ->
exists (n : nat),
extract_field t i = Some n.
Proof.
intros.
destruct (extract_field t i) eqn:Heq.
- exists n.
reflexivity.
- exfalso.
unfold extract_field in Heq.
assert (nth_error t i <> None).
{
apply nth_error_Some.
rewrite <- H in H0.
apply H0.
}.
rewrite Heq in H1.
unfold not in H1.
apply H1.
reflexivity.
Qed.
Inductive predicate_value : predicate -> Prop :=
| LitVal : forall n, predicate_value (Literal n)
| TruVal : predicate_value Tru
| FlsVal : predicate_value Fls.
Lemma value_choice : forall p,
predicate_value p \/ ~ (predicate_value p).
Proof.
intros.
induction p;
try (right; unfold not; intros; inversion H; fail).
- left. apply LitVal.
- left. apply TruVal.
- left. apply FlsVal.
Qed.
(* small step semantics for predicate *)
Inductive predicate_steps : predicate -> tuple -> predicate -> Prop :=
| FieldStep : forall t i n,
extract_field t i = Some n ->
predicate_steps (Field i) t (Literal n)
| Andvalue__true : forall p1 p2 t,
predicate_value p1 ->
predicate_value p2 ->
p1 = Tru ->
p2 = Tru ->
predicate_steps (And p1 p2) t (Tru)
| Andvalue__false : forall p1 p2 t,
predicate_value p1 ->
predicate_value p2 ->
p1 = Fls \/ p2 = Fls ->
predicate_steps (And p1 p2) t (Fls)
| And1 : forall p1 p1' p2 t,
predicate_steps p1 t p1' ->
predicate_steps (And p1 p2) t (And p1' p2)
| And2 : forall p1 p2 p2' t,
predicate_value p1 ->
predicate_steps p2 t p2' ->
predicate_steps (And p1 p2) t (And p1 p2')
| Orvalue__true : forall p1 p2 t,
predicate_value p1 ->
predicate_value p2 ->
p1 = Tru \/ p2 = Tru ->
predicate_steps (Or p1 p2) t (Tru)
| Orvalue__false : forall p1 p2 t,
predicate_value p1 ->
predicate_value p2 ->
p1 = Fls ->
p2 = Fls ->
predicate_steps (Or p1 p2) t (Fls)
| Or1 : forall p1 p1' p2 t,
predicate_steps p1 t p1' ->
predicate_steps (Or p1 p2) t (Or p1' p2)
| Or2 : forall p1 p2 p2' t,
predicate_value p1 ->
predicate_steps p2 t p2' ->
predicate_steps (Or p1 p2) t (Or p1 p2')
| Equalsvalue__true : forall p1 p2 t,
predicate_value p1 ->
predicate_value p2 ->
p1 = p2 ->
predicate_steps (Equals p1 p2) t (Tru)
| Equalsvalue__false : forall p1 p2 t,
predicate_value p1 ->
predicate_value p2 ->
p1 <> p2 ->
predicate_steps (Equals p1 p2) t (Fls)
| Equals1 : forall p1 p1' p2 t,
predicate_steps p1 t p1' ->
predicate_steps (Equals p1 p2) t (Equals p1' p2)
| Equals2 : forall p1 p2 p2' t,
predicate_value p1 ->
predicate_steps p2 t p2' ->
predicate_steps (Equals p1 p2) t (Equals p1 p2')
| LessThanvalue__true : forall p1 p2 t n1 n2,
predicate_value p1 ->
predicate_value p2 ->
p1 = Literal n1 ->
p2 = Literal n2 ->
n1 < n2 ->
predicate_steps (LessThan p1 p2) t (Tru)
| LessThanvalue__false : forall p1 p2 t n1 n2,
predicate_value p1 ->
predicate_value p2 ->
p1 = Literal n1 ->
p2 = Literal n2 ->
n2 <= n1 ->
predicate_steps (LessThan p1 p2) t (Fls)
| LessThan1 : forall p1 p1' p2 t,
predicate_steps p1 t p1' ->
predicate_steps (LessThan p1 p2) t (LessThan p1' p2)
| LessThan2 : forall p1 p2 p2' t,
predicate_value p1 ->
predicate_steps p2 t p2' ->
predicate_steps (LessThan p1 p2) t (LessThan p1 p2')
| Plus__value : forall p1 n1 p2 n2 t,
predicate_value p1 ->
predicate_value p2 ->
p1 = Literal n1 ->
p2 = Literal n2 ->
predicate_steps (Plus p1 p2) t
(Literal (n1 + n2))
| Plus1 : forall p1 p1' p2 t,
predicate_steps p1 t p1' ->
predicate_steps (Plus p1 p2) t (Plus p1' p2)
| Plus2 : forall p1 p2 p2' t,
predicate_value p1 ->
predicate_steps p2 t p2' ->
predicate_steps (Plus p1 p2) t (Plus p1 p2').
(* Big Step relation *)
Inductive predicate_bigstep : predicate -> tuple -> predicate -> Prop :=
| Refl : forall p1 t, predicate_bigstep p1 t p1
| Steps : forall p1 t p2,
predicate_steps p1 t p2 ->
predicate_bigstep p1 t p2
| Trans : forall p1 p2 p3 t,
predicate_bigstep p1 t p2 ->
predicate_bigstep p2 t p3 ->
predicate_bigstep p1 t p3.
Definition make_value (p : predicate) : option value :=
match p with
| Literal n => Some (VNum n)
| Tru => Some VTrue
| Fls => Some VFalse
| _ => None
end.
Lemma make_value_spec : forall p,
predicate_value p ->
exists v,
make_value p = Some v.
Proof.
intros.
inversion H; simpl.
- exists (VNum n).
auto.
- exists VTrue.
auto.
- exists VFalse.
auto.
Qed.
Lemma make_value_type_spec : forall p t o,
well_typed o p t ->
predicate_value p ->
exists v,
make_value p = Some v /\
value_type v = t.
Proof.
intros.
inversion H0; rewrite <- H1 in H; inversion H;
subst.
- exists (VNum n).
split; auto.
- exists VTrue. split; auto.
- exists VFalse. split; auto.
Qed.
Fixpoint predicate_eqb (p1 p2 : predicate) : boolean :=
match (p1, p2) with
| (Literal n1, Literal n2) => n1 =? n2
| (Field n1, Field n2) => n1 =? n2
| (Tru, Tru) => true
| (Fls, Fls) => true
| (Plus n11 n12, Plus n21 n22) =>
andb (predicate_eqb n11 n21)
(predicate_eqb n12 n22)
| (Equals n11 n12, Equals n21 n22) =>
andb (predicate_eqb n11 n21)
(predicate_eqb n12 n22)
| (LessThan n11 n12, LessThan n21 n22) =>
andb (predicate_eqb n11 n21)
(predicate_eqb n12 n22)
| (Or n11 n12, Or n21 n22) =>
andb (predicate_eqb n11 n21)
(predicate_eqb n12 n22)
| (And n11 n12, And n21 n22) =>
andb (predicate_eqb n11 n21)
(predicate_eqb n12 n22)
| _ => false
end.
Lemma predicate_eqb_eq1 : forall p1 p2,
p1 = p2 -> predicate_eqb p1 p2 = true.
Proof.
intros p1.
induction p1; intros;
destruct p2; try discriminate;
auto;
try (injection H;
intros;
simpl;
try rewrite IHp1_1;
try rewrite IHp1_2;
auto);
try (simpl;
destruct (n =? n0) eqn:Heq;
try (apply Nat.eqb_neq in Heq);
auto
).
Qed.
Lemma andb_prop_intro : forall b1 b2,
andb b1 b2 = true ->
b1 = true /\ b2 = true.
Proof.
intros.
destruct b1;
destruct b2;
try discriminate.
auto.
Qed.
Lemma predicate_eqb_eq2 : forall p1 p2,
predicate_eqb p1 p2 = true -> p1 = p2.
Proof.
intros p1.
induction p1; intros;
destruct p2; try discriminate;
auto;
try (
simpl in H;
apply andb_prop_intro in H;
destruct H;
apply IHp1_1 in H;
apply IHp1_2 in H0;
subst;
auto
);
try (
simpl in H;
apply Nat.eqb_eq in H;
auto
).
Qed.
Lemma predicate_eqb_eq : forall p1 p2,
p1 = p2 <-> predicate_eqb p1 p2 = true.
Proof.
intros.
split.
- apply predicate_eqb_eq1.
- apply predicate_eqb_eq2.
Qed.
Lemma predicate_eq_choice : forall (p1 p2 : predicate),
(p1 = p2) \/ (p1 <> p2).
Proof.
intros.
destruct (predicate_eqb p1 p2) eqn:Heq.
- apply predicate_eqb_eq in Heq.
left.
auto.
- right.
unfold not.
intros.
apply predicate_eqb_eq in H.
rewrite H in Heq.
discriminate.
Qed.
Lemma canonical_forms_bool : forall (t : tuple) o p,
length t = o ->
well_typed o p Boolean ->
predicate_value p ->
(p = Tru) \/ (p = Fls).
Proof.
intros.
inversion H1.
- inversion H0;
subst; discriminate.
- left.
reflexivity.
- right.
reflexivity.
Qed.
Lemma canonical_forms_number : forall (t : tuple) o p,
length t = o ->
well_typed o p Number ->
predicate_value p ->
exists n, p = Literal n.
Proof.
intros.
inversion H1;
try (
inversion H0; subst; discriminate
).
exists n.
auto.
Qed.
Lemma predicate_progress : forall (typ : type) (t : tuple)
(o : nat) (p : predicate),
length t = o ->
well_typed o p typ ->
predicate_value p \/ exists p', predicate_steps p t p'.
Proof.
intros.
induction H0;
try (
destruct IHwell_typed1;
destruct IHwell_typed2;
auto;
right
).
- left.
apply LitVal.
- right.
destruct (extract_field t i) eqn:Hextract.
+ exists (Literal n).
apply FieldStep.
apply Hextract.
+ exfalso.
unfold extract_field in Hextract.
assert (nth_error t i <> None).
{
apply nth_error_Some.
rewrite <- H in H0.
apply H0.
}.
unfold not in H1.
apply H1.
rewrite Hextract.
reflexivity.
- left.
apply TruVal.
- left.
apply FlsVal.
- assert (HA := H).
apply canonical_forms_number with (p := p1) in H;
auto.
apply canonical_forms_number with (p := p2) in HA;
auto.
destruct H as [ n1 ].
destruct HA as [ n2 ].
subst.
exists (Literal (n1 + n2)).
apply Plus__value; auto.
- destruct H1 as [ p2' ].
exists (Plus p1 p2').
apply Plus2; auto.
- destruct H0 as [ p1' ].
exists (Plus p1' p2).
apply Plus1; auto.
- destruct H0 as [ p1' ].
exists (Plus p1' p2).
apply Plus1; auto.
- assert (HA := H).
apply canonical_forms_number with (p := p1) in H;
auto.
apply canonical_forms_number with (p := p2) in HA;
auto.
destruct H as [ n1 ].
destruct HA as [ n2 ].
destruct (n1 =? n2) eqn:Heq.
+ apply Nat.eqb_eq in Heq.
subst.
exists (Tru).
apply Equalsvalue__true; auto.
+ apply Nat.eqb_neq in Heq.
exists (Fls).
apply Equalsvalue__false; auto.
rewrite H.
rewrite H2.
injection.
intros.
rewrite H4 in Heq.
unfold not in Heq.
apply Heq.
reflexivity.
- destruct H1 as [ p2' ].
exists (Equals p1 p2').
apply Equals2; auto.
- destruct H0 as [ p1' ].
exists (Equals p1' p2).
apply Equals1; auto.
- destruct H0 as [ p1' ].
exists (Equals p1' p2).
apply Equals1; auto.
- assert (HA := H).
apply canonical_forms_number with (p := p1) in H;
auto.
apply canonical_forms_number with (p := p2) in HA;
auto.
destruct H as [ n1 ].
destruct HA as [ n2 ].
destruct (n1 <? n2) eqn:Hlt.
+ apply Nat.ltb_lt in Hlt.
exists (Tru).
apply LessThanvalue__true with (n1 := n1) (n2 := n2);
auto.
+ apply Nat.ltb_ge in Hlt.
exists Fls.
apply LessThanvalue__false with (n1 := n1) (n2 := n2);
auto.
- destruct H1 as [ p2' ].
exists (LessThan p1 p2').
apply LessThan2; auto.
- destruct H0 as [ p1' ].
exists (LessThan p1' p2).
apply LessThan1; auto.
- destruct H0 as [ p1' ].
exists (LessThan p1' p2).
apply LessThan1; auto.
- assert (HA := H).
apply canonical_forms_bool with (p := p1) in H;
auto.
apply canonical_forms_bool with (p := p2) in HA;
auto.
destruct H; destruct HA;
try (exists Fls; apply Andvalue__false; auto; fail).
exists Tru.
apply Andvalue__true; auto.
- destruct H1 as [ p2' ].
exists (And p1 p2').
apply And2; auto.
- destruct H0 as [ p1' ].
exists (And p1' p2).
apply And1; auto.
- destruct H0 as [ p1' ].
exists (And p1' p2).
apply And1; auto.
- assert (HA := H).
apply canonical_forms_bool with (p := p1) in H;
auto.
apply canonical_forms_bool with (p := p2) in HA;
auto.
destruct H; destruct HA;
try (exists Tru; apply Orvalue__true; auto; fail).
exists Fls.
apply Orvalue__false; auto.
- destruct H1 as [ p2' ].
exists (Or p1 p2').
apply Or2; auto.
- destruct H0 as [ p1' ].
exists (Or p1' p2).
apply Or1; auto.
- destruct H0 as [ p1' ].
exists (Or p1' p2).
apply Or1; auto.
Qed.
Lemma predicate_preservation : forall p p' (t : tuple),
predicate_steps p t p' ->
forall typ,
well_typed (length t) p typ ->
well_typed (length t) p' typ.
Proof.
intros p p' t H.
induction H; intros.
- inversion H0. apply WT_Lit.
- inversion H3. apply WT_True.
- inversion H2. apply WT_False.
- inversion H0. apply WT_And; auto.
- inversion H1. apply WT_And; auto.
- inversion H2. apply WT_True.
- inversion H3. apply WT_False.
- inversion H0. apply WT_Or; auto.
- inversion H1. apply WT_Or; auto.
- inversion H2. apply WT_True.
- inversion H2. apply WT_False.
- inversion H0. apply WT_Equals; auto.
- inversion H1. apply WT_Equals; auto.
- inversion H4. apply WT_True.
- inversion H4. apply WT_False.
- inversion H0. apply WT_LessThan; auto.
- inversion H1. apply WT_LessThan; auto.
- inversion H3. apply WT_Lit.
- inversion H0. apply WT_Plus; auto.
- inversion H1. apply WT_Plus; auto.
Qed.
Lemma bigstep_preservation : forall (p p' : predicate)
(t : tuple)