src/Pure/ML/ml_context.ML
author wenzelm
Mon, 17 May 2010 15:11:25 +0200
changeset 36969 f5417836dbea
parent 36950 75b8f26f2f07
child 37199 3af985b10550
permissions -rw-r--r--
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
eliminated slightly odd alias structure T;
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@30518
    26
  val add_antiq: string -> (Position.T -> antiq context_parser) -> unit
wenzelm@32738
    27
  val trace: bool Unsynchronized.ref
wenzelm@30641
    28
  val eval_antiquotes: ML_Lex.token Antiquote.antiquote list * Position.T ->
wenzelm@30641
    29
    Context.generic option -> (ML_Lex.token list * ML_Lex.token list) * Context.generic option
wenzelm@30577
    30
  val eval: bool -> Position.T -> Symbol_Pos.text -> unit
wenzelm@26455
    31
  val eval_file: Path.T -> unit
wenzelm@30577
    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@24574
    51
wenzelm@26416
    52
wenzelm@26416
    53
(* theorem bindings *)
wenzelm@26416
    54
wenzelm@33233
    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@31368
    66
        ML_Compiler.eval true Position.none
wenzelm@31368
    67
          (ML_Lex.tokenize ("val " ^ name ^ " = " ^ sel ^ "(! ML_Context.stored_thms);"))) ();
wenzelm@26492
    68
  in () end;
wenzelm@26416
    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@27343
    88
wenzelm@32738
    89
val global_parsers =
wenzelm@32738
    90
  Unsynchronized.ref (Symtab.empty: (Position.T -> antiq context_parser) Symtab.table);
wenzelm@24574
    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@24574
    96
   (if not (Symtab.defined tab name) then ()
wenzelm@24574
    97
    else warning ("Redefined ML antiquotation: " ^ quote name);
wenzelm@27343
    98
    Symtab.update (name, scan) tab)));
wenzelm@24574
    99
wenzelm@27343
   100
fun antiquotation src ctxt =
wenzelm@27343
   101
  let val ((name, _), pos) = Args.dest_src src in
