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