author | haftmann |
Fri, 20 Apr 2007 11:21:35 +0200 | |
changeset 22737 | d87ccbcc2702 |
parent 22705 | 6199df39688d |
child 22804 | d3c23b90c6c6 |
permissions | -rw-r--r-- |
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 |
22737 | 11 |
val mk_rew: thm -> thm |
12 |
val mk_func: thm -> thm |
|
13 |
val head_func: thm -> CodegenConsts.const * typ |
|
14 |
val bad_thm: string -> 'a |
|
15 |
val error_thm: (thm -> thm) -> thm -> thm |
|
16 |
val warning_thm: (thm -> thm) -> thm -> thm option |
|
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 |
||
22737 | 28 |
|
29 |
(* auxiliary *) |
|
22033 | 30 |
|
22737 | 31 |
exception BAD_THM of string; |
32 |
fun bad_thm msg = raise BAD_THM msg; |
|
33 |
fun error_thm f thm = f thm handle BAD_THM msg => error msg; |
|
34 |
fun warning_thm f thm = SOME (f thm) handle BAD_THM msg |
|
35 |
=> (warning msg; NONE); |
|
22033 | 36 |
|
37 |
||
38 |
(* making rewrite theorems *) |
|
39 |
||
22185 | 40 |
fun assert_rew thm = |
22033 | 41 |
let |
42 |
val thy = Thm.theory_of_thm thm; |
|
22737 | 43 |
val (lhs, rhs) = (Logic.dest_equals o Thm.prop_of) thm |
44 |
handle TERM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm thm); |
|
22033 | 45 |
fun vars_of t = fold_aterms |
46 |
(fn Var (v, _) => insert (op =) v |
|
22737 | 47 |
| Free _ => bad_thm ("Illegal free variable in rewrite theorem\n" |
48 |
^ Display.string_of_thm thm) |
|
22033 | 49 |
| _ => I) t []; |
50 |
fun tvars_of t = fold_term_types |
|
51 |
(fn _ => fold_atyps (fn TVar (v, _) => insert (op =) v |
|
22737 | 52 |
| TFree _ => bad_thm |
53 |
("Illegal free type variable in rewrite theorem\n" ^ Display.string_of_thm thm))) t []; |
|
22033 | 54 |
val lhs_vs = vars_of lhs; |
55 |
val rhs_vs = vars_of rhs; |
|
56 |
val lhs_tvs = tvars_of lhs; |
|
57 |
val rhs_tvs = tvars_of lhs; |
|
58 |
val _ = if null (subtract (op =) lhs_vs rhs_vs) |
|
59 |
then () |
|
22737 | 60 |
else bad_thm ("Free variables on right hand side of rewrite theorem\n" |
61 |
^ Display.string_of_thm thm); |
|
22033 | 62 |
val _ = if null (subtract (op =) lhs_tvs rhs_tvs) |
63 |
then () |
|
22737 | 64 |
else bad_thm ("Free type variables on right hand side of rewrite theorem\n" |
65 |
^ Display.string_of_thm thm) |
|
22033 | 66 |
in thm end; |
67 |
||
68 |
fun mk_rew thm = |
|
22023 | 69 |
let |
22033 | 70 |
val thy = Thm.theory_of_thm thm; |
22737 | 71 |
val ctxt = ProofContext.init thy; |
22033 | 72 |
in |
22737 | 73 |
thm |
74 |
|> LocalDefs.meta_rewrite_rule ctxt |
|
75 |
|> assert_rew |
|
22033 | 76 |
end; |
77 |
||
78 |
||
22197 | 79 |
(* making defining equations *) |
22033 | 80 |
|
22737 | 81 |
fun assert_func thm = |
82 |
let |
|
83 |
val thy = Thm.theory_of_thm thm; |
|
84 |
val args = (snd o strip_comb o fst o Logic.dest_equals |
|
85 |
o ObjectLogic.drop_judgment thy o Thm.plain_prop_of) thm; |
|
86 |
val _ = |
|
87 |
if has_duplicates (op =) |
|
88 |
((fold o fold_aterms) (fn Var (v, _) => cons v |
|
89 |
| _ => I |
|
90 |
) args []) |
|
91 |
then bad_thm ("Duplicated variables on left hand side of equation\n" |
|
92 |
^ Display.string_of_thm thm) |
|
93 |
else () |
|
94 |
fun check _ (Abs _) = bad_thm |
|
95 |
("Abstraction on left hand side of equation\n" |
|
96 |
^ Display.string_of_thm thm) |
|
97 |
| check 0 (Var _) = () |
|
98 |
| check _ (Var _) = bad_thm |
|
99 |
("Variable with application on left hand side of defining equation\n" |
|
100 |
^ Display.string_of_thm thm) |
|
101 |
| check n (t1 $ t2) = (check (n+1) t1; check 0 t2) |
|
102 |
| check n (Const (_, ty)) = if n <> (length o fst o strip_type) ty |
|
103 |
then bad_thm |
|
104 |
("Partially applied constant on left hand side of equation" |
|
105 |
^ Display.string_of_thm thm) |
|
106 |
else (); |
|
107 |
val _ = map (check 0) args; |
|
108 |
in thm end; |
|
22484
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
109 |
|
22737 | 110 |
val mk_func = assert_func o Drule.fconv_rule Drule.beta_eta_conversion o mk_rew; |
22023 | 111 |
|
22737 | 112 |
fun head_func thm = |
113 |
let |
|
114 |
val thy = Thm.theory_of_thm thm; |
|
115 |
val (Const (c_ty as (_, ty))) = (fst o strip_comb o fst o Logic.dest_equals |
|
116 |
o ObjectLogic.drop_judgment thy o Thm.plain_prop_of) thm; |
|
117 |
val const = CodegenConsts.const_of_cexpr thy c_ty; |
|
118 |
in (const, ty) end; |
|
22033 | 119 |
|
120 |
||
121 |
(* utilities *) |
|
122 |
||
22197 | 123 |
fun inst_thm tvars' thm = |
124 |
let |
|
125 |
val thy = Thm.theory_of_thm thm; |
|
126 |
val tvars = (Term.add_tvars o Thm.prop_of) thm []; |
|
127 |
fun mk_inst (tvar as (v, _)) = case Vartab.lookup tvars' v |
|
128 |
of SOME sort => SOME (pairself (Thm.ctyp_of thy o TVar) (tvar, (v, sort))) |
|
129 |
| NONE => NONE; |
|
130 |
val insts = map_filter mk_inst tvars; |
|
131 |
in Thm.instantiate (insts, []) thm end; |
|
132 |
||
22033 | 133 |
fun expand_eta k thm = |
134 |
let |
|
135 |
val thy = Thm.theory_of_thm thm; |
|
22705 | 136 |
val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm; |
22023 | 137 |
val (head, args) = strip_comb lhs; |
138 |
val l = if k = ~1 |
|
139 |
then (length o fst o strip_abs) rhs |
|
140 |
else Int.max (0, k - length args); |
|
141 |
val used = Name.make_context (map (fst o fst) (Term.add_vars lhs [])); |
|
142 |
fun get_name _ 0 used = ([], used) |
|
143 |
| get_name (Abs (v, ty, t)) k used = |
|
144 |
used |
|
22033 | 145 |
|> Name.variants [v] |
22023 | 146 |
||>> get_name t (k - 1) |
147 |
|>> (fn ([v'], vs') => (v', ty) :: vs') |
|
148 |
| get_name t k used = |
|
149 |
let |
|
150 |
val (tys, _) = (strip_type o fastype_of) t |
|
151 |
in case tys |
|
152 |
of [] => raise TERM ("expand_eta", [t]) |
|
153 |
| ty :: _ => |
|
154 |
used |
|
155 |
|> Name.variants [""] |
|
156 |
|-> (fn [v] => get_name (t $ Var ((v, 0), ty)) (k - 1) |
|
157 |
#>> (fn vs' => (v, ty) :: vs')) |
|
158 |
end; |
|
159 |
val (vs, _) = get_name rhs l used; |
|
160 |
val vs_refl = map (fn (v, ty) => Thm.reflexive (Thm.cterm_of thy (Var ((v, 0), ty)))) vs; |
|
161 |
in |
|
162 |
fold (fn refl => fn thm => Thm.combination thm refl) vs_refl thm |
|
163 |
end; |
|
164 |
||
22033 | 165 |
fun rewrite_func rewrites thm = |
166 |
let |
|
167 |
val rewrite = MetaSimplifier.rewrite false rewrites; |
|
168 |
val (ct_eq, [ct_lhs, ct_rhs]) = (Drule.strip_comb o Thm.cprop_of) thm; |
|
169 |
val Const ("==", _) = Thm.term_of ct_eq; |
|
170 |
val (ct_f, ct_args) = Drule.strip_comb ct_lhs; |
|
171 |
val rhs' = rewrite ct_rhs; |
|
172 |
val args' = map rewrite ct_args; |
|
173 |
val lhs' = Thm.symmetric (fold (fn th1 => fn th2 => Thm.combination th2 th1) |
|
174 |
args' (Thm.reflexive ct_f)); |
|
22197 | 175 |
in Thm.transitive (Thm.transitive lhs' thm) rhs' end; |
22033 | 176 |
|
22211 | 177 |
fun norm_args thms = |
178 |
let |
|
179 |
val num_args_of = length o snd o strip_comb o fst o Logic.dest_equals; |
|
22705 | 180 |
val k = fold (curry Int.max o num_args_of o Thm.plain_prop_of) thms 0; |
22211 | 181 |
in |
182 |
thms |
|
183 |
|> map (expand_eta k) |
|
184 |
|> map (Drule.fconv_rule Drule.beta_eta_conversion) |
|
185 |
end; |
|
186 |
||
187 |
fun canonical_tvars purify_tvar thm = |
|
188 |
let |
|
189 |
val ctyp = Thm.ctyp_of (Thm.theory_of_thm thm); |
|
190 |
fun tvars_subst_for thm = (fold_types o fold_atyps) |
|
191 |
(fn TVar (v_i as (v, _), sort) => let |
|
192 |
val v' = purify_tvar v |
|
193 |
in if v = v' then I |
|
194 |
else insert (op =) (v_i, (v', sort)) end |
|
195 |
| _ => I) (prop_of thm) []; |
|
196 |
fun mk_inst (v_i, (v', sort)) (maxidx, acc) = |
|
197 |
let |
|
198 |
val ty = TVar (v_i, sort) |
|
199 |
in |
|
200 |
(maxidx + 1, (ctyp ty, ctyp (TVar ((v', maxidx), sort))) :: acc) |
|
201 |
end; |
|
202 |
val maxidx = Thm.maxidx_of thm + 1; |
|
203 |
val (_, inst) = fold mk_inst (tvars_subst_for thm) (maxidx + 1, []); |
|
204 |
in Thm.instantiate (inst, []) thm end; |
|
205 |
||
206 |
fun canonical_vars purify_var thm = |
|
207 |
let |
|
208 |
val cterm = Thm.cterm_of (Thm.theory_of_thm thm); |
|
209 |
fun vars_subst_for thm = fold_aterms |
|
210 |
(fn Var (v_i as (v, _), ty) => let |
|
211 |
val v' = purify_var v |
|
212 |
in if v = v' then I |
|
213 |
else insert (op =) (v_i, (v', ty)) end |
|
214 |
| _ => I) (prop_of thm) []; |
|
215 |
fun mk_inst (v_i as (v, i), (v', ty)) (maxidx, acc) = |
|
216 |
let |
|
217 |
val t = Var (v_i, ty) |
|
218 |
in |
|
219 |
(maxidx + 1, (cterm t, cterm (Var ((v', maxidx), ty))) :: acc) |
|
220 |
end; |
|
221 |
val maxidx = Thm.maxidx_of thm + 1; |
|
222 |
val (_, inst) = fold mk_inst (vars_subst_for thm) (maxidx + 1, []); |
|
223 |
in Thm.instantiate ([], inst) thm end; |
|
224 |
||
225 |
fun canonical_absvars purify_var thm = |
|
226 |
let |
|
227 |
val t = Thm.prop_of thm; |
|
228 |
val t' = Term.map_abs_vars purify_var t; |
|
229 |
in Thm.rename_boundvars t t' thm end; |
|
230 |
||
231 |
fun norm_varnames purify_tvar purify_var thms = |
|
232 |
let |
|
233 |
fun burrow_thms f [] = [] |
|
234 |
| burrow_thms f thms = |
|
235 |
thms |
|
236 |
|> Conjunction.intr_list |
|
237 |
|> f |
|
238 |
|> Conjunction.elim_list; |
|
239 |
in |
|
240 |
thms |
|
241 |
|> burrow_thms (canonical_tvars purify_tvar) |
|
242 |
|> map (canonical_vars purify_var) |
|
243 |
|> map (canonical_absvars purify_var) |
|
244 |
|> map Drule.zero_var_indexes |
|
245 |
end; |
|
246 |
||
22023 | 247 |
end; |