src/Pure/ML/ml_context.ML
author wenzelm
Tue Jun 24 19:43:19 2008 +0200 (2008-06-24)
changeset 27343 4b28b80dd1f8
parent 26881 bb68f50644a9
child 27359 54b5367a827a
permissions -rw-r--r--
add_antiq: more general notion of ML antiquotation;
eval_antiquotes: support blocks and background context;
moved concrete antiquotations to ml_antiquote.ML;
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@24574
    26
  val add_keywords: string 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@27343
    30
  val add_antiq: string ->
wenzelm@27343
    31
    (Context.generic * Args.T list -> antiq * (Context.generic * Args.T list)) -> unit
wenzelm@24574
    32
  val trace: bool ref
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 keywords *)
wenzelm@27343
    86
wenzelm@27343
    87
local
wenzelm@24574
    88
wenzelm@24574
    89
val global_lexicon = ref Scan.empty_lexicon;
wenzelm@24574
    90
wenzelm@27343
    91
in
wenzelm@27343
    92
wenzelm@24574
    93
fun add_keywords keywords = CRITICAL (fn () =>
wenzelm@24574
    94
  change global_lexicon (Scan.extend_lexicon (map Symbol.explode keywords)));
wenzelm@24574
    95
wenzelm@27343
    96
fun get_lexicon () = ! global_lexicon;
wenzelm@24574
    97
wenzelm@24574
    98
end;
wenzelm@24574
    99
wenzelm@24574
   100
wenzelm@27343
   101
(* antiquotation commands *)
wenzelm@27343
   102
wenzelm@27343
   103
type antiq =
wenzelm@27343
   104
  {struct_name: string, background: Proof.context} ->
wenzelm@27343
   105
    (Proof.context -> string * string) * Proof.context;
wenzelm@27343
   106
wenzelm@27343
   107
local
wenzelm@24574
   108
wenzelm@27343
   109
val global_parsers = ref (Symtab.empty:
wenzelm@27343
   110
  (Context.generic * Args.T list -> antiq * (Context.generic * Args.T list)) Symtab.table);
wenzelm@27343
   111
wenzelm@27343
   112
in
wenzelm@24574
   113
wenzelm@27343
   114
fun add_antiq name scan = CRITICAL (fn () =>
wenzelm@27343
   115
  change global_parsers (fn tab =>
wenzelm@27343
   116
   (if not (Symtab.defined tab name) then ()
wenzelm@27343
   117
    else warning ("Redefined ML antiquotation: " ^ quote name);
wenzelm@27343
   118
    Symtab.update (name, scan) tab)));
wenzelm@27343
   119
wenzelm@27343
   120
fun antiquotation src ctxt =
wenzelm@24574
   121
  let val ((name, _), pos) = Args.dest_src src in
wenzelm@24574
   122
    (case Symtab.lookup (! global_parsers) name of
wenzelm@24574
   123
      NONE => error ("Unknown ML antiquotation command: " ^ quote name ^ Position.str_of pos)
wenzelm@27343
   124
    | SOME scan => Args.context_syntax "ML antiquotation" scan src ctxt)
wenzelm@24574
   125
  end;
wenzelm@24574
   126
wenzelm@27343
   127
end;
wenzelm@27343
   128
wenzelm@24574
   129
wenzelm@27343
   130
(* parsing and evaluation *)
wenzelm@24574
   131
wenzelm@27343
   132
local
wenzelm@27343
   133
wenzelm@24574
   134
structure P = OuterParse;
wenzelm@24574
   135
wenzelm@24574
   136
val antiq =
wenzelm@24574
   137
  P.!!! (P.position P.xname -- P.arguments --| Scan.ahead P.eof)
wenzelm@24574
   138
  >> (fn ((x, pos), y) => Args.src ((x, y), pos));
wenzelm@24574
   139
wenzelm@27343
   140
fun eval_antiquotes struct_name (txt, pos) opt_ctxt =
wenzelm@24574
   141
  let
wenzelm@27343
   142
    val ants = Antiquote.scan_antiquotes (txt, pos);
