22023
|
1 |
(* Title: Pure/Tools/codegen_func.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Florian Haftmann, TU Muenchen
|
|
4 |
|
22185
|
5 |
Basic handling of defining equations ("func"s) for code generator framework.
|
22023
|
6 |
*)
|
|
7 |
|
|
8 |
signature CODEGEN_FUNC =
|
|
9 |
sig
|
22185
|
10 |
val assert_rew: thm -> thm
|
22033
|
11 |
val mk_rew: thm -> thm list
|
22185
|
12 |
val assert_func: thm -> thm
|
22033
|
13 |
val mk_func: thm -> (CodegenConsts.const * thm) list
|
22185
|
14 |
val mk_head: thm -> CodegenConsts.const * thm
|
22033
|
15 |
val dest_func: thm -> (string * typ) * term list
|
|
16 |
val typ_func: thm -> typ
|
22185
|
17 |
|
22197
|
18 |
val inst_thm: sort Vartab.table -> thm -> thm
|
22033
|
19 |
val expand_eta: int -> thm -> thm
|
|
20 |
val rewrite_func: thm list -> thm -> thm
|
22211
|
21 |
val norm_args: thm list -> thm list
|
|
22 |
val norm_varnames: (string -> string) -> (string -> string) -> thm list -> thm list
|
22023
|
23 |
end;
|
|
24 |
|
|
25 |
structure CodegenFunc : CODEGEN_FUNC =
|
|
26 |
struct
|
|
27 |
|
22033
|
28 |
fun lift_thm_thy f thm = f (Thm.theory_of_thm thm) thm;
|
|
29 |
|
|
30 |
fun bad_thm msg thm =
|
|
31 |
error (msg ^ ": " ^ string_of_thm thm);
|
|
32 |
|
|
33 |
|
|
34 |
(* making rewrite theorems *)
|
|
35 |
|
22185
|
36 |
fun assert_rew thm =
|
22033
|
37 |
let
|
|
38 |
val thy = Thm.theory_of_thm thm;
|
|
39 |
val (lhs, rhs) = (Logic.dest_equals o Thm.prop_of) thm;
|
|
40 |
fun vars_of t = fold_aterms
|
|
41 |
(fn Var (v, _) => insert (op =) v
|
|
42 |
| Free _ => bad_thm "Illegal free variable in rewrite theorem" thm
|
|
43 |
| _ => I) t [];
|
|
44 |
fun tvars_of t = fold_term_types
|
|
45 |
(fn _ => fold_atyps (fn TVar (v, _) => insert (op =) v
|
|
46 |
| TFree _ => bad_thm "Illegal free type variable in rewrite theorem" thm)) t [];
|
|
47 |
val lhs_vs = vars_of lhs;
|
|
48 |
val rhs_vs = vars_of rhs;
|
|
49 |
val lhs_tvs = tvars_of lhs;
|
|
50 |
val rhs_tvs = tvars_of lhs;
|
|
51 |
val _ = if null (subtract (op =) lhs_vs rhs_vs)
|
|
52 |
then ()
|
|
53 |
else bad_thm "Free variables on right hand side of rewrite theorems" thm
|
|
54 |
val _ = if null (subtract (op =) lhs_tvs rhs_tvs)
|
|
55 |
then ()
|
|
56 |
else bad_thm "Free type variables on right hand side of rewrite theorems" thm
|
|
57 |
in thm end;
|
|
58 |
|
|
59 |
fun mk_rew thm =
|
22023
|
60 |
let
|
22033
|
61 |
val thy = Thm.theory_of_thm thm;
|
|
62 |
val thms = (#mk o #mk_rews o snd o MetaSimplifier.rep_ss o Simplifier.simpset_of) thy thm;
|
|
63 |
in
|
22185
|
64 |
map assert_rew thms
|
22033
|
65 |
end;
|
|
66 |
|
|
67 |
|
22197
|
68 |
(* making defining equations *)
|
22033
|
69 |
|
|
70 |
val typ_func = lift_thm_thy (fn thy => snd o dest_Const o fst o strip_comb
|
|
71 |
o fst o Logic.dest_equals o ObjectLogic.drop_judgment thy o Drule.plain_prop_of);
|
|
72 |
|
|
73 |
val dest_func = lift_thm_thy (fn thy => apfst dest_Const o strip_comb
|
|
74 |
o fst o Logic.dest_equals o ObjectLogic.drop_judgment thy o Drule.plain_prop_of
|
|
75 |
o Drule.fconv_rule Drule.beta_eta_conversion);
|
|
76 |
|
|
77 |
val mk_head = lift_thm_thy (fn thy => fn thm =>
|
|
78 |
((CodegenConsts.norm_of_typ thy o fst o dest_func) thm, thm));
|
22023
|
79 |
|
22185
|
80 |
fun assert_func thm = case try dest_func thm
|
22033
|
81 |
of SOME (c_ty as (c, ty), args) =>
|
|
82 |
let
|
|
83 |
val thy = Thm.theory_of_thm thm;
|
|
84 |
val _ =
|
|
85 |
if has_duplicates (op =)
|
|
86 |
((fold o fold_aterms) (fn Var (v, _) => cons v
|
|
87 |
| _ => I
|
|
88 |
) args [])
|
22197
|
89 |
then bad_thm "Repeated variables on left hand side of defining equation" thm
|
22033
|
90 |
else ()
|
22197
|
91 |
fun no_abs (Abs _) = bad_thm "Abstraction on left hand side of defining equation" thm
|
22033
|
92 |
| no_abs (t1 $ t2) = (no_abs t1; no_abs t2)
|
|
93 |
| no_abs _ = ();
|
|
94 |
val _ = map no_abs args;
|
22185
|
95 |
in thm end
|
22197
|
96 |
| NONE => bad_thm "Not a defining equation" thm;
|
22023
|
97 |
|
22185
|
98 |
val mk_func = map (mk_head o assert_func) o mk_rew;
|
22033
|
99 |
|
|
100 |
|
|
101 |
(* utilities *)
|
|
102 |
|
22197
|
103 |
fun inst_thm tvars' thm =
|
|
104 |
let
|
|
105 |
val thy = Thm.theory_of_thm thm;
|
|
106 |
val tvars = (Term.add_tvars o Thm.prop_of) thm [];
|
|
107 |
fun mk_inst (tvar as (v, _)) = case Vartab.lookup tvars' v
|
|
108 |
of SOME sort => SOME (pairself (Thm.ctyp_of thy o TVar) (tvar, (v, sort)))
|
|
109 |
| NONE => NONE;
|
|
110 |
val insts = map_filter mk_inst tvars;
|
|
111 |
in Thm.instantiate (insts, []) thm end;
|
|
112 |
|
22033
|
113 |
fun expand_eta k thm =
|
|
114 |
let
|
|
115 |
val thy = Thm.theory_of_thm thm;
|
22023
|
116 |
val (lhs, rhs) = (Logic.dest_equals o Drule.plain_prop_of) thm;
|
|
117 |
val (head, args) = strip_comb lhs;
|
|
118 |
val l = if k = ~1
|
|
119 |
then (length o fst o strip_abs) rhs
|
|
120 |
else Int.max (0, k - length args);
|
|
121 |
val used = Name.make_context (map (fst o fst) (Term.add_vars lhs []));
|
|
122 |
fun get_name _ 0 used = ([], used)
|
|
123 |
| get_name (Abs (v, ty, t)) k used =
|
|
124 |
used
|
22033
|
125 |
|> Name.variants [v]
|
22023
|
126 |
||>> get_name t (k - 1)
|
|
127 |
|>> (fn ([v'], vs') => (v', ty) :: vs')
|
|
128 |
| get_name t k used =
|
|
129 |
let
|
|
130 |
val (tys, _) = (strip_type o fastype_of) t
|
|
131 |
in case tys
|
|
132 |
of [] => raise TERM ("expand_eta", [t])
|
|
133 |
| ty :: _ =>
|
|
134 |
used
|
|
135 |
|> Name.variants [""]
|
|
136 |
|-> (fn [v] => get_name (t $ Var ((v, 0), ty)) (k - 1)
|
|
137 |
#>> (fn vs' => (v, ty) :: vs'))
|
|
138 |
end;
|
|
139 |
val (vs, _) = get_name rhs l used;
|
|
140 |
val vs_refl = map (fn (v, ty) => Thm.reflexive (Thm.cterm_of thy (Var ((v, 0), ty)))) vs;
|
|
141 |
in
|
|
142 |
fold (fn refl => fn thm => Thm.combination thm refl) vs_refl thm
|
|
143 |
end;
|
|
144 |
|
22033
|
145 |
fun rewrite_func rewrites thm =
|
|
146 |
let
|
|
147 |
val rewrite = MetaSimplifier.rewrite false rewrites;
|
|
148 |
val (ct_eq, [ct_lhs, ct_rhs]) = (Drule.strip_comb o Thm.cprop_of) thm;
|
|
149 |
val Const ("==", _) = Thm.term_of ct_eq;
|
|
150 |
val (ct_f, ct_args) = Drule.strip_comb ct_lhs;
|
|
151 |
val rhs' = rewrite ct_rhs;
|
|
152 |
val args' = map rewrite ct_args;
|
|
153 |
val lhs' = Thm.symmetric (fold (fn th1 => fn th2 => Thm.combination th2 th1)
|
|
154 |
args' (Thm.reflexive ct_f));
|
22197
|
155 |
in Thm.transitive (Thm.transitive lhs' thm) rhs' end;
|
22033
|
156 |
|
22211
|
157 |
fun norm_args thms =
|
|
158 |
let
|
|
159 |
val num_args_of = length o snd o strip_comb o fst o Logic.dest_equals;
|
|
160 |
val k = fold (curry Int.max o num_args_of o Drule.plain_prop_of) thms 0;
|
|
161 |
in
|
|
162 |
thms
|
|
163 |
|> map (expand_eta k)
|
|
164 |
|> map (Drule.fconv_rule Drule.beta_eta_conversion)
|
|
165 |
end;
|
|
166 |
|
|
167 |
fun canonical_tvars purify_tvar thm =
|
|
168 |
let
|
|
169 |
val ctyp = Thm.ctyp_of (Thm.theory_of_thm thm);
|
|
170 |
fun tvars_subst_for thm = (fold_types o fold_atyps)
|
|
171 |
(fn TVar (v_i as (v, _), sort) => let
|
|
172 |
val v' = purify_tvar v
|
|
173 |
in if v = v' then I
|
|
174 |
else insert (op =) (v_i, (v', sort)) end
|
|
175 |
| _ => I) (prop_of thm) [];
|
|
176 |
fun mk_inst (v_i, (v', sort)) (maxidx, acc) =
|
|
177 |
let
|
|
178 |
val ty = TVar (v_i, sort)
|
|
179 |
in
|
|
180 |
(maxidx + 1, (ctyp ty, ctyp (TVar ((v', maxidx), sort))) :: acc)
|
|
181 |
end;
|
|
182 |
val maxidx = Thm.maxidx_of thm + 1;
|
|
183 |
val (_, inst) = fold mk_inst (tvars_subst_for thm) (maxidx + 1, []);
|
|
184 |
in Thm.instantiate (inst, []) thm end;
|
|
185 |
|
|
186 |
fun canonical_vars purify_var thm =
|
|
187 |
let
|
|
188 |
val cterm = Thm.cterm_of (Thm.theory_of_thm thm);
|
|
189 |
fun vars_subst_for thm = fold_aterms
|
|
190 |
(fn Var (v_i as (v, _), ty) => let
|
|
191 |
val v' = purify_var v
|
|
192 |
in if v = v' then I
|
|
193 |
else insert (op =) (v_i, (v', ty)) end
|
|
194 |
| _ => I) (prop_of thm) [];
|
|
195 |
fun mk_inst (v_i as (v, i), (v', ty)) (maxidx, acc) =
|
|
196 |
let
|
|
197 |
val t = Var (v_i, ty)
|
|
198 |
in
|
|
199 |
(maxidx + 1, (cterm t, cterm (Var ((v', maxidx), ty))) :: acc)
|
|
200 |
end;
|
|
201 |
val maxidx = Thm.maxidx_of thm + 1;
|
|
202 |
val (_, inst) = fold mk_inst (vars_subst_for thm) (maxidx + 1, []);
|
|
203 |
in Thm.instantiate ([], inst) thm end;
|
|
204 |
|
|
205 |
fun canonical_absvars purify_var thm =
|
|
206 |
let
|
|
207 |
val t = Thm.prop_of thm;
|
|
208 |
val t' = Term.map_abs_vars purify_var t;
|
|
209 |
in Thm.rename_boundvars t t' thm end;
|
|
210 |
|
|
211 |
fun norm_varnames purify_tvar purify_var thms =
|
|
212 |
let
|
|
213 |
fun burrow_thms f [] = []
|
|
214 |
| burrow_thms f thms =
|
|
215 |
thms
|
|
216 |
|> Conjunction.intr_list
|
|
217 |
|> f
|
|
218 |
|> Conjunction.elim_list;
|
|
219 |
in
|
|
220 |
thms
|
|
221 |
|> burrow_thms (canonical_tvars purify_tvar)
|
|
222 |
|> map (canonical_vars purify_var)
|
|
223 |
|> map (canonical_absvars purify_var)
|
|
224 |
|> map Drule.zero_var_indexes
|
|
225 |
end;
|
|
226 |
|
22023
|
227 |
end;
|