| author | wenzelm |
| Mon, 28 Jan 2008 22:27:19 +0100 | |
| changeset 25999 | f8bcd311d501 |
| parent 25969 | d3f8ab2726ed |
| child 26011 | d55224947082 |
| 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 |
|
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; |
| 25611 | 20 |
val codegen_shell_command: string (*theory name*) -> string (*cg expr*) -> unit; |
| 24219 | 21 |
end; |
22 |
||
23 |
structure CodePackage : CODE_PACKAGE = |
|
24 |
struct |
|
25 |
||
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24835
diff
changeset
|
26 |
(** code theorems **) |
| 24219 | 27 |
|
| 24283 | 28 |
fun code_depgr thy [] = CodeFuncgr.make thy [] |
| 24219 | 29 |
| code_depgr thy consts = |
30 |
let |
|
| 24283 | 31 |
val gr = CodeFuncgr.make thy consts; |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
32 |
val select = Graph.all_succs gr consts; |
| 24219 | 33 |
in |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
34 |
gr |
|
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
35 |
|> Graph.subgraph (member (op =) select) |
|
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25485
diff
changeset
|
36 |
|> Graph.map_nodes ((apsnd o map) (AxClass.overload thy)) |
| 24219 | 37 |
end; |
38 |
||
39 |
fun code_thms thy = |
|
40 |
Pretty.writeln o CodeFuncgr.pretty thy o code_depgr thy; |
|
41 |
||
42 |
fun code_deps thy consts = |
|
43 |
let |
|
44 |
val gr = code_depgr thy consts; |
|
45 |
fun mk_entry (const, (_, (_, parents))) = |
|
46 |
let |
|
47 |
val name = CodeUnit.string_of_const thy const; |
|
48 |
val nameparents = map (CodeUnit.string_of_const thy) parents; |
|
49 |
in { name = name, ID = name, dir = "", unfold = true,
|
|
50 |
path = "", parents = nameparents } |
|
51 |
end; |
|
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
52 |
val prgr = Graph.fold ((fn x => fn xs => xs @ [x]) o mk_entry) gr []; |
| 24219 | 53 |
in Present.display_graph prgr end; |
54 |
||
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24835
diff
changeset
|
55 |
|
| 24918 | 56 |
(** code generation interfaces **) |
57 |
||
58 |
(* code data *) |
|
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24835
diff
changeset
|
59 |
|
| 24219 | 60 |
structure Program = CodeDataFun |
61 |
( |
|
62 |
type T = CodeThingol.code; |
|
63 |
val empty = CodeThingol.empty_code; |
|
64 |
fun merge _ = CodeThingol.merge_code; |
|
65 |
fun purge _ NONE _ = CodeThingol.empty_code |
|
66 |
| purge NONE _ _ = CodeThingol.empty_code |
|
67 |
| purge (SOME thy) (SOME cs) code = |
|
68 |
let |
|
69 |
val cs_exisiting = |
|
70 |
map_filter (CodeName.const_rev thy) (Graph.keys code); |
|
71 |
val dels = (Graph.all_preds code |
|
72 |
o map (CodeName.const thy) |
|
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
73 |
o filter (member (op =) cs_exisiting) |
| 24219 | 74 |
) cs; |
75 |
in Graph.del_nodes dels code end; |
|
76 |
); |
|
77 |
||
| 24918 | 78 |
(* generic generation combinators *) |
| 24811 | 79 |
|
| 24918 | 80 |
val ensure_const = CodeThingol.ensure_const; |
| 24219 | 81 |
|
| 24918 | 82 |
fun perhaps_const thy algbr funcgr c trns = |
83 |
case try (CodeThingol.ensure_const thy algbr funcgr c) trns |
|
| 24219 | 84 |
of SOME (c, trns) => (SOME c, trns) |
85 |
| NONE => (NONE, trns); |
|
86 |
||
| 25969 | 87 |
fun generate thy funcgr f x = |
88 |
Program.change_yield thy (CodeThingol.transact thy funcgr |
|
89 |
(fn thy => fn funcgr => fn algbr => f thy funcgr algbr x)); |
|
| 24219 | 90 |
|
| 24436 | 91 |
fun code thy permissive cs seris = |
92 |
let |
|
93 |
val code = Program.get thy; |
|
94 |
val seris' = map (fn (((target, module), file), args) => |
|
| 24918 | 95 |
CodeTarget.get_serializer thy target permissive module file args cs) seris; |
| 24436 | 96 |
in (map (fn f => f code) seris' : unit list; ()) end; |
97 |
||
|
24835
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24811
diff
changeset
|
98 |
fun raw_eval evaluate term_of thy g = |
| 24250 | 99 |
let |
| 24918 | 100 |
fun result (_, code) = |
| 24250 | 101 |
let |
| 24918 | 102 |
val CodeThingol.Fun ((vs, ty), [(([], t), _)]) = |
103 |
Graph.get_node code CodeName.value_name; |
|
104 |
val deps = Graph.imm_succs code CodeName.value_name; |
|
105 |
val code' = Graph.del_nodes [CodeName.value_name] code; |
|
106 |
val code'' = CodeThingol.project_code false [] (SOME deps) code'; |
|
107 |
in ((code'', ((vs, ty), t), deps), code') end; |
|
| 24283 | 108 |
fun h funcgr ct = |
| 24219 | 109 |
let |
| 24918 | 110 |
val ((code, vs_ty_t, deps), _) = term_of ct |
111 |
|> generate thy funcgr CodeThingol.ensure_value |
|
112 |
|> result |
|
113 |
||> `(fn code' => Program.change thy (K code')); |
|
| 24381 | 114 |
in g code vs_ty_t deps ct end; |
|
24835
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24811
diff
changeset
|
115 |
in evaluate thy h end; |
| 24219 | 116 |
|
|
24835
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24811
diff
changeset
|
117 |
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
|
118 |
fun eval_term thy = raw_eval CodeFuncgr.eval_term I thy; |
| 24219 | 119 |
|
| 24585 | 120 |
val satisfies_ref : (unit -> bool) option ref = ref NONE; |
| 24219 | 121 |
|
|
24835
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24811
diff
changeset
|
122 |
fun satisfies thy t witnesses = |
| 24283 | 123 |
let |
| 25935 | 124 |
fun evl code ((vs, ty), t) deps _ = |
| 24918 | 125 |
CodeTarget.eval_invoke thy ("CodePackage.satisfies_ref", satisfies_ref)
|
|
24662
f79f6061525c
more precise treatment of free dictionary parameters for evaluation
haftmann
parents:
24621
diff
changeset
|
126 |
code (t, ty) witnesses; |
|
24835
8c26128f8997
clarified relationship of code generator conversions and evaluations
haftmann
parents:
24811
diff
changeset
|
127 |
in eval_term thy evl t end; |
| 24219 | 128 |
|
129 |
fun filter_generatable thy consts = |
|
130 |
let |
|
| 24283 | 131 |
val (consts', funcgr) = CodeFuncgr.make_consts thy consts; |
| 24918 | 132 |
val (consts'', _) = generate thy funcgr (fold_map ooo perhaps_const) consts'; |
| 24219 | 133 |
val consts''' = map_filter (fn (const, SOME _) => SOME const | (_, NONE) => NONE) |
134 |
(consts' ~~ consts''); |
|
135 |
in consts''' end; |
|
136 |
||
| 24436 | 137 |
fun generate_const_exprs thy raw_cs = |
138 |
let |
|
139 |
val (perm1, cs) = CodeUnit.read_const_exprs thy |
|
140 |
(filter_generatable thy) raw_cs; |
|
141 |
val (perm2, cs') = case generate thy (CodeFuncgr.make thy cs) |
|
| 24918 | 142 |
(fold_map ooo ensure_const) cs |
143 |
of ([], _) => (true, NONE) |
|
144 |
| (cs, _) => (false, SOME cs); |
|
| 24436 | 145 |
in (perm1 orelse perm2, cs') end; |
146 |
||
147 |
||
148 |
(** code properties **) |
|
149 |
||
150 |
fun mk_codeprops thy all_cs sel_cs = |
|
151 |
let |
|
|
24976
821628d16552
moved Drule.unvarify to Thm.unvarify (cf. more_thm.ML);
wenzelm
parents:
24971
diff
changeset
|
152 |
fun select (thmref, thm) = case try (Thm.unvarify o Drule.zero_var_indexes) thm |
| 24436 | 153 |
of NONE => NONE |
154 |
| SOME thm => let |
|
155 |
val t = (ObjectLogic.drop_judgment thy o Thm.prop_of) thm; |
|
156 |
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
|
157 |
cons (AxClass.unoverload_const thy (c, ty)) | _ => I) t []; |
| 24436 | 158 |
in if exists (member (op =) sel_cs) cs |
159 |
andalso forall (member (op =) all_cs) cs |
|
160 |
then SOME (thmref, thm) else NONE end; |
|
161 |
fun mk_codeprop (thmref, thm) = |
|
162 |
let |
|
163 |
val t = ObjectLogic.drop_judgment thy (Thm.prop_of thm); |
|
164 |
val ty_judg = fastype_of t; |
|
165 |
val tfrees1 = fold_aterms (fn Const (c, ty) => |
|
166 |
Term.add_tfreesT ty | _ => I) t []; |
|
167 |
val vars = Term.add_frees t []; |
|
168 |
val tfrees2 = fold (Term.add_tfreesT o snd) vars []; |
|
169 |
val tfrees' = subtract (op =) tfrees2 tfrees1 |> map TFree; |
|
170 |
val ty = map Term.itselfT tfrees' @ map snd vars ---> ty_judg; |
|
171 |
val tfree_vars = map Logic.mk_type tfrees'; |
|
172 |
val c = PureThy.string_of_thmref thmref |
|
173 |
|> NameSpace.explode |
|
174 |
|> (fn [x] => [x] | (x::xs) => xs) |
|
175 |
|> space_implode "_" |
|
176 |
val propdef = (((c, ty), tfree_vars @ map Free vars), t); |
|
177 |
in if c = "" then NONE else SOME (thmref, propdef) end; |
|
178 |
in |
|
179 |
PureThy.thms_containing thy ([], []) |
|
180 |
|> maps PureThy.selections |
|
181 |
|> map_filter select |
|
182 |
|> map_filter mk_codeprop |
|
183 |
end; |
|
184 |
||
185 |
fun add_codeprops all_cs sel_cs thy = |
|
186 |
let |
|
187 |
val codeprops = mk_codeprops thy all_cs sel_cs; |
|
188 |
fun lift_name_yield f x = (Name.context, x) |> f ||> snd; |
|
189 |
fun add (thmref, (((raw_c, ty), ts), t)) (names, thy) = |
|
190 |
let |
|
191 |
val _ = warning ("Adding theorem " ^ quote (PureThy.string_of_thmref thmref)
|
|
192 |
^ " as code property " ^ quote raw_c); |
|
193 |
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
|
194 |
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
|
195 |
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
|
196 |
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
|
197 |
in ((c, def), (names', thy'')) end; |
| 24436 | 198 |
in |
199 |
thy |
|
200 |
|> Sign.sticky_prefix "codeprop" |
|
201 |
|> lift_name_yield (fold_map add codeprops) |
|
202 |
||> Sign.restore_naming thy |
|
| 24621 | 203 |
|-> (fn c_thms => fold (Code.add_func o snd) c_thms #> pair c_thms) |
| 24436 | 204 |
end; |
205 |
||
206 |
||
| 24918 | 207 |
(** interfaces and Isar setup **) |
| 24219 | 208 |
|
209 |
local |
|
210 |
||
211 |
structure P = OuterParse |
|
212 |
and K = OuterKeyword |
|
213 |
||
| 24436 | 214 |
fun code_cmd raw_cs seris thy = |
| 24219 | 215 |
let |
| 24436 | 216 |
val (permissive, cs) = generate_const_exprs thy raw_cs; |
217 |
val _ = code thy permissive cs seris; |
|
218 |
in () end; |
|
| 24219 | 219 |
|
220 |
fun code_thms_cmd thy = |
|
| 24283 | 221 |
code_thms thy o snd o CodeUnit.read_const_exprs thy (fst o CodeFuncgr.make_consts thy); |
| 24219 | 222 |
|
223 |
fun code_deps_cmd thy = |
|
| 24283 | 224 |
code_deps thy o snd o CodeUnit.read_const_exprs thy (fst o CodeFuncgr.make_consts thy); |
| 24219 | 225 |
|
| 24436 | 226 |
fun code_props_cmd raw_cs seris thy = |
227 |
let |
|
228 |
val (_, all_cs) = generate_const_exprs thy ["*"]; |
|
229 |
val (permissive, cs) = generate_const_exprs thy raw_cs; |
|
230 |
val (c_thms, thy') = add_codeprops (map (the o CodeName.const_rev thy) (these all_cs)) |
|
231 |
(map (the o CodeName.const_rev thy) (these cs)) thy; |
|
232 |
val prop_cs = (filter_generatable thy' o map fst) c_thms; |
|
| 24918 | 233 |
val _ = if null seris then () else (generate thy' (CodeFuncgr.make thy' prop_cs) |
234 |
(fold_map ooo ensure_const) prop_cs; ()); |
|
| 24436 | 235 |
val _ = if null seris then () else code thy' permissive |
236 |
(SOME (map (CodeName.const thy') prop_cs)) seris; |
|
237 |
in thy' end; |
|
238 |
||
| 24250 | 239 |
val (inK, module_nameK, fileK) = ("in", "module_name", "file");
|
| 24219 | 240 |
|
| 24436 | 241 |
fun code_exprP cmd = |
| 24219 | 242 |
(Scan.repeat P.term |
243 |
-- Scan.repeat (P.$$$ inK |-- P.name |
|
| 24250 | 244 |
-- Scan.option (P.$$$ module_nameK |-- P.name) |
| 24219 | 245 |
-- Scan.option (P.$$$ fileK |-- P.name) |
246 |
-- Scan.optional (P.$$$ "(" |-- P.arguments --| P.$$$ ")") []
|
|
| 24436 | 247 |
) >> (fn (raw_cs, seris) => cmd raw_cs seris)); |
| 24219 | 248 |
|
| 24867 | 249 |
val _ = OuterSyntax.keywords [inK, module_nameK, fileK]; |
| 24219 | 250 |
|
| 24436 | 251 |
val (codeK, code_thmsK, code_depsK, code_propsK) = |
252 |
("export_code", "code_thms", "code_deps", "code_props");
|
|
| 24219 | 253 |
|
254 |
in |
|
255 |
||
| 24867 | 256 |
val _ = |
| 25110 | 257 |
OuterSyntax.command codeK "generate executable code for constants" |
| 24436 | 258 |
K.diag (P.!!! (code_exprP code_cmd) >> (fn f => Toplevel.keep (f o Toplevel.theory_of))); |
| 24219 | 259 |
|
| 25611 | 260 |
fun codegen_shell_command thyname cmd = Isar.toplevel (fn _ => |
261 |
(use_thy thyname; case Scan.read OuterLex.stopper (P.!!! (code_exprP code_cmd)) ((filter OuterLex.is_proper o OuterSyntax.scan) cmd) |
|
262 |
of SOME f => (writeln "Now generating code..."; f (theory thyname)) |
|
263 |
| NONE => error ("Bad directive " ^ quote cmd)))
|
|
264 |
handle TOPLEVEL_ERROR => OS.Process.exit OS.Process.failure; |
|
| 24219 | 265 |
|
| 24867 | 266 |
val _ = |
| 24219 | 267 |
OuterSyntax.improper_command code_thmsK "print system of defining equations for code" OuterKeyword.diag |
268 |
(Scan.repeat P.term |
|
269 |
>> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory |
|
270 |
o Toplevel.keep ((fn thy => code_thms_cmd thy cs) o Toplevel.theory_of))); |
|
271 |
||
| 24867 | 272 |
val _ = |
| 24219 | 273 |
OuterSyntax.improper_command code_depsK "visualize dependencies of defining equations for code" OuterKeyword.diag |
274 |
(Scan.repeat P.term |
|
275 |
>> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory |
|
276 |
o Toplevel.keep ((fn thy => code_deps_cmd thy cs) o Toplevel.theory_of))); |
|
277 |
||
| 24867 | 278 |
val _ = |
| 24436 | 279 |
OuterSyntax.command code_propsK "generate characteristic properties for executable constants" |
280 |
K.thy_decl (P.!!! (code_exprP code_props_cmd) >> Toplevel.theory); |
|
| 24219 | 281 |
|
| 24436 | 282 |
end; (*local*) |
283 |
||
284 |
end; (*struct*) |