author | haftmann |
Tue, 02 Oct 2007 07:59:55 +0200 | |
changeset 24811 | 3bf788a0c49a |
parent 24770 | 695a8e087b9f |
child 24835 | 8c26128f8997 |
permissions | -rw-r--r-- |
24219 | 1 |
(* Title: Tools/code/code_package.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
5 |
Code generator translation kernel. Code generator Isar setup. |
|
6 |
*) |
|
7 |
||
8 |
signature CODE_PACKAGE = |
|
9 |
sig |
|
10 |
val eval_conv: theory |
|
24381 | 11 |
-> (CodeThingol.code -> CodeThingol.typscheme * CodeThingol.iterm |
12 |
-> string list -> cterm -> thm) |
|
24283 | 13 |
-> cterm -> thm; |
14 |
val eval_term: theory |
|
24381 | 15 |
-> (CodeThingol.code -> CodeThingol.typscheme * CodeThingol.iterm |
16 |
-> string list -> cterm -> 'a) |
|
24283 | 17 |
-> cterm -> 'a; |
24585 | 18 |
val satisfies_ref: (unit -> bool) option ref; |
24283 | 19 |
val satisfies: theory -> cterm -> string list -> bool; |
24621 | 20 |
val eval_invoke: theory -> (string * (unit -> 'a) option ref) -> CodeThingol.code |
21 |
-> CodeThingol.iterm * CodeThingol.itype -> string list -> 'a; |
|
24219 | 22 |
val codegen_command: theory -> string -> unit; |
23 |
||
24 |
type appgen; |
|
25 |
val add_appconst: string * appgen -> theory -> theory; |
|
26 |
val appgen_let: appgen; |
|
27 |
val appgen_if: appgen; |
|
28 |
val appgen_case: (theory -> term |
|
29 |
-> ((string * typ) list * ((term * typ) * (term * term) list)) option) |
|
30 |
-> appgen; |
|
31 |
end; |
|
32 |
||
33 |
structure CodePackage : CODE_PACKAGE = |
|
34 |
struct |
|
35 |
||
36 |
open BasicCodeThingol; |
|
37 |
||
38 |
(** code translation **) |
|
39 |
||
40 |
(* theory data *) |
|
41 |
||
42 |
type appgen = theory -> ((sort -> sort) * Sorts.algebra) * Consts.T |
|
43 |
-> CodeFuncgr.T |
|
44 |
-> (string * typ) * term list -> CodeThingol.transact -> iterm * CodeThingol.transact; |
|
45 |
||
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
46 |
structure Appgens = TheoryDataFun |
24219 | 47 |
( |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
48 |
type T = (int * (appgen * stamp)) Symtab.table; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
49 |
val empty = Symtab.empty; |
24219 | 50 |
val copy = I; |
51 |
val extend = I; |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
52 |
fun merge _ = Symtab.merge (fn ((bounds1, (_, stamp1)), (bounds2, (_, stamp2))) => |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
53 |
bounds1 = bounds2 andalso stamp1 = stamp2); |
24219 | 54 |
); |
55 |
||
24283 | 56 |
fun code_depgr thy [] = CodeFuncgr.make thy [] |
24219 | 57 |
| code_depgr thy consts = |
58 |
let |
|
24283 | 59 |
val gr = CodeFuncgr.make thy consts; |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
60 |
val select = Graph.all_succs gr consts; |
24219 | 61 |
in |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
62 |
gr |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
63 |
|> Graph.subgraph (member (op =) select) |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
64 |
|> Graph.map_nodes ((apsnd o map) (Conv.fconv_rule (Class.overload thy))) |
24219 | 65 |
end; |
66 |
||
67 |
fun code_thms thy = |
|
68 |
Pretty.writeln o CodeFuncgr.pretty thy o code_depgr thy; |
|
69 |
||
70 |
fun code_deps thy consts = |
|
71 |
let |
|
72 |
val gr = code_depgr thy consts; |
|
73 |
fun mk_entry (const, (_, (_, parents))) = |
|
74 |
let |
|
75 |
val name = CodeUnit.string_of_const thy const; |
|
76 |
val nameparents = map (CodeUnit.string_of_const thy) parents; |
|
77 |
in { name = name, ID = name, dir = "", unfold = true, |
|
78 |
path = "", parents = nameparents } |
|
79 |
end; |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
80 |
val prgr = Graph.fold ((fn x => fn xs => xs @ [x]) o mk_entry) gr []; |
24219 | 81 |
in Present.display_graph prgr end; |
82 |
||
83 |
structure Program = CodeDataFun |
|
84 |
( |
|
85 |
type T = CodeThingol.code; |
|
86 |
val empty = CodeThingol.empty_code; |
|
87 |
fun merge _ = CodeThingol.merge_code; |
|
88 |
fun purge _ NONE _ = CodeThingol.empty_code |
|
89 |
| purge NONE _ _ = CodeThingol.empty_code |
|
90 |
| purge (SOME thy) (SOME cs) code = |
|
91 |
let |
|
92 |
val cs_exisiting = |
|
93 |
map_filter (CodeName.const_rev thy) (Graph.keys code); |
|
94 |
val dels = (Graph.all_preds code |
|
95 |
o map (CodeName.const thy) |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
96 |
o filter (member (op =) cs_exisiting) |
24219 | 97 |
) cs; |
98 |
in Graph.del_nodes dels code end; |
|
99 |
); |
|
100 |
||
101 |
||
102 |
(* translation kernel *) |
|
103 |
||
24283 | 104 |
val value_name = "Isabelle_Eval.EVAL.EVAL"; |
105 |
||
106 |
fun ensure_def thy = CodeThingol.ensure_def |
|
107 |
(fn s => if s = value_name then "<term>" else CodeName.labelled_name thy s); |
|
24219 | 108 |
|
24811 | 109 |
exception CONSTRAIN of (string * typ) * typ; |
110 |
||
24219 | 111 |
fun ensure_def_class thy (algbr as ((_, algebra), _)) funcgr class = |
112 |
let |
|
113 |
val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class; |
|
114 |
val (v, cs) = AxClass.params_of_class thy class; |
|
115 |
val class' = CodeName.class thy class; |
|
116 |
val defgen_class = |
|
24811 | 117 |
fold_map (fn superclass => ensure_def_class thy algbr funcgr superclass |
118 |
##>> ensure_def_classrel thy algbr funcgr (class, superclass)) superclasses |
|
119 |
##>> fold_map (fn (c, ty) => ensure_def_const thy algbr funcgr c |
|
120 |
##>> exprgen_typ thy algbr funcgr ty) cs |
|
121 |
#>> (fn info => CodeThingol.Class (unprefix "'" v, info)) |
|
24219 | 122 |
in |
24811 | 123 |
ensure_def thy defgen_class class' |
24219 | 124 |
#> pair class' |
125 |
end |
|
126 |
and ensure_def_classrel thy algbr funcgr (subclass, superclass) = |
|
24811 | 127 |
let |
128 |
val classrel' = CodeName.classrel thy (subclass, superclass); |
|
129 |
val defgen_classrel = |
|
130 |
ensure_def_class thy algbr funcgr subclass |
|
131 |
##>> ensure_def_class thy algbr funcgr superclass |
|
132 |
#>> CodeThingol.Classrel; |
|
133 |
in |
|
134 |
ensure_def thy defgen_classrel classrel' |
|
135 |
#> pair classrel' |
|
136 |
end |
|
24250 | 137 |
and ensure_def_tyco thy algbr funcgr "fun" = |
138 |
pair "fun" |
|
139 |
| ensure_def_tyco thy algbr funcgr tyco = |
|
24219 | 140 |
let |
24250 | 141 |
val defgen_datatype = |
24219 | 142 |
let |
143 |
val (vs, cos) = Code.get_datatype thy tyco; |
|
144 |
in |
|
24250 | 145 |
fold_map (exprgen_tyvar_sort thy algbr funcgr) vs |
146 |
##>> fold_map (fn (c, tys) => |
|
24811 | 147 |
ensure_def_const thy algbr funcgr c |
148 |
##>> fold_map (exprgen_typ thy algbr funcgr) tys) cos |
|
149 |
#>> CodeThingol.Datatype |
|
24219 | 150 |
end; |
151 |
val tyco' = CodeName.tyco thy tyco; |
|
152 |
in |
|
24811 | 153 |
ensure_def thy defgen_datatype tyco' |
24250 | 154 |
#> pair tyco' |
24219 | 155 |
end |
24585 | 156 |
and exprgen_tyvar_sort thy (algbr as ((proj_sort, _), _)) funcgr (v, sort) = |
157 |
fold_map (ensure_def_class thy algbr funcgr) (proj_sort sort) |
|
158 |
#>> (fn sort => (unprefix "'" v, sort)) |
|
159 |
and exprgen_typ thy algbr funcgr (TFree vs) = |
|
160 |
exprgen_tyvar_sort thy algbr funcgr vs |
|
161 |
#>> (fn (v, sort) => ITyVar v) |
|
162 |
| exprgen_typ thy algbr funcgr (Type (tyco, tys)) = |
|
163 |
ensure_def_tyco thy algbr funcgr tyco |
|
164 |
##>> fold_map (exprgen_typ thy algbr funcgr) tys |
|
24811 | 165 |
#>> (fn (tyco, tys) => tyco `%% tys) |
166 |
and exprgen_dicts thy (algbr as ((proj_sort, algebra), consts)) funcgr (ty_ctxt, sort_decl) = |
|
24219 | 167 |
let |
168 |
val pp = Sign.pp thy; |
|
169 |
datatype typarg = |
|
170 |
Global of (class * string) * typarg list list |
|
171 |
| Local of (class * class) list * (string * (int * sort)); |
|
172 |
fun class_relation (Global ((_, tyco), yss), _) class = |
|
173 |
Global ((class, tyco), yss) |
|
174 |
| class_relation (Local (classrels, v), subclass) superclass = |
|
175 |
Local ((subclass, superclass) :: classrels, v); |
|
176 |
fun type_constructor tyco yss class = |
|
177 |
Global ((class, tyco), (map o map) fst yss); |
|
178 |
fun type_variable (TFree (v, sort)) = |
|
179 |
let |
|
180 |
val sort' = proj_sort sort; |
|
181 |
in map_index (fn (n, class) => (Local ([], (v, (n, sort'))), class)) sort' end; |
|
182 |
val typargs = Sorts.of_sort_derivation pp algebra |
|
183 |
{class_relation = class_relation, type_constructor = type_constructor, |
|
184 |
type_variable = type_variable} |
|
185 |
(ty_ctxt, proj_sort sort_decl); |
|
186 |
fun mk_dict (Global (inst, yss)) = |
|
187 |
ensure_def_inst thy algbr funcgr inst |
|
188 |
##>> (fold_map o fold_map) mk_dict yss |
|
189 |
#>> (fn (inst, dss) => DictConst (inst, dss)) |
|
190 |
| mk_dict (Local (classrels, (v, (k, sort)))) = |
|
191 |
fold_map (ensure_def_classrel thy algbr funcgr) classrels |
|
192 |
#>> (fn classrels => DictVar (classrels, (unprefix "'" v, (k, length sort)))) |
|
193 |
in |
|
194 |
fold_map mk_dict typargs |
|
195 |
end |
|
196 |
and exprgen_dict_parms thy (algbr as (_, consts)) funcgr (c, ty_ctxt) = |
|
197 |
let |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
198 |
val ty_decl = Consts.the_declaration consts c; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
199 |
val (tys, tys_decl) = pairself (curry (Consts.typargs consts) c) (ty_ctxt, ty_decl); |
24219 | 200 |
val sorts = map (snd o dest_TVar) tys_decl; |
201 |
in |
|
202 |
fold_map (exprgen_dicts thy algbr funcgr) (tys ~~ sorts) |
|
203 |
end |
|
24585 | 204 |
and exprgen_eq thy algbr funcgr thm = |
205 |
let |
|
206 |
val (args, rhs) = (apfst (snd o strip_comb) o Logic.dest_equals |
|
207 |
o Logic.unvarify o prop_of) thm; |
|
208 |
in |
|
209 |
fold_map (exprgen_term thy algbr funcgr) args |
|
210 |
##>> exprgen_term thy algbr funcgr rhs |
|
211 |
#>> rpair thm |
|
212 |
end |
|
213 |
and ensure_def_inst thy (algbr as ((_, algebra), _)) funcgr (class, tyco) = |
|
24219 | 214 |
let |
215 |
val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class; |
|
216 |
val (var, classops) = try (AxClass.params_of_class thy) class |> the_default ("'a", []) |
|
24585 | 217 |
val vs = Name.names Name.context "'a" (Sorts.mg_domain algebra tyco [class]); |
218 |
val sorts' = Sorts.mg_domain (Sign.classes_of thy) tyco [class]; |
|
219 |
val vs' = map2 (fn (v, sort1) => fn sort2 => (v, |
|
220 |
Sorts.inter_sort (Sign.classes_of thy) (sort1, sort2))) vs sorts'; |
|
24219 | 221 |
val arity_typ = Type (tyco, map TFree vs); |
24585 | 222 |
val arity_typ' = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) vs'); |
223 |
fun gen_superarity superclass = |
|
224 |
ensure_def_class thy algbr funcgr superclass |
|
225 |
##>> ensure_def_classrel thy algbr funcgr (class, superclass) |
|
226 |
##>> exprgen_dicts thy algbr funcgr (arity_typ, [superclass]) |
|
227 |
#>> (fn ((superclass, classrel), [DictConst (inst, dss)]) => |
|
24219 | 228 |
(superclass, (classrel, (inst, dss)))); |
24585 | 229 |
fun gen_classop_def (c, ty) = |
230 |
let |
|
231 |
val c_inst = Const (c, map_type_tfree (K arity_typ') ty); |
|
232 |
val thm = Class.unoverload thy (Thm.cterm_of thy c_inst); |
|
233 |
val c_ty = (apsnd Logic.unvarifyT o dest_Const o snd |
|
234 |
o Logic.dest_equals o Thm.prop_of) thm; |
|
235 |
in |
|
236 |
ensure_def_const thy algbr funcgr c |
|
237 |
##>> exprgen_const thy algbr funcgr c_ty |
|
238 |
#>> (fn (c, IConst c_inst) => ((c, c_inst), thm)) |
|
239 |
end; |
|
240 |
val defgen_inst = |
|
241 |
ensure_def_class thy algbr funcgr class |
|
242 |
##>> ensure_def_tyco thy algbr funcgr tyco |
|
243 |
##>> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs |
|
244 |
##>> fold_map gen_superarity superclasses |
|
245 |
##>> fold_map gen_classop_def classops |
|
246 |
#>> (fn ((((class, tyco), arity), superarities), classops) => |
|
24250 | 247 |
CodeThingol.Classinst ((class, (tyco, arity)), (superarities, classops))); |
24219 | 248 |
val inst = CodeName.instance thy (class, tyco); |
249 |
in |
|
24811 | 250 |
ensure_def thy defgen_inst inst |
24585 | 251 |
#> pair inst |
24219 | 252 |
end |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
253 |
and ensure_def_const thy (algbr as (_, consts)) funcgr c = |
24219 | 254 |
let |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
255 |
val c' = CodeName.const thy c; |
24585 | 256 |
fun defgen_datatypecons tyco = |
257 |
ensure_def_tyco thy algbr funcgr tyco |
|
24811 | 258 |
#>> K (CodeThingol.Datatypecons c'); |
24585 | 259 |
fun defgen_classop class = |
260 |
ensure_def_class thy algbr funcgr class |
|
24811 | 261 |
#>> K (CodeThingol.Classop c'); |
24219 | 262 |
fun defgen_fun trns = |
263 |
let |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
264 |
val raw_thms = CodeFuncgr.funcs funcgr c; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
265 |
val ty = (Logic.unvarifyT o CodeFuncgr.typ funcgr) c; |
24585 | 266 |
val vs = (map dest_TFree o Consts.typargs consts) (c, ty); |
24219 | 267 |
val thms = if (null o Term.typ_tfrees) ty orelse (null o fst o strip_type) ty |
268 |
then raw_thms |
|
269 |
else map (CodeUnit.expand_eta 1) raw_thms; |
|
270 |
in |
|
271 |
trns |
|
24381 | 272 |
|> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs |
24219 | 273 |
||>> exprgen_typ thy algbr funcgr ty |
24585 | 274 |
||>> fold_map (exprgen_eq thy algbr funcgr) thms |
24381 | 275 |
|>> (fn ((vs, ty), eqs) => CodeThingol.Fun ((vs, ty), eqs)) |
24219 | 276 |
end; |
24585 | 277 |
val defgen = case Code.get_datatype_of_constr thy c |
278 |
of SOME tyco => defgen_datatypecons tyco |
|
279 |
| NONE => (case AxClass.class_of_param thy c |
|
280 |
of SOME class => defgen_classop class |
|
281 |
| NONE => defgen_fun) |
|
24219 | 282 |
in |
24811 | 283 |
ensure_def thy defgen c' |
24250 | 284 |
#> pair c' |
24219 | 285 |
end |
24585 | 286 |
and exprgen_term thy algbr funcgr (Const (c, ty)) = |
287 |
select_appgen thy algbr funcgr ((c, ty), []) |
|
288 |
| exprgen_term thy algbr funcgr (Free (v, _)) = |
|
289 |
pair (IVar v) |
|
290 |
| exprgen_term thy algbr funcgr (Abs (abs as (_, ty, _))) = |
|
24219 | 291 |
let |
24585 | 292 |
val (v, t) = Syntax.variant_abs abs; |
24219 | 293 |
in |
24585 | 294 |
exprgen_typ thy algbr funcgr ty |
295 |
##>> exprgen_term thy algbr funcgr t |
|
296 |
#>> (fn (ty, t) => (v, ty) `|-> t) |
|
24219 | 297 |
end |
24585 | 298 |
| exprgen_term thy algbr funcgr (t as _ $ _) = |
24219 | 299 |
case strip_comb t |
300 |
of (Const (c, ty), ts) => |
|
24585 | 301 |
select_appgen thy algbr funcgr ((c, ty), ts) |
24219 | 302 |
| (t', ts) => |
24585 | 303 |
exprgen_term thy algbr funcgr t' |
304 |
##>> fold_map (exprgen_term thy algbr funcgr) ts |
|
305 |
#>> (fn (t, ts) => t `$$ ts) |
|
306 |
and exprgen_const thy algbr funcgr (c, ty) = |
|
307 |
ensure_def_const thy algbr funcgr c |
|
308 |
##>> exprgen_dict_parms thy algbr funcgr (c, ty) |
|
309 |
##>> fold_map (exprgen_typ thy algbr funcgr) ((fst o Term.strip_type) ty) |
|
310 |
(*##>> exprgen_typ thy algbr funcgr ((snd o Term.strip_type) ty)*) |
|
311 |
#>> (fn ((c, iss), tys) => IConst (c, (iss, tys))) |
|
312 |
and exprgen_app_default thy algbr funcgr (c_ty, ts) = |
|
313 |
exprgen_const thy algbr funcgr c_ty |
|
314 |
##>> fold_map (exprgen_term thy algbr funcgr) ts |
|
315 |
#>> (fn (t, ts) => t `$$ ts) |
|
316 |
and select_appgen thy algbr funcgr ((c, ty), ts) = |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
317 |
case Symtab.lookup (Appgens.get thy) c |
24219 | 318 |
of SOME (i, (appgen, _)) => |
319 |
if length ts < i then |
|
320 |
let |
|
321 |
val k = length ts; |
|
322 |
val tys = (curry Library.take (i - k) o curry Library.drop k o fst o strip_type) ty; |
|
323 |
val ctxt = (fold o fold_aterms) |
|
324 |
(fn Free (v, _) => Name.declare v | _ => I) ts Name.context; |
|
325 |
val vs = Name.names ctxt "a" tys; |
|
326 |
in |
|
24585 | 327 |
fold_map (exprgen_typ thy algbr funcgr) tys |
328 |
##>> appgen thy algbr funcgr ((c, ty), ts @ map Free vs) |
|
329 |
#>> (fn (tys, t) => map2 (fn (v, _) => pair v) vs tys `|--> t) |
|
24219 | 330 |
end |
331 |
else if length ts > i then |
|
24585 | 332 |
appgen thy algbr funcgr ((c, ty), Library.take (i, ts)) |
333 |
##>> fold_map (exprgen_term thy algbr funcgr) (Library.drop (i, ts)) |
|
334 |
#>> (fn (t, ts) => t `$$ ts) |
|
24219 | 335 |
else |
24585 | 336 |
appgen thy algbr funcgr ((c, ty), ts) |
24219 | 337 |
| NONE => |
24585 | 338 |
exprgen_app_default thy algbr funcgr ((c, ty), ts); |
24219 | 339 |
|
340 |
||
341 |
(* entrance points into translation kernel *) |
|
342 |
||
343 |
fun ensure_def_const' thy algbr funcgr c trns = |
|
344 |
ensure_def_const thy algbr funcgr c trns |
|
345 |
handle CONSTRAIN ((c, ty), ty_decl) => error ( |
|
346 |
"Constant " ^ c ^ " with most general type\n" |
|
347 |
^ CodeUnit.string_of_typ thy ty |
|
348 |
^ "\noccurs with type\n" |
|
349 |
^ CodeUnit.string_of_typ thy ty_decl); |
|
350 |
||
351 |
fun perhaps_def_const thy algbr funcgr c trns = |
|
352 |
case try (ensure_def_const thy algbr funcgr c) trns |
|
353 |
of SOME (c, trns) => (SOME c, trns) |
|
354 |
| NONE => (NONE, trns); |
|
355 |
||
356 |
fun exprgen_term' thy algbr funcgr t trns = |
|
357 |
exprgen_term thy algbr funcgr t trns |
|
358 |
handle CONSTRAIN ((c, ty), ty_decl) => error ("In term " ^ (quote o Sign.string_of_term thy) t |
|
359 |
^ ",\nconstant " ^ c ^ " with most general type\n" |
|
360 |
^ CodeUnit.string_of_typ thy ty |
|
361 |
^ "\noccurs with type\n" |
|
362 |
^ CodeUnit.string_of_typ thy ty_decl); |
|
363 |
||
364 |
||
365 |
(* parametrized application generators, for instantiation in object logic *) |
|
366 |
(* (axiomatic extensions of translation kernel) *) |
|
367 |
||
368 |
fun appgen_case dest_case_expr thy algbr funcgr (app as (c_ty, ts)) = |
|
369 |
let |
|
370 |
val SOME ([], ((st, sty), ds)) = dest_case_expr thy (list_comb (Const c_ty, ts)); |
|
371 |
fun clause_gen (dt, bt) = |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
372 |
exprgen_term thy algbr funcgr |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
373 |
(map_aterms (fn Const (c_ty as (c, ty)) => Const |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
374 |
(Class.unoverload_const thy c_ty, ty) | t => t) dt) |
24219 | 375 |
##>> exprgen_term thy algbr funcgr bt; |
376 |
in |
|
377 |
exprgen_term thy algbr funcgr st |
|
378 |
##>> exprgen_typ thy algbr funcgr sty |
|
379 |
##>> fold_map clause_gen ds |
|
24585 | 380 |
##>> exprgen_app_default thy algbr funcgr app |
24219 | 381 |
#>> (fn (((se, sty), ds), t0) => ICase (((se, sty), ds), t0)) |
382 |
end; |
|
383 |
||
384 |
fun appgen_let thy algbr funcgr (app as (_, [st, ct])) = |
|
385 |
exprgen_term thy algbr funcgr ct |
|
386 |
##>> exprgen_term thy algbr funcgr st |
|
24585 | 387 |
##>> exprgen_app_default thy algbr funcgr app |
24219 | 388 |
#>> (fn (((v, ty) `|-> be, se), t0) => |
389 |
ICase (CodeThingol.collapse_let (((v, ty), se), be), t0) |
|
390 |
| (_, t0) => t0); |
|
391 |
||
392 |
fun appgen_if thy algbr funcgr (app as (_, [tb, tt, tf])) = |
|
393 |
exprgen_term thy algbr funcgr tb |
|
394 |
##>> exprgen_typ thy algbr funcgr (Type ("bool", [])) |
|
395 |
##>> exprgen_term thy algbr funcgr (Const ("True", Type ("bool", []))) |
|
396 |
##>> exprgen_term thy algbr funcgr tt |
|
397 |
##>> exprgen_term thy algbr funcgr (Const ("False", Type ("bool", []))) |
|
398 |
##>> exprgen_term thy algbr funcgr tf |
|
24585 | 399 |
##>> exprgen_app_default thy algbr funcgr app |
24219 | 400 |
#>> (fn ((((((tb, B), T), tt), F), tf), t0) => ICase (((tb, B), [(T, tt), (F, tf)]), t0)); |
401 |
||
402 |
fun add_appconst (c, appgen) thy = |
|
403 |
let |
|
404 |
val i = (length o fst o strip_type o Sign.the_const_type thy) c; |
|
405 |
val _ = Program.change thy (K CodeThingol.empty_code); |
|
406 |
in |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
407 |
Appgens.map (Symtab.update (c, (i, (appgen, stamp ())))) thy |
24219 | 408 |
end; |
409 |
||
410 |
||
411 |
(** code generation interfaces **) |
|
412 |
||
413 |
(* generic generation combinators *) |
|
414 |
||
415 |
fun generate thy funcgr gen it = |
|
416 |
let |
|
417 |
val naming = NameSpace.qualified_names NameSpace.default_naming; |
|
418 |
val consttab = Consts.empty |
|
24770
695a8e087b9f
Sign.add_consts_authentic: tags (Markup.property list);
wenzelm
parents:
24662
diff
changeset
|
419 |
|> fold (fn c => Consts.declare true naming [] (c, CodeFuncgr.typ funcgr c)) |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
420 |
(CodeFuncgr.all funcgr); |
24219 | 421 |
val algbr = (Code.operational_algebra thy, consttab); |
422 |
in |
|
423 |
Program.change_yield thy |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
424 |
(CodeThingol.start_transact (gen thy algbr funcgr it)) |
24283 | 425 |
|> fst |
24219 | 426 |
end; |
427 |
||
24436 | 428 |
fun code thy permissive cs seris = |
429 |
let |
|
430 |
val code = Program.get thy; |
|
431 |
val seris' = map (fn (((target, module), file), args) => |
|
432 |
CodeTarget.get_serializer thy target permissive module file args |
|
24621 | 433 |
CodeName.labelled_name (K false) cs) seris; |
24436 | 434 |
in (map (fn f => f code) seris' : unit list; ()) end; |
435 |
||
24283 | 436 |
fun raw_eval f thy g = |
24250 | 437 |
let |
438 |
val value_name = "Isabelle_Eval.EVAL.EVAL"; |
|
439 |
fun ensure_eval thy algbr funcgr t = |
|
440 |
let |
|
24381 | 441 |
val ty = fastype_of t; |
24436 | 442 |
val vs = fold_term_types (K (fold_atyps (insert (eq_fst op =) |
443 |
o dest_TFree))) t []; |
|
24250 | 444 |
val defgen_eval = |
24381 | 445 |
fold_map (exprgen_tyvar_sort thy algbr funcgr) vs |
446 |
##>> exprgen_typ thy algbr funcgr ty |
|
447 |
##>> exprgen_term' thy algbr funcgr t |
|
24585 | 448 |
#>> (fn ((vs, ty), t) => CodeThingol.Fun ((vs, ty), [(([], t), Drule.dummy_thm)])); |
24283 | 449 |
fun result (dep, code) = |
450 |
let |
|
24585 | 451 |
val CodeThingol.Fun ((vs, ty), [(([], t), _)]) = Graph.get_node code value_name; |
24283 | 452 |
val deps = Graph.imm_succs code value_name; |
453 |
val code' = Graph.del_nodes [value_name] code; |
|
454 |
val code'' = CodeThingol.project_code false [] (SOME deps) code'; |
|
24381 | 455 |
in ((code'', ((vs, ty), t), deps), (dep, code')) end; |
24250 | 456 |
in |
24811 | 457 |
ensure_def thy defgen_eval value_name |
24283 | 458 |
#> result |
24250 | 459 |
end; |
24283 | 460 |
fun h funcgr ct = |
24219 | 461 |
let |
24381 | 462 |
val (code, vs_ty_t, deps) = generate thy funcgr ensure_eval (Thm.term_of ct); |
463 |
in g code vs_ty_t deps ct end; |
|
24283 | 464 |
in f thy h end; |
24219 | 465 |
|
24283 | 466 |
fun eval_conv thy = raw_eval CodeFuncgr.eval_conv thy; |
467 |
fun eval_term thy = raw_eval CodeFuncgr.eval_term thy; |
|
24219 | 468 |
|
24621 | 469 |
fun eval_invoke thy = CodeTarget.eval_invoke thy CodeName.labelled_name (K false); |
470 |
||
24585 | 471 |
val satisfies_ref : (unit -> bool) option ref = ref NONE; |
24219 | 472 |
|
24283 | 473 |
fun satisfies thy ct witnesses = |
474 |
let |
|
24381 | 475 |
fun evl code ((vs, ty), t) deps ct = |
24662
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24621
diff
changeset
|
476 |
eval_invoke thy ("CodePackage.satisfies_ref", satisfies_ref) |
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24621
diff
changeset
|
477 |
code (t, ty) witnesses; |
24283 | 478 |
in eval_term thy evl ct end; |
24219 | 479 |
|
480 |
fun filter_generatable thy consts = |
|
481 |
let |
|
24283 | 482 |
val (consts', funcgr) = CodeFuncgr.make_consts thy consts; |
483 |
val consts'' = generate thy funcgr (fold_map ooo perhaps_def_const) consts'; |
|
24219 | 484 |
val consts''' = map_filter (fn (const, SOME _) => SOME const | (_, NONE) => NONE) |
485 |
(consts' ~~ consts''); |
|
486 |
in consts''' end; |
|
487 |
||
24436 | 488 |
fun generate_const_exprs thy raw_cs = |
489 |
let |
|
490 |
val (perm1, cs) = CodeUnit.read_const_exprs thy |
|
491 |
(filter_generatable thy) raw_cs; |
|
492 |
val (perm2, cs') = case generate thy (CodeFuncgr.make thy cs) |
|
493 |
(fold_map ooo ensure_def_const') cs |
|
494 |
of [] => (true, NONE) |
|
495 |
| cs => (false, SOME cs); |
|
496 |
in (perm1 orelse perm2, cs') end; |
|
497 |
||
498 |
||
499 |
(** code properties **) |
|
500 |
||
501 |
fun mk_codeprops thy all_cs sel_cs = |
|
502 |
let |
|
503 |
fun select (thmref, thm) = case try (Drule.unvarify o Drule.zero_var_indexes) thm |
|
504 |
of NONE => NONE |
|
505 |
| SOME thm => let |
|
506 |
val t = (ObjectLogic.drop_judgment thy o Thm.prop_of) thm; |
|
507 |
val cs = fold_aterms (fn Const (c, ty) => |
|
508 |
cons (Class.unoverload_const thy (c, ty)) | _ => I) t []; |
|
509 |
in if exists (member (op =) sel_cs) cs |
|
510 |
andalso forall (member (op =) all_cs) cs |
|
511 |
then SOME (thmref, thm) else NONE end; |
|
512 |
fun mk_codeprop (thmref, thm) = |
|
513 |
let |
|
514 |
val t = ObjectLogic.drop_judgment thy (Thm.prop_of thm); |
|
515 |
val ty_judg = fastype_of t; |
|
516 |
val tfrees1 = fold_aterms (fn Const (c, ty) => |
|
517 |
Term.add_tfreesT ty | _ => I) t []; |
|
518 |
val vars = Term.add_frees t []; |
|
519 |
val tfrees2 = fold (Term.add_tfreesT o snd) vars []; |
|
520 |
val tfrees' = subtract (op =) tfrees2 tfrees1 |> map TFree; |
|
521 |
val ty = map Term.itselfT tfrees' @ map snd vars ---> ty_judg; |
|
522 |
val tfree_vars = map Logic.mk_type tfrees'; |
|
523 |
val c = PureThy.string_of_thmref thmref |
|
524 |
|> NameSpace.explode |
|
525 |
|> (fn [x] => [x] | (x::xs) => xs) |
|
526 |
|> space_implode "_" |
|
527 |
val propdef = (((c, ty), tfree_vars @ map Free vars), t); |
|
528 |
in if c = "" then NONE else SOME (thmref, propdef) end; |
|
529 |
in |
|
530 |
PureThy.thms_containing thy ([], []) |
|
531 |
|> maps PureThy.selections |
|
532 |
|> map_filter select |
|
533 |
|> map_filter mk_codeprop |
|
534 |
end; |
|
535 |
||
536 |
fun add_codeprops all_cs sel_cs thy = |
|
537 |
let |
|
538 |
val codeprops = mk_codeprops thy all_cs sel_cs; |
|
539 |
fun lift_name_yield f x = (Name.context, x) |> f ||> snd; |
|
540 |
fun add (thmref, (((raw_c, ty), ts), t)) (names, thy) = |
|
541 |
let |
|
542 |
val _ = warning ("Adding theorem " ^ quote (PureThy.string_of_thmref thmref) |
|
543 |
^ " as code property " ^ quote raw_c); |
|
544 |
val ([raw_c'], names') = Name.variants [raw_c] names; |
|
545 |
in |
|
546 |
thy |
|
547 |
|> PureThy.simple_def ("", []) (((raw_c', ty, Syntax.NoSyn), ts), t) |
|
548 |
||> pair names' |
|
549 |
end; |
|
550 |
in |
|
551 |
thy |
|
552 |
|> Sign.sticky_prefix "codeprop" |
|
553 |
|> lift_name_yield (fold_map add codeprops) |
|
554 |
||> Sign.restore_naming thy |
|
24621 | 555 |
|-> (fn c_thms => fold (Code.add_func o snd) c_thms #> pair c_thms) |
24436 | 556 |
end; |
557 |
||
558 |
||
24219 | 559 |
|
560 |
(** toplevel interface and setup **) |
|
561 |
||
562 |
local |
|
563 |
||
564 |
structure P = OuterParse |
|
565 |
and K = OuterKeyword |
|
566 |
||
24436 | 567 |
fun code_cmd raw_cs seris thy = |
24219 | 568 |
let |
24436 | 569 |
val (permissive, cs) = generate_const_exprs thy raw_cs; |
570 |
val _ = code thy permissive cs seris; |
|
571 |
in () end; |
|
24219 | 572 |
|
573 |
fun code_thms_cmd thy = |
|
24283 | 574 |
code_thms thy o snd o CodeUnit.read_const_exprs thy (fst o CodeFuncgr.make_consts thy); |
24219 | 575 |
|
576 |
fun code_deps_cmd thy = |
|
24283 | 577 |
code_deps thy o snd o CodeUnit.read_const_exprs thy (fst o CodeFuncgr.make_consts thy); |
24219 | 578 |
|
24436 | 579 |
fun code_props_cmd raw_cs seris thy = |
580 |
let |
|
581 |
val (_, all_cs) = generate_const_exprs thy ["*"]; |
|
582 |
val (permissive, cs) = generate_const_exprs thy raw_cs; |
|
583 |
val (c_thms, thy') = add_codeprops (map (the o CodeName.const_rev thy) (these all_cs)) |
|
584 |
(map (the o CodeName.const_rev thy) (these cs)) thy; |
|
585 |
val prop_cs = (filter_generatable thy' o map fst) c_thms; |
|
586 |
val _ = if null seris then [] else generate thy' (CodeFuncgr.make thy' prop_cs) |
|
587 |
(fold_map ooo ensure_def_const') prop_cs; |
|
588 |
val _ = if null seris then () else code thy' permissive |
|
589 |
(SOME (map (CodeName.const thy') prop_cs)) seris; |
|
590 |
in thy' end; |
|
591 |
||
24250 | 592 |
val (inK, module_nameK, fileK) = ("in", "module_name", "file"); |
24219 | 593 |
|
24436 | 594 |
fun code_exprP cmd = |
24219 | 595 |
(Scan.repeat P.term |
596 |
-- Scan.repeat (P.$$$ inK |-- P.name |
|
24250 | 597 |
-- Scan.option (P.$$$ module_nameK |-- P.name) |
24219 | 598 |
-- Scan.option (P.$$$ fileK |-- P.name) |
599 |
-- Scan.optional (P.$$$ "(" |-- P.arguments --| P.$$$ ")") [] |
|
24436 | 600 |
) >> (fn (raw_cs, seris) => cmd raw_cs seris)); |
24219 | 601 |
|
24250 | 602 |
val _ = OuterSyntax.add_keywords [inK, module_nameK, fileK]; |
24219 | 603 |
|
24436 | 604 |
val (codeK, code_thmsK, code_depsK, code_propsK) = |
605 |
("export_code", "code_thms", "code_deps", "code_props"); |
|
24219 | 606 |
|
607 |
in |
|
608 |
||
609 |
val codeP = |
|
610 |
OuterSyntax.improper_command codeK "generate executable code for constants" |
|
24436 | 611 |
K.diag (P.!!! (code_exprP code_cmd) >> (fn f => Toplevel.keep (f o Toplevel.theory_of))); |
24219 | 612 |
|
613 |
fun codegen_command thy cmd = |
|
24436 | 614 |
case Scan.read OuterLex.stopper (P.!!! (code_exprP code_cmd)) ((filter OuterLex.is_proper o OuterSyntax.scan) cmd) |
24219 | 615 |
of SOME f => (writeln "Now generating code..."; f thy) |
616 |
| NONE => error ("Bad directive " ^ quote cmd); |
|
617 |
||
618 |
val code_thmsP = |
|
619 |
OuterSyntax.improper_command code_thmsK "print system of defining equations for code" OuterKeyword.diag |
|
620 |
(Scan.repeat P.term |
|
621 |
>> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory |
|
622 |
o Toplevel.keep ((fn thy => code_thms_cmd thy cs) o Toplevel.theory_of))); |
|
623 |
||
624 |
val code_depsP = |
|
625 |
OuterSyntax.improper_command code_depsK "visualize dependencies of defining equations for code" OuterKeyword.diag |
|
626 |
(Scan.repeat P.term |
|
627 |
>> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory |
|
628 |
o Toplevel.keep ((fn thy => code_deps_cmd thy cs) o Toplevel.theory_of))); |
|
629 |
||
24436 | 630 |
val code_propsP = |
631 |
OuterSyntax.command code_propsK "generate characteristic properties for executable constants" |
|
632 |
K.thy_decl (P.!!! (code_exprP code_props_cmd) >> Toplevel.theory); |
|
24219 | 633 |
|
24436 | 634 |
val _ = OuterSyntax.add_parsers [codeP, code_thmsP, code_depsP, code_propsP]; |
24219 | 635 |
|
24436 | 636 |
end; (*local*) |
637 |
||
638 |
end; (*struct*) |