src/Pure/ML/ml_context.ML
author wenzelm
Wed Jun 25 17:38:39 2008 +0200 (2008-06-25)
changeset 27359 54b5367a827a
parent 27343 4b28b80dd1f8
child 27378 0968c0d0b969
permissions -rw-r--r--
re-use official outer keywords;
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
wenzelm@26455
    32
  val eval: bool -> Position.T -> string -> unit
wenzelm@26455
    33
  val eval_file: Path.T -> unit
wenzelm@26455
    34
  val eval_in: Context.generic option -> bool -> Position.T -> string -> unit
wenzelm@25204
    35
  val evaluate: (string -> unit) * (string -> 'b) -> bool ->
wenzelm@25204
    36
    string * (unit -> 'a) option ref -> string -> 'a
wenzelm@26455
    37
  val expression: Position.T -> string -> string -> string -> Context.generic -> Context.generic
wenzelm@25204
    38
end
wenzelm@24574
    39
wenzelm@24574
    40
structure ML_Context: ML_CONTEXT =
wenzelm@24574
    41
struct
wenzelm@24574
    42
wenzelm@26416
    43
(** implicit ML context **)
wenzelm@24574
    44
wenzelm@26416
    45
val the_generic_context = Context.the_thread_data;
wenzelm@26432
    46
val the_global_context = Context.theory_of o the_generic_context;
wenzelm@24574
    47
val the_local_context = Context.proof_of o the_generic_context;
wenzelm@24574
    48
wenzelm@26455
    49
fun exec (e: unit -> unit) context =
wenzelm@26455
    50
  (case Context.setmp_thread_data (SOME context) (fn () => (e (); Context.thread_data ())) () of
wenzelm@26455
    51
    SOME context' => context'
wenzelm@26455
    52
  | NONE => error "Missing context after execution");
wenzelm@26416
    53
wenzelm@26416
    54
wenzelm@26416
    55
(* theorem bindings *)
wenzelm@26416
    56
wenzelm@26416
    57
val stored_thms: thm list ref = ref [];
wenzelm@26416
    58
wenzelm@26492
    59
fun ml_store sel (name, ths) =
wenzelm@26492
    60
  let
wenzelm@26492
    61
    val ths' = Context.>>> (Context.map_theory_result (PureThy.store_thms (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@26492
    67
        use_text (0, "") Output.ml_output 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@27343
    92
val global_parsers = ref (Symtab.empty:
wenzelm@27343
    93
  (Context.generic * Args.T list -> antiq * (Context.generic * Args.T list)) Symtab.table);
wenzelm@27343
    94
wenzelm@27343
    95
in
wenzelm@24574
    96
wenzelm@27343
    97
fun add_antiq name scan = CRITICAL (fn () =>
wenzelm@27343
    98
  change global_parsers (fn tab =>
wenzelm@27343
    99
   (if not (Symtab.defined tab name) then ()
wenzelm@27343
   100
    else warning ("Redefined ML antiquotation: " ^ quote name);
wenzelm@27343
   101
    Symtab.update (name, scan) tab)));
wenzelm@27343
   102
wenzelm@27343
   103
fun antiquotation src ctxt =
wenzelm@24574
   104
  let val ((name, _), pos) = Args.dest_src src in
wenzelm@24574
   105
    (case Symtab.lookup (! global_parsers) name of
wenzelm@24574
   106
      NONE => error ("Unknown ML antiquotation command: " ^ quote name ^ Position.str_of pos)
wenzelm@27343
   107
    | SOME scan => Args.context_syntax "ML antiquotation" scan 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@24574
   118
wenzelm@24574
   119
val antiq =
wenzelm@24574
   120
  P.!!! (P.position P.xname -- P.arguments --| Scan.ahead P.eof)
wenzelm@24574
   121
  >> (fn ((x, pos), y) => Args.src ((x, y), pos));
wenzelm@24574
   122
wenzelm@27343
   123
fun eval_antiquotes struct_name (txt, pos) opt_ctxt =
wenzelm@24574
   124
  let
wenzelm@27343
   125
    val ants = Antiquote.scan_antiquotes (txt, pos);
wenzelm@27343
   126
    val ((ml_env, ml_body), opt_ctxt') =
wenzelm@27343
   127
      if not (exists Antiquote.is_antiq ants) then (("", Symbol.escape txt), opt_ctxt)
wenzelm@27343
   128
      else
wenzelm@27343
   129
        let
wenzelm@27343
   130
          val ctxt =
wenzelm@27343
   131
            (case opt_ctxt of
wenzelm@27343
   132
              NONE => error ("Unknown context -- cannot expand ML antiquotations" ^
wenzelm@27343
   133
                Position.str_of pos)
wenzelm@27343
   134
            | SOME context => Context.proof_of context);
wenzelm@27343
   135
      
wenzelm@27359
   136
          val lex = #1 (OuterKeyword.get_lexicons ());
wenzelm@27343
   137
          fun no_decl _ = ("", "");
wenzelm@27343
   138
      
wenzelm@27343
   139
          fun expand (Antiquote.Text s) state = (K ("", Symbol.escape s), state)
wenzelm@27343
   140
            | expand (Antiquote.Antiq x) (scope, background) =
wenzelm@27343
   141
                let
wenzelm@27343
   142
                  val context = Stack.top scope;
wenzelm@27343
   143
                  val (f, context') = antiquotation (Antiquote.scan_arguments lex antiq x) context;
wenzelm@27343
   144
                  val (decl, background') = f {background = background, struct_name = struct_name};
wenzelm@27343
   145
                in (decl, (Stack.map_top (K context') scope, background')) end
wenzelm@27343
   146
            | expand (Antiquote.Open _) (scope, background) =
wenzelm@27343
   147
                (no_decl, (Stack.push scope, background))
wenzelm@27343
   148
            | expand (Antiquote.Close _) (scope, background) =
wenzelm@27343
   149
                (no_decl, (Stack.pop scope, background));
wenzelm@24574
   150
wenzelm@27343
   151
          val (decls, (_, ctxt')) = fold_map expand ants (Stack.init ctxt, ctxt);
wenzelm@27343
   152
          val ml = decls |> map (fn decl => decl ctxt') |> split_list |> pairself implode;
wenzelm@27343
   153
        in (ml, SOME (Context.Proof ctxt')) end;
wenzelm@27343
   154
  in (("structure " ^ struct_name ^ " =\nstruct\n" ^ ml_env ^ "end;", ml_body), opt_ctxt') end;
wenzelm@24574
   155
wenzelm@24574
   156
in
wenzelm@24574
   157
wenzelm@24574
   158
val trace = ref false;
wenzelm@24574
   159
wenzelm@26385
   160
fun eval_wrapper pr verbose pos txt =
wenzelm@24574
   161
  let
wenzelm@26473
   162
    val struct_name =
wenzelm@26473
   163
      if Multithreading.available then "Isabelle" ^ serial_string ()
wenzelm@26473
   164
      else "Isabelle";
wenzelm@27343
   165
    val ((env, body), env_ctxt) = eval_antiquotes struct_name (txt, pos) (Context.thread_data ());
wenzelm@27343
   166
    val _ = if ! trace then tracing (cat_lines [env, body]) else ();
wenzelm@27343
   167
    fun eval_raw p =
wenzelm@26385
   168
      use_text (the_default 1 (Position.line_of p), the_default "ML" (Position.file_of p)) pr;
wenzelm@25755
   169
  in
wenzelm@27343
   170
    Context.setmp_thread_data env_ctxt (fn () => eval_raw Position.none false env) ();
wenzelm@27343
   171
    NAMED_CRITICAL "ML" (fn () => eval_raw pos verbose body); (* FIXME non-critical with local ML env *)
wenzelm@26473
   172
    forget_structure struct_name
wenzelm@25755
   173
  end;
wenzelm@24574
   174
wenzelm@24574
   175
end;
wenzelm@24574
   176
wenzelm@24574
   177
wenzelm@24574
   178
(* ML evaluation *)
wenzelm@24574
   179
wenzelm@26455
   180
val eval = eval_wrapper Output.ml_output;
wenzelm@26881
   181
fun eval_file path = eval true (Path.position path) (File.read path);
wenzelm@24574
   182
wenzelm@26455
   183
fun eval_in context verbose pos txt =
wenzelm@26455
   184
  Context.setmp_thread_data context (fn () => eval verbose pos txt) ();
wenzelm@25204
   185
wenzelm@25700
   186
fun evaluate pr verbose (ref_name, r) txt = NAMED_CRITICAL "ML" (fn () =>
wenzelm@25204
   187
  let
wenzelm@25204
   188
    val _ = r := NONE;
wenzelm@26385
   189
    val _ = eval_wrapper pr verbose Position.none
wenzelm@26385
   190
      ("val _ = (" ^ ref_name ^ " := SOME (fn () => " ^ txt ^ "))");
wenzelm@25204
   191
  in (case ! r of NONE => error ("Bad evaluation for " ^ ref_name) | SOME e => e) end) ();
wenzelm@24574
   192
wenzelm@26455
   193
fun expression pos bind body txt =
wenzelm@26455
   194
  exec (fn () => eval false pos
wenzelm@26455
   195
    ("Context.set_thread_data (SOME (let " ^ bind ^ " = " ^ txt ^ " in " ^ body ^
wenzelm@26455
   196
      " end (ML_Context.the_generic_context ())));"));
wenzelm@26455
   197
wenzelm@24574
   198
end;
wenzelm@24574
   199
wenzelm@24574
   200
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
wenzelm@24574
   201
open Basic_ML_Context;