| author | wenzelm |
| Wed, 14 Mar 2012 19:27:15 +0100 | |
| changeset 46924 | f2c60ad58374 |
| parent 46909 | 3c73a121a387 |
| child 47022 | 8eac39af4ec0 |
| permissions | -rw-r--r-- |
| 37781 | 1 |
(* Title: Tools/misc_legacy.ML |
2 |
||
3 |
Misc legacy stuff -- to be phased out eventually. |
|
4 |
*) |
|
5 |
||
6 |
signature MISC_LEGACY = |
|
7 |
sig |
|
| 44121 | 8 |
val add_term_names: term * string list -> string list |
9 |
val add_typ_tvars: typ * (indexname * sort) list -> (indexname * sort) list |
|
10 |
val add_typ_tfree_names: typ * string list -> string list |
|
11 |
val add_typ_tfrees: typ * (string * sort) list -> (string * sort) list |
|
12 |
val add_term_tvars: term * (indexname * sort) list -> (indexname * sort) list |
|
13 |
val add_term_tfrees: term * (string * sort) list -> (string * sort) list |
|
14 |
val add_term_tfree_names: term * string list -> string list |
|
15 |
val typ_tfrees: typ -> (string * sort) list |
|
16 |
val typ_tvars: typ -> (indexname * sort) list |
|
17 |
val term_tfrees: term -> (string * sort) list |
|
18 |
val term_tvars: term -> (indexname * sort) list |
|
19 |
val add_term_vars: term * term list -> term list |
|
20 |
val term_vars: term -> term list |
|
21 |
val add_term_frees: term * term list -> term list |
|
22 |
val term_frees: term -> term list |
|
| 37781 | 23 |
val mk_defpair: term * term -> string * term |
24 |
val get_def: theory -> xstring -> thm |
|
25 |
val METAHYPS: (thm list -> tactic) -> int -> tactic |
|
26 |
end; |
|
27 |
||
28 |
structure Misc_Legacy: MISC_LEGACY = |
|
29 |
struct |
|
30 |
||
| 44121 | 31 |
(*iterate a function over all types in a term*) |
32 |
fun it_term_types f = |
|
33 |
let fun iter(Const(_,T), a) = f(T,a) |
|
34 |
| iter(Free(_,T), a) = f(T,a) |
|
35 |
| iter(Var(_,T), a) = f(T,a) |
|
36 |
| iter(Abs(_,T,t), a) = iter(t,f(T,a)) |
|
37 |
| iter(f$u, a) = iter(f, iter(u, a)) |
|
38 |
| iter(Bound _, a) = a |
|
39 |
in iter end |
|
40 |
||
41 |
(*Accumulates the names in the term, suppressing duplicates. |
|
42 |
Includes Frees and Consts. For choosing unambiguous bound var names.*) |
|
43 |
fun add_term_names (Const(a,_), bs) = insert (op =) (Long_Name.base_name a) bs |
|
44 |
| add_term_names (Free(a,_), bs) = insert (op =) a bs |
|
45 |
| add_term_names (f$u, bs) = add_term_names (f, add_term_names(u, bs)) |
|
46 |
| add_term_names (Abs(_,_,t), bs) = add_term_names(t,bs) |
|
47 |
| add_term_names (_, bs) = bs; |
|
48 |
||
49 |
(*Accumulates the TVars in a type, suppressing duplicates.*) |
|
50 |
fun add_typ_tvars(Type(_,Ts),vs) = List.foldr add_typ_tvars vs Ts |
|
51 |
| add_typ_tvars(TFree(_),vs) = vs |
|
52 |
| add_typ_tvars(TVar(v),vs) = insert (op =) v vs; |
|
53 |
||
54 |
(*Accumulates the TFrees in a type, suppressing duplicates.*) |
|
55 |
fun add_typ_tfree_names(Type(_,Ts),fs) = List.foldr add_typ_tfree_names fs Ts |
|
56 |
| add_typ_tfree_names(TFree(f,_),fs) = insert (op =) f fs |
|
57 |
| add_typ_tfree_names(TVar(_),fs) = fs; |
|
58 |
||
59 |
fun add_typ_tfrees(Type(_,Ts),fs) = List.foldr add_typ_tfrees fs Ts |
|
60 |
| add_typ_tfrees(TFree(f),fs) = insert (op =) f fs |
|
61 |
| add_typ_tfrees(TVar(_),fs) = fs; |
|
62 |
||
63 |
(*Accumulates the TVars in a term, suppressing duplicates.*) |
|
64 |
val add_term_tvars = it_term_types add_typ_tvars; |
|
65 |
||
66 |
(*Accumulates the TFrees in a term, suppressing duplicates.*) |
|
67 |
val add_term_tfrees = it_term_types add_typ_tfrees; |
|
68 |
val add_term_tfree_names = it_term_types add_typ_tfree_names; |
|
69 |
||
70 |
(*Non-list versions*) |
|
71 |
fun typ_tfrees T = add_typ_tfrees(T,[]); |
|
72 |
fun typ_tvars T = add_typ_tvars(T,[]); |
|
73 |
fun term_tfrees t = add_term_tfrees(t,[]); |
|
74 |
fun term_tvars t = add_term_tvars(t,[]); |
|
75 |
||
76 |
||
77 |
(*Accumulates the Vars in the term, suppressing duplicates.*) |
|
78 |
fun add_term_vars (t, vars: term list) = case t of |
|
79 |
Var _ => Ord_List.insert Term_Ord.term_ord t vars |
|
80 |
| Abs (_,_,body) => add_term_vars(body,vars) |
|
81 |
| f$t => add_term_vars (f, add_term_vars(t, vars)) |
|
82 |
| _ => vars; |
|
83 |
||
84 |
fun term_vars t = add_term_vars(t,[]); |
|
85 |
||
86 |
(*Accumulates the Frees in the term, suppressing duplicates.*) |
|
87 |
fun add_term_frees (t, frees: term list) = case t of |
|
88 |
Free _ => Ord_List.insert Term_Ord.term_ord t frees |
|
89 |
| Abs (_,_,body) => add_term_frees(body,frees) |
|
90 |
| f$t => add_term_frees (f, add_term_frees(t, frees)) |
|
91 |
| _ => frees; |
|
92 |
||
93 |
fun term_frees t = add_term_frees(t,[]); |
|
94 |
||
95 |
||
| 37781 | 96 |
fun mk_defpair (lhs, rhs) = |
97 |
(case Term.head_of lhs of |
|
98 |
Const (name, _) => |
|
| 46909 | 99 |
(Thm.def_name (Long_Name.base_name name), Logic.mk_equals (lhs, rhs)) |
| 37781 | 100 |
| _ => raise TERM ("Malformed definition: head of lhs not a constant", [lhs, rhs]));
|
101 |
||
102 |
||
103 |
fun get_def thy = Thm.axiom thy o Name_Space.intern (Theory.axiom_space thy) o Thm.def_name; |
|
104 |
||
105 |
||
106 |
(**** METAHYPS -- tactical for using hypotheses as meta-level assumptions |
|
107 |
METAHYPS (fn prems => tac prems) i |
|
108 |
||
109 |
converts subgoal i, of the form !!x1...xm. [| A1;...;An] ==> A into a new |
|
110 |
proof state A==>A, supplying A1,...,An as meta-level assumptions (in |
|
111 |
"prems"). The parameters x1,...,xm become free variables. If the |
|
112 |
resulting proof state is [| B1;...;Bk] ==> C (possibly assuming A1,...,An) |
|
113 |
then it is lifted back into the original context, yielding k subgoals. |
|
114 |
||
115 |
Replaces unknowns in the context by Frees having the prefix METAHYP_ |
|
116 |
New unknowns in [| B1;...;Bk] ==> C are lifted over x1,...,xm. |
|
117 |
DOES NOT HANDLE TYPE UNKNOWNS. |
|
118 |
||
119 |
||
120 |
NOTE: This version does not observe the proof context, and thus cannot |
|
121 |
work reliably. See also Subgoal.SUBPROOF and Subgoal.FOCUS for |
|
122 |
properly localized variants of the same idea. |
|
123 |
****) |
|
124 |
||
125 |
local |
|
126 |
||
127 |
(*Strips assumptions in goal yielding ( [x1,...,xm], [H1,...,Hn], B ) |
|
128 |
H1,...,Hn are the hypotheses; x1...xm are variants of the parameters. |
|
129 |
Main difference from strip_assums concerns parameters: |
|
130 |
it replaces the bound variables by free variables. *) |
|
131 |
fun strip_context_aux (params, Hs, Const ("==>", _) $ H $ B) =
|
|
132 |
strip_context_aux (params, H :: Hs, B) |
|
133 |
| strip_context_aux (params, Hs, Const ("all",_) $ Abs (a, T, t)) =
|
|
| 42284 | 134 |
let val (b, u) = Syntax_Trans.variant_abs (a, T, t) |
| 37781 | 135 |
in strip_context_aux ((b, T) :: params, Hs, u) end |
136 |
| strip_context_aux (params, Hs, B) = (rev params, rev Hs, B); |
|
137 |
||
138 |
fun strip_context A = strip_context_aux ([], [], A); |
|
139 |
||
140 |
(*Left-to-right replacements: ctpairs = [...,(vi,ti),...]. |
|
141 |
Instantiates distinct free variables by terms of same type.*) |
|
142 |
fun free_instantiate ctpairs = |
|
143 |
forall_elim_list (map snd ctpairs) o forall_intr_list (map fst ctpairs); |
|
144 |
||
145 |
fun free_of s ((a, i), T) = |
|
146 |
Free (s ^ (case i of 0 => a | _ => a ^ "_" ^ string_of_int i), T) |
|
147 |
||
148 |
fun mk_inst v = (Var v, free_of "METAHYP1_" v) |
|
149 |
||
150 |
fun metahyps_split_prem prem = |
|
151 |
let (*find all vars in the hyps -- should find tvars also!*) |
|
152 |
val hyps_vars = fold Term.add_vars (Logic.strip_assums_hyp prem) [] |
|
153 |
val insts = map mk_inst hyps_vars |
|
154 |
(*replace the hyps_vars by Frees*) |
|
155 |
val prem' = subst_atomic insts prem |
|
156 |
val (params,hyps,concl) = strip_context prem' |
|
157 |
in (insts,params,hyps,concl) end; |
|
158 |
||
159 |
fun metahyps_aux_tac tacf (prem,gno) state = |
|
160 |
let val (insts,params,hyps,concl) = metahyps_split_prem prem |
|
161 |
val maxidx = Thm.maxidx_of state |
|
162 |
val cterm = Thm.cterm_of (Thm.theory_of_thm state) |
|
163 |
val chyps = map cterm hyps |
|
164 |
val hypths = map Thm.assume chyps |
|
165 |
val subprems = map (Thm.forall_elim_vars 0) hypths |
|
166 |
val fparams = map Free params |
|
167 |
val cparams = map cterm fparams |
|
168 |
fun swap_ctpair (t,u) = (cterm u, cterm t) |
|
169 |
(*Subgoal variables: make Free; lift type over params*) |
|
170 |
fun mk_subgoal_inst concl_vars (v, T) = |
|
171 |
if member (op =) concl_vars (v, T) |
|
172 |
then ((v, T), true, free_of "METAHYP2_" (v, T)) |
|
173 |
else ((v, T), false, free_of "METAHYP2_" (v, map #2 params ---> T)) |
|
174 |
(*Instantiate subgoal vars by Free applied to params*) |
|
175 |
fun mk_ctpair (v, in_concl, u) = |
|
176 |
if in_concl then (cterm (Var v), cterm u) |
|
177 |
else (cterm (Var v), cterm (list_comb (u, fparams))) |
|
178 |
(*Restore Vars with higher type and index*) |
|
179 |
fun mk_subgoal_swap_ctpair (((a, i), T), in_concl, u as Free (_, U)) = |
|
180 |
if in_concl then (cterm u, cterm (Var ((a, i), T))) |
|
181 |
else (cterm u, cterm (Var ((a, i + maxidx), U))) |
|
182 |
(*Embed B in the original context of params and hyps*) |
|
|
46215
0da9433f959e
discontinued old-style Term.list_all_free in favour of plain Logic.all;
wenzelm
parents:
45862
diff
changeset
|
183 |
fun embed B = fold_rev Logic.all fparams (Logic.list_implies (hyps, B)) |
| 37781 | 184 |
(*Strip the context using elimination rules*) |
185 |
fun elim Bhyp = implies_elim_list (forall_elim_list cparams Bhyp) hypths |
|
186 |
(*A form of lifting that discharges assumptions.*) |
|
187 |
fun relift st = |
|
188 |
let val prop = Thm.prop_of st |
|
189 |
val subgoal_vars = (*Vars introduced in the subgoals*) |
|
190 |
fold Term.add_vars (Logic.strip_imp_prems prop) [] |
|
191 |
and concl_vars = Term.add_vars (Logic.strip_imp_concl prop) [] |
|
192 |
val subgoal_insts = map (mk_subgoal_inst concl_vars) subgoal_vars |
|
193 |
val st' = Thm.instantiate ([], map mk_ctpair subgoal_insts) st |
|
194 |
val emBs = map (cterm o embed) (prems_of st') |
|
195 |
val Cth = implies_elim_list st' (map (elim o Thm.assume) emBs) |
|
196 |
in (*restore the unknowns to the hypotheses*) |
|
197 |
free_instantiate (map swap_ctpair insts @ |
|
198 |
map mk_subgoal_swap_ctpair subgoal_insts) |
|
199 |
(*discharge assumptions from state in same order*) |
|
200 |
(implies_intr_list emBs |
|
201 |
(forall_intr_list cparams (implies_intr_list chyps Cth))) |
|
202 |
end |
|
203 |
(*function to replace the current subgoal*) |
|
204 |
fun next st = Thm.bicompose false (false, relift st, nprems_of st) gno state |
|
205 |
in Seq.maps next (tacf subprems (Thm.trivial (cterm concl))) end; |
|
206 |
||
207 |
fun print_vars_terms n thm = |
|
208 |
let |
|
209 |
val thy = theory_of_thm thm |
|
210 |
fun typed s ty = |
|
211 |
" " ^ s ^ " has type: " ^ Syntax.string_of_typ_global thy ty; |
|
212 |
fun find_vars (Const (c, ty)) = |
|
213 |
if null (Term.add_tvarsT ty []) then I |
|
214 |
else insert (op =) (typed c ty) |
|
215 |
| find_vars (Var (xi, ty)) = |
|
216 |
insert (op =) (typed (Term.string_of_vname xi) ty) |
|
217 |
| find_vars (Free _) = I |
|
218 |
| find_vars (Bound _) = I |
|
219 |
| find_vars (Abs (_, _, t)) = find_vars t |
|
220 |
| find_vars (t1 $ t2) = find_vars t1 #> find_vars t2; |
|
221 |
val prem = Logic.nth_prem (n, Thm.prop_of thm) |
|
222 |
val tms = find_vars prem [] |
|
223 |
in warning (cat_lines ("Found schematic vars in assumptions:" :: tms)) end;
|
|
224 |
||
225 |
in |
|
226 |
||
227 |
fun METAHYPS tacf n thm = SUBGOAL (metahyps_aux_tac tacf) n thm |
|
228 |
handle THM("assume: variables",_,_) => (print_vars_terms n thm; Seq.empty)
|
|
229 |
||
230 |
end; |
|
231 |
||
232 |
end; |
|
233 |