wenzelm@27343
   143
    val ((ml_env, ml_body), opt_ctxt') =
wenzelm@27343
   144
      if not (exists Antiquote.is_antiq ants) then (("", Symbol.escape txt), opt_ctxt)
wenzelm@27343
   145
      else
wenzelm@27343
   146
        let
wenzelm@27343
   147
          val ctxt =
wenzelm@27343
   148
            (case opt_ctxt of
wenzelm@27343
   149
              NONE => error ("Unknown context -- cannot expand ML antiquotations" ^
wenzelm@27343
   150
                Position.str_of pos)
wenzelm@27343
   151
            | SOME context => Context.proof_of context);
wenzelm@27343
   152
      
wenzelm@27343
   153
          val lex = get_lexicon ();
wenzelm@27343
   154
          fun no_decl _ = ("", "");
wenzelm@27343
   155
      
wenzelm@27343
   156
          fun expand (Antiquote.Text s) state = (K ("", Symbol.escape s), state)
wenzelm@27343
   157
            | expand (Antiquote.Antiq x) (scope, background) =
wenzelm@27343
   158
                let
wenzelm@27343
   159
                  val context = Stack.top scope;
wenzelm@27343
   160
                  val (f, context') = antiquotation (Antiquote.scan_arguments lex antiq x) context;
wenzelm@27343
   161
                  val (decl, background') = f {background = background, struct_name = struct_name};
wenzelm@27343
   162
                in (decl, (Stack.map_top (K context') scope, background')) end
wenzelm@27343
   163
            | expand (Antiquote.Open _) (scope, background) =
wenzelm@27343
   164
                (no_decl, (Stack.push scope, background))
wenzelm@27343
   165
            | expand (Antiquote.Close _) (scope, background) =
wenzelm@27343
   166
                (no_decl, (Stack.pop scope, background));
wenzelm@24574
   167
wenzelm@27343
   168
          val (decls, (_, ctxt')) = fold_map expand ants (Stack.init ctxt, ctxt);
wenzelm@27343
   169
          val ml = decls |> map (fn decl => decl ctxt') |> split_list |> pairself implode;
wenzelm@27343
   170
        in (ml, SOME (Context.Proof ctxt')) end;
wenzelm@27343
   171
  in (("structure " ^ struct_name ^ " =\nstruct\n" ^ ml_env ^ "end;", ml_body), opt_ctxt') end;
wenzelm@24574
   172
wenzelm@24574
   173
in
wenzelm@24574
   174
wenzelm@24574
   175
val trace = ref false;
wenzelm@24574
   176
wenzelm@26385
   177
fun eval_wrapper pr verbose pos txt =
wenzelm@24574
   178
  let
wenzelm@26473
   179
    val struct_name =
wenzelm@26473
   180
      if Multithreading.available then "Isabelle" ^ serial_string ()
wenzelm@26473
   181
      else "Isabelle";
wenzelm@27343
   182
    val ((env, body), env_ctxt) = eval_antiquotes struct_name (txt, pos) (Context.thread_data ());
wenzelm@27343
   183
    val _ = if ! trace then tracing (cat_lines [env, body]) else ();
wenzelm@27343
   184
    fun eval_raw p =
wenzelm@26385
   185
      use_text (the_default 1 (Position.line_of p), the_default "ML" (Position.file_of p)) pr;
wenzelm@25755
   186
  in
wenzelm@27343
   187
    Context.setmp_thread_data env_ctxt (fn () => eval_raw Position.none false env) ();
wenzelm@27343
   188
    NAMED_CRITICAL "ML" (fn () => eval_raw pos verbose body); (* FIXME non-critical with local ML env *)
wenzelm@26473
   189
    forget_structure struct_name
wenzelm@25755
   190
  end;
wenzelm@24574
   191
wenzelm@24574
   192
end;
wenzelm@24574
   193
wenzelm@24574
   194
wenzelm@24574
   195
(* ML evaluation *)
wenzelm@24574
   196
wenzelm@26455
   197
val eval = eval_wrapper Output.ml_output;
wenzelm@26881
   198
fun eval_file path = eval true (Path.position path) (File.read path);
wenzelm@24574
   199
wenzelm@26455
   200
fun eval_in context verbose pos txt =
wenzelm@26455
   201
  Context.setmp_thread_data context (fn () => eval verbose pos txt) ();
wenzelm@25204
   202
wenzelm@25700
   203
fun evaluate pr verbose (ref_name, r) txt = NAMED_CRITICAL "ML" (fn () =>
wenzelm@25204
   204
  let
wenzelm@25204
   205
    val _ = r := NONE;
wenzelm@26385
   206
    val _ = eval_wrapper pr verbose Position.none
wenzelm@26385
   207
      ("val _ = (" ^ ref_name ^ " := SOME (fn () => " ^ txt ^ "))");
wenzelm@25204
   208
  in (case ! r of NONE => error ("Bad evaluation for " ^ ref_name) | SOME e => e) end) ();
wenzelm@24574
   209
wenzelm@26455
   210
fun expression pos bind body txt =
wenzelm@26455
   211
  exec (fn () => eval false pos
wenzelm@26455
   212
    ("Context.set_thread_data (SOME (let " ^ bind ^ " = " ^ txt ^ " in " ^ body ^
wenzelm@26455
   213
      " end (ML_Context.the_generic_context ())));"));
wenzelm@26455
   214
wenzelm@24574
   215
end;
wenzelm@24574
   216
wenzelm@24574
   217
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
wenzelm@24574
   218
open Basic_ML_Context;