src/Pure/ML/ml_context.ML
author wenzelm
Fri Mar 13 21:25:15 2009 +0100 (2009-03-13)
changeset 30513 1796b8ea88aa
parent 29606 fedb8be05f24
child 30573 49899f26fbd1
permissions -rw-r--r--
eliminated type Args.T;
pervasive types 'a parser and 'a context_parser;
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@24574
    11
  val thm: xstring -> thm
wenzelm@24574
    12
  val thms: xstring -> thm list
wenzelm@25204
    13
end
wenzelm@24574
    14
wenzelm@24574
    15
signature ML_CONTEXT =
wenzelm@24574
    16
sig
wenzelm@24574
    17
  include BASIC_ML_CONTEXT
wenzelm@24574
    18
  val the_generic_context: unit -> Context.generic
wenzelm@26432
    19
  val the_global_context: unit -> theory
wenzelm@24574
    20
  val the_local_context: unit -> Proof.context
wenzelm@26455
    21
  val exec: (unit -> unit) -> Context.generic -> Context.generic
wenzelm@28279
    22
  val inherit_env: Proof.context -> Proof.context -> Proof.context
wenzelm@28270
    23
  val name_space: ML_NameSpace.nameSpace
wenzelm@26416
    24
  val stored_thms: thm list ref
wenzelm@26416
    25
  val ml_store_thm: string * thm -> unit
wenzelm@26416
    26
  val ml_store_thms: string * thm list -> unit
wenzelm@27343
    27
  type antiq =
wenzelm@27343
    28
    {struct_name: string, background: Proof.context} ->
wenzelm@27343
    29
      (Proof.context -> string * string) * Proof.context
wenzelm@30513
    30
  val add_antiq: string -> (Position.T -> antiq context_parser) -> unit
wenzelm@24574
    31
  val trace: bool ref
