author | wenzelm |
Sat, 15 Mar 2008 18:08:54 +0100 | |
changeset 26285 | 3905e52a1a0e |
parent 26113 | ba5909699cc3 |
child 26336 | a0e2b706ce73 |
permissions | -rw-r--r-- |
24219 | 1 |
(* Title: Tools/code/code_package.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
24918 | 5 |
Code generator interfaces and Isar setup. |
24219 | 6 |
*) |
7 |
||
8 |
signature CODE_PACKAGE = |
|
9 |
sig |
|
26011 | 10 |
val evaluate_conv: theory |
24381 | 11 |
-> (CodeThingol.code -> CodeThingol.typscheme * CodeThingol.iterm |
12 |
-> string list -> cterm -> thm) |
|
24283 | 13 |
-> cterm -> thm; |
26011 | 14 |
val evaluate_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; |
26011 | 18 |
val eval_conv: string * (unit -> thm) option ref |
19 |
-> theory -> cterm -> string list -> thm; |
|
20 |
val eval_term: string * (unit -> 'a) option ref |
|
21 |
-> theory -> term -> string list -> 'a; |
|
22 |
val satisfies: theory -> term -> string list -> bool; |
|
24585 | 23 |
val satisfies_ref: (unit -> bool) option ref; |
25611 | 24 |
val codegen_shell_command: string (*theory name*) -> string (*cg expr*) -> unit; |
24219 | 25 |
end; |
26 |
||
27 |
structure CodePackage : CODE_PACKAGE = |
|
28 |
struct |
|
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) |
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25485
diff
changeset
|
40 |
|> Graph.map_nodes ((apsnd o map) (AxClass.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 |
|
24918 | 60 |
(** code generation interfaces **) |
61 |
||
62 |
(* code data *) |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24835
diff
changeset
|
63 |
|
24219 | 64 |
structure Program = CodeDataFun |
65 |
( |
|
66 |
type T = CodeThingol.code; |
|
67 |
val empty = CodeThingol.empty_code; |
|
68 |
fun merge _ = CodeThingol.merge_code; |
|
69 |
fun purge _ NONE _ = CodeThingol.empty_code |
|
70 |
| purge NONE _ _ = CodeThingol.empty_code |
|
71 |
| purge (SOME thy) (SOME cs) code = |
|
72 |
let |
|
73 |
val cs_exisiting = |
|
74 |
map_filter (CodeName.const_rev thy) (Graph.keys code); |
|
75 |
val dels = (Graph.all_preds code |
|
76 |
o map (CodeName.const thy) |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
77 |
o filter (member (op =) cs_exisiting) |
24219 | 78 |
) cs; |
79 |
in Graph.del_nodes dels code end; |
|
80 |
); |
|
81 |
||
24918 | 82 |
(* generic generation combinators *) |
24811 | 83 |
|
24918 | 84 |
val ensure_const = CodeThingol.ensure_const; |
24219 | 85 |
|
24918 | 86 |
fun perhaps_const thy algbr funcgr c trns = |
87 |
case try (CodeThingol.ensure_const thy algbr funcgr c) trns |
|
24219 | 88 |
of SOME (c, trns) => (SOME c, trns) |
89 |
| NONE => (NONE, trns); |
|
90 |
||
25969 | 91 |
fun generate thy funcgr f x = |
92 |
Program.change_yield thy (CodeThingol.transact thy funcgr |
|
93 |
(fn thy => fn funcgr => fn algbr => f thy funcgr algbr x)); |
|
24219 | 94 |
|
26113 | 95 |
(* export_code functionality *) |
96 |
||
24436 | 97 |
fun code thy permissive cs seris = |
98 |
let |
|
99 |
val code = Program.get thy; |
|
100 |
val seris' = map (fn (((target, module), file), args) => |
|
24918 | 101 |
CodeTarget.get_serializer thy target permissive module file args cs) seris; |
24436 | 102 |
in (map (fn f => f code) seris' : unit list; ()) end; |
103 |
||
26113 | 104 |
(* evaluation machinery *) |
105 |
||
26011 | 106 |
fun evaluate eval_kind term_of thy eval = eval_kind thy (fn funcgr => fn ct => |
24250 | 107 |
let |
26011 | 108 |
val ((code, (vs_ty_t, deps)), _) = generate thy funcgr |
109 |
CodeThingol.ensure_value (term_of ct) |
|
110 |
in eval code vs_ty_t deps ct end); |
|
111 |
||
112 |
fun evaluate_conv thy = evaluate CodeFuncgr.eval_conv Thm.term_of thy; |
|
113 |
fun evaluate_term thy = evaluate CodeFuncgr.eval_term I thy; |
|
114 |
||
115 |
fun eval_ml reff args thy code ((vs, ty), t) deps _ = |
|
116 |
CodeTarget.eval thy reff code (t, ty) args; |
|
24219 | 117 |
|
26011 | 118 |
fun eval evaluate term_of reff thy ct args = |
119 |
let |
|
120 |
val _ = if null (term_frees (term_of ct)) then () else error ("Term " |
|
121 |
^ quote (Sign.string_of_term thy (term_of ct)) |
|
122 |
^ " to be evaluated containts free variables"); |
|
123 |
in evaluate thy (eval_ml reff args thy) ct end; |
|
124 |
||
125 |
fun eval_conv reff = eval evaluate_conv Thm.term_of reff; |
|
126 |
fun eval_term reff = eval evaluate_term I reff; |
|
24219 | 127 |
|
24585 | 128 |
val satisfies_ref : (unit -> bool) option ref = ref NONE; |
24219 | 129 |
|
26011 | 130 |
val satisfies = eval_term ("CodePackage.satisfies_ref", satisfies_ref); |
24219 | 131 |
|
26113 | 132 |
(* code antiquotation *) |
133 |
||
134 |
fun code_antiq (ctxt, args) = |
|
135 |
let |
|
136 |
val thy = Context.theory_of ctxt; |
|
137 |
val (ts, (ctxt', args')) = Scan.repeat1 Args.term (ctxt, args); |
|
138 |
val cs = map (CodeUnit.check_const thy) ts; |
|
139 |
val (cs', code') = generate thy (CodeFuncgr.make thy cs) |
|
140 |
(fold_map ooo ensure_const) cs; |
|
141 |
val code'' = CodeTarget.sml_of thy cs' code' ^ " ()"; |
|
142 |
in (("codevals", code''), (ctxt', args')) end; |
|
143 |
||
144 |
||
145 |
(* const expressions *) |
|
146 |
||
24219 | 147 |
fun filter_generatable thy consts = |
148 |
let |
|
24283 | 149 |
val (consts', funcgr) = CodeFuncgr.make_consts thy consts; |
24918 | 150 |
val (consts'', _) = generate thy funcgr (fold_map ooo perhaps_const) consts'; |
24219 | 151 |
val consts''' = map_filter (fn (const, SOME _) => SOME const | (_, NONE) => NONE) |
152 |
(consts' ~~ consts''); |
|
153 |
in consts''' end; |
|
154 |
||
24436 | 155 |
fun generate_const_exprs thy raw_cs = |
156 |
let |
|
157 |
val (perm1, cs) = CodeUnit.read_const_exprs thy |
|
158 |
(filter_generatable thy) raw_cs; |
|
159 |
val (perm2, cs') = case generate thy (CodeFuncgr.make thy cs) |
|
24918 | 160 |
(fold_map ooo ensure_const) cs |
161 |
of ([], _) => (true, NONE) |
|
162 |
| (cs, _) => (false, SOME cs); |
|
24436 | 163 |
in (perm1 orelse perm2, cs') end; |
164 |
||
165 |
||
166 |
(** code properties **) |
|
167 |
||
168 |
fun mk_codeprops thy all_cs sel_cs = |
|
169 |
let |
|
24976
821628d16552
moved Drule.unvarify to Thm.unvarify (cf. more_thm.ML);
wenzelm
parents:
24971
diff
changeset
|
170 |
fun select (thmref, thm) = case try (Thm.unvarify o Drule.zero_var_indexes) thm |
24436 | 171 |
of NONE => NONE |
172 |
| SOME thm => let |
|
173 |
val t = (ObjectLogic.drop_judgment thy o Thm.prop_of) thm; |
|
174 |
val cs = fold_aterms (fn Const (c, ty) => |
|
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25485
diff
changeset
|
175 |
cons (AxClass.unoverload_const thy (c, ty)) | _ => I) t []; |
24436 | 176 |
in if exists (member (op =) sel_cs) cs |
177 |
andalso forall (member (op =) all_cs) cs |
|
178 |
then SOME (thmref, thm) else NONE end; |
|
179 |
fun mk_codeprop (thmref, thm) = |
|
180 |
let |
|
181 |
val t = ObjectLogic.drop_judgment thy (Thm.prop_of thm); |
|
182 |
val ty_judg = fastype_of t; |
|
183 |
val tfrees1 = fold_aterms (fn Const (c, ty) => |
|
184 |
Term.add_tfreesT ty | _ => I) t []; |
|
185 |
val vars = Term.add_frees t []; |
|
186 |
val tfrees2 = fold (Term.add_tfreesT o snd) vars []; |
|
187 |
val tfrees' = subtract (op =) tfrees2 tfrees1 |> map TFree; |
|
188 |
val ty = map Term.itselfT tfrees' @ map snd vars ---> ty_judg; |
|
189 |
val tfree_vars = map Logic.mk_type tfrees'; |
|
190 |
val c = PureThy.string_of_thmref thmref |
|
191 |
|> NameSpace.explode |
|
192 |
|> (fn [x] => [x] | (x::xs) => xs) |
|
193 |
|> space_implode "_" |
|
194 |
val propdef = (((c, ty), tfree_vars @ map Free vars), t); |
|
195 |
in if c = "" then NONE else SOME (thmref, propdef) end; |
|
196 |
in |
|
26285 | 197 |
Facts.dest (PureThy.all_facts_of thy) |
24436 | 198 |
|> maps PureThy.selections |
199 |
|> map_filter select |
|
200 |
|> map_filter mk_codeprop |
|
201 |
end; |
|
202 |
||
203 |
fun add_codeprops all_cs sel_cs thy = |
|
204 |
let |
|
205 |
val codeprops = mk_codeprops thy all_cs sel_cs; |
|
206 |
fun lift_name_yield f x = (Name.context, x) |> f ||> snd; |
|
207 |
fun add (thmref, (((raw_c, ty), ts), t)) (names, thy) = |
|
208 |
let |
|
209 |
val _ = warning ("Adding theorem " ^ quote (PureThy.string_of_thmref thmref) |
|
210 |
^ " as code property " ^ quote raw_c); |
|
211 |
val ([raw_c'], names') = Name.variants [raw_c] names; |
|
24971
4d006b03aa4a
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24918
diff
changeset
|
212 |
val (const as Const (c, _), thy') = thy |> Sign.declare_const [] (raw_c', ty, NoSyn); |
4d006b03aa4a
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24918
diff
changeset
|
213 |
val eq = Logic.mk_equals (list_comb (const, ts), t); |
4d006b03aa4a
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24918
diff
changeset
|
214 |
val ([def], thy'') = thy' |> PureThy.add_defs_i false [((Thm.def_name raw_c', eq), [])]; |
4d006b03aa4a
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24918
diff
changeset
|
215 |
in ((c, def), (names', thy'')) end; |
24436 | 216 |
in |
217 |
thy |
|
218 |
|> Sign.sticky_prefix "codeprop" |
|
219 |
|> lift_name_yield (fold_map add codeprops) |
|
220 |
||> Sign.restore_naming thy |
|
24621 | 221 |
|-> (fn c_thms => fold (Code.add_func o snd) c_thms #> pair c_thms) |
24436 | 222 |
end; |
223 |
||
224 |
||
24918 | 225 |
(** interfaces and Isar setup **) |
24219 | 226 |
|
227 |
local |
|
228 |
||
229 |
structure P = OuterParse |
|
230 |
and K = OuterKeyword |
|
231 |
||
24436 | 232 |
fun code_cmd raw_cs seris thy = |
24219 | 233 |
let |
24436 | 234 |
val (permissive, cs) = generate_const_exprs thy raw_cs; |
235 |
val _ = code thy permissive cs seris; |
|
236 |
in () end; |
|
24219 | 237 |
|
238 |
fun code_thms_cmd thy = |
|
24283 | 239 |
code_thms thy o snd o CodeUnit.read_const_exprs thy (fst o CodeFuncgr.make_consts thy); |
24219 | 240 |
|
241 |
fun code_deps_cmd thy = |
|
24283 | 242 |
code_deps thy o snd o CodeUnit.read_const_exprs thy (fst o CodeFuncgr.make_consts thy); |
24219 | 243 |
|
24436 | 244 |
fun code_props_cmd raw_cs seris thy = |
245 |
let |
|
246 |
val (_, all_cs) = generate_const_exprs thy ["*"]; |
|
247 |
val (permissive, cs) = generate_const_exprs thy raw_cs; |
|
248 |
val (c_thms, thy') = add_codeprops (map (the o CodeName.const_rev thy) (these all_cs)) |
|
249 |
(map (the o CodeName.const_rev thy) (these cs)) thy; |
|
250 |
val prop_cs = (filter_generatable thy' o map fst) c_thms; |
|
24918 | 251 |
val _ = if null seris then () else (generate thy' (CodeFuncgr.make thy' prop_cs) |
252 |
(fold_map ooo ensure_const) prop_cs; ()); |
|
24436 | 253 |
val _ = if null seris then () else code thy' permissive |
254 |
(SOME (map (CodeName.const thy') prop_cs)) seris; |
|
255 |
in thy' end; |
|
256 |
||
24250 | 257 |
val (inK, module_nameK, fileK) = ("in", "module_name", "file"); |
24219 | 258 |
|
24436 | 259 |
fun code_exprP cmd = |
24219 | 260 |
(Scan.repeat P.term |
261 |
-- Scan.repeat (P.$$$ inK |-- P.name |
|
24250 | 262 |
-- Scan.option (P.$$$ module_nameK |-- P.name) |
24219 | 263 |
-- Scan.option (P.$$$ fileK |-- P.name) |
264 |
-- Scan.optional (P.$$$ "(" |-- P.arguments --| P.$$$ ")") [] |
|
24436 | 265 |
) >> (fn (raw_cs, seris) => cmd raw_cs seris)); |
24219 | 266 |
|
24867 | 267 |
val _ = OuterSyntax.keywords [inK, module_nameK, fileK]; |
24219 | 268 |
|
24436 | 269 |
val (codeK, code_thmsK, code_depsK, code_propsK) = |
270 |
("export_code", "code_thms", "code_deps", "code_props"); |
|
24219 | 271 |
|
272 |
in |
|
273 |
||
24867 | 274 |
val _ = |
25110 | 275 |
OuterSyntax.command codeK "generate executable code for constants" |
24436 | 276 |
K.diag (P.!!! (code_exprP code_cmd) >> (fn f => Toplevel.keep (f o Toplevel.theory_of))); |
24219 | 277 |
|
25611 | 278 |
fun codegen_shell_command thyname cmd = Isar.toplevel (fn _ => |
279 |
(use_thy thyname; case Scan.read OuterLex.stopper (P.!!! (code_exprP code_cmd)) ((filter OuterLex.is_proper o OuterSyntax.scan) cmd) |
|
280 |
of SOME f => (writeln "Now generating code..."; f (theory thyname)) |
|
281 |
| NONE => error ("Bad directive " ^ quote cmd))) |
|
282 |
handle TOPLEVEL_ERROR => OS.Process.exit OS.Process.failure; |
|
24219 | 283 |
|
24867 | 284 |
val _ = |
24219 | 285 |
OuterSyntax.improper_command code_thmsK "print system of defining equations for code" OuterKeyword.diag |
286 |
(Scan.repeat P.term |
|
287 |
>> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory |
|
288 |
o Toplevel.keep ((fn thy => code_thms_cmd thy cs) o Toplevel.theory_of))); |
|
289 |
||
24867 | 290 |
val _ = |
24219 | 291 |
OuterSyntax.improper_command code_depsK "visualize dependencies of defining equations for code" OuterKeyword.diag |
292 |
(Scan.repeat P.term |
|
293 |
>> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory |
|
294 |
o Toplevel.keep ((fn thy => code_deps_cmd thy cs) o Toplevel.theory_of))); |
|
295 |
||
24867 | 296 |
val _ = |
24436 | 297 |
OuterSyntax.command code_propsK "generate characteristic properties for executable constants" |
298 |
K.thy_decl (P.!!! (code_exprP code_props_cmd) >> Toplevel.theory); |
|
24219 | 299 |
|
26113 | 300 |
val _ = ML_Context.value_antiq "code" code_antiq; |
301 |
||
24436 | 302 |
end; (*local*) |
303 |
||
304 |
end; (*struct*) |