src/Pure/ML/ml_context.ML
author wenzelm
Mon Jun 01 15:26:00 2009 +0200 (2009-06-01)
changeset 31325 700951b53d21
parent 30683 e8ac1f9d9469
child 31334 999fa9e1dbdd
permissions -rw-r--r--
moved local ML environment to separate module ML_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@26416
    22
  val stored_thms: thm list ref
wenzelm@26416
    23
  val ml_store_thm: string * thm -> unit
wenzelm@26416
    24
  val ml_store_thms: string * thm list -> unit
wenzelm@27343
    25
  type antiq =
wenzelm@27343
    26
    {struct_name: string, background: Proof.context} ->
wenzelm@27343
    27
      (Proof.context -> string * string) * Proof.context
wenzelm@30513
    28
  val add_antiq: string -> (Position.T -> antiq context_parser) -> unit
wenzelm@24574
    29
  val trace: bool ref
wenzelm@30637
    30
  val eval_antiquotes: ML_Lex.token Antiquote.antiquote list * Position.T ->
wenzelm@30637
    31
    Context.generic option -> (ML_Lex.token list * ML_Lex.token list) * Context.generic option
wenzelm@30574
    32
  val eval: bool -> Position.T -> Symbol_Pos.text -> unit
wenzelm@26455
    33
  val eval_file: Path.T -> unit
wenzelm@30574
    34
  val eval_in: Proof.context option -> bool -> Position.T -> Symbol_Pos.text -> unit
wenzelm@30672
    35
  val evaluate: Proof.context -> bool -> string * (unit -> 'a) option ref -> string -> 'a
wenzelm@26455
    36
  val expression: Position.T -> string -> string -> string -> Context.generic -> Context.generic
wenzelm@25204
    37
end
wenzelm@24574
    38
wenzelm@24574
    39
structure ML_Context: ML_CONTEXT =
wenzelm@24574
    40
struct
wenzelm@24574
    41
wenzelm@26416
    42
(** implicit ML context **)
wenzelm@24574
    43
wenzelm@26416
    44
val the_generic_context = Context.the_thread_data;
wenzelm@26432
    45
val the_global_context = Context.theory_of o the_generic_context;
wenzelm@24574
    46
val the_local_context = Context.proof_of o the_generic_context;
wenzelm@24574
    47
wenzelm@26455
    48
fun exec (e: unit -> unit) context =
wenzelm@26455
    49
  (case Context.setmp_thread_data (SOME context) (fn () => (e (); Context.thread_data ())) () of
wenzelm@26455
    50
    SOME context' => context'
wenzelm@26455
    51
  | NONE => error "Missing context after execution");
wenzelm@26416
    52
wenzelm@26416
    53
wenzelm@26416
    54
(* theorem bindings *)
wenzelm@26416
    55
wenzelm@26416
    56
val stored_thms: thm list ref = ref [];
wenzelm@26416
    57
wenzelm@26492
    58
fun ml_store sel (name, ths) =
wenzelm@26492
    59
  let
haftmann@29579
    60
    val ths' = Context.>>> (Context.map_theory_result
haftmann@29579
    61
      (PureThy.store_thms (Binding.name name, ths)));
wenzelm@26492
    62
    val _ =
wenzelm@26492
    63
      if name = "" then ()
wenzelm@26492
    64
      else if not (ML_Syntax.is_identifier name) then
wenzelm@26492
    65
        error ("Cannot bind theorem(s) " ^ quote name ^ " as ML value")
wenzelm@26492
    66
      else setmp stored_thms ths' (fn () =>
wenzelm@31325
    67
        use_text ML_Env.local_context (0, "") true
wenzelm@26492
    68
          ("val " ^ name ^ " = " ^ sel ^ "(! ML_Context.stored_thms);")) ();
wenzelm@26492
    69
  in () end;
wenzelm@24574
    70
wenzelm@26492
    71
val ml_store_thms = ml_store "";
wenzelm@26492
    72
fun ml_store_thm (name, th) = ml_store "hd" (name, [th]);
wenzelm@26416
    73
wenzelm@26416
    74
fun bind_thm (name, thm) = ml_store_thm (name, Drule.standard thm);
wenzelm@26416
    75
fun bind_thms (name, thms) = ml_store_thms (name, map Drule.standard thms);
wenzelm@24574
    76
wenzelm@27343
    77
fun thm name = ProofContext.get_thm (the_local_context ()) name;
wenzelm@27343
    78
fun thms name = ProofContext.get_thms (the_local_context ()) name;
wenzelm@27343
    79
wenzelm@24574
    80
wenzelm@24574
    81
wenzelm@24574
    82
