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