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