src/Pure/ML/ml_context.ML
author wenzelm
Sun Jan 09 19:58:08 2011 +0100 (2011-01-09)
changeset 41485 6c0de045d127
parent 41375 7a89b4b94817
child 42360 da8817d01e7c
permissions -rw-r--r--
ML_trace: observe context visibility flag (import for Latex mode, for example);
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@41375
    28
  val trace_raw: Config.raw
wenzelm@41375
    29
  val trace: bool Config.T
wenzelm@30637
    30
  val eval_antiquotes: ML_Lex.token Antiquote.antiquote list * Position.T ->
wenzelm@30637
    31
    Context.generic option -> (ML_Lex.token list * ML_Lex.token list) * Context.generic option
wenzelm@37198
    32
  val eval: bool -> Position.T -> ML_Lex.token Antiquote.antiquote list -> unit
wenzelm@37198
    33
  val eval_text: bool -> Position.T -> Symbol_Pos.text -> unit
wenzelm@26455
    34
  val eval_file: Path.T -> unit
wenzelm@37198
    35
  val eval_in: Proof.context option -> bool -> Position.T ->
wenzelm@37198
    36
    ML_Lex.token Antiquote.antiquote list -> unit
wenzelm@37198
    37
  val eval_text_in: Proof.context option -> bool -> Position.T -> Symbol_Pos.text -> unit
wenzelm@37198
    38
  val expression: Position.T -> string -> string -> ML_Lex.token Antiquote.antiquote list ->
wenzelm@37198
    39
    Context.generic -> Context.generic
wenzelm@25204
    40
end
wenzelm@24574
    41
wenzelm@24574
    42
structure ML_Context: ML_CONTEXT =
wenzelm@24574
    43
struct
wenzelm@24574
    44
wenzelm@26416
    45
(** implicit ML context **)
wenzelm@24574
    46
wenzelm@26416
    47
val the_generic_context = Context.the_thread_data;
wenzelm@26432
    48
val the_global_context = Context.theory_of o the_generic_context;
wenzelm@24574
    49
val the_local_context = Context.proof_of o the_generic_context;
wenzelm@24574
    50
wenzelm@39164
    51
fun thm name = ProofContext.get_thm (the_local_context ()) name;
wenzelm@39164
    52
fun thms name = ProofContext.get_thms (the_local_context ()) name;
wenzelm@39164
    53
wenzelm@26455
    54
fun exec (e: unit -> unit) context =
wenzelm@26455
    55
  (case Context.setmp_thread_data (SOME context) (fn () => (e (); Context.thread_data ())) () of
wenzelm@26455
    56
    SOME context' => context'
wenzelm@26455
    57
  | NONE => error "Missing context after execution");
wenzelm@26416
    58
wenzelm@26416
    59
wenzelm@26416
    60
(* theorem bindings *)
wenzelm@26416
    61
wenzelm@39140
    62
structure Stored_Thms = Theory_Data
wenzelm@39140
    63
(
wenzelm@39140
    64
  type T = thm list;
wenzelm@39140
    65
  val empty = [];
wenzelm@39140
    66
  fun extend _ = [];
wenzelm@39140
    67
  fun merge _ = [];
wenzelm@39140
    68
);
wenzelm@26416
    69
wenzelm@39140
    70
fun get_stored_thms () = Stored_Thms.get (the_global_context ());
wenzelm@39140
    71
val get_stored_thm = hd o get_stored_thms;
wenzelm@39140
    72
wenzelm@39140
    73
fun ml_store get (name, ths) =
wenzelm@26492
    74
  let
haftmann@29579
    75
    val ths' = Context.>>> (Context.map_theory_result
wenzelm@39557
    76
      (Global_Theory.store_thms (Binding.name name, ths)));
