src/Pure/ML/ml_context.ML
author wenzelm
Tue Feb 25 14:56:58 2014 +0100 (2014-02-25)
changeset 55743 225a060e7445
parent 55526 39708e59f4b0
child 55828 42ac3cfb89f6
permissions -rw-r--r--
proper context for global data;
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@53169
    26
  type antiq = Proof.context -> string * string
wenzelm@53169
    27
  val add_antiq: binding -> antiq context_parser -> theory -> theory
wenzelm@55743
    28
  val check_antiq: Proof.context -> xstring * Position.T -> string
wenzelm@41375
    29
  val trace_raw: Config.raw
wenzelm@41375
    30
  val trace: bool Config.T
wenzelm@30637
    31
  val eval_antiquotes: ML_Lex.token Antiquote.antiquote list * Position.T ->
wenzelm@30637
    32
    Context.generic option -> (ML_Lex.token list * ML_Lex.token list) * Context.generic option
wenzelm@37198
    33
  val eval: bool -> Position.T -> ML_Lex.token Antiquote.antiquote list -> unit
wenzelm@37198
    34
  val eval_text: bool -> Position.T -> Symbol_Pos.text -> 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@42360
    51
fun thm name = Proof_Context.get_thm (the_local_context ()) name;
wenzelm@42360
    52
