aboutsummaryrefslogtreecommitdiffstats
path: root/src/Predicate.v
blob: 1ff98312f68d0c10319b9825025dcdc49f93031f (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
Require Import Coq.Structures.Equalities.

Require Import TVSMT.Coqlib.
Require Import TVSMT.Maps.
Require Import TVSMT.Errors.
Require Import TVSMT.Sat.
Require Import TVSMT.Common.
Require Import TVSMT.Hashtree.

Local Open Scope error_monad_scope.
Local Open Scope positive.

(** The [predicate] type defines the path predicates that are used in the [Ieta] and [Igamma]
    instructions. *)
Inductive pred {A}: Type :=
| Ptrue : pred
| Pfalse : pred
| Pundef : pred
| Pbase : A -> pred
| Pand : pred -> pred -> pred
| Por : pred -> pred -> pred
| Pimp : pred -> pred -> pred
| Pnot : pred -> pred.

Module PredicateNotation.

  Declare Scope predicate.

  Notation "A ∧ B" := (Pand A B) (at level 20, left associativity) : predicate.
  Notation "A ∨ B" := (Por A B) (at level 25, left associativity) : predicate.
  Notation "A → B" := (Pimp A B) (at level 30, right associativity) : predicate.
  Notation "¬ A" := (Pnot A) (at level 15) : predicate.
  Notation "⟂" := (Pfalse) : predicate.
  Notation "'T'" := (Ptrue) : predicate.
  Notation "○" := (Pundef) : predicate.

End PredicateNotation.

Import PredicateNotation.
Local Open Scope predicate.
Local Open Scope bool.
Local Open Scope positive.

Module ThreeValued(Base: UsualDecidableTypeBoth).

Definition A := Base.t.
Definition el_dec: forall a b: A, {a = b} + {a <> b} := Base.eq_dec.

Module Import AH := HashTree(Base).
Module Import AH_Prop := HashTreeProperties(Base).

Section EVAL.

Context (eval: A -> option bool).

Definition predicate := @pred A.

(* This evaluation follows Łukasiewicz logic. *)
Fixpoint eval_predicate (p: predicate) : option bool :=
  match p with
  | T => Some true
  | ⟂ => Some false
  | ○ => None
  | ¬ p =>
      match eval_predicate p with
      | None => None
      | Some b => Some (negb b)
      end
  | Pbase c =>
    match eval c with
    | Some p => Some p
    | None => None
    end
  | p1 ∧ p2 =>
    match eval_predicate p1, eval_predicate p2 with
    | Some p1', Some p2' => Some (p1' && p2')
    | Some false, None => Some false
    | None, Some false => Some false
    | _, _ => None
    end
  | p1 ∨ p2 =>
    match eval_predicate p1, eval_predicate p2 with
    | Some p1', Some p2' => Some (p1' || p2')
    | Some true, None => Some true
    | None, Some true => Some true
    | _, _ => None
    end
  | p1 → p2 =>
      match eval_predicate p1, eval_predicate p2 with
      | Some p1', Some p2' => Some (negb p1' || p2')
      | None, Some false => None
      | Some true, None => None
      | _, _ => Some true
      end
  end.

Definition equiv {A} (a b: @pred A) :=
  Pand (Pimp a b) (Pimp b a).

Definition hash_pred := @pred positive.

Definition hash_tree := PTree.t A.

Lemma comp_list_dec :
  forall (a b: list positive),
    {a = b} + {a <> b}.
Proof. generalize Pos.eq_dec. decide equality. Defined.

Lemma hash_pred_dec :
  forall (a b: hash_pred),
    {a = b} + {a <> b}.
Proof. generalize Pos.eq_dec. generalize bool_dec. repeat decide equality. Defined.

Lemma predicate_dec :
  forall (a b: predicate),
    {a = b} + {a <> b}.
Proof. generalize el_dec. generalize bool_dec. repeat decide equality. Defined.

Definition find_tree (el: A) (h: hash_tree) : option positive :=
  match
    filter (fun x => match x with (a, b) => if el_dec el b then true else false end)
           (PTree.elements h) with
  | (p, _) :: nil => Some p
  | _ => None
  end.

Definition combine_option {A} (a b: option A) : option A :=
  match a, b with
  | Some a', _ => Some a'
  | _, Some b' => Some b'
  | _, _ => None
  end.

Definition max_key {A} (t: PTree.t A) :=
  fold_right Pos.max 1 (map fst (PTree.elements t)).

Fixpoint hash_predicate (p: predicate) (h: PTree.t A)
  : hash_pred * PTree.t A :=
  match p with
  | T => (T, h)
  | ⟂ => (, h)
  | ○ => (, h)
  | ¬ p => let (p', t) := hash_predicate p h in (¬ p', t)
  | Pbase c =>
      match find_tree c h with
      | Some p => (Pbase p, h)
      | None =>
          let nkey := max_key h + 1 in
          (Pbase nkey, PTree.set nkey c h)
      end
  | p1 ∧ p2 =>
      let (p1', t1) := hash_predicate p1 h in
      let (p2', t2) := hash_predicate p2 t1 in
      (p1' ∧ p2', t2)
  | p1 ∨ p2 =>
      let (p1', t1) := hash_predicate p1 h in
      let (p2', t2) := hash_predicate p2 t1 in
      (p1' ∨ p2', t2)
  | p1 → p2 =>
      let (p1', t1) := hash_predicate p1 h in
      let (p2', t2) := hash_predicate p2 t1 in
      (p1' → p2', t2)
  end.

Fixpoint eval_hash_pred (p: hash_pred) (a: PTree.t bool) : option bool :=
  match p with
  | T => Some true
  | ⟂ => Some false
  | ○ => None
  | ¬ p =>
      match eval_hash_pred p a with
      | Some b => Some (negb b)
      | None => None
      end
  | Pbase k => a ! k
  | p1 ∧ p2 =>
    match eval_hash_pred p1 a, eval_hash_pred p2 a with
    | Some p1', Some p2' => Some (p1' && p2')
    | Some false, None => Some false
    | None, Some false => Some false
    | _, _ => None
    end
  | p1 ∨ p2 =>
    match eval_hash_pred p1 a, eval_hash_pred p2 a with
    | Some p1', Some p2' => Some (p1' || p2')
    | Some true, None => Some true
    | None, Some true => Some true
    | _, _ => None
    end
  | p1 → p2 =>
      match eval_hash_pred p1 a, eval_hash_pred p2 a with
      | Some p1', Some p2' => Some (negb p1' || p2')
      | None, Some false => None
      | Some true, None => None
      | _, _ => Some true
      end
  end.

(** The [predicate] type defines the path predicates that are used in the [Ieta] and [Igamma]
    instructions. *)
Inductive pred_bin {A}: Type :=
| PBtrue : pred_bin
| PBfalse : pred_bin
| PBbase : (bool * A) -> pred_bin
| PBand : pred_bin -> pred_bin -> pred_bin
| PBor : pred_bin -> pred_bin -> pred_bin.

Definition hash_pred_bin : Type := @pred_bin positive * @pred_bin positive.

Fixpoint eval_hash_pred_bin1 (p1: @pred_bin positive) (m: PMap.t bool) : bool :=
  match p1 with
  | PBtrue => true
  | PBfalse => false
  | PBbase (b, p) => if b then m!!p else negb m!!p
  | PBand p1 p2 => eval_hash_pred_bin1 p1 m && eval_hash_pred_bin1 p2 m
  | PBor p1 p2 => eval_hash_pred_bin1 p1 m || eval_hash_pred_bin1 p2 m
  end.

Definition eval_hash_pred_bin (p1: hash_pred_bin) (m: PMap.t bool) : (bool * bool) :=
  (eval_hash_pred_bin1 (fst p1) m, eval_hash_pred_bin1 (snd p1) m).

Fixpoint negate (h: @pred_bin positive) : @pred_bin positive :=
  match h with
  | PBtrue => PBfalse
  | PBfalse => PBtrue
  | PBbase (b, p) => PBbase (negb b, p)
  | PBand p1 p2 => PBor (negate p1) (negate p2)
  | PBor p1 p2 => PBand (negate p1) (negate p2)
  end.

Fixpoint conv_hash_pred (p: hash_pred) (h: PTree.t positive) (fresh: positive):
  (hash_pred_bin * PTree.t positive * positive) :=
  match p with
  | Ptrue => ((PBfalse, PBtrue), h, fresh)
  | Pfalse => ((PBfalse, PBfalse), h, fresh)
  | Pundef => ((PBtrue, PBfalse), h, fresh)
  | Pnot p => let '((p1, p2), h', fresh') := conv_hash_pred p h fresh in
              ((p1, negate p2), h', fresh')
  | Pand p1 p2 =>
      let '((p1s, p1'), h', fresh') := conv_hash_pred p1 h fresh in
      let '((p2s, p2'), h'', fresh'') := conv_hash_pred p2 h' fresh' in
      ((PBor (PBand p1' p2s) (PBor (PBand p1s p2s) (PBand p1s p2')),
         PBand p1' p2'), h'', fresh'')
  | Por p1 p2 =>
      let '((p1s, p1'), h', fresh') := conv_hash_pred p1 h fresh in
      let '((p2s, p2'), h'', fresh'') := conv_hash_pred p2 h' fresh' in
      ((PBor (PBand (negate p1') p2s) (PBor (PBand (negate p2') p1s) (PBand p1s p2s)),
         PBor p1' p2'), h'', fresh'')
  | Pimp p1 p2 =>
      let '((p1s, p1'), h', fresh') := conv_hash_pred p1 h fresh in
      let '((p2s, p2'), h'', fresh'') := conv_hash_pred p2 h' fresh' in
      ((PBor (PBand (negate p1s) (PBand p1' p2s)) (PBand (negate p2s) (PBand (negate p2') p1s)),
        PBor p1s (PBor (negate p1') p2')), h'', fresh'')
  | Pbase k =>
      ((PBbase (true, fresh), PBbase (true, k)), PTree.set k fresh h, fresh+1)
  end.

