src/Pure/ML/ml_context.ML
author wenzelm
Wed Aug 29 11:48:45 2012 +0200 (2012-08-29)
changeset 48992 0518bf89c777
parent 48781 21af4b8103fa
child 50201 c26369c9eda6
permissions -rw-r--r--
renamed Position.str_of to Position.here;
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@43563
    28
  val intern_antiq: theory -> xstring -> string
wenzelm@43563
    29
  val defined_antiq: theory -> string -> bool
wenzelm@41375
    30
  val trace_raw: Config.raw
wenzelm@41375
    31
  val trace: bool Config.T
wenzelm@30637
    32
  val eval_antiquotes: ML_Lex.token Antiquote.antiquote list * Position.T ->
wenzelm@30637
    33
    Context.generic option -> (ML_Lex.token list * ML_Lex.token list) * Context.generic option
wenzelm@37198
    34
  val eval: bool -> Position.T -> ML_Lex.token Antiquote.antiquote list -> unit
wenzelm@37198
    35
  val eval_text: bool -> Position.T -> Symbol_Pos.text -> unit
wenzelm@37198
    36
  val eval_in: Proof.context option -> bool -> Position.T ->
wenzelm@37198
    37
    ML_Lex.token Antiquote.antiquote list -> unit
wenzelm@37198
    38
  val eval_text_in: Proof.context option -> bool -> Position.T -> Symbol_Pos.text -> unit
wenzelm@37198
    39
  val expression: Position.T -> string -> string -> ML_Lex.token Antiquote.antiquote list ->
wenzelm@37198
    40
    Context.generic -> Context.generic
wenzelm@25204
    41
end
wenzelm@24574
    42
wenzelm@24574
    43
structure ML_Context: ML_CONTEXT =
wenzelm@24574
    44
struct
wenzelm@24574
    45
wenzelm@26416
    46
(** implicit ML context **)
wenzelm@24574
    47
wenzelm@26416
    48
val the_generic_context = Context.the_thread_data;
wenzelm@26432
    49
val the_global_context = Context.theory_of o the_generic_context;
wenzelm@24574
    50
val the_local_context = Context.proof_of o the_generic_context;
wenzelm@24574
    51
wenzelm@42360
    52
fun thm name = Proof_Context.get_thm (the_local_context ()) name;
wenzelm@42360
    53
fun thms name = Proof_Context.get_thms (the_local_context ()) name;
wenzelm@39164
    54
wenzelm@26455
    55
fun exec (e: unit -> unit) context =
wenzelm@26455
    56
  (case Context.setmp_thread_data (SOME context) (fn () => (e (); Context.thread_data ())) () of
wenzelm@26455
    57
    SOME context' => context'
wenzelm@26455
    58
  | NONE => error "Missing context after execution");
wenzelm@26416
    59
wenzelm@26416
    60
wenzelm@26416
    61
(* theorem bindings *)
wenzelm@26416
    62
wenzelm@39140
    63
structure Stored_Thms = Theory_Data
wenzelm@39140
    64
(
wenzelm@39140
    65
  type T = thm list;
wenzelm@39140
    66
  val empty = [];
wenzelm@39140
    67
  fun extend _ = [];
wenzelm@39140
    68
  fun merge _ = [];
wenzelm@39140
    69
);
wenzelm@26416
    70
wenzelm@39140
    71
fun get_stored_thms () = Stored_Thms.get (the_global_context ());
wenzelm@39140
    72
val get_stored_thm = hd o get_stored_thms;
wenzelm@39140
    73
wenzelm@39140
    74
fun ml_store get (name, ths) =
wenzelm@26492
    75
  let
haftmann@29579
    76
    val ths' = Context.>>> (Context.map_theory_result
wenzelm@39557
    77
      (Global_Theory.store_thms (Binding.name name, ths)));