(** ML antiquotations **)
wenzelm@24574
    83
wenzelm@27343
    84
(* antiquotation commands *)
wenzelm@27343
    85
wenzelm@27343
    86
type antiq =
wenzelm@27343
    87
  {struct_name: string, background: Proof.context} ->
wenzelm@27343
    88
    (Proof.context -> string * string) * Proof.context;
wenzelm@27343
    89
wenzelm@27343
    90
local
wenzelm@24574
    91
wenzelm@30513
    92
val global_parsers = ref (Symtab.empty: (Position.T -> antiq context_parser) Symtab.table);
wenzelm@27343
    93
wenzelm@27343
    94
in
wenzelm@24574
    95
wenzelm@27343
    96
fun add_antiq name scan = CRITICAL (fn () =>
wenzelm@27343
    97
  change global_parsers (fn tab =>
wenzelm@27343
    98
   (if not (Symtab.defined tab name) then ()
wenzelm@27343
    99
    else warning ("Redefined ML antiquotation: " ^ quote name);
wenzelm@27343
   100
    Symtab.update (name, scan) tab)));
wenzelm@27343
   101
wenzelm@27343
   102
fun antiquotation src ctxt =
wenzelm@24574
   103
  let val ((name, _), pos) = Args.dest_src src in
wenzelm@24574
   104
    (case Symtab.lookup (! global_parsers) name of
wenzelm@24574
   105
      NONE => error ("Unknown ML antiquotation command: " ^ quote name ^ Position.str_of pos)
wenzelm@28412
   106
    | SOME scan => (Position.report (Markup.ML_antiq name) pos;
wenzelm@27895
   107
        Args.context_syntax "ML antiquotation" (scan pos) src ctxt))
wenzelm@24574
   108
  end;
wenzelm@24574
   109
wenzelm@27343
   110
end;
wenzelm@27343
   111
wenzelm@24574
   112
wenzelm@27343
   113
(* parsing and evaluation *)
wenzelm@24574
   114
wenzelm@27343
   115
local
wenzelm@27343
   116
wenzelm@24574
   117
structure P = OuterParse;
wenzelm@30588
   118
structure T = OuterLex;
wenzelm@24574
   119
wenzelm@24574
   120
val antiq =
wenzelm@27809
   121
  P.!!! (P.position P.xname -- Args.parse --| Scan.ahead P.eof)
wenzelm@24574
   122
  >> (fn ((x, pos), y) => Args.src ((x, y), pos));
wenzelm@24574
   123
wenzelm@28270
   124
val struct_name = "Isabelle";
wenzelm@30637
   125
val begin_env = ML_Lex.tokenize ("structure " ^ struct_name ^ " =\nstruct\n");
wenzelm@30637
   126
val end_env = ML_Lex.tokenize "end;";
wenzelm@28270
   127
wenzelm@30637
   128
in
wenzelm@30637
   129
wenzelm@30637
   130
fun eval_antiquotes (ants, pos) opt_context =
wenzelm@24574
   131
  let
wenzelm@28270
   132
    val opt_ctxt = Option.map (Context.Proof o Context.proof_of) opt_context;