berghofe@27723
    32
  val eval_wrapper: (string -> unit) * (string -> 'a) -> bool -> Position.T -> string -> unit
wenzelm@26455
    33
  val eval: bool -> Position.T -> string -> unit
wenzelm@26455
    34
  val eval_file: Path.T -> unit
wenzelm@28270
    35
  val eval_in: Proof.context option -> bool -> Position.T -> string -> unit
wenzelm@28270
    36
  val evaluate: Proof.context -> (string -> unit) * (string -> 'b) -> bool ->
wenzelm@25204
    37
    string * (unit -> 'a) option ref -> string -> 'a
wenzelm@26455
    38
  val expression: Position.T -> string -> string -> string -> Context.generic -> Context.generic
wenzelm@25204
    39
end
wenzelm@24574
    40
wenzelm@24574
    41
structure ML_Context: ML_CONTEXT =
wenzelm@24574
    42
struct
wenzelm@24574
    43
wenzelm@26416
    44
(** implicit ML context **)
wenzelm@24574
    45
wenzelm@26416
    46
val the_generic_context = Context.the_thread_data;
wenzelm@26432
    47
val the_global_context = Context.theory_of o the_generic_context;
wenzelm@24574
    48
val the_local_context = Context.proof_of o the_generic_context;
wenzelm@24574
    49
wenzelm@26455
    50
fun exec (e: unit -> unit) context =
wenzelm@26455
    51
  (case Context.setmp_thread_data (SOME context) (fn () => (e (); Context.thread_data ())) () of
wenzelm@26455
    52
    SOME context' => context'
wenzelm@26455
    53
  | NONE => error "Missing context after execution");
wenzelm@26416
    54
wenzelm@26416
    55
wenzelm@28270
    56
(* ML name space *)
wenzelm@28270
    57
wenzelm@28270
    58
structure ML_Env = GenericDataFun
wenzelm@28270
    59
(
wenzelm@28270
    60
  type T =
wenzelm@28270
    61
    ML_NameSpace.valueVal Symtab.table *
wenzelm@28270
    62
    ML_NameSpace.typeVal Symtab.table *
wenzelm@28270
    63
    ML_NameSpace.fixityVal Symtab.table *
wenzelm@28270
    64
    ML_NameSpace.structureVal Symtab.table *
wenzelm@28270
    65
    ML_NameSpace.signatureVal Symtab.table *
wenzelm@28270
    66
    ML_NameSpace.functorVal Symtab.table;
wenzelm@28270
    67
  val empty = (Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty);
wenzelm@28270
    68
  val extend = I;
wenzelm@28270
    69
  fun merge _
wenzelm@28270
    70
   ((val1, type1, fixity1, structure1, signature1, functor1),
wenzelm@28270
    71
    (val2, type2, fixity2, structure2, signature2, functor2)) : T =
wenzelm@28270
    72
   (Symtab.merge (K true) (val1, val2),
wenzelm@28270
    73
    Symtab.merge (K true) (type1, type2),
wenzelm@28270
    74
    Symtab.merge (K true) (fixity1, fixity2),
wenzelm@28270
    75
    Symtab.merge (K true) (structure1, structure2),
wenzelm@28270
    76
    Symtab.merge (K true) (signature1, signature2),
wenzelm@28270
    77
    Symtab.merge (K true) (functor1, functor2));
wenzelm@28270
    78
);
wenzelm@28270
    79
wenzelm@28279
    80
val inherit_env = Context.proof_map o ML_Env.put o ML_Env.get o Context.Proof;
wenzelm@28279
    81
wenzelm@28270
    82
val name_space: ML_NameSpace.nameSpace =
wenzelm@28270
    83
  let
wenzelm@28270
    84
    fun lookup sel1 sel2 name =
wenzelm@28270
    85
      Context.thread_data ()
wenzelm@28270
    86
      |> (fn NONE => NONE | SOME context => Symtab.lookup (sel1 (ML_Env.get context)) name)
wenzelm@28270
    87
      |> (fn NONE => sel2 ML_NameSpace.global name | some => some);
wenzelm@28270
    88
wenzelm@28270
    89
    fun all sel1 sel2 () =
wenzelm@28270
    90
      Context.thread_data ()
wenzelm@28270
    91
      |> (fn NONE => [] | SOME context => Symtab.dest (sel1 (ML_Env.get context)))
wenzelm@28270
    92
      |> append (sel2 ML_NameSpace.global ())
wenzelm@28270
    93
      |> sort_distinct (string_ord o pairself #1);
wenzelm@28270
    94
wenzelm@28270
    95
    fun enter ap1 sel2 entry =
wenzelm@28270
    96
      if is_some (Context.thread_data ()) then
wenzelm@28270
    97
        Context.>> (ML_Env.map (ap1 (Symtab.update entry)))
wenzelm@28270
    98
      else sel2 ML_NameSpace.global entry;
wenzelm@28270
    99
  in
wenzelm@28270
   100
   {lookupVal    = lookup #1 #lookupVal,
wenzelm@28270
   101
    lookupType   = lookup #2 #lookupType,
wenzelm@28270
   102
    lookupFix    = lookup #3 #lookupFix,
wenzelm@28270
   103
    lookupStruct = lookup #4 #lookupStruct,
wenzelm@28270
   104
    lookupSig    = lookup #5 #lookupSig,
wenzelm@28270
   105
    lookupFunct  = lookup #6 #lookupFunct,
wenzelm@28270
   106
    enterVal     = enter (fn h => fn (a, b, c, d, e, f) => (h a, b, c, d, e, f)) #enterVal,
wenzelm@28270
   107
    enterType    = enter (fn h => fn (a, b, c, d, e, f) => (a, h b, c, d, e, f)) #enterType,
wenzelm@28270
   108
    enterFix     = enter (fn h => fn (a, b, c, d, e, f) => (a, b, h c, d, e, f)) #enterFix,
wenzelm@28270
   109
    enterStruct  = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, h d, e, f)) #enterStruct,
wenzelm@28270
   110
    enterSig     = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, d, h e, f)) #enterSig,
wenzelm@28270
   111
    enterFunct   = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, d, e, h f)) #enterFunct,
