src/Pure/ML/ml_context.ML
author wenzelm
Fri Aug 16 22:39:31 2013 +0200 (2013-08-16)
changeset 53044 be27b6be8027
parent 52663 6e71d43775e5
child 53167 4e7ddd76e632
permissions -rw-r--r--
more markup via Name_Space.check;
tuned signature;
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@43560
    27
  val add_antiq: binding -> (Position.T -> antiq context_parser) -> theory -> theory
wenzelm@53044
    28
  val check_antiq: theory -> 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@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@43560
   102
structure Antiq_Parsers = Theory_Data
wenzelm@43560
   103
(
wenzelm@43560
   104
  type T = (Position.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@43560
   110
fun add_antiq name scan thy = thy
wenzelm@47005
   111
  |> Antiq_Parsers.map (Name_Space.define (Context.Theory thy) true (name, scan) #> snd);
wenzelm@27343
   112
wenzelm@53044
   113
fun check_antiq thy = #1 o Name_Space.check (Context.Theory thy) (Antiq_Parsers.get thy);
wenzelm@43563
   114
wenzelm@27343
   115
fun antiquotation src ctxt =
wenzelm@43560
   116
  let
wenzelm@43560
   117
    val thy = Proof_Context.theory_of ctxt;
wenzelm@43560
   118
    val ((xname, _), pos) = Args.dest_src src;
wenzelm@47005
   119
    val (_, scan) = Name_Space.check (Context.Proof ctxt) (Antiq_Parsers.get thy) (xname, pos);
wenzelm@50201
   120
  in Args.context_syntax Markup.ML_antiquotationN (scan pos) src ctxt end;
wenzelm@27343
   121
wenzelm@24574
   122
wenzelm@27343
   123
(* parsing and evaluation *)
wenzelm@24574
   124
wenzelm@27343
   125
local
wenzelm@27343
   126
wenzelm@24574
   127
val antiq =
wenzelm@36950
   128
  Parse.!!! (Parse.position Parse.xname -- Args.parse --| Scan.ahead Parse.eof)
wenzelm@24574
   129
  >> (fn ((x, pos), y) => Args.src ((x, y), pos));
wenzelm@24574
   130
wenzelm@48776
   131
val begin_env0 = ML_Lex.tokenize "structure Isabelle =\nstruct\n";
wenzelm@52663
   132
wenzelm@52663
   133
fun begin_env visible =
wenzelm@48776
   134
  ML_Lex.tokenize
wenzelm@52663
   135
    ("structure Isabelle =\nstruct\n\
wenzelm@52663
   136
     \val ML_context = Context_Position.set_visible " ^ Bool.toString visible ^
wenzelm@52663
   137
     " (ML_Context.the_local_context ());\n");
wenzelm@48776
   138
wenzelm@30637
   139
val end_env = ML_Lex.tokenize "end;";
wenzelm@31334
   140
val reset_env = ML_Lex.tokenize "structure Isabelle = struct end";
wenzelm@28270
   141
wenzelm@30637
   142
in
wenzelm@30637
   143
wenzelm@30637
   144
fun eval_antiquotes (ants, pos) opt_context =
wenzelm@24574
   145
  let
wenzelm@52663
   146
    val visible =
wenzelm@52663
   147
      (case opt_context of
wenzelm@52663
   148
        SOME (Context.Proof ctxt) => Context_Position.is_visible ctxt
wenzelm@52663
   149
      | _ => true);
wenzelm@28270
   150
    val opt_ctxt = Option.map (Context.Proof o Context.proof_of) opt_context;
wenzelm@52663
   151
wenzelm@27343
   152
    val ((ml_env, ml_body), opt_ctxt') =
wenzelm@30637
   153
      if forall Antiquote.is_text ants
wenzelm@48776
   154
      then ((begin_env0, map (fn Antiquote.Text tok => tok) ants), opt_ctxt)
wenzelm@27343
   155
      else
wenzelm@27343
   156
        let
wenzelm@27343
   157
          val ctxt =
wenzelm@27343
   158
            (case opt_ctxt of
wenzelm@48992
   159
              NONE => error ("No context -- cannot expand ML antiquotations" ^ Position.here pos)
wenzelm@28270
   160
            | SOME ctxt => Context.proof_of ctxt);
wenzelm@27378
   161
wenzelm@36950
   162
          val lex = #1 (Keyword.get_lexicons ());
wenzelm@30637
   163
          fun no_decl _ = ([], []);
wenzelm@27378
   164
wenzelm@30637
   165
          fun expand (Antiquote.Text tok) state = (K ([], [tok]), state)
wenzelm@30683
   166
            | expand (Antiquote.Antiq (ss, range)) (scope, background) =
wenzelm@27343
   167
                let
wenzelm@27343
   168
                  val context = Stack.top scope;
wenzelm@36959
   169
                  val (f, context') =
wenzelm@36959
   170
                    antiquotation (Token.read_antiq lex antiq (ss, #1 range)) context;
wenzelm@35019
   171
                  val (decl, background') = f background;
wenzelm@30683
   172
                  val decl' = decl #> pairself (ML_Lex.tokenize #> map (ML_Lex.set_range range));
wenzelm@30637
   173
                in (decl', (Stack.map_top (K context') scope, background')) end
wenzelm@27343
   174
            | expand (Antiquote.Open _) (scope, background) =
wenzelm@27343
   175
                (no_decl, (Stack.push scope, background))
wenzelm@27343
   176
            | expand (Antiquote.Close _) (scope, background) =
wenzelm@27343
   177
                (no_decl, (Stack.pop scope, background));
wenzelm@24574
   178
wenzelm@27343
   179
          val (decls, (_, ctxt')) = fold_map expand ants (Stack.init ctxt, ctxt);
wenzelm@52663
   180
          val (ml_env, ml_body) =
wenzelm@52663
   181
            decls |> map (fn decl => decl ctxt') |> split_list |> pairself flat;
wenzelm@52663
   182
        in ((begin_env visible @ ml_env, ml_body), SOME (Context.Proof ctxt')) end;
wenzelm@48776
   183
  in ((ml_env @ end_env, ml_body), opt_ctxt') end;
wenzelm@24574
   184
wenzelm@41375
   185
val trace_raw = Config.declare "ML_trace" (fn _ => Config.Bool false);
wenzelm@41375
   186
val trace = Config.bool trace_raw;
wenzelm@24574
   187
wenzelm@37198
   188
fun eval verbose pos ants =
wenzelm@24574
   189
  let
wenzelm@28270
   190
    (*prepare source text*)
wenzelm@41485
   191
    val ((env, body), env_ctxt) = eval_antiquotes (ants, pos) (Context.thread_data ());
wenzelm@31334
   192
    val _ =
wenzelm@41485
   193
      (case Option.map Context.proof_of env_ctxt of
wenzelm@41485
   194
        SOME ctxt =>
wenzelm@41485
   195
          if Config.get ctxt trace then
wenzelm@41485
   196
            Context_Position.if_visible ctxt
wenzelm@41485
   197
              tracing (cat_lines [ML_Lex.flatten env, ML_Lex.flatten body])
wenzelm@41375
   198
          else ()
wenzelm@41375
   199
      | NONE => ());
wenzelm@28270
   200
wenzelm@28270
   201
    (*prepare static ML environment*)
wenzelm@41485
   202
    val _ =
wenzelm@41485
   203
      Context.setmp_thread_data
wenzelm@41485
   204
        (Option.map (Context.mapping I (Context_Position.set_visible false)) env_ctxt)
wenzelm@31334
   205
        (fn () => (ML_Compiler.eval false Position.none env; Context.thread_data ())) ()
wenzelm@31325
   206
      |> (fn NONE => () | SOME context' => Context.>> (ML_Env.inherit context'));
wenzelm@28270
   207
wenzelm@31334
   208
    val _ = ML_Compiler.eval verbose pos body;
wenzelm@31334
   209
    val _ = ML_Compiler.eval false Position.none reset_env;
wenzelm@28270
   210
  in () end;
wenzelm@24574
   211
wenzelm@24574
   212
end;
wenzelm@24574
   213
wenzelm@24574
   214
wenzelm@30672
   215
(* derived versions *)
wenzelm@24574
   216
wenzelm@37198
   217
fun eval_text verbose pos txt = eval verbose pos (ML_Lex.read pos txt);
wenzelm@37198
   218
wenzelm@37198
   219
fun eval_in ctxt verbose pos ants =
wenzelm@37198
   220
  Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval verbose pos ants) ();
wenzelm@24574
   221
wenzelm@37198
   222
fun eval_text_in ctxt verbose pos txt =
wenzelm@37198
   223
  Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval_text verbose pos txt) ();
wenzelm@37198
   224
wenzelm@37198
   225
fun expression pos bind body ants =
wenzelm@37198
   226
  exec (fn () => eval false pos
wenzelm@37198
   227
    (ML_Lex.read Position.none ("Context.set_thread_data (SOME (let " ^ bind ^ " = ") @ ants @
wenzelm@37198
   228
      ML_Lex.read Position.none (" in " ^ body ^ " end (ML_Context.the_generic_context ())));")));
wenzelm@37198
   229
wenzelm@24574
   230
end;
wenzelm@24574
   231
wenzelm@24574
   232
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
wenzelm@24574
   233
open Basic_ML_Context;