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