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