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