-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reordering_Quantifiers.thy
1080 lines (968 loc) · 37.8 KB
/
Reordering_Quantifiers.thy
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
theory Reordering_Quantifiers
imports Main "HOL-Eisbach.Eisbach"
begin
(* Printing util *)
ML \<open>
fun pretty_cterm ctxt ctm = Syntax.pretty_term ctxt (Thm.term_of ctm)
val string_of_cterm = Pretty.string_of oo pretty_cterm
val string_of_term = Pretty.string_of oo Syntax.pretty_term
\<close>
ML_val \<open>tracing (Syntax.string_of_term @{context} @{term "a < b"})\<close>
ML_val \<open>tracing (ML_Syntax.print_term @{term "a < b"})\<close>
ML \<open>
fun strip_prop (Const (@{const_name HOL.Trueprop}, _) $ t) = t
| strip_prop t = t
\<close>
declare [[ML_print_depth = 50]]
ML \<open>
signature QUANTIFIER1_DATA =
sig
(*functionality*)
(*terms to be moved around*)
(*arguments: preceding quantifies, term under question, preceding terms*)
val move: (term * string * typ) list -> term -> term list -> bool
(*always move? if false then moves appear if a non-mover was encountered before*)
val force_move: bool
(*rotate quantifiers after moving*)
val rotate: bool
(*abstract syntax*)
val dest_eq: term -> (term * term) option
val dest_conj: term -> (term * term) option
val dest_imp: term -> (term * term) option
val conj: term
val imp: term
(*rules*)
val iff_reflection: thm (* P <-> Q ==> P == Q *)
val iffI: thm
val iff_trans: thm
val conjI: thm
val conjE: thm
val impI: thm
val mp: thm
val exI: thm
val exE: thm
val uncurry: thm (* P --> Q --> R ==> P & Q --> R *)
val iff_allI: thm (* !!x. P x <-> Q x ==> (!x. P x) = (!x. Q x) *)
val iff_exI: thm (* !!x. P x <-> Q x ==> (? x. P x) = (? x. Q x) *)
val all_comm: thm (* (!x y. P x y) = (!y x. P x y) *)
val ex_comm: thm (* (? x y. P x y) = (? y x. P x y) *)
end;
signature QUANTIFIER1 =
sig
val prove_one_point_all_tac: Proof.context -> tactic
val prove_one_point_ex_tac: Proof.context -> tactic
val rearrange_all: Proof.context -> cterm -> thm option
(* XXX Need to export this ?*)
val rearrange_ex': Proof.context -> term -> thm option
val rearrange_ex: Proof.context -> cterm -> thm option
val rotate_ex: Proof.context -> cterm -> thm option
val miniscope_ex: Proof.context -> cterm -> thm option
val rotate_all: Proof.context -> cterm -> thm option
val rearrange_ball: (Proof.context -> tactic) -> Proof.context -> cterm -> thm option
val rearrange_bex: (Proof.context -> tactic) -> Proof.context -> cterm -> thm option
val rearrange_Collect: (Proof.context -> tactic) -> Proof.context -> cterm -> thm option
end;
functor Quantifier(Data: QUANTIFIER1_DATA): QUANTIFIER1 =
struct
fun extract_conj trms fst xs t =
(case Data.dest_conj t of
NONE => NONE
| SOME (P, Q) =>
let
val mover = Data.move xs P trms
in
if Data.force_move andalso mover then (if fst then NONE else SOME (xs, P, Q))
else if Data.force_move andalso Data.move xs Q (P :: trms) then SOME (xs, Q, P)
else if mover andalso not fst then SOME (xs, P, Q)
else if
not Data.force_move andalso (not mover orelse not fst) andalso Data.move xs Q (P :: trms)
then SOME (xs, Q, P)
else
(case extract_conj trms (if Data.force_move then false else fst) xs P of
SOME (xs, eq, P') => SOME (xs, eq, Data.conj $ P' $ Q)
| NONE =>
(case extract_conj (P :: trms)
(if Data.force_move then false else (fst andalso mover)) xs Q
of
SOME (xs, eq, Q') => SOME (xs, eq, Data.conj $ P $ Q')
| NONE => NONE))
end);
(* XXX This is not regularized with respect to term context *)
fun extract_imp fst xs t =
(case Data.dest_imp t of
NONE => NONE
| SOME (P, Q) =>
if Data.move xs P [] then (if fst then NONE else SOME (xs, P, Q))
else
(case extract_conj [] false xs P of
SOME (xs, eq, P') => SOME (xs, eq, Data.imp $ P' $ Q)
| NONE =>
(case extract_imp false xs Q of
NONE => NONE
| SOME (xs, eq, Q') => SOME (xs, eq, Data.imp $ P $ Q'))));
fun extract_quant extract q =
let
fun exqu xs ((qC as Const (qa, _)) $ Abs (x, T, Q)) =
if qa = q then exqu ((qC, x, T) :: xs) Q else NONE
| exqu xs P = extract (if Data.force_move then null xs else true) xs P
in exqu [] end;
fun prove_conv ctxt tu tac =
let
val (goal, ctxt') =
yield_singleton (Variable.import_terms true) (Logic.mk_equals tu) ctxt;
val thm =
Goal.prove ctxt' [] [] goal
(fn {context = ctxt'', ...} =>
resolve_tac ctxt'' [Data.iff_reflection] 1 THEN tac ctxt'');
in singleton (Variable.export ctxt' ctxt) thm end;
fun maybe_tac tac = if Data.rotate then tac else K all_tac;
fun qcomm_tac ctxt qcomm qI i =
REPEAT_DETERM (maybe_tac (resolve_tac ctxt [qcomm]) i THEN resolve_tac ctxt [qI] i);
(* Proves (? x0..xn. ... & x0 = t & ...) = (? x1..xn x0. x0 = t & ... & ...)
Better: instantiate exI
*)
local
val excomm = Data.ex_comm RS Data.iff_trans;
in
fun prove_rotate_ex_tac ctxt i = qcomm_tac ctxt excomm Data.iff_exI i
fun prove_one_point_ex_tac ctxt =
prove_rotate_ex_tac ctxt 1 THEN resolve_tac ctxt [Data.iffI] 1 THEN
ALLGOALS
(EVERY' [maybe_tac (eresolve_tac ctxt [Data.exE]),
REPEAT_DETERM o eresolve_tac ctxt [Data.conjE],
maybe_tac (resolve_tac ctxt [Data.exI]),
DEPTH_SOLVE_1 o ares_tac ctxt [Data.conjI]])
end;
(* Proves (! x0..xn. (... & x0 = t & ...) --> P x0) =
(! x1..xn x0. x0 = t --> (... & ...) --> P x0)
*)
local
fun tac ctxt =
SELECT_GOAL
(EVERY1 [REPEAT o dresolve_tac ctxt [Data.uncurry],
REPEAT o resolve_tac ctxt [Data.impI],
eresolve_tac ctxt [Data.mp],
REPEAT o eresolve_tac ctxt [Data.conjE],
REPEAT o ares_tac ctxt [Data.conjI]]);
val allcomm = Data.all_comm RS Data.iff_trans;
in
fun prove_one_point_all_tac ctxt =
EVERY1 [qcomm_tac ctxt allcomm Data.iff_allI,
resolve_tac ctxt [Data.iff_allI],
resolve_tac ctxt [Data.iffI], tac ctxt, tac ctxt];
end
(* Proves (! x0..xn. (... & x0 = t & ...) --> P x0) =
(! x1..xn x0. x0 = t --> (... & ...) --> P x0)
*)
local
val allcomm = Data.all_comm RS Data.iff_trans;
in
fun prove_one_point_all_tac2 ctxt =
EVERY1 [qcomm_tac ctxt allcomm Data.iff_allI,
resolve_tac ctxt [Data.iff_allI],
resolve_tac ctxt [Data.iffI], blast_tac ctxt, blast_tac ctxt];
end
fun renumber l u (Bound i) =
Bound (if i < l orelse i > u then i else if i = u then l else i + 1)
| renumber l u (s $ t) = renumber l u s $ renumber l u t
| renumber l u (Abs (x, T, t)) = Abs (x, T, renumber (l + 1) (u + 1) t)
| renumber _ _ atom = atom;
fun quantify qC x T xs P =
let
fun quant [] P = P
| quant ((qC, x, T) :: xs) P = quant xs (qC $ Abs (x, T, P));
val n = length xs;
val Q = if n = 0 then P else renumber 0 n P;
in if Data.rotate then quant xs (qC $ Abs (x, T, Q)) else qC $ Abs (x, T, quant xs P) end;
fun rearrange_all ctxt ct =
(case Thm.term_of ct of
F as (all as Const (q, _)) $ Abs (x, T, P) =>
(case extract_quant extract_imp q P of
NONE => NONE
| SOME (xs, eq, Q) =>
let val R = quantify all x T xs (Data.imp $ eq $ Q)
in SOME (prove_conv ctxt (F, R) prove_one_point_all_tac) end)
| _ => NONE);
fun rotate_all ctxt ct =
let
fun extract fst xs P =
if fst then NONE else SOME (xs, P, P)
in
(case strip_prop (Thm.term_of ct) of
F as (ex as Const (q, _)) $ Abs (x, T, P) =>
(case extract_quant extract q P of
NONE => NONE
| SOME (xs, _, Q) =>
let val R = quantify ex x T xs Q
in SOME (prove_conv ctxt (F, R) prove_one_point_all_tac2) end)
| _ => NONE) end;
fun rearrange_ball tac ctxt ct =
(case Thm.term_of ct of
F as Ball $ A $ Abs (x, T, P) =>
(case extract_imp true [] P of
NONE => NONE
| SOME (xs, eq, Q) =>
if not (null xs) then NONE
else
let val R = Data.imp $ eq $ Q
in SOME (prove_conv ctxt (F, Ball $ A $ Abs (x, T, R)) tac) end)
| _ => NONE);
fun rearrange_ex' ctxt trm =
(case strip_prop trm of
F as (ex as Const (q, _)) $ Abs (x, T, P) =>
(case extract_quant (extract_conj []) q P of
NONE => NONE
| SOME (xs, eq, Q) =>
let val R = quantify ex x T xs (Data.conj $ eq $ Q)
in SOME (prove_conv ctxt (F, R) prove_one_point_ex_tac) end)
| _ => NONE);
fun rearrange_ex ctxt = rearrange_ex' ctxt o Thm.term_of
fun rotate_ex ctxt ct =
let
fun extract fst xs P =
if fst then NONE else SOME (xs, P, P)
in
(case strip_prop (Thm.term_of ct) of
F as (ex as Const (q, _)) $ Abs (x, T, P) =>
(case extract_quant extract q P of
NONE => NONE
| SOME (xs, _, Q) =>
let val R = quantify ex x T xs Q
in SOME (prove_conv ctxt (F, R) prove_one_point_ex_tac) end)
| _ => NONE) end;
fun miniscope_ex ctxt ct =
let
fun extract fst xs t =
case Data.dest_conj t of
SOME (P, _) => if Data.move xs P [] andalso not fst then SOME (xs, t, t) else NONE
| NONE => NONE
in
(case strip_prop (Thm.term_of ct) of
F as (ex as Const (q, _)) $ Abs (x, T, P) =>
(case extract_quant extract q P of
NONE => NONE
| SOME (xs, _, Q) =>
let val R = quantify ex x T xs Q
in SOME (prove_conv ctxt (F, R) prove_one_point_ex_tac) end)
| _ => NONE) end;
fun rearrange_bex tac ctxt ct =
(case Thm.term_of ct of
F as Bex $ A $ Abs (x, T, P) =>
(case extract_conj [] true [] P of
NONE => NONE
| SOME (xs, eq, Q) =>
if not (null xs) then NONE
else SOME (prove_conv ctxt (F, Bex $ A $ Abs (x, T, Data.conj $ eq $ Q)) tac))
| _ => NONE);
fun rearrange_Collect tac ctxt ct =
(case Thm.term_of ct of
F as Collect $ Abs (x, T, P) =>
(case extract_conj [] true [] P of
NONE => NONE
| SOME (_, eq, Q) =>
let val R = Collect $ Abs (x, T, Data.conj $ eq $ Q)
in SOME (prove_conv ctxt (F, R) tac) end)
| _ => NONE);
end;
structure Quantifier1 = Quantifier
(
(*abstract syntax*)
fun dest_eq (Const(@{const_name HOL.eq},_) $ s $ t) = SOME (s, t)
| dest_eq _ = NONE;
fun dest_conj (Const(@{const_name HOL.conj},_) $ s $ t) = SOME (s, t)
| dest_conj _ = NONE;
fun dest_imp (Const(@{const_name HOL.implies},_) $ s $ t) = SOME (s, t)
| dest_imp _ = NONE;
val conj = HOLogic.conj
val imp = HOLogic.imp
fun move xs eq _ =
(case dest_eq eq of
SOME (s, t) =>
let val n = length xs in
s = Bound n andalso not (loose_bvar1 (t, n)) orelse
t = Bound n andalso not (loose_bvar1 (s, n))
end
| NONE => false);
val force_move = true
val rotate = true
(*rules*)
val iff_reflection = @{thm eq_reflection}
val iffI = @{thm iffI}
val iff_trans = @{thm trans}
val conjI= @{thm conjI}
val conjE= @{thm conjE}
val impI = @{thm impI}
val mp = @{thm mp}
val uncurry = @{thm uncurry}
val exI = @{thm exI}
val exE = @{thm exE}
val iff_allI = @{thm iff_allI}
val iff_exI = @{thm iff_exI}
val all_comm = @{thm all_comm}
val ex_comm = @{thm ex_comm}
);
(* loose_bvar2(t,k) iff t contains a 'loose' bound variable referring to
a level below k. *)
fun loose_bvar2(Bound i,k) = i < k
| loose_bvar2(f$t, k) = loose_bvar2(f,k) orelse loose_bvar2(t,k)
| loose_bvar2(Abs(_,_,t),k) = loose_bvar2(t,k+1)
| loose_bvar2 _ = false;
structure Quantifier2 = Quantifier
(
(*abstract syntax*)
fun dest_eq (Const(@{const_name HOL.eq},_) $ s $ t) = SOME (s, t)
| dest_eq _ = NONE;
fun dest_conj (Const(@{const_name HOL.conj},_) $ s $ t) = SOME (s, t)
| dest_conj _ = NONE;
fun dest_imp (Const(@{const_name HOL.implies},_) $ s $ t) = SOME (s, t)
| dest_imp _ = NONE;
val conj = HOLogic.conj
val imp = HOLogic.imp
fun move xs t _ =
let val n = length xs in
loose_bvar1 (t, n) andalso not (loose_bvar2 (t, n))
end
val force_move = false
val rotate = false
(*rules*)
val iff_reflection = @{thm eq_reflection}
val iffI = @{thm iffI}
val iff_trans = @{thm trans}
val conjI= @{thm conjI}
val conjE= @{thm conjE}
val impI = @{thm impI}
val mp = @{thm mp}
val uncurry = @{thm uncurry}
val exI = @{thm exI}
val exE = @{thm exE}
val iff_allI = @{thm iff_allI}
val iff_exI = @{thm iff_exI}
val all_comm = @{thm all_comm}
val ex_comm = @{thm ex_comm}
);
structure Quantifier3 = Quantifier
(
(*abstract syntax*)
fun dest_eq (Const(@{const_name HOL.eq},_) $ s $ t) = SOME (s, t)
| dest_eq _ = NONE;
fun dest_conj (Const(@{const_name HOL.conj},_) $ s $ t) = SOME (s, t)
| dest_conj _ = NONE;
fun dest_imp (Const(@{const_name HOL.implies},_) $ s $ t) = SOME (s, t)
| dest_imp _ = NONE;
val conj = HOLogic.conj
val imp = HOLogic.imp
fun move xs t _ =
let val n = length xs in
loose_bvar1 (t, n) andalso not (loose_bvar (t, n + 1))
end
val force_move = false
val rotate = false
(*rules*)
val iff_reflection = @{thm eq_reflection}
val iffI = @{thm iffI}
val iff_trans = @{thm trans}
val conjI= @{thm conjI}
val conjE= @{thm conjE}
val impI = @{thm impI}
val mp = @{thm mp}
val uncurry = @{thm uncurry}
val exI = @{thm exI}
val exE = @{thm exE}
val iff_allI = @{thm iff_allI}
val iff_exI = @{thm iff_exI}
val all_comm = @{thm all_comm}
val ex_comm = @{thm ex_comm}
);
signature Int_Param =
sig
val x : int
end;
fun is_conj (Const(@{const_name HOL.conj},_) $ _ $ _) = true
| is_conj _ = false;
functor Quantifier4 (to_move: Int_Param) = Quantifier
(
(*abstract syntax*)
fun dest_eq (Const(@{const_name HOL.eq},_) $ s $ t) = SOME (s, t)
| dest_eq _ = NONE;
fun dest_conj (Const(@{const_name HOL.conj},_) $ s $ t) = SOME (s, t)
| dest_conj _ = NONE;
fun dest_imp (Const(@{const_name HOL.implies},_) $ s $ t) = SOME (s, t)
| dest_imp _ = NONE;
val conj = HOLogic.conj
val imp = HOLogic.imp
fun move _ P trms = length trms + 1 = to_move.x andalso not (is_conj P)
val force_move = true
val rotate = false
(*rules*)
val iff_reflection = @{thm eq_reflection}
val iffI = @{thm iffI}
val iff_trans = @{thm trans}
val conjI= @{thm conjI}
val conjE= @{thm conjE}
val impI = @{thm impI}
val mp = @{thm mp}
val uncurry = @{thm uncurry}
val exI = @{thm exI}
val exE = @{thm exE}
val iff_allI = @{thm iff_allI}
val iff_exI = @{thm iff_exI}
val all_comm = @{thm all_comm}
val ex_comm = @{thm ex_comm}
);
structure Quantifier5 = Quantifier
(
(*abstract syntax*)
fun dest_eq (Const(@{const_name HOL.eq},_) $ s $ t) = SOME (s, t)
| dest_eq _ = NONE;
fun dest_conj (Const(@{const_name HOL.conj},_) $ s $ t) = SOME (s, t)
| dest_conj _ = NONE;
fun dest_imp (Const(@{const_name HOL.implies},_) $ s $ t) = SOME (s, t)
| dest_imp _ = NONE;
val conj = HOLogic.conj
val imp = HOLogic.imp
fun move _ t _ = is_conj t
val force_move = true
val rotate = false
(*rules*)
val iff_reflection = @{thm eq_reflection}
val iffI = @{thm iffI}
val iff_trans = @{thm trans}
val conjI= @{thm conjI}
val conjE= @{thm conjE}
val impI = @{thm impI}
val mp = @{thm mp}
val uncurry = @{thm uncurry}
val exI = @{thm exI}
val exE = @{thm exE}
val iff_allI = @{thm iff_allI}
val iff_exI = @{thm iff_exI}
val all_comm = @{thm all_comm}
val ex_comm = @{thm ex_comm}
);
structure Quantifier6 = Quantifier
(
(*abstract syntax*)
fun dest_eq (Const(@{const_name HOL.eq},_) $ s $ t) = SOME (s, t)
| dest_eq _ = NONE;
fun dest_conj (Const(@{const_name HOL.conj},_) $ s $ t) = SOME (s, t)
| dest_conj _ = NONE;
fun dest_imp (Const(@{const_name HOL.implies},_) $ s $ t) = SOME (s, t)
| dest_imp _ = NONE;
val conj = HOLogic.conj
val imp = HOLogic.imp
fun move xs t _ =
let val n = length xs in
not (loose_bvar1 (t, n))
end
val force_move = true
val rotate = true
(*rules*)
val iff_reflection = @{thm eq_reflection}
val iffI = @{thm iffI}
val iff_trans = @{thm trans}
val conjI= @{thm conjI}
val conjE= @{thm conjE}
val impI = @{thm impI}
val mp = @{thm mp}
val uncurry = @{thm uncurry}
val exI = @{thm exI}
val exE = @{thm exE}
val iff_allI = @{thm iff_allI}
val iff_exI = @{thm iff_exI}
val all_comm = @{thm all_comm}
val ex_comm = @{thm ex_comm}
);
structure Quantifier7 = Quantifier
(
(*abstract syntax*)
fun dest_eq (Const(@{const_name HOL.eq},_) $ s $ t) = SOME (s, t)
| dest_eq _ = NONE;
fun dest_conj (Const(@{const_name HOL.conj},_) $ s $ t) = SOME (s, t)
| dest_conj _ = NONE;
fun dest_imp (Const(@{const_name HOL.implies},_) $ s $ t) = SOME (s, t)
| dest_imp _ = NONE;
val conj = HOLogic.conj
val imp = HOLogic.imp
fun move xs t _ =
let val n = length xs in
not (loose_bvar1 (t, n))
end
val force_move = true
val rotate = true
(*rules*)
val iff_reflection = @{thm eq_reflection}
val iffI = @{thm iffI}
val iff_trans = @{thm trans}
val conjI= @{thm conjI}
val conjE= @{thm conjE}
val impI = @{thm impI}
val mp = @{thm mp}
val uncurry = @{thm uncurry}
val exI = @{thm exI}
val exE = @{thm exE}
val iff_allI = @{thm iff_allI}
val iff_exI = @{thm iff_exI}
val all_comm = @{thm all_comm}
val ex_comm = @{thm ex_comm}
);
\<close>
ML_val \<open>Quantifier1.rearrange_ex @{context} @{cterm "\<exists> a c. c < n \<and> a \<in> A"}\<close>
ML_val \<open>Quantifier1.rearrange_ex @{context} @{cterm "\<exists> a c. c < n \<and> a = b"}\<close>
ML_val \<open>Quantifier2.rearrange_ex @{context} @{cterm "\<exists> a c. a = b \<and> c < n"}\<close>
ML_val \<open>Quantifier1.rearrange_ex @{context} @{cterm "\<exists> a c. a = b \<and> c < n"}\<close>
ML_val \<open>Quantifier2.rearrange_ex @{context} @{cterm "\<exists> a c. c < n \<and> a = b"}\<close>
ML_val \<open>Quantifier2.rearrange_ex @{context} @{cterm "\<exists> a c. a < n \<and> a = b"}\<close>
ML_val \<open>Quantifier2.rearrange_ex @{context} @{cterm "\<exists> a c. a < n \<and> c < n \<and> a = b"}\<close>
ML_val \<open>Quantifier2.rearrange_ex @{context} @{cterm "\<exists> a c. c < n \<and> a > c"}\<close>
ML_val \<open>Quantifier3.rearrange_ex @{context} @{cterm "\<exists> a c. c < n \<and> a > c"}\<close>
ML_val \<open>Quantifier2.rearrange_ex @{context} @{cterm "\<exists> a c. c < n \<and> a > b"}\<close>
ML_val \<open>Quantifier2.rearrange_ex @{context} @{cterm "\<exists> a c. c < n \<and> (P a c \<and> a > b) \<and> Q c"}\<close>
ML_val \<open>Quantifier2.rearrange_ex @{context} @{cterm "finite {(a, c) | a c. c < n \<and> a \<in> A}"}\<close>
ML_val \<open>Quantifier2.rearrange_ex @{context} @{cterm "finite {t. \<exists> a c. a \<in> A \<and> c < n \<and> t = (a,c)}"}\<close>
ML_val \<open>Quantifier1.rotate_ex @{context} @{cterm "\<exists> a c. c < n \<and> a > b"}\<close>
ML_val \<open>Quantifier1.rotate_ex @{context} @{cterm "\<exists> a c d. c < n \<and> a > b \<and> P d"}\<close>
ML_val \<open>Quantifier1.rearrange_ex @{context} @{cterm "\<exists> a c. a < n \<and> c = b"}\<close>
ML_val \<open>Quantifier1.rearrange_ex @{context} @{cterm "\<forall> a. \<exists> c. a < n \<and> c = b"}\<close>
ML_val \<open>Quantifier1.rearrange_ex @{context} @{cterm "\<forall> a c. a < n \<and> c = b"}\<close>
ML_val \<open>Quantifier6.rearrange_ex @{context} @{cterm "\<exists> a b c. a < n \<and> b < 3 \<and> b > c"}\<close>
ML_val \<open>Quantifier6.rearrange_ex @{context} @{cterm "\<exists>b c. a < n \<and> b < 3 \<and> b > c"}\<close>
ML_val \<open>Quantifier7.miniscope_ex @{context} @{cterm "\<exists> a b c. a < n \<and> b < 3 \<and> b > c"}\<close>
ML_val \<open>Quantifier7.miniscope_ex @{context} @{cterm "\<exists>b c. a < n \<and> b < 3 \<and> b > c"}\<close>
simproc_setup ex_reorder ("\<exists>x. P x") = \<open>fn _ => Quantifier2.rearrange_ex\<close>
declare [[simproc del: ex_reorder]]
simproc_setup ex_reorder2 ("\<exists>x. P x") = \<open>fn _ => Quantifier3.rearrange_ex\<close>
declare [[simproc del: ex_reorder2]]
simproc_setup ex_reorder3 ("\<exists>x. P x") = \<open>fn _ => Quantifier6.rearrange_ex\<close>
declare [[simproc del: ex_reorder3]]
simproc_setup ex_reorder4 ("\<exists>x. P x") = \<open>fn _ => Quantifier7.miniscope_ex\<close>
declare [[simproc del: ex_reorder4]]
ML_val \<open>@{term "\<exists> a c. c < n \<and> a \<in> A"}\<close>
ML_val \<open>@{term "finite {(a, c). c < n \<and> a \<in> A}"}\<close>
ML_val \<open>@{term "finite {(a, c) | a c. c < n \<and> a \<in> A}"}\<close>
lemma
fixes n :: nat
assumes A: "finite A"
shows "finite {(a, c). c < n \<and> a \<in> A}"
using assms
using [[simproc add: finite_Collect]]
by simp
lemma
fixes n :: nat
assumes A: "finite A"
shows "finite {(a, c) | a c. c < n \<and> a \<in> A}"
using assms
using [[simproc add: ex_reorder]]
by simp
lemma
fixes n :: nat
assumes A: "finite A"
shows "finite {t. \<exists> a c. a \<in> A \<and> c < n \<and> t = (a,c)}"
apply simp
using assms apply simp
oops
lemma
fixes n :: nat
assumes A: "finite A"
shows "finite {t. \<exists> a c. (t = (a,c) \<and> c < n) \<and> a \<in> A}"
using [[simproc add: ex_reorder]]
using [[simp_trace]] apply simp
using assms by simp
lemma
fixes n :: nat
assumes A: "finite A"
shows "finite {t. \<exists> a c. (t = (a,c) \<and> a \<in> A) \<and> c < n}"
using [[simp_trace]] apply (simp del: Product_Type.Collect_case_prod)
using assms by simp
lemma
fixes n :: nat
assumes A: "finite A"
shows "finite {t. \<exists> a c. (a \<in> A \<and> t = (a,c)) \<and> c < n}"
using [[simp_trace]] apply simp
using assms by simp
lemma
fixes n :: nat
assumes A: "finite A"
shows "finite {t. \<exists> a c. a \<in> A \<and> c < n \<and> t = (a,c)}"
using [[simp_trace]] apply simp
using assms by simp
lemma
assumes A: "finite A"
shows "finite {t. \<exists> a c. a \<in> A \<and> P c \<and> t = (a,c)}"
using [[simp_trace]] apply simp
using assms apply simp
oops
ML \<open>
fun rotate_quant reorder_thm n ctxt =
let
fun subst j =
if j > n then K all_tac else
(
EqSubst.eqsubst_tac ctxt [j] [reorder_thm]
) THEN' subst (j + 1)
in subst 1 end;
\<close>
ML_val Pretty.string_of
ML_val Syntax.pretty_term
ML \<open>
fun rotate_ex_tac ctxt =
let
fun foc_tac {concl, ...} =
case Quantifier1.rotate_ex ctxt concl of
NONE => no_tac
| SOME thm => rewrite_goals_tac ctxt [thm]
in
Subgoal.FOCUS foc_tac ctxt
end;
\<close>
ML \<open>
fun rotate_all_tac ctxt =
let
fun foc_tac {concl, ...} =
case Quantifier1.rotate_all ctxt concl of
NONE => no_tac
| SOME thm => rewrite_goals_tac ctxt [thm]
in
Subgoal.FOCUS foc_tac ctxt
end;
\<close>
ML \<open>
fun rearrange_ex_tac ctxt =
let
fun foc_tac {concl, ...} =
case Quantifier2.rearrange_ex ctxt concl of
NONE => no_tac
| SOME thm => rewrite_goals_tac ctxt [thm]
in
Subgoal.FOCUS foc_tac ctxt
end;
\<close>
ML \<open>
fun rearrange_ex_tac2 ctxt =
let
fun foc_tac {concl, ...} =
case Quantifier3.rearrange_ex ctxt concl of
NONE => no_tac
| SOME thm => rewrite_goals_tac ctxt [thm]
in
Subgoal.FOCUS foc_tac ctxt
end;
\<close>
(* XXX How to do this? *)
(*
ML \<open>
fun rearrange_ex_tac2 n ctxt =
let
struct Quant = Quantifier4(val x = n);
fun foc_tac {concl, ...} =
case Quantifier4.rearrange_ex ctxt concl of
NONE => no_tac
| SOME thm => rewrite_goals_tac ctxt [thm]
in
Subgoal.FOCUS foc_tac ctxt
end;
\<close>
*)
ML_val Abs
ML_val Conv.rewr_conv
ML \<open>
fun strip_fin (Const (@{const_name "finite"}, _) $ (Const (@{const_name "Collect"}, _) $ t)) = t
| strip_fin t = t
fun wrap_fin tac ctxt = tac ctxt o strip_fin
structure Quant2 = Quantifier4(val x = 2);
structure Quant3 = Quantifier4(val x = 3);
structure Quant4 = Quantifier4(val x = 4);
structure Quant5 = Quantifier4(val x = 5);
fun rearrange_ex_fixed_n rearrange_n ctxt =
let
fun foc_tac {concl, ...} =
case rearrange_n ctxt concl of
NONE => no_tac
| SOME thm => rewrite_goals_tac ctxt [thm, @{thm HOL.conj_assoc} RS @{thm HOL.eq_reflection}]
in
Subgoal.FOCUS foc_tac ctxt
end;
val rearrange_ex_fixed_2 = rearrange_ex_fixed_n Quant2.rearrange_ex;
val rearrange_ex_fixed_3 = rearrange_ex_fixed_n Quant3.rearrange_ex;
val rearrange_ex_fixed_4 = rearrange_ex_fixed_n Quant4.rearrange_ex;
val rearrange_ex_fixed_5 = rearrange_ex_fixed_n Quant5.rearrange_ex;
(* val defer_ex = rearrange_ex_fixed_n (wrap_fin Quantifier5.rearrange_ex); *)
fun CONV conv ctxt =
let
fun foc_tac {concl, ...} =
rewrite_goals_tac ctxt [conv ctxt concl]
in
Subgoal.FOCUS foc_tac ctxt
end;
fun mk_conv f ctxt ct =
case (f ctxt ct) of
SOME thm => thm
| _ => raise CTERM ("no conversion", [])
fun success_conv cv ct =
let
val eq = cv ct
in
if Thm.is_reflexive eq then raise CTERM ("no conversion", []) else eq
end
fun mk_conv' f ctxt ct = the_default (Thm.reflexive ct) (f ctxt ct)
val assoc_conv = Conv.rewr_conv (@{thm HOL.conj_assoc} RS @{thm HOL.eq_reflection})
val comm_conv = Conv.rewr_conv (@{thm HOL.conj_commute} RS @{thm HOL.eq_reflection})
fun wrap_conv f ctxt =
success_conv (
Conv.top_sweep_conv (fn ctxt => mk_conv f ctxt then_conv Conv.repeat_conv assoc_conv) ctxt
)
fun mk_tac conv ctxt = CONVERSION (Conv.concl_conv ~1 (Object_Logic.judgment_conv ctxt (conv ctxt)))
val defer_conv = mk_conv Quantifier5.rearrange_ex
val conv = wrap_conv Quantifier5.rearrange_ex
fun defer_ex_tac ctxt = CONVERSION (Conv.params_conv ~1 (fn ctxt => Conv.concl_conv ~1 (conv ctxt)) ctxt)
val defer_ex_tac = CONV conv
fun defer_ex_tac ctxt i =
CHANGED (mk_tac (fn ctxt => wrap_conv Quantifier5.rearrange_ex ctxt else_conv Conv.top_sweep_conv (K comm_conv) ctxt) ctxt i)
val mini_ex_tac = mk_tac (wrap_conv Quantifier6.rearrange_ex)
val mini_ex_tac2 = mk_tac (wrap_conv Quantifier7.miniscope_ex)
val rearrange_ex_fixed_2 = mk_tac (wrap_conv Quant2.rearrange_ex);
val rearrange_ex_fixed_3 = mk_tac (wrap_conv Quant3.rearrange_ex);
val rearrange_ex_fixed_4 = mk_tac (wrap_conv Quant4.rearrange_ex);
val rearrange_ex_fixed_5 = mk_tac (wrap_conv Quant5.rearrange_ex);
\<close>
ML_val Object_Logic.judgment_conv
ML_val \<open>defer_conv @{context} @{cterm "\<exists> a b c d. a < 1 \<and> b < 2 \<and> c < 3 \<and> d < 4"}\<close>
ML_val \<open>assoc_conv @{cterm "(a < 1 \<and> b < 2) \<and> c < 3 \<and> d < 4"}\<close>
ML_val \<open>Conv.binder_conv (K assoc_conv) @{context} @{cterm "\<exists> a. (a < 1 \<and> b < 2) \<and> c < 3 \<and> d < 4"}\<close>
ML_val \<open>Conv.top_sweep_conv (K assoc_conv) @{context}
@{cterm "\<exists> a b c d. (a < 1 \<and> b < 2) \<and> c < 3 \<and> d < 4"}\<close>
ML_val \<open>Conv.bottom_conv (K (Conv.try_conv assoc_conv)) @{context}
@{cterm "\<exists> a b c d. (a < 1 \<and> b < 2) \<and> c < 3 \<and> d < 4"}\<close>
ML_val \<open>Conv.every_conv [defer_conv @{context}, Conv.try_conv assoc_conv]
@{cterm "\<exists> a b c d. a < 1 \<and> b < 2 \<and> c < 3 \<and> d < 4"}\<close>
ML_val \<open>conv @{context} @{cterm "\<exists> a b c d. a < 1 \<and> b < 2 \<and> c < 3 \<and> d < 4"}\<close>
ML_val \<open>conv @{context} @{cterm "finite {t. \<exists> a b c d. a < 1 \<and> b < 2 \<and> c < 3 \<and> d < 4}"}\<close>
ML_val \<open>conv @{context} @{cterm "\<exists>a b c d. d = 4 \<and> c = 3 \<and> b < 2 \<and> a < 1"}\<close>
ML_val \<open>CONVERSION (Conv.concl_conv ~1 (conv @{context}))\<close>
ML_val Conv.concl_conv
ML_val \<open>Quantifier1.rotate_ex @{context} @{cterm "\<exists> a b c d. a < 1 \<and> b < 2 \<and> c < 3 \<and> d < 4"}\<close>
ML_val \<open>Quantifier1.rotate_all @{context} @{cterm "\<forall> a b c d. a < 1 \<and> b < 2 \<and> c < 3 \<and> d < 4"}\<close>
lemma
"\<forall> a b c d. a < 1 \<and> b < 2 \<and> c < 3 \<and> d < 4"
apply (tactic \<open>rotate_all_tac @{context} 1\<close>)
apply (tactic \<open>rotate_all_tac @{context} 1\<close>)
apply (tactic \<open>rotate_all_tac @{context} 1\<close>)
apply (tactic \<open>rotate_all_tac @{context} 1\<close>)
apply (tactic \<open>rotate_all_tac @{context} 1\<close>)
oops
lemmas a = HOL.refl[THEN eq_reflection]
lemmas b = enum_the_def[THEN eq_reflection]
ML_val \<open>Thm.is_reflexive @{thm a}\<close>
ML_val \<open>Thm.is_reflexive @{thm b}\<close>
lemma
"\<exists> a b c d. a < 1 \<and> b < 2 \<and> c = 3 \<and> d = 4"
apply (tactic \<open>rearrange_ex_fixed_2 @{context} 1\<close>)
apply (tactic \<open>rearrange_ex_fixed_3 @{context} 1\<close>)
apply (tactic \<open>rearrange_ex_fixed_4 @{context} 1\<close>)
apply (tactic \<open>defer_ex_tac @{context} 1\<close>)
apply (subst conj_assoc)+
oops
ML_val \<open>@{const_name finite}\<close>
ML_val \<open>@{const_name Collect}\<close>
ML_val \<open>strip_fin @{term \<open>finite {t. \<exists>a b c d. a < 1 \<and> b < 2 \<and> c = 3 \<and> d = 4}\<close>}\<close>
lemma
"finite {t. \<exists> a b c d. a < 1 \<and> b < 2 \<and> c = 3 \<and> d = 4}"
apply (tactic \<open>rearrange_ex_fixed_2 @{context} 1\<close>)
apply (tactic \<open>rearrange_ex_fixed_3 @{context} 1\<close>)
apply (tactic \<open>rearrange_ex_fixed_4 @{context} 1\<close>)
apply (tactic \<open>defer_ex_tac @{context} 1\<close>)
oops
lemma
"finite S \<Longrightarrow> finite {t. \<exists> a b c d. a < 1 \<and> b < 2 \<and> c = 3 \<and> d = 4}"
apply (tactic \<open>rearrange_ex_fixed_2 @{context} 1\<close>)
apply (tactic \<open>rearrange_ex_fixed_3 @{context} 1\<close>)
apply (tactic \<open>rearrange_ex_fixed_4 @{context} 1\<close>)
apply (tactic \<open>defer_ex_tac @{context} 1\<close>, simp only: conj_assoc)
oops
lemma
"finite S \<Longrightarrow> finite {t. \<exists> a b c d. P a b d \<and> c > 3}"
apply (tactic \<open>defer_ex_tac @{context} 1\<close>)
apply (tactic \<open>mini_ex_tac @{context} 1\<close>)
apply (simp only: ex_simps)
oops
lemma
"\<exists> a b c d. d < 4 \<and> a < 1 \<and> b < 2 \<and> c < 3 \<and> d < 4"
using [[simproc add: ex_reorder3]]
apply simp
oops
lemma
"\<exists> a b c d. d < 4 \<and> a < 1 \<and> b < 2 \<and> c < 3 \<and> d < 4"
using [[simproc add: ex_reorder4]]
apply simp
oops
lemma
"\<exists> a b c d. d < 4 \<and> a < 1 \<and> b < 2 \<and> c < 3 \<and> d < 4"
apply (tactic \<open>mini_ex_tac @{context} 1\<close>)
apply simp
apply (tactic \<open>mini_ex_tac @{context} 1\<close>)
apply simp
apply (tactic \<open>mini_ex_tac @{context} 1\<close>)
apply simp
apply (tactic \<open>mini_ex_tac @{context} 1\<close>)
apply simp
apply (tactic \<open>mini_ex_tac @{context} 1\<close>)
apply simp
apply (tactic \<open>mini_ex_tac @{context} 1\<close>)
apply simp
oops
lemma
"\<exists> a b c d. d < 4 \<and> a < 1 \<and> b < 2 \<and> c < 3 \<and> d < 4"
apply (tactic \<open>mini_ex_tac2 @{context} 1\<close>)
apply simp
apply (tactic \<open>mini_ex_tac2 @{context} 1\<close>)
apply simp
apply (tactic \<open>mini_ex_tac2 @{context} 1\<close>)
apply simp
apply (tactic \<open>mini_ex_tac @{context} 1\<close>)
apply simp
apply (tactic \<open>mini_ex_tac @{context} 1\<close>)
apply simp
apply (tactic \<open>mini_ex_tac @{context} 1\<close>)
apply simp
oops
lemma
"\<exists> a c b d. d < 4 \<and> a < 1 \<and> b < 2 \<and> c < 3"
apply simp
oops
lemma
"\<exists> a b c d. a < 1 \<and> b < 2 \<and> c < 3 \<and> d < 4"
apply (tactic \<open>rotate_ex_tac @{context} 1\<close>)
apply (tactic \<open>rotate_ex_tac @{context} 1\<close>)
apply (tactic \<open>rotate_ex_tac @{context} 1\<close>)
apply (tactic \<open>rotate_ex_tac @{context} 1\<close>)
apply (tactic \<open>rotate_ex_tac @{context} 1\<close>)
oops
lemma
"\<exists> a b c d. b < 2 \<and> c < 3 \<and> d < 4 \<and> a < 1"
apply (tactic \<open>rearrange_ex_tac @{context} 1\<close>)
oops
lemma
"\<exists> a b c d. b < 2 \<and> c < 3 \<and> d < 4 \<and> a < c"
apply (tactic \<open>rearrange_ex_tac2 @{context} 1\<close>; simp only: conj_assoc)+
oops
lemma
"\<exists> a b c d. b < 2 \<and> c < 3 \<and> d < 4 \<and> a < c"
apply (tactic \<open>rearrange_ex_tac2 @{context} 1\<close>; simp del: ex_simps)+
oops
lemma
"\<exists> a b c d. b < 2 \<and> c < 3 \<and> d < 4 \<and> a < c"
apply (tactic \<open>rearrange_ex_tac2 @{context} 1\<close>)
apply (simp del: ex_simps)
apply (tactic \<open>rearrange_ex_tac2 @{context} 1\<close>)
apply (simp del: ex_simps)
apply (tactic \<open>rearrange_ex_tac2 @{context} 1\<close>)
using [[simp_trace]]
apply (simp del: ex_simps)
oops
lemma finite_Collect_bounded_ex_4:
assumes "finite {(a,b,c,d) . P a b c d}"
shows
"finite {x. \<exists>a b c d. P a b c d \<and> Q x a b c d}
\<longleftrightarrow> (\<forall> a b c d. P a b c d \<longrightarrow> finite {x. Q x a b c d})"
proof -
have *:
"{x. \<exists>a b c d. P a b c d \<and> Q x a b c d}
= {x. \<exists> t. t \<in> {(a,b,c,d). P a b c d} \<and> (\<exists>a b c d. t = (a, b, c, d) \<and> Q x a b c d)}"
by simp
show ?thesis apply (subst *)
apply (subst finite_Collect_bounded_ex)
using assms by simp+
oops
lemma finite_Collect_bounded_ex_4':
assumes "finite {(a,b,c,d) | a b c d. P a b c d}"
shows
"finite {x. \<exists>a b c d. P a b c d \<and> Q x a b c d}
\<longleftrightarrow> (\<forall> a b c d. P a b c d \<longrightarrow> finite {x. Q x a b c d})"
proof -
have *:
"{x. \<exists>a b c d. P a b c d \<and> Q x a b c d}
= {x. \<exists> t. t \<in> {(a,b,c,d) | a b c d. P a b c d} \<and> (\<exists>a b c d. t = (a, b, c, d) \<and> Q x a b c d)}"
by simp
show ?thesis apply (subst *)
apply (subst finite_Collect_bounded_ex)
using assms by simp+
qed
lemma finite_Collect_bounded_ex_2 [simp]:
assumes "finite {(a,b). P a b}"
shows
"finite {x. \<exists>a b. P a b \<and> Q x a b}
\<longleftrightarrow> (\<forall> a b. P a b \<longrightarrow> finite {x. Q x a b})"
using assms finite_Collect_bounded_ex[OF assms, where Q = "\<lambda> x. \<lambda> (a, b). Q x a b"]
by clarsimp (* force, simp *)
lemma finite_Collect_bounded_ex_3 [simp]:
assumes "finite {(a,b,c) . P a b c}"
shows
"finite {x. \<exists>a b c. P a b c \<and> Q x a b c}
\<longleftrightarrow> (\<forall> a b c. P a b c \<longrightarrow> finite {x. Q x a b c})"