src/Pure/ML/ml_context.ML
author wenzelm
Mon Sep 06 22:08:49 2010 +0200 (2010-09-06)
changeset 39164 e7e12555e763
parent 39140 8a2fb4ce1f39
child 39388 fdbb2c55ffc2
permissions -rw-r--r--
ML_Context.thm and ML_Context.thms no longer pervasive;
wenzelm@24581
     1
(*  Title:      Pure/ML/ml_context.ML
wenzelm@24574
     2
    Author:     Makarius
wenzelm@24574
     3
wenzelm@24574
     4
ML context and antiquotations.
wenzelm@24574
     5
*)
wenzelm@24574
     6
wenzelm@24574
     7
signature BASIC_ML_CONTEXT =
wenzelm@24574
     8
sig
wenzelm@26416
     9
  val bind_thm: string * thm -> unit
wenzelm@26416
    10
  val bind_thms: string * thm list -> unit
wenzelm@25204
    11
end
wenzelm@24574
    12
wenzelm@24574
    13
signature ML_CONTEXT =
wenzelm@24574
    14
sig
wenzelm@24574
    15
  include BASIC_ML_CONTEXT
wenzelm@24574
    16
  val the_generic_context: unit -> Context.generic
wenzelm@26432
    17
  val the_global_context: unit -> theory
wenzelm@24574
    18
  val the_local_context: unit -> Proof.context
wenzelm@39164
    19
  val thm: xstring -> thm
wenzelm@39164
    20
  val thms: xstring -> thm list
wenzelm@26455
    21
  val exec: (unit -> unit) -> Context.generic -> Context.generic
wenzelm@39140
    22
  val get_stored_thms: unit -> thm list
wenzelm@39140
    23
  val get_stored_thm: unit -> thm
wenzelm@39140
    24
  val ml_store_thms: string * thm list -> unit
wenzelm@26416
    25
  val ml_store_thm: string * thm -> unit
wenzelm@35019
    26
  type antiq = Proof.context -> (Proof.context -> string * string) * Proof.context
wenzelm@30513
    27
  val add_antiq: string -> (Position.T -> antiq context_parser) -> unit
wenzelm@32738
    28
  val trace: bool Unsynchronized.ref
wenzelm@30637
    29
  val eval_antiquotes: ML_Lex.token Antiquote.antiquote list * Position.T ->
wenzelm@30637
    30
    Context.generic option -> (ML_Lex.token list * ML_Lex.token list) * Context.generic option
wenzelm@37198
    31
  val eval: bool -> Position.T -> ML_Lex.token Antiquote.antiquote list -> unit
wenzelm@37198
    32
  val eval_text: bool -> Position.T -> Symbol_Pos.text -> unit
wenzelm@26455
    33
  val eval_file: Path.T -> unit
wenzelm@37198
    34
  val eval_in: Proof.context option -> bool -> Position.T ->
wenzelm@37198
    35
    ML_Lex.token Antiquote.antiquote list -> unit
wenzelm@37198
    36
  val eval_text_in: Proof.context option -> bool -> Position.T -> Symbol_Pos.text -> unit
wenzelm@37198
    37
  val expression: Position.T -> string -> string -> ML_Lex.token Antiquote.antiquote list ->
wenzelm@37198
    38
    Context.generic -> Context.generic
haftmann@38931
    39
  val evaluate: Proof.context -> bool ->