wenzelm@28270
   112
    allVal       = all #1 #allVal,
wenzelm@28270
   113
    allType      = all #2 #allType,
wenzelm@28270
   114
    allFix       = all #3 #allFix,
wenzelm@28270
   115
    allStruct    = all #4 #allStruct,
wenzelm@28270
   116
    allSig       = all #5 #allSig,
wenzelm@28270
   117
    allFunct     = all #6 #allFunct}
wenzelm@28270
   118
  end;
wenzelm@28270
   119
wenzelm@28270
   120
wenzelm@26416
   121
(* theorem bindings *)
wenzelm@26416
   122
wenzelm@26416
   123
val stored_thms: thm list ref = ref [];
wenzelm@26416
   124
wenzelm@26492
   125
fun ml_store sel (name, ths) =
wenzelm@26492
   126
  let
haftmann@29579
   127
    val ths' = Context.>>> (Context.map_theory_result
haftmann@29579
   128
      (PureThy.store_thms (Binding.name name, ths)));
wenzelm@26492
   129
    val _ =
wenzelm@26492
   130
      if name = "" then ()
wenzelm@26492
   131
      else if not (ML_Syntax.is_identifier name) then
wenzelm@26492
   132
        error ("Cannot bind theorem(s) " ^ quote name ^ " as ML value")
wenzelm@26492
   133
      else setmp stored_thms ths' (fn () =>
wenzelm@28270
   134
        use_text name_space (0, "") Output.ml_output true
wenzelm@26492
   135
          ("val " ^ name ^ " = " ^ sel ^ "(! ML_Context.stored_thms);")) ();
wenzelm@26492
   136
  in () end;
wenzelm@24574
   137
wenzelm@26492
   138
val ml_store_thms = ml_store "";
wenzelm@26492
   139
fun ml_store_thm (name, th) = ml_store "hd" (name, [th]);
wenzelm@26416
   140
wenzelm@26416
   141
fun bind_thm (name, thm) = ml_store_thm (name, Drule.standard thm);
wenzelm@26416
   142
fun bind_thms (name, thms) = ml_store_thms (name, map Drule.standard thms);
wenzelm@24574
   143
wenzelm@27343
   144
fun thm name = ProofContext.get_thm (the_local_context ()) name;
wenzelm@27343
   145
fun thms name = ProofContext.get_thms (the_local_context ()) name;
wenzelm@27343
   146
wenzelm@24574
   147
wenzelm@24574
   148
wenzelm@24574
   149
(** ML antiquotations **)
wenzelm@24574
   150
wenzelm@27343
   151
(* antiquotation commands *)
wenzelm@27343
   152
wenzelm@27343
   153
type antiq =
wenzelm@27343
   154
  {struct_name: string, background: Proof.context} ->
wenzelm@27343
   155
    (Proof.context -> string * string) * Proof.context;
wenzelm@27343
   156
wenzelm@27343
   157
local
wenzelm@24574
   158
wenzelm@30513
   159
val global_parsers = ref (Symtab.empty: (Position.T -> antiq context_parser) Symtab.table);
wenzelm@27343
   160
wenzelm@27343
   161
in
wenzelm@24574
   162
wenzelm@27343
   163
fun add_antiq name scan = CRITICAL (fn () =>
wenzelm@27343
   164
  change global_parsers (fn tab =>
wenzelm@27343
   165
   (if not (Symtab.defined tab name) then ()
wenzelm@27343
   166
    else warning ("Redefined ML antiquotation: " ^ quote name);
wenzelm@27343
   167
    Symtab.update (name, scan) tab)));
wenzelm@27343
   168
wenzelm@27343
   169
fun antiquotation src ctxt =
wenzelm@24574
   170
  let val ((name, _), pos) = Args.dest_src src in
