src/Pure/ML/ml_context.ML
author wenzelm
Sat Aug 09 22:43:46 2008 +0200 (2008-08-09)
changeset 27809 a1e409db516b
parent 27781 5a82ee34e9fc
child 27868 a28b3cd0077b
permissions -rw-r--r--
unified Args.T with OuterLex.token, renamed some operations;
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@26416
    23
  val stored_thms: thm list ref
wenzelm@26416
    24
  val ml_store_thm: string * thm -> unit
wenzelm@26416
    25
  val ml_store_thms: string * thm list -> unit
wenzelm@27343
    26
  type antiq =
wenzelm@27343
    27
    {struct_name: string, background: Proof.context} ->
wenzelm@27343
    28
      (Proof.context -> string * string) * Proof.context
wenzelm@27343
    29
  val add_antiq: string ->
wenzelm@27343
    30
    (Context.generic * Args.T list -> antiq * (Context.generic * Args.T list)) -> 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@26455
    35
  val eval_in: Context.generic option -> bool -> Position.T -> string -> unit
wenzelm@25204
    36
  val evaluate: (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@26416
    56
(* theorem bindings *)
wenzelm@26416
    57
wenzelm@26416
    58
val stored_thms: thm list ref = ref [];
wenzelm@26416
    59
wenzelm@26492
    60
fun ml_store sel (name, ths) =
wenzelm@26492
    61
  let
wenzelm@26492
    62
    val ths' = Context.>>> (Context.map_theory_result (PureThy.store_thms (name, ths)));
wenzelm@26492
    63
    val _ =
wenzelm@26492
    64
      if name = "" then ()
wenzelm@26492
    65
      else if not (ML_Syntax.is_identifier name) then
wenzelm@26492
    66
        error ("Cannot bind theorem(s) " ^ quote name ^ " as ML value")
wenzelm@26492
    67
      else setmp stored_thms ths' (fn () =>
wenzelm@26492
    68
        use_text (0, "") Output.ml_output true
wenzelm@26492
    69
          ("val " ^ name ^ " = " ^ sel ^ "(! ML_Context.stored_thms);")) ();
wenzelm@26492
    70
  in () end;
wenzelm@24574
    71
wenzelm@26492
    72
val ml_store_thms = ml_store "";
wenzelm@26492
    73
fun ml_store_thm (name, th) = ml_store "hd" (name, [th]);
wenzelm@26416
    74
wenzelm@26416
    75
fun bind_thm (name, thm) = ml_store_thm (name, Drule.standard thm);
wenzelm@26416
    76
fun bind_thms (name, thms) = ml_store_thms (name, map Drule.standard thms);
wenzelm@24574
    77
wenzelm@27343
    78
fun thm name = ProofContext.get_thm (the_local_context ()) name;
wenzelm@27343
    79
fun thms name = ProofContext.get_thms (the_local_context ()) name;
wenzelm@27343
    80
wenzelm@24574
    81
wenzelm@24574
    82
wenzelm@24574
    83
(** ML antiquotations **)
wenzelm@24574
    84
wenzelm@27343
    85
(* antiquotation commands *)
wenzelm@27343
    86
wenzelm@27343
    87
type antiq =
wenzelm@27343
    88
  {struct_name: string, background: Proof.context} ->
wenzelm@27343
    89
    (Proof.context -> string * string) * Proof.context;
wenzelm@27343
    90
wenzelm@27343
    91
local
wenzelm@24574
    92
wenzelm@27343
    93
val global_parsers = ref (Symtab.empty:
wenzelm@27343
    94
  (Context.generic * Args.T list -> antiq * (Context.generic * Args.T list)) Symtab.table);
wenzelm@27343
    95
wenzelm@27343
    96
in
wenzelm@24574
    97
wenzelm@27343
    98
fun add_antiq name scan = CRITICAL (fn () =>
wenzelm@27343
    99
  change global_parsers (fn tab =>
wenzelm@27343
   100
   (if not (Symtab.defined tab name) then ()
wenzelm@27343
   101
    else warning ("Redefined ML antiquotation: " ^ quote name);
wenzelm@27343
   102
    Symtab.update (name, scan) tab)));
wenzelm@27343
   103
wenzelm@27343
   104
fun antiquotation src ctxt =
wenzelm@24574
   105
  let val ((name, _), pos) = Args.dest_src src in
wenzelm@24574
   106
    (case Symtab.lookup (! global_parsers) name of
wenzelm@24574
   107
      NONE => error ("Unknown ML antiquotation command: " ^ quote name ^ Position.str_of pos)
wenzelm@27343
   108
    | SOME scan => Args.context_syntax "ML antiquotation" scan src ctxt)
wenzelm@24574
   109
  end;
wenzelm@24574
   110
wenzelm@27343
   111
end;
wenzelm@27343
   112
wenzelm@24574
   113
wenzelm@27343
   114
(* parsing and evaluation *)
wenzelm@24574
   115
wenzelm@27343
   116
local
wenzelm@27343
   117
wenzelm@24574
   118
structure P = OuterParse;
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@27343
   124
fun eval_antiquotes struct_name (txt, pos) opt_ctxt =
wenzelm@24574
   125
  let
wenzelm@27771
   126
    val ants = Antiquote.read (txt, pos);