haftmann@38931
    40
    string * (unit -> 'a) option Unsynchronized.ref -> string * string -> 'a
wenzelm@25204
    41
end
wenzelm@24574
    42
wenzelm@24574
    43
structure ML_Context: ML_CONTEXT =
wenzelm@24574
    44
struct
wenzelm@24574
    45
wenzelm@26416
    46
(** implicit ML context **)
wenzelm@24574
    47
wenzelm@26416
    48
val the_generic_context = Context.the_thread_data;
wenzelm@26432
    49
val the_global_context = Context.theory_of o the_generic_context;
wenzelm@24574
    50
val the_local_context = Context.proof_of o the_generic_context;
wenzelm@24574
    51
wenzelm@39164
    52
fun thm name = ProofContext.get_thm (the_local_context ()) name;
wenzelm@39164
    53
fun thms name = ProofContext.get_thms (the_local_context ()) name;
wenzelm@39164
    54
wenzelm@26455
    55
fun exec (e: unit -> unit) context =
wenzelm@26455
    56
  (case Context.setmp_thread_data (SOME context) (fn () => (e (); Context.thread_data ())) () of
wenzelm@26455
    57
    SOME context' => context'
wenzelm@26455
    58
  | NONE => error "Missing context after execution");
wenzelm@26416
    59
wenzelm@26416
    60
wenzelm@26416
    61
(* theorem bindings *)
wenzelm@26416
    62
wenzelm@39140
    63
structure Stored_Thms = Theory_Data
wenzelm@39140
    64
(
wenzelm@39140
    65
  type T = thm list;
wenzelm@39140
    66
  val empty = [];
wenzelm@39140
    67
  fun extend _ = [];
wenzelm@39140
    68
  fun merge _ = [];
wenzelm@39140
    69
);
wenzelm@26416
    70
wenzelm@39140
    71
fun get_stored_thms () = Stored_Thms.get (the_global_context ());
wenzelm@39140
    72
val get_stored_thm = hd o get_stored_thms;
wenzelm@39140
    73
wenzelm@39140
    74
fun ml_store get (name, ths) =
wenzelm@26492
    75
  let
haftmann@29579
    76
    val ths' = Context.>>> (Context.map_theory_result
haftmann@29579
    77
      (PureThy.store_thms (Binding.name name, ths)));
