-
Notifications
You must be signed in to change notification settings - Fork 0
/
ottLib.sml
167 lines (145 loc) · 6.46 KB
/
ottLib.sml
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
(**************************************************************************)
(* ottLib *)
(* *)
(* Scott Owens, Computer Laboratory, University of Cambridge *)
(* *)
(* Copyright 2006 *)
(* *)
(* Redistribution and use in source and binary forms, with or without *)
(* modification, are permitted provided that the following conditions *)
(* are met: *)
(* 1. Redistributions of source code must retain the above copyright *)
(* notice, this list of conditions and the following disclaimer. *)
(* 2. Redistributions in binary form must reproduce the above copyright *)
(* notice, this list of conditions and the following disclaimer in the *)
(* documentation and/or other materials provided with the distribution. *)
(* 3. The names of the authors may not be used to endorse or promote *)
(* products derived from this software without specific prior written *)
(* permission. *)
(* *)
(* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS *)
(* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *)
(* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *)
(* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY *)
(* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *)
(* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE *)
(* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *)
(* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER *)
(* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR *)
(* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN *)
(* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *)
(**************************************************************************)
structure ottLib :> ottLib = struct
infix THEN
open numSyntax listSyntax pairSyntax listTheory HolKernel bossLib boolLib Defn;
open ottTheory;
fun inst_filter_size tm =
case total (PART_MATCH (fst o dest_leq) filter_size) tm of
NONE =>
if is_comb tm then
let val (f, args) = strip_comb tm in
inst_filter_size f @ List.concat (List.map inst_filter_size args)
end
else
[]
| SOME thm => [thm];
fun APPLY_FILTER_SIZE (assums, g) =
case inst_filter_size g of
[] => ALL_TAC (assums, g)
| l => (MAP_EVERY STRIP_ASSUME_TAC l) (assums, g);
fun find_list t =
if is_var t then
t
else if is_map t then
find_list (snd (dest_map t))
else
t;
fun PAIR_CASES_TAC (a, g) =
case total dest_prod (type_of (fst (dest_forall g))) of
NONE => ALL_TAC (a, g)
| SOME _ => Cases (a, g);
fun MEM_IND_TAC mem =
let val (arg1, arg2) = dest_mem mem
val lst = find_list arg2
in
PAT_ASSUM mem (fn ma => REPEAT (POP_ASSUM MP_TAC) THEN MP_TAC ma) THEN
SPEC_TAC (lst, genvar (type_of lst)) THEN
HO_MATCH_MP_TAC list_induction THEN STRIP_TAC THENL
[ALL_TAC, STRIP_TAC THEN STRIP_TAC THEN PAIR_CASES_TAC] THEN
SRW_TAC [] [] THEN
CONV_TAC TotalDefn.TC_SIMP_CONV THEN
SRW_TAC [] [] THEN
RES_TAC THEN
DECIDE_TAC THEN NO_TAC
end;
fun ONE_TERM_TAC measure =
WF_REL_TAC `^measure` THEN
CONV_TAC TotalDefn.TC_SIMP_CONV THEN
SRW_TAC [] [] THEN
APPLY_FILTER_SIZE THEN
TRY (ASSUM_LIST (MAP_FIRST MEM_IND_TAC o List.filter is_mem o List.map concl))
THEN
RES_TAC THEN DECIDE_TAC THEN NO_TAC;
fun TERM_TAC defn = (MAP_FIRST ONE_TERM_TAC (TotalDefn.guessR defn));
fun ottDefine name tq =
let val defn = Hol_defn name tq in
if not (null (tcs_of defn)) then
fst (tstore_defn (defn, TERM_TAC defn))
else
(save_defn defn;
LIST_CONJ (eqns_of defn))
end;
(* Adapted from THENL *)
fun mapshape [] _ _ = []
| mapshape (n1::nums) (f1::funcs) args =
let val (f1_args,args') = split_after n1 args
in f1 f1_args :: mapshape nums funcs args'
end;
fun mk_branch_name (consts, TAC) =
((fn (assums, g) =>
let val (cn, goal_name) = (dest_comb o hd o strip_conj o fst o dest_imp o snd o strip_forall) g in
same_const ``clause_name`` cn andalso
List.exists (fn tm => term_eq goal_name tm) consts
end),
TAC);
fun mk_branch (consts, TAC) =
((fn (assums, g) => List.exists (fn const => can (find_term (can (match_term const))) g) consts),
TAC);
fun THENcases (rws:thm list) (tac1:tactic) (branches:((term list * term -> bool) * tactic) list) : tactic =
fn g =>
let val tac2 = SIMP_TAC (srw_ss()) rws;
fun apply_branch subgoal =
case List.find (fn (pred, T) => pred subgoal) branches of
NONE => tac2 subgoal
| SOME (pred, T) =>
(case (tac2 THEN T) subgoal of
([], vf) => let val th = vf [] in ([], fn [] => th) end
| x => x);
val (gl, vf) = tac1 g;
val (G,V,lengths) =
itlist (fn goal => fn (G,V,lengths) =>
let val (goals, vfun) = apply_branch goal in
(goals@G, vfun::V, length goals::lengths)
end)
gl
([],[],[]);
in
case G of
[] => ([], let val th = vf (map (fn f => f[]) V) in fn [] => th end)
| _ => (G, (vf o mapshape lengths V))
end
handle e as HOL_ERR _ => raise (wrap_exn "Tactical" "THENcases" e);
fun RULE_INDUCT_TAC (ind:thm) (rws:thm list) (spec:(term list * tactic) list) : tactic =
THENcases rws (HO_MATCH_MP_TAC ind THEN REPEAT CONJ_TAC) (List.map mk_branch_name spec);
fun INDUCT_TAC (ind:thm) (rws:thm list) (spec:(term list * tactic) list) : tactic =
THENcases rws (HO_MATCH_MP_TAC ind THEN REPEAT CONJ_TAC) (List.map mk_branch spec);
fun get_terms f =
List.map (hd o snd o strip_comb o hd o snd o strip_comb o concl o SPEC_ALL) (CONJUNCTS f)
fun structural_cases datatype_thms arg cases thm =
let fun helper (terms, thm) =
let val thm1 = LIST_CONJ (List.map (fn qt => (GEN_ALL o SPEC qt o funpow arg Q.ID_SPEC) thm) terms)
in SIMP_RULE bool_ss datatype_thms thm1 end
in
LIST_CONJ (ListPair.map helper (cases, CONJUNCTS thm))
end;
end