wenzelm@24574
   171
    (case Symtab.lookup (! global_parsers) name of
wenzelm@24574
   172
      NONE => error ("Unknown ML antiquotation command: " ^ quote name ^ Position.str_of pos)
wenzelm@28412
   173
    | SOME scan => (Position.report (Markup.ML_antiq name) pos;
wenzelm@27895
   174
        Args.context_syntax "ML antiquotation" (scan pos) src ctxt))
wenzelm@24574
   175
  end;
wenzelm@24574
   176
wenzelm@27343
   177
end;
wenzelm@27343
   178
wenzelm@24574
   179
wenzelm@27343
   180
(* parsing and evaluation *)
wenzelm@24574
   181
wenzelm@27343
   182
local
wenzelm@27343
   183
wenzelm@24574
   184
structure P = OuterParse;
wenzelm@24574
   185
wenzelm@24574
   186
val antiq =
wenzelm@27809
   187
  P.!!! (P.position P.xname -- Args.parse --| Scan.ahead P.eof)
wenzelm@24574
   188
  >> (fn ((x, pos), y) => Args.src ((x, y), pos));
wenzelm@24574
   189
wenzelm@28270
   190
val struct_name = "Isabelle";
wenzelm@28270
   191
wenzelm@28270
   192
fun eval_antiquotes (txt, pos) opt_context =
wenzelm@24574
   193
  let
wenzelm@27874
   194
    val syms = SymbolPos.explode (txt, pos);
wenzelm@27874
   195
    val ants = Antiquote.read (syms, pos);
wenzelm@28270
   196
    val opt_ctxt = Option.map (Context.Proof o Context.proof_of) opt_context;
