author | wenzelm |
Wed, 06 Aug 2008 00:12:21 +0200 | |
changeset 27755 | f7cdde18aeb3 |
parent 27723 | ce8f79b91ed1 |
child 27771 | 98499933a50f |
permissions | -rw-r--r-- |
24581 | 1 |
(* Title: Pure/ML/ml_context.ML |
24574 | 2 |
ID: $Id$ |
3 |
Author: Makarius |
|
4 |
||
5 |
ML context and antiquotations. |
|
6 |
*) |
|
7 |
||
8 |
signature BASIC_ML_CONTEXT = |
|
9 |
sig |
|
26416
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset
|
10 |
val bind_thm: string * thm -> unit |
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset
|
11 |
val bind_thms: string * thm list -> unit |
24574 | 12 |
val thm: xstring -> thm |
13 |
val thms: xstring -> thm list |
|
25204 | 14 |
end |
24574 | 15 |
|
16 |
signature ML_CONTEXT = |
|
17 |
sig |
|
18 |
include BASIC_ML_CONTEXT |
|
19 |
val the_generic_context: unit -> Context.generic |
|
26432
095e448b95a0
renamed ML_Context.the_context to ML_Context.the_global_context;
wenzelm
parents:
26416
diff
changeset
|
20 |
val the_global_context: unit -> theory |
24574 | 21 |
val the_local_context: unit -> Proof.context |
26455 | 22 |
val exec: (unit -> unit) -> Context.generic -> Context.generic |
26416
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset
|
23 |
val stored_thms: thm list ref |
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset
|
24 |
val ml_store_thm: string * thm -> unit |
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset
|
25 |
val ml_store_thms: string * thm list -> unit |
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
26 |
type antiq = |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
27 |
{struct_name: string, background: Proof.context} -> |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
28 |
(Proof.context -> string * string) * Proof.context |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
29 |
val add_antiq: string -> |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
30 |
(Context.generic * Args.T list -> antiq * (Context.generic * Args.T list)) -> unit |
24574 | 31 |
val trace: bool ref |
27723 | 32 |
val eval_wrapper: (string -> unit) * (string -> 'a) -> bool -> Position.T -> string -> unit |
26455 | 33 |
val eval: bool -> Position.T -> string -> unit |
34 |
val eval_file: Path.T -> unit |
|
35 |
val eval_in: Context.generic option -> bool -> Position.T -> string -> unit |
|
25204 | 36 |
val evaluate: (string -> unit) * (string -> 'b) -> bool -> |
37 |
string * (unit -> 'a) option ref -> string -> 'a |
|
26455 | 38 |
val expression: Position.T -> string -> string -> string -> Context.generic -> Context.generic |
25204 | 39 |
end |
24574 | 40 |
|
41 |
structure ML_Context: ML_CONTEXT = |
|
42 |
struct |
|
43 |
||
26416
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset
|
44 |
(** implicit ML context **) |
24574 | 45 |
|
26416
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset
|
46 |
val the_generic_context = Context.the_thread_data; |
26432
095e448b95a0
renamed ML_Context.the_context to ML_Context.the_global_context;
wenzelm
parents:
26416
diff
changeset
|
47 |
val the_global_context = Context.theory_of o the_generic_context; |
24574 | 48 |
val the_local_context = Context.proof_of o the_generic_context; |
49 |
||
26455 | 50 |
fun exec (e: unit -> unit) context = |
51 |
(case Context.setmp_thread_data (SOME context) (fn () => (e (); Context.thread_data ())) () of |
|
52 |
SOME context' => context' |
|
53 |
| NONE => error "Missing context after execution"); |
|
26416
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset
|
54 |
|
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset
|
55 |
|
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset
|
56 |
(* theorem bindings *) |
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset
|
57 |
|
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset
|
58 |
val stored_thms: thm list ref = ref []; |
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset
|
59 |
|
26492
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset
|
60 |
fun ml_store sel (name, ths) = |
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset
|
61 |
let |
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset
|
62 |
val ths' = Context.>>> (Context.map_theory_result (PureThy.store_thms (name, ths))); |
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset
|
63 |
val _ = |
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset
|
64 |
if name = "" then () |
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset
|
65 |
else if not (ML_Syntax.is_identifier name) then |
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset
|
66 |
error ("Cannot bind theorem(s) " ^ quote name ^ " as ML value") |
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset
|
67 |
else setmp stored_thms ths' (fn () => |
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset
|
68 |
use_text (0, "") Output.ml_output true |
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset
|
69 |
("val " ^ name ^ " = " ^ sel ^ "(! ML_Context.stored_thms);")) (); |
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset
|
70 |
in () end; |
24574 | 71 |
|
26492
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset
|
72 |
val ml_store_thms = ml_store ""; |
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset
|
73 |
fun ml_store_thm (name, th) = ml_store "hd" (name, [th]); |
26416
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset
|
74 |
|
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset
|
75 |
fun bind_thm (name, thm) = ml_store_thm (name, Drule.standard thm); |
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset
|
76 |
fun bind_thms (name, thms) = ml_store_thms (name, map Drule.standard thms); |
24574 | 77 |
|
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
78 |
fun thm name = ProofContext.get_thm (the_local_context ()) name; |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
79 |
fun thms name = ProofContext.get_thms (the_local_context ()) name; |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
80 |
|
24574 | 81 |
|
82 |
||
83 |
(** ML antiquotations **) |
|
84 |
||
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
85 |
(* antiquotation commands *) |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
86 |
|
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
87 |
type antiq = |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
88 |
{struct_name: string, background: Proof.context} -> |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
89 |
(Proof.context -> string * string) * Proof.context; |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
90 |
|
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
91 |
local |
24574 | 92 |
|
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
93 |
val global_parsers = ref (Symtab.empty: |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
94 |
(Context.generic * Args.T list -> antiq * (Context.generic * Args.T list)) Symtab.table); |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
95 |
|
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
96 |
in |
24574 | 97 |
|
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
98 |
fun add_antiq name scan = CRITICAL (fn () => |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
99 |
change global_parsers (fn tab => |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
100 |
(if not (Symtab.defined tab name) then () |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
101 |
else warning ("Redefined ML antiquotation: " ^ quote name); |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
102 |
Symtab.update (name, scan) tab))); |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
103 |
|
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
104 |
fun antiquotation src ctxt = |
24574 | 105 |
let val ((name, _), pos) = Args.dest_src src in |
106 |
(case Symtab.lookup (! global_parsers) name of |
|
107 |
NONE => error ("Unknown ML antiquotation command: " ^ quote name ^ Position.str_of pos) |
|
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
108 |
| SOME scan => Args.context_syntax "ML antiquotation" scan src ctxt) |
24574 | 109 |
end; |
110 |
||
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
111 |
end; |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
112 |
|
24574 | 113 |
|
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
114 |
(* parsing and evaluation *) |
24574 | 115 |
|
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
116 |
local |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
117 |
|
24574 | 118 |
structure P = OuterParse; |
119 |
||
120 |
val antiq = |
|
121 |
P.!!! (P.position P.xname -- P.arguments --| Scan.ahead P.eof) |
|
122 |
>> (fn ((x, pos), y) => Args.src ((x, y), pos)); |
|
123 |
||
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
124 |
fun eval_antiquotes struct_name (txt, pos) opt_ctxt = |
24574 | 125 |
let |
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
126 |
val ants = Antiquote.scan_antiquotes (txt, pos); |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
127 |
val ((ml_env, ml_body), opt_ctxt') = |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
128 |
if not (exists Antiquote.is_antiq ants) then (("", Symbol.escape txt), opt_ctxt) |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
129 |
else |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
130 |
let |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
131 |
val ctxt = |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
132 |
(case opt_ctxt of |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
133 |
NONE => error ("Unknown context -- cannot expand ML antiquotations" ^ |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
134 |
Position.str_of pos) |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
135 |
| SOME context => Context.proof_of context); |
27378 | 136 |
|
27359 | 137 |
val lex = #1 (OuterKeyword.get_lexicons ()); |
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
138 |
fun no_decl _ = ("", ""); |
27378 | 139 |
|
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
140 |
fun expand (Antiquote.Text s) state = (K ("", Symbol.escape s), state) |
27755 | 141 |
| expand (Antiquote.Antiq (x, _)) (scope, background) = |
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
142 |
let |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
143 |
val context = Stack.top scope; |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
144 |
val (f, context') = antiquotation (Antiquote.scan_arguments lex antiq x) context; |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
145 |
val (decl, background') = f {background = background, struct_name = struct_name}; |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
146 |
in (decl, (Stack.map_top (K context') scope, background')) end |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
147 |
| expand (Antiquote.Open _) (scope, background) = |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
148 |
(no_decl, (Stack.push scope, background)) |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
149 |
| expand (Antiquote.Close _) (scope, background) = |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
150 |
(no_decl, (Stack.pop scope, background)); |
24574 | 151 |
|
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
152 |
val (decls, (_, ctxt')) = fold_map expand ants (Stack.init ctxt, ctxt); |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
153 |
val ml = decls |> map (fn decl => decl ctxt') |> split_list |> pairself implode; |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
154 |
in (ml, SOME (Context.Proof ctxt')) end; |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
155 |
in (("structure " ^ struct_name ^ " =\nstruct\n" ^ ml_env ^ "end;", ml_body), opt_ctxt') end; |
24574 | 156 |
|
157 |
in |
|
158 |
||
159 |
val trace = ref false; |
|
160 |
||
26385
ae7564661e76
ML runtime compilation: pass position, tuned signature;
wenzelm
parents:
26374
diff
changeset
|
161 |
fun eval_wrapper pr verbose pos txt = |
24574 | 162 |
let |
26473
2266e5fd7b63
eval_wrapper: non-critical version via unique Isabelle structure, proper forget_structure;
wenzelm
parents:
26455
diff
changeset
|
163 |
val struct_name = |
2266e5fd7b63
eval_wrapper: non-critical version via unique Isabelle structure, proper forget_structure;
wenzelm
parents:
26455
diff
changeset
|
164 |
if Multithreading.available then "Isabelle" ^ serial_string () |
2266e5fd7b63
eval_wrapper: non-critical version via unique Isabelle structure, proper forget_structure;
wenzelm
parents:
26455
diff
changeset
|
165 |
else "Isabelle"; |
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
166 |
val ((env, body), env_ctxt) = eval_antiquotes struct_name (txt, pos) (Context.thread_data ()); |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
167 |
val _ = if ! trace then tracing (cat_lines [env, body]) else (); |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
168 |
fun eval_raw p = |
26385
ae7564661e76
ML runtime compilation: pass position, tuned signature;
wenzelm
parents:
26374
diff
changeset
|
169 |
use_text (the_default 1 (Position.line_of p), the_default "ML" (Position.file_of p)) pr; |
25755 | 170 |
in |
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
171 |
Context.setmp_thread_data env_ctxt (fn () => eval_raw Position.none false env) (); |
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset
|
172 |
NAMED_CRITICAL "ML" (fn () => eval_raw pos verbose body); (* FIXME non-critical with local ML env *) |
26473
2266e5fd7b63
eval_wrapper: non-critical version via unique Isabelle structure, proper forget_structure;
wenzelm
parents:
26455
diff
changeset
|
173 |
forget_structure struct_name |
25755 | 174 |
end; |
24574 | 175 |
|
176 |
end; |
|
177 |
||
178 |
||
179 |
(* ML evaluation *) |
|
180 |
||
26455 | 181 |
val eval = eval_wrapper Output.ml_output; |
26881 | 182 |
fun eval_file path = eval true (Path.position path) (File.read path); |
24574 | 183 |
|
26455 | 184 |
fun eval_in context verbose pos txt = |
185 |
Context.setmp_thread_data context (fn () => eval verbose pos txt) (); |
|
25204 | 186 |
|
25700 | 187 |
fun evaluate pr verbose (ref_name, r) txt = NAMED_CRITICAL "ML" (fn () => |
25204 | 188 |
let |
189 |
val _ = r := NONE; |
|
26385
ae7564661e76
ML runtime compilation: pass position, tuned signature;
wenzelm
parents:
26374
diff
changeset
|
190 |
val _ = eval_wrapper pr verbose Position.none |
ae7564661e76
ML runtime compilation: pass position, tuned signature;
wenzelm
parents:
26374
diff
changeset
|
191 |
("val _ = (" ^ ref_name ^ " := SOME (fn () => " ^ txt ^ "))"); |
25204 | 192 |
in (case ! r of NONE => error ("Bad evaluation for " ^ ref_name) | SOME e => e) end) (); |
24574 | 193 |
|
26455 | 194 |
fun expression pos bind body txt = |
195 |
exec (fn () => eval false pos |
|
196 |
("Context.set_thread_data (SOME (let " ^ bind ^ " = " ^ txt ^ " in " ^ body ^ |
|
197 |
" end (ML_Context.the_generic_context ())));")); |
|
198 |
||
24574 | 199 |
end; |
200 |
||
201 |
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context; |
|
202 |
open Basic_ML_Context; |
|
27378 | 203 |