-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathZFLib.v
2304 lines (2030 loc) · 85.6 KB
/
ZFLib.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
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 Coq.Strings.String.
Require Export Coq.ZArith.ZArith.
Require Export Coq.Sets.Ensembles.
Require Export Coq.Bool.Bool.
Require Export FV.ExplicitName.
Require Export Compare_dec.
Require Export Coq.Lists.List.
Export ListNotations.
(* ***************************************************************** *)
(* *)
(* *)
(* Basic library for Coq *)
(* *)
(* *)
(* ***************************************************************** *)
Module A:= StringName.
Module B:= StringName.
Lemma Union_spec: forall A (v: A) P Q, Union _ P Q v <-> P v \/ Q v.
Proof.
intros.
split; intros.
+ inversion H; auto.
+ destruct H; [apply Union_introl | apply Union_intror]; auto.
Qed.
Lemma Singleton_spec: forall U x y, (Singleton U x) y <-> x = y.
Proof.
intros; split; intro.
+ inversion H; auto.
+ subst; constructor.
Qed.
(* ***************************************************************** *)
(* *)
(* *)
(* Syntax *)
(* *)
(* *)
(* ***************************************************************** *)
Module V := StringName.
Inductive term :=
| var (v:V.t)
| empty_set
| singleton (x:term)
| union (x y: term)
| intersection (x y:term).
Inductive prop: Type :=
| PEq (t1 t2: term): prop
| PRel (t1 t2: term): prop
| PFalse: prop
| PTrue: prop
| PNot(P: prop)
| PAnd (P Q: prop): prop
| POr(P Q: prop): prop
| PImpl (P Q: prop): prop
| PIff (P Q: prop): prop
| PForall (x: V.t) (P: prop): prop
| PExists (x: V.t)(P:prop): prop.
Declare Custom Entry set.
Coercion var: V.t >-> term.
Notation "[[ e ]]" := e (at level 0, e custom set at level 99).
Notation "( x )" := x (in custom set, x custom set at level 99).
Notation "x" := x (in custom set at level 0, x constr at level 0).
(* Notation "f x .. y" := (.. (f x) .. y)
(in custom set at level 0, only parsing,
f constr, x constr, y constr). *)
Notation "∅":= empty_set (in custom set at level 5, no associativity).
Notation "{ x }":= (singleton x) (in custom set at level 5, x at level 13, no associativity).
Notation "x ∩ y" := (intersection x y)(in custom set at level 11, left associativity).
Notation "x ∪ y" := (union x y)(in custom set at level 12, left associativity).
(* Notation "f x" := (f x) (in custom set at level 20, f constr, x constr, left associativity). *)
Notation "x = y" := (PEq x y) (in custom set at level 20, no associativity).
Notation "x ∈ y" := (PRel x y) (in custom set at level 20, no associativity).
Notation "¬ P" := (PNot P) (in custom set at level 23, right associativity).
Notation "P1 /\ P2" := (PAnd P1 P2) (in custom set at level 24, left associativity).
Notation "P1 \/ P2" := (POr P1 P2) (in custom set at level 26, left associativity).
Notation "P1 -> P2" := (PImpl P1 P2) (in custom set at level 27, right associativity).
Notation "P1 <-> P2" := (PIff P1 P2) (in custom set at level 28, no associativity).
Notation "∃ x , P" := (PExists x P) (in custom set at level 29, right associativity).
Notation "∀ x , P" := (PForall x P) (in custom set at level 29, right associativity).
Notation " 'var2tm' x":= (var x) (in custom set at level 5, only parsing, x constr).
(* ***************************************************************** *)
(* *)
(* *)
(* Substitution *)
(* *)
(* *)
(* ***************************************************************** *)
Fixpoint term_occur (x: V.t) (t: term): nat :=
match t with
| [[∅]] => O
| var x0 => if V.eq_dec x x0 then S O else O
| [[{x0}]] => term_occur x x0
| [[ x0 ∩ y0 ]]
| [[ x0 ∪ y0 ]] => term_occur x x0 + term_occur x y0
end.
Fixpoint prop_free_occur (x: V.t) (d: prop): nat :=
match d with
| [[ t1 = t2]] => (term_occur x t1) + (term_occur x t2)
| [[ t1 ∈ t2]] => (term_occur x t1) + (term_occur x t2)
| PFalse
| PTrue => O
| [[ ¬ P ]] => prop_free_occur x P
| [[ P1 /\ P2 ]]
| [[ P1 \/ P2 ]]
| [[ P1 -> P2 ]]
| [[ P1 <-> P2]] => (prop_free_occur x P1) + (prop_free_occur x P2)
| [[ ∀x',P]]
| [[ ∃x',P]] => if V.eq_dec x x'
then O
else prop_free_occur x P
end.
Fixpoint term_max_var (t: term): V.t :=
match t with
| [[∅]] => V.default
| var x0 => x0
| [[ {x0} ]] => term_max_var x0
| [[ x0 ∩ y0 ]]
| [[ x0 ∪ y0 ]]=> V.max (term_max_var x0) (term_max_var y0)
end.
Fixpoint prop_max_var (d: prop): V.t :=
match d with
| [[ t1 = t2]]
| [[ t1 ∈ t2]] => V.max (term_max_var t1) (term_max_var t2)
| PTrue
| PFalse => V.default
| [[ ¬ P ]] => prop_max_var P
| [[ P1 /\ P2 ]]
| [[ P1 \/ P2 ]]
| [[ P1 -> P2 ]]
| [[ P1 <-> P2]] => V.max (prop_max_var P1) (prop_max_var P2)
| [[ ∀x',P]]
| [[ ∃x',P]] => V.max x' (prop_max_var P)
end.
Definition subst_task: Type := list (V.t * term).
Fixpoint subst_task_occur (x: V.t) (st: subst_task): nat :=
match st with
| nil => O
| cons (x', t') st' => term_occur x x' + term_occur x t' + subst_task_occur x st'
end.
Fixpoint subst_task_max_var (st: subst_task): V.t :=
match st with
| nil => V.default
| cons (x', t') st' => V.max x' (V.max (term_max_var t') (subst_task_max_var st'))
end.
Definition new_var (P: prop) (st: subst_task): V.t :=
V.next_name (V.max (prop_max_var P) (subst_task_max_var st)).
Fixpoint var_sub (x: V.t) (st: subst_task): term :=
match st with
| nil => x
| cons (x', t') st' =>
if V.eq_dec x x' then t' else var_sub x st'
end.
Fixpoint term_sub (st: subst_task) (t: term) :=
match t with
| [[∅]]=> [[∅]]
| var x => var_sub x st
| [[ {x} ]]=> singleton (term_sub st x)
| [[ x ∪ y]] => union (term_sub st x) (term_sub st y)
| [[ x ∩ y]] => intersection (term_sub st x) (term_sub st y)
end.
Fixpoint prop_sub (st: subst_task) (d: prop): prop :=
match d with
| [[ t1 = t2]] => PEq (term_sub st t1) (term_sub st t2)
| [[ t1 ∈ t2]] => PRel (term_sub st t1) (term_sub st t2)
| PTrue => PTrue
| PFalse => PFalse
| [[ ¬ P ]] => PNot (prop_sub st P)
| [[ P1 /\ P2 ]]=> PAnd (prop_sub st P1) (prop_sub st P2)
| [[ P1 \/ P2 ]] => POr (prop_sub st P1) (prop_sub st P2)
| [[ P1 -> P2 ]] => PImpl (prop_sub st P1) (prop_sub st P2)
| [[ P1 <-> P2]] => PIff (prop_sub st P1) (prop_sub st P2)
| [[ ∀x,P]] => match subst_task_occur x st with
| O => PForall x (prop_sub st P)
| _ => let x' := new_var P st in
PForall x' (prop_sub (cons (x, var x') st) P)
end
| [[ ∃x,P]] => match subst_task_occur x st with
| O => PExists x (prop_sub st P)
| _ => let x' := new_var P st in
PExists x' (prop_sub (cons (x, var x') st) P)
end
end.
Notation "x |-> t" := (x, t) (in custom set at level 17, no associativity).
Notation "P [ xt ]" :=
(prop_sub ( cons xt nil ) P) (in custom set at level 20, no associativity) .
Notation "P [ xt1 ; xt2 ; .. ; xtn ]" :=
(prop_sub ( cons xt1 (cons xt2 .. (cons xtn nil) .. ) ) P) (in custom set at level 20, no associativity).
(* ***************************************************************** *)
(* *)
(* *)
(* Alpha equivalence *)
(* *)
(* *)
(* ***************************************************************** *)
Definition binder_pair:Type:=V.t*V.t*bool.
Definition binder_list: Type:=list binder_pair.
Definition binder_update (x:V.t)(y:V.t)(bd:binder_pair):binder_pair:=
let '(x0,y0,b):= bd in
if zerop (term_occur x x0 + term_occur y y0) then bd else (x0,y0,false).
Definition update (x y:V.t)(st:binder_list):=
map (fun bd => binder_update x y bd) st.
Fixpoint in_binder_list(x y:V.t)(l:binder_list):bool:=
match l with
| nil => false
| (x0,y0,b)::ls => if V.eq_dec x x0
then if V.eq_dec y y0
then if Sumbool.sumbool_of_bool b
then true
else
in_binder_list x y ls
else in_binder_list x y ls else in_binder_list x y ls
end.
Fixpoint not_in_binder_list(x:V.t)(l:binder_list):bool:=
match l with
| nil => true
| (x0,y0,b)::ls => if V.eq_dec x x0 then false else
if V.eq_dec x y0 then false else not_in_binder_list x ls
end.
Fixpoint term_alpha_eq(l:binder_list)(t1 t2:term):bool:=
match t1,t2 with
| empty_set, empty_set => true
| var x, var y => if V.eq_dec x y then in_binder_list x y l || not_in_binder_list x l
else in_binder_list x y l
| singleton x, singleton y => term_alpha_eq l x y
| intersection x1 x2, intersection y1 y2
| union x1 x2, union y1 y2 => term_alpha_eq l x1 y1 && term_alpha_eq l x2 y2
| _ , _ => false
end.
Fixpoint alpha_eq(l:binder_list)(P Q:prop):bool:=
match P,Q with
| [[t1 = t2]], [[t3=t4]]
| [[t1 ∈ t2]], [[t3 ∈ t4]] => term_alpha_eq l t1 t3 && term_alpha_eq l t2 t4
| PTrue, PTrue
| PFalse, PFalse => true
| [[¬ P1]], [[¬ Q1]] => alpha_eq l P1 Q1
| [[P1 /\ P2]], [[Q1 /\ Q2]]
| [[P1 \/ P2]], [[Q1 \/ Q2]]
| [[P1 -> P2]], [[Q1 -> Q2]]
| [[P1 <-> P2]], [[Q1 <-> Q2]] => alpha_eq l P1 Q1 && alpha_eq l P2 Q2
| [[∀x, P1]], [[∀y, Q1]]
| [[∃x, P1]], [[∃y, Q1]]=> alpha_eq ((x,y,true)::(update x y l)) P1 Q1
| _, _ => false
end.
Definition aeq(P Q:prop):bool:= alpha_eq nil P Q.
(* ***************************************************************** *)
(* *)
(* *)
(* SAT solver *)
(* *)
(* *)
(* ***************************************************************** *)
Module prop_table.
Fixpoint term_eqb (s t: term): bool :=
match s, t with
| var x, var y => if V.eq_dec x y then true else false
| empty_set, empty_set => true
| singleton s1, singleton t1 => term_eqb s1 t1
| union s1 s2, union t1 t2 => term_eqb s1 t1 && term_eqb s2 t2
| intersection s1 s2, intersection t1 t2 => term_eqb s1 t1 && term_eqb s2 t2
| _, _ => false
end.
Fixpoint prop_eqb (P Q: prop) :=
match P, Q with
| PEq s1 s2, PEq t1 t2 => term_eqb s1 t1 && term_eqb s2 t2
| PRel s1 s2, PRel t1 t2 => term_eqb s1 t1 && term_eqb s2 t2
| PFalse, PFalse => true
| PTrue, PTrue => true
| PNot P1, PNot Q1 => prop_eqb P1 Q1
| PAnd P1 P2, PAnd Q1 Q2 => prop_eqb P1 Q1 && prop_eqb P2 Q2
| POr P1 P2, POr Q1 Q2 => prop_eqb P1 Q1 && prop_eqb P2 Q2
| PImpl P1 P2, PImpl Q1 Q2 => prop_eqb P1 Q1 && prop_eqb P2 Q2
| PIff P1 P2, PIff Q1 Q2 => prop_eqb P1 Q1 && prop_eqb P2 Q2
| PForall x P1, PForall y Q1 => if V.eq_dec x y then prop_eqb P1 Q1 else false
| PExists x P1, PExists y Q1 => if V.eq_dec x y then prop_eqb P1 Q1 else false
| _, _ => false
end.
Definition prop_table := list (prop * string).
Fixpoint prop_look_up {B: Type} (x: prop) (KV: list (prop * B)): option B :=
match KV with
| nil => None
| cons (x0, v0) KV0 =>
if aeq x0 x then Some v0 else prop_look_up x KV0
end.
End prop_table.
Module ToyDPLL.
Module PV := StringName.
Definition ident := PV.t.
Definition clause := list (bool * ident).
Definition CNF := list clause.
Definition partial_asgn := list (ident * bool).
Inductive UP_result :=
| Conflict
| UP (x: ident) (b: bool)
| Nothing.
Fixpoint find_unit_pro_in_clause (c: clause) (J: partial_asgn) (cont: UP_result): UP_result :=
match c with
| nil => cont
| (op, x) :: c' =>
match PV.look_up x J with
| None => match cont with
| Conflict => find_unit_pro_in_clause c' J (UP x op)
| UP _ _ => Nothing
| _ => Nothing
end
| Some b => if eqb op b then Nothing else find_unit_pro_in_clause c' J cont
end
end.
Definition unit_pro' (P: CNF) (J: partial_asgn): list UP_result :=
map (fun c => find_unit_pro_in_clause c J Conflict) P.
Definition fold_UP_result (rs: list UP_result): option partial_asgn :=
fold_left (fun (o: option partial_asgn) (r: UP_result) =>
match r, o with
| _, None => None
| Nothing, _ => o
| Conflict, _ => None
| UP x b, Some J => Some ((x, b) :: J)
end) rs (Some nil).
Definition unit_pro (P: CNF) (J: partial_asgn): option partial_asgn :=
fold_UP_result (unit_pro' P J).
Definition clause_filter (J: partial_asgn) (c: clause): clause :=
filter (fun opx: bool * ident =>
match opx with
| (op, x) => match PV.look_up x J with
| None => true
| Some b => eqb b op
end
end) c.
Definition clause_not_ex_true (J: partial_asgn) (c: clause): bool :=
negb
(existsb
(fun opx: bool * ident =>
match opx with
| (op, x) => match PV.look_up x J with
| None => false
| Some b => eqb b op
end
end) c).
Definition pick (P: CNF): ident :=
match P with
| ((_, x) :: _) :: _ => x
| _ => "impossible"%string
end.
Definition CNF_filter (P: CNF) (J: partial_asgn): CNF :=
map (clause_filter J) (filter (clause_not_ex_true J) P).
Fixpoint DPLL_UP (P: CNF) (J: partial_asgn) (n: nat): bool :=
match n with | O => true | S n' =>
match unit_pro P J with
| None => false
| Some kJ => match kJ with
| nil => DPLL_filter P J n'
| _ => DPLL_UP P (kJ ++ J) n'
end
end
end
with DPLL_filter (P: CNF) (J: partial_asgn) (n: nat): bool :=
match n with | O => true | S n' =>
DPLL_pick (CNF_filter P J) nil n'
end
with DPLL_pick (P: CNF) (J: partial_asgn) (n: nat): bool :=
match n with | O => true | S n' =>
let x := pick P in
DPLL_UP P ((x, true) :: J) n' || DPLL_UP P ((x, false) :: J) n'
end.
Local Open Scope string.
Definition cnf1 :=
((true, "x") :: (true, "y") :: nil) :: ((true, "x") :: (false, "y") :: nil) :: nil.
Eval compute in (DPLL_UP cnf1 nil 6).
Definition cnf2 :=
((true, "x") :: (true, "y") :: nil) :: ((true, "x") :: (false, "y") :: nil) :: ((false, "x") :: nil) :: nil.
Eval compute in (DPLL_UP cnf2 nil 6).
Definition cnf3 :=
((false, "x") :: (true, "y") :: nil) ::
((false, "y") :: (true, "z") :: nil) ::
((false, "z") :: (true, "w") :: nil) ::
((true, "x") :: nil) ::
((false, "w") :: nil) :: nil.
Eval compute in (DPLL_UP cnf3 nil 12).
Inductive sprop: Type :=
| SId (x: V.t)
| SFalse
| STrue
| SNot (P: sprop)
| SAnd (P Q: sprop)
| SOr (P Q: sprop)
| SImpl (P Q: sprop).
Fixpoint sprop_gen (P: prop) (t: prop_table.prop_table) (n: V.t): sprop * prop_table.prop_table * V.t :=
match prop_table.prop_look_up P t with
| Some x => (SId x, t, n)
| None => match P with
| PFalse => (SFalse, t, n)
| PTrue => (STrue, t, n)
| PNot P1 => match sprop_gen P1 t n with
| (res0, t0, n0) => (SNot res0, t0, n0)
end
| PAnd P1 P2 => match sprop_gen P1 t n with
| (res0, t0, n0) =>
match sprop_gen P2 t0 n0 with
| (res1, t1, n1) => (SAnd res0 res1, t1, n1)
end
end
| POr P1 P2 => match sprop_gen P1 t n with
| (res0, t0, n0) =>
match sprop_gen P2 t0 n0 with
| (res1, t1, n1) => (SOr res0 res1, t1, n1)
end
end
| PImpl P1 P2=> match sprop_gen P1 t n with
| (res0, t0, n0) =>
match sprop_gen P2 t0 n0 with
| (res1, t1, n1) => (SImpl res0 res1, t1, n1)
end
end
| PIff P1 P2=> match sprop_gen P1 t n with
| (res0, t0, n0) =>
match sprop_gen P2 t0 n0 with
| (res1, t1, n1) => (SAnd (SImpl res0 res1) (SImpl res1 res0), t1, n1)
end
end
| _ => (SId n, (P, n) :: t, V.next_name n)
end
end.
Fixpoint clause_gen (P: sprop) (n: V.t) (cont': clause) (cont: CNF): clause * CNF * V.t :=
match P with
| SId x => ((true, x) :: cont', cont, n)
| SFalse => (cont', cont, n)
| STrue => (((true, "tauto"):: (false, "tauto"):: nil), cont, n)
| SNot P1 => clause_neg_gen P1 n cont' cont
| SAnd P1 P2 => match clause_gen P1 (V.next_name n) ((false, n) :: nil) cont with
| (cont'0, cont0, n0) =>
match clause_gen P2 n0 ((false, n) :: nil) (cont'0 :: cont0) with
| (cont'1, cont1, n1) =>
((true, n) :: cont', cont'1 :: cont1, n1)
end
end
| SOr P1 P2 => match clause_gen P1 n cont' cont with
| (cont'0, cont0, n0) =>
clause_gen P2 n0 cont'0 cont0
end
| SImpl P1 P2 => match clause_neg_gen P1 n cont' cont with
| (cont'0, cont0, n0) =>
clause_gen P2 n0 cont'0 cont0
end
end
with clause_neg_gen (P: sprop) (n: V.t) (cont': clause) (cont: CNF): clause * CNF * V.t :=
match P with
| SId x => ((false, x) :: cont', cont, n)
| SFalse => (((true, "tauto"):: (false, "tauto"):: nil), cont, n)
| STrue => (cont', cont, n)
| SNot P1 => clause_gen P1 n cont' cont
| SAnd P1 P2 => match clause_neg_gen P1 n cont' cont with
| (cont'0, cont0, n0) =>
clause_neg_gen P2 n0 cont'0 cont0
end
| SOr P1 P2 => match clause_neg_gen P1 (V.next_name n) ((true, n) :: nil) cont with
| (cont'0, cont0, n0) =>
match clause_neg_gen P2 n0 ((true, n) :: nil) (cont'0 :: cont0) with
| (cont'1, cont1, n1) =>
((false, n) :: cont', cont'1 :: cont1, n1)
end
end
| SImpl P1 P2 => match clause_gen P1 (V.next_name n) ((true, n) :: nil) cont with
| (cont'0, cont0, n0) =>
match clause_neg_gen P2 n0 ((true, n) :: nil) (cont'0 :: cont0) with
| (cont'1, cont1, n1) =>
((false, n) :: cont', cont'1 :: cont1, n1)
end
end
end.
Fixpoint cnf_gen (P: sprop) (n: V.t) (cont: CNF): CNF * V.t :=
match P with
| SId x => (((true, x) :: nil) :: cont, n)
| SFalse => (((true, "impossible"):: nil) :: ((false, "impossible"):: nil) :: nil, n)
| STrue => (cont, n)
| SNot P1 => match clause_neg_gen P1 (V.next_name n) ((true, n) :: nil) (((false, n) :: nil) :: cont) with
| (cont'0, cont0, n0) => (cont'0 :: cont0, n0)
end
| SAnd P1 P2 => match cnf_gen P1 n cont with
| (cont0, n0) => cnf_gen P2 n0 cont0
end
| SOr P1 P2 => match clause_gen P1 n nil cont with
| (cont'0, cont0, n0) =>
match clause_gen P2 n0 cont'0 cont0 with
| (cont'1, cont1, n1) => (cont'1 :: cont1, n1)
end
end
| SImpl P1 P2 => match clause_neg_gen P1 n nil cont with
| (cont'0, cont0, n0) =>
match clause_gen P2 n0 cont'0 cont0 with
| (cont'1, cont1, n1) => (cont'1 :: cont1, n1)
end
end
end.
Definition valid (P: prop): bool :=
match sprop_gen (PNot P) nil "x" with
| (P', _, n) =>
match cnf_gen P' n nil with
| (P'', _) => negb (DPLL_UP P'' nil 24)
end
end.
Definition der_judgement: Type := list prop * prop.
Definition proof_goal: Type := list der_judgement * der_judgement.
Definition der2prop (d: der_judgement): prop :=
fold_right PImpl (snd d) (fst d).
Definition pg2prop (pg: proof_goal): prop :=
fold_right (fun x y => PImpl (der2prop x) y) (der2prop (snd pg)) (fst pg).
End ToyDPLL.
Module ToyDPLL2.
Import ToyDPLL.
Inductive result :=
| SAT
| UNSAT
| UNKNOWN (J: partial_asgn) (P: CNF).
Fixpoint DPLL_UP (P: CNF) (J: partial_asgn) (n: nat): result :=
match n with | O => UNKNOWN J P | S n' =>
match unit_pro P J with
| None => UNSAT
| Some kJ => match kJ with
| nil => DPLL_filter P J n'
| _ => DPLL_UP P (kJ ++ J) n'
end
end
end
with DPLL_filter (P: CNF) (J: partial_asgn) (n: nat): result :=
match n with | O => UNKNOWN J P | S n' =>
DPLL_pick (CNF_filter P J) nil n'
end
with DPLL_pick (P: CNF) (J: partial_asgn) (n: nat): result :=
match n with | O => UNKNOWN J P | S n' =>
let x := pick P in
match DPLL_UP P ((x, true) :: J) n' with
| SAT => SAT
| UNSAT => DPLL_UP P ((x, false) :: J) n'
| UNKNOWN J P => match DPLL_UP P ((x, false) :: J) n' with
| SAT => SAT
| _ => UNKNOWN J P
end
end
end.
End ToyDPLL2.
(* ***************************************************************** *)
(* *)
(* *)
(* Variable name definitions *)
(* *)
(* *)
(* ***************************************************************** *)
Notation "'EVAL' x" := (ltac:(let y := eval compute in x in exact y): V.t) (at level 0).
Module ShortNames.
Definition x := EVAL "x"%string.
Definition x0 := EVAL (V.next_name x).
Definition x1 := EVAL (V.next_name x0).
Definition x2 := EVAL (V.next_name x1).
Definition x3 := EVAL (V.next_name x2).
Definition y := EVAL "y"%string.
Definition y0 := EVAL (V.next_name y).
Definition y1 := EVAL (V.next_name y0).
Definition y2 := EVAL (V.next_name y1).
Definition y3 := EVAL (V.next_name y2).
Definition z := EVAL "z"%string.
Definition z0 := EVAL (V.next_name z).
Definition z1 := EVAL (V.next_name z0).
Definition z2 := EVAL (V.next_name z1).
Definition z3 := EVAL (V.next_name z2).
Definition u := EVAL "u"%string.
Definition u0 := EVAL (V.next_name u).
Definition u1 := EVAL (V.next_name u0).
Definition u2 := EVAL (V.next_name u1).
Definition u3 := EVAL (V.next_name u2).
Definition v := EVAL "v"%string.
Definition v0 := EVAL (V.next_name v).
Definition v1 := EVAL (V.next_name v0).
Definition v2 := EVAL (V.next_name v1).
Definition v3 := EVAL (V.next_name v2).
Definition w := EVAL "w"%string.
Definition w0 := EVAL (V.next_name w).
Definition w1 := EVAL (V.next_name w0).
Definition w2 := EVAL (V.next_name w1).
Definition w3 := EVAL (V.next_name w2).
Definition n := EVAL "n"%string.
Definition n0 := EVAL (V.next_name n).
Definition n1 := EVAL (V.next_name n0).
Definition n2 := EVAL (V.next_name n1).
Definition n3 := EVAL (V.next_name n2).
Definition m := EVAL "m"%string.
Definition m0 := EVAL (V.next_name m).
Definition m1 := EVAL (V.next_name m0).
Definition m2 := EVAL (V.next_name m1).
Definition m3 := EVAL (V.next_name m2).
Definition a := EVAL "a"%string.
Definition a0 := EVAL (V.next_name a).
Definition a1 := EVAL (V.next_name a0).
Definition a2 := EVAL (V.next_name a1).
Definition a3 := EVAL (V.next_name a2).
Definition b := EVAL "b"%string.
Definition b0 := EVAL (V.next_name b).
Definition b1 := EVAL (V.next_name b0).
Definition b2 := EVAL (V.next_name b1).
Definition b3 := EVAL (V.next_name b2).
Definition c := EVAL "c"%string.
Definition c0 := EVAL (V.next_name c).
Definition c1 := EVAL (V.next_name c0).
Definition c2 := EVAL (V.next_name c1).
Definition c3 := EVAL (V.next_name c2).
Definition d := EVAL "d"%string.
Definition d0 := EVAL (V.next_name d).
Definition d1 := EVAL (V.next_name d0).
Definition d2 := EVAL (V.next_name d1).
Definition d3 := EVAL (V.next_name d2).
Definition e := EVAL "e"%string.
Definition e0 := EVAL (V.next_name e).
Definition e1 := EVAL (V.next_name e0).
Definition e2 := EVAL (V.next_name e1).
Definition e3 := EVAL (V.next_name e2).
Definition f := EVAL "f"%string.
Definition f0 := EVAL (V.next_name f).
Definition f1 := EVAL (V.next_name f0).
Definition f2 := EVAL (V.next_name f1).
Definition f3 := EVAL (V.next_name f2).
Definition g := EVAL "g"%string.
Definition g0 := EVAL (V.next_name g).
Definition g1 := EVAL (V.next_name g0).
Definition g2 := EVAL (V.next_name g1).
Definition g3 := EVAL (V.next_name g2).
Definition h := EVAL "h"%string.
Definition h0 := EVAL (V.next_name h).
Definition h1 := EVAL (V.next_name h0).
Definition h2 := EVAL (V.next_name h1).
Definition h3 := EVAL (V.next_name h2).
Definition p := EVAL "p"%string.
Definition p0 := EVAL (V.next_name p).
Definition p1 := EVAL (V.next_name p0).
Definition p2 := EVAL (V.next_name p1).
Definition p3 := EVAL (V.next_name p2).
Definition q := EVAL "q"%string.
Definition q0 := EVAL (V.next_name q).
Definition q1 := EVAL (V.next_name q0).
Definition q2 := EVAL (V.next_name q1).
Definition q3 := EVAL (V.next_name q2).
Definition X := EVAL "X"%string.
Definition X0 := EVAL (V.next_name X).
Definition X1 := EVAL (V.next_name X0).
Definition X2 := EVAL (V.next_name X1).
Definition X3 := EVAL (V.next_name X2).
Definition Y := EVAL "Y"%string.
Definition Y0 := EVAL (V.next_name Y).
Definition Y1 := EVAL (V.next_name Y0).
Definition Y2 := EVAL (V.next_name Y1).
Definition Y3 := EVAL (V.next_name Y2).
Definition Z := EVAL "Z"%string.
Definition Z0 := EVAL (V.next_name Z).
Definition Z1 := EVAL (V.next_name Z0).
Definition Z2 := EVAL (V.next_name Z1).
Definition Z3 := EVAL (V.next_name Z2).
Definition N := EVAL "N"%string.
Ltac super_fold_in H :=
repeat
first
[ progress fold x3 in H
| progress fold x2 in H
| progress fold x1 in H
| progress fold x0 in H
| progress fold x in H
| progress fold y3 in H
| progress fold y2 in H
| progress fold y1 in H
| progress fold y0 in H
| progress fold y in H
| progress fold z3 in H
| progress fold z2 in H
| progress fold z1 in H
| progress fold z0 in H
| progress fold z in H
| progress fold u3 in H
| progress fold u2 in H
| progress fold u1 in H
| progress fold u0 in H
| progress fold u in H
| progress fold v3 in H
| progress fold v2 in H
| progress fold v1 in H
| progress fold v0 in H
| progress fold v in H
| progress fold w3 in H
| progress fold w2 in H
| progress fold w1 in H
| progress fold w0 in H
| progress fold w in H
| progress fold n3 in H
| progress fold n2 in H
| progress fold n1 in H
| progress fold n0 in H
| progress fold n in H
| progress fold m3 in H
| progress fold m2 in H
| progress fold m1 in H
| progress fold m0 in H
| progress fold m in H
| progress fold a3 in H
| progress fold a2 in H
| progress fold a1 in H
| progress fold a0 in H
| progress fold a in H
| progress fold b3 in H
| progress fold b2 in H
| progress fold b1 in H
| progress fold b0 in H
| progress fold b in H
| progress fold c3 in H
| progress fold c2 in H
| progress fold c1 in H
| progress fold c0 in H
| progress fold c in H
| progress fold d3 in H
| progress fold d2 in H
| progress fold d1 in H
| progress fold d0 in H
| progress fold d in H
| progress fold e3 in H
| progress fold e2 in H
| progress fold e1 in H
| progress fold e0 in H
| progress fold e in H
| progress fold f3 in H
| progress fold f2 in H
| progress fold f1 in H
| progress fold f0 in H
| progress fold f in H
| progress fold g3 in H
| progress fold g2 in H
| progress fold g1 in H
| progress fold g0 in H
| progress fold g in H
| progress fold h3 in H
| progress fold h2 in H
| progress fold h1 in H
| progress fold h0 in H
| progress fold h in H
| progress fold p3 in H
| progress fold p2 in H
| progress fold p1 in H
| progress fold p0 in H
| progress fold p in H
| progress fold q3 in H
| progress fold q2 in H
| progress fold q1 in H
| progress fold q0 in H
| progress fold q in H
| progress fold X3 in H
| progress fold X2 in H
| progress fold X1 in H
| progress fold X0 in H
| progress fold X in H
| progress fold Y3 in H
| progress fold Y2 in H
| progress fold Y1 in H
| progress fold Y0 in H
| progress fold Y in H
| progress fold Z3 in H
| progress fold Z2 in H
| progress fold Z1 in H
| progress fold Z0 in H
| progress fold Z in H
| progress fold N in H].
End ShortNames.
(* ***************************************************************** *)
(* *)
(* *)
(* Abbreviations *)
(* *)
(* *)
(* ***************************************************************** *)
Module Rules.
Import ShortNames.
Definition subset (t1 t2: term): prop :=
[[ ( ∀ z, z ∈ x -> z ∈ y )[ x |-> t1 ; y |-> t2 ] ]].
Notation "t1 '⊆' t2" := (subset t1 t2) (in custom set at level 20, no associativity) .
Definition is_empty_def (t: term): prop :=
[[ (∀ y, ¬ y ∈ x) [ x |-> t ] ]].
Notation " 'is_empty' t1":= (is_empty_def t1) (in custom set at level 20, t1 at level 15,no associativity).
(* t1 = {t2} *)
Definition is_singleton_def (t1 t2: term): prop :=
[[ (∀ z, z ∈ x <-> z = y) [ x |-> t1 ; y |-> t2 ] ]].
Notation " 'is_singleton' t1 t2 ":= (is_singleton_def t1 t2) (in custom set at level 20, t1 at level 15, t1 at level 15, no associativity).
(* t1 = {t2, t3} *)
Definition has_two_ele_def (t1 t2 t3: term): prop :=
[[ (∀ u, u ∈ x <-> u = y \/ u = z) [ x |-> t1 ; y |-> t2 ; z |-> t3 ] ]].
Notation " 'has_two_ele' t1 t2 t3":= (has_two_ele_def t1 t2 t3) (in custom set at level 20, t1 at level 15, t2 at level 15, t3 at level 15, no associativity).
(* t1 = (t2, t3) := {{t2}, {t2, t3}} *)
Definition is_pair_def (t1 t2 t3: term): prop :=
[[ (∃ u, ∃ v, is_singleton u y /\ has_two_ele v y z/\ has_two_ele x u v) [ x |-> t1 ; y |-> t2 ; z |-> t3 ] ]].
Notation " 'is_pair' t1 t2 t3":= (is_pair_def t1 t2 t3) (in custom set at level 20, t1 at level 15, t2 at level 15, t3 at level 15, no associativity).
(* t1 = t2 U t3 *)
Definition is_union2_def (t1 t2 t3: term): prop :=
[[ (∀ u, u ∈ x <-> u ∈ y \/ u ∈ z) [ x |-> t1 ; y |-> t2 ; z |-> t3 ] ]].
Notation " 'is_union2' t1 t2 t3":= (is_union2_def t1 t2 t3) (in custom set at level 20, t1 at level 15, t2 at level 15, t3 at level 15, no associativity).
Definition is_prod_def (t1 t2 t3: term): prop :=
[[(∀ u, u ∈ x <-> ∃ v, ∃ w, v ∈ y /\ w ∈ z /\ is_pair u v w )
[ x |-> t1 ; y |-> t2 ; z |-> t3 ] ]].
Notation " 'is_prod' t1 t2 t3":= (is_prod_def t1 t2 t3) (in custom set at level 20, t1 at level 15, t2 at level 15, t3 at level 15, no associativity).
Definition is_rel_def (t1 t2 t3: term): prop :=
[[ (∀ u, u ∈ x -> ∃ v, ∃ w, v ∈ y /\ w ∈ z /\ is_pair u v w)
[ x |-> t1 ; y |-> t2 ; z |-> t3 ] ]].
Notation " 'is_rel' t1 t2 t3":= (is_rel_def t1 t2 t3) (in custom set at level 20, t1 at level 15, t2 at level 15, t3 at level 15, no associativity).
(* (t1, t2) in t3 *)
Definition in_rel_def (t1 t2 t3: term): prop :=
[[ (∃ u, is_pair u x y /\ u ∈ z) [ x |-> t1 ; y |-> t2 ; z |-> t3 ] ]].
Notation " 'in_rel' t1 t2 t3":= (in_rel_def t1 t2 t3) (in custom set at level 20, t1 at level 15, t2 at level 15, t3 at level 15, no associativity).
Definition is_func_def (t: term): prop :=
[[ (∀ y, ∀ z, ∀ u,
in_rel y z x -> in_rel y u x -> z = u) ]].
Notation " 'is_func' t1":= (is_func_def t1) (in custom set at level 20, t1 at level 15, no associativity).
Definition is_inductive_def(t:term):=
[[ (∅ ∈ x /\ ∀ y, (y ∈ x -> y ∪ {y} ∈ x) ) [ x |-> t] ]].
Notation " 'is_inductive' t1":= (is_inductive_def t1) (in custom set at level 20, t1 at level 15, no associativity).
Definition is_natural_number_def(t:term):=
[[ (is_inductive x /\ ∀ w, (is_inductive w) -> x ⊆ w)[ x |-> t] ]].
Notation " 'is_natural_number' t1":= (is_natural_number_def t1) (in custom set at level 20, t1 at level 15, no associativity).
Definition is_triple_def (t1 t2 t3 t4: term): prop :=
[[ (∃ a, is_pair a x y /\ is_pair b a z ) [ b |-> t1 ; x |-> t2 ; y |-> t3 ; z |-> t4] ]].
Notation " 'is_triple' t1 t2 t3 t4":= (is_triple_def t1 t2 t3 t4) (in custom set at level 20, t1 at level 15, t2 at level 15, t3 at level 15, t4 at level 15, no associativity).
Definition is_N2_def(t1 t2:term): prop :=
[[( is_pair x N N )[x |-> t1; N |-> t2] ]].
Notation " 'is_N2' t1 t2":= (is_N2_def t1 t2) (in custom set at level 20, t1 at level 15, t2 at level 15, no associativity).
Definition is_N3_def(t1 t2:term): prop :=
[[(∃Y, is_N2 Y N /\ is_pair x Y N )[x|-> t1; N |-> t2] ]].
Notation " 'is_N3' t1 t2":= (is_N3_def t1 t2) (in custom set at level 20, t1 at level 15, t2 at level 15, no associativity).
Definition in_rel3_def (t1 t2 t3 t4: term): prop :=
[[ (∃ u, is_triple u x y z /\ u ∈ n) [ x |-> t1 ; y |-> t2 ; z |-> t3 ; n |-> t4 ] ]].
Notation " 'in_rel3' t1 t2 t3 t4":= (in_rel3_def t1 t2 t3 t4) (in custom set at level 20, t1 at level 15, t2 at level 15, t3 at level 15, t4 at level 15, no associativity).
Definition is_legal_plus_def(t1 t2:term):prop:=
[[( (∀ n, n ∈ N -> in_rel3 ∅ n n x) /\ (∀ n,∀ d,∀ e, n ∈ N /\ d ∈ N /\ e ∈ N ->( in_rel3 n d e x -> in_rel3 n∪{n} d e∪{e} x))) [ x |-> t1 ; N |-> t2] ]].
Notation " 'is_legal_plus' t1 t2":= (is_legal_plus_def t1 t2) (in custom set at level 20, t1 at level 15, t2 at level 15, no associativity).
Definition is_plus_def(t1 t2:term):=
[[(is_legal_plus x N /\ (∀ y, is_legal_plus y N -> x ⊆ y))[x |-> t1 ; N |-> t2] ]].
Notation " 'is_plus' t1 t2":= (is_plus_def t1 t2) (in custom set at level 20, t1 at level 15, t2 at level 15, no associativity).
Definition is_legal_mult_def(t1 t2 t3:term):prop:=
[[( (∀ n, n ∈ N -> in_rel3 n ∅ ∅ f) /\ (∀x, ∀y, ∀z, ∀a, x∈N/\y∈N/\z∈N/\a∈N -> in_rel3 x y z f -> (in_rel3 z x a e -> in_rel3 x y∪{y} a f ))) [ f |-> t1 ; e |-> t2 ; N |-> t3] ]].
Notation " 'is_legal_mult' t1 t2 t3":= (is_legal_mult_def t1 t2 t3) (in custom set at level 20, t1 at level 15, t2 at level 15, t3 at level 15, no associativity).
Definition is_mult_def(t1 t2 t3:term):=
[[(is_legal_mult x e N /\ (∀ y, is_legal_mult y e N -> x ⊆ y))[x |-> t1 ; e |-> t2 ; N |-> t3] ]].
Notation " 'is_mult' t1 t2 t3":= (is_mult_def t1 t2 t3) (in custom set at level 20, t1 at level 15, t2 at level 15, t3 at level 15, no associativity).
(* ***************************************************************** *)
(* *)
(* *)
(* Proof theory *)
(* *)
(* *)
(* ***************************************************************** *)
Definition context: Type := prop -> Prop.
Definition empty_context: context := fun _ => False.
Notation "Phi ;; x" := (Union _ Phi (Singleton _ x)) (in custom set at level 31, left associativity).
Inductive derivable: context -> prop -> Prop :=
| PForall_elim: forall Phi (vr: V.t) (t: term) P,
derivable Phi [[∀ vr, P]] ->
derivable Phi [[ P [ vr |-> t ] ]]
| PExists_intros: forall Phi (vr: V.t) (t: term) P,
derivable Phi [[ P [ vr |-> t ] ]] ->
derivable Phi [[ ∃ vr, P ]]
| PForall_intros: forall (Phi: context) (vr: V.t) P,
(forall phi, Phi phi -> prop_free_occur vr phi = O) ->
derivable Phi P ->
derivable Phi [[ ∀ vr, P]]
| PExists_elim: forall (Phi: context) (vr: V.t) P Q,
(forall phi, Phi phi -> prop_free_occur vr phi = O) ->
prop_free_occur vr Q = O ->
derivable [[Phi ;; P]] Q ->
derivable [[Phi ;; (∃ vr, P)]] Q
| PAnd_intros: forall Phi P Q,
derivable Phi P ->
derivable Phi Q ->
derivable Phi [[P /\ Q]]