wenzelm@27343
   133
    val ((ml_env, ml_body), opt_ctxt') =
wenzelm@30637
   134
      if forall Antiquote.is_text ants
wenzelm@30637
   135
      then (([], map (fn Antiquote.Text tok => tok) ants), opt_ctxt)
wenzelm@27343
   136
      else
wenzelm@27343
   137
        let
wenzelm@27343
   138
          val ctxt =
wenzelm@27343
   139
            (case opt_ctxt of
wenzelm@30637
   140
              NONE => error ("No context -- cannot expand ML antiquotations" ^ Position.str_of pos)
wenzelm@28270
   141
            | SOME ctxt => Context.proof_of ctxt);
wenzelm@27378
   142
wenzelm@27359
   143
          val lex = #1 (OuterKeyword.get_lexicons ());
wenzelm@30637
   144
          fun no_decl _ = ([], []);
wenzelm@27378
   145
wenzelm@30637
   146
          fun expand (Antiquote.Text tok) state = (K ([], [tok]), state)
wenzelm@30683
   147
            | expand (Antiquote.Antiq (ss, range)) (scope, background) =
wenzelm@27343
   148
                let
wenzelm@27343
   149
                  val context = Stack.top scope;
wenzelm@30683
   150
                  val (f, context') = antiquotation (T.read_antiq lex antiq (ss, #1 range)) context;
wenzelm@27343
   151
                  val (decl, background') = f {background = background, struct_name = struct_name};
wenzelm@30683
   152
                  val decl' = decl #> pairself (ML_Lex.tokenize #> map (ML_Lex.set_range range));
wenzelm@30637
   153
                in (decl', (Stack.map_top (K context') scope, background')) end
wenzelm@27343
   154
            | expand (Antiquote.Open _) (scope, background) =
wenzelm@27343
   155
                (no_decl, (Stack.push scope, background))
wenzelm@27343
   156
            | expand (Antiquote.Close _) (scope, background) =
wenzelm@27343
   157
                (no_decl, (Stack.pop scope, background));
wenzelm@24574
   158
wenzelm@27343
   159
          val (decls, (_, ctxt')) = fold_map expand ants (Stack.init ctxt, ctxt);
wenzelm@30637
   160
          val ml = decls |> map (fn decl => decl ctxt') |> split_list |> pairself flat;
wenzelm@27343
   161
        in (ml, SOME (Context.Proof ctxt')) end;
wenzelm@30637
   162
  in ((begin_env @ ml_env @ end_env, ml_body), opt_ctxt') end;
wenzelm@24574
   163
wenzelm@24574
   164
val trace = ref false;
wenzelm@24574
   165
wenzelm@30672
   166
fun eval verbose pos txt =
wenzelm@24574
   167
  let
wenzelm@31325
   168
    fun eval_raw p = use_text ML_Env.local_context
wenzelm@30672
   169
      (the_default 1 (Position.line_of p), the_default "ML" (Position.file_of p));
wenzelm@28270
   170
wenzelm@28270
   171
    (*prepare source text*)
wenzelm@27878
   172
    val _ = Position.report Markup.ML_source pos;
wenzelm@30643
   173
    val ants = ML_Lex.read_antiq (Symbol_Pos.explode (txt, pos), pos);
wenzelm@30637
   174
    val ((env, body), env_ctxt) = eval_antiquotes (ants, pos) (Context.thread_data ())
wenzelm@30637
   175
      |>> pairself (implode o map ML_Lex.text_of);
wenzelm@27343
   176
    val _ = if ! trace then tracing (cat_lines [env, body]) else ();
wenzelm@28270
   177
wenzelm@28270
   178
    (*prepare static ML environment*)
wenzelm@28270
   179
    val _ = Context.setmp_thread_data env_ctxt
wenzelm@28270
   180
        (fn () => (eval_raw Position.none false env; Context.thread_data ())) ()
wenzelm@31325
   181
      |> (fn NONE => () | SOME context' => Context.>> (ML_Env.inherit context'));
wenzelm@28270
   182
wenzelm@28270
   183
    (*eval ML*)
wenzelm@28270
   184
    val _ = eval_raw pos verbose body;
wenzelm@28270
   185
wenzelm@28270
   186
    (*reset static ML environment*)
wenzelm@28270
   187
    val _ = eval_raw Position.none false "structure Isabelle = struct end";
wenzelm@28270
   188
  in () end;
wenzelm@24574
   189
wenzelm@24574
   190
end;
wenzelm@24574
   191
wenzelm@24574
   192
wenzelm@30672
   193
(* derived versions *)
wenzelm@24574
   194
wenzelm@26881
   195
fun eval_file path = eval true (Path.position path) (File.read path);
wenzelm@24574
   196
wenzelm@28270
   197
fun eval_in ctxt verbose pos txt =
wenzelm@28270
   198
  Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval verbose pos txt) ();
wenzelm@25204
   199
wenzelm@30672
   200
fun evaluate ctxt verbose (ref_name, r) txt = NAMED_CRITICAL "ML" (fn () =>
wenzelm@25204
   201
  let
wenzelm@25204
   202
    val _ = r := NONE;
wenzelm@28270
   203
    val _ = Context.setmp_thread_data (SOME (Context.Proof ctxt)) (fn () =>
wenzelm@30672
   204
      eval verbose Position.none ("val _ = (" ^ ref_name ^ " := SOME (fn () => " ^ txt ^ "))")) ();
wenzelm@25204
   205
  in (case ! r of NONE => error ("Bad evaluation for " ^ ref_name) | SOME e => e) end) ();
wenzelm@24574
   206
wenzelm@26455
   207
fun expression pos bind body txt =
wenzelm@26455
   208
  exec (fn () => eval false pos
wenzelm@26455
   209
    ("Context.set_thread_data (SOME (let " ^ bind ^ " = " ^ txt ^ " in " ^ body ^
wenzelm@26455
   210
      " end (ML_Context.the_generic_context ())));"));
wenzelm@26455
   211
wenzelm@24574
   212
end;
wenzelm@24574
   213
wenzelm@24574
   214
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
wenzelm@24574
   215
open Basic_ML_Context;