Fixpoint mult {A: Type} (a b: list (list A)) : list (list A) :=
  match a with
  | nil => nil
  | l :: ls => mult ls b ++ (List.map (fun x => l ++ x) b)
  end.

Definition simplify' {A} (p: @pred A) :=
  match p with
  | A ∧ T => A
  | T ∧ A => A
  | _ ∧ ⟂ => ⟂
  | ⟂ ∧ _ => ⟂
  | ○ ∧ ○ => ○
  | A ∨ ⟂ => A
  | ⟂ ∨ A => A
  | _ ∨ T => T
  | T ∨ _ => T
  | ○ ∨ ○ => ○
  | T → A => A
  | ⟂ → _ => T
  | _ → T => T
  | ○ → ○ => T
  | ¬ T => ⟂
  | ¬ ⟂ => T
  | ¬ ○ => ○
  | A => A
  end.

Fixpoint simplify {A} (dec: forall a b: @pred A, {a = b} + {a <> b} ) (p: @pred A) :=
  match p with
  | A ∧ B =>
    let A' := simplify dec A in
    let B' := simplify dec B in
    if dec A' B' then A' else simplify' (A' ∧ B')
  | A ∨ B =>
    let A' := simplify dec A in
    let B' := simplify dec B in
    if dec A' B' then A' else simplify' (A' ∨ B')
  | A → B =>
    let A' := simplify dec A in
    let B' := simplify dec B in
    if dec A' B' then A' else simplify' (A' → B')
  | ¬ p => simplify' (¬ (simplify dec p))
  | T => T
  | ⟂ => ⟂
  | ○ => ○
  | Pbase a => Pbase a
  end.

