author | haftmann |
Fri, 19 Feb 2010 11:06:22 +0100 | |
changeset 35228 | ac2cab4583f4 |
parent 35019 | 1ec0a3ff229e |
child 35299 | 4f4d5bf4ea08 |
permissions | -rw-r--r-- |
34028
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
1 |
(* Title: Tools/code/code_eval.ML_ |
28054 | 2 |
Author: Florian Haftmann, TU Muenchen |
3 |
||
34028
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
4 |
Runtime services building on code generation into implementation language SML. |
28054 | 5 |
*) |
6 |
||
34028
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
7 |
signature CODE_EVAL = |
28054 | 8 |
sig |
34028
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
9 |
val target: string |
32740 | 10 |
val eval: string option -> string * (unit -> 'a) option Unsynchronized.ref |
30970
3fe2e418a071
generic postprocessing scheme for term evaluations
haftmann
parents:
30962
diff
changeset
|
11 |
-> ((term -> term) -> 'a -> 'a) -> theory -> term -> string list -> 'a |
34028
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
12 |
val evaluation_code: theory -> string list -> string list |
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
13 |
-> string * ((string * string) list * (string * string) list) |
28054 | 14 |
val setup: theory -> theory |
15 |
end; |
|
16 |
||
34028
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
17 |
structure Code_Eval : CODE_EVAL = |
28054 | 18 |
struct |
19 |
||
33992 | 20 |
(** generic **) |
21 |
||
34028
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
22 |
val target = "Eval"; |
28054 | 23 |
|
34032 | 24 |
val eval_struct_name = "Code"; |
33992 | 25 |
|
34028
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
26 |
fun evaluation_code thy tycos consts = |
33992 | 27 |
let |
34028
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
28 |
val (consts', (naming, program)) = Code_Thingol.consts_program thy consts; |
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
29 |
val tycos' = map (the o Code_Thingol.lookup_tyco naming) tycos; |
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
30 |
val (ml_code, target_names) = Code_ML.evaluation_code_of thy target |
34032 | 31 |
eval_struct_name naming program (consts' @ tycos'); |
34028
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
32 |
val (consts'', tycos'') = chop (length consts') target_names; |
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
33 |
val consts_map = map2 (fn const => fn NONE => |
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
34 |
error ("Constant " ^ (quote o Code.string_of_const thy) const |
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
35 |
^ "\nhas a user-defined serialization") |
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
36 |
| SOME const'' => (const, const'')) consts consts'' |
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
37 |
val tycos_map = map2 (fn tyco => fn NONE => |
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
38 |
error ("Type " ^ (quote o Sign.extern_type thy) tyco |
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
39 |
^ "\nhas a user-defined serialization") |
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
40 |
| SOME tyco'' => (tyco, tyco'')) tycos tycos''; |
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
41 |
in (ml_code, (tycos_map, consts_map)) end; |
28054 | 42 |
|
43 |
||
34028
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
44 |
(** evaluation **) |
28054 | 45 |
|
30970
3fe2e418a071
generic postprocessing scheme for term evaluations
haftmann
parents:
30962
diff
changeset
|
46 |
fun eval some_target reff postproc thy t args = |
28054 | 47 |
let |
28275
8dab53900e8c
ML_Context.evaluate: proper context (for ML environment);
wenzelm
parents:
28064
diff
changeset
|
48 |
val ctxt = ProofContext.init thy; |
31063
88aaab83b6fc
dropped explicit suppport for frees in evaluation conversion stack
haftmann
parents:
31054
diff
changeset
|
49 |
fun evaluator naming program ((_, (_, ty)), t) deps = |
28054 | 50 |
let |
51 |
val _ = if Code_Thingol.contains_dictvar t then |
|
28724 | 52 |
error "Term to be evaluated contains free dictionaries" else (); |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
53 |
val value_name = "Value.VALUE.value" |
28054 | 54 |
val program' = program |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
55 |
|> Graph.new_node (value_name, |
35228 | 56 |
Code_Thingol.Fun (Term.dummy_patternN, (([], ty), [(([], t), (NONE, true))]))) |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
57 |
|> fold (curry Graph.add_edge value_name) deps; |
34028
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
58 |
val (value_code, [SOME value_name']) = Code_ML.evaluation_code_of thy |
34032 | 59 |
(the_default target some_target) "" naming program' [value_name]; |
28054 | 60 |
val sml_code = "let\n" ^ value_code ^ "\nin " ^ value_name' |
61 |
^ space_implode " " (map (enclose "(" ")") args) ^ " end"; |
|
30672
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
wenzelm
parents:
30648
diff
changeset
|
62 |
in ML_Context.evaluate ctxt false reff sml_code end; |
32123
8bac9ee4b28d
integrated add_triv_classes into evaluation stack
haftmann
parents:
31934
diff
changeset
|
63 |
in Code_Thingol.eval thy postproc evaluator t end; |
28054 | 64 |
|
65 |
||
34028
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
66 |
(** instrumentalization by antiquotation **) |
28054 | 67 |
|
68 |
local |
|
69 |
||
33519 | 70 |
structure CodeAntiqData = Proof_Data |
28054 | 71 |
( |
30962 | 72 |
type T = (string list * string list) * (bool * (string |
73 |
* (string * ((string * string) list * (string * string) list)) lazy)); |
|
74 |
fun init _ = (([], []), (true, ("", Lazy.value ("", ([], []))))); |
|
28054 | 75 |
); |
76 |
||
77 |
val is_first_occ = fst o snd o CodeAntiqData.get; |
|
78 |
||
30962 | 79 |
fun register_code new_tycos new_consts ctxt = |
28054 | 80 |
let |
30962 | 81 |
val ((tycos, consts), (_, (struct_name, _))) = CodeAntiqData.get ctxt; |
82 |
val tycos' = fold (insert (op =)) new_tycos tycos; |
|
83 |
val consts' = fold (insert (op =)) new_consts consts; |
|
28054 | 84 |
val (struct_name', ctxt') = if struct_name = "" |
33992 | 85 |
then ML_Antiquote.variant eval_struct_name ctxt |
28054 | 86 |
else (struct_name, ctxt); |
34028
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
87 |
val acc_code = Lazy.lazy (fn () => evaluation_code (ProofContext.theory_of ctxt) tycos' consts'); |
30962 | 88 |
in CodeAntiqData.put ((tycos', consts'), (false, (struct_name', acc_code))) ctxt' end; |
89 |
||
90 |
fun register_const const = register_code [] [const]; |
|
28054 | 91 |
|
30962 | 92 |
fun register_datatype tyco constrs = register_code [tyco] constrs; |
93 |
||
94 |
fun print_const const all_struct_name tycos_map consts_map = |
|
95 |
(Long_Name.append all_struct_name o the o AList.lookup (op =) consts_map) const; |
|
96 |
||
97 |
fun print_datatype tyco constrs all_struct_name tycos_map consts_map = |
|
28054 | 98 |
let |
30962 | 99 |
val upperize = implode o nth_map 0 Symbol.to_ascii_upper o explode; |
100 |
fun check_base name name'' = |
|
101 |
if upperize (Long_Name.base_name name) = upperize name'' |
|
102 |
then () else error ("Name as printed " ^ quote name'' |
|
103 |
^ "\ndiffers from logical base name " ^ quote (Long_Name.base_name name) ^ "; sorry."); |
|
104 |
val tyco'' = (the o AList.lookup (op =) tycos_map) tyco; |
|
105 |
val constrs'' = map (the o AList.lookup (op =) consts_map) constrs; |
|
106 |
val _ = check_base tyco tyco''; |
|
107 |
val _ = map2 check_base constrs constrs''; |
|
108 |
in "datatype " ^ tyco'' ^ " = datatype " ^ Long_Name.append all_struct_name tyco'' end; |
|
109 |
||
35019
1ec0a3ff229e
simplified interface for ML antiquotations, struct_name is always "Isabelle";
wenzelm
parents:
34032
diff
changeset
|
110 |
fun print_code is_first print_it ctxt = |
30962 | 111 |
let |
112 |
val (_, (_, (struct_code_name, acc_code))) = CodeAntiqData.get ctxt; |
|
33992 | 113 |
val (ml_code, (tycos_map, consts_map)) = Lazy.force acc_code; |
114 |
val ml_code = if is_first then ml_code |
|
28054 | 115 |
else ""; |
35019
1ec0a3ff229e
simplified interface for ML antiquotations, struct_name is always "Isabelle";
wenzelm
parents:
34032
diff
changeset
|
116 |
val all_struct_name = "Isabelle." ^ struct_code_name; |
30962 | 117 |
in (ml_code, print_it all_struct_name tycos_map consts_map) end; |
28054 | 118 |
|
119 |
in |
|
120 |
||
35019
1ec0a3ff229e
simplified interface for ML antiquotations, struct_name is always "Isabelle";
wenzelm
parents:
34032
diff
changeset
|
121 |
fun ml_code_antiq raw_const background = |
28054 | 122 |
let |
31156 | 123 |
val const = Code.check_const (ProofContext.theory_of background) raw_const; |
28054 | 124 |
val is_first = is_first_occ background; |
125 |
val background' = register_const const background; |
|
35019
1ec0a3ff229e
simplified interface for ML antiquotations, struct_name is always "Isabelle";
wenzelm
parents:
34032
diff
changeset
|
126 |
in (print_code is_first (print_const const), background') end; |
30962 | 127 |
|
35019
1ec0a3ff229e
simplified interface for ML antiquotations, struct_name is always "Isabelle";
wenzelm
parents:
34032
diff
changeset
|
128 |
fun ml_code_datatype_antiq (raw_tyco, raw_constrs) background = |
30962 | 129 |
let |
130 |
val thy = ProofContext.theory_of background; |
|
131 |
val tyco = Sign.intern_type thy raw_tyco; |
|
31156 | 132 |
val constrs = map (Code.check_const thy) raw_constrs; |
30962 | 133 |
val constrs' = (map fst o snd o Code.get_datatype thy) tyco; |
33038 | 134 |
val _ = if eq_set (op =) (constrs, constrs') then () |
30962 | 135 |
else error ("Type " ^ quote tyco ^ ": given constructors diverge from real constructors") |
136 |
val is_first = is_first_occ background; |
|
137 |
val background' = register_datatype tyco constrs background; |
|
35019
1ec0a3ff229e
simplified interface for ML antiquotations, struct_name is always "Isabelle";
wenzelm
parents:
34032
diff
changeset
|
138 |
in (print_code is_first (print_datatype tyco constrs), background') end; |
28054 | 139 |
|
140 |
end; (*local*) |
|
141 |
||
142 |
||
143 |
(** Isar setup **) |
|
144 |
||
145 |
val _ = ML_Context.add_antiq "code" (fn _ => Args.term >> ml_code_antiq); |
|
30962 | 146 |
val _ = ML_Context.add_antiq "code_datatype" (fn _ => |
147 |
(Args.tyname --| Scan.lift (Args.$$$ "=") |
|
148 |
-- (Args.term ::: Scan.repeat (Scan.lift (Args.$$$ "|") |-- Args.term))) |
|
149 |
>> ml_code_datatype_antiq); |
|
28054 | 150 |
|
34028
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
151 |
val setup = Code_Target.extend_target (target, (Code_ML.target_SML, K I)); |
28054 | 152 |
|
153 |
end; (*struct*) |