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