forked from jrh13/hol-light
-
Notifications
You must be signed in to change notification settings - Fork 2
/
impconv.ml
1862 lines (1670 loc) · 65.8 KB
/
impconv.ml
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
(* ========================================================================= *)
(* Implicational conversions, implicational rewriting and target rewriting. *)
(* *)
(* (c) Copyright, Vincent Aravantinos, 2012-2013 *)
(* Analysis and Design of Dependable Systems *)
(* fortiss GmbH, Munich, Germany *)
(* *)
(* Formerly: Hardware Verification Group, *)
(* Concordia University *)
(* *)
(* Contact: <vincent.aravantinos@fortiss.org> *)
(* *)
(* Distributed under the same license as HOL Light. *)
(* ========================================================================= *)
needs "quot.ml";;
(* OA: Reorganized this file b/c first-class modules.
Lots of the syntax was incompatible with out parser, and there was
an 'open' somewhere in there.
*)
module Impconv = struct
(* Same as [UNDISCH] but also returns the undischarged term *)
let UNDISCH_TERM th =
let p = (fst o dest_imp o concl) th in
p,UNDISCH th ;;
(* Same as [UNDISCH_ALL] but also returns the undischarged terms *)
let rec UNDISCH_TERMS th =
try
let t,th' = UNDISCH_TERM th in
let ts,th'' = UNDISCH_TERMS th' in
t::ts,th''
with Failure _ -> [],th ;;
(* Comblies the function [f] to the conclusion of an implicational theorem. *)
let MAP_CONCLUSION f th =
let p,th = UNDISCH_TERM th in
DISCH p (f th) ;;
let strip_conj = binops `(/\)` ;;
(* For a list [f1;...;fk], returns the first [fi x] that succeeds. *)
let rec tryfind_fun fs x =
match fs with
|[] -> failwith "tryfind_fun"
|f::fs' -> try f x with Failure _ -> tryfind_fun fs' x ;;
(* Same as [mapfilter] but also provides the rank of the iteration as an
* argument to [f]. *)
let mapfilteri f =
let rec self i = function
|[] -> []
|h::t ->
let rest = self (i+1) t in
try f i h :: rest with Failure _ -> rest
in
self 0 ;;
let list_of_option = function None -> [] | Some x -> [x] ;;
let try_list f x = try f x with Failure _ -> [] ;;
(* A few constants. *)
let A_Tm = `A:bool` and B_Tm = `B:bool` and C_Tm = `C:bool` and D_Tm = `D:bool`
and T_Tm = `T:bool` ;;
(* For a term t, builds `t ==> t` *)
let IMP_REFL =
let lem = TAUT `A ==> A` in
fun t -> INST [t,A_Tm] lem ;;
(* Conversion version of [variant]:
* Given variables [v1;...;vk] to avoid and a term [t],
* returns [|- t = t'] where [t'] is the same as [t] without any use of the
* variables [v1;...;vk].
*)
let VARIANT_CONV av t =
let vs = variables t in
let mapping = filter (fun (x,y) -> x <> y) (zip vs (variants av vs)) in
DEPTH_CONV (fun u -> ALPHA_CONV (assoc (bndvar u) mapping) u) t ;;
(* Rule version of [VARIANT_CONV] *)
let VARIANT_RULE = CONV_RULE o VARIANT_CONV ;;
(* Discharges the first hypothesis of a theorem. *)
let DISCH_HD th = DISCH (hd (hyp th)) th ;;
(* Rule version of [REWR_CONV] *)
let REWR_RULE = CONV_RULE o REWR_CONV ;;
(* Given a list [A1;...;Ak] and a theorem [th],
* returns [|- A1 /\ ... /\ Ak ==> th].
*)
let DISCH_IMP_IMP =
let f = function
|[] -> I
|t::ts -> rev_itlist (fun t -> REWR_RULE IMP_IMP o DISCH t) ts o DISCH t
in
f o rev ;;
(* Given a term [A /\ B] and a theorem [th], returns [|- A ==> B ==> th]. *)
let rec DISCH_CONJ t th =
try
let t1,t2 = dest_conj t in
REWR_RULE IMP_IMP (DISCH_CONJ t1 (DISCH_CONJ t2 th))
with Failure _ -> DISCH t th ;;
(* Specializes all the universally quantified variables of a theorem,
* and returns both the theorem and the list of variables.
*)
let rec SPEC_VARS th =
try
let v,th' = SPEC_VAR th in
let vs,th'' = SPEC_VARS th' in
v::vs,th''
with Failure _ -> [],th ;;
(* Comblies the function [f] to the body of a universally quantified theorem. *)
let MAP_FORALL_BODY f th =
let vs,th = SPEC_VARS th in
GENL vs (f th) ;;
(* Given a theorem of the form [!xyz. P ==> !uvw. C] and a function [f],
* return [!xyz. P ==> !uvw. f C].
*)
let GEN_MAP_CONCLUSION = MAP_FORALL_BODY o MAP_CONCLUSION o MAP_FORALL_BODY ;;
(* Turn a theorem of the form [x ==> y /\ z] into [(x==>y) /\ (x==>z)].
* Also deals with universal quantifications if necessary
* (e.g., [x ==> !v. y /\ z] will be turned into
* [(x ==> !v. y) /\ (x ==> !v. z)])
*
* possible improvement: apply the rewrite more locally
*)
let IMPLY_AND =
let IMPLY_AND_RDISTRIB = TAUT `(x ==> y /\ z) <=> (x==>y) /\(x==>z)` in
PURE_REWRITE_RULE [GSYM AND_FORALL_THM;IMP_IMP;
RIGHT_IMP_FORALL_THM;IMPLY_AND_RDISTRIB;GSYM CONJ_ASSOC] ;;
(* Returns the two operands of a binary combination.
* Contrary to [dest_binary], does not check what is the operator.
*)
let dest_binary_blind = function
|Comb(Comb(_,l),r) -> l,r
|_ -> failwith "dest_binary_blind" ;;
let spec_all = repeat (snd o dest_forall) ;;
let thm_lt (th1:thm) th2 = Thm.(<) th1 th2 ;;
(* GMATCH_MP (U1 |- !x1...xn. H1 /\ ... /\ Hk ==> C) (U2 |- P)
* = (U1 u U2 |- !y1...ym. G1' /\ ... /\ Gl' ==> C')
* where:
* - P matches some Hi
* - C' is the result of applying the matching substitution to C
* - Gj' is the result of applying the matching substitution to Hj
* - G1',...,Gl' is the list corresponding to H1,...,Hk but without Hi
* - y1...ym are the variables among x1,...,xn that are not instantiated
*
* possible improvement: make a specific conversion,
* define a MATCH_MP that also returns the instantiated variables *)
let GMATCH_MP =
let swap = CONV_RULE (REWR_CONV (TAUT `(p==>q==>r) <=> (q==>p==>r)`)) in
fun th1 ->
let vs,th1' = SPEC_VARS th1 in
let hs,th1'' = UNDISCH_TERMS (PURE_REWRITE_RULE [IMP_CONJ] th1') in
fun th2 ->
let f h hs =
let th1''' = DISCH h th1'' in
let th1'''' =
try swap (DISCH_IMP_IMP hs th1''') with Failure _ -> th1'''
in
MATCH_MP (GENL vs th1'''') th2
in
let rec loop acc = function
|[] -> []
|h::hs ->
(try [f h (acc @ hs)] with Failure _ -> []) @ loop (h::acc) hs
in
loop [] hs ;;
let GMATCH_MPS ths1 ths2 =
let insert (y:thm) = function
|[] -> [y]
|x::_ as xs when equals_thm x y -> xs
|x::xs when thm_lt x y -> x :: insert y xs
|_::_ as xs -> y::xs
in
let inserts ys = itlist insert ys in
match ths1 with
|[] -> []
|th1::ths1' ->
let rec self acc th1 ths1 = function
|[] -> (match ths1 with [] -> acc | th::ths1' -> self acc th ths1' ths2)
|th2::ths2' -> self (inserts (GMATCH_MP th1 th2) acc) th1 ths1 ths2'
in
self [] th1 ths1' ths2 ;;
let MP_CLOSURE ths1 ths2 =
let ths1 = filter (is_imp o spec_all o concl) ths1 in
let rec self ths2 = function
|[] -> []
|_::_ as ths1 ->
let ths1'' = GMATCH_MPS ths1 ths2 in
self ths2 ths1'' @ ths1''
in
self ths2 ths1 ;;
(* Set of terms. Implemented as ordered lists. *)
module Tset = struct
(*type t = term list*) (* TODO Makes CakeML pretty printer system dizzy *)
let lift f = List.sort Term.(<) o f
let of_list = lift I
let insert ts t =
let rec self xs = match xs with
|[] -> [t]
|x::xs when Term.(<) x t -> x::self xs
|x::_ as xs when x = t -> xs
|xs -> t::xs
in
if t = T_Tm then ts else self ts
let remove ts t =
let rec self xs = match xs with
|[] -> []
|x::xs when Term.(<) x t -> x::self xs
|x::xs when x = t -> xs
|_::_ as xs -> xs
in
self ts
let strip_conj =
let rec self acc t =
try
let t1,t2 = dest_conj t in
self (self acc t1) t2
with Failure _ -> insert acc t
in
self []
let rec union l1 l2 =
match l1 with
|[] -> l2
|h1::t1 ->
match l2 with
|[] -> l1
|h2::t2 when Term.(<) h1 h2 -> h1::union t1 l2
|h2::t2 when h1 = h2 -> h1::union t1 t2
|h2::t2 -> h2::union l1 t2
let rec mem x = function
|x'::xs when x' = x -> true
|x'::xs when Term.(<) x' x -> mem x xs
|_ -> false
let subtract l1 l2 = filter (fun x -> not (mem x l2)) l1
let empty = []
let flat_revmap f =
let rec self acc = function
|[] -> acc
|x::xs -> self (union (f x) acc) xs
in
self []
let flat_map f = flat_revmap f o rev
let rec frees acc = function
|Var (_,_) as t -> insert acc t
|Const (_,_) -> acc
|Abs(v,b) -> remove (frees acc b) v
|Comb(u,v) -> frees (frees acc u) v
let freesl ts = itlist (C frees) ts empty
let frees = frees empty
end ;;
module Annot = struct
type t =
|Var_ of string * hol_type
|Const_ of string * hol_type * term
|Comb_ of t * t * hol_type
|Abs_ of t * t * hol_type
let type_of = function
|Var_(_,ty) -> ty
|Const_(_,ty,_) -> ty
|Comb_(_,_,ty) -> ty
|Abs_(_,_,ty) -> ty
let rec of_term tm = match tm with
|Var(s,ty) -> Var_(s,ty)
|Const(s,ty) as t -> Const_(s,ty,t)
|Comb(u,v) ->
let u' = of_term u and v' = of_term v in
Comb_(u',v',snd (dest_fun_ty (type_of u')))
|Abs(x,b) ->
let x' = of_term x and b' = of_term b in
Abs_(x',b',mk_fun_ty (type_of x') (type_of b'))
let rec equal t1 t2 =
match t1,t2 with
|Var_(s1,ty1),Var_(s2,ty2)
|Const_(s1,ty1,_),Const_(s2,ty2,_) -> s1 = s2 && ty1 = ty2
|Comb_(u1,v1,_),Comb_(u2,v2,_) -> equal u1 u2 && equal v1 v2
|Abs_(v1,b1,_),Abs_(v2,b2,_) -> equal v1 v2 && equal b1 b2
|_ -> false
let rec to_term tm = match tm with
|Var_(s,ty) -> mk_var(s,ty)
|Const_(_,_,t) -> t
|Comb_(u,v,_) -> mk_comb(to_term u,to_term v)
|Abs_(v,b,_) -> mk_abs(to_term v,to_term b)
let dummy = Var_("",aty)
let rec find_term p t =
if p t then t else
match t with
|Abs_(_,b,_) -> find_term p b
|Comb_(u,v,_) -> try find_term p u with Failure _ -> find_term p v
|_ -> failwith "Annot.find_term"
end ;;
(* ------------------------------------------------------------------------- *)
(* First-order matching of terms. *)
(* *)
(* Same note as in [drule.ml]: *)
(* in the event of spillover patterns, this may return false results; *)
(* but there's usually an implicit check outside that the match worked *)
(* anyway. A test could be put in (see if any "env" variables are left in *)
(* the term after abstracting out the pattern instances) but it'd be slower. *)
(* ------------------------------------------------------------------------- *)
let rec rev_map acc f xs =
match xs with
| [] -> acc
| x::xs -> rev_map (f x::acc) f xs;;
let rev_map f xs = rev_map [] f xs;;
let fo_term_match lcs p t =
let fail () = failwith "fo_term_match" in
let rec self bnds ((tenv,tyenv) as env) p t =
match p,t with
|Comb(p1,p2),Annot.Comb_(t1,t2,_) -> self bnds (self bnds env p1 t1) p2 t2
|Abs(v,p),Annot.Abs_(v',t,_) ->
let tyenv' = type_match (type_of v) (Annot.type_of v') tyenv in
self ((v',v)::bnds) (tenv,tyenv') p t
|Const(n,ty),Annot.Const_(n',ty',_) ->
if n <> n' then fail ()
else
let tyenv' = type_match ty ty' tyenv in
tenv,tyenv'
| (Var(n,ty) as v),t ->
(* Is [v] bound? *)
(try if Annot.equal t (rev_assoc v bnds) then env else fail ()
(* No *)
with Failure _ ->
if mem v lcs
then
match t with
|Annot.Var_(n',ty') when n' = n && ty' = ty -> env
|_ -> fail ()
else
let tyenv' = type_match ty (Annot.type_of t) tyenv in
let t' = try Some (rev_assoc v tenv) with Failure _ -> None in
match t' with
|Some t' -> if t = t' then tenv,tyenv' else fail ()
|None -> (t,v)::tenv,tyenv')
|_ -> fail ()
in
let tenv,tyenv = self [] ([],[]) p (Annot.of_term t) in
let inst = inst tyenv in
rev_map (fun (t,v) -> Annot.to_term t,inst v) tenv,tyenv ;;
let GEN_PART_MATCH_ALL =
let rec match_bvs t1 t2 acc =
try let v1,b1 = dest_abs t1
and v2,b2 = dest_abs t2 in
let n1 = fst(dest_var v1) and n2 = fst(dest_var v2) in
let newacc = if n1 = n2 then acc else insert (n1,n2) acc in
match_bvs b1 b2 newacc
with Failure _ -> try
let l1,r1 = dest_comb t1
and l2,r2 = dest_comb t2 in
match_bvs l1 l2 (match_bvs r1 r2 acc)
with Failure _ -> acc
in
fun partfn th ->
let sth = SPEC_ALL th in
let bod = concl sth in
let pbod = partfn bod in
let lcs = intersect (frees (concl th)) (freesl(hyp th)) in
let fvs = subtract (subtract (frees bod) (frees pbod)) lcs in
fun tm ->
let bvms = match_bvs tm pbod [] in
let abod = deep_alpha bvms bod in
let ath = EQ_MP (ALPHA bod abod) sth in
let insts,tyinsts = fo_term_match lcs (partfn abod) tm in
let eth = INSTANTIATE_ALL ([],insts,tyinsts) (GENL fvs ath) in
let fth = itlist (fun v th -> snd(SPEC_VAR th)) fvs eth in
let tm' = partfn (concl fth) in
if tm' = tm then fth else
try SUBS[ALPHA tm' tm] fth
with Failure _ -> failwith "PART_MATCH: Sanity check failure" ;;
module Fo_nets = struct
type term_label =
|Vnet of int
|Lcnet of string * int
|Cnet of string * int
|Lnet of int
type 'a t = Netnode of (term_label * 'a t) list * 'a list
let empty_net = Netnode([],[])
let enter lcs =
let label_to_store lcs t =
let op,args = strip_comb t in
let nargs = length args in
match op with
|Const(n,_) -> Cnet(n,nargs),args
|Abs(v,b) ->
let b' = if mem v lcs then vsubst [genvar(type_of v),v] b else b in
Lnet nargs,b'::args
|Var(n,_) when mem op lcs -> Lcnet(n,nargs),args
|Var(_,_) -> Vnet nargs,args
|_ -> raise Bind (* assert false *)
in
let rec net_update lcs elem (Netnode(edges,tips)) = function
|[] -> Netnode(edges,elem::tips)
|t::rts ->
let label,nts = label_to_store lcs t in
let child,others =
try (snd F_F I) (remove (fun (x,y) -> x = label) edges)
with Failure _ -> empty_net,edges in
let new_child = net_update lcs elem child (nts@rts) in
Netnode ((label,new_child)::others,tips)
in
fun (t,elem) net -> net_update lcs elem net [t]
let lookup t =
let label_for_lookup t =
let op,args = strip_comb t in
let nargs = length args in
match op with
|Const(n,_) -> Cnet(n,nargs),args
|Abs(_,b) -> Lnet nargs,b::args
|Var(n,_) -> Lcnet(n,nargs),args
|Comb (_,_) -> raise Bind (* assert false *)
in
let rec follow (Netnode(edges,tips)) = function
|[] -> tips
|t::rts ->
let label,nts = label_for_lookup t in
let collection =
try follow (assoc label edges) (nts@rts) with Failure _ -> []
in
let rec support xs = match xs with
|[] -> [0,rts]
|t::ts ->
let ((k,nts')::res') as res = support ts in
(k+1,(t::nts'))::res
in
let follows =
let f (k,nts) =
try follow (assoc (Vnet k) edges) nts with Failure _ -> []
in
map f (support nts)
in
collection @ flat follows
in
fun net -> follow net [t]
let rec filter p (Netnode(edges,tips)) =
Netnode(
List.map (fun (l,n) -> l,filter p n) edges,
List.filter p tips)
end ;;
module Variance = struct
type t = Co | Contra
let neg = function Co -> Contra | Contra -> Co
end ;;
(*****************************************************************************)
(* IMPLICATIONAL RULES *)
(* i.e., rules to build propositions based on implications rather than *)
(* equivalence. *)
(*****************************************************************************)
let MKIMP_common lem th1 th2 =
let a,b = dest_imp (concl th1) and c,d = dest_imp (concl th2) in
MP (INST [a,A_Tm;b,B_Tm;c,C_Tm;d,D_Tm] lem) (CONJ th1 th2)
(* Similar to [MK_CONJ] but theorems should be implicational instead of
* equational, i.e., conjoin both sides of two implicational theorems.
*
* More precisely: given two theorems [A ==> B] and [C ==> D],
* returns [A /\ C ==> B /\ D].
*)
let MKIMP_CONJ = MKIMP_common MONO_AND
(* Similar to [MK_DISJ] but theorems should be implicational instead of
* equational, i.e., disjoin both sides of two implicational theorems.
*
* More precisely: given two theorems [A ==> B] and [C ==> D],
* returns [A \/ C ==> B \/ D].
*)
let MKIMP_DISJ = MKIMP_common MONO_OR
let MKIMP_IFF =
let lem =
TAUT `((A ==> B) ==> (C ==> D)) /\ ((B ==> A) ==> (D ==> C)) ==> (A <=> B)
==> (C <=> D)`
in
fun th1 th2 ->
let ab,cd = dest_imp (concl th1) in
let a,b = dest_imp ab and c,d = dest_imp cd in
MP (INST [a,A_Tm;b,B_Tm;c,C_Tm;d,D_Tm] lem) (CONJ th1 th2)
(* th1 = (A ==> B) ==> C1
* th2 = (B ==> A) ==> C2
* output = (A <=> B) ==> (C1 /\ C2)
*)
let MKIMP_CONTRA_IFF =
let lem =
TAUT `((A ==> B) ==> C) /\ ((B ==> A) ==> D) ==> (A <=> B) ==> C /\ D`
in
fun th1 th2 ->
let ab,c = dest_imp (concl th1) and _,d = dest_imp (concl th2) in
let a,b = dest_imp ab in
MP (INST [a,A_Tm;b,B_Tm;c,C_Tm;d,D_Tm] lem) (CONJ th1 th2)
let MKIMPL_CONTRA_IFF =
let lem = TAUT `((A ==> B) ==> C) ==> (A <=> B) ==> C /\ (B ==> A)` in
fun th ->
let ab,c = dest_imp (concl th) in
let a,b = dest_imp ab in
MP (INST [a,A_Tm;b,B_Tm;c,C_Tm] lem) th
let MKIMPR_CONTRA_IFF =
let lem =
TAUT `((B ==> A) ==> D) ==> (A <=> B) ==> (A ==> B) /\ D`
in
fun th ->
let ba,d = dest_imp (concl th) in
let b,a = dest_imp ba in
MP (INST [a,A_Tm;b,B_Tm;d,D_Tm] lem) th
let MKIMP_CO_IFF =
let lem =
TAUT `(C ==> A ==> B) /\ (D ==> B ==> A) ==> C /\ D ==> (A <=> B)`
in
fun th1 th2 ->
let c,ab = dest_imp (concl th1) and d,_ = dest_imp (concl th2) in
let a,b = dest_imp ab in
MP (INST [a,A_Tm;b,B_Tm;c,C_Tm;d,D_Tm] lem) (CONJ th1 th2)
let MKIMPL_CO_IFF =
let lem =
TAUT `(C ==> A ==> B) ==> C /\ (B ==> A) ==> (A <=> B)`
in
fun th ->
let c,ab = dest_imp (concl th) in
let a,b = dest_imp ab in
MP (INST [a,A_Tm;b,B_Tm;c,C_Tm] lem) th
let MKIMPR_CO_IFF =
let lem = TAUT `(D ==> B ==> A) ==> (A ==> B) /\ D ==> (A <=> B)` in
fun th ->
let d,ba = dest_imp (concl th) in
let b,a = dest_imp ba in
MP (INST [a,A_Tm;b,B_Tm;d,D_Tm] lem) th
(* Given two theorems [A ==> B] and [C ==> D],
* returns [(B ==> C) ==> (A ==> D)].
*)
let MKIMP_IMP th1 th2 =
let b,a = dest_imp (concl th1) and c,d = dest_imp (concl th2) in
MP (INST [a,A_Tm;b,B_Tm;c,C_Tm;d,D_Tm] MONO_IMP) (CONJ th1 th2)
let MKIMPL_common lem =
let lem' = REWRITE_RULE[] (INST [C_Tm,D_Tm] lem) in
fun th t ->
let a,b = dest_imp (concl th) in
MP (INST [a,A_Tm;b,B_Tm;t,C_Tm] lem') th
(* Given a theorem [A ==> B] and a term [C],
* returns [A /\ C ==> B /\ C].
*)
let MKIMPL_CONJ = MKIMPL_common MONO_AND
(* Given a theorem [A ==> B] and a term [C],
* returns [A \/ C ==> B \/ C].
*)
let MKIMPL_DISJ = MKIMPL_common MONO_OR
(* Given a theorem [A ==> B] and a term [C],
* returns [(B ==> C) ==> (A ==> C)].
*)
let MKIMPL_IMP =
let MONO_IMP' = REWRITE_RULE[] (INST [C_Tm,D_Tm] MONO_IMP) in
fun th t ->
let b,a = dest_imp (concl th) in
MP (INST [a,A_Tm;b,B_Tm;t,C_Tm] MONO_IMP') th
let MKIMPR_common lem =
let lem' = REWRITE_RULE[] (INST [A_Tm,B_Tm] lem) in
fun t th ->
let c,d = dest_imp (concl th) in
MP (INST [c,C_Tm;d,D_Tm;t,A_Tm] lem') th
(* Given a term [A] and a theorem [B ==> C],
* returns [A /\ B ==> A /\ C].
*)
let MKIMPR_CONJ = MKIMPR_common MONO_AND
(* Given a term [A] and a theorem [B ==> C],
* returns [A \/ B ==> A \/ C].
*)
let MKIMPR_DISJ = MKIMPR_common MONO_OR
(* Given a term [A] and a theorem [B ==> C],
* returns [(A ==> B) ==> (A ==> C)].
*)
let MKIMPR_IMP = MKIMPR_common MONO_IMP
(* Given a theorem [A ==> B], returns [~B ==> ~A]. *)
let MKIMP_NOT th =
let b,a = dest_imp (concl th) in
MP (INST [a,A_Tm;b,B_Tm] MONO_NOT) th
let MKIMP_QUANT lem x th =
let x_ty = type_of x and p,q = dest_imp (concl th) in
let p' = mk_abs(x,p) and q' = mk_abs(x,q) in
let P = mk_var("P",mk_fun_ty x_ty bool_ty) in
let Q = mk_var("Q",mk_fun_ty x_ty bool_ty) in
let lem = INST [p',P;q',Q] (INST_TYPE [x_ty,aty] lem) in
let c = ONCE_DEPTH_CONV (ALPHA_CONV x) THENC ONCE_DEPTH_CONV BETA_CONV in
MP (CONV_RULE c lem) (GEN x th)
(* Given a variable [x] and a theorem [A ==> B],
* returns [(!x. A) ==> (!x. B)]. *)
let MKIMP_FORALL = MKIMP_QUANT MONO_FORALL
(* Given a variable [x] and a theorem [A ==> B],
* returns [(?x. A) ==> (?x. B)]. *)
let MKIMP_EXISTS = MKIMP_QUANT MONO_EXISTS
(* Given two theorems [A ==> B] and [B ==> C ==> D],
* returns [(B ==> C) ==> (A ==> D)],
* i.e., similar to [MKIMP_IMP] but allows to remove the context [B]
* since it is a consequence of [A].
*)
let MKIMP_IMP_CONTRA_CTXT =
let lem = TAUT `(B==>A) /\ (A==>B==>C==>D) ==> (A==>C) ==> (B==>D)` in
fun th1 th2 ->
let a,bcd = dest_imp (concl th2) in
let b,cd = dest_imp bcd in
let c,d = dest_imp cd in
MP (INST [a,A_Tm;b,B_Tm;c,C_Tm;d,D_Tm] lem) (CONJ th1 th2)
let MKIMP_IMP_CO_CTXT =
let lem = TAUT `(A==>B) /\ (A==>B==>D==>C) ==> (B==>D) ==> (A==>C)` in
fun th1 th2 ->
let a,bdc = dest_imp (concl th2) in
let b,dc = dest_imp bdc in
let d,c = dest_imp dc in
MP (INST [a,A_Tm;b,B_Tm;c,C_Tm;d,D_Tm] lem) (CONJ th1 th2)
(* Given a theorem [B ==> C ==> D], returns [(B ==> C) ==> (B ==> D)],
* i.e., similar to [MKIMP_IMP] but allows to remove the context [B]
* since it is a consequence of [A].
*)
let MKIMPR_IMP_CTXT =
let lem = TAUT `(A==>C==>D) ==> (A==>C) ==> (A==>D)` in
fun th ->
let a,cd = dest_imp (concl th) in
let c,d = dest_imp cd in
MP (INST [c,C_Tm;d,D_Tm;a,A_Tm] lem) th
(* Given two theorems [A ==> B] and [A ==> B ==> C ==> D],
* returns [(A /\ C) ==> (B /\ D)],
* i.e., similar to [MKIMP_CONJ] but allows to remove the contexts [A] and [B].
*)
let MKIMP_CONJ_CONTRA_CTXT =
let lem = TAUT `(C==>A==>B) /\ (A==>B==>C==>D) ==> (A/\C==>B/\D)` in
fun th1 th2 ->
let a,bcd = dest_imp (concl th2) in
let b,cd = dest_imp bcd in
let c,d = dest_imp cd in
MP (INST [a,A_Tm;b,B_Tm;c,C_Tm;d,D_Tm] lem) (CONJ th1 th2)
let MKIMPL_CONJ_CONTRA_CTXT =
let lem = TAUT `(C==>A==>B) ==> (A/\C==>B/\C)` in
fun th ->
let c,ab = dest_imp (concl th) in
let a,b = dest_imp ab in
MP (INST [a,A_Tm;b,B_Tm;c,C_Tm] lem) th
let MKIMPR_CONJ_CONTRA_CTXT =
let lem = TAUT `(A==>C==>D) ==> (A/\C==>A/\D)` in
fun th ->
let a,cd = dest_imp (concl th) in
let c,d = dest_imp cd in
MP (INST [a,A_Tm;c,C_Tm;d,D_Tm] lem) th
let MKIMP_CONJ_CO_CTXT =
let lem = TAUT `(B==>A) /\ (B==>D==>C) ==> (B/\D==>A/\C)` in
fun th1 th2 ->
let b,a = dest_imp (concl th1) in
let d,c = dest_imp (snd (dest_imp (concl th2))) in
MP (INST [a,A_Tm;b,B_Tm;c,C_Tm;d,D_Tm] lem) (CONJ th1 th2)
let MKIMPL_CONJ_CO_CTXT =
let lem = TAUT `(B==>A) ==> (B/\C==>A/\C)` in
fun th ->
let b,a = dest_imp (concl th) in
fun c -> MP (INST [a,A_Tm;b,B_Tm;c,C_Tm] lem) th
let MKIMPL_CONJ_CO2_CTXT =
let lem = TAUT `(C==>B==>A) ==> (B/\C==>A/\C)` in
fun th ->
let c,ba = dest_imp (concl th) in
let b,a = dest_imp ba in
MP (INST [a,A_Tm;b,B_Tm;c,C_Tm] lem) th
let MKIMPR_CONJ_CO_CTXT = MKIMPR_CONJ_CONTRA_CTXT
(*****************************************************************************)
(* IMPLICATIONAL CONVERSIONS *)
(*****************************************************************************)
(*open Variance*)
(* An implicational conversion maps a term t to a theorem of the form:
* t' ==> t if covariant
* t ==> t' if contravariant
*)
type imp_conv = Variance.t -> term -> thm
(* Trivial embedding of conversions into implicational conversions. *)
let (imp_conv_of_conv:conv->imp_conv) =
fun c v t ->
let th1,th2 = EQ_IMP_RULE (c t) in
match v with Variance.Co -> th2 | Variance.Contra -> th1
(* Retrieves the outcome of an implicational conversion, i.e., t'. *)
let imp_conv_outcome th v =
let t1,t2 = dest_binary_blind (concl th) in
match v with Variance.Co -> t1 | Variance.Contra -> t2
(* [ALL_IMPCONV _ t] returns `t==>t` *)
let (ALL_IMPCONV:imp_conv) = fun _ -> IMP_REFL
(* The implicational conversion which always fails. *)
let (NO_IMPCONV:imp_conv) = fun _ _ -> failwith "NO_IMPCONV"
let bind_impconv (c:imp_conv) v th =
let t1,t2 = dest_imp (concl th) in
match v with
|Variance.Co -> IMP_TRANS (c v t1) th
|Variance.Contra -> IMP_TRANS th (c v t2)
let THEN_IMPCONV (c1:imp_conv) c2 v t = bind_impconv c2 v (c1 v t)
(*****************************************************************************)
(* SOME USEFUL IMPLICATIONAL CONVERSIONS *)
(*****************************************************************************)
(* Given a theorem [p ==> c], returns the implicational conversion which:
* - in the covariant case, matches the input term [t] against [c] and returns
* [s(p) ==> t], where [s] is the matching substitution
* - in the contravariant case, matches the input term [t] against [p] and returns
* [t ==> s(c)], where [s] is the matching substitution
*)
let (MATCH_MP_IMPCONV:thm->imp_conv) =
fun th -> function
|Variance.Co -> GEN_PART_MATCH rand th
|Variance.Contra -> GEN_PART_MATCH lhand th
(*****************************************************************************)
(* INTERFACE *)
(*****************************************************************************)
(* From an implicational conversion builds a rule, i.e., a function which
* takes a theorem and returns a new theorem.
*)
let (IMPCONV_RULE:imp_conv->thm->thm) =
fun c th ->
let t = concl th in
MATCH_MP (c Variance.Contra t) th
(* From an implicational conversion builds a tactic. *)
let (IMPCONV_TAC:imp_conv->tactic) =
fun cnv ((_,c) as g) ->
(MATCH_MP_TAC (cnv Variance.Co c) THEN TRY (ACCEPT_TAC TRUTH)) g
(*****************************************************************************)
(* CONTEXT HANDLING *)
(*****************************************************************************)
(* [term list] = terms to add to the context *)
type 'a with_context =
With_context of 'a * ((*Tset.t*) term list -> 'a with_context) * (term -> 'a with_context)
let apply (With_context(c,_,_)) = c
(* Maybe avoid the augment if the input list is empty? *)
let augment (With_context(_,a,_)) = a
let diminish (With_context(_,_,d)) = d
let apply_with_context c ctx v t =
DISCH_CONJ ctx (apply (augment c (Tset.strip_conj ctx)) v t)
let imp_conv_of_ctx_imp_conv = (apply:imp_conv with_context -> imp_conv)
(* Consider two implicational conversions ic1, ic2.
* Suppose [ic1 Variance.Co A] returns [B ==> A], and [ic2 Variance.Co C] returns [D ==> C],
* then [CONJ_IMPCONV ic1 ic2 Variance.Co (A /\ C)] returns [B /\ D ==> A /\ C].
* Suppose [ic1 Variance.Contra A] returns [A ==> B], and [ic2 Variance.Contra C] returns
* [C ==> D], then [CONJ_IMPCONV ic1 ic2 Variance.Contra (A /\ B)]
* returns [A /\ B ==> C /\ D].
*
* Additionally takes the context into account, i.e., if [ic2 Variance.Co C] returns
* [A |- D ==> C],
* then [CONJ_IMPCONV ic1 ic2 Variance.Co (A /\ B)] returns [|- C /\ D ==> A /\ B]
* (i.e., [A] does not appear in the hypotheses).
*)
let rec CONJ_CTXIMPCONV (c:imp_conv with_context) =
With_context(
((fun v t ->
let t1,t2 = dest_conj t in
match v with
|Variance.Co ->
(try
let th1 = apply c Variance.Co t1 in
try
let t1' = imp_conv_outcome th1 Variance.Co in
MKIMP_CONJ_CO_CTXT th1 (apply_with_context c t1' Variance.Co t2)
with Failure _ -> MKIMPL_CONJ_CO_CTXT th1 t2
with Failure _ -> MKIMPR_CONJ_CO_CTXT (apply_with_context c t1 Variance.Co t2))
|Variance.Contra ->
try
(* note: we remove t1 in case it appears in t2, since otherwise,
* t1 removes t2 and t2 removes t1
*)
let t2s = Tset.remove (Tset.strip_conj t2) t1 in
let th1 = apply (augment c t2s) Variance.Contra t1 in
try
let t1' = imp_conv_outcome th1 Variance.Contra in
let t1s = Tset.strip_conj t1 and t1s' = Tset.strip_conj t1' in
let t1s'' = Tset.union t1s t1s' in
let th2 = apply (augment c t1s'') Variance.Contra t2 in
let th2' = DISCH_CONJ t1 (DISCH_CONJ t1' th2) in
MKIMP_CONJ_CONTRA_CTXT (DISCH_CONJ t2 th1) th2'
with Failure _ -> MKIMPL_CONJ_CONTRA_CTXT (DISCH_CONJ t2 th1)
with Failure _ ->
MKIMPR_CONJ_CONTRA_CTXT (apply_with_context c t1 Variance.Contra t2))
:imp_conv),
CONJ_CTXIMPCONV o augment c,
CONJ_CTXIMPCONV o diminish c)
(* Consider two implicational conversions ic1, ic2.
* Suppose [ic1 Variance.Co A] returns [B ==> A], and [ic2 Variance.Co C] returns [D ==> C],
* then [DISJ_IMPCONV ic1 ic2 Variance.Co (A \/ C)] returns [B \/ D ==> A \/ C].
* Suppose [ic1 Variance.Contra A] returns [A ==> B], and [ic2 Variance.Contra C] returns
* [C ==> D], then [DISJ_IMPCONV ic1 ic2 Variance.Contra (A \/ B)]
* returns [A \/ B ==> C \/ D].
*)
let rec DISJ_CTXIMPCONV (c:imp_conv with_context) =
With_context(
((fun v t ->
let t1,t2 = dest_disj t in
try
let th1 = apply c v t1 in
try MKIMP_DISJ th1 (apply c v t2) with Failure _ -> MKIMPL_DISJ th1 t2
with Failure _ -> MKIMPR_DISJ t1 (apply c v t2)):imp_conv),
DISJ_CTXIMPCONV o augment c,
DISJ_CTXIMPCONV o diminish c)
(* Consider two implicational conversions ic1, ic2.
* Suppose [ic1 Variance.Contra A] returns [A ==> B], and [ic2 Variance.Co C] returns [D ==> C],
* then [IMP_IMPCONV ic1 ic2 Variance.Co (A ==> C)] returns [(B ==> D) ==> (A ==> C)].
* Suppose [ic1 Variance.Co A] returns [B ==> A], and [ic2 Variance.Contra C] returns
* [C ==> D], then [IMP_IMPCONV ic1 ic2 Variance.Contra (A ==> C)]
* returns [(A ==> C) ==> (B ==> D)].
*
* Additionally takes the context into account, i.e., if [ic2 Variance.Co C] returns
* [B |- D ==> C], then [IMP_IMPCONV ic1 ic2 Variance.Co (A ==> C)] returns
* [|- (B ==> D) ==> (A ==> C)] (i.e., [B] does not appear in the hypotheses).
*)
let rec IMP_CTXIMPCONV (c:imp_conv with_context) =
With_context(
((fun v t ->
let t1,t2 = dest_imp t in
try
let v' = Variance.neg v in
let th1 = apply c v' t1 in
let t1' = imp_conv_outcome th1 v' in
let t1s = Tset.union (Tset.strip_conj t1) (Tset.strip_conj t1') in
let c' = augment c t1s in
let mk =
match v with Variance.Co -> MKIMP_IMP_CO_CTXT | Variance.Contra -> MKIMP_IMP_CONTRA_CTXT
in
try mk th1 (DISCH_CONJ t1 (DISCH_CONJ t1' (apply c' v t2)))
with Failure _ -> MKIMPL_IMP th1 t2
with Failure _ -> MKIMPR_IMP_CTXT (apply_with_context c t1 v t2)
):imp_conv),
IMP_CTXIMPCONV o augment c,
IMP_CTXIMPCONV o diminish c)
let rec IFF_CTXIMPCONV (c:imp_conv with_context) =
With_context(
((fun v t ->
let t1,t2 = dest_iff t in
let lr,l,r =
match v with
|Variance.Co -> MKIMP_CO_IFF,MKIMPL_CO_IFF,MKIMPR_CO_IFF
|Variance.Contra -> MKIMP_CONTRA_IFF,MKIMPL_CONTRA_IFF,MKIMPR_CONTRA_IFF
in
(try
let th1 = apply c v (mk_imp (t1,t2)) in
try
let th2 = apply c v (mk_imp (t2,t1)) in
(try MKIMP_IFF th1 th2 with Failure _ -> lr th1 th2)
with Failure _ -> l th1
with Failure _ -> r (apply c v (mk_imp (t2,t1))))):imp_conv),
IFF_CTXIMPCONV o augment c,
IFF_CTXIMPCONV o diminish c)
(* Consider an implicational conversion ic.
* Suppose [ic Variance.Contra A] returns [A ==> B]
* then [NOT_IMPCONV ic Variance.Co ~A] returns [~B ==> ~A].
* Suppose [ic Variance.Co A] returns [B ==> A]
* then [NOT_IMPCONV ic Variance.Contra ~A] returns [~A ==> ~B].
*)
let rec NOT_CTXIMPCONV (c:imp_conv with_context) =
With_context(
((fun v t -> MKIMP_NOT (apply c (Variance.neg v) (dest_neg t))):imp_conv),
NOT_CTXIMPCONV o augment c,
NOT_CTXIMPCONV o diminish c)
let rec QUANT_CTXIMPCONV mkimp sel (c:imp_conv with_context) =
With_context(
((fun v t ->
let x,b = sel t in
let c' = diminish c x in
mkimp x (apply c' v b)):imp_conv),
QUANT_CTXIMPCONV mkimp sel o augment c,
QUANT_CTXIMPCONV mkimp sel o diminish c)
(* Consider an implicational conversion ic.
* Suppose [ic Variance.Co A] returns [B ==> A]
* then [FORALL_IMPCONV ic Variance.Co (!x.A)] returns [(!x.B) ==> (!x.A)].
* Suppose [ic Variance.Contra A] returns [A ==> B]
* then [FORALL_IMPCONV ic Variance.Contra (!x.A)] returns [(!x.A) ==> (!x.B)].
*)
let FORALL_CTXIMPCONV = QUANT_CTXIMPCONV MKIMP_FORALL dest_forall
(* Consider an implicational conversion ic.
* Suppose [ic Variance.Co A] returns [B ==> A]
* then [EXISTS_IMPCONV ic Variance.Co (?x.A)] returns [(?x.B) ==> (?x.A)].
* Suppose [ic Variance.Contra A] returns [A ==> B]
* then [EXISTS_IMPCONV ic Variance.Contra (?x.A)] returns [(?x.A) ==> (?x.B)].
*)
let EXISTS_CTXIMPCONV = QUANT_CTXIMPCONV MKIMP_EXISTS dest_exists
(* Applies an implicational conversion on the subformula(s) of the input term*)
let SUB_CTXIMPCONV =
let iff_ty = `:bool->bool->bool` in
let rec SUB_CTXIMPCONV c =
With_context(
((fun v t ->
let n,ty = dest_const (fst (strip_comb t)) in
apply
((match n with
|"==>" -> IMP_CTXIMPCONV
|"/\\" -> CONJ_CTXIMPCONV
|"\\/" -> DISJ_CTXIMPCONV
|"=" when ty = iff_ty -> IFF_CTXIMPCONV
|"!" -> FORALL_CTXIMPCONV
|"?" -> EXISTS_CTXIMPCONV
|"~" -> NOT_CTXIMPCONV
|_ -> failwith "SUB_CTXIMPCONV") c)
v t):imp_conv),
SUB_CTXIMPCONV o augment c,
SUB_CTXIMPCONV o diminish c)
in SUB_CTXIMPCONV;;
(* Takes a theorem which results of an implicational conversion and applies
* another implicational conversion on the outcome.
*)
let bind_ctximpconv (c:imp_conv with_context) v th =
let t1,t2 = dest_imp (concl th) in
match v with
|Variance.Co -> IMP_TRANS (apply c v t1) th
|Variance.Contra -> IMP_TRANS th (apply c v t2)