fun thms name = Proof_Context.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@53171
    77
    val _ = Theory.setup (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@53171
    85
    val _ = Theory.setup (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@53169
   100
type antiq = Proof.context -> string * string;
wenzelm@27343
   101
wenzelm@43560
   102
structure Antiq_Parsers = Theory_Data
wenzelm@43560
   103
(
wenzelm@53169
   104
  type T = antiq context_parser Name_Space.table;
wenzelm@50201
   105
  val empty : T = Name_Space.empty_table Markup.ML_antiquotationN;
wenzelm@43560
   106
  val extend = I;
wenzelm@43560
   107
  fun merge data : T = Name_Space.merge_tables data;
wenzelm@43560
   108
);
wenzelm@27343
   109
wenzelm@55743
   110
val get_antiqs = Antiq_Parsers.get o Proof_Context.theory_of;
wenzelm@55743
   111
wenzelm@43560
   112
fun add_antiq name scan thy = thy
wenzelm@47005
   113
  |> Antiq_Parsers.map (Name_Space.define (Context.Theory thy) true (name, scan) #> snd);
wenzelm@27343
   114
wenzelm@55743
   115
fun check_antiq ctxt = #1 o Name_Space.check (Context.Proof ctxt) (get_antiqs ctxt);
wenzelm@43563
   116
wenzelm@27343
   117
fun antiquotation src ctxt =
wenzelm@43560
   118
  let
wenzelm@43560
   119
    val ((xname, _), pos) = Args.dest_src src;
wenzelm@55743
   120
    val (_, scan) = Name_Space.check (Context.Proof ctxt) (get_antiqs ctxt) (xname, pos);
wenzelm@53169
   121
  in Args.context_syntax Markup.ML_antiquotationN scan src ctxt end;
wenzelm@27343
   122
wenzelm@24574
   123
wenzelm@27343
   124
(* parsing and evaluation *)
wenzelm@24574
   125
wenzelm@27343
   126
local
wenzelm@27343
   127
wenzelm@24574
   128
val antiq =
wenzelm@36950
   129
  Parse.!!! (Parse.position Parse.xname -- Args.parse --| Scan.ahead Parse.eof)
wenzelm@24574
   130
  >> (fn ((x, pos), y) => Args.src ((x, y), pos));
wenzelm@24574
   131
wenzelm@48776
   132
val begin_env0 = ML_Lex.tokenize "structure Isabelle =\nstruct\n";
wenzelm@52663
   133
wenzelm@52663
   134
fun begin_env visible =
wenzelm@48776
   135
  ML_Lex.tokenize
wenzelm@52663
   136
    ("structure Isabelle =\nstruct\n\
wenzelm@52663
   137
     \val ML_context = Context_Position.set_visible " ^ Bool.toString visible ^
wenzelm@52663
   138
     " (ML_Context.the_local_context ());\n");
wenzelm@48776
   139
wenzelm@30637
   140
val end_env = ML_Lex.tokenize "end;";
wenzelm@31334
   141
val reset_env = ML_Lex.tokenize "structure Isabelle = struct end";
wenzelm@28270
   142
wenzelm@30637
   143
in
wenzelm@30637
   144
wenzelm@30637
   145
fun eval_antiquotes (ants, pos) opt_context =
wenzelm@24574
   146
  let
wenzelm@52663
   147
    val visible =
wenzelm@52663
   148
      (case opt_context of
wenzelm@52663
   149
        SOME (Context.Proof ctxt) => Context_Position.is_visible ctxt
wenzelm@52663
   150
      | _ => true);
wenzelm@28270
   151
    val opt_ctxt = Option.map (Context.Proof o Context.proof_of) opt_context;
wenzelm@52663
   152
wenzelm@27343
   153
    val ((ml_env, ml_body), opt_ctxt') =
wenzelm@30637
   154
      if forall Antiquote.is_text ants
wenzelm@48776
   155
      then ((begin_env0, map (fn Antiquote.Text tok => tok) ants), opt_ctxt)
wenzelm@27343
   156
      else
wenzelm@27343
   157
        let
wenzelm@53167
   158
          val lex = #1 (Keyword.get_lexicons ());
wenzelm@53167
   159
          fun no_decl _ = ([], []);
wenzelm@53167
   160
wenzelm@53167
   161
          fun expand (Antiquote.Text tok) ctxt = (K ([], [tok]), ctxt)
wenzelm@55526
   162
            | expand (Antiquote.Antiq (ss, {range, ...})) ctxt =
wenzelm@53167
   163
                let
wenzelm@53169
   164
                  val (decl, ctxt') = antiquotation (Token.read_antiq lex antiq (ss, #1 range)) ctxt;
wenzelm@53167
   165
                  val decl' = decl #> pairself (ML_Lex.tokenize #> map (ML_Lex.set_range range));
wenzelm@53167
   166
                in (decl', ctxt') end;
wenzelm@53167
   167
wenzelm@27343
   168
          val ctxt =
wenzelm@27343
   169
            (case opt_ctxt of
wenzelm@48992
   170
              NONE => error ("No context -- cannot expand ML antiquotations" ^ Position.here pos)
wenzelm@28270
   171
            | SOME ctxt => Context.proof_of ctxt);
wenzelm@27378
   172
wenzelm@53167
   173
          val (decls, ctxt') = fold_map expand ants ctxt;
wenzelm@52663
   174
          val (ml_env, ml_body) =
wenzelm@52663
   175
            decls |> map (fn decl => decl ctxt') |> split_list |> pairself flat;
wenzelm@52663
   176
        in ((begin_env visible @ ml_env, ml_body), SOME (Context.Proof ctxt')) end;
wenzelm@48776
   177
  in ((ml_env @ end_env, ml_body), opt_ctxt') end;
wenzelm@24574
   178
wenzelm@41375
   179
val trace_raw = Config.declare "ML_trace" (fn _ => Config.Bool false);
wenzelm@41375
   180
val trace = Config.bool trace_raw;
wenzelm@24574
   181
wenzelm@37198
   182
fun eval verbose pos ants =
wenzelm@24574
   183
  let
wenzelm@28270
   184
    (*prepare source text*)
wenzelm@41485
   185
    val ((env, body), env_ctxt) = eval_antiquotes (ants, pos) (Context.thread_data ());
wenzelm@31334
   186
    val _ =
wenzelm@41485
   187
      (case Option.map Context.proof_of env_ctxt of
wenzelm@41485
   188
        SOME ctxt =>
wenzelm@41485
   189
          if Config.get ctxt trace then
wenzelm@41485
   190
            Context_Position.if_visible ctxt
wenzelm@41485
   191
              tracing (cat_lines [ML_Lex.flatten env, ML_Lex.flatten body])
wenzelm@41375
   192
          else ()
wenzelm@41375
   193
      | NONE => ());
wenzelm@28270
   194
wenzelm@28270
   195
    (*prepare static ML environment*)
wenzelm@41485
   196
    val _ =
wenzelm@41485
   197
      Context.setmp_thread_data
wenzelm@41485
   198
        (Option.map (Context.mapping I (Context_Position.set_visible false)) env_ctxt)
wenzelm@31334
   199
        (fn () => (ML_Compiler.eval false Position.none env; Context.thread_data ())) ()
wenzelm@31325
   200
      |> (fn NONE => () | SOME context' => Context.>> (ML_Env.inherit context'));
wenzelm@28270
   201
wenzelm@31334
   202
    val _ = ML_Compiler.eval verbose pos body;
wenzelm@31334
   203
    val _ = ML_Compiler.eval false Position.none reset_env;
wenzelm@28270
   204
  in () end;
wenzelm@24574
   205
wenzelm@24574
   206
end;
wenzelm@24574
   207
wenzelm@24574
   208
wenzelm@30672
   209
(* derived versions *)
wenzelm@24574
   210
wenzelm@37198
   211
fun eval_text verbose pos txt = eval verbose pos (ML_Lex.read pos txt);
wenzelm@37198
   212
wenzelm@37198
   213
fun eval_in ctxt verbose pos ants =
wenzelm@37198
   214
  Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval verbose pos ants) ();
wenzelm@24574
   215
wenzelm@37198
   216
fun eval_text_in ctxt verbose pos txt =
wenzelm@37198
   217
  Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval_text verbose pos txt) ();
wenzelm@37198
   218
wenzelm@37198
   219
fun expression pos bind body ants =
wenzelm@37198
   220
  exec (fn () => eval false pos
wenzelm@37198
   221
    (ML_Lex.read Position.none ("Context.set_thread_data (SOME (let " ^ bind ^ " = ") @ ants @
wenzelm@37198
   222
      ML_Lex.read Position.none (" in " ^ body ^ " end (ML_Context.the_generic_context ())));")));
wenzelm@37198
   223
wenzelm@24574
   224
end;
wenzelm@24574
   225
wenzelm@24574
   226
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
wenzelm@24574
   227
open Basic_ML_Context;