wenzelm@39140
    78
    val _ = Context.>> (Context.map_theory (Stored_Thms.put ths'));
wenzelm@26492
    79
    val _ =
wenzelm@26492
    80
      if name = "" then ()
wenzelm@26492
    81
      else if not (ML_Syntax.is_identifier name) then
wenzelm@26492
    82
        error ("Cannot bind theorem(s) " ^ quote name ^ " as ML value")
wenzelm@39140
    83
      else
wenzelm@31334
    84
        ML_Compiler.eval true Position.none
wenzelm@39140
    85
          (ML_Lex.tokenize ("val " ^ name ^ " = " ^ get ^ " ();"));
wenzelm@39140
    86
    val _ = Context.>> (Context.map_theory (Stored_Thms.put []));
wenzelm@26492
    87
  in () end;
wenzelm@24574
    88
wenzelm@39140
    89
val ml_store_thms = ml_store "ML_Context.get_stored_thms";
wenzelm@39140
    90
fun ml_store_thm (name, th) = ml_store "ML_Context.get_stored_thm" (name, [th]);
wenzelm@26416
    91
wenzelm@35021
    92
fun bind_thm (name, thm) = ml_store_thm (name, Drule.export_without_context thm);
wenzelm@35021
    93
fun bind_thms (name, thms) = ml_store_thms (name, map Drule.export_without_context thms);
wenzelm@24574
    94
wenzelm@24574
    95
wenzelm@24574
    96
wenzelm@24574
    97
(** ML antiquotations **)
wenzelm@24574
    98
wenzelm@27343
    99
(* antiquotation commands *)
wenzelm@27343
   100
wenzelm@35019
   101
type antiq = Proof.context -> (Proof.context -> string * string) * Proof.context;
wenzelm@27343
   102
wenzelm@43560
   103
structure Antiq_Parsers = Theory_Data
wenzelm@43560
   104
(
wenzelm@43560
   105
  type T = (Position.T -> antiq context_parser) Name_Space.table;
wenzelm@45666
   106
  val empty : T = Name_Space.empty_table Isabelle_Markup.ML_antiquotationN;
wenzelm@43560
   107
  val extend = I;
wenzelm@43560
   108
  fun merge data : T = Name_Space.merge_tables data;
wenzelm@43560
   109
);
wenzelm@27343
   110
wenzelm@43560
   111
fun add_antiq name scan thy = thy
wenzelm@47005
   112
  |> Antiq_Parsers.map (Name_Space.define (Context.Theory thy) true (name, scan) #> snd);
wenzelm@27343
   113
wenzelm@43563
   114
val intern_antiq = Name_Space.intern o #1 o Antiq_Parsers.get;
wenzelm@43563
   115
val defined_antiq = Symtab.defined o #2 o Antiq_Parsers.get;
wenzelm@43563
   116
wenzelm@27343
   117
fun antiquotation src ctxt =
wenzelm@43560
   118
  let
wenzelm@43560
   119
    val thy = Proof_Context.theory_of ctxt;
wenzelm@43560
   120
    val ((xname, _), pos) = Args.dest_src src;
wenzelm@47005
   121
    val (_, scan) = Name_Space.check (Context.Proof ctxt) (Antiq_Parsers.get thy) (xname, pos);
wenzelm@45666
   122
  in Args.context_syntax Isabelle_Markup.ML_antiquotationN (scan pos) src ctxt end;
wenzelm@27343
   123
wenzelm@24574
   124
wenzelm@27343
   125
(* parsing and evaluation *)
wenzelm@24574
   126
wenzelm@27343
   127
local
wenzelm@27343
   128
wenzelm@24574
   129
val antiq =
wenzelm@36950
   130
  Parse.!!! (Parse.position Parse.xname -- Args.parse --| Scan.ahead Parse.eof)
wenzelm@24574
   131
  >> (fn ((x, pos), y) => Args.src ((x, y), pos));
wenzelm@24574
   132
wenzelm@48776
   133
val begin_env0 = ML_Lex.tokenize "structure Isabelle =\nstruct\n";
wenzelm@48776
   134
val begin_env =
wenzelm@48776
   135
  ML_Lex.tokenize
wenzelm@48781
   136
    "structure Isabelle =\nstruct\nval ML_context = ML_Context.the_local_context ();\n";
wenzelm@48776
   137
wenzelm@30637
   138
val end_env = ML_Lex.tokenize "end;";
wenzelm@31334
   139
val reset_env = ML_Lex.tokenize "structure Isabelle = struct end";
wenzelm@28270
   140
wenzelm@30637
   141
in
wenzelm@30637
   142
wenzelm@30637
   143
fun eval_antiquotes (ants, pos) opt_context =
wenzelm@24574
   144
  let
wenzelm@28270
   145
    val opt_ctxt = Option.map (Context.Proof o Context.proof_of) opt_context;
wenzelm@27343
   146
    val ((ml_env, ml_body), opt_ctxt') =
wenzelm@30637
   147
      if forall Antiquote.is_text ants
wenzelm@48776
   148
      then ((begin_env0, map (fn Antiquote.Text tok => tok) ants), opt_ctxt)
wenzelm@27343
   149
      else
wenzelm@27343
   150
        let
wenzelm@27343
   151
          val ctxt =
wenzelm@27343
   152
            (case opt_ctxt of
wenzelm@48992
   153
              NONE => error ("No context -- cannot expand ML antiquotations" ^ Position.here pos)
wenzelm@28270
   154
            | SOME ctxt => Context.proof_of ctxt);
wenzelm@27378
   155
wenzelm@36950
   156
          val lex = #1 (Keyword.get_lexicons ());
wenzelm@30637
   157
          fun no_decl _ = ([], []);
wenzelm@27378
   158
wenzelm@30637
   159
          fun expand (Antiquote.Text tok) state = (K ([], [tok]), state)
wenzelm@30683
   160
            | expand (Antiquote.Antiq (ss, range)) (scope, background) =
wenzelm@27343
   161
                let
wenzelm@27343
   162
                  val context = Stack.top scope;
wenzelm@36959
   163
                  val (f, context') =
wenzelm@36959
   164
                    antiquotation (Token.read_antiq lex antiq (ss, #1 range)) context;
wenzelm@35019
   165
                  val (decl, background') = f background;
wenzelm@30683
   166
                  val decl' = decl #> pairself (ML_Lex.tokenize #> map (ML_Lex.set_range range));
wenzelm@30637
   167
                in (decl', (Stack.map_top (K context') scope, background')) end
wenzelm@27343
   168
            | expand (Antiquote.Open _) (scope, background) =
wenzelm@27343
   169
                (no_decl, (Stack.push scope, background))
wenzelm@27343
   170
            | expand (Antiquote.Close _) (scope, background) =
wenzelm@27343
   171
                (no_decl, (Stack.pop scope, background));
wenzelm@24574
   172
wenzelm@27343
   173
          val (decls, (_, ctxt')) = fold_map expand ants (Stack.init ctxt, ctxt);
wenzelm@48776
   174
          val (ml_env, ml_body) = decls |> map (fn decl => decl ctxt') |> split_list |> pairself flat;
wenzelm@48776
   175
        in ((begin_env @ ml_env, ml_body), SOME (Context.Proof ctxt')) end;
wenzelm@48776
   176
  in ((ml_env @ end_env, ml_body), opt_ctxt') end;
wenzelm@24574
   177
wenzelm@41375
   178
val trace_raw = Config.declare "ML_trace" (fn _ => Config.Bool false);
wenzelm@41375
   179
val trace = Config.bool trace_raw;
wenzelm@24574
   180
wenzelm@37198
   181
fun eval verbose pos ants =
wenzelm@24574
   182
  let
wenzelm@28270
   183
    (*prepare source text*)
wenzelm@41485
   184
    val ((env, body), env_ctxt) = eval_antiquotes (ants, pos) (Context.thread_data ());
wenzelm@31334
   185
    val _ =
wenzelm@41485
   186
      (case Option.map Context.proof_of env_ctxt of
wenzelm@41485
   187
        SOME ctxt =>
wenzelm@41485
   188
          if Config.get ctxt trace then
wenzelm@41485
   189
            Context_Position.if_visible ctxt
wenzelm@41485
   190
              tracing (cat_lines [ML_Lex.flatten env, ML_Lex.flatten body])
wenzelm@41375
   191
          else ()
wenzelm@41375
   192
      | NONE => ());
wenzelm@28270
   193
wenzelm@28270
   194
    (*prepare static ML environment*)
wenzelm@41485
   195
    val _ =
wenzelm@41485
   196
      Context.setmp_thread_data
wenzelm@41485
   197
        (Option.map (Context.mapping I (Context_Position.set_visible false)) env_ctxt)
wenzelm@31334
   198
        (fn () => (ML_Compiler.eval false Position.none env; Context.thread_data ())) ()
wenzelm@31325
   199
      |> (fn NONE => () | SOME context' => Context.>> (ML_Env.inherit context'));
wenzelm@28270
   200
wenzelm@31334
   201
    val _ = ML_Compiler.eval verbose pos body;
wenzelm@31334
   202
    val _ = ML_Compiler.eval false Position.none reset_env;
wenzelm@28270
   203
  in () end;
wenzelm@24574
   204
wenzelm@24574
   205
end;
wenzelm@24574
   206
wenzelm@24574
   207
wenzelm@30672
   208
(* derived versions *)
wenzelm@24574
   209
wenzelm@37198
   210
fun eval_text verbose pos txt = eval verbose pos (ML_Lex.read pos txt);
wenzelm@37198
   211
wenzelm@37198
   212
fun eval_in ctxt verbose pos ants =
wenzelm@37198
   213
  Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval verbose pos ants) ();
wenzelm@24574
   214
wenzelm@37198
   215
fun eval_text_in ctxt verbose pos txt =
wenzelm@37198
   216
  Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval_text verbose pos txt) ();
wenzelm@37198
   217
wenzelm@37198
   218
fun expression pos bind body ants =
wenzelm@37198
   219
  exec (fn () => eval false pos
wenzelm@37198
   220
    (ML_Lex.read Position.none ("Context.set_thread_data (SOME (let " ^ bind ^ " = ") @ ants @
wenzelm@37198
   221
      ML_Lex.read Position.none (" in " ^ body ^ " end (ML_Context.the_generic_context ())));")));
wenzelm@37198
   222
wenzelm@24574
   223
end;
wenzelm@24574
   224
wenzelm@24574
   225
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
wenzelm@24574
   226
open Basic_ML_Context;