wenzelm@39140
    78
    val _ = Context.>> (Context.map_theory (Stored_Thms.put ths'));
wenzelm@26492
    79
    val _ =
wenzelm@26492
    80
      if name = "" then ()
wenzelm@26492
    81
      else if not (ML_Syntax.is_identifier name) then
wenzelm@26492
    82
        error ("Cannot bind theorem(s) " ^ quote name ^ " as ML value")
wenzelm@39140
    83
      else
wenzelm@31334
    84
        ML_Compiler.eval true Position.none
wenzelm@39140
    85
          (ML_Lex.tokenize ("val " ^ name ^ " = " ^ get ^ " ();"));
wenzelm@39140
    86
    val _ = Context.>> (Context.map_theory (Stored_Thms.put []));
wenzelm@26492
    87
  in () end;
wenzelm@24574
    88
wenzelm@39140
    89
val ml_store_thms = ml_store "ML_Context.get_stored_thms";
wenzelm@39140
    90
fun ml_store_thm (name, th) = ml_store "ML_Context.get_stored_thm" (name, [th]);
wenzelm@26416
    91
wenzelm@35021
    92
fun bind_thm (name, thm) = ml_store_thm (name, Drule.export_without_context thm);
wenzelm@35021
    93
fun bind_thms (name, thms) = ml_store_thms (name, map Drule.export_without_context thms);
wenzelm@24574
    94
wenzelm@24574
    95
wenzelm@24574
    96
wenzelm@24574
    97
(** ML antiquotations **)
wenzelm@24574
    98
wenzelm@27343
    99
(* antiquotation commands *)
wenzelm@27343
   100
wenzelm@35019
   101
type antiq = Proof.context -> (Proof.context -> string * string) * Proof.context;
wenzelm@27343
   102
wenzelm@27343
   103
local
wenzelm@24574
   104
wenzelm@32738
   105
val global_parsers =
wenzelm@32738
   106
  Unsynchronized.ref (Symtab.empty: (Position.T -> antiq context_parser) Symtab.table);
wenzelm@27343
   107
wenzelm@27343
   108
in
wenzelm@24574
   109
wenzelm@27343
   110
fun add_antiq name scan = CRITICAL (fn () =>
wenzelm@32738
   111
  Unsynchronized.change global_parsers (fn tab =>
wenzelm@27343
   112
   (if not (Symtab.defined tab name) then ()
wenzelm@27343
   113
    else warning ("Redefined ML antiquotation: " ^ quote name);
wenzelm@27343
   114
    Symtab.update (name, scan) tab)));
wenzelm@27343
   115
wenzelm@27343
   116
fun antiquotation src ctxt =
wenzelm@24574
   117
  let val ((name, _), pos) = Args.dest_src src in
wenzelm@24574
   118
    (case Symtab.lookup (! global_parsers) name of
wenzelm@24574
   119
      NONE => error ("Unknown ML antiquotation command: " ^ quote name ^ Position.str_of pos)
wenzelm@38237
   120
    | SOME scan => (Context_Position.report ctxt (Markup.ML_antiq name) pos;
wenzelm@27895
   121
        Args.context_syntax "ML antiquotation" (scan pos) src ctxt))
wenzelm@24574
   122
  end;
wenzelm@24574
   123
wenzelm@27343
   124
end;
wenzelm@27343
   125
wenzelm@24574
   126
wenzelm@27343
   127
(* parsing and evaluation *)
wenzelm@24574
   128
wenzelm@27343
   129
local
wenzelm@27343
   130
wenzelm@24574
   131
val antiq =
wenzelm@36950
   132
  Parse.!!! (Parse.position Parse.xname -- Args.parse --| Scan.ahead Parse.eof)
wenzelm@24574
   133
  >> (fn ((x, pos), y) => Args.src ((x, y), pos));
wenzelm@24574
   134
wenzelm@35019
   135
val begin_env = ML_Lex.tokenize "structure Isabelle =\nstruct\n";
wenzelm@30637
   136
val end_env = ML_Lex.tokenize "end;";
wenzelm@31334
   137
val reset_env = ML_Lex.tokenize "structure Isabelle = struct end";
wenzelm@28270
   138
wenzelm@30637
   139
in
wenzelm@30637
   140
wenzelm@30637
   141
fun eval_antiquotes (ants, pos) opt_context =
wenzelm@24574
   142
  let
wenzelm@28270
   143
    val opt_ctxt = Option.map (Context.Proof o Context.proof_of) opt_context;
wenzelm@27343
   144
    val ((ml_env, ml_body), opt_ctxt') =
wenzelm@30637
   145
      if forall Antiquote.is_text ants
wenzelm@30637
   146
      then (([], map (fn Antiquote.Text tok => tok) ants), opt_ctxt)
wenzelm@27343
   147
      else
wenzelm@27343
   148
        let
wenzelm@27343
   149
          val ctxt =
wenzelm@27343
   150
            (case opt_ctxt of
wenzelm@30637
   151
              NONE => error ("No context -- cannot expand ML antiquotations" ^ Position.str_of pos)
wenzelm@28270
   152
            | SOME ctxt => Context.proof_of ctxt);
wenzelm@27378
   153
wenzelm@36950
   154
          val lex = #1 (Keyword.get_lexicons ());
wenzelm@30637
   155
          fun no_decl _ = ([], []);
wenzelm@27378
   156
wenzelm@30637
   157
          fun expand (Antiquote.Text tok) state = (K ([], [tok]), state)
wenzelm@30683
   158
            | expand (Antiquote.Antiq (ss, range)) (scope, background) =
wenzelm@27343
   159
                let
wenzelm@27343
   160
                  val context = Stack.top scope;
wenzelm@36959
   161
                  val (f, context') =
wenzelm@36959
   162
                    antiquotation (Token.read_antiq lex antiq (ss, #1 range)) context;
wenzelm@35019
   163
                  val (decl, background') = f background;
wenzelm@30683
   164
                  val decl' = decl #> pairself (ML_Lex.tokenize #> map (ML_Lex.set_range range));
wenzelm@30637
   165
                in (decl', (Stack.map_top (K context') scope, background')) end
wenzelm@27343
   166
            | expand (Antiquote.Open _) (scope, background) =
wenzelm@27343
   167
                (no_decl, (Stack.push scope, background))
wenzelm@27343
   168
            | expand (Antiquote.Close _) (scope, background) =
wenzelm@27343
   169
                (no_decl, (Stack.pop scope, background));
wenzelm@24574
   170
wenzelm@27343
   171
          val (decls, (_, ctxt')) = fold_map expand ants (Stack.init ctxt, ctxt);
wenzelm@30637
   172
          val ml = decls |> map (fn decl => decl ctxt') |> split_list |> pairself flat;
wenzelm@27343
   173
        in (ml, SOME (Context.Proof ctxt')) end;
wenzelm@30637
   174
  in ((begin_env @ ml_env @ end_env, ml_body), opt_ctxt') end;
wenzelm@24574
   175
wenzelm@32738
   176
val trace = Unsynchronized.ref false;
wenzelm@24574
   177
wenzelm@37198
   178
fun eval verbose pos ants =
wenzelm@24574
   179
  let
wenzelm@28270
   180
    (*prepare source text*)
wenzelm@38720
   181
    val ((env, body), env_ctxt) =
wenzelm@38720
   182
      eval_antiquotes (ants, pos) (Context.thread_data ())
wenzelm@38720
   183
      ||> Option.map (Context.mapping I (Context_Position.set_visible false));
wenzelm@31334
   184
    val _ =
wenzelm@31334
   185
      if ! trace then tracing (cat_lines [ML_Lex.flatten env, ML_Lex.flatten body])
wenzelm@31334
   186
      else ();
wenzelm@28270
   187
wenzelm@28270
   188
    (*prepare static ML environment*)
wenzelm@28270
   189
    val _ = Context.setmp_thread_data env_ctxt
wenzelm@31334
   190
        (fn () => (ML_Compiler.eval false Position.none env; Context.thread_data ())) ()
wenzelm@31325
   191
      |> (fn NONE => () | SOME context' => Context.>> (ML_Env.inherit context'));
wenzelm@28270
   192
wenzelm@31334
   193
    val _ = ML_Compiler.eval verbose pos body;
wenzelm@31334
   194
    val _ = ML_Compiler.eval false Position.none reset_env;
wenzelm@28270
   195
  in () end;
wenzelm@24574
   196
wenzelm@24574
   197
end;
wenzelm@24574
   198
wenzelm@24574
   199
wenzelm@30672
   200
(* derived versions *)
wenzelm@24574
   201
wenzelm@37198
   202
fun eval_text verbose pos txt = eval verbose pos (ML_Lex.read pos txt);
wenzelm@37198
   203
fun eval_file path = eval_text true (Path.position path) (File.read path);
wenzelm@37198
   204
wenzelm@37198
   205
fun eval_in ctxt verbose pos ants =
wenzelm@37198
   206
  Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval verbose pos ants) ();
wenzelm@24574
   207
wenzelm@37198
   208
fun eval_text_in ctxt verbose pos txt =
wenzelm@37198
   209
  Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval_text verbose pos txt) ();
wenzelm@37198
   210
wenzelm@37198
   211
fun expression pos bind body ants =
wenzelm@37198
   212
  exec (fn () => eval false pos
wenzelm@37198
   213
    (ML_Lex.read Position.none ("Context.set_thread_data (SOME (let " ^ bind ^ " = ") @ ants @
wenzelm@37198
   214
      ML_Lex.read Position.none (" in " ^ body ^ " end (ML_Context.the_generic_context ())));")));
wenzelm@37198
   215
wenzelm@25204
   216
wenzelm@33222
   217
(* FIXME not thread-safe -- reference can be accessed directly *)
haftmann@38931
   218
fun evaluate ctxt verbose (ref_name, r) (prelude, txt) = NAMED_CRITICAL "ML" (fn () =>
wenzelm@25204
   219
  let
haftmann@38931
   220
    val s = prelude ^ "val _ = (" ^ ref_name ^ " := SOME (fn () => " ^ txt ^ "))"
haftmann@38931
   221
    val ants = ML_Lex.read Position.none s;
wenzelm@25204
   222
    val _ = r := NONE;
wenzelm@37198
   223
    val _ =
wenzelm@37198
   224
      Context.setmp_thread_data (SOME (Context.Proof ctxt))
wenzelm@37198
   225
        (fn () => eval verbose Position.none ants) ();
wenzelm@25204
   226
  in (case ! r of NONE => error ("Bad evaluation for " ^ ref_name) | SOME e => e) end) ();
wenzelm@24574
   227
wenzelm@24574
   228
end;
wenzelm@24574
   229
wenzelm@24574
   230
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
wenzelm@24574
   231
open Basic_ML_Context;