wenzelm@27343
   102
    (case Symtab.lookup (! global_parsers) name of
wenzelm@27343
   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@27343
   106
  end;
wenzelm@24574
   107
wenzelm@24574
   108
end;
wenzelm@24574
   109
wenzelm@24574
   110
wenzelm@27343
   111
(* parsing and evaluation *)
wenzelm@24574
   112
wenzelm@27343
   113
local
wenzelm@24574
   114
wenzelm@24574
   115
val antiq =
wenzelm@36950
   116
  Parse.!!! (Parse.position Parse.xname -- Args.parse --| Scan.ahead Parse.eof)
wenzelm@24574
   117
  >> (fn ((x, pos), y) => Args.src ((x, y), pos));
wenzelm@24574
   118
wenzelm@35019
   119
val begin_env = ML_Lex.tokenize "structure Isabelle =\nstruct\n";
wenzelm@30641
   120
val end_env = ML_Lex.tokenize "end;";
wenzelm@31368
   121
val reset_env = ML_Lex.tokenize "structure Isabelle = struct end";
wenzelm@28270
   122
wenzelm@30641
   123
in
wenzelm@30641
   124
wenzelm@30641
   125
fun eval_antiquotes (ants, pos) opt_context =
wenzelm@27343
   126
  let
wenzelm@28270
   127
    val opt_ctxt = Option.map (Context.Proof o Context.proof_of) opt_context;
wenzelm@27343
   128
    val ((ml_env, ml_body), opt_ctxt') =
wenzelm@30641
   129
      if forall Antiquote.is_text ants
wenzelm@30641
   130
      then (([], map (fn Antiquote.Text tok => tok) ants), opt_ctxt)
wenzelm@27343
   131
      else
wenzelm@27343
   132
        let
wenzelm@27343
   133
          val ctxt =
wenzelm@27343
   134
            (case opt_ctxt of
wenzelm@30641
   135
              NONE => error ("No context -- cannot expand ML antiquotations" ^ Position.str_of pos)
wenzelm@28270
   136
            | SOME ctxt => Context.proof_of ctxt);
wenzelm@27378
   137
wenzelm@36950
   138
          val lex = #1 (Keyword.get_lexicons ());
wenzelm@30641
   139
          fun no_decl _ = ([], []);
wenzelm@27378
   140
wenzelm@30641
   141
          fun expand (Antiquote.Text tok) state = (K ([], [tok]), state)
wenzelm@30698
   142
            | expand (Antiquote.Antiq (ss, range)) (scope, background) =
wenzelm@27343
   143
                let
wenzelm@27343
   144
                  val context = Stack.top scope;
wenzelm@36969
   145
                  val (f, context') =
wenzelm@36969
   146
                    antiquotation (Token.read_antiq lex antiq (ss, #1 range)) context;
wenzelm@35019
   147
                  val (decl, background') = f background;
wenzelm@30698
   148
                  val decl' = decl #> pairself (ML_Lex.tokenize #> map (ML_Lex.set_range range));
wenzelm@30641
   149
                in (decl', (Stack.map_top (K context') scope, background')) end
wenzelm@27343
   150
            | expand (Antiquote.Open _) (scope, background) =
wenzelm@27343
   151
                (no_decl, (Stack.push scope, background))
wenzelm@27343
   152
            | expand (Antiquote.Close _) (scope, background) =
wenzelm@27343
   153
                (no_decl, (Stack.pop scope, background));
wenzelm@24574
   154
wenzelm@27343
   155
          val (decls, (_, ctxt')) = fold_map expand ants (Stack.init ctxt, ctxt);
wenzelm@30641
   156
          val ml = decls |> map (fn decl => decl ctxt') |> split_list |> pairself flat;
wenzelm@27343
   157
        in (ml, SOME (Context.Proof ctxt')) end;
wenzelm@30641
   158
  in ((begin_env @ ml_env @ end_env, ml_body), opt_ctxt') end;
wenzelm@24574
   159
wenzelm@32738
   160
val trace = Unsynchronized.ref false;
wenzelm@24574
   161
wenzelm@30687
   162
fun eval verbose pos txt =
wenzelm@24574
   163
  let
wenzelm@28270
   164
    (*prepare source text*)
wenzelm@27878
   165
    val _ = Position.report Markup.ML_source pos;
wenzelm@30647
   166
    val ants = ML_Lex.read_antiq (Symbol_Pos.explode (txt, pos), pos);
wenzelm@31368
   167
    val ((env, body), env_ctxt) = eval_antiquotes (ants, pos) (Context.thread_data ());
wenzelm@31368
   168
    val _ =
wenzelm@31368
   169
      if ! trace then tracing (cat_lines [ML_Lex.flatten env, ML_Lex.flatten body])
wenzelm@31368
   170
      else ();
wenzelm@28270
   171
wenzelm@28270
   172
    (*prepare static ML environment*)
wenzelm@28270
   173
    val _ = Context.setmp_thread_data env_ctxt
wenzelm@31368
   174
        (fn () => (ML_Compiler.eval false Position.none env; Context.thread_data ())) ()
wenzelm@31359
   175
      |> (fn NONE => () | SOME context' => Context.>> (ML_Env.inherit context'));
wenzelm@28270
   176
wenzelm@31368
   177
    val _ = ML_Compiler.eval verbose pos body;
wenzelm@31368
   178
    val _ = ML_Compiler.eval false Position.none reset_env;
wenzelm@28270
   179
  in () end;
wenzelm@24574
   180
wenzelm@24574
   181
end;
wenzelm@24574
   182
wenzelm@24574
   183
wenzelm@30687
   184
(* derived versions *)
wenzelm@24574
   185
wenzelm@26881
   186
fun eval_file path = eval true (Path.position path) (File.read path);
wenzelm@24574
   187
wenzelm@28270
   188
fun eval_in ctxt verbose pos txt =
wenzelm@28270
   189
  Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval verbose pos txt) ();
wenzelm@25204
   190
wenzelm@33233
   191
(* FIXME not thread-safe -- reference can be accessed directly *)
wenzelm@30687
   192
fun evaluate ctxt verbose (ref_name, r) txt = NAMED_CRITICAL "ML" (fn () =>
wenzelm@25204
   193
  let
wenzelm@25204
   194
    val _ = r := NONE;
wenzelm@28270
   195
    val _ = Context.setmp_thread_data (SOME (Context.Proof ctxt)) (fn () =>
wenzelm@30687
   196
      eval verbose Position.none ("val _ = (" ^ ref_name ^ " := SOME (fn () => " ^ txt ^ "))")) ();
wenzelm@25204
   197
  in (case ! r of NONE => error ("Bad evaluation for " ^ ref_name) | SOME e => e) end) ();
wenzelm@24574
   198
wenzelm@26455
   199
fun expression pos bind body txt =
wenzelm@26455
   200
  exec (fn () => eval false pos
wenzelm@26455
   201
    ("Context.set_thread_data (SOME (let " ^ bind ^ " = " ^ txt ^ " in " ^ body ^
wenzelm@26455
   202
      " end (ML_Context.the_generic_context ())));"));
wenzelm@26455
   203
wenzelm@24574
   204
end;
wenzelm@24574
   205
wenzelm@24574
   206
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
wenzelm@24574
   207
open Basic_ML_Context;