author | haftmann |
Tue, 20 Mar 2007 15:52:41 +0100 | |
changeset 22484 | 25dfebd7b4c8 |
parent 22475 | bd3378255cc8 |
child 22554 | d1499fff65d8 |
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 |
22033 | 11 |
val mk_rew: thm -> thm list |
22484
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
12 |
val assert_func: bool -> thm -> thm option |
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
13 |
val mk_func: bool -> 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 |
|
22484
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
80 |
local |
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
81 |
|
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
82 |
exception BAD of string; |
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
83 |
|
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
84 |
fun handle_bad strict thm msg = |
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
85 |
if strict then error (msg ^ ": " ^ string_of_thm thm) |
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
86 |
else (warning (msg ^ ": " ^ string_of_thm thm); NONE); |
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
87 |
|
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
88 |
in |
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
89 |
|
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
90 |
fun assert_func strict thm = case try dest_func thm |
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
91 |
of SOME (c_ty as (c, ty), args) => ( |
22033 | 92 |
let |
93 |
val thy = Thm.theory_of_thm thm; |
|
94 |
val _ = |
|
95 |
if has_duplicates (op =) |
|
96 |
((fold o fold_aterms) (fn Var (v, _) => cons v |
|
97 |
| _ => I |
|
98 |
) args []) |
|
22484
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
99 |
then raise BAD "Repeated variables on left hand side of defining equation" |
22033 | 100 |
else () |
22484
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
101 |
fun check _ (Abs _) = raise BAD |
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
102 |
"Abstraction on left hand side of defining equation" |
22475 | 103 |
| check 0 (Var _) = () |
22484
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
104 |
| check _ (Var _) = raise BAD |
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
105 |
"Variable with application on left hand side of defining equation" |
22475 | 106 |
| check n (t1 $ t2) = (check (n+1) t1; check 0 t2) |
107 |
| check n (Const (_, ty)) = if n <> (length o fst o strip_type) ty |
|
22484
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
108 |
then raise BAD |
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
109 |
("Partially applied constant on left hand side of defining equation") |
22475 | 110 |
else (); |
111 |
val _ = map (check 0) args; |
|
22484
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
112 |
in SOME thm end handle BAD msg => handle_bad strict thm msg) |
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
113 |
| NONE => handle_bad strict thm "Not a defining equation"; |
22023 | 114 |
|
22484
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
115 |
end; |
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
116 |
|
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22475
diff
changeset
|
117 |
fun mk_func strict = map_filter (Option.map mk_head o assert_func strict) o mk_rew; |
22033 | 118 |
|
119 |
||
120 |
(* utilities *) |
|
121 |
||
22197 | 122 |
fun inst_thm tvars' thm = |
123 |
let |
|
124 |
val thy = Thm.theory_of_thm thm; |
|
125 |
val tvars = (Term.add_tvars o Thm.prop_of) thm []; |
|
126 |
fun mk_inst (tvar as (v, _)) = case Vartab.lookup tvars' v |
|
127 |
of SOME sort => SOME (pairself (Thm.ctyp_of thy o TVar) (tvar, (v, sort))) |
|
128 |
| NONE => NONE; |
|
129 |
val insts = map_filter mk_inst tvars; |
|
130 |
in Thm.instantiate (insts, []) thm end; |
|
131 |
||
22033 | 132 |
fun expand_eta k thm = |
133 |
let |
|
134 |
val thy = Thm.theory_of_thm thm; |
|
22023 | 135 |
val (lhs, rhs) = (Logic.dest_equals o Drule.plain_prop_of) thm; |
136 |
val (head, args) = strip_comb lhs; |
|
137 |
val l = if k = ~1 |
|
138 |
then (length o fst o strip_abs) rhs |
|
139 |
else Int.max (0, k - length args); |
|
140 |
val used = Name.make_context (map (fst o fst) (Term.add_vars lhs [])); |
|
141 |
fun get_name _ 0 used = ([], used) |
|
142 |
| get_name (Abs (v, ty, t)) k used = |
|
143 |
used |
|
22033 | 144 |
|> Name.variants [v] |
22023 | 145 |
||>> get_name t (k - 1) |
146 |
|>> (fn ([v'], vs') => (v', ty) :: vs') |
|
147 |
| get_name t k used = |
|
148 |
let |
|
149 |
val (tys, _) = (strip_type o fastype_of) t |
|
150 |
in case tys |
|
151 |
of [] => raise TERM ("expand_eta", [t]) |
|
152 |
| ty :: _ => |
|
153 |
used |
|
154 |
|> Name.variants [""] |
|
155 |
|-> (fn [v] => get_name (t $ Var ((v, 0), ty)) (k - 1) |
|
156 |
#>> (fn vs' => (v, ty) :: vs')) |
|
157 |
end; |
|
158 |
val (vs, _) = get_name rhs l used; |
|
159 |
val vs_refl = map (fn (v, ty) => Thm.reflexive (Thm.cterm_of thy (Var ((v, 0), ty)))) vs; |
|
160 |
in |
|
161 |
fold (fn refl => fn thm => Thm.combination thm refl) vs_refl thm |
|
162 |
end; |
|
163 |
||
22033 | 164 |
fun rewrite_func rewrites thm = |
165 |
let |
|
166 |
val rewrite = MetaSimplifier.rewrite false rewrites; |
|
167 |
val (ct_eq, [ct_lhs, ct_rhs]) = (Drule.strip_comb o Thm.cprop_of) thm; |
|
168 |
val Const ("==", _) = Thm.term_of ct_eq; |
|
169 |
val (ct_f, ct_args) = Drule.strip_comb ct_lhs; |
|
170 |
val rhs' = rewrite ct_rhs; |
|
171 |
val args' = map rewrite ct_args; |
|
172 |
val lhs' = Thm.symmetric (fold (fn th1 => fn th2 => Thm.combination th2 th1) |
|
173 |
args' (Thm.reflexive ct_f)); |
|
22197 | 174 |
in Thm.transitive (Thm.transitive lhs' thm) rhs' end; |
22033 | 175 |
|
22211 | 176 |
fun norm_args thms = |
177 |
let |
|
178 |
val num_args_of = length o snd o strip_comb o fst o Logic.dest_equals; |
|
179 |
val k = fold (curry Int.max o num_args_of o Drule.plain_prop_of) thms 0; |
|
180 |
in |
|
181 |
thms |
|
182 |
|> map (expand_eta k) |
|
183 |
|> map (Drule.fconv_rule Drule.beta_eta_conversion) |
|
184 |
end; |
|
185 |
||
186 |
fun canonical_tvars purify_tvar thm = |
|
187 |
let |
|
188 |
val ctyp = Thm.ctyp_of (Thm.theory_of_thm thm); |
|
189 |
fun tvars_subst_for thm = (fold_types o fold_atyps) |
|
190 |
(fn TVar (v_i as (v, _), sort) => let |
|
191 |
val v' = purify_tvar v |
|
192 |
in if v = v' then I |
|
193 |
else insert (op =) (v_i, (v', sort)) end |
|
194 |
| _ => I) (prop_of thm) []; |
|
195 |
fun mk_inst (v_i, (v', sort)) (maxidx, acc) = |
|
196 |
let |
|
197 |
val ty = TVar (v_i, sort) |
|
198 |
in |
|
199 |
(maxidx + 1, (ctyp ty, ctyp (TVar ((v', maxidx), sort))) :: acc) |
|
200 |
end; |
|
201 |
val maxidx = Thm.maxidx_of thm + 1; |
|
202 |
val (_, inst) = fold mk_inst (tvars_subst_for thm) (maxidx + 1, []); |
|
203 |
in Thm.instantiate (inst, []) thm end; |
|
204 |
||
205 |
fun canonical_vars purify_var thm = |
|
206 |
let |
|
207 |
val cterm = Thm.cterm_of (Thm.theory_of_thm thm); |
|
208 |
fun vars_subst_for thm = fold_aterms |
|
209 |
(fn Var (v_i as (v, _), ty) => let |
|
210 |
val v' = purify_var v |
|
211 |
in if v = v' then I |
|
212 |
else insert (op =) (v_i, (v', ty)) end |
|
213 |
| _ => I) (prop_of thm) []; |
|
214 |
fun mk_inst (v_i as (v, i), (v', ty)) (maxidx, acc) = |
|
215 |
let |
|
216 |
val t = Var (v_i, ty) |
|
217 |
in |
|
218 |
(maxidx + 1, (cterm t, cterm (Var ((v', maxidx), ty))) :: acc) |
|
219 |
end; |
|
220 |
val maxidx = Thm.maxidx_of thm + 1; |
|
221 |
val (_, inst) = fold mk_inst (vars_subst_for thm) (maxidx + 1, []); |
|
222 |
in Thm.instantiate ([], inst) thm end; |
|
223 |
||
224 |
fun canonical_absvars purify_var thm = |
|
225 |
let |
|
226 |
val t = Thm.prop_of thm; |
|
227 |
val t' = Term.map_abs_vars purify_var t; |
|
228 |
in Thm.rename_boundvars t t' thm end; |
|
229 |
||
230 |
fun norm_varnames purify_tvar purify_var thms = |
|
231 |
let |
|
232 |
fun burrow_thms f [] = [] |
|
233 |
| burrow_thms f thms = |
|
234 |
thms |
|
235 |
|> Conjunction.intr_list |
|
236 |
|> f |
|
237 |
|> Conjunction.elim_list; |
|
238 |
in |
|
239 |
thms |
|
240 |
|> burrow_thms (canonical_tvars purify_tvar) |
|
241 |
|> map (canonical_vars purify_var) |
|
242 |
|> map (canonical_absvars purify_var) |
|
243 |
|> map Drule.zero_var_indexes |
|
244 |
end; |
|
245 |
||
22023 | 246 |
end; |