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
|
|
10 |
val the_context: unit -> theory
|
|
11 |
val thm: xstring -> thm
|
|
12 |
val thms: xstring -> thm list
|
|
13 |
end;
|
|
14 |
|
|
15 |
signature ML_CONTEXT =
|
|
16 |
sig
|
|
17 |
include BASIC_ML_CONTEXT
|
|
18 |
val get_context: unit -> Context.generic option
|
|
19 |
val set_context: Context.generic option -> unit
|
|
20 |
val setmp: Context.generic option -> ('a -> 'b) -> 'a -> 'b
|
|
21 |
val the_generic_context: unit -> Context.generic
|
|
22 |
val the_local_context: unit -> Proof.context
|
|
23 |
val pass: Context.generic option -> ('a -> 'b) -> 'a -> 'b * Context.generic option
|
|
24 |
val pass_context: Context.generic -> ('a -> 'b) -> 'a -> 'b * Context.generic
|
|
25 |
val save: ('a -> 'b) -> 'a -> 'b
|
|
26 |
val >> : (theory -> theory) -> unit
|
|
27 |
val add_keywords: string list -> unit
|
|
28 |
val inline_antiq: string ->
|
|
29 |
(Context.generic * Args.T list -> string * (Context.generic * Args.T list)) -> unit
|
|
30 |
val value_antiq: string ->
|
|
31 |
(Context.generic * Args.T list -> (string * string) * (Context.generic * Args.T list)) -> unit
|
|
32 |
val proj_value_antiq: string -> (Context.generic * Args.T list ->
|
|
33 |
(string * string * string) * (Context.generic * Args.T list)) -> unit
|
|
34 |
val eval_antiquotes_fn: (string -> string * string) ref (* FIXME tmp *)
|
|
35 |
val trace: bool ref
|
|
36 |
val use_mltext: string -> bool -> Context.generic option -> unit
|
|
37 |
val use_mltext_update: string -> bool -> Context.generic -> Context.generic
|
|
38 |
val use_let: string -> string -> string -> Context.generic -> Context.generic
|
|
39 |
val use: Path.T -> unit
|
|
40 |
end;
|
|
41 |
|
|
42 |
structure ML_Context: ML_CONTEXT =
|
|
43 |
struct
|
|
44 |
|
|
45 |
(** Implicit ML context **)
|
|
46 |
|
|
47 |
local
|
|
48 |
val current_context = ref (NONE: Context.generic option);
|
|
49 |
in
|
|
50 |
fun get_context () = ! current_context;
|
|
51 |
fun set_context opt_context = current_context := opt_context;
|
|
52 |
fun setmp opt_context f x = Library.setmp current_context opt_context f x;
|
|
53 |
end;
|
|
54 |
|
|
55 |
fun the_generic_context () =
|
|
56 |
(case get_context () of
|
|
57 |
SOME context => context
|
|
58 |
| _ => error "Unknown context");
|
|
59 |
|
|
60 |
val the_local_context = Context.proof_of o the_generic_context;
|
|
61 |
|
|
62 |
val the_context = Context.theory_of o the_generic_context;
|
|
63 |
|
|
64 |
fun pass opt_context f x =
|
|
65 |
setmp opt_context (fn x => let val y = f x in (y, get_context ()) end) x;
|
|
66 |
|
|
67 |
fun pass_context context f x =
|
|
68 |
(case pass (SOME context) f x of
|
|
69 |
(y, SOME context') => (y, context')
|
|
70 |
| (_, NONE) => error "Lost context in ML");
|
|
71 |
|
|
72 |
fun save f x = CRITICAL (fn () => setmp (get_context ()) f x);
|
|
73 |
|
|
74 |
fun change_theory f = CRITICAL (fn () =>
|
|
75 |
set_context (SOME (Context.map_theory f (the_generic_context ()))));
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
(** ML antiquotations **)
|
|
80 |
|
|
81 |
(* maintain keywords *)
|
|
82 |
|
|
83 |
val global_lexicon = ref Scan.empty_lexicon;
|
|
84 |
|
|
85 |
fun add_keywords keywords = CRITICAL (fn () =>
|
|
86 |
change global_lexicon (Scan.extend_lexicon (map Symbol.explode keywords)));
|
|
87 |
|
|
88 |
|
|
89 |
(* maintain commands *)
|
|
90 |
|
|
91 |
datatype antiq = Inline of string | ProjValue of string * string * string;
|
|
92 |
val global_parsers = ref (Symtab.empty:
|
|
93 |
(Context.generic * Args.T list -> antiq * (Context.generic * Args.T list)) Symtab.table);
|
|
94 |
|
|
95 |
local
|
|
96 |
|
|
97 |
fun add_item kind name scan = CRITICAL (fn () =>
|
|
98 |
change global_parsers (fn tab =>
|
|
99 |
(if not (Symtab.defined tab name) then ()
|
|
100 |
else warning ("Redefined ML antiquotation: " ^ quote name);
|
|
101 |
Symtab.update (name, scan >> kind) tab)));
|
|
102 |
|
|
103 |
in
|
|
104 |
|
|
105 |
val inline_antiq = add_item Inline;
|
|
106 |
val proj_value_antiq = add_item ProjValue;
|
|
107 |
fun value_antiq name scan = proj_value_antiq name (scan >> (fn (a, s) => (a, s, "")));
|
|
108 |
|
|
109 |
end;
|
|
110 |
|
|
111 |
|
|
112 |
(* command syntax *)
|
|
113 |
|
|
114 |
fun syntax scan src =
|
|
115 |
#1 (Args.context_syntax "ML antiquotation" scan src (the_local_context ()));
|
|
116 |
|
|
117 |
fun command src =
|
|
118 |
let val ((name, _), pos) = Args.dest_src src in
|
|
119 |
(case Symtab.lookup (! global_parsers) name of
|
|
120 |
NONE => error ("Unknown ML antiquotation command: " ^ quote name ^ Position.str_of pos)
|
|
121 |
| SOME scan => syntax scan src)
|
|
122 |
end;
|
|
123 |
|
|
124 |
|
|
125 |
(* outer syntax *)
|
|
126 |
|
|
127 |
structure T = OuterLex;
|
|
128 |
structure P = OuterParse;
|
|
129 |
|
|
130 |
val antiq =
|
|
131 |
P.!!! (P.position P.xname -- P.arguments --| Scan.ahead P.eof)
|
|
132 |
>> (fn ((x, pos), y) => Args.src ((x, y), pos));
|
|
133 |
|
|
134 |
|
|
135 |
(* input/output wrappers *)
|
|
136 |
|
|
137 |
local
|
|
138 |
|
|
139 |
val isabelle_struct = enclose "structure Isabelle =\nstruct\n" "end;";
|
|
140 |
val isabelle_struct0 = isabelle_struct "";
|
|
141 |
|
|
142 |
fun eval_antiquotes txt = CRITICAL (fn () =>
|
|
143 |
let
|
|
144 |
val ants = Antiquote.scan_antiquotes (txt, Position.line 1);
|
|
145 |
|
|
146 |
fun expand (Antiquote.Text s) names = (("", Symbol.escape s), names)
|
|
147 |
| expand (Antiquote.Antiq x) names =
|
|
148 |
(case command (Antiquote.scan_arguments (! global_lexicon) antiq x) of
|
|
149 |
Inline s => (("", s), names)
|
|
150 |
| ProjValue (a, s, f) =>
|
|
151 |
let
|
|
152 |
val a' = if ML_Syntax.is_identifier a then a else "val";
|
|
153 |
val ([b], names') = Name.variants [a'] names;
|
|
154 |
val binding = "val " ^ b ^ " = " ^ s ^ ";\n";
|
|
155 |
val value =
|
|
156 |
if f = "" then "Isabelle." ^ b
|
|
157 |
else "(" ^ f ^ " Isabelle." ^ b ^ ")";
|
|
158 |
in ((binding, value), names') end);
|
|
159 |
|
|
160 |
val (decls, body) =
|
|
161 |
fold_map expand ants ML_Syntax.reserved
|
|
162 |
|> #1 |> split_list |> pairself implode;
|
|
163 |
in (isabelle_struct decls, body) end);
|
|
164 |
|
|
165 |
fun eval name verbose txt = use_text name Output.ml_output verbose txt;
|
|
166 |
|
|
167 |
in
|
|
168 |
|
|
169 |
val eval_antiquotes_fn = ref eval_antiquotes;
|
|
170 |
|
|
171 |
val trace = ref false;
|
|
172 |
|
|
173 |
fun eval_wrapper name verbose txt =
|
|
174 |
let
|
|
175 |
val (txt1, txt2) = ! eval_antiquotes_fn txt;
|
|
176 |
val _ = if ! trace then tracing (cat_lines [txt1, txt2]) else ();
|
|
177 |
in eval "" false txt1; eval name verbose txt2; eval "" false isabelle_struct0 end;
|
|
178 |
|
|
179 |
end;
|
|
180 |
|
|
181 |
|
|
182 |
(* ML evaluation *)
|
|
183 |
|
|
184 |
fun use_mltext txt verbose opt_context =
|
|
185 |
setmp opt_context (fn () => eval_wrapper "ML" verbose txt) ();
|
|
186 |
|
|
187 |
fun use_mltext_update txt verbose context =
|
|
188 |
#2 (pass_context context (eval_wrapper "ML" verbose) txt);
|
|
189 |
|
|
190 |
fun use_context txt = use_mltext_update
|
|
191 |
("ML_Context.set_context (SOME ((" ^ txt ^ ") (ML_Context.the_generic_context ())));") false;
|
|
192 |
|
|
193 |
fun use_let bind body txt =
|
|
194 |
use_context ("let " ^ bind ^ " = " ^ txt ^ " in\n" ^ body ^ " end");
|
|
195 |
|
|
196 |
fun use path = eval_wrapper (Path.implode path) true (File.read path);
|
|
197 |
|
|
198 |
|
|
199 |
(* basic antiquotations *)
|
|
200 |
|
|
201 |
val _ = value_antiq "context" (Scan.succeed ("context", "ML_Context.the_local_context ()"));
|
|
202 |
|
|
203 |
val _ = inline_antiq "typ" (Args.typ >> (ML_Syntax.atomic o ML_Syntax.print_typ));
|
|
204 |
val _ = inline_antiq "term" (Args.term >> (ML_Syntax.atomic o ML_Syntax.print_term));
|
|
205 |
val _ = inline_antiq "prop" (Args.prop >> (ML_Syntax.atomic o ML_Syntax.print_term));
|
|
206 |
|
|
207 |
val _ = value_antiq "ctyp" (Args.typ >> (fn T =>
|
|
208 |
("ctyp",
|
|
209 |
"Thm.ctyp_of (ML_Context.the_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_typ T))));
|
|
210 |
|
|
211 |
val _ = value_antiq "cterm" (Args.term >> (fn t =>
|
|
212 |
("cterm",
|
|
213 |
"Thm.cterm_of (ML_Context.the_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_term t))));
|
|
214 |
|
|
215 |
val _ = value_antiq "cprop" (Args.prop >> (fn t =>
|
|
216 |
("cprop",
|
|
217 |
"Thm.cterm_of (ML_Context.the_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_term t))));
|
|
218 |
|
|
219 |
|
|
220 |
fun const syn = (Scan.state >> Context.proof_of) -- Scan.lift Args.name >> (fn (ctxt, c) =>
|
|
221 |
#1 (Term.dest_Const (Consts.read_const (ProofContext.consts_of ctxt) c))
|
|
222 |
|> syn ? ProofContext.const_syntax_name ctxt
|
|
223 |
|> ML_Syntax.print_string);
|
|
224 |
|
|
225 |
val _ = inline_antiq "const_name" (const false);
|
|
226 |
val _ = inline_antiq "const_syntax" (const true);
|
|
227 |
|
|
228 |
|
|
229 |
|
|
230 |
(** fact references **)
|
|
231 |
|
|
232 |
fun thm name = ProofContext.get_thm (the_local_context ()) (Name name);
|
|
233 |
fun thms name = ProofContext.get_thms (the_local_context ()) (Name name);
|
|
234 |
|
|
235 |
|
|
236 |
local
|
|
237 |
|
|
238 |
fun print_interval (FromTo arg) =
|
|
239 |
"PureThy.FromTo " ^ ML_Syntax.print_pair ML_Syntax.print_int ML_Syntax.print_int arg
|
|
240 |
| print_interval (From i) = "PureThy.From " ^ ML_Syntax.print_int i
|
|
241 |
| print_interval (Single i) = "PureThy.Single " ^ ML_Syntax.print_int i;
|
|
242 |
|
|
243 |
fun thm_sel name =
|
|
244 |
Args.thm_sel >> (fn is => "PureThy.NameSelection " ^
|
|
245 |
ML_Syntax.print_pair ML_Syntax.print_string (ML_Syntax.print_list print_interval) (name, is))
|
|
246 |
|| Scan.succeed ("PureThy.Name " ^ ML_Syntax.print_string name);
|
|
247 |
|
|
248 |
fun thm_antiq kind = value_antiq kind
|
|
249 |
(Scan.lift (Args.name :-- thm_sel) >> apsnd (fn sel =>
|
|
250 |
"ProofContext.get_" ^ kind ^ " (ML_Context.the_local_context ()) " ^ ML_Syntax.atomic sel));
|
|
251 |
|
|
252 |
in
|
|
253 |
|
|
254 |
val _ = add_keywords ["(", ")", "-", ","];
|
|
255 |
val _ = thm_antiq "thm";
|
|
256 |
val _ = thm_antiq "thms";
|
|
257 |
|
|
258 |
end;
|
|
259 |
|
|
260 |
val _ = proj_value_antiq "cpat" (Scan.lift Args.name >>
|
|
261 |
(fn ns => ("cpat",
|
|
262 |
"((cterm_of (ML_Context.the_context ())) o Library.the_single o " ^
|
|
263 |
"(ProofContext.read_term_pats TypeInfer.logicT (ML_Context.the_local_context ())))"
|
|
264 |
^ (ML_Syntax.print_list ML_Syntax.print_string [ns]), "")));
|
|
265 |
|
|
266 |
(*final declarations of this structure!*)
|
|
267 |
nonfix >>;
|
|
268 |
fun >> f = change_theory f;
|
|
269 |
|
|
270 |
end;
|
|
271 |
|
|
272 |
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
|
|
273 |
open Basic_ML_Context;
|