Fixpoint sat_hash_pred_bin1' (p: @pred_bin positive) (a: asgn) : bool :=
  match p with
  | PBtrue => true
  | PBfalse => false
  | PBbase (b, p') => if b then a (Pos.to_nat p') else negb (a (Pos.to_nat p'))
  | PBand p1 p2 => sat_hash_pred_bin1' p1 a && sat_hash_pred_bin1' p2 a
  | PBor p1 p2 => sat_hash_pred_bin1' p1 a || sat_hash_pred_bin1' p2 a
  end.

Fixpoint sat_hash_pred_bin1 (p: @pred_bin positive) (a: PMap.t bool) : bool :=
  match p with
  | PBtrue => true
  | PBfalse => false
  | PBbase (b, p') => if b then a !! p' else negb (a !! p')
  | PBand p1 p2 => sat_hash_pred_bin1 p1 a && sat_hash_pred_bin1 p2 a
  | PBor p1 p2 => sat_hash_pred_bin1 p1 a || sat_hash_pred_bin1 p2 a
  end.

Definition sat_hash_pred_bin (p: hash_pred_bin) (a: PMap.t bool) : bool :=
  negb (sat_hash_pred_bin1 (fst p) a) && sat_hash_pred_bin1 (snd p) a.

Definition conv_tree (p: PTree.t positive) (assigns: PTree.t bool) : PMap.t bool :=
  PTree.fold (fun m i a =>
                match assigns ! i with
                | Some true => PMap.set a false (PMap.set i true m)
                | Some false => PMap.set a false (PMap.set i false m)
                | None => PMap.set a true (PMap.set i false m)
                end) p (PMap.init false).

Definition interp_bin (b: bool * bool): option bool :=
  match b with | (true, _) => None | (_, b) => Some b end.

Fixpoint max_hash_pred (p: hash_pred) :=
  match p with
  | T | ⟂ | ○ => 1
  | Pbase k => k
  | p1 ∧ p2 | p1 ∨ p2 | p1 → p2 => Pos.max (max_hash_pred p1) (max_hash_pred p2)
  | ¬ p' => max_hash_pred p'
  end.

Definition eval_through_bin (p: hash_pred) (a: PTree.t bool) :=
  let '(p', h, f) := conv_hash_pred p (PTree.empty _) (max_hash_pred p + 1) in
  let m := conv_tree h a in
  interp_bin (sat_hash_pred_bin1 (fst p') m, sat_hash_pred_bin1 (snd p') m).

(*Compute eval_hash_pred (Pand (Por (Pbase 1) (Pbase 2)) (Pbase 3))
        (PTree.set 1 true (PTree.set 3 true (PTree.empty _))).
Compute eval_through_bin (Pand (Por (Pbase 1) (Pbase 2)) (Pbase 3))
        (PTree.set 1 true (PTree.set 3 true (PTree.empty _))).*)

Lemma satFormula_concat:
  forall a b agn,
  satFormula a agn ->
  satFormula b agn ->
  satFormula (a ++ b) agn.
Proof. induction a; crush. Qed.

Lemma satFormula_concat2:
  forall a b agn,
  satFormula (a ++ b) agn ->
  satFormula a agn /\ satFormula b agn.
Proof.
  induction a; simplify;
    try apply IHa in H1; crush.
Qed.

Lemma satClause_concat:
  forall a a1 a0,
  satClause a a1 ->
  satClause (a0 ++ a) a1.
Proof. induction a0; crush. Qed.

Lemma satClause_concat2:
  forall a a1 a0,
  satClause a0 a1 ->
  satClause (a0 ++ a) a1.
Proof.
  induction a0; crush.
  inv H; crush.
Qed.

Lemma satClause_concat3:
  forall a b c,
  satClause (a ++ b) c ->
  satClause a c \/ satClause b c.
Proof.
  induction a; crush.
  inv H; crush.
  apply IHa in H0; crush.
  inv H0; crush.
Qed.

Lemma satFormula_mult':
  forall p2 a a0,
  satFormula p2 a0 \/ satClause a a0 ->
  satFormula (map (fun x : list lit => a ++ x) p2) a0.
Proof.
  induction p2; crush.
  - inv H. inv H0. apply satClause_concat. auto.
    apply satClause_concat2; auto.
  - apply IHp2.
    inv H; crush; inv H0; crush.
Qed.

Lemma satFormula_mult2':
  forall p2 a a0,
  satFormula (map (fun x : list lit => a ++ x) p2) a0 ->
  satClause a a0 \/ satFormula p2 a0.
Proof.
  induction p2; crush.
  apply IHp2 in H1. inv H1; crush.
  apply satClause_concat3 in H0.
  inv H0; crush.
Qed.

Lemma satFormula_mult:
  forall p1 p2 a,
  satFormula p1 a \/ satFormula p2 a ->
  satFormula (mult p1 p2) a.
Proof.
  induction p1; crush.
  apply satFormula_concat; crush.
  inv H. inv H0.
  apply IHp1. auto.
  apply IHp1. auto.
  apply satFormula_mult';
  inv H; crush.
Qed.

Lemma satFormula_mult2:
  forall p1 p2 a,
  satFormula (mult p1 p2) a ->
  satFormula p1 a \/ satFormula p2 a.
Proof.
  induction p1; crush.
  apply satFormula_concat2 in H; crush.
  apply IHp1 in H0.
  inv H0; crush.
  apply satFormula_mult2' in H1. inv H1; crush.
Qed.

Fixpoint trans_pred (p: @pred_bin positive) :
  {fm: formula | forall a, sat_hash_pred_bin1' p a = true <-> satFormula fm a}.
  refine
    (match p with
     | PBtrue => exist _ nil _
     | PBfalse => exist _ (nil :: nil) _
     | PBbase (b, p') => exist _ (((b, Pos.to_nat p') :: nil) :: nil) _
     | PBand p1 p2 =>
         match trans_pred p1, trans_pred p2 with
         | exist p1' _, exist p2' _ => exist _ (p1' ++ p2') _
         end
     | PBor p1 p2 =>
         match trans_pred p1, trans_pred p2 with
         | exist p1' _, exist p2' _ => exist _ (mult p1' p2') _
         end
     end); split; crush.
  - destruct_match; subst. tauto. simplify. tauto.
  - inv H0; crush. destruct_match; crush. subst. apply negb_true_iff. crush.
  - apply satFormula_concat. apply i; auto. apply i0; auto.
  - apply satFormula_concat2 in H. simplify. apply andb_true_intro.
    split. apply i in H0. auto.
    apply i0 in H1. auto.
  - apply orb_prop in H. inv H; apply satFormula_mult. apply i in H0. auto.
    apply i0 in H0. auto.
  - apply orb_true_intro.
    apply satFormula_mult2 in H. inv H. apply i in H0. auto.
    apply i0 in H0. auto.
Qed.

Definition sat_pred (bound: nat) (p: @pred_bin positive) :
  option ({al : alist | sat_hash_pred_bin1' p (interp_alist al) = true}
          + {forall a : asgn, sat_hash_pred_bin1' p a = false}).
  refine
    ( match trans_pred p with
      | exist fm _ =>
          match boundedSat bound fm with
          | Some (inleft (exist a _)) => Some (inleft (exist _ a _))
          | Some (inright _) => Some (inright _)
          | None => None
          end
      end ).
  - apply i in s1. auto.
  - intros. specialize (n a). specialize (i a).
    destruct (sat_hash_pred_bin1' p a). exfalso.
    apply n. apply i. auto. auto.
Qed.

Lemma negate_correct :
  forall h a, sat_hash_pred_bin1' (negate h) a = negb (sat_hash_pred_bin1' h a).
Proof.
  induction h; crush.
  - repeat destruct_match; subst; crush; symmetry; apply negb_involutive.
  - rewrite negb_andb; crush.
  - rewrite negb_orb; crush.
Qed.

Lemma sat_implication :
  forall h h' a,
    sat_hash_pred_bin1' (PBand (negate h) h') a = false ->
    sat_hash_pred_bin1' h' a = true ->
    sat_hash_pred_bin1' h a = true.
Proof.
  intros. simplify. rewrite negate_correct in H.
  rewrite H0 in H. rewrite andb_true_r in H. crush.
Qed.

Fixpoint trans_pred_simple (p: @pred_bin positive) :=
  match p with
  | PBtrue => nil
  | PBfalse => nil :: nil
  | PBbase (b, p') => ((b, Pos.to_nat p') :: nil) :: nil
  | PBand p1 p2 =>
      match trans_pred_simple p1, trans_pred_simple p2 with
      | p1', p2' => (p1' ++ p2')
      end
  | PBor p1 p2 =>
      match trans_pred_simple p1, trans_pred_simple p2 with
      | p1', p2' => (mult p1' p2')
      end
  end.

(*Compute trans_pred_simple (PBand (PBbase (false, 1)) (PBbase (true, 3))).*)

Definition sat_pred_simple (bound: nat) (p: @pred_bin positive) :=
  boundedSatSimple bound (trans_pred_simple p).

Definition sat_hash_pred_simple (bound: nat) (p: hash_pred_bin) :=
  match sat_pred_simple bound (PBor (fst p) (negate (snd p))) with
  | Some None => Some None
  | Some (Some a) => Some (Some a)
  | None => None
  end.

Definition sat_hpred_simple bound (p: hash_pred) :=
  let '(p', h, f) := conv_hash_pred p (PTree.empty _) (max_hash_pred p + 1) in
  sat_hash_pred_simple bound p'.

Lemma max_key_correct' :
  forall l hi, In hi l -> hi <= fold_right Pos.max 1 l.
Proof.
  induction l; crush.
  inv H. lia.
  destruct (Pos.max_dec a (fold_right Pos.max 1 l)); rewrite e.
  - apply Pos.max_l_iff in e.
    assert (forall a b c, a <= c -> c <= b -> a <= b) by lia.
    eapply H; eauto.
  - apply IHl; auto.
Qed.

Definition bar (p1: lit): lit := (negb (fst p1), (snd p1)).

Definition stseytin_or (cur p1 p2: lit) : formula :=
  (bar cur :: p1 :: p2 :: nil)
    :: (cur :: bar p1 :: nil)
    :: (cur :: bar p2 :: nil) :: nil.

Definition stseytin_and (cur p1 p2: lit) : formula :=
  (cur :: bar p1 :: bar p2 :: nil)
    :: (bar cur :: p1 :: nil)
    :: (bar cur :: p2 :: nil) :: nil.

Fixpoint xtseytin (next: nat) (p: @pred_bin positive) {struct p} : (nat * lit * formula) :=
  match p with
  | PBbase (b, p') => (next, (b, Pos.to_nat p'), nil)
  | PBtrue =>
      ((next+1)%nat, (true, next), ((true, next)::nil)::nil)
  | PBfalse =>
      ((next+1)%nat, (true, next), ((false, next)::nil)::nil)
  | PBor p1 p2 =>
      let '(m1, n1, f1) := xtseytin next p1 in
      let '(m2, n2, f2) := xtseytin m1 p2 in
      ((m2+1)%nat, (true, m2), stseytin_or (true, m2) n1 n2 ++ f1 ++ f2)
  | PBand p1 p2 =>
      let '(m1, n1, f1) := xtseytin next p1 in
      let '(m2, n2, f2) := xtseytin m1 p2 in
      ((m2+1)%nat, (true, m2), stseytin_and (true, m2) n1 n2 ++ f1 ++ f2)
  end.

Lemma stseytin_and_correct :
  forall cur p1 p2 fm c,
    stseytin_and cur p1 p2 = fm ->
    satLit cur c ->
    satLit p1 c /\ satLit p2 c ->
    satFormula fm c.
Proof.
  intros.
  unfold stseytin_and in *. rewrite <- H.
  unfold satLit in *. destruct p1. destruct p2. destruct cur.
  simpl in *|-. cbn. unfold satLit. cbn. crush.
Qed.

Lemma stseytin_or_correct :
  forall cur p1 p2 fm c,
    stseytin_or cur p1 p2 = fm ->
    satLit cur c ->
    satLit p1 c \/ satLit p2 c ->
    satFormula fm c.
Proof.
  intros.
  unfold stseytin_or in *. rewrite <- H. inv H1.
  unfold satLit in *. destruct p1. destruct p2. destruct cur.
  simpl in *|-. cbn. unfold satLit. cbn. crush.
  unfold satLit in *. destruct p1. destruct p2. destruct cur.
  simpl in *|-. cbn. unfold satLit. cbn. crush.
Qed.

Fixpoint max_predicate (p: @pred_bin positive) : positive :=
  match p with
  | PBbase (b, p) => p
  | PBtrue => 1
  | PBfalse => 1
  | PBand a b => Pos.max (max_predicate a) (max_predicate b)
  | PBor a b => Pos.max (max_predicate a) (max_predicate b)
  end.

Definition tseytin_simple (p: @pred_bin positive) : formula :=
    let m := Pos.to_nat (max_predicate p + 1) in
    let '(m, n, fm) := xtseytin m p in
    (n::nil) :: fm.

Definition sat_pred_simple_tseytin (p: @pred_bin positive) : option (option alist) :=
  match boundedSatSimple 10000%nat (tseytin_simple p) with
  | Some (Some a) => Some (Some a)
  | Some None => Some None
  | None => None
  end.

Definition sat_hash_pred_simple_tseytin (p: hash_pred_bin) :=
  match sat_pred_simple_tseytin (PBor (fst p) (negate (snd p))) with
  | Some None => Some None
  | Some (Some a) => Some (Some a)
  | None => None
  end.

Definition sat_hpred_simple_tseytin (p: hash_pred) :=
  let '(p', h, f) := conv_hash_pred p (PTree.empty _) (max_hash_pred p + 1) in
  sat_hash_pred_simple_tseytin p'.

