author | wenzelm |
Wed, 07 Jun 2006 02:01:28 +0200 | |
changeset 19806 | f860b7a98445 |
parent 19597 | 8ced57ffc090 |
child 19816 | a8c8ed1c85e0 |
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 |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
10 |
val add_notify: (string option -> theory -> theory) -> theory -> theory; |
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 |
|
19597 | 23 |
val proper_name: string -> string; |
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; |
19597 | 25 |
val preprocess: theory -> thm list -> (typ * thm list) option; |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
26 |
val preprocess_term: theory -> term -> term; |
19597 | 27 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
28 |
val get_funs: theory -> string * typ -> (typ * thm list) option; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
29 |
val get_datatypes: theory -> string |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
30 |
-> (((string * sort) list * (string * typ list) list) * thm list) option; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
31 |
|
19597 | 32 |
(* |
33 |
type thmtab; |
|
34 |
val get_thmtab: (string * typ) list -> theory -> thmtab * theory; |
|
35 |
val get_cons: thmtab -> string -> string option; |
|
36 |
val get_dtyp: thmtab -> string -> (string * sort) list * (string * typ list) list; |
|
37 |
val get_thms: thmtab -> string * typ -> typ * thm list; |
|
38 |
*) |
|
39 |
||
40 |
val print_thms: theory -> unit; |
|
41 |
||
42 |
val init_obj: (thm * thm) * (thm * thm) -> theory -> theory; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
43 |
val debug: bool ref; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
44 |
val debug_msg: ('a -> string) -> 'a -> 'a; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
45 |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
46 |
end; |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
47 |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
48 |
structure CodegenTheorems: CODEGEN_THEOREMS = |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
49 |
struct |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
50 |
|
19597 | 51 |
(** preliminaries **) |
52 |
||
53 |
(* diagnostics *) |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
54 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
55 |
val debug = ref false; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
56 |
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
|
57 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
58 |
|
19597 | 59 |
(* auxiliary *) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
60 |
|
19597 | 61 |
fun proper_name s = |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
62 |
let |
19597 | 63 |
fun replace_invalid c = |
64 |
if (Char.isAlphaNum o the o Char.fromString) c orelse c = "'" |
|
65 |
andalso not (NameSpace.separator = c) |
|
66 |
then c |
|
67 |
else "_"; |
|
68 |
fun contract "_" (acc as "_" :: _) = acc |
|
69 |
| contract c acc = c :: acc; |
|
70 |
fun contract_underscores s = |
|
71 |
implode (fold_rev contract (explode s) []); |
|
72 |
fun ensure_char s = |
|
73 |
if forall (Char.isDigit o the o Char.fromString) (explode s) |
|
74 |
then prefix "x" s |
|
75 |
else s |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
76 |
in |
19597 | 77 |
s |
78 |
|> translate_string replace_invalid |
|
79 |
|> contract_underscores |
|
80 |
|> ensure_char |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
81 |
end; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
82 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
83 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
84 |
fun getf_first [] _ = NONE |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
85 |
| getf_first (f::fs) x = case f x |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
86 |
of NONE => getf_first fs x |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
87 |
| y as SOME x => y; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
88 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
89 |
fun getf_first_list [] x = [] |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
90 |
| getf_first_list (f::fs) x = case f x |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
91 |
of [] => getf_first_list fs x |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
92 |
| xs => xs; |
19597 | 93 |
|
94 |
||
95 |
(* object logic setup *) |
|
96 |
||
97 |
structure CodegenTheoremsSetup = TheoryDataFun |
|
98 |
(struct |
|
99 |
val name = "Pure/CodegenTheoremsSetup"; |
|
100 |
type T = ((string * thm) * ((string * string) * (string * string))) option; |
|
101 |
val empty = NONE; |
|
102 |
val copy = I; |
|
103 |
val extend = I; |
|
104 |
fun merge pp = merge_opt (eq_pair (eq_pair (op =) eq_thm) |
|
105 |
(eq_pair (eq_pair (op =) (op =)) (eq_pair (op =) (op =)))) : T * T -> T; |
|
106 |
fun print thy data = (); |
|
107 |
end); |
|
108 |
||
109 |
val _ = Context.add_setup CodegenTheoremsSetup.init; |
|
110 |
||
111 |
fun init_obj ((TrueI, FalseE), (conjI, atomize_eq)) thy = |
|
112 |
case CodegenTheoremsSetup.get thy |
|
113 |
of SOME _ => error "code generator already set up for object logic" |
|
114 |
| NONE => |
|
115 |
let |
|
116 |
fun strip_implies t = (Logic.strip_imp_prems t, Logic.strip_imp_concl t); |
|
117 |
fun dest_TrueI thm = |
|
118 |
Drule.plain_prop_of thm |
|
119 |
|> ObjectLogic.drop_judgment thy |
|
120 |
|> Term.dest_Const |
|
121 |
|> apsnd ( |
|
122 |
Term.dest_Type |
|
123 |
#> fst |
|
124 |
); |
|
125 |
fun dest_FalseE thm = |
|
126 |
Drule.plain_prop_of thm |
|
127 |
|> Logic.dest_implies |
|
128 |
|> apsnd ( |
|
129 |
ObjectLogic.drop_judgment thy |
|
130 |
#> Term.dest_Var |
|
131 |
) |
|
132 |
|> fst |
|
133 |
|> ObjectLogic.drop_judgment thy |
|
134 |
|> Term.dest_Const |
|
135 |
|> fst; |
|
136 |
fun dest_conjI thm = |
|
137 |
Drule.plain_prop_of thm |
|
138 |
|> strip_implies |
|
139 |
|> apfst (map (ObjectLogic.drop_judgment thy #> Term.dest_Var)) |
|
140 |
|> apsnd ( |
|
141 |
ObjectLogic.drop_judgment thy |
|
142 |
#> Term.strip_comb |
|
143 |
#> apsnd (map Term.dest_Var) |
|
144 |
#> apfst Term.dest_Const |
|
145 |
) |
|
146 |
|> (fn (v1, ((conj, _), v2)) => if v1 = v2 then conj else error "wrong premise") |
|
147 |
fun dest_atomize_eq thm = |
|
148 |
Drule.plain_prop_of thm |
|
149 |
|> Logic.dest_equals |
|
150 |
|> apfst ( |
|
151 |
ObjectLogic.drop_judgment thy |
|
152 |
#> Term.strip_comb |
|
153 |
#> apsnd (map Term.dest_Var) |
|
154 |
#> apfst Term.dest_Const |
|
155 |
) |
|
156 |
|> apsnd ( |
|
157 |
Logic.dest_equals |
|
158 |
#> apfst Term.dest_Var |
|
159 |
#> apsnd Term.dest_Var |
|
160 |
) |
|
161 |
|> (fn (((eq, _), v2), (v1a as (_, TVar (_, sort)), v1b)) => |
|
162 |
if [v1a, v1b] = v2 andalso sort = Sign.defaultS thy then eq else error "wrong premise") |
|
163 |
in |
|
164 |
((dest_TrueI TrueI, [dest_FalseE FalseE, dest_conjI conjI, dest_atomize_eq atomize_eq]) |
|
165 |
handle _ => error "bad code generator setup") |
|
166 |
|> (fn ((tr, b), [fl, con, eq]) => CodegenTheoremsSetup.put |
|
167 |
(SOME ((b, atomize_eq), ((tr, fl), (con, eq)))) thy) |
|
168 |
end; |
|
169 |
||
170 |
fun get_obj thy = |
|
171 |
case CodegenTheoremsSetup.get thy |
|
172 |
of SOME ((b, atomize), x) => ((Type (b, []), atomize) ,x) |
|
173 |
| NONE => error "no object logic setup for code theorems"; |
|
174 |
||
175 |
fun mk_true thy = |
|
176 |
let |
|
177 |
val ((b, _), ((tr, fl), (con, eq))) = get_obj thy; |
|
178 |
in Const (tr, b) end; |
|
179 |
||
180 |
fun mk_false thy = |
|
181 |
let |
|
182 |
val ((b, _), ((tr, fl), (con, eq))) = get_obj thy; |
|
183 |
in Const (fl, b) end; |
|
184 |
||
185 |
fun mk_obj_conj thy (x, y) = |
|
186 |
let |
|
187 |
val ((b, _), ((tr, fl), (con, eq))) = get_obj thy; |
|
188 |
in Const (con, b --> b --> b) $ x $ y end; |
|
189 |
||
190 |
fun mk_obj_eq thy (x, y) = |
|
191 |
let |
|
192 |
val ((b, _), ((tr, fl), (con, eq))) = get_obj thy; |
|
193 |
in Const (eq, type_of x --> type_of y --> b) $ x $ y end; |
|
194 |
||
195 |
fun is_obj_eq thy c = |
|
196 |
let |
|
197 |
val ((b, _), ((tr, fl), (con, eq))) = get_obj thy; |
|
198 |
in c = eq end; |
|
199 |
||
200 |
fun mk_func thy ((x, y), rhs) = |
|
201 |
Logic.mk_equals ( |
|
202 |
(mk_obj_eq thy (x, y)), |
|
203 |
rhs |
|
204 |
); |
|
205 |
||
206 |
||
207 |
(* theorem purification *) |
|
208 |
||
209 |
fun err_thm msg thm = |
|
210 |
error (msg ^ ": " ^ string_of_thm thm); |
|
211 |
||
212 |
fun abs_norm thy thm = |
|
213 |
let |
|
214 |
fun expvars t = |
|
215 |
let |
|
216 |
val lhs = (fst o Logic.dest_equals) t; |
|
217 |
val tys = (fst o strip_type o type_of) lhs; |
|
218 |
val used = fold_aterms (fn Var ((v, _), _) => insert (op =) v | _ => I) lhs []; |
|
219 |
val vs = Term.invent_names used "x" (length tys); |
|
220 |
in |
|
221 |
map2 (fn v => fn ty => Var ((v, 0), ty)) vs tys |
|
222 |
end; |
|
223 |
fun expand ct thm = |
|
224 |
Thm.combination thm (Thm.reflexive ct); |
|
225 |
fun beta_norm thm = |
|
226 |
thm |
|
227 |
|> prop_of |
|
228 |
|> Logic.dest_equals |
|
229 |
|> fst |
|
230 |
|> cterm_of thy |
|
231 |
|> Thm.beta_conversion true |
|
232 |
|> Thm.symmetric |
|
233 |
|> (fn thm' => Thm.transitive thm' thm); |
|
234 |
in |
|
235 |
thm |
|
236 |
|> fold (expand o cterm_of thy) ((expvars o prop_of) thm) |
|
237 |
|> beta_norm |
|
238 |
end; |
|
239 |
||
240 |
fun canonical_tvars thy thm = |
|
241 |
let |
|
242 |
fun mk_inst (v_i as (v, i), (v', sort)) (s as (maxidx, set, acc)) = |
|
243 |
if v = v' orelse member (op =) set v then s |
|
244 |
else let |
|
245 |
val ty = TVar (v_i, sort) |
|
246 |
in |
|
247 |
(maxidx + 1, v :: set, |
|
248 |
(ctyp_of thy ty, ctyp_of thy (TVar ((v', maxidx), sort))) :: acc) |
|
249 |
end; |
|
250 |
val lower_name = (prefix "'" o implode o map (Char.toString o Char.toLower o the o Char.fromString) |
|
251 |
o explode o proper_name o unprefix "'"); |
|
252 |
fun tvars_of thm = (fold_types o fold_atyps) |
|
253 |
(fn TVar (v_i as (v, i), sort) => cons (v_i, (lower_name v, sort)) |
|
254 |
| _ => I) (prop_of thm) []; |
|
255 |
val maxidx = Thm.maxidx_of thm + 1; |
|
256 |
val (_, _, inst) = fold mk_inst (tvars_of thm) (maxidx + 1, [], []); |
|
257 |
in Thm.instantiate (inst, []) thm end; |
|
258 |
||
259 |
fun canonical_vars thy thm = |
|
260 |
let |
|
261 |
fun mk_inst (v_i as (v, i), (v', ty)) (s as (maxidx, set, acc)) = |
|
262 |
if v = v' orelse member (op =) set v then s |
|
263 |
else let |
|
264 |
val t = if i = ~1 then Free (v, ty) else Var (v_i, ty) |
|
265 |
in |
|
266 |
(maxidx + 1, v :: set, |
|
267 |
(cterm_of thy t, cterm_of thy (Var ((v', maxidx), ty))) :: acc) |
|
268 |
end; |
|
269 |
val lower_name = (implode o map (Char.toString o Char.toLower o the o Char.fromString) |
|
270 |
o explode o proper_name); |
|
271 |
fun vars_of thm = fold_aterms |
|
272 |
(fn Var (v_i as (v, i), ty) => cons (v_i, (lower_name v, ty)) |
|
273 |
| _ => I) (prop_of thm) []; |
|
274 |
val maxidx = Thm.maxidx_of thm + 1; |
|
275 |
val (_, _, inst) = fold mk_inst (vars_of thm) (maxidx + 1, [], []); |
|
276 |
in Thm.instantiate ([], inst) thm end; |
|
277 |
||
278 |
fun make_eq thy = |
|
279 |
let |
|
280 |
val ((_, atomize), _) = get_obj thy; |
|
281 |
in rewrite_rule [atomize] end; |
|
282 |
||
283 |
fun dest_eq thy thm = |
|
284 |
case try (make_eq thy #> Drule.plain_prop_of |
|
285 |
#> ObjectLogic.drop_judgment thy #> Logic.dest_equals) thm |
|
286 |
of SOME eq => (eq, thm) |
|
287 |
| NONE => err_thm "not an equation" thm; |
|
288 |
||
289 |
fun dest_fun thy thm = |
|
290 |
let |
|
291 |
fun dest_fun' ((lhs, _), thm) = |
|
292 |
case try (dest_Const o fst o strip_comb) lhs |
|
293 |
of SOME (c, ty) => (c, (ty, thm)) |
|
294 |
| NONE => err_thm "not a function equation" thm; |
|
295 |
in |
|
296 |
thm |
|
297 |
|> debug_msg (prefix "[cg_thm] dest_fun " o Pretty.output o Display.pretty_thm) |
|
298 |
|> dest_eq thy |
|
299 |
|> dest_fun' |
|
300 |
end; |
|
301 |
||
302 |
||
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
303 |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
304 |
(** theory data **) |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
305 |
|
19597 | 306 |
(* data structures *) |
307 |
||
308 |
fun merge' eq (xys as (xs, ys)) = |
|
309 |
if eq_list eq (xs, ys) then (false, xs) else (true, merge eq xys); |
|
310 |
||
311 |
fun alist_merge' eq_key eq (xys as (xs, ys)) = |
|
312 |
if eq_list (eq_pair eq_key eq) (xs, ys) then (false, xs) else (true, AList.merge eq_key eq xys); |
|
313 |
||
314 |
fun list_symtab_join' eq (xyt as (xt, yt)) = |
|
315 |
let |
|
316 |
val xc = Symtab.keys xt; |
|
317 |
val yc = Symtab.keys yt; |
|
318 |
val zc = filter (member (op =) yc) xc; |
|
319 |
val wc = subtract (op =) zc xc @ subtract (op =) zc yc; |
|
320 |
fun same_thms c = if eq_list eq_thm ((the o Symtab.lookup xt) c, (the o Symtab.lookup yt) c) |
|
321 |
then NONE else SOME c; |
|
322 |
in (wc @ map_filter same_thms zc, Symtab.join (K (merge eq)) xyt) end; |
|
323 |
||
324 |
datatype notify = Notify of (serial * (string option -> theory -> theory)) list; |
|
325 |
||
326 |
val mk_notify = Notify; |
|
327 |
fun map_notify f (Notify notify) = mk_notify (f notify); |
|
328 |
fun merge_notify pp (Notify notify1, Notify notify2) = |
|
329 |
mk_notify (AList.merge (op =) (K true) (notify1, notify2)); |
|
330 |
||
331 |
datatype preproc = Preproc of { |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
332 |
preprocs: (serial * (theory -> thm list -> thm list)) list, |
19597 | 333 |
unfolds: thm list |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
334 |
}; |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
335 |
|
19597 | 336 |
fun mk_preproc (preprocs, unfolds) = |
337 |
Preproc { preprocs = preprocs, unfolds = unfolds }; |
|
338 |
fun map_preproc f (Preproc { preprocs, unfolds }) = |
|
339 |
mk_preproc (f (preprocs, unfolds)); |
|
340 |
fun merge_preproc _ (Preproc { preprocs = preprocs1, unfolds = unfolds1 }, |
|
341 |
Preproc { preprocs = preprocs2, unfolds = unfolds2 }) = |
|
342 |
let |
|
343 |
val (dirty1, preprocs) = alist_merge' (op =) (K true) (preprocs1, preprocs2); |
|
344 |
val (dirty2, unfolds) = merge' eq_thm (unfolds1, unfolds2); |
|
345 |
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
|
346 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
347 |
datatype extrs = Extrs of { |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
348 |
funs: (serial * (theory -> string * typ -> thm list)) list, |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
349 |
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
|
350 |
}; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
351 |
|
19597 | 352 |
fun mk_extrs (funs, datatypes) = |
353 |
Extrs { funs = funs, datatypes = datatypes }; |
|
354 |
fun map_extrs f (Extrs { funs, datatypes }) = |
|
355 |
mk_extrs (f (funs, datatypes)); |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
356 |
fun merge_extrs _ (Extrs { funs = funs1, datatypes = datatypes1 }, |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
357 |
Extrs { funs = funs2, datatypes = datatypes2 }) = |
19597 | 358 |
let |
359 |
val (dirty1, funs) = alist_merge' (op =) (K true) (funs1, funs2); |
|
360 |
val (dirty2, datatypes) = alist_merge' (op =) (K true) (datatypes1, datatypes2); |
|
361 |
in (dirty1 orelse dirty2, mk_extrs (funs, datatypes)) end; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
362 |
|
19597 | 363 |
datatype funthms = Funthms of { |
364 |
dirty: string list, |
|
365 |
funs: thm list Symtab.table |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
366 |
}; |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
367 |
|
19597 | 368 |
fun mk_funthms (dirty, funs) = |
369 |
Funthms { dirty = dirty, funs = funs }; |
|
370 |
fun map_funthms f (Funthms { dirty, funs }) = |
|
371 |
mk_funthms (f (dirty, funs)); |
|
372 |
fun merge_funthms _ (Funthms { dirty = dirty1, funs = funs1 }, |
|
373 |
Funthms { dirty = dirty2, funs = funs2 }) = |
|
374 |
let |
|
375 |
val (dirty3, funs) = list_symtab_join' eq_thm (funs1, funs2); |
|
376 |
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
|
377 |
|
19597 | 378 |
datatype T = T of { |
379 |
dirty: bool, |
|
380 |
notify: notify, |
|
381 |
preproc: preproc, |
|
382 |
extrs: extrs, |
|
383 |
funthms: funthms |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
384 |
}; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
385 |
|
19597 | 386 |
fun mk_T ((dirty, notify), (preproc, (extrs, funthms))) = |
387 |
T { dirty = dirty, notify = notify, preproc = preproc, extrs = extrs, funthms = funthms }; |
|
388 |
fun map_T f (T { dirty, notify, preproc, extrs, funthms }) = |
|
389 |
mk_T (f ((dirty, notify), (preproc, (extrs, funthms)))); |
|
390 |
fun merge_T pp (T { dirty = dirty1, notify = notify1, preproc = preproc1, extrs = extrs1, funthms = funthms1 }, |
|
391 |
T { dirty = dirty2, notify = notify2, preproc = preproc2, extrs = extrs2, funthms = funthms2 }) = |
|
392 |
let |
|
393 |
val (dirty3, preproc) = merge_preproc pp (preproc1, preproc2); |
|
394 |
val (dirty4, extrs) = merge_extrs pp (extrs1, extrs2); |
|
395 |
in |
|
396 |
mk_T ((dirty1 orelse dirty2 orelse dirty3 orelse dirty4, merge_notify pp (notify1, notify2)), |
|
397 |
(preproc, (extrs, merge_funthms pp (funthms1, funthms2)))) |
|
398 |
end; |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
399 |
|
19597 | 400 |
|
401 |
(* setup *) |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
402 |
|
19597 | 403 |
structure CodegenTheoremsData = TheoryDataFun |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
404 |
(struct |
19597 | 405 |
val name = "Pure/CodegenTheoremsData"; |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
406 |
type T = T; |
19597 | 407 |
val empty = mk_T ((false, mk_notify []), (mk_preproc ([], []), |
408 |
(mk_extrs ([], []), mk_funthms ([], Symtab.empty)))); |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
409 |
val copy = I; |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
410 |
val extend = I; |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
411 |
val merge = merge_T; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
412 |
fun print (thy : theory) (data : T) = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
413 |
let |
19436 | 414 |
val pretty_thm = ProofContext.pretty_thm (ProofContext.init thy); |
19597 | 415 |
val funthms = (fn T { funthms, ... } => funthms) data; |
416 |
val funs = (Symtab.dest o (fn Funthms { funs, ... } => funs)) funthms; |
|
417 |
val preproc = (fn T { preproc, ... } => preproc) data; |
|
418 |
val unfolds = (fn Preproc { unfolds, ... } => unfolds) preproc; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
419 |
in |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
420 |
(Pretty.writeln o Pretty.block o Pretty.fbreaks) ([ |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
421 |
Pretty.str "code generation theorems:", |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
422 |
Pretty.str "function theorems:" ] @ |
19597 | 423 |
(*Pretty.fbreaks ( *) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
424 |
map (fn (c, thms) => |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
425 |
(Pretty.block o Pretty.fbreaks) ( |
19436 | 426 |
Pretty.str c :: map pretty_thm thms |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
427 |
) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
428 |
) funs |
19597 | 429 |
(*) *) @ [ |
430 |
Pretty.fbrk, |
|
431 |
Pretty.block ( |
|
432 |
Pretty.str "unfolding theorems:" |
|
433 |
:: Pretty.fbrk |
|
434 |
:: (Pretty.fbreaks o map pretty_thm) unfolds |
|
435 |
)]) |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
436 |
end; |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
437 |
end); |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
438 |
|
19597 | 439 |
val _ = Context.add_setup CodegenTheoremsData.init; |
440 |
val print_thms = CodegenTheoremsData.print; |
|
441 |
||
442 |
||
443 |
(* accessors *) |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
444 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
445 |
local |
19597 | 446 |
val the_preproc = (fn T { preproc = Preproc preproc, ... } => preproc) o CodegenTheoremsData.get; |
447 |
val the_extrs = (fn T { extrs = Extrs extrs, ... } => extrs) o CodegenTheoremsData.get; |
|
448 |
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
|
449 |
in |
19597 | 450 |
val is_dirty = (fn T { dirty = dirty, ... } => dirty) o CodegenTheoremsData.get; |
451 |
val the_dirty_consts = (fn { dirty = dirty, ... } => dirty) o the_funthms; |
|
452 |
val the_notify = (fn T { notify = Notify notify, ... } => map snd notify) o CodegenTheoremsData.get; |
|
453 |
val the_preprocs = (fn { preprocs, ... } => map snd preprocs) o the_preproc; |
|
454 |
val the_unfolds = (fn { unfolds, ... } => unfolds) o the_preproc; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
455 |
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
|
456 |
val the_datatypes_extrs = (fn { datatypes, ... } => map snd datatypes) o the_extrs; |
19597 | 457 |
val the_funs = (fn { funs, ... } => funs) o the_funthms; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
458 |
end (*local*); |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
459 |
|
19597 | 460 |
val map_data = CodegenTheoremsData.map o map_T; |
461 |
||
462 |
(* notifiers *) |
|
463 |
||
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
464 |
fun add_notify f = |
19597 | 465 |
map_data (fn ((dirty, notify), x) => |
466 |
((dirty, notify |> map_notify (cons (serial (), f))), x)); |
|
467 |
||
468 |
fun get_reset_dirty thy = |
|
469 |
let |
|
470 |
val dirty = is_dirty thy; |
|
471 |
val dirty_const = if dirty then [] else the_dirty_consts thy; |
|
472 |
in |
|
473 |
thy |
|
474 |
|> map_data (fn ((_, notify), (procs, (extrs, funthms))) => |
|
475 |
((false, notify), (procs, (extrs, funthms |> map_funthms (fn (_, funs) => ([], funs)))))) |
|
476 |
|> pair (dirty, dirty_const) |
|
477 |
end; |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
478 |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
479 |
fun notify_all c thy = |
19597 | 480 |
thy |
481 |
|> get_reset_dirty |
|
482 |
|-> (fn (true, _) => fold (fn f => f NONE) (the_notify thy) |
|
483 |
| (false, cs) => let val cs' = case c of NONE => cs | SOME c => c::cs |
|
484 |
in fold (fn f => fold (f o SOME) cs') (the_notify thy) end); |
|
485 |
||
486 |
fun notify_dirty thy = |
|
487 |
thy |
|
488 |
|> get_reset_dirty |
|
489 |
|-> (fn (true, _) => fold (fn f => f NONE) (the_notify thy) |
|
490 |
| (false, cs) => fold (fn f => fold (f o SOME) cs) (the_notify thy)); |
|
491 |
||
492 |
||
493 |
(* modifiers *) |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
494 |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
495 |
fun add_preproc f = |
19597 | 496 |
map_data (fn (x, (preproc, y)) => |
497 |
(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
|
498 |
#> notify_all NONE; |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
499 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
500 |
fun add_fun_extr f = |
19597 | 501 |
map_data (fn (x, (preproc, (extrs, funthms))) => |
502 |
(x, (preproc, (extrs |> map_extrs (fn (funs, datatypes) => |
|
503 |
((serial (), f) :: funs, datatypes)), funthms)))) |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
504 |
#> notify_all NONE; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
505 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
506 |
fun add_datatype_extr f = |
19597 | 507 |
map_data (fn (x, (preproc, (extrs, funthms))) => |
508 |
(x, (preproc, (extrs |> map_extrs (fn (funs, datatypes) => |
|
509 |
(funs, (serial (), f) :: datatypes)), funthms)))) |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
510 |
#> notify_all NONE; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
511 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
512 |
fun add_fun thm thy = |
19597 | 513 |
case dest_fun thy thm |
514 |
of (c, _) => |
|
515 |
thy |
|
516 |
|> map_data (fn (x, (preproc, (extrs, funthms))) => |
|
517 |
(x, (preproc, (extrs, funthms |> map_funthms (fn (dirty, funs) => |
|
518 |
(dirty, funs |> Symtab.default (c, []) |> Symtab.map_entry c (fn thms => thms @ [thm]))))))) |
|
519 |
|> notify_all (SOME c); |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
520 |
|
19597 | 521 |
fun del_fun thm thy = |
522 |
case dest_fun thy thm |
|
523 |
of (c, _) => |
|
524 |
thy |
|
525 |
|> map_data (fn (x, (preproc, (extrs, funthms))) => |
|
526 |
(x, (preproc, (extrs, funthms |> map_funthms (fn (dirty, funs) => |
|
527 |
(dirty, funs |> Symtab.map_entry c (remove eq_thm thm))))))) |
|
528 |
|> notify_all (SOME c); |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
529 |
|
19597 | 530 |
fun add_unfold thm thy = |
531 |
thy |
|
532 |
|> tap (fn thy => dest_eq thy thm) |
|
533 |
|> map_data (fn (x, (preproc, y)) => |
|
534 |
(x, (preproc |> map_preproc (fn (preprocs, unfolds) => |
|
535 |
(preprocs, thm :: unfolds)), y))) |
|
536 |
|> notify_all NONE; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
537 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
538 |
fun del_unfold thm = |
19597 | 539 |
map_data (fn (x, (preproc, y)) => |
540 |
(x, (preproc |> map_preproc (fn (preprocs, unfolds) => |
|
541 |
(preprocs, remove eq_thm thm unfolds)), y))) |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
542 |
#> notify_all NONE; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
543 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
544 |
fun purge_defs (c, ty) thy = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
545 |
thy |
19597 | 546 |
|> map_data (fn (x, (preproc, (extrs, funthms))) => |
547 |
(x, (preproc, (extrs, funthms |> map_funthms (fn (dirty, funs) => |
|
548 |
(dirty, funs |> Symtab.map_entry c |
|
549 |
(filter (fn thm => Sign.typ_instance thy |
|
550 |
((fst o snd o dest_fun thy) thm, ty))))))))) |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
551 |
|> notify_all (SOME c); |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
552 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
553 |
|
19597 | 554 |
|
555 |
(** theorem handling **) |
|
556 |
||
557 |
(* preprocessing *) |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
558 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
559 |
fun common_typ thy _ [] = [] |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
560 |
| common_typ thy _ [thm] = [thm] |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
561 |
| common_typ thy extract_typ thms = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
562 |
let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
563 |
fun incr_thm thm max = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
564 |
let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
565 |
val thm' = incr_indexes max thm; |
19597 | 566 |
val max' = (maxidx_of_typ o type_of o Drule.plain_prop_of) thm' + 1; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
567 |
in (thm', max') end; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
568 |
val (thms', maxidx) = fold_map incr_thm thms 0; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
569 |
val (ty1::tys) = map extract_typ thms; |
19436 | 570 |
fun unify ty = Sign.typ_unify thy (ty1, ty); |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
571 |
val (env, _) = fold unify tys (Vartab.empty, maxidx) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
572 |
val instT = Vartab.fold (fn (x_i, (sort, ty)) => |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
573 |
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
|
574 |
in map (Thm.instantiate (instT, [])) thms end; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
575 |
|
19597 | 576 |
fun extr_typ thy thm = case dest_fun thy thm |
577 |
of (_, (ty, _)) => ty; |
|
578 |
||
579 |
fun tap_typ thy [] = NONE |
|
580 |
| tap_typ thy (thms as (thm::_)) = SOME (extr_typ thy thm, thms); |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
581 |
|
19597 | 582 |
fun preprocess' thy extr_ty thms = |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
583 |
let |
19597 | 584 |
fun burrow_thms f [] = [] |
585 |
| burrow_thms f thms = |
|
586 |
thms |
|
587 |
|> Conjunction.intr_list |
|
588 |
|> f |
|
589 |
|> Conjunction.elim_list; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
590 |
in |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
591 |
thms |
19597 | 592 |
|> map (make_eq thy) |
593 |
|> map (Thm.transfer thy) |
|
594 |
|> fold (fn f => f thy) (the_preprocs thy) |
|
595 |
|> map (rewrite_rule (map (make_eq thy) (the_unfolds thy))) |
|
596 |
|> debug_msg (fn _ => "[cg_thm] filter") |
|
597 |
|> debug_msg (commas o map string_of_thm) |
|
598 |
|> debug_msg (fn _ => "[cg_thm] extr_typ") |
|
599 |
|> debug_msg (commas o map string_of_thm) |
|
600 |
|> debug_msg (fn _ => "[cg_thm] common_typ / abs_norm") |
|
601 |
|> debug_msg (commas o map string_of_thm) |
|
602 |
|> (if extr_ty then common_typ thy (extr_typ thy) #> map (abs_norm thy) else I) |
|
603 |
|> burrow_thms ( |
|
604 |
debug_msg (fn _ => "[cg_thm] canonical tvars") |
|
605 |
#> debug_msg (string_of_thm) |
|
606 |
#> canonical_tvars thy |
|
607 |
#> debug_msg (fn _ => "[cg_thm] canonical vars") |
|
608 |
#> debug_msg (string_of_thm) |
|
609 |
#> canonical_vars thy |
|
610 |
#> debug_msg (fn _ => "[cg_thm] zero indices") |
|
611 |
#> debug_msg (string_of_thm) |
|
612 |
#> Drule.zero_var_indexes |
|
613 |
) |
|
614 |
|> map Drule.unvarifyT |
|
615 |
|> map Drule.unvarify |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
616 |
end; |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
617 |
|
19597 | 618 |
fun preprocess thy = preprocess' thy true #> tap_typ thy; |
619 |
||
620 |
fun preprocess_term thy raw_t = |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
621 |
let |
19806 | 622 |
val t = Logic.legacy_varify raw_t; |
19597 | 623 |
val x = variant (add_term_names (t, [])) "a"; |
624 |
val t_eq = Logic.mk_equals (Free (x, fastype_of t), t); |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
625 |
(*fake definition*) |
19597 | 626 |
val thm_eq = setmp quick_and_dirty true (SkipProof.make_thm thy) |
627 |
t_eq; |
|
628 |
fun err ts' = error ("preprocess_term: bad preprocessor; started with " |
|
629 |
^ (quote o Sign.string_of_term thy) t_eq ^ ", ended up with " |
|
630 |
^ (quote o commas o map (Sign.string_of_term thy)) ts' |
|
631 |
) |
|
632 |
in case preprocess' thy false [thm_eq] |
|
633 |
of [thm] => (case Drule.plain_prop_of thm |
|
634 |
of t_res as (Const ("==", _) $ Free (x', _) $ t') => if x = x' then t' else err [t_res] |
|
635 |
| t_res => err [t_res]) |
|
636 |
| thms => (err o map Drule.plain_prop_of) thms |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
637 |
end; |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
638 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
639 |
|
19597 | 640 |
(* retrieval *) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
641 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
642 |
fun get_funs thy (c, ty) = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
643 |
let |
19597 | 644 |
val _ = debug_msg (fn _ => "[cg_thm] const (1) " ^ c ^ " :: " ^ Sign.string_of_typ thy ty) () |
645 |
val filter_typ = map_filter (fn (_, (ty', thm)) => |
|
646 |
if Sign.typ_instance thy (ty, ty') |
|
647 |
then SOME thm else debug_msg (fn _ => "[cg_thm] dropping " ^ string_of_thm thm) NONE); |
|
648 |
fun get_funs (c, ty) = |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
649 |
(these o Symtab.lookup (the_funs thy)) c |
19597 | 650 |
|> debug_msg (fn _ => "[cg_thm] trying funs") |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
651 |
|> map (dest_fun thy) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
652 |
|> filter_typ; |
19597 | 653 |
fun get_extr (c, ty) = |
654 |
getf_first_list (map (fn f => f thy) (the_funs_extrs thy)) (c, ty) |
|
655 |
|> debug_msg (fn _ => "[cg_thm] trying extr") |
|
656 |
|> map (dest_fun thy) |
|
657 |
|> filter_typ; |
|
658 |
fun get_spec (c, ty) = |
|
659 |
Theory.definitions_of thy c |
|
660 |
|> debug_msg (fn _ => "[cg_thm] trying spec") |
|
661 |
(* FIXME avoid dynamic name space lookup!? (via Thm.get_axiom_i etc.??) *) |
|
662 |
|> maps (PureThy.get_thms thy o Name o #name) |
|
663 |
|> map_filter (try (dest_fun thy)) |
|
664 |
|> filter_typ; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
665 |
in |
19597 | 666 |
getf_first_list [get_funs, get_extr, get_spec] (c, ty) |
667 |
|> debug_msg (fn _ => "[cg_thm] const (2) " ^ c ^ " :: " ^ Sign.string_of_typ thy ty) |
|
668 |
|> preprocess thy |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
669 |
end; |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
670 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
671 |
fun get_datatypes thy dtco = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
672 |
let |
19597 | 673 |
val _ = debug_msg (fn _ => "[cg_thm] datatype " ^ dtco) () |
674 |
val truh = mk_true thy; |
|
675 |
val fals = mk_false thy; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
676 |
fun mk_lhs vs ((co1, tys1), (co2, tys2)) = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
677 |
let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
678 |
val dty = Type (dtco, map TFree vs); |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
679 |
val (xs1, xs2) = chop (length tys1) (Term.invent_names [] "x" (length tys1 + length tys2)); |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
680 |
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
|
681 |
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
|
682 |
fun zip_co co xs tys = list_comb (Const (co, |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
683 |
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
|
684 |
in |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
685 |
((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
|
686 |
end; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
687 |
fun mk_rhs [] [] = truh |
19597 | 688 |
| 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
|
689 |
fun mk_eq vs (args as ((co1, _), (co2, _))) (inj, dist) = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
690 |
if co1 = co2 |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
691 |
then let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
692 |
val ((fs1, fs2), lhs) = mk_lhs vs args; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
693 |
val rhs = mk_rhs fs1 fs2; |
19597 | 694 |
in (mk_func thy (lhs, rhs) :: inj, dist) end |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
695 |
else let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
696 |
val (_, lhs) = mk_lhs vs args; |
19597 | 697 |
in (inj, mk_func thy (lhs, fals) :: dist) end; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
698 |
fun mk_eqs (vs, cos) = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
699 |
let val cos' = rev cos |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
700 |
in (op @) (fold (mk_eq vs) (product cos' cos') ([], [])) end; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
701 |
fun mk_eq_thms tac vs_cos = |
19597 | 702 |
map (fn t => Goal.prove thy [] [] |
703 |
(ObjectLogic.ensure_propT thy t) (K tac) |> standard) (mk_eqs vs_cos); |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
704 |
in |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
705 |
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
|
706 |
of NONE => NONE |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
707 |
| 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
|
708 |
end; |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
709 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
710 |
fun get_eq thy (c, ty) = |
19597 | 711 |
if is_obj_eq thy c |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
712 |
then case get_datatypes thy ((fst o dest_Type o hd o fst o strip_type) ty) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
713 |
of SOME (_, thms) => thms |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
714 |
| _ => [] |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
715 |
else []; |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
716 |
|
19597 | 717 |
type thmtab = ((thm list Typtab.table Symtab.table |
718 |
* string Symtab.table) |
|
719 |
* ((string * sort) list * (string * typ list) list) Symtab.table); |
|
720 |
||
721 |
(* |
|
722 |
fun mk_thmtab thy cs = |
|
723 |
let |
|
724 |
fun add_c (c, ty) gr = |
|
725 |
(* |
|
726 |
Das ist noch viel komplizierter: Zyklen |
|
727 |
und die aktuellen Instantiierungen muss man auch noch mitschleppen |
|
728 |
man sieht: man braucht zusätzlich ein Mapping |
|
729 |
c ~> [ty] (Symtab) |
|
730 |
wobei dort immer die bislang allgemeinsten... ??? |
|
731 |
*) |
|
732 |
(* |
|
733 |
thm holen für bestimmten typ |
|
734 |
typ dann behalten |
|
735 |
typ normalisieren |
|
736 |
damit haben wir den key |
|
737 |
hier den check machen, ob schon prozessiert wurde |
|
738 |
NEIN: |
|
739 |
ablegen |
|
740 |
consts der rechten Seiten |
|
741 |
in die Rekursion gehen für alles |
|
742 |
JA: |
|
743 |
fertig |
|
744 |
*) |
|
745 |
in fold add_c cs Constgraph.empty end; |
|
746 |
||
747 |
fun get_thmtab cs thy = |
|
748 |
thy |
|
749 |
|> get_reset_dirty |
|
750 |
|-> (fn _ => I) |
|
751 |
|> `mk_thmtab; |
|
752 |
*) |
|
753 |
||
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
754 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
755 |
(** code attributes and setup **) |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
diff
changeset
|
756 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
757 |
local |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
758 |
fun add_simple_attribute (name, f) = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
759 |
(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
|
760 |
(Context.map_theory o f); |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
761 |
in |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
762 |
val _ = map (Context.add_setup o add_simple_attribute) [ |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
763 |
("fun", add_fun), |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
764 |
("unfold", (fn thm => Codegen.add_unfold thm #> add_unfold thm)), |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
765 |
("unfolt", add_unfold), |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
766 |
("nofold", del_unfold) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
767 |
] |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
768 |
end; (*local*) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
769 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
770 |
val _ = Context.add_setup (add_fun_extr get_eq); |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
771 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
772 |
end; (*struct*) |