wenzelm@39140
    77
    val _ = Context.>> (Context.map_theory (Stored_Thms.put ths'));
wenzelm@26492
    78
    val _ =
wenzelm@26492
    79
      if name = "" then ()
wenzelm@26492
    80
      else if not (ML_Syntax.is_identifier name) then
wenzelm@26492
    81
        error ("Cannot bind theorem(s) " ^ quote name ^ " as ML value")
wenzelm@39140
    82
      else
wenzelm@31334
    83
        ML_Compiler.eval true Position.none
wenzelm@39140
    84
          (ML_Lex.tokenize ("val " ^ name ^ " = " ^ get ^ " ();"));
wenzelm@39140
    85
    val _ = Context.>> (Context.map_theory (Stored_Thms.put []));
wenzelm@26492
    86
  in () end;
wenzelm@24574
    87
wenzelm@39140
    88
val ml_store_thms = ml_store "ML_Context.get_stored_thms";
wenzelm@39140
    89
fun ml_store_thm (name, th) = ml_store "ML_Context.get_stored_thm" (name, [th]);
wenzelm@26416
    90
wenzelm@35021
    91
fun bind_thm (name, thm) = ml_store_thm (name, Drule.export_without_context thm);
wenzelm@35021
    92
fun bind_thms (name, thms) = ml_store_thms (name, map Drule.export_without_context thms);
wenzelm@24574
    93
wenzelm@24574
    94
wenzelm@24574
    95
wenzelm@24574
    96
(** ML antiquotations **)
wenzelm@24574
    97
wenzelm@27343
    98
(* antiquotation commands *)
wenzelm@27343
    99
wenzelm@35019
   100
type antiq = Proof.context -> (Proof.context -> string * string) * Proof.context;
wenzelm@27343
   101
wenzelm@27343
   102
local
wenzelm@24574
   103
wenzelm@32738
   104
val global_parsers =
wenzelm@32738
   105
  Unsynchronized.ref (Symtab.empty: (Position.T -> antiq context_parser) Symtab.table);
wenzelm@27343
   106
wenzelm@27343
   107
in
wenzelm@24574
   108
wenzelm@27343
   109
fun add_antiq name scan = CRITICAL (fn () =>
wenzelm@32738
   110
  Unsynchronized.change global_parsers (fn tab =>
wenzelm@27343
   111
   (if not (Symtab.defined tab name) then ()
wenzelm@27343
   112
    else warning ("Redefined ML antiquotation: " ^ quote name);
wenzelm@27343
   113
    Symtab.update (name, scan) tab)));
wenzelm@27343
   114
wenzelm@27343
   115
fun antiquotation src ctxt =
wenzelm@24574
   116
  let val ((name, _), pos) = Args.dest_src src in
wenzelm@24574
   117
    (case Symtab.lookup (! global_parsers) name of
wenzelm@24574
   118
      NONE => error ("Unknown ML antiquotation command: " ^ quote name ^ Position.str_of pos)
wenzelm@39507
   119
    | SOME scan =>
wenzelm@39507
   120
       (Context_Position.report ctxt pos (Markup.ML_antiq name);
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@41375
   176
val trace_raw = Config.declare "ML_trace" (fn _ => Config.Bool false);
wenzelm@41375
   177
val trace = Config.bool trace_raw;
wenzelm@24574
   178
wenzelm@37198
   179
fun eval verbose pos ants =
wenzelm@24574
   180
  let
wenzelm@28270
   181
    (*prepare source text*)
wenzelm@41485
   182
    val ((env, body), env_ctxt) = eval_antiquotes (ants, pos) (Context.thread_data ());
wenzelm@31334
   183
    val _ =
wenzelm@41485
   184
      (case Option.map Context.proof_of env_ctxt of
wenzelm@41485
   185
        SOME ctxt =>
wenzelm@41485
   186
          if Config.get ctxt trace then
wenzelm@41485
   187
            Context_Position.if_visible ctxt
wenzelm@41485
   188
              tracing (cat_lines [ML_Lex.flatten env, ML_Lex.flatten body])
wenzelm@41375
   189
          else ()
wenzelm@41375
   190
      | NONE => ());
wenzelm@28270
   191
wenzelm@28270
   192
    (*prepare static ML environment*)
wenzelm@41485
   193
    val _ =
wenzelm@41485
   194
      Context.setmp_thread_data
wenzelm@41485
   195
        (Option.map (Context.mapping I (Context_Position.set_visible false)) env_ctxt)
wenzelm@31334
   196
        (fn () => (ML_Compiler.eval false Position.none env; Context.thread_data ())) ()
wenzelm@31325
   197
      |> (fn NONE => () | SOME context' => Context.>> (ML_Env.inherit context'));
wenzelm@28270
   198
wenzelm@31334
   199
    val _ = ML_Compiler.eval verbose pos body;
wenzelm@31334
   200
    val _ = ML_Compiler.eval false Position.none reset_env;
wenzelm@28270
   201
  in () end;
wenzelm@24574
   202
wenzelm@24574
   203
end;
wenzelm@24574
   204
wenzelm@24574
   205
wenzelm@30672
   206
(* derived versions *)
wenzelm@24574
   207
wenzelm@37198
   208
fun eval_text verbose pos txt = eval verbose pos (ML_Lex.read pos txt);
wenzelm@37198
   209
fun eval_file path = eval_text true (Path.position path) (File.read path);
wenzelm@37198
   210
wenzelm@37198
   211
fun eval_in ctxt verbose pos ants =
wenzelm@37198
   212
  Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval verbose pos ants) ();
wenzelm@24574
   213
wenzelm@37198
   214
fun eval_text_in ctxt verbose pos txt =
wenzelm@37198
   215
  Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval_text verbose pos txt) ();
wenzelm@37198
   216
wenzelm@37198
   217
fun expression pos bind body ants =
wenzelm@37198
   218
  exec (fn () => eval false pos
wenzelm@37198
   219
    (ML_Lex.read Position.none ("Context.set_thread_data (SOME (let " ^ bind ^ " = ") @ ants @
wenzelm@37198
   220
      ML_Lex.read Position.none (" in " ^ body ^ " end (ML_Context.the_generic_context ())));")));
wenzelm@37198
   221
wenzelm@24574
   222
end;
wenzelm@24574
   223
wenzelm@24574
   224
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
wenzelm@24574
   225
open Basic_ML_Context;