author | haftmann |
Mon, 14 Aug 2006 13:46:21 +0200 | |
changeset 20386 | d1cbe5aa6bf2 |
parent 20353 | d73e49780ef2 |
child 20394 | 21227c43ba26 |
permissions | -rw-r--r-- |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
1 |
(* Title: Pure/Tools/codegen_theorems.ML |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
2 |
ID: $Id$ |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
3 |
Author: Florian Haftmann, TU Muenchen |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
4 |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
5 |
Theorems used for code generation. |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
6 |
*) |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
7 |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
8 |
signature CODEGEN_THEOREMS = |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
9 |
sig |
19884 | 10 |
val add_notify: ((string * typ) list option -> theory -> theory) -> theory -> theory; |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
11 |
val add_preproc: (theory -> thm list -> thm list) -> theory -> theory; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
12 |
val add_fun_extr: (theory -> string * typ -> thm list) -> theory -> theory; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
13 |
val add_datatype_extr: (theory -> string |
19597 | 14 |
-> (((string * sort) list * (string * typ list) list) * tactic) option) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
15 |
-> theory -> theory; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
16 |
val add_fun: thm -> theory -> theory; |
19597 | 17 |
val del_fun: thm -> theory -> theory; |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
18 |
val add_unfold: thm -> theory -> theory; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
19 |
val del_unfold: thm -> theory -> theory; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
20 |
val purge_defs: string * typ -> theory -> theory; |
19597 | 21 |
val notify_dirty: theory -> theory; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
22 |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19897
diff
changeset
|
23 |
val extr_typ: theory -> thm -> typ; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
24 |
val common_typ: theory -> (thm -> typ) -> thm list -> thm list; |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19897
diff
changeset
|
25 |
val preprocess: theory -> thm list -> thm list; |
19597 | 26 |
|
27 |
type thmtab; |
|
20353 | 28 |
val mk_thmtab: theory -> (string * typ) list -> thmtab; |
20191 | 29 |
val get_sortalgebra: thmtab -> Sorts.algebra; |
30 |
val get_dtyp_of_cons: thmtab -> string * typ -> string option; |
|
31 |
val get_dtyp_spec: thmtab -> string |
|
32 |
-> ((string * sort) list * (string * typ list) list) option; |
|
33 |
val get_fun_thms: thmtab -> string * typ -> thm list; |
|
34 |
||
19597 | 35 |
val print_thms: theory -> unit; |
36 |
||
37 |
val init_obj: (thm * thm) * (thm * thm) -> theory -> theory; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
38 |
val debug: bool ref; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
39 |
val debug_msg: ('a -> string) -> 'a -> 'a; |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
40 |
end; |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
41 |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
42 |
structure CodegenTheorems: CODEGEN_THEOREMS = |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
43 |
struct |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
44 |
|
19597 | 45 |
(** preliminaries **) |
46 |
||
47 |
(* diagnostics *) |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
48 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
49 |
val debug = ref false; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
50 |
fun debug_msg f x = (if !debug then Output.debug (f x) else (); x); |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
51 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
52 |
|
19597 | 53 |
(* auxiliary *) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
54 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
55 |
fun getf_first [] _ = NONE |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
56 |
| getf_first (f::fs) x = case f x |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
57 |
of NONE => getf_first fs x |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
58 |
| y as SOME x => y; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
59 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
60 |
fun getf_first_list [] x = [] |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
61 |
| getf_first_list (f::fs) x = case f x |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
62 |
of [] => getf_first_list fs x |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
63 |
| xs => xs; |
19597 | 64 |
|
65 |
||
66 |
(* object logic setup *) |
|
67 |
||
68 |
structure CodegenTheoremsSetup = TheoryDataFun |
|
69 |
(struct |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19897
diff
changeset
|
70 |
val name = "Pure/codegen_theorems_setup"; |
19597 | 71 |
type T = ((string * thm) * ((string * string) * (string * string))) option; |
72 |
val empty = NONE; |
|
73 |
val copy = I; |
|
74 |
val extend = I; |
|
75 |
fun merge pp = merge_opt (eq_pair (eq_pair (op =) eq_thm) |
|
76 |
(eq_pair (eq_pair (op =) (op =)) (eq_pair (op =) (op =)))) : T * T -> T; |
|
77 |
fun print thy data = (); |
|
78 |
end); |
|
79 |
||
80 |
val _ = Context.add_setup CodegenTheoremsSetup.init; |
|
81 |
||
82 |
fun init_obj ((TrueI, FalseE), (conjI, atomize_eq)) thy = |
|
83 |
case CodegenTheoremsSetup.get thy |
|
84 |
of SOME _ => error "code generator already set up for object logic" |
|
20191 | 85 |
| NONE => |
19597 | 86 |
let |
87 |
fun strip_implies t = (Logic.strip_imp_prems t, Logic.strip_imp_concl t); |
|
88 |
fun dest_TrueI thm = |
|
89 |
Drule.plain_prop_of thm |
|
90 |
|> ObjectLogic.drop_judgment thy |
|
91 |
|> Term.dest_Const |
|
92 |
|> apsnd ( |
|
93 |
Term.dest_Type |
|
94 |
#> fst |
|
95 |
); |
|
96 |
fun dest_FalseE thm = |
|
97 |
Drule.plain_prop_of thm |
|
98 |
|> Logic.dest_implies |
|
99 |
|> apsnd ( |
|
100 |
ObjectLogic.drop_judgment thy |
|
101 |
#> Term.dest_Var |
|
102 |
) |
|
103 |
|> fst |
|
104 |
|> ObjectLogic.drop_judgment thy |
|
105 |
|> Term.dest_Const |
|
106 |
|> fst; |
|
107 |
fun dest_conjI thm = |
|
108 |
Drule.plain_prop_of thm |
|
109 |
|> strip_implies |
|
110 |
|> apfst (map (ObjectLogic.drop_judgment thy #> Term.dest_Var)) |
|
111 |
|> apsnd ( |
|
112 |
ObjectLogic.drop_judgment thy |
|
113 |
#> Term.strip_comb |
|
114 |
#> apsnd (map Term.dest_Var) |
|
115 |
#> apfst Term.dest_Const |
|
116 |
) |
|
117 |
|> (fn (v1, ((conj, _), v2)) => if v1 = v2 then conj else error "wrong premise") |
|
20191 | 118 |
fun dest_atomize_eq thm= |
19597 | 119 |
Drule.plain_prop_of thm |
120 |
|> Logic.dest_equals |
|
121 |
|> apfst ( |
|
122 |
ObjectLogic.drop_judgment thy |
|
123 |
#> Term.strip_comb |
|
124 |
#> apsnd (map Term.dest_Var) |
|
125 |
#> apfst Term.dest_Const |
|
126 |
) |
|
127 |
|> apsnd ( |
|
128 |
Logic.dest_equals |
|
129 |
#> apfst Term.dest_Var |
|
130 |
#> apsnd Term.dest_Var |
|
131 |
) |
|
132 |
|> (fn (((eq, _), v2), (v1a as (_, TVar (_, sort)), v1b)) => |
|
133 |
if [v1a, v1b] = v2 andalso sort = Sign.defaultS thy then eq else error "wrong premise") |
|
134 |
in |
|
135 |
((dest_TrueI TrueI, [dest_FalseE FalseE, dest_conjI conjI, dest_atomize_eq atomize_eq]) |
|
136 |
handle _ => error "bad code generator setup") |
|
137 |
|> (fn ((tr, b), [fl, con, eq]) => CodegenTheoremsSetup.put |
|
138 |
(SOME ((b, atomize_eq), ((tr, fl), (con, eq)))) thy) |
|
139 |
end; |
|
140 |
||
141 |
fun get_obj thy = |
|
142 |
case CodegenTheoremsSetup.get thy |
|
143 |
of SOME ((b, atomize), x) => ((Type (b, []), atomize) ,x) |
|
144 |
| NONE => error "no object logic setup for code theorems"; |
|
145 |
||
146 |
fun mk_true thy = |
|
147 |
let |
|
148 |
val ((b, _), ((tr, fl), (con, eq))) = get_obj thy; |
|
149 |
in Const (tr, b) end; |
|
150 |
||
151 |
fun mk_false thy = |
|
152 |
let |
|
153 |
val ((b, _), ((tr, fl), (con, eq))) = get_obj thy; |
|
154 |
in Const (fl, b) end; |
|
155 |
||
156 |
fun mk_obj_conj thy (x, y) = |
|
157 |
let |
|
158 |
val ((b, _), ((tr, fl), (con, eq))) = get_obj thy; |
|
159 |
in Const (con, b --> b --> b) $ x $ y end; |
|
160 |
||
161 |
fun mk_obj_eq thy (x, y) = |
|
162 |
let |
|
163 |
val ((b, _), ((tr, fl), (con, eq))) = get_obj thy; |
|
20105 | 164 |
in Const (eq, fastype_of x --> fastype_of y --> b) $ x $ y end; |
19597 | 165 |
|
166 |
fun is_obj_eq thy c = |
|
167 |
let |
|
168 |
val ((b, _), ((tr, fl), (con, eq))) = get_obj thy; |
|
169 |
in c = eq end; |
|
170 |
||
171 |
fun mk_func thy ((x, y), rhs) = |
|
172 |
Logic.mk_equals ( |
|
173 |
(mk_obj_eq thy (x, y)), |
|
174 |
rhs |
|
175 |
); |
|
176 |
||
177 |
||
178 |
(* theorem purification *) |
|
179 |
||
180 |
fun err_thm msg thm = |
|
181 |
error (msg ^ ": " ^ string_of_thm thm); |
|
182 |
||
183 |
fun abs_norm thy thm = |
|
184 |
let |
|
185 |
fun expvars t = |
|
186 |
let |
|
187 |
val lhs = (fst o Logic.dest_equals) t; |
|
20105 | 188 |
val tys = (fst o strip_type o fastype_of) lhs; |
19597 | 189 |
val used = fold_aterms (fn Var ((v, _), _) => insert (op =) v | _ => I) lhs []; |
20076 | 190 |
val vs = Name.invent_list used "x" (length tys); |
19597 | 191 |
in |
192 |
map2 (fn v => fn ty => Var ((v, 0), ty)) vs tys |
|
193 |
end; |
|
194 |
fun expand ct thm = |
|
195 |
Thm.combination thm (Thm.reflexive ct); |
|
196 |
fun beta_norm thm = |
|
197 |
thm |
|
198 |
|> prop_of |
|
199 |
|> Logic.dest_equals |
|
200 |
|> fst |
|
201 |
|> cterm_of thy |
|
202 |
|> Thm.beta_conversion true |
|
203 |
|> Thm.symmetric |
|
204 |
|> (fn thm' => Thm.transitive thm' thm); |
|
205 |
in |
|
206 |
thm |
|
207 |
|> fold (expand o cterm_of thy) ((expvars o prop_of) thm) |
|
208 |
|> beta_norm |
|
209 |
end; |
|
210 |
||
211 |
fun canonical_tvars thy thm = |
|
212 |
let |
|
213 |
fun mk_inst (v_i as (v, i), (v', sort)) (s as (maxidx, set, acc)) = |
|
214 |
if v = v' orelse member (op =) set v then s |
|
215 |
else let |
|
216 |
val ty = TVar (v_i, sort) |
|
217 |
in |
|
218 |
(maxidx + 1, v :: set, |
|
219 |
(ctyp_of thy ty, ctyp_of thy (TVar ((v', maxidx), sort))) :: acc) |
|
220 |
end; |
|
221 |
fun tvars_of thm = (fold_types o fold_atyps) |
|
20386 | 222 |
(fn TVar (v_i as (v, i), sort) => cons (v_i, (CodegenNames.purify_var v, sort)) |
19597 | 223 |
| _ => I) (prop_of thm) []; |
224 |
val maxidx = Thm.maxidx_of thm + 1; |
|
225 |
val (_, _, inst) = fold mk_inst (tvars_of thm) (maxidx + 1, [], []); |
|
226 |
in Thm.instantiate (inst, []) thm end; |
|
227 |
||
228 |
fun canonical_vars thy thm = |
|
229 |
let |
|
230 |
fun mk_inst (v_i as (v, i), (v', ty)) (s as (maxidx, set, acc)) = |
|
231 |
if v = v' orelse member (op =) set v then s |
|
232 |
else let |
|
233 |
val t = if i = ~1 then Free (v, ty) else Var (v_i, ty) |
|
20191 | 234 |
in |
19597 | 235 |
(maxidx + 1, v :: set, |
236 |
(cterm_of thy t, cterm_of thy (Var ((v', maxidx), ty))) :: acc) |
|
237 |
end; |
|
238 |
fun vars_of thm = fold_aterms |
|
20386 | 239 |
(fn Var (v_i as (v, i), ty) => cons (v_i, (CodegenNames.purify_var v, ty)) |
19597 | 240 |
| _ => I) (prop_of thm) []; |
241 |
val maxidx = Thm.maxidx_of thm + 1; |
|
242 |
val (_, _, inst) = fold mk_inst (vars_of thm) (maxidx + 1, [], []); |
|
243 |
in Thm.instantiate ([], inst) thm end; |
|
244 |
||
19884 | 245 |
fun drop_redundant thy eqs = |
246 |
let |
|
247 |
val matches = curry (Pattern.matches thy o |
|
248 |
pairself (fst o Logic.dest_equals o prop_of)) |
|
249 |
fun drop eqs [] = eqs |
|
250 |
| drop eqs (eq::eqs') = |
|
251 |
drop (eq::eqs) (filter_out (matches eq) eqs') |
|
252 |
in drop [] eqs end; |
|
253 |
||
20191 | 254 |
fun make_eq thy = |
19597 | 255 |
let |
256 |
val ((_, atomize), _) = get_obj thy; |
|
257 |
in rewrite_rule [atomize] end; |
|
258 |
||
259 |
fun dest_eq thy thm = |
|
260 |
case try (make_eq thy #> Drule.plain_prop_of |
|
261 |
#> ObjectLogic.drop_judgment thy #> Logic.dest_equals) thm |
|
262 |
of SOME eq => (eq, thm) |
|
263 |
| NONE => err_thm "not an equation" thm; |
|
264 |
||
265 |
fun dest_fun thy thm = |
|
266 |
let |
|
267 |
fun dest_fun' ((lhs, _), thm) = |
|
268 |
case try (dest_Const o fst o strip_comb) lhs |
|
269 |
of SOME (c, ty) => (c, (ty, thm)) |
|
270 |
| NONE => err_thm "not a function equation" thm; |
|
271 |
in |
|
272 |
thm |
|
273 |
|> dest_eq thy |
|
274 |
|> dest_fun' |
|
275 |
end; |
|
276 |
||
277 |
||
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
278 |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
279 |
(** theory data **) |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
280 |
|
19597 | 281 |
(* data structures *) |
282 |
||
283 |
fun merge' eq (xys as (xs, ys)) = |
|
284 |
if eq_list eq (xs, ys) then (false, xs) else (true, merge eq xys); |
|
285 |
||
286 |
fun alist_merge' eq_key eq (xys as (xs, ys)) = |
|
287 |
if eq_list (eq_pair eq_key eq) (xs, ys) then (false, xs) else (true, AList.merge eq_key eq xys); |
|
288 |
||
289 |
fun list_symtab_join' eq (xyt as (xt, yt)) = |
|
290 |
let |
|
291 |
val xc = Symtab.keys xt; |
|
292 |
val yc = Symtab.keys yt; |
|
293 |
val zc = filter (member (op =) yc) xc; |
|
294 |
val wc = subtract (op =) zc xc @ subtract (op =) zc yc; |
|
295 |
fun same_thms c = if eq_list eq_thm ((the o Symtab.lookup xt) c, (the o Symtab.lookup yt) c) |
|
296 |
then NONE else SOME c; |
|
297 |
in (wc @ map_filter same_thms zc, Symtab.join (K (merge eq)) xyt) end; |
|
298 |
||
19884 | 299 |
datatype notify = Notify of (serial * ((string * typ) list option -> theory -> theory)) list; |
19597 | 300 |
|
301 |
val mk_notify = Notify; |
|
302 |
fun map_notify f (Notify notify) = mk_notify (f notify); |
|
303 |
fun merge_notify pp (Notify notify1, Notify notify2) = |
|
304 |
mk_notify (AList.merge (op =) (K true) (notify1, notify2)); |
|
305 |
||
306 |
datatype preproc = Preproc of { |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
307 |
preprocs: (serial * (theory -> thm list -> thm list)) list, |
19597 | 308 |
unfolds: thm list |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
309 |
}; |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
310 |
|
19597 | 311 |
fun mk_preproc (preprocs, unfolds) = |
312 |
Preproc { preprocs = preprocs, unfolds = unfolds }; |
|
313 |
fun map_preproc f (Preproc { preprocs, unfolds }) = |
|
314 |
mk_preproc (f (preprocs, unfolds)); |
|
315 |
fun merge_preproc _ (Preproc { preprocs = preprocs1, unfolds = unfolds1 }, |
|
316 |
Preproc { preprocs = preprocs2, unfolds = unfolds2 }) = |
|
317 |
let |
|
318 |
val (dirty1, preprocs) = alist_merge' (op =) (K true) (preprocs1, preprocs2); |
|
319 |
val (dirty2, unfolds) = merge' eq_thm (unfolds1, unfolds2); |
|
320 |
in (dirty1 orelse dirty2, mk_preproc (preprocs, unfolds)) end; |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
321 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
322 |
datatype extrs = Extrs of { |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
323 |
funs: (serial * (theory -> string * typ -> thm list)) list, |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
324 |
datatypes: (serial * (theory -> string -> (((string * sort) list * (string * typ list) list) * tactic) option)) list |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
325 |
}; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
326 |
|
19597 | 327 |
fun mk_extrs (funs, datatypes) = |
328 |
Extrs { funs = funs, datatypes = datatypes }; |
|
329 |
fun map_extrs f (Extrs { funs, datatypes }) = |
|
330 |
mk_extrs (f (funs, datatypes)); |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
331 |
fun merge_extrs _ (Extrs { funs = funs1, datatypes = datatypes1 }, |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
332 |
Extrs { funs = funs2, datatypes = datatypes2 }) = |
19597 | 333 |
let |
334 |
val (dirty1, funs) = alist_merge' (op =) (K true) (funs1, funs2); |
|
335 |
val (dirty2, datatypes) = alist_merge' (op =) (K true) (datatypes1, datatypes2); |
|
336 |
in (dirty1 orelse dirty2, mk_extrs (funs, datatypes)) end; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
337 |
|
19597 | 338 |
datatype funthms = Funthms of { |
339 |
dirty: string list, |
|
340 |
funs: thm list Symtab.table |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
341 |
}; |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
342 |
|
19597 | 343 |
fun mk_funthms (dirty, funs) = |
344 |
Funthms { dirty = dirty, funs = funs }; |
|
345 |
fun map_funthms f (Funthms { dirty, funs }) = |
|
346 |
mk_funthms (f (dirty, funs)); |
|
347 |
fun merge_funthms _ (Funthms { dirty = dirty1, funs = funs1 }, |
|
348 |
Funthms { dirty = dirty2, funs = funs2 }) = |
|
349 |
let |
|
350 |
val (dirty3, funs) = list_symtab_join' eq_thm (funs1, funs2); |
|
351 |
in mk_funthms (merge (op =) (merge (op =) (dirty1, dirty2), dirty3), funs) end; |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
352 |
|
19597 | 353 |
datatype T = T of { |
354 |
dirty: bool, |
|
355 |
notify: notify, |
|
356 |
preproc: preproc, |
|
357 |
extrs: extrs, |
|
358 |
funthms: funthms |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
359 |
}; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
360 |
|
19597 | 361 |
fun mk_T ((dirty, notify), (preproc, (extrs, funthms))) = |
20191 | 362 |
T { dirty = dirty, notify = notify, preproc= preproc, extrs = extrs, funthms = funthms }; |
19597 | 363 |
fun map_T f (T { dirty, notify, preproc, extrs, funthms }) = |
364 |
mk_T (f ((dirty, notify), (preproc, (extrs, funthms)))); |
|
365 |
fun merge_T pp (T { dirty = dirty1, notify = notify1, preproc = preproc1, extrs = extrs1, funthms = funthms1 }, |
|
366 |
T { dirty = dirty2, notify = notify2, preproc = preproc2, extrs = extrs2, funthms = funthms2 }) = |
|
367 |
let |
|
368 |
val (dirty3, preproc) = merge_preproc pp (preproc1, preproc2); |
|
369 |
val (dirty4, extrs) = merge_extrs pp (extrs1, extrs2); |
|
370 |
in |
|
371 |
mk_T ((dirty1 orelse dirty2 orelse dirty3 orelse dirty4, merge_notify pp (notify1, notify2)), |
|
372 |
(preproc, (extrs, merge_funthms pp (funthms1, funthms2)))) |
|
373 |
end; |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
374 |
|
19597 | 375 |
|
376 |
(* setup *) |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
377 |
|
19597 | 378 |
structure CodegenTheoremsData = TheoryDataFun |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
379 |
(struct |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19897
diff
changeset
|
380 |
val name = "Pure/codegen_theorems_data"; |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
381 |
type T = T; |
19597 | 382 |
val empty = mk_T ((false, mk_notify []), (mk_preproc ([], []), |
383 |
(mk_extrs ([], []), mk_funthms ([], Symtab.empty)))); |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
384 |
val copy = I; |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
385 |
val extend = I; |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
386 |
val merge = merge_T; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
387 |
fun print (thy : theory) (data : T) = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
388 |
let |
19436 | 389 |
val pretty_thm = ProofContext.pretty_thm (ProofContext.init thy); |
19597 | 390 |
val funthms = (fn T { funthms, ... } => funthms) data; |
391 |
val funs = (Symtab.dest o (fn Funthms { funs, ... } => funs)) funthms; |
|
392 |
val preproc = (fn T { preproc, ... } => preproc) data; |
|
393 |
val unfolds = (fn Preproc { unfolds, ... } => unfolds) preproc; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
394 |
in |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
395 |
(Pretty.writeln o Pretty.block o Pretty.fbreaks) ([ |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
396 |
Pretty.str "code generation theorems:", |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
397 |
Pretty.str "function theorems:" ] @ |
19597 | 398 |
(*Pretty.fbreaks ( *) |
20191 | 399 |
map (fn (c, thms) => |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
400 |
(Pretty.block o Pretty.fbreaks) ( |
19884 | 401 |
Pretty.str c :: map pretty_thm (rev thms) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
402 |
) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
403 |
) funs |
19597 | 404 |
(*) *) @ [ |
405 |
Pretty.fbrk, |
|
406 |
Pretty.block ( |
|
20353 | 407 |
Pretty.str "inlined theorems:" |
19597 | 408 |
:: Pretty.fbrk |
409 |
:: (Pretty.fbreaks o map pretty_thm) unfolds |
|
410 |
)]) |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
411 |
end; |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
412 |
end); |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
413 |
|
19597 | 414 |
val _ = Context.add_setup CodegenTheoremsData.init; |
415 |
val print_thms = CodegenTheoremsData.print; |
|
416 |
||
417 |
||
418 |
(* accessors *) |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
419 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
420 |
local |
19597 | 421 |
val the_preproc = (fn T { preproc = Preproc preproc, ... } => preproc) o CodegenTheoremsData.get; |
422 |
val the_extrs = (fn T { extrs = Extrs extrs, ... } => extrs) o CodegenTheoremsData.get; |
|
423 |
val the_funthms = (fn T { funthms = Funthms funthms, ... } => funthms) o CodegenTheoremsData.get; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
424 |
in |
19597 | 425 |
val is_dirty = (fn T { dirty = dirty, ... } => dirty) o CodegenTheoremsData.get; |
426 |
val the_dirty_consts = (fn { dirty = dirty, ... } => dirty) o the_funthms; |
|
427 |
val the_notify = (fn T { notify = Notify notify, ... } => map snd notify) o CodegenTheoremsData.get; |
|
428 |
val the_preprocs = (fn { preprocs, ... } => map snd preprocs) o the_preproc; |
|
429 |
val the_unfolds = (fn { unfolds, ... } => unfolds) o the_preproc; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
430 |
val the_funs_extrs = (fn { funs, ... } => map snd funs) o the_extrs; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
431 |
val the_datatypes_extrs = (fn { datatypes, ... } => map snd datatypes) o the_extrs; |
19597 | 432 |
val the_funs = (fn { funs, ... } => funs) o the_funthms; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
433 |
end (*local*); |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
434 |
|
19597 | 435 |
val map_data = CodegenTheoremsData.map o map_T; |
436 |
||
437 |
(* notifiers *) |
|
438 |
||
19884 | 439 |
fun all_typs thy c = |
440 |
map (pair c) (Sign.the_const_type thy c :: (map (#lhs) o Theory.definitions_of thy) c); |
|
441 |
||
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
442 |
fun add_notify f = |
19597 | 443 |
map_data (fn ((dirty, notify), x) => |
444 |
((dirty, notify |> map_notify (cons (serial (), f))), x)); |
|
445 |
||
446 |
fun get_reset_dirty thy = |
|
447 |
let |
|
448 |
val dirty = is_dirty thy; |
|
449 |
val dirty_const = if dirty then [] else the_dirty_consts thy; |
|
450 |
in |
|
451 |
thy |
|
452 |
|> map_data (fn ((_, notify), (procs, (extrs, funthms))) => |
|
453 |
((false, notify), (procs, (extrs, funthms |> map_funthms (fn (_, funs) => ([], funs)))))) |
|
454 |
|> pair (dirty, dirty_const) |
|
455 |
end; |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
456 |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
457 |
fun notify_all c thy = |
19597 | 458 |
thy |
459 |
|> get_reset_dirty |
|
460 |
|-> (fn (true, _) => fold (fn f => f NONE) (the_notify thy) |
|
19884 | 461 |
| (false, cs) => let val cs' = case c of NONE => cs | SOME c => insert (op =) c cs |
462 |
in fold (fn f => f (SOME (maps (all_typs thy) cs'))) (the_notify thy) end); |
|
19597 | 463 |
|
464 |
fun notify_dirty thy = |
|
465 |
thy |
|
466 |
|> get_reset_dirty |
|
467 |
|-> (fn (true, _) => fold (fn f => f NONE) (the_notify thy) |
|
19884 | 468 |
| (false, cs) => fold (fn f => f (SOME (maps (all_typs thy) cs))) (the_notify thy)); |
19597 | 469 |
|
470 |
||
471 |
(* modifiers *) |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
472 |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
473 |
fun add_preproc f = |
19597 | 474 |
map_data (fn (x, (preproc, y)) => |
475 |
(x, (preproc |> map_preproc (fn (preprocs, unfolds) => ((serial (), f) :: preprocs, unfolds)), y))) |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
476 |
#> notify_all NONE; |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
477 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
478 |
fun add_fun_extr f = |
19597 | 479 |
map_data (fn (x, (preproc, (extrs, funthms))) => |
480 |
(x, (preproc, (extrs |> map_extrs (fn (funs, datatypes) => |
|
481 |
((serial (), f) :: funs, datatypes)), funthms)))) |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
482 |
#> notify_all NONE; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
483 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
484 |
fun add_datatype_extr f = |
19597 | 485 |
map_data (fn (x, (preproc, (extrs, funthms))) => |
486 |
(x, (preproc, (extrs |> map_extrs (fn (funs, datatypes) => |
|
487 |
(funs, (serial (), f) :: datatypes)), funthms)))) |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
488 |
#> notify_all NONE; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
489 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
490 |
fun add_fun thm thy = |
19597 | 491 |
case dest_fun thy thm |
492 |
of (c, _) => |
|
493 |
thy |
|
494 |
|> map_data (fn (x, (preproc, (extrs, funthms))) => |
|
495 |
(x, (preproc, (extrs, funthms |> map_funthms (fn (dirty, funs) => |
|
19884 | 496 |
(dirty, funs |> Symtab.default (c, []) |> Symtab.map_entry c (cons thm))))))) |
19597 | 497 |
|> notify_all (SOME c); |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
498 |
|
19597 | 499 |
fun del_fun thm thy = |
500 |
case dest_fun thy thm |
|
501 |
of (c, _) => |
|
502 |
thy |
|
503 |
|> map_data (fn (x, (preproc, (extrs, funthms))) => |
|
504 |
(x, (preproc, (extrs, funthms |> map_funthms (fn (dirty, funs) => |
|
505 |
(dirty, funs |> Symtab.map_entry c (remove eq_thm thm))))))) |
|
506 |
|> notify_all (SOME c); |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
507 |
|
19597 | 508 |
fun add_unfold thm thy = |
509 |
thy |
|
510 |
|> tap (fn thy => dest_eq thy thm) |
|
511 |
|> map_data (fn (x, (preproc, y)) => |
|
512 |
(x, (preproc |> map_preproc (fn (preprocs, unfolds) => |
|
513 |
(preprocs, thm :: unfolds)), y))) |
|
514 |
|> notify_all NONE; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
515 |
|
20191 | 516 |
fun del_unfold thm = |
19597 | 517 |
map_data (fn (x, (preproc, y)) => |
518 |
(x, (preproc |> map_preproc (fn (preprocs, unfolds) => |
|
519 |
(preprocs, remove eq_thm thm unfolds)), y))) |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
520 |
#> notify_all NONE; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
521 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
522 |
fun purge_defs (c, ty) thy = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
523 |
thy |
19597 | 524 |
|> map_data (fn (x, (preproc, (extrs, funthms))) => |
525 |
(x, (preproc, (extrs, funthms |> map_funthms (fn (dirty, funs) => |
|
526 |
(dirty, funs |> Symtab.map_entry c |
|
527 |
(filter (fn thm => Sign.typ_instance thy |
|
528 |
((fst o snd o dest_fun thy) thm, ty))))))))) |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
529 |
|> notify_all (SOME c); |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
530 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
531 |
|
19597 | 532 |
|
533 |
(** theorem handling **) |
|
534 |
||
535 |
(* preprocessing *) |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
536 |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19897
diff
changeset
|
537 |
fun extr_typ thy thm = case dest_fun thy thm |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19897
diff
changeset
|
538 |
of (_, (ty, _)) => ty; |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19897
diff
changeset
|
539 |
|
20191 | 540 |
fun rewrite_rhs conv thm = (case (Drule.strip_comb o cprop_of) thm |
541 |
of (ct', [ct1, ct2]) => (case term_of ct' |
|
542 |
of Const ("==", _) => |
|
543 |
Thm.equal_elim (combination (combination (reflexive ct') (reflexive ct1)) |
|
544 |
(conv ct2)) thm |
|
545 |
| _ => raise ERROR "rewrite_rhs") |
|
546 |
| _ => raise ERROR "rewrite_rhs"); |
|
547 |
||
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
548 |
fun common_typ thy _ [] = [] |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
549 |
| common_typ thy _ [thm] = [thm] |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
550 |
| common_typ thy extract_typ thms = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
551 |
let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
552 |
fun incr_thm thm max = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
553 |
let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
554 |
val thm' = incr_indexes max thm; |
20105 | 555 |
val max' = (maxidx_of_typ o fastype_of o Drule.plain_prop_of) thm' + 1; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
556 |
in (thm', max') end; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
557 |
val (thms', maxidx) = fold_map incr_thm thms 0; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
558 |
val (ty1::tys) = map extract_typ thms; |
19436 | 559 |
fun unify ty = Sign.typ_unify thy (ty1, ty); |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
560 |
val (env, _) = fold unify tys (Vartab.empty, maxidx) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
561 |
val instT = Vartab.fold (fn (x_i, (sort, ty)) => |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
562 |
cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env []; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
563 |
in map (Thm.instantiate (instT, [])) thms end; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
564 |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
565 |
fun preprocess thy thms = |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
566 |
let |
19597 | 567 |
fun burrow_thms f [] = [] |
20191 | 568 |
| burrow_thms f thms = |
19597 | 569 |
thms |
570 |
|> Conjunction.intr_list |
|
571 |
|> f |
|
572 |
|> Conjunction.elim_list; |
|
19884 | 573 |
fun cmp_thms (thm1, thm2) = |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19897
diff
changeset
|
574 |
not (Sign.typ_instance thy (extr_typ thy thm1, extr_typ thy thm2)); |
19884 | 575 |
fun unvarify thms = |
20218
be3bfb0699ba
Variable.import(T): result includes fixed types/terms;
wenzelm
parents:
20203
diff
changeset
|
576 |
#2 (#1 (Variable.import true thms (ProofContext.init thy))); |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
577 |
val unfold_thms = Tactic.rewrite true (map (make_eq thy) (the_unfolds thy)); |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
578 |
in |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
579 |
thms |
19597 | 580 |
|> map (make_eq thy) |
581 |
|> map (Thm.transfer thy) |
|
582 |
|> fold (fn f => f thy) (the_preprocs thy) |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
583 |
|> map (rewrite_rhs unfold_thms) |
19884 | 584 |
|> debug_msg (fn _ => "[cg_thm] sorting") |
19597 | 585 |
|> debug_msg (commas o map string_of_thm) |
19884 | 586 |
|> sort (make_ord cmp_thms) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
587 |
|> debug_msg (fn _ => "[cg_thm] common_typ") |
19597 | 588 |
|> debug_msg (commas o map string_of_thm) |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19897
diff
changeset
|
589 |
|> common_typ thy (extr_typ thy) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
590 |
|> debug_msg (fn _ => "[cg_thm] abs_norm") |
19597 | 591 |
|> debug_msg (commas o map string_of_thm) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
592 |
|> map (abs_norm thy) |
19597 | 593 |
|> burrow_thms ( |
594 |
debug_msg (fn _ => "[cg_thm] canonical tvars") |
|
595 |
#> debug_msg (string_of_thm) |
|
596 |
#> canonical_tvars thy |
|
597 |
#> debug_msg (fn _ => "[cg_thm] canonical vars") |
|
598 |
#> debug_msg (string_of_thm) |
|
599 |
#> canonical_vars thy |
|
600 |
#> debug_msg (fn _ => "[cg_thm] zero indices") |
|
601 |
#> debug_msg (string_of_thm) |
|
602 |
#> Drule.zero_var_indexes |
|
603 |
) |
|
19884 | 604 |
|> drop_redundant thy |
20353 | 605 |
|> debug_msg (fn _ => "[cg_thm] preprocessing done") |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
606 |
end; |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
607 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
608 |
|
19597 | 609 |
(* retrieval *) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
610 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
611 |
fun get_funs thy (c, ty) = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
612 |
let |
19597 | 613 |
val _ = debug_msg (fn _ => "[cg_thm] const (1) " ^ c ^ " :: " ^ Sign.string_of_typ thy ty) () |
614 |
val filter_typ = map_filter (fn (_, (ty', thm)) => |
|
615 |
if Sign.typ_instance thy (ty, ty') |
|
616 |
then SOME thm else debug_msg (fn _ => "[cg_thm] dropping " ^ string_of_thm thm) NONE); |
|
617 |
fun get_funs (c, ty) = |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
618 |
(these o Symtab.lookup (the_funs thy)) c |
19597 | 619 |
|> debug_msg (fn _ => "[cg_thm] trying funs") |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
620 |
|> map (dest_fun thy) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
621 |
|> filter_typ; |
19597 | 622 |
fun get_extr (c, ty) = |
623 |
getf_first_list (map (fn f => f thy) (the_funs_extrs thy)) (c, ty) |
|
624 |
|> debug_msg (fn _ => "[cg_thm] trying extr") |
|
625 |
|> map (dest_fun thy) |
|
626 |
|> filter_typ; |
|
627 |
fun get_spec (c, ty) = |
|
628 |
Theory.definitions_of thy c |
|
629 |
|> debug_msg (fn _ => "[cg_thm] trying spec") |
|
630 |
(* FIXME avoid dynamic name space lookup!? (via Thm.get_axiom_i etc.??) *) |
|
20353 | 631 |
|> maps (fn { name, ... } => these (try (PureThy.get_thms thy) (Name name))) |
19597 | 632 |
|> map_filter (try (dest_fun thy)) |
633 |
|> filter_typ; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
634 |
in |
19597 | 635 |
getf_first_list [get_funs, get_extr, get_spec] (c, ty) |
636 |
|> debug_msg (fn _ => "[cg_thm] const (2) " ^ c ^ " :: " ^ Sign.string_of_typ thy ty) |
|
637 |
|> preprocess thy |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
638 |
end; |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
639 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
640 |
fun get_datatypes thy dtco = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
641 |
let |
19597 | 642 |
val _ = debug_msg (fn _ => "[cg_thm] datatype " ^ dtco) () |
643 |
val truh = mk_true thy; |
|
644 |
val fals = mk_false thy; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
645 |
fun mk_lhs vs ((co1, tys1), (co2, tys2)) = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
646 |
let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
647 |
val dty = Type (dtco, map TFree vs); |
20076 | 648 |
val (xs1, xs2) = chop (length tys1) (Name.invent_list [] "x" (length tys1 + length tys2)); |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
649 |
val frees1 = map2 (fn x => fn ty => Free (x, ty)) xs1 tys1; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
650 |
val frees2 = map2 (fn x => fn ty => Free (x, ty)) xs2 tys2; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
651 |
fun zip_co co xs tys = list_comb (Const (co, |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
652 |
tys ---> dty), map2 (fn x => fn ty => Free (x, ty)) xs tys); |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
653 |
in |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
654 |
((frees1, frees2), (zip_co co1 xs1 tys1, zip_co co2 xs2 tys2)) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
655 |
end; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
656 |
fun mk_rhs [] [] = truh |
19597 | 657 |
| mk_rhs xs ys = foldr1 (mk_obj_conj thy) (map2 (curry (mk_obj_eq thy)) xs ys); |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
658 |
fun mk_eq vs (args as ((co1, _), (co2, _))) (inj, dist) = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
659 |
if co1 = co2 |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
660 |
then let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
661 |
val ((fs1, fs2), lhs) = mk_lhs vs args; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
662 |
val rhs = mk_rhs fs1 fs2; |
19597 | 663 |
in (mk_func thy (lhs, rhs) :: inj, dist) end |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
664 |
else let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
665 |
val (_, lhs) = mk_lhs vs args; |
19597 | 666 |
in (inj, mk_func thy (lhs, fals) :: dist) end; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
667 |
fun mk_eqs (vs, cos) = |
20191 | 668 |
let val cos' = rev cos |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
669 |
in (op @) (fold (mk_eq vs) (product cos' cos') ([], [])) end; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
670 |
fun mk_eq_thms tac vs_cos = |
20046 | 671 |
map (fn t => Goal.prove_global thy [] [] |
672 |
(ObjectLogic.ensure_propT thy t) (K tac)) (mk_eqs vs_cos); |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
673 |
in |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
674 |
case getf_first (map (fn f => f thy) (the_datatypes_extrs thy)) dtco |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
675 |
of NONE => NONE |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
676 |
| SOME (vs_cos, tac) => SOME (vs_cos, mk_eq_thms tac vs_cos) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
677 |
end; |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
678 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
679 |
fun get_eq thy (c, ty) = |
19597 | 680 |
if is_obj_eq thy c |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
681 |
then case strip_type ty |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
682 |
of (Type (tyco, _) :: _, _) => |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
683 |
(case get_datatypes thy tyco |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
684 |
of SOME (_, thms) => thms |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
685 |
| _ => []) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
686 |
| _ => [] |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
687 |
else []; |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
688 |
|
20353 | 689 |
fun check_thms c thms = |
690 |
let |
|
691 |
fun check_head_lhs thm (lhs, rhs) = |
|
692 |
case strip_comb lhs |
|
693 |
of (Const (c', _), _) => if c' = c then () |
|
694 |
else error ("illegal function equation for " ^ quote c |
|
695 |
^ ", actually defining " ^ quote c' ^ ": " ^ Display.string_of_thm thm) |
|
696 |
| _ => error ("illegal function equation: " ^ Display.string_of_thm thm); |
|
697 |
fun check_vars_lhs thm (lhs, rhs) = |
|
698 |
if has_duplicates (op =) |
|
699 |
(fold_aterms (fn Free (v, _) => cons v | _ => I) lhs []) |
|
700 |
then error ("repeated variables on left hand side of function equation:" |
|
701 |
^ Display.string_of_thm thm) |
|
702 |
else (); |
|
703 |
fun check_vars_rhs thm (lhs, rhs) = |
|
704 |
if null (subtract (op =) |
|
705 |
(fold_aterms (fn Free (v, _) => cons v | _ => I) lhs []) |
|
706 |
(fold_aterms (fn Free (v, _) => cons v | _ => I) rhs [])) |
|
707 |
then () |
|
708 |
else error ("free variables on right hand side of function equation:" |
|
709 |
^ Display.string_of_thm thm) |
|
710 |
val tts = map (Logic.dest_equals o Logic.unvarify o Thm.prop_of) thms; |
|
711 |
in |
|
712 |
(map2 check_head_lhs thms tts; map2 check_vars_lhs thms tts; |
|
713 |
map2 check_vars_rhs thms tts; thms) |
|
714 |
end; |
|
715 |
||
20386 | 716 |
structure Consttab = CodegenConsts.Consttab; |
717 |
type thmtab = (theory * (thm list Consttab.table |
|
718 |
* string Consttab.table) |
|
20191 | 719 |
* (Sorts.algebra * ((string * sort) list * (string * typ list) list) Symtab.table)); |
720 |
||
20386 | 721 |
fun thmtab_empty thy = (thy, (Consttab.empty, Consttab.empty), |
20191 | 722 |
(ClassPackage.operational_algebra thy, Symtab.empty)); |
723 |
||
724 |
fun get_sortalgebra (_, _, (algebra, _)) = |
|
725 |
algebra; |
|
19597 | 726 |
|
20386 | 727 |
fun get_dtyp_of_cons (thy, (_, dtcotab), _) = |
728 |
Consttab.lookup dtcotab o CodegenConsts.norminst_of_typ thy; |
|
729 |
||
730 |
fun get_dtyp_spec (_, _, (_, dttab)) = |
|
731 |
Symtab.lookup dttab; |
|
732 |
||
733 |
fun has_fun_thms (thy, (funtab, _), _) = |
|
734 |
is_some o Consttab.lookup funtab o CodegenConsts.norminst_of_typ thy; |
|
20191 | 735 |
|
20386 | 736 |
fun get_fun_thms (thy, (funtab, _), _) (c_ty as (c, _)) = |
737 |
(check_thms c o these o Consttab.lookup funtab |
|
738 |
o CodegenConsts.norminst_of_typ thy) c_ty; |
|
739 |
||
740 |
fun pretty_funtab thy funtab = |
|
741 |
funtab |
|
742 |
|> CodegenConsts.Consttab.dest |
|
743 |
|> map (fn (c, thms) => |
|
744 |
(Pretty.block o Pretty.fbreaks) ( |
|
745 |
(Pretty.str o CodegenConsts.string_of_const thy) c |
|
746 |
:: map Display.pretty_thm thms |
|
747 |
)) |
|
748 |
|> Pretty.chunks; |
|
20191 | 749 |
|
20386 | 750 |
fun constrain_funtab thy funtab = |
20191 | 751 |
let |
20386 | 752 |
fun max k [] = k |
753 |
| max k (l::ls) = max (if k < l then l else k) ls; |
|
754 |
fun mk_consttyps funtab = |
|
755 |
CodegenConsts.Consttab.empty |
|
756 |
|> CodegenConsts.Consttab.fold (fn (c, thm :: _) => |
|
757 |
CodegenConsts.Consttab.update_new (c, extr_typ thy thm) | (_, []) => I) funtab |
|
758 |
fun mk_typescheme_of typtab (c, ty) = |
|
759 |
CodegenConsts.Consttab.lookup typtab (CodegenConsts.norminst_of_typ thy (c, ty)); |
|
760 |
fun incr_indices (c, thms) maxidx = |
|
761 |
let |
|
762 |
val thms' = map (Thm.incr_indexes maxidx) thms; |
|
763 |
val maxidx' = Int.max |
|
764 |
(maxidx, max ~1 (map Thm.maxidx_of thms') + 1); |
|
765 |
in (thms', maxidx') end; |
|
766 |
fun consts_of_eqs thms = |
|
767 |
let |
|
768 |
fun terms_of_eq thm = |
|
769 |
let |
|
770 |
val (lhs, rhs) = (Logic.dest_equals o Drule.plain_prop_of) thm |
|
771 |
in rhs :: (snd o strip_comb) lhs end; |
|
772 |
in (fold o fold_aterms) (fn Const c => insert (eq_pair (op =) (Type.eq_type Vartab.empty)) c | _ => I) |
|
773 |
(maps terms_of_eq thms) [] |
|
774 |
end; |
|
775 |
val typscheme_of = |
|
776 |
mk_typescheme_of (mk_consttyps funtab); |
|
777 |
val tsig = Sign.tsig_of thy; |
|
778 |
fun unify_const (c, ty) (env, maxidx) = |
|
779 |
case typscheme_of (c, ty) |
|
780 |
of SOME ty_decl => let |
|
781 |
(*val _ = writeln "UNIFY"; |
|
782 |
val _ = writeln (CodegenConsts.string_of_const_typ thy (c, ty))*) |
|
783 |
val ty_decl' = Logic.incr_tvar maxidx ty_decl; |
|
784 |
(*val _ = writeln "WITH"; |
|
785 |
val _ = writeln (CodegenConsts.string_of_const_typ thy (c, ty_decl'))*) |
|
786 |
val maxidx' = Int.max (Term.maxidx_of_typ ty_decl' + 1, maxidx); |
|
787 |
(*val _ = writeln (" " ^ string_of_int maxidx ^ " +> " ^ string_of_int maxidx');*) |
|
788 |
in Type.unify tsig (ty_decl', ty) (env, maxidx') end |
|
789 |
| NONE => (env, maxidx); |
|
790 |
fun apply_unifier unif [] = [] |
|
791 |
| apply_unifier unif (thms as thm :: _) = |
|
792 |
let |
|
793 |
val ty = extr_typ thy thm; |
|
794 |
val ty' = Envir.norm_type unif ty; |
|
795 |
val env = Type.typ_match (Sign.tsig_of thy) (ty, ty') Vartab.empty; |
|
796 |
val inst = Thm.instantiate (Vartab.fold (fn (x_i, (sort, ty)) => |
|
797 |
cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env [], []); |
|
798 |
in map (Drule.zero_var_indexes o inst) thms end; |
|
799 |
(* val _ = writeln "(1)"; *) |
|
800 |
(* val _ = (Pretty.writeln o pretty_funtab thy) funtab; *) |
|
801 |
val (funtab', maxidx) = |
|
802 |
CodegenConsts.Consttab.fold_map incr_indices funtab 0; |
|
803 |
(* val _ = writeln "(2)"; |
|
804 |
* val _ = (Pretty.writeln o pretty_funtab thy) funtab'; |
|
805 |
*) |
|
806 |
val (unif, _) = |
|
807 |
CodegenConsts.Consttab.fold (fold unify_const o consts_of_eqs o snd) |
|
808 |
funtab' (Vartab.empty, maxidx); |
|
809 |
(* val _ = writeln "(3)"; *) |
|
810 |
val funtab'' = |
|
811 |
CodegenConsts.Consttab.map (apply_unifier unif) funtab'; |
|
812 |
(* val _ = writeln "(4)"; |
|
813 |
* val _ = (Pretty.writeln o pretty_funtab thy) funtab''; |
|
814 |
*) |
|
815 |
in funtab'' end; |
|
20191 | 816 |
|
20353 | 817 |
fun mk_thmtab thy cs = |
19597 | 818 |
let |
20191 | 819 |
fun add_tycos (Type (tyco, tys)) = insert (op =) tyco #> fold add_tycos tys |
820 |
| add_tycos _ = I; |
|
20386 | 821 |
fun consts_of ts = |
822 |
Consttab.empty |
|
823 |
|> (fold o fold_aterms) |
|
824 |
(fn Const c_ty => Consttab.update (CodegenConsts.norminst_of_typ thy c_ty, ()) |
|
825 |
| _ => I) ts |
|
826 |
|> Consttab.keys; |
|
20191 | 827 |
fun add_dtyps_of_type ty thmtab = |
828 |
let |
|
829 |
val tycos = add_tycos ty []; |
|
20353 | 830 |
val tycos_new = filter (is_none o get_dtyp_spec thmtab) tycos; |
20386 | 831 |
fun add_dtyp_spec dtco (dtyp_spec as (vs, cs)) ((thy, (funtab, dtcotab), (algebra, dttab))) = |
20191 | 832 |
let |
833 |
fun mk_co (c, tys) = |
|
20386 | 834 |
CodegenConsts.norminst_of_typ thy (c, Logic.varifyT (tys ---> Type (dtco, map TFree vs))); |
20191 | 835 |
in |
20386 | 836 |
(thy, (funtab, dtcotab |> fold (fn c_tys => |
837 |
Consttab.update_new (mk_co c_tys, dtco)) cs), |
|
20191 | 838 |
(algebra, dttab |> Symtab.update_new (dtco, dtyp_spec))) |
839 |
end; |
|
840 |
in |
|
841 |
thmtab |
|
842 |
|> fold (fn tyco => case get_datatypes thy tyco |
|
843 |
of SOME (dtyp_spec, _) => add_dtyp_spec tyco dtyp_spec |
|
844 |
| NONE => I) tycos_new |
|
845 |
end; |
|
846 |
fun known thmtab (c, ty) = |
|
847 |
is_some (get_dtyp_of_cons thmtab (c, ty)) orelse has_fun_thms thmtab (c, ty); |
|
20386 | 848 |
fun add_funthms (c, ty) (thmtab as (thy, (funtab, dtcotab), algebra_dttab))= |
849 |
if known thmtab (c, ty) then thmtab |
|
20191 | 850 |
else let |
851 |
val thms = get_funs thy (c, ty) |
|
20386 | 852 |
val cs_dep = (consts_of o map Thm.prop_of) thms; |
20191 | 853 |
in |
20386 | 854 |
(thy, (funtab |> Consttab.update_new (CodegenConsts.norminst_of_typ thy (c, ty), thms) |
20191 | 855 |
, dtcotab), algebra_dttab) |
856 |
|> fold add_c cs_dep |
|
857 |
end |
|
20386 | 858 |
and add_c (c_tys as (c, tys)) thmtab = |
859 |
thmtab |
|
860 |
|> add_dtyps_of_type (snd (CodegenConsts.typ_of_typinst thy c_tys)) |
|
861 |
|> fold (add_funthms o CodegenConsts.typ_of_typinst thy) |
|
862 |
(CodegenConsts.insts_of_classop thy c_tys); |
|
20191 | 863 |
in |
864 |
thmtab_empty thy |
|
20386 | 865 |
|> fold (add_c o CodegenConsts.norminst_of_typ thy) cs |
866 |
|> (fn (a, (funtab, b), c) => (a, (funtab |> constrain_funtab thy, b), c)) |
|
20191 | 867 |
end; |
19597 | 868 |
|
869 |
||
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
870 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
871 |
(** code attributes and setup **) |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
872 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
873 |
local |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
874 |
fun add_simple_attribute (name, f) = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
875 |
(Codegen.add_attribute name o (Scan.succeed o Thm.declaration_attribute)) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
876 |
(Context.map_theory o f); |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
877 |
in |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
878 |
val _ = map (Context.add_setup o add_simple_attribute) [ |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
879 |
("fun", add_fun), |
20353 | 880 |
("nofun", del_fun), |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
881 |
("unfold", (fn thm => Codegen.add_unfold thm #> add_unfold thm)), |
20105 | 882 |
("inline", add_unfold), |
20353 | 883 |
("noinline", del_unfold) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
884 |
] |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
885 |
end; (*local*) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
886 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
887 |
val _ = Context.add_setup (add_fun_extr get_eq); |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
888 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
889 |
end; (*struct*) |