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