51726
|
1 |
(* Title: HOL/Tools/reflection.ML
|
|
2 |
Author: Amine Chaieb, TU Muenchen
|
|
3 |
|
|
4 |
A trial for automatical reification.
|
|
5 |
*)
|
|
6 |
|
|
7 |
signature REFLECTION =
|
|
8 |
sig
|
52272
|
9 |
val reify: Proof.context -> thm list -> term -> thm
|
|
10 |
val reify_tac: Proof.context -> thm list -> term option -> int -> tactic
|
|
11 |
val reflect: Proof.context -> (cterm -> thm)
|
|
12 |
-> thm list -> thm list -> term -> thm
|
|
13 |
val reflection_tac: Proof.context -> (cterm -> thm)
|
51726
|
14 |
-> thm list -> thm list -> term option -> int -> tactic
|
|
15 |
val get_default: Proof.context -> { reification_eqs: thm list, correctness_thms: thm list }
|
|
16 |
val add_reification_eq: attribute
|
|
17 |
val del_reification_eq: attribute
|
|
18 |
val add_correctness_thm: attribute
|
|
19 |
val del_correctness_thm: attribute
|
|
20 |
val default_reify_tac: Proof.context -> thm list -> term option -> int -> tactic
|
|
21 |
val default_reflection_tac: Proof.context -> thm list -> thm list -> term option -> int -> tactic
|
|
22 |
end;
|
|
23 |
|
|
24 |
structure Reflection : REFLECTION =
|
|
25 |
struct
|
|
26 |
|
|
27 |
val FWD = curry (op OF);
|
|
28 |
|
|
29 |
fun dest_listT (Type (@{type_name "list"}, [T])) = T;
|
|
30 |
|
|
31 |
|
|
32 |
(* Make a congruence rule out of a defining equation for the interpretation
|
|
33 |
|
|
34 |
th is one defining equation of f,
|
|
35 |
i.e. th is "f (Cp ?t1 ... ?tn) = P(f ?t1, .., f ?tn)"
|
|
36 |
Cp is a constructor pattern and P is a pattern
|
|
37 |
|
|
38 |
The result is:
|
|
39 |
[|?A1 = f ?t1 ; .. ; ?An= f ?tn |] ==> P (?A1, .., ?An) = f (Cp ?t1 .. ?tn)
|
|
40 |
+ the a list of names of the A1 .. An, Those are fresh in the ctxt *)
|
|
41 |
|
|
42 |
fun mk_congeq ctxt fs th =
|
|
43 |
let
|
|
44 |
val Const (fN, _) = th |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq
|
|
45 |
|> fst |> strip_comb |> fst;
|
52273
|
46 |
val cert = Thm.cterm_of (Proof_Context.theory_of ctxt);
|
51726
|
47 |
val ((_, [th']), ctxt') = Variable.import true [th] ctxt;
|
|
48 |
val (lhs, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop (Thm.prop_of th'));
|
|
49 |
fun add_fterms (t as t1 $ t2) =
|
|
50 |
if exists (fn f => Term.could_unify (t |> strip_comb |> fst, f)) fs
|
|
51 |
then insert (op aconv) t
|
|
52 |
else add_fterms t1 #> add_fterms t2
|
|
53 |
| add_fterms (t as Abs _) =
|
|
54 |
if exists_Const (fn (c, _) => c = fN) t
|
|
55 |
then K [t]
|
|
56 |
else K []
|
|
57 |
| add_fterms _ = I;
|
|
58 |
val fterms = add_fterms rhs [];
|
|
59 |
val (xs, ctxt'') = Variable.variant_fixes (replicate (length fterms) "x") ctxt';
|
|
60 |
val tys = map fastype_of fterms;
|
|
61 |
val vs = map Free (xs ~~ tys);
|
|
62 |
val env = fterms ~~ vs; (*FIXME*)
|
|
63 |
fun replace_fterms (t as t1 $ t2) =
|
|
64 |
(case AList.lookup (op aconv) env t of
|
|
65 |
SOME v => v
|
|
66 |
| NONE => replace_fterms t1 $ replace_fterms t2)
|
|
67 |
| replace_fterms t =
|
|
68 |
(case AList.lookup (op aconv) env t of
|
|
69 |
SOME v => v
|
|
70 |
| NONE => t);
|
|
71 |
fun mk_def (Abs (x, xT, t), v) =
|
|
72 |
HOLogic.mk_Trueprop (HOLogic.all_const xT $ Abs (x, xT, HOLogic.mk_eq (v $ Bound 0, t)))
|
|
73 |
| mk_def (t, v) = HOLogic.mk_Trueprop (HOLogic.mk_eq (v, t));
|
|
74 |
fun tryext x =
|
|
75 |
(x RS @{lemma "(\<forall>x. f x = g x) \<Longrightarrow> f = g" by blast} handle THM _ => x);
|
|
76 |
val cong =
|
|
77 |
(Goal.prove ctxt'' [] (map mk_def env)
|
|
78 |
(HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, replace_fterms rhs)))
|
|
79 |
(fn {context, prems, ...} =>
|
|
80 |
Local_Defs.unfold_tac context (map tryext prems) THEN rtac th' 1)) RS sym;
|
|
81 |
val (cong' :: vars') =
|
|
82 |
Variable.export ctxt'' ctxt (cong :: map (Drule.mk_term o cert) vs);
|
|
83 |
val vs' = map (fst o fst o Term.dest_Var o Thm.term_of o Drule.dest_term) vars';
|
|
84 |
|
|
85 |
in (vs', cong') end;
|
|
86 |
|
|
87 |
(* congs is a list of pairs (P,th) where th is a theorem for
|
|
88 |
[| f p1 = A1; ...; f pn = An|] ==> f (C p1 .. pn) = P *)
|
|
89 |
|
|
90 |
fun rearrange congs =
|
|
91 |
let
|
|
92 |
fun P (_, th) =
|
|
93 |
let val @{term "Trueprop"} $ (Const (@{const_name HOL.eq}, _) $ l $ _) = concl_of th
|
|
94 |
in can dest_Var l end;
|
|
95 |
val (yes, no) = List.partition P congs;
|
|
96 |
in no @ yes end;
|
|
97 |
|
52272
|
98 |
fun reify ctxt eqs t =
|
51726
|
99 |
let
|
|
100 |
fun index_of t bds =
|
|
101 |
let
|
|
102 |
val tt = HOLogic.listT (fastype_of t);
|
|
103 |
in
|
|
104 |
(case AList.lookup Type.could_unify bds tt of
|
52273
|
105 |
NONE => error "index_of: type not found in environements!"
|
51726
|
106 |
| SOME (tbs, tats) =>
|
|
107 |
let
|
|
108 |
val i = find_index (fn t' => t' = t) tats;
|
|
109 |
val j = find_index (fn t' => t' = t) tbs;
|
|
110 |
in
|
|
111 |
if j = ~1 then
|
|
112 |
if i = ~1
|
|
113 |
then (length tbs + length tats, AList.update Type.could_unify (tt, (tbs, tats @ [t])) bds)
|
|
114 |
else (i, bds)
|
|
115 |
else (j, bds)
|
|
116 |
end)
|
|
117 |
end;
|
|
118 |
|
|
119 |
(* Generic decomp for reification : matches the actual term with the
|
|
120 |
rhs of one cong rule. The result of the matching guides the
|
|
121 |
proof synthesis: The matches of the introduced Variables A1 .. An are
|
|
122 |
processed recursively
|
|
123 |
The rest is instantiated in the cong rule,i.e. no reification is needed *)
|
|
124 |
|
|
125 |
(* da is the decomposition for atoms, ie. it returns ([],g) where g
|
|
126 |
returns the right instance f (AtC n) = t , where AtC is the Atoms
|
|
127 |
constructor and n is the number of the atom corresponding to t *)
|
52272
|
128 |
fun decomp_reify da cgns (t, ctxt) bds =
|
51726
|
129 |
let
|
|
130 |
val thy = Proof_Context.theory_of ctxt;
|
|
131 |
val cert = cterm_of thy;
|
52273
|
132 |
val certT = ctyp_of thy;
|
51726
|
133 |
fun tryabsdecomp (s, ctxt) bds =
|
|
134 |
(case s of
|
|
135 |
Abs (_, xT, ta) =>
|
|
136 |
let
|
|
137 |
val ([raw_xn], ctxt') = Variable.variant_fixes ["x"] ctxt;
|
|
138 |
val (xn, ta) = Syntax_Trans.variant_abs (raw_xn, xT, ta); (* FIXME !? *)
|
52273
|
139 |
val x = Free (xn, xT);
|
51726
|
140 |
val bds = (case AList.lookup Type.could_unify bds (HOLogic.listT xT) of
|
|
141 |
NONE => error "tryabsdecomp: Type not found in the Environement"
|
|
142 |
| SOME (bsT, atsT) => AList.update Type.could_unify (HOLogic.listT xT, (x :: bsT, atsT)) bds);
|
|
143 |
in (([(ta, ctxt')],
|
|
144 |
fn ([th], bds) =>
|
|
145 |
(hd (Variable.export ctxt' ctxt [(Thm.forall_intr (cert x) th) COMP allI]),
|
|
146 |
let
|
|
147 |
val (bsT, asT) = the (AList.lookup Type.could_unify bds (HOLogic.listT xT));
|
|
148 |
in
|
|
149 |
AList.update Type.could_unify (HOLogic.listT xT,(tl bsT, asT)) bds
|
|
150 |
end)),
|
|
151 |
bds)
|
|
152 |
end
|
|
153 |
| _ => da (s, ctxt) bds)
|
|
154 |
in
|
|
155 |
(case cgns of
|
|
156 |
[] => tryabsdecomp (t, ctxt) bds
|
|
157 |
| ((vns, cong) :: congs) =>
|
|
158 |
(let
|
|
159 |
val (tyenv, tmenv) =
|
|
160 |
Pattern.match thy
|
|
161 |
((fst o HOLogic.dest_eq o HOLogic.dest_Trueprop) (concl_of cong), t)
|
|
162 |
(Vartab.empty, Vartab.empty);
|
|
163 |
val (fnvs, invs) = List.partition (fn ((vn, _),_) => member (op =) vns vn) (Vartab.dest tmenv);
|
|
164 |
val (fts, its) =
|
|
165 |
(map (snd o snd) fnvs,
|
|
166 |
map (fn ((vn, vi), (tT, t)) => (cert (Var ((vn, vi), tT)), cert t)) invs);
|
52273
|
167 |
val ctyenv = map (fn ((vn, vi), (s, ty)) => (certT (TVar((vn, vi), s)), certT ty)) (Vartab.dest tyenv);
|
51726
|
168 |
in
|
|
169 |
((fts ~~ replicate (length fts) ctxt,
|
|
170 |
apfst (FWD (Drule.instantiate_normalize (ctyenv, its) cong))), bds)
|
52272
|
171 |
end handle Pattern.MATCH => decomp_reify da congs (t,ctxt) bds))
|
51726
|
172 |
end;
|
|
173 |
|
|
174 |
(* looks for the atoms equation and instantiates it with the right number *)
|
|
175 |
fun mk_decompatom eqs (t, ctxt) bds = (([], fn (_, bds) =>
|
|
176 |
let
|
|
177 |
val tT = fastype_of t;
|
|
178 |
fun isat eq =
|
|
179 |
let
|
|
180 |
val rhs = eq |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> snd;
|
|
181 |
in exists_Const
|
|
182 |
(fn (n, ty) => n = @{const_name "List.nth"}
|
|
183 |
andalso AList.defined Type.could_unify bds (domain_type ty)) rhs
|
|
184 |
andalso Type.could_unify (fastype_of rhs, tT)
|
|
185 |
end;
|
|
186 |
|
|
187 |
fun get_nths t acc =
|
|
188 |
case t of
|
|
189 |
Const(@{const_name "List.nth"}, _) $ vs $ n => insert (fn ((a, _), (b, _)) => a aconv b) (t, (vs, n)) acc
|
|
190 |
| t1 $ t2 => get_nths t1 (get_nths t2 acc)
|
|
191 |
| Abs (_ ,_ ,t') => get_nths t' acc
|
|
192 |
| _ => acc;
|
|
193 |
|
|
194 |
fun tryeqs [] bds = error "Can not find the atoms equation"
|
|
195 |
| tryeqs (eq :: eqs) bds = ((
|
|
196 |
let
|
|
197 |
val rhs = eq |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> snd;
|
|
198 |
val nths = get_nths rhs [];
|
|
199 |
val (vss, _) = fold_rev (fn (_, (vs, n)) => fn (vss, ns) =>
|
|
200 |
(insert (op aconv) vs vss, insert (op aconv) n ns)) nths ([], []);
|
|
201 |
val (vsns, ctxt') = Variable.variant_fixes (replicate (length vss) "vs") ctxt;
|
|
202 |
val (xns, ctxt'') = Variable.variant_fixes (replicate (length nths) "x") ctxt';
|
|
203 |
val thy = Proof_Context.theory_of ctxt'';
|
|
204 |
val cert = cterm_of thy;
|
|
205 |
val certT = ctyp_of thy;
|
|
206 |
val vsns_map = vss ~~ vsns;
|
|
207 |
val xns_map = fst (split_list nths) ~~ xns;
|
|
208 |
val subst = map (fn (nt, xn) => (nt, Var ((xn, 0), fastype_of nt))) xns_map;
|
|
209 |
val rhs_P = subst_free subst rhs;
|
|
210 |
val (tyenv, tmenv) = Pattern.match thy (rhs_P, t) (Vartab.empty, Vartab.empty);
|
|
211 |
val sbst = Envir.subst_term (tyenv, tmenv);
|
|
212 |
val sbsT = Envir.subst_type tyenv;
|
|
213 |
val subst_ty = map (fn (n, (s, t)) =>
|
|
214 |
(certT (TVar (n, s)), certT t)) (Vartab.dest tyenv)
|
|
215 |
val tml = Vartab.dest tmenv;
|
|
216 |
val (subst_ns, bds) = fold_map
|
|
217 |
(fn (Const _ $ _ $ n, Var (xn0, _)) => fn bds =>
|
|
218 |
let
|
|
219 |
val name = snd (the (AList.lookup (op =) tml xn0));
|
|
220 |
val (idx, bds) = index_of name bds;
|
|
221 |
in ((cert n, idx |> (HOLogic.mk_nat #> cert)), bds) end) subst bds;
|
|
222 |
val subst_vs =
|
|
223 |
let
|
|
224 |
fun h (Const _ $ (vs as Var (_, lT)) $ _, Var (_, T)) =
|
|
225 |
let
|
|
226 |
val cns = sbst (Const (@{const_name "List.Cons"}, T --> lT --> lT));
|
|
227 |
val lT' = sbsT lT;
|
|
228 |
val (bsT, _) = the (AList.lookup Type.could_unify bds lT);
|
|
229 |
val vsn = the (AList.lookup (op =) vsns_map vs);
|
|
230 |
val cvs = cert (fold_rev (fn x => fn xs => cns $ x $xs) bsT (Free (vsn, lT')));
|
|
231 |
in (cert vs, cvs) end;
|
|
232 |
in map h subst end;
|
|
233 |
val cts = map (fn ((vn, vi), (tT, t)) => (cert (Var ((vn, vi), tT)), cert t))
|
|
234 |
(fold (AList.delete (fn (((a : string), _), (b, _)) => a = b))
|
|
235 |
(map (fn n => (n, 0)) xns) tml);
|
|
236 |
val substt =
|
|
237 |
let
|
|
238 |
val ih = Drule.cterm_rule (Thm.instantiate (subst_ty, []));
|
|
239 |
in map (fn (v, t) => (ih v, ih t)) (subst_ns @ subst_vs @ cts) end;
|
|
240 |
val th = (Drule.instantiate_normalize (subst_ty, substt) eq) RS sym;
|
|
241 |
in (hd (Variable.export ctxt'' ctxt [th]), bds) end)
|
|
242 |
handle Pattern.MATCH => tryeqs eqs bds)
|
|
243 |
in tryeqs (filter isat eqs) bds end), bds);
|
|
244 |
|
|
245 |
(* Generic reification procedure: *)
|
|
246 |
(* creates all needed cong rules and then just uses the theorem synthesis *)
|
|
247 |
|
|
248 |
fun mk_congs ctxt eqs =
|
|
249 |
let
|
|
250 |
val fs = fold_rev (fn eq => insert (op =) (eq |> prop_of |> HOLogic.dest_Trueprop
|
|
251 |
|> HOLogic.dest_eq |> fst |> strip_comb
|
|
252 |
|> fst)) eqs [];
|
|
253 |
val tys = fold_rev (fn f => fold (insert (op =)) (f |> fastype_of |> binder_types |> tl)) fs [];
|
|
254 |
val (vs, ctxt') = Variable.variant_fixes (replicate (length tys) "vs") ctxt;
|
52273
|
255 |
val cert = cterm_of (Proof_Context.theory_of ctxt');
|
|
256 |
val subst =
|
|
257 |
the o AList.lookup (op =) (map2 (fn T => fn v => (T, cert (Free (v, T)))) tys vs);
|
51726
|
258 |
fun prep_eq eq =
|
|
259 |
let
|
|
260 |
val (_, _ :: vs) = eq |> prop_of |> HOLogic.dest_Trueprop
|
|
261 |
|> HOLogic.dest_eq |> fst |> strip_comb;
|
52273
|
262 |
val subst = map_filter (fn (v as Var (_, T)) => SOME (cert v, subst T)
|
|
263 |
| _ => NONE) vs;
|
51726
|
264 |
in Thm.instantiate ([], subst) eq end;
|
|
265 |
val (ps, congs) = map_split (mk_congeq ctxt' fs o prep_eq) eqs;
|
|
266 |
val bds = AList.make (K ([], [])) tys;
|
|
267 |
in (ps ~~ Variable.export ctxt' ctxt congs, bds) end
|
|
268 |
|
|
269 |
val (congs, bds) = mk_congs ctxt eqs;
|
|
270 |
val congs = rearrange congs;
|
52272
|
271 |
val (th, bds) = divide_and_conquer' (decomp_reify (mk_decompatom eqs) congs) (t,ctxt) bds;
|
52273
|
272 |
fun is_list_var (Var (_, t)) = can dest_listT t
|
|
273 |
| is_list_var _ = false;
|
51726
|
274 |
val vars = th |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> snd
|
52273
|
275 |
|> strip_comb |> snd |> filter is_list_var;
|
51726
|
276 |
val cert = cterm_of (Proof_Context.theory_of ctxt);
|
|
277 |
val cvs = map (fn (v as Var(_, t)) => (cert v,
|
|
278 |
the (AList.lookup Type.could_unify bds t) |> snd |> HOLogic.mk_list (dest_listT t) |> cert)) vars;
|
|
279 |
val th' = Drule.instantiate_normalize ([], cvs) th;
|
|
280 |
val t' = (fst o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) th';
|
|
281 |
val th'' = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop (HOLogic.mk_eq (t, t')))
|
|
282 |
(fn _ => simp_tac ctxt 1)
|
52273
|
283 |
in FWD trans [th'', th'] end;
|
51726
|
284 |
|
52272
|
285 |
fun reflect ctxt conv corr_thms eqs t =
|
51726
|
286 |
let
|
52272
|
287 |
val reify_thm = reify ctxt eqs t;
|
52273
|
288 |
fun try_corr corr_thm =
|
|
289 |
SOME (FWD trans [reify_thm, corr_thm RS sym]) handle THM _ => NONE;
|
52274
|
290 |
val refl_thm = case get_first try_corr corr_thms
|
51726
|
291 |
of NONE => error "No suitable correctness theorem found"
|
|
292 |
| SOME thm => thm;
|
52274
|
293 |
val rhs = (Thm.dest_arg o Thm.dest_arg o cprop_of) refl_thm;
|
|
294 |
val number_of_args = (length o snd o strip_comb o term_of) rhs;
|
|
295 |
val reified = Thm.dest_arg (fold_range (K Thm.dest_fun) (number_of_args - 1) rhs);
|
|
296 |
val evaluated = conv reified;
|
51726
|
297 |
in
|
52274
|
298 |
refl_thm
|
|
299 |
|> simplify (put_simpset HOL_basic_ss ctxt addsimps [evaluated])
|
51726
|
300 |
|> simplify (put_simpset HOL_basic_ss ctxt addsimps eqs addsimps @{thms nth_Cons_0 nth_Cons_Suc})
|
|
301 |
end;
|
|
302 |
|
|
303 |
fun tac_of_thm mk_thm to = SUBGOAL (fn (goal, i) =>
|
|
304 |
let
|
|
305 |
val t = (case to of NONE => HOLogic.dest_Trueprop goal | SOME t => t)
|
|
306 |
val thm = mk_thm t RS ssubst;
|
|
307 |
in rtac thm i end);
|
|
308 |
|
52272
|
309 |
fun reify_tac ctxt eqs = tac_of_thm (reify ctxt eqs);
|
51726
|
310 |
|
|
311 |
(*Reflection calls reification and uses the correctness theorem assumed to be the head of the list*)
|
52272
|
312 |
fun reflection_tac ctxt conv corr_thms eqs =
|
|
313 |
tac_of_thm (reflect ctxt conv corr_thms eqs);
|
51726
|
314 |
|
|
315 |
structure Data = Generic_Data
|
|
316 |
(
|
|
317 |
type T = thm list * thm list;
|
|
318 |
val empty = ([], []);
|
|
319 |
val extend = I;
|
|
320 |
fun merge ((ths1, rths1), (ths2, rths2)) =
|
|
321 |
(Thm.merge_thms (ths1, ths2), Thm.merge_thms (rths1, rths2));
|
|
322 |
);
|
|
323 |
|
|
324 |
fun get_default ctxt =
|
|
325 |
let
|
|
326 |
val (reification_eqs, correctness_thms) = Data.get (Context.Proof ctxt);
|
|
327 |
in { reification_eqs = reification_eqs, correctness_thms = correctness_thms } end;
|
|
328 |
|
|
329 |
val add_reification_eq = Thm.declaration_attribute (Data.map o apfst o Thm.add_thm);
|
|
330 |
val del_reification_eq = Thm.declaration_attribute (Data.map o apfst o Thm.del_thm);
|
|
331 |
val add_correctness_thm = Thm.declaration_attribute (Data.map o apsnd o Thm.add_thm);
|
|
332 |
val del_correctness_thm = Thm.declaration_attribute (Data.map o apsnd o Thm.del_thm);
|
|
333 |
|
|
334 |
val _ = Context.>> (Context.map_theory
|
|
335 |
(Attrib.setup @{binding reify}
|
|
336 |
(Attrib.add_del add_reification_eq del_reification_eq) "declare reification equations" #>
|
|
337 |
Attrib.setup @{binding reflection}
|
|
338 |
(Attrib.add_del add_correctness_thm del_correctness_thm) "declare reflection correctness theorems"));
|
|
339 |
|
|
340 |
fun default_reify_tac ctxt user_eqs =
|
|
341 |
let
|
|
342 |
val { reification_eqs = default_eqs, correctness_thms = _ } =
|
|
343 |
get_default ctxt;
|
|
344 |
val eqs = fold Thm.add_thm user_eqs default_eqs;
|
52272
|
345 |
in reify_tac ctxt eqs end;
|
51726
|
346 |
|
|
347 |
fun default_reflection_tac ctxt user_thms user_eqs =
|
|
348 |
let
|
|
349 |
val { reification_eqs = default_eqs, correctness_thms = default_thms } =
|
|
350 |
get_default ctxt;
|
|
351 |
val corr_thms = fold Thm.add_thm user_thms default_thms;
|
|
352 |
val eqs = fold Thm.add_thm user_eqs default_eqs;
|
|
353 |
val conv = Code_Evaluation.dynamic_conv (Proof_Context.theory_of ctxt);
|
|
354 |
(*FIXME why Code_Evaluation.dynamic_conv? very specific*)
|
52272
|
355 |
in reflection_tac ctxt conv corr_thms eqs end;
|
51726
|
356 |
|
|
357 |
|
|
358 |
end
|