src/Pure/ML/ml_context.ML
author wenzelm
Wed Mar 18 22:41:14 2009 +0100 (2009-03-18)
changeset 30574 b9bcc640ed58
parent 30573 49899f26fbd1
child 30588 05f81bbb2614
permissions -rw-r--r--
more precise type Symbol_Pos.text;
geralized ML_Context.inherit_env;
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@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
wenzelm@30574
    32
  val eval_wrapper: (string -> unit) * (string -> 'a) -> bool ->
wenzelm@30574
    33
    Position.T -> Symbol_Pos.text -> unit
wenzelm@30574
    34
  val eval: bool -> Position.T -> Symbol_Pos.text -> unit
wenzelm@26455
    35
  val eval_file: Path.T -> unit
wenzelm@30574
    36
  val eval_in: Proof.context option -> bool -> Position.T -> Symbol_Pos.text -> unit
wenzelm@28270
    37
  val evaluate: Proof.context -> (string -> unit) * (string -> 'b) -> bool ->
wenzelm@25204
    38
    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@28270
    62
    ML_NameSpace.valueVal Symtab.table *
wenzelm@28270
    63
    ML_NameSpace.typeVal Symtab.table *
wenzelm@28270
    64
    ML_NameSpace.fixityVal Symtab.table *
wenzelm@28270
    65
    ML_NameSpace.structureVal Symtab.table *
wenzelm@28270
    66
    ML_NameSpace.signatureVal Symtab.table *
wenzelm@28270
    67
    ML_NameSpace.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@28270
    83
val name_space: ML_NameSpace.nameSpace =
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@28270
    88
      |> (fn NONE => sel2 ML_NameSpace.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@28270
    93
      |> append (sel2 ML_NameSpace.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@28270
    99
      else sel2 ML_NameSpace.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@28270
   121
wenzelm@26416
   122
(* theorem bindings *)
wenzelm@26416
   123
wenzelm@26416
   124
val stored_thms: thm list ref = ref [];
wenzelm@26416
   125
wenzelm@26492
   126
fun ml_store sel (name, ths) =
wenzelm@26492
   127
  let
haftmann@29579
   128
    val ths' = Context.>>> (Context.map_theory_result
haftmann@29579
   129
      (PureThy.store_thms (Binding.name name, ths)));
wenzelm@26492
   130
    val _ =
wenzelm@26492
   131
      if name = "" then ()
wenzelm@26492
   132
      else if not (ML_Syntax.is_identifier name) then
wenzelm@26492
   133
        error ("Cannot bind theorem(s) " ^ quote name ^ " as ML value")
wenzelm@26492
   134
      else setmp stored_thms ths' (fn () =>
wenzelm@28270
   135
        use_text name_space (0, "") Output.ml_output true
wenzelm@26492
   136
          ("val " ^ name ^ " = " ^ sel ^ "(! ML_Context.stored_thms);")) ();
wenzelm@26492
   137
  in () end;
wenzelm@24574
   138
wenzelm@26492
   139
val ml_store_thms = ml_store "";
wenzelm@26492
   140
fun ml_store_thm (name, th) = ml_store "hd" (name, [th]);
wenzelm@26416
   141
wenzelm@26416
   142
fun bind_thm (name, thm) = ml_store_thm (name, Drule.standard thm);
wenzelm@26416
   143
fun bind_thms (name, thms) = ml_store_thms (name, map Drule.standard thms);
wenzelm@24574
   144
wenzelm@27343
   145
fun thm name = ProofContext.get_thm (the_local_context ()) name;
wenzelm@27343
   146
fun thms name = ProofContext.get_thms (the_local_context ()) name;
wenzelm@27343
   147
wenzelm@24574
   148
wenzelm@24574
   149
wenzelm@24574
   150
(** ML antiquotations **)
wenzelm@24574
   151
wenzelm@27343
   152
(* antiquotation commands *)
wenzelm@27343
   153
wenzelm@27343
   154
type antiq =
wenzelm@27343
   155
  {struct_name: string, background: Proof.context} ->
wenzelm@27343
   156
    (Proof.context -> string * string) * Proof.context;
wenzelm@27343
   157
wenzelm@27343
   158
local
wenzelm@24574
   159
wenzelm@30513
   160
val global_parsers = ref (Symtab.empty: (Position.T -> antiq context_parser) Symtab.table);
wenzelm@27343
   161
wenzelm@27343
   162
in
wenzelm@24574
   163
wenzelm@27343
   164
fun add_antiq name scan = CRITICAL (fn () =>
wenzelm@27343
   165
  change global_parsers (fn tab =>
wenzelm@27343
   166
   (if not (Symtab.defined tab name) then ()
wenzelm@27343
   167
    else warning ("Redefined ML antiquotation: " ^ quote name);
wenzelm@27343
   168
    Symtab.update (name, scan) tab)));
wenzelm@27343
   169
wenzelm@27343
   170
fun antiquotation src ctxt =
wenzelm@24574
   171
  let val ((name, _), pos) = Args.dest_src src in
wenzelm@24574
   172
    (case Symtab.lookup (! global_parsers) name of
wenzelm@24574
   173
      NONE => error ("Unknown ML antiquotation command: " ^ quote name ^ Position.str_of pos)
wenzelm@28412
   174
    | SOME scan => (Position.report (Markup.ML_antiq name) pos;
wenzelm@27895
   175
        Args.context_syntax "ML antiquotation" (scan pos) src ctxt))
wenzelm@24574
   176
  end;
wenzelm@24574
   177
wenzelm@27343
   178
end;
wenzelm@27343
   179
wenzelm@24574
   180
wenzelm@27343
   181
(* parsing and evaluation *)
wenzelm@24574
   182
wenzelm@27343
   183
local
wenzelm@27343
   184
wenzelm@24574
   185
structure P = OuterParse;
wenzelm@24574
   186
wenzelm@24574
   187
val antiq =
wenzelm@27809
   188
  P.!!! (P.position P.xname -- Args.parse --| Scan.ahead P.eof)
wenzelm@24574
   189
  >> (fn ((x, pos), y) => Args.src ((x, y), pos));
wenzelm@24574
   190
wenzelm@28270
   191
val struct_name = "Isabelle";
wenzelm@28270
   192
wenzelm@28270
   193
fun eval_antiquotes (txt, pos) opt_context =
wenzelm@24574
   194
  let
wenzelm@30573
   195
    val syms = Symbol_Pos.explode (txt, pos);
wenzelm@27874
   196
    val ants = Antiquote.read (syms, pos);
wenzelm@28270
   197
    val opt_ctxt = Option.map (Context.Proof o Context.proof_of) opt_context;
wenzelm@27343
   198
    val ((ml_env, ml_body), opt_ctxt') =
wenzelm@27874
   199
      if not (exists Antiquote.is_antiq ants)
wenzelm@30573
   200
      then (("", Symbol.escape (Symbol_Pos.content syms)), opt_ctxt)
wenzelm@27343
   201
      else
wenzelm@27343
   202
        let
wenzelm@27343
   203
          val ctxt =
wenzelm@27343
   204
            (case opt_ctxt of
wenzelm@27343
   205
              NONE => error ("Unknown context -- cannot expand ML antiquotations" ^
wenzelm@27343
   206
                Position.str_of pos)
wenzelm@28270
   207
            | SOME ctxt => Context.proof_of ctxt);
wenzelm@27378
   208
wenzelm@27359
   209
          val lex = #1 (OuterKeyword.get_lexicons ());
wenzelm@27343
   210
          fun no_decl _ = ("", "");
wenzelm@27378
   211
wenzelm@27343
   212
          fun expand (Antiquote.Text s) state = (K ("", Symbol.escape s), state)
wenzelm@27781
   213
            | expand (Antiquote.Antiq x) (scope, background) =
wenzelm@27343
   214
                let
wenzelm@27343
   215
                  val context = Stack.top scope;
wenzelm@27781
   216
                  val (f, context') = antiquotation (Antiquote.read_antiq lex antiq x) context;
wenzelm@27343
   217
                  val (decl, background') = f {background = background, struct_name = struct_name};
wenzelm@27343
   218
                in (decl, (Stack.map_top (K context') scope, background')) end
wenzelm@27343
   219
            | expand (Antiquote.Open _) (scope, background) =
wenzelm@27343
   220
                (no_decl, (Stack.push scope, background))
wenzelm@27343
   221
            | expand (Antiquote.Close _) (scope, background) =
wenzelm@27343
   222
                (no_decl, (Stack.pop scope, background));
wenzelm@24574
   223
wenzelm@27343
   224
          val (decls, (_, ctxt')) = fold_map expand ants (Stack.init ctxt, ctxt);
wenzelm@27343
   225
          val ml = decls |> map (fn decl => decl ctxt') |> split_list |> pairself implode;
wenzelm@27343
   226
        in (ml, SOME (Context.Proof ctxt')) end;
wenzelm@27343
   227
  in (("structure " ^ struct_name ^ " =\nstruct\n" ^ ml_env ^ "end;", ml_body), opt_ctxt') end;
wenzelm@24574
   228
wenzelm@24574
   229
in
wenzelm@24574
   230
wenzelm@24574
   231
val trace = ref false;
wenzelm@24574
   232
wenzelm@26385
   233
fun eval_wrapper pr verbose pos txt =
wenzelm@24574
   234
  let
wenzelm@28270
   235
    fun eval_raw p = use_text name_space
wenzelm@28270
   236
      (the_default 1 (Position.line_of p), the_default "ML" (Position.file_of p)) pr;
wenzelm@28270
   237
wenzelm@28270
   238
    (*prepare source text*)
wenzelm@27878
   239
    val _ = Position.report Markup.ML_source pos;
wenzelm@28270
   240
    val ((env, body), env_ctxt) = eval_antiquotes (txt, pos) (Context.thread_data ());
wenzelm@27343
   241
    val _ = if ! trace then tracing (cat_lines [env, body]) else ();
wenzelm@28270
   242
wenzelm@28270
   243
    (*prepare static ML environment*)
wenzelm@28270
   244
    val _ = Context.setmp_thread_data env_ctxt
wenzelm@28270
   245
        (fn () => (eval_raw Position.none false env; Context.thread_data ())) ()
wenzelm@28270
   246
      |> (fn NONE => () | SOME context' => Context.>> (ML_Env.put (ML_Env.get context')));
wenzelm@28270
   247
wenzelm@28270
   248
    (*eval ML*)
wenzelm@28270
   249
    val _ = eval_raw pos verbose body;
wenzelm@28270
   250
wenzelm@28270
   251
    (*reset static ML environment*)
wenzelm@28270
   252
    val _ = eval_raw Position.none false "structure Isabelle = struct end";
wenzelm@28270
   253
  in () end;
wenzelm@24574
   254
wenzelm@24574
   255
end;
wenzelm@24574
   256
wenzelm@24574
   257
wenzelm@24574
   258
(* ML evaluation *)
wenzelm@24574
   259
wenzelm@26455
   260
val eval = eval_wrapper Output.ml_output;
wenzelm@26881
   261
fun eval_file path = eval true (Path.position path) (File.read path);
wenzelm@24574
   262
wenzelm@28270
   263
fun eval_in ctxt verbose pos txt =
wenzelm@28270
   264
  Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval verbose pos txt) ();
wenzelm@25204
   265
wenzelm@28270
   266
fun evaluate ctxt pr verbose (ref_name, r) txt = NAMED_CRITICAL "ML" (fn () =>
wenzelm@25204
   267
  let
wenzelm@25204
   268
    val _ = r := NONE;
wenzelm@28270
   269
    val _ = Context.setmp_thread_data (SOME (Context.Proof ctxt)) (fn () =>
wenzelm@28270
   270
      eval_wrapper pr verbose Position.none
wenzelm@28270
   271
        ("val _ = (" ^ ref_name ^ " := SOME (fn () => " ^ txt ^ "))")) ();
wenzelm@25204
   272
  in (case ! r of NONE => error ("Bad evaluation for " ^ ref_name) | SOME e => e) end) ();
wenzelm@24574
   273
wenzelm@26455
   274
fun expression pos bind body txt =
wenzelm@26455
   275
  exec (fn () => eval false pos
wenzelm@26455
   276
    ("Context.set_thread_data (SOME (let " ^ bind ^ " = " ^ txt ^ " in " ^ body ^
wenzelm@26455
   277
      " end (ML_Context.the_generic_context ())));"));
wenzelm@26455
   278
wenzelm@24574
   279
end;
wenzelm@24574
   280
wenzelm@24574
   281
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
wenzelm@24574
   282
open Basic_ML_Context;