wenzelm@27343
   197
    val ((ml_env, ml_body), opt_ctxt') =
wenzelm@27874
   198
      if not (exists Antiquote.is_antiq ants)
wenzelm@27874
   199
      then (("", Symbol.escape (SymbolPos.content syms)), opt_ctxt)
wenzelm@27343
   200
      else
wenzelm@27343
   201
        let
wenzelm@27343
   202
          val ctxt =
wenzelm@27343
   203
            (case opt_ctxt of
wenzelm@27343
   204
              NONE => error ("Unknown context -- cannot expand ML antiquotations" ^
wenzelm@27343
   205
                Position.str_of pos)
wenzelm@28270
   206
            | SOME ctxt => Context.proof_of ctxt);
wenzelm@27378
   207
wenzelm@27359
   208
          val lex = #1 (OuterKeyword.get_lexicons ());
wenzelm@27343
   209
          fun no_decl _ = ("", "");
wenzelm@27378
   210
wenzelm@27343
   211
          fun expand (Antiquote.Text s) state = (K ("", Symbol.escape s), state)
wenzelm@27781
   212
            | expand (Antiquote.Antiq x) (scope, background) =
wenzelm@27343
   213
                let
wenzelm@27343
   214
                  val context = Stack.top scope;
wenzelm@27781
   215
                  val (f, context') = antiquotation (Antiquote.read_antiq lex antiq x) context;
wenzelm@27343
   216
                  val (decl, background') = f {background = background, struct_name = struct_name};
wenzelm@27343
   217
                in (decl, (Stack.map_top (K context') scope, background')) end
wenzelm@27343
   218
            | expand (Antiquote.Open _) (scope, background) =
wenzelm@27343
   219
                (no_decl, (Stack.push scope, background))
wenzelm@27343
   220
            | expand (Antiquote.Close _) (scope, background) =
wenzelm@27343
   221
                (no_decl, (Stack.pop scope, background));
wenzelm@24574
   222
wenzelm@27343
   223
          val (decls, (_, ctxt')) = fold_map expand ants (Stack.init ctxt, ctxt);
wenzelm@27343
   224
          val ml = decls |> map (fn decl => decl ctxt') |> split_list |> pairself implode;
wenzelm@27343
   225
        in (ml, SOME (Context.Proof ctxt')) end;
wenzelm@27343
   226
  in (("structure " ^ struct_name ^ " =\nstruct\n" ^ ml_env ^ "end;", ml_body), opt_ctxt') end;
wenzelm@24574
   227
wenzelm@24574
   228
in
wenzelm@24574
   229
wenzelm@24574
   230
val trace = ref false;
wenzelm@24574
   231
wenzelm@26385
   232
fun eval_wrapper pr verbose pos txt =
wenzelm@24574
   233
  let
wenzelm@28270
   234
    fun eval_raw p = use_text name_space
wenzelm@28270
   235
      (the_default 1 (Position.line_of p), the_default "ML" (Position.file_of p)) pr;
wenzelm@28270
   236
wenzelm@28270
   237
    (*prepare source text*)
wenzelm@27878
   238
    val _ = Position.report Markup.ML_source pos;
wenzelm@28270
   239
    val ((env, body), env_ctxt) = eval_antiquotes (txt, pos) (Context.thread_data ());
wenzelm@27343
   240
    val _ = if ! trace then tracing (cat_lines [env, body]) else ();
wenzelm@28270
   241
wenzelm@28270
   242
    (*prepare static ML environment*)
wenzelm@28270
   243
    val _ = Context.setmp_thread_data env_ctxt
wenzelm@28270
   244
        (fn () => (eval_raw Position.none false env; Context.thread_data ())) ()
wenzelm@28270
   245
      |> (fn NONE => () | SOME context' => Context.>> (ML_Env.put (ML_Env.get context')));
wenzelm@28270
   246
wenzelm@28270
   247
    (*eval ML*)
wenzelm@28270
   248
    val _ = eval_raw pos verbose body;
wenzelm@28270
   249
wenzelm@28270
   250
    (*reset static ML environment*)
wenzelm@28270
   251
    val _ = eval_raw Position.none false "structure Isabelle = struct end";
wenzelm@28270
   252
  in () end;
wenzelm@24574
   253
wenzelm@24574
   254
end;
wenzelm@24574
   255
wenzelm@24574
   256
wenzelm@24574
   257
(* ML evaluation *)
wenzelm@24574
   258
wenzelm@26455
   259
val eval = eval_wrapper Output.ml_output;
wenzelm@26881
   260
fun eval_file path = eval true (Path.position path) (File.read path);
wenzelm@24574
   261
wenzelm@28270
   262
fun eval_in ctxt verbose pos txt =
wenzelm@28270
   263
  Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval verbose pos txt) ();
wenzelm@25204
   264
wenzelm@28270
   265
fun evaluate ctxt pr verbose (ref_name, r) txt = NAMED_CRITICAL "ML" (fn () =>
wenzelm@25204
   266
  let
wenzelm@25204
   267
    val _ = r := NONE;
wenzelm@28270
   268
    val _ = Context.setmp_thread_data (SOME (Context.Proof ctxt)) (fn () =>
wenzelm@28270
   269
      eval_wrapper pr verbose Position.none
wenzelm@28270
   270
        ("val _ = (" ^ ref_name ^ " := SOME (fn () => " ^ txt ^ "))")) ();
wenzelm@25204
   271
  in (case ! r of NONE => error ("Bad evaluation for " ^ ref_name) | SOME e => e) end) ();
wenzelm@24574
   272
wenzelm@26455
   273
fun expression pos bind body txt =
wenzelm@26455
   274
  exec (fn () => eval false pos
wenzelm@26455
   275
    ("Context.set_thread_data (SOME (let " ^ bind ^ " = " ^ txt ^ " in " ^ body ^
wenzelm@26455
   276
      " end (ML_Context.the_generic_context ())));"));
wenzelm@26455
   277
wenzelm@24574
   278
end;
wenzelm@24574
   279
wenzelm@24574
   280
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
wenzelm@24574
   281
open Basic_ML_Context;