(*Compute sat_hpred_simple_tseytin ((Pbase 1 ∨ Pbase 2)(Pbase 1 ∨ Pbase 2)).
Compute sat_hpred_simple 1000%nat (simplify ((Pbase 1 ∨ Pbase 2)(Pbase 1 ∨ Pbase 2))).*)

Lemma max_key_correct :
  forall A h_tree hi (c: A),
    h_tree ! hi = Some c ->
    hi <= max_key h_tree.
Proof.
  unfold max_key. intros. apply PTree.elements_correct in H.
  apply max_key_correct'.
  eapply in_map with (f := fst) in H. auto.
Qed.

Lemma hash_constant :
  forall p h h_tree hi c h_tree',
    h_tree ! hi = Some c ->
    hash_predicate p h_tree = (h, h_tree') ->
    h_tree' ! hi = Some c.
Proof.
  induction p; crush.
  - repeat (destruct_match; crush); subst.
    pose proof H as X. apply max_key_correct in X.
    rewrite PTree.gso by lia; auto.
  - repeat (destruct_match; crush). exploit IHp1; eauto.
  - repeat (destruct_match; crush). exploit IHp1; eauto.
  - repeat (destruct_match; crush). exploit IHp1; eauto.
  - repeat (destruct_match; crush). exploit IHp; eauto.
Qed.

Lemma find_tree_correct :
  forall c h_tree p,
    find_tree c h_tree = Some p ->
    h_tree ! p = Some c.
Proof.
  intros.
  unfold find_tree in H. destruct_match; crush.
  destruct_match; simplify.
  destruct_match; crush.
  assert (In (p, a) (filter
           (fun x : positive * A =>
            let (_, b) := x in if el_dec c b then true else false) (PTree.elements h_tree))).
  { rewrite Heql. constructor. auto. }
  apply filter_In in H. simplify. destruct_match; crush. subst.
  apply PTree.elements_complete; auto.
Qed.

Lemma find_tree_unique :
  forall c h_tree p p',
    find_tree c h_tree = Some p ->
    h_tree ! p' = Some c ->
    p = p'.
Proof.
  intros.
  unfold find_tree in H.
  repeat (destruct_match; crush; []).
  assert (In (p, a) (filter
           (fun x : positive * A =>
            let (_, b) := x in if el_dec c b then true else false) (PTree.elements h_tree))).
  { rewrite Heql. constructor. auto. }
  apply filter_In in H. simplify.
  destruct (Pos.eq_dec p p'); auto.
  exfalso.
  destruct_match; subst; crush.
  assert (In (p', a) (PTree.elements h_tree) /\ (fun x : positive * A =>
            let (_, b) := x in if el_dec a b then true else false) (p', a) = true).
  { split. apply PTree.elements_correct. auto. rewrite Heqs. auto. }
  apply filter_In in H. rewrite Heql in H. inv H. simplify. crush. crush.
Qed.

Lemma hash_predicate_hash_le :
  forall p n p' n',
    hash_predicate p n = (p', n') ->
    (PTree_Properties.cardinal n <= PTree_Properties.cardinal n')%nat.
Proof.
  induction p; crush.
  - repeat (destruct_match; crush).
    assert (n ! (max_key n + 1) = None).
    { destruct (n ! (max_key n + 1)) eqn:?; auto.
      apply max_key_correct in Heqo0. lia. }
    exploit PTree_Properties.cardinal_set; eauto.
    instantiate (1 := a); intros. lia.
  - repeat (destruct_match; crush).
    apply IHp1 in Heqp. apply IHp2 in Heqp0. lia.
  - repeat (destruct_match; crush).
    apply IHp1 in Heqp. apply IHp2 in Heqp0. lia.
  - repeat (destruct_match; crush).
    apply IHp1 in Heqp. apply IHp2 in Heqp0. lia.
  - repeat (destruct_match; crush).
    apply IHp in Heqp0. lia.
Qed.

Lemma hash_predicate_hash_len :
  forall p n p' n',
    hash_predicate p n = (p', n') ->
    PTree_Properties.cardinal n = PTree_Properties.cardinal n' ->
    n = n'.
Proof.
  induction p; crush.
  - repeat (destruct_match; crush).
    assert (n ! (max_key n + 1) = None).
    { destruct (n ! (max_key n + 1)) eqn:?; auto.
      apply max_key_correct in Heqo0. lia. }
    exploit PTree_Properties.cardinal_set; eauto.
    instantiate (1 := a); intros. lia.
  - repeat (destruct_match; crush).
    pose proof Heqp as X; apply hash_predicate_hash_le in X.
    pose proof Heqp0 as X0; apply hash_predicate_hash_le in X0.
    exploit IHp1; eauto. lia. intros; subst.
    exploit IHp2; eauto.
  - repeat (destruct_match; crush).
    pose proof Heqp as X; apply hash_predicate_hash_le in X.
    pose proof Heqp0 as X0; apply hash_predicate_hash_le in X0.
    exploit IHp1; eauto. lia. intros; subst.
    exploit IHp2; eauto.
  - repeat (destruct_match; crush).
    pose proof Heqp as X; apply hash_predicate_hash_le in X.
    pose proof Heqp0 as X0; apply hash_predicate_hash_le in X0.
    exploit IHp1; eauto. lia. intros; subst.
    exploit IHp2; eauto.
  - repeat (destruct_match; crush).
    pose proof Heqp0 as X; apply hash_predicate_hash_le in X.
    exploit IHp; eauto.
Qed.

Inductive pred_In {A} : A -> @pred A -> Prop :=
| pred_In_Pbase : forall a, pred_In a (Pbase a)
| pred_In_Pand1 : forall c p1 p2, pred_In c p1 -> pred_In c (p1 ∧ p2)
| pred_In_Pand2 : forall c p1 p2, pred_In c p2 -> pred_In c (p1 ∧ p2)
| pred_In_Por1 : forall c p1 p2, pred_In c p1 -> pred_In c (p1 ∨ p2)
| pred_In_Por2 : forall c p1 p2, pred_In c p2 -> pred_In c (p1 ∨ p2)
| pred_In_Pimp1 : forall c p1 p2, pred_In c p1 -> pred_In c (p1 → p2)
| pred_In_Pimp2 : forall c p1 p2, pred_In c p2 -> pred_In c (p1 → p2)
| pred_In_Pnot : forall c p, pred_In c p -> pred_In c (¬ p).

Lemma pred_In_dec :
  forall A (c: A) p,
    (forall a b : A, { a = b } + { a <> b }) ->
    { pred_In c p } + { ~ pred_In c p }.
Proof.
  induction p; crush.
  - right. unfold not. intros. inv H.
  - right. unfold not. intros. inv H.
  - right. unfold not. intros. inv H.
  - destruct (X c a); subst.
    { left. constructor. }
    { right. unfold not. intros. inv H. auto. }
  - pose proof X as X1. pose proof X as X2.
    apply IHp1 in X1. apply IHp2 in X2.
    inv X1; inv X2. left. constructor. tauto.
    left. constructor. auto.
    left. apply pred_In_Pand2. auto.
    right. unfold not. intros. inv H1; auto.
  - pose proof X as X1. pose proof X as X2.
    apply IHp1 in X1. apply IHp2 in X2.
    inv X1; inv X2. left. constructor. tauto.
    left. constructor. auto.
    left. apply pred_In_Por2. auto.
    right. unfold not. intros. inv H1; auto.
  - pose proof X as X1. pose proof X as X2.
    apply IHp1 in X1. apply IHp2 in X2.
    inv X1; inv X2. left. constructor. tauto.
    left. constructor. auto.
    left. apply pred_In_Pimp2. auto.
    right. unfold not. intros. inv H1; auto.
  - pose proof X as X1. apply IHp in X1.
    inv X1. left. constructor. tauto.
    right. unfold not. intros. inv H0; auto.
Qed.

Lemma pred_In_hash :
  forall p n p' n' c,
    hash_predicate p n = (p', n') ->
    pred_In c p ->
    exists t, n' ! t = Some c /\ pred_In t p'.
Proof.
  induction p; simplify.
  - inv H0.
  - inv H0.
  - inv H0.
  - repeat (destruct_match; crush; []); subst.
    destruct_match; simplify.
    { inv H0. apply find_tree_correct in Heqo.
      econstructor; ecrush core. constructor. }
    { inv H0. econstructor. rewrite PTree.gss. crush. constructor. }
  - repeat (destruct_match; crush; []). inv H0.
    exploit IHp1; eauto. simplify. econstructor. simplify. eapply hash_constant; eauto.
    constructor; auto.
    exploit IHp2; eauto. simplify. econstructor; ecrush core. apply pred_In_Pand2. auto.
  - repeat (destruct_match; crush; []). inv H0.
    exploit IHp1; eauto. simplify. econstructor. simplify. eapply hash_constant; eauto.
    constructor; auto.
    exploit IHp2; eauto. simplify. econstructor; ecrush core. apply pred_In_Por2. auto.
  - repeat (destruct_match; crush; []). inv H0.
    exploit IHp1; eauto. simplify. econstructor. simplify. eapply hash_constant; eauto.
    constructor; auto.
    exploit IHp2; eauto. simplify. econstructor; ecrush core. apply pred_In_Pimp2. auto.
  - repeat (destruct_match; crush; []). inv H0. exploit IHp; eauto; simplify.
    econstructor; simplify; eauto. econstructor; auto.
Qed.

Lemma hash_pred_In' :
  forall p h t c n n',
    hash_predicate p n = (h, n') ->
    n ! t = None ->
    n' ! t = Some c ->
    pred_In c p.
Proof.
  induction p; crush.
  - repeat (destruct_match); subst; crush.
    destruct (Pos.eq_dec t (max_key n + 1)); subst.
    { rewrite PTree.gss in H1. simplify. constructor. }
    { rewrite PTree.gso in H1; crush. }
  - repeat (destruct_match); subst; crush.
    destruct (t0 ! t) eqn:?.
    { pose proof Heqo as X. eapply hash_constant in X; eauto. simplify.
      constructor. eapply IHp1; eauto. }
    { apply pred_In_Pand2; eapply IHp2; eauto. }
  - repeat (destruct_match); subst; crush.
    destruct (t0 ! t) eqn:?.
    { pose proof Heqo as X. eapply hash_constant in X; eauto. simplify.
      constructor. eapply IHp1; eauto. }
    { apply pred_In_Por2; eapply IHp2; eauto. }
  - repeat (destruct_match); subst; crush.
    destruct (t0 ! t) eqn:?.
    { pose proof Heqo as X. eapply hash_constant in X; eauto. simplify.
      constructor. eapply IHp1; eauto. }
    { apply pred_In_Pimp2; eapply IHp2; eauto. }
  - repeat (destruct_match); subst; crush.
    exploit IHp; eauto; intros. econstructor; auto.
Qed.

Lemma hash_pred_In :
  forall p h p' h' c n,
    hash_predicate p (PTree.empty _) = (h, n) ->
    hash_predicate p' n = (h', n) ->
    pred_In c p' -> pred_In c p.
Proof.
  intros. exploit pred_In_hash; eauto. simplify.
  eapply hash_pred_In'; eauto. apply PTree.gempty.
Qed.

Lemma pred_In_hash2 :
  forall p h n n' t,
    hash_predicate p n = (h, n') ->
    pred_In t h ->
    exists c, n' ! t = Some c /\ pred_In c p.
Proof.
  induction p; simplify.
  - inv H0.
  - inv H0.
  - inv H0.
  - repeat (destruct_match; crush; []); subst.
    destruct_match; simplify.
    { inv H0. apply find_tree_correct in Heqo.
      econstructor; ecrush core. constructor. }
    { inv H0. econstructor. rewrite PTree.gss. crush. constructor. }
  - repeat (destruct_match; crush; []). inv H0.
    exploit IHp1; eauto. simplify. econstructor. simplify. eapply hash_constant; eauto.
    constructor; auto.
    exploit IHp2; eauto. simplify. econstructor; ecrush core. apply pred_In_Pand2. auto.
  - repeat (destruct_match; crush; []). inv H0.
    exploit IHp1; eauto. simplify. econstructor. simplify. eapply hash_constant; eauto.
    constructor; auto.
    exploit IHp2; eauto. simplify. econstructor; ecrush core. apply pred_In_Por2. auto.
  - repeat (destruct_match; crush; []). inv H0.
    exploit IHp1; eauto. simplify. econstructor. simplify. eapply hash_constant; eauto.
    constructor; auto.
    exploit IHp2; eauto. simplify. econstructor; ecrush core. apply pred_In_Pimp2. auto.
  - repeat (destruct_match; crush; []). inv H0.
    exploit IHp; eauto; simplify; repeat econstructor; ecrush core.
Qed.

Lemma hash_constant2 :
  forall p n h n' v c c',
    hash_predicate p n = (h, n') ->
    n ! v = Some c ->
    n' ! v = Some c' ->
    c = c'.
Proof.
  intros.
  eapply hash_constant in H; eauto. rewrite H1 in H. crush.
Qed.

Lemma pred_not_in_Pand :
  forall A (c: A) p1 p2,
    ~ pred_In c (p1 ∧ p2) ->
    ~ pred_In c p1 /\ ~ pred_In c p2.
Proof.
  intros. split.
  - unfold not in *. intros. apply H. constructor. auto.
  - unfold not in *. intros. apply H. apply pred_In_Pand2. auto.
Qed.

Lemma pred_not_in_Por :
  forall A (c: A) p1 p2,
    ~ pred_In c (p1 ∨ p2) ->
    ~ pred_In c p1 /\ ~ pred_In c p2.
Proof.
  intros. split.
  - unfold not in *. intros. apply H. constructor. auto.
  - unfold not in *. intros. apply H. apply pred_In_Por2. auto.
Qed.

Lemma simplify'_correct :
  forall h a,
    eval_hash_pred (simplify' h) a = eval_hash_pred h a.
Proof. destruct h; crush; repeat (destruct_match; crush). Qed.

Lemma eval_hash_pred_Pand_destr :
  forall p1 p2 p1' p2' a,
    eval_hash_pred p1 a = eval_hash_pred p1' a ->
    eval_hash_pred p2 a = eval_hash_pred p2' a ->
    eval_hash_pred (Pand p1 p2) a = eval_hash_pred (Pand p1' p2') a.
Proof. intros. simpl. rewrite H in *. rewrite H0 in *. auto. Qed.

Lemma eval_hash_pred_Pand_symm :
  forall p1 p2 a,
    eval_hash_pred (Pand p1 p2) a = eval_hash_pred (Pand p2 p1) a.
Proof. simplify. repeat destruct_match; auto. Qed.

Lemma eval_hash_pred_Por_symm :
  forall p1 p2 a,
    eval_hash_pred (Por p1 p2) a = eval_hash_pred (Por p2 p1) a.
Proof. simplify. repeat destruct_match; auto. Qed.

Lemma eval_hash_pred_Por_destr :
  forall p1 p2 p1' p2' a,
    eval_hash_pred p1 a = eval_hash_pred p1' a ->
    eval_hash_pred p2 a = eval_hash_pred p2' a ->
    eval_hash_pred (Por p1 p2) a = eval_hash_pred (Por p1' p2') a.
Proof. intros. simpl. rewrite H in *. rewrite H0 in *. auto. Qed.

Lemma eval_hash_pred_Pand_refl :
  forall p1 a,
    eval_hash_pred (Pand p1 p1) a = eval_hash_pred p1 a.
Proof. intros. simpl. repeat destruct_match; auto. Qed.

Lemma eval_hash_pred_Por_refl :
  forall p1 a,
    eval_hash_pred (Por p1 p1) a = eval_hash_pred p1 a.
Proof. intros. simpl. repeat destruct_match; auto. Qed.

  Lemma pand_false :
  forall a b l,
    eval_hash_pred (Pand a b) l <> Some true ->
    eval_hash_pred b l = Some true ->
    eval_hash_pred a l <> Some true.
Proof. simplify. repeat destruct_match; crush. Qed.

Lemma pand_true :
  forall a b l,
    eval_hash_pred (Pand a b) l = Some true ->
    eval_hash_pred a l = Some true /\ eval_hash_pred b l = Some true.
Proof. simplify; repeat destruct_match; crush. Qed.

Lemma pand_true2 :
  forall a b l,
    eval_hash_pred b l = Some true ->
    eval_hash_pred a l = Some true ->
    eval_hash_pred (Pand a b) l = Some true.
Proof. simplify; repeat destruct_match; crush. Qed.

Lemma eval_hash_pred_fold_Pand :
  forall l x la,
    eval_hash_pred (fold_left Pand l x) la = Some true ->
    eval_hash_pred x la = Some true.
Proof.
  induction l as [| el l' IHl ]; [tauto|].
  intros * EVAL. cbn in *.
  eapply IHl in EVAL. eapply pand_true in EVAL; tauto.
Qed.

Lemma eval_hash_pred_fold_Pand2 :
  forall l x la y,
    eval_hash_pred (fold_left Pand l x) la = Some true ->
    eval_hash_pred y la = Some true ->
    eval_hash_pred (fold_left Pand l y) la = Some true.
Proof.
  induction l as [| el l' IHl ]; [tauto|].
  intros * EVAL EVALY. cbn in *.
  pose proof EVAL as EVAL2.
  apply eval_hash_pred_fold_Pand in EVAL.
  eapply IHl; eauto. apply pand_true2; apply pand_true in EVAL; tauto.
Qed.

Lemma eval_hash_pred_fold_Pand3 :
  forall l x la,
    eval_hash_pred (fold_left Pand l x) la = Some true ->
    eval_hash_pred (fold_left Pand l T) la = Some true.
Proof. eauto using eval_hash_pred_fold_Pand2. Qed.

Lemma eval_hash_pred_fold_Pand4 :
  forall l x la,
    eval_hash_pred (fold_left Pand l x) la = Some true ->
    eval_hash_pred (fold_left Pand l T) la = Some true /\ eval_hash_pred x la = Some true.
Proof.
  intros; split;
    eauto using eval_hash_pred_fold_Pand3, eval_hash_pred_fold_Pand.
Qed.

Lemma eval_equiv2 :
  forall a b la,
    eval_hash_pred a la = eval_hash_pred b la ->
    eval_hash_pred (equiv a b) la = Some true.
Proof.
  unfold equiv; intros. unfold eval_hash_pred in *. fold eval_hash_pred in *.
  repeat destruct_match; try discriminate; crush.
Qed.

Lemma eval_equiv :
  forall a b la,
    eval_hash_pred (equiv a b) la = Some true ->
    eval_hash_pred a la = eval_hash_pred b la.
Proof.
  unfold equiv; intros. unfold eval_hash_pred in *. fold eval_hash_pred in *.
  repeat destruct_match; try discriminate; crush.
Qed.

Lemma eval_hash_pred_T_Pand :
  forall a l,
    eval_hash_pred (Pand T a) l = eval_hash_pred a l.
Proof.
  intros; unfold eval_hash_pred; fold eval_hash_pred;
  destruct_match; auto.
Qed.

Lemma fold_and_forall :
  forall p a pt,
    eval_hash_pred (fold_left Pand p pt) a = Some true ->
    Forall (fun x => eval_hash_pred x a = Some true) p.
Proof.
  induction p; crush.
  constructor.
  - apply eval_hash_pred_fold_Pand in H. eapply pand_true; eauto.
  - eapply IHp. eauto.
Qed.

Lemma fold_and_forall2 :
  forall p a pt,
    eval_hash_pred pt a = Some true ->
    Forall (fun x => eval_hash_pred x a = Some true) p ->
    eval_hash_pred (fold_left Pand p pt) a = Some true.
Proof.
  induction p; crush. inv H0.
  eapply IHp; eauto.
  apply pand_true2; eauto.
Qed.

Lemma fold_and_forall3 :
  forall p a,
    Forall (fun x => eval_hash_pred x a = Some true) p <->
      eval_hash_pred (fold_left Pand p T) a = Some true.
Proof.
  intros; split; intros.
  eapply fold_and_forall2; eauto.
  eapply fold_and_forall; eauto.
Qed.

Lemma eval_hash_pred_gso2 :
  forall p x y b,
    ~ pred_In y p ->
    eval_hash_pred p (PTree.set y b x) = eval_hash_pred p x.
Proof.
  induction p; try solve [crush].
  - intros * MAX. cbn in *. rewrite PTree.gso; auto. unfold not; intros.
    apply MAX; subst; constructor.
  - intros. cbn in H. simplify. erewrite !IHp1.
    erewrite !IHp2. auto. unfold not in *; intros; apply H; now apply pred_In_Pand2.
    unfold not in *; intros; apply H; now constructor.
  - intros. cbn in H. simplify. erewrite !IHp1.
    erewrite !IHp2. auto. unfold not in *; intros; apply H; now apply pred_In_Por2.
    unfold not in *; intros; apply H; now constructor.
  - intros. cbn in H. simplify. erewrite !IHp1.
    erewrite !IHp2. auto. unfold not in *; intros; apply H; now apply pred_In_Pimp2.
    unfold not in *; intros; apply H; now constructor.
  - intros. cbn in H. simplify. erewrite !IHp. auto.
    unfold not in *; intros; apply H; now constructor.
Qed.

Lemma eval_hash_pred_gso :
  forall p x y b,
    (max_hash_pred p < y)%positive ->
    eval_hash_pred p (PTree.set y b x) = eval_hash_pred p x.
Proof.
  induction p; try solve [crush].
  - intros * MAX. cbn in *. now rewrite PTree.gso by lia.
  - intros. cbn in H. simplify. erewrite !IHp1 by lia.
    now erewrite !IHp2 by lia.
  - intros. cbn in H. simplify. erewrite !IHp1 by lia.
    now erewrite !IHp2 by lia.
  - intros. cbn in H. simplify. erewrite !IHp1 by lia.
    now erewrite !IHp2 by lia.
  - intros. cbn in H. simplify. now erewrite !IHp by lia.
Qed.

Lemma eval_hash_pred_get :
  forall p a,
    eval_hash_pred (Pbase p) a = a ! p.
Proof. crush. Qed.

Lemma eval_hash_pred_except :
  forall p a a0 a0',
    ~ pred_In a p ->
    (forall x : positive, x <> a -> a0 ! x = a0' ! x) ->
    eval_hash_pred p a0 = eval_hash_pred p a0'.
Proof.
  induction p; crush.
  - destruct (peq a0 a); subst.
    + exfalso. apply H. constructor.
    + now rewrite H0 by auto.
  - erewrite IHp1; eauto.
    erewrite IHp2; eauto.
    unfold not; intros; apply H. apply pred_In_Pand2; auto.
    unfold not; intros; apply H. constructor; auto.
  - erewrite IHp1; eauto.
    erewrite IHp2; eauto.
    unfold not; intros; apply H. apply pred_In_Por2; auto.
    unfold not; intros; apply H. constructor; auto.
  - erewrite IHp1; eauto.
    erewrite IHp2; eauto.
    unfold not; intros; apply H. apply pred_In_Pimp2; auto.
    unfold not; intros; apply H. constructor; auto.
  - erewrite IHp; eauto.
    unfold not; intros; apply H. constructor; auto.
Qed.

Lemma eval_hash_pred_pand :
  forall p p1 p2 p3 a a',
    eval_hash_pred p1 a = eval_hash_pred p a' ->
    eval_hash_pred p2 a = eval_hash_pred p3 a' ->
    eval_hash_pred (p1 ∧ p2) a = eval_hash_pred (p ∧ p3) a'.
Proof. simplify; rewrite !H; now rewrite !H0. Qed.

Lemma eval_hash_pred_por :
  forall p p1 p2 p3 a a',
    eval_hash_pred p1 a = eval_hash_pred p a' ->
    eval_hash_pred p2 a = eval_hash_pred p3 a' ->
    eval_hash_pred (p1 ∨ p2) a = eval_hash_pred (p ∨ p3) a'.
Proof. simplify; rewrite !H; now rewrite !H0. Qed.

Lemma eval_hash_pred_pimp :
  forall p p1 p2 p3 a a',
    eval_hash_pred p1 a = eval_hash_pred p a' ->
    eval_hash_pred p2 a = eval_hash_pred p3 a' ->
    eval_hash_pred (p1 → p2) a = eval_hash_pred (p → p3) a'.
Proof. simplify; rewrite !H; now rewrite !H0. Qed.

Lemma eval_hash_pred_pnot :
  forall p p' a a',
    eval_hash_pred p a = eval_hash_pred p' a' ->
    eval_hash_pred (¬ p) a = eval_hash_pred (¬ p') a'.
Proof. simplify; now rewrite !H. Qed.

Lemma fold_left_Pand_rev :
  forall l b,
    eval_hash_pred (fold_left Pand (rev l) T) b = Some true ->
    eval_hash_pred (fold_left Pand l T) b = Some true.
Proof.
  intros.
  apply fold_and_forall3.
  apply fold_and_forall3 in H.
  apply Forall_forall; intros.
  now eapply Forall_forall in H; [|rewrite <- in_rev; eassumption].
Qed.

Lemma fold_left_Pand_rev2 :
  forall l b,
    eval_hash_pred (fold_left Pand l T) b = Some true ->
    eval_hash_pred (fold_left Pand (rev l) T) b = Some true.
Proof.
  intros.
  apply fold_and_forall3.
  apply fold_and_forall3 in H.
  apply Forall_forall; intros.
  now eapply Forall_forall in H; [|rewrite in_rev; eassumption].
Qed.

Lemma eval_hash_pred_T_Pand2 :
  forall a b l,
    eval_hash_pred b l = Some true ->
    eval_hash_pred (Pand a b) l = eval_hash_pred a l.
Proof.
  intros; unfold eval_hash_pred; fold eval_hash_pred.
    repeat (destruct_match; crush).
Qed.

Lemma eval_hash_pred_T_Pand3:
  forall b a,
    eval_hash_pred (b ∧ T) a = eval_hash_pred b a.
Proof.
  unfold eval_hash_pred; crush.
  repeat (destruct_match; crush).
Qed.

End EVAL.

End ThreeValued.