wenzelm@27343
   127
    val ((ml_env, ml_body), opt_ctxt') =
wenzelm@27343
   128
      if not (exists Antiquote.is_antiq ants) then (("", Symbol.escape txt), opt_ctxt)
wenzelm@27343
   129
      else
wenzelm@27343
   130
        let
wenzelm@27343
   131
          val ctxt =
wenzelm@27343
   132
            (case opt_ctxt of
wenzelm@27343
   133
              NONE => error ("Unknown context -- cannot expand ML antiquotations" ^
wenzelm@27343
   134
                Position.str_of pos)
wenzelm@27343
   135
            | SOME context => Context.proof_of context);
wenzelm@27378
   136
wenzelm@27359
   137
          val lex = #1 (OuterKeyword.get_lexicons ());
wenzelm@27343
   138
          fun no_decl _ = ("", "");
wenzelm@27378
   139
wenzelm@27343
   140
          fun expand (Antiquote.Text s) state = (K ("", Symbol.escape s), state)
wenzelm@27781
   141
            | expand (Antiquote.Antiq x) (scope, background) =
wenzelm@27343
   142
                let
wenzelm@27343
   143
                  val context = Stack.top scope;
wenzelm@27781
   144
                  val (f, context') = antiquotation (Antiquote.read_antiq lex antiq x) context;
wenzelm@27343
   145
                  val (decl, background') = f {background = background, struct_name = struct_name};
wenzelm@27343
   146
                in (decl, (Stack.map_top (K context') scope, background')) end
wenzelm@27343
   147
            | expand (Antiquote.Open _) (scope, background) =
wenzelm@27343
   148
                (no_decl, (Stack.push scope, background))
wenzelm@27343
   149
            | expand (Antiquote.Close _) (scope, background) =
wenzelm@27343
   150
                (no_decl, (Stack.pop scope, background));
wenzelm@24574
   151
wenzelm@27343
   152
          val (decls, (_, ctxt')) = fold_map expand ants (Stack.init ctxt, ctxt);
wenzelm@27343
   153
          val ml = decls |> map (fn decl => decl ctxt') |> split_list |> pairself implode;
wenzelm@27343
   154
        in (ml, SOME (Context.Proof ctxt')) end;
wenzelm@27343
   155
  in (("structure " ^ struct_name ^ " =\nstruct\n" ^ ml_env ^ "end;", ml_body), opt_ctxt') end;
wenzelm@24574
   156
wenzelm@24574
   157
in
wenzelm@24574
   158
wenzelm@24574
   159
val trace = ref false;
wenzelm@24574
   160
wenzelm@26385
   161
fun eval_wrapper pr verbose pos txt =
wenzelm@24574
   162
  let
wenzelm@26473
   163
    val struct_name =
wenzelm@26473
   164
      if Multithreading.available then "Isabelle" ^ serial_string ()
wenzelm@26473
   165
      else "Isabelle";
wenzelm@27343
   166
    val ((env, body), env_ctxt) = eval_antiquotes struct_name (txt, pos) (Context.thread_data ());
wenzelm@27343
   167
    val _ = if ! trace then tracing (cat_lines [env, body]) else ();
wenzelm@27343
   168
    fun eval_raw p =
wenzelm@26385
   169
      use_text (the_default 1 (Position.line_of p), the_default "ML" (Position.file_of p)) pr;
wenzelm@25755
   170
  in
wenzelm@27343
   171
    Context.setmp_thread_data env_ctxt (fn () => eval_raw Position.none false env) ();
wenzelm@27343
   172
    NAMED_CRITICAL "ML" (fn () => eval_raw pos verbose body); (* FIXME non-critical with local ML env *)
wenzelm@26473
   173
    forget_structure struct_name
wenzelm@25755
   174
  end;
wenzelm@24574
   175
wenzelm@24574
   176
end;
wenzelm@24574
   177
wenzelm@24574
   178
wenzelm@24574
   179
(* ML evaluation *)
wenzelm@24574
   180
wenzelm@26455
   181
val eval = eval_wrapper Output.ml_output;
wenzelm@26881
   182
fun eval_file path = eval true (Path.position path) (File.read path);
wenzelm@24574
   183
wenzelm@26455
   184
fun eval_in context verbose pos txt =
wenzelm@26455
   185
  Context.setmp_thread_data context (fn () => eval verbose pos txt) ();
wenzelm@25204
   186
wenzelm@25700
   187
fun evaluate pr verbose (ref_name, r) txt = NAMED_CRITICAL "ML" (fn () =>
wenzelm@25204
   188
  let
wenzelm@25204
   189
    val _ = r := NONE;
wenzelm@26385
   190
    val _ = eval_wrapper pr verbose Position.none
wenzelm@26385
   191
      ("val _ = (" ^ ref_name ^ " := SOME (fn () => " ^ txt ^ "))");
wenzelm@25204
   192
  in (case ! r of NONE => error ("Bad evaluation for " ^ ref_name) | SOME e => e) end) ();
wenzelm@24574
   193
wenzelm@26455
   194
fun expression pos bind body txt =
wenzelm@26455
   195
  exec (fn () => eval false pos
wenzelm@26455
   196
    ("Context.set_thread_data (SOME (let " ^ bind ^ " = " ^ txt ^ " in " ^ body ^
wenzelm@26455
   197
      " end (ML_Context.the_generic_context ())));"));
wenzelm@26455
   198
wenzelm@24574
   199
end;
wenzelm@24574
   200
wenzelm@24574
   201
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
wenzelm@24574
   202
open Basic_ML_Context;
wenzelm@27378
   203