src/Pure/ML/ml_context.ML
author wenzelm
Thu Mar 27 14:41:20 2008 +0100 (2008-03-27)
changeset 26432 095e448b95a0
parent 26416 a66f86ef7bb9
child 26455 1757a6e049f4
permissions -rw-r--r--
renamed ML_Context.the_context to ML_Context.the_global_context;
moved old the_context to old_goals.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 store_thm: string * thm -> thm
wenzelm@26416
    11
  val store_thms: string * thm list -> thm list
wenzelm@26416
    12
  val bind_thm: string * thm -> unit
wenzelm@26416
    13
  val bind_thms: string * thm list -> unit
wenzelm@24574
    14
  val thm: xstring -> thm
wenzelm@24574
    15
  val thms: xstring -> thm list
wenzelm@25204
    16
end
wenzelm@24574
    17
wenzelm@24574
    18
signature ML_CONTEXT =
wenzelm@24574
    19
sig
wenzelm@24574
    20
  include BASIC_ML_CONTEXT
wenzelm@24574
    21
  val the_generic_context: unit -> Context.generic
wenzelm@26432
    22
  val the_global_context: unit -> theory
wenzelm@24574
    23
  val the_local_context: unit -> Proof.context
wenzelm@24574
    24
  val pass_context: Context.generic -> ('a -> 'b) -> 'a -> 'b * Context.generic
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@24574
    28
  val add_keywords: string list -> unit
wenzelm@24574
    29
  val inline_antiq: string ->
wenzelm@24574
    30
    (Context.generic * Args.T list -> string * (Context.generic * Args.T list)) -> unit
wenzelm@24574
    31
  val value_antiq: string ->
wenzelm@24574
    32
    (Context.generic * Args.T list -> (string * string) * (Context.generic * Args.T list)) -> unit
wenzelm@24574
    33
  val proj_value_antiq: string -> (Context.generic * Args.T list ->
wenzelm@24574
    34
      (string * string * string) * (Context.generic * Args.T list)) -> unit
wenzelm@26385
    35
  val eval_antiquotes_fn: (string * Position.T -> string * string) ref  (* FIXME tmp *)
wenzelm@24574
    36
  val trace: bool ref
wenzelm@26385
    37
  val use_mltext: bool -> Position.T -> string -> Context.generic option -> unit
wenzelm@26385
    38
  val use_mltext_update: bool -> Position.T -> string -> Context.generic -> Context.generic
wenzelm@26385
    39
  val use_let: Position.T -> string -> string -> string -> Context.generic -> Context.generic
wenzelm@24574
    40
  val use: Path.T -> unit
wenzelm@25204
    41
  val evaluate: (string -> unit) * (string -> 'b) -> bool ->
wenzelm@25204
    42
    string * (unit -> 'a) option ref -> string -> 'a
wenzelm@25204
    43
end
wenzelm@24574
    44
wenzelm@24574
    45
structure ML_Context: ML_CONTEXT =
wenzelm@24574
    46
struct
wenzelm@24574
    47
wenzelm@26416
    48
(** implicit ML context **)
wenzelm@24574
    49
wenzelm@26416
    50
val the_generic_context = Context.the_thread_data;
wenzelm@26432
    51
val the_global_context = Context.theory_of o the_generic_context;
wenzelm@24574
    52
val the_local_context = Context.proof_of o the_generic_context;
wenzelm@24574
    53
wenzelm@24574
    54
fun pass_context context f x =
wenzelm@26416
    55
  (case Context.setmp_thread_data (SOME context) (fn () => (f x, Context.thread_data ())) () of
wenzelm@24574
    56
    (y, SOME context') => (y, context')
wenzelm@26416
    57
  | (_, NONE) => error "Lost context after evaluation");
wenzelm@26416
    58
wenzelm@26416
    59
wenzelm@26416
    60
(* theorem bindings *)
wenzelm@26416
    61
wenzelm@26416
    62
val store_thms = PureThy.smart_store_thms;
wenzelm@26416
    63
fun store_thm (name, th) = the_single (store_thms (name, [th]));
wenzelm@26416
    64
wenzelm@26416
    65
val stored_thms: thm list ref = ref [];
wenzelm@26416
    66
wenzelm@26416
    67
fun no_ml name =
wenzelm@26416
    68
  if name = "" then true
wenzelm@26416
    69
  else if ML_Syntax.is_identifier name then false
wenzelm@26416
    70
  else error ("Cannot bind theorem(s) " ^ quote name ^ " as ML value");
wenzelm@26416
    71
wenzelm@26416
    72
val use_text_verbose = use_text (0, "") Output.ml_output true;
wenzelm@24574
    73
wenzelm@26416
    74
fun ml_store_thm (name, th) =
wenzelm@26416
    75
  let val th' = store_thm (name, th) in
wenzelm@26416
    76
    if no_ml name then ()
wenzelm@26416
    77
    else setmp stored_thms [th']
wenzelm@26416
    78
      (fn () => use_text_verbose ("val " ^ name ^ " = hd (! ML_Context.stored_thms);")) ()
wenzelm@26416
    79
  end;
wenzelm@26416
    80
wenzelm@26416
    81
fun ml_store_thms (name, ths) =
wenzelm@26416
    82
  let val ths' = store_thms (name, ths) in
wenzelm@26416
    83
    if no_ml name then ()
wenzelm@26416
    84
    else setmp stored_thms ths'
wenzelm@26416
    85
      (fn () => use_text_verbose ("val " ^ name ^ " = ! ML_Context.stored_thms;")) ()
wenzelm@26416
    86
  end;
wenzelm@26416
    87
wenzelm@26416
    88
fun bind_thm (name, thm) = ml_store_thm (name, Drule.standard thm);
wenzelm@26416
    89
fun bind_thms (name, thms) = ml_store_thms (name, map Drule.standard thms);
wenzelm@24574
    90
wenzelm@24574
    91
wenzelm@24574
    92
wenzelm@24574
    93
(** ML antiquotations **)
wenzelm@24574
    94
wenzelm@24574
    95
(* maintain keywords *)
wenzelm@24574
    96
wenzelm@24574
    97
val global_lexicon = ref Scan.empty_lexicon;
wenzelm@24574
    98
wenzelm@24574
    99
fun add_keywords keywords = CRITICAL (fn () =>
wenzelm@24574
   100
  change global_lexicon (Scan.extend_lexicon (map Symbol.explode keywords)));
wenzelm@24574
   101
wenzelm@24574
   102
wenzelm@24574
   103
(* maintain commands *)
wenzelm@24574
   104
wenzelm@24574
   105
datatype antiq = Inline of string | ProjValue of string * string * string;
wenzelm@24574
   106
val global_parsers = ref (Symtab.empty:
wenzelm@24574
   107
  (Context.generic * Args.T list -> antiq * (Context.generic * Args.T list)) Symtab.table);
wenzelm@24574
   108
wenzelm@24574
   109
local
wenzelm@24574
   110
wenzelm@24574
   111
fun add_item kind name scan = CRITICAL (fn () =>
wenzelm@24574
   112
  change global_parsers (fn tab =>
wenzelm@24574
   113
   (if not (Symtab.defined tab name) then ()
wenzelm@24574
   114
    else warning ("Redefined ML antiquotation: " ^ quote name);
wenzelm@24574
   115
    Symtab.update (name, scan >> kind) tab)));
wenzelm@24574
   116
wenzelm@24574
   117
in
wenzelm@24574
   118
wenzelm@24574
   119
val inline_antiq = add_item Inline;
wenzelm@24574
   120
val proj_value_antiq = add_item ProjValue;
wenzelm@24574
   121
fun value_antiq name scan = proj_value_antiq name (scan >> (fn (a, s) => (a, s, "")));
wenzelm@24574
   122
wenzelm@24574
   123
end;
wenzelm@24574
   124
wenzelm@24574
   125
wenzelm@24574
   126
(* command syntax *)
wenzelm@24574
   127
wenzelm@24574
   128
fun syntax scan src =
wenzelm@24574
   129
  #1 (Args.context_syntax "ML antiquotation" scan src (the_local_context ()));
wenzelm@24574
   130
wenzelm@24574
   131
fun command src =
wenzelm@24574
   132
  let val ((name, _), pos) = Args.dest_src src in
wenzelm@24574
   133
    (case Symtab.lookup (! global_parsers) name of
wenzelm@24574
   134
      NONE => error ("Unknown ML antiquotation command: " ^ quote name ^ Position.str_of pos)
wenzelm@24574
   135
    | SOME scan => syntax scan src)
wenzelm@24574
   136
  end;
wenzelm@24574
   137
wenzelm@24574
   138
wenzelm@24574
   139
(* outer syntax *)
wenzelm@24574
   140
wenzelm@24574
   141
structure T = OuterLex;
wenzelm@24574
   142
structure P = OuterParse;
wenzelm@24574
   143
wenzelm@24574
   144
val antiq =
wenzelm@24574
   145
  P.!!! (P.position P.xname -- P.arguments --| Scan.ahead P.eof)
wenzelm@24574
   146
  >> (fn ((x, pos), y) => Args.src ((x, y), pos));
wenzelm@24574
   147
wenzelm@24574
   148
wenzelm@24574
   149
(* input/output wrappers *)
wenzelm@24574
   150
wenzelm@24574
   151
local
wenzelm@24574
   152
wenzelm@24574
   153
val isabelle_struct = enclose "structure Isabelle =\nstruct\n" "end;";
wenzelm@24574
   154
val isabelle_struct0 = isabelle_struct "";
wenzelm@24574
   155
wenzelm@26385
   156
fun eval_antiquotes txt_pos =
wenzelm@24574
   157
  let
wenzelm@26385
   158
    val ants = Antiquote.scan_antiquotes txt_pos;
wenzelm@24574
   159
wenzelm@24574
   160
    fun expand (Antiquote.Text s) names = (("", Symbol.escape s), names)
wenzelm@24574
   161
      | expand (Antiquote.Antiq x) names =
wenzelm@24574
   162
          (case command (Antiquote.scan_arguments (! global_lexicon) antiq x) of
wenzelm@24574
   163
            Inline s => (("", s), names)
wenzelm@24574
   164
          | ProjValue (a, s, f) =>
wenzelm@24574
   165
              let
wenzelm@24574
   166
                val a' = if ML_Syntax.is_identifier a then a else "val";
wenzelm@24574
   167
                val ([b], names') = Name.variants [a'] names;
wenzelm@24574
   168
                val binding = "val " ^ b ^ " = " ^ s ^ ";\n";
wenzelm@24574
   169
                val value =
wenzelm@24574
   170
                  if f = "" then "Isabelle." ^ b
wenzelm@24574
   171
                  else "(" ^ f ^ " Isabelle." ^ b ^ ")";
wenzelm@24574
   172
              in ((binding, value), names') end);
wenzelm@24574
   173
wenzelm@24574
   174
    val (decls, body) =
wenzelm@25700
   175
      NAMED_CRITICAL "ML" (fn () => fold_map expand ants ML_Syntax.reserved)
wenzelm@24574
   176
      |> #1 |> split_list |> pairself implode;
wenzelm@25700
   177
  in (isabelle_struct decls, body) end;
wenzelm@24574
   178
wenzelm@24574
   179
in
wenzelm@24574
   180
wenzelm@24574
   181
val eval_antiquotes_fn = ref eval_antiquotes;
wenzelm@24574
   182
wenzelm@24574
   183
val trace = ref false;
wenzelm@24574
   184
wenzelm@26385
   185
fun eval_wrapper pr verbose pos txt =
wenzelm@24574
   186
  let
wenzelm@26385
   187
    val (txt1, txt2) = ! eval_antiquotes_fn (txt, pos);  (* FIXME tmp hook *)
wenzelm@24574
   188
    val _ = if ! trace then tracing (cat_lines [txt1, txt2]) else ();
wenzelm@26385
   189
    fun eval p =
wenzelm@26385
   190
      use_text (the_default 1 (Position.line_of p), the_default "ML" (Position.file_of p)) pr;
wenzelm@25755
   191
  in
wenzelm@25755
   192
    NAMED_CRITICAL "ML" (fn () =>
wenzelm@26385
   193
     (eval Position.none false txt1;
wenzelm@26385
   194
      eval pos verbose txt2;
wenzelm@26385
   195
      eval Position.none false isabelle_struct0))
wenzelm@25755
   196
  end;
wenzelm@24574
   197
wenzelm@24574
   198
end;
wenzelm@24574
   199
wenzelm@24574
   200
wenzelm@24574
   201
(* ML evaluation *)
wenzelm@24574
   202
wenzelm@26385
   203
fun use_mltext verbose pos txt opt_context =
wenzelm@26416
   204
  Context.setmp_thread_data opt_context (fn () => eval_wrapper Output.ml_output verbose pos txt) ();
wenzelm@24574
   205
wenzelm@26385
   206
fun use_mltext_update verbose pos txt context =
wenzelm@26385
   207
  #2 (pass_context context (fn () => eval_wrapper Output.ml_output verbose pos txt) ());
wenzelm@24574
   208
wenzelm@26385
   209
fun use_let pos bind body txt =
wenzelm@26385
   210
  use_mltext_update false pos
wenzelm@26416
   211
    ("Context.set_thread_data (SOME (let " ^ bind ^ " = " ^ txt ^ " in " ^ body ^
wenzelm@26385
   212
      " end (ML_Context.the_generic_context ())));");
wenzelm@24574
   213
wenzelm@26385
   214
fun use path = eval_wrapper Output.ml_output true (Position.path path) (File.read path);
wenzelm@25204
   215
wenzelm@25700
   216
fun evaluate pr verbose (ref_name, r) txt = NAMED_CRITICAL "ML" (fn () =>
wenzelm@25204
   217
  let
wenzelm@25204
   218
    val _ = r := NONE;
wenzelm@26385
   219
    val _ = eval_wrapper pr verbose Position.none
wenzelm@26385
   220
      ("val _ = (" ^ ref_name ^ " := SOME (fn () => " ^ txt ^ "))");
wenzelm@25204
   221
  in (case ! r of NONE => error ("Bad evaluation for " ^ ref_name) | SOME e => e) end) ();
wenzelm@24574
   222
wenzelm@24574
   223
wenzelm@24574
   224
(* basic antiquotations *)
wenzelm@24574
   225
wenzelm@26385
   226
fun context x = (Scan.state >> Context.proof_of) x;
wenzelm@25142
   227
wenzelm@26385
   228
local
wenzelm@25142
   229
wenzelm@24574
   230
val _ = value_antiq "context" (Scan.succeed ("context", "ML_Context.the_local_context ()"));
wenzelm@24574
   231
wenzelm@25142
   232
val _ = inline_antiq "sort" (context -- Scan.lift Args.name >> (fn (ctxt, s) =>
wenzelm@25142
   233
  ML_Syntax.atomic (ML_Syntax.print_sort (Syntax.read_sort ctxt s))));
wenzelm@25142
   234
wenzelm@24574
   235
val _ = inline_antiq "typ" (Args.typ >> (ML_Syntax.atomic o ML_Syntax.print_typ));
wenzelm@24574
   236
val _ = inline_antiq "term" (Args.term >> (ML_Syntax.atomic o ML_Syntax.print_term));
wenzelm@24574
   237
val _ = inline_antiq "prop" (Args.prop >> (ML_Syntax.atomic o ML_Syntax.print_term));
wenzelm@24574
   238
wenzelm@24574
   239
val _ = value_antiq "ctyp" (Args.typ >> (fn T =>
wenzelm@24574
   240
  ("ctyp",
wenzelm@26432
   241
    "Thm.ctyp_of (ML_Context.the_global_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_typ T))));
wenzelm@24574
   242
wenzelm@24574
   243
val _ = value_antiq "cterm" (Args.term >> (fn t =>
wenzelm@24574
   244
  ("cterm",
wenzelm@26432
   245
    "Thm.cterm_of (ML_Context.the_global_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_term t))));
wenzelm@24574
   246
wenzelm@24574
   247
val _ = value_antiq "cprop" (Args.prop >> (fn t =>
wenzelm@24574
   248
  ("cprop",
wenzelm@26432
   249
    "Thm.cterm_of (ML_Context.the_global_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_term t))));
wenzelm@24574
   250
wenzelm@25142
   251
fun type_ syn = (context -- Scan.lift Args.name >> (fn (ctxt, c) =>
wenzelm@24695
   252
    #1 (Term.dest_Type (ProofContext.read_tyname ctxt c))
wenzelm@25142
   253
    |> syn ? Sign.base_name
wenzelm@24695
   254
    |> ML_Syntax.print_string));
wenzelm@24574
   255
wenzelm@25142
   256
val _ = inline_antiq "type_name" (type_ false);
wenzelm@25142
   257
val _ = inline_antiq "type_syntax" (type_ true);
wenzelm@25142
   258
wenzelm@26385
   259
fun const syn = context -- Scan.lift Args.name >> (fn (ctxt, c) =>
wenzelm@25346
   260
  #1 (Term.dest_Const (ProofContext.read_const_proper ctxt c))
wenzelm@24574
   261
  |> syn ? ProofContext.const_syntax_name ctxt
wenzelm@24574
   262
  |> ML_Syntax.print_string);
wenzelm@24574
   263
wenzelm@24574
   264
val _ = inline_antiq "const_name" (const false);
wenzelm@24574
   265
val _ = inline_antiq "const_syntax" (const true);
wenzelm@24574
   266
wenzelm@26188
   267
val _ = inline_antiq "const"
wenzelm@26385
   268
  (context -- Scan.lift Args.name --
wenzelm@26188
   269
    Scan.optional (Scan.lift (Args.$$$ "(") |-- Args.enum1 "," Args.typ --| Scan.lift (Args.$$$ ")")) []
wenzelm@26188
   270
    >> (fn ((ctxt, c), Ts) =>
wenzelm@26188
   271
      let
wenzelm@26188
   272
        val (c, _) = Term.dest_Const (ProofContext.read_const_proper ctxt c);
haftmann@26268
   273
      in ML_Syntax.atomic (ML_Syntax.print_term (ProofContext.mk_const ctxt (c, Ts))) end));
wenzelm@26188
   274
wenzelm@25142
   275
in val _ = () end;
wenzelm@24574
   276
wenzelm@24574
   277
wenzelm@26416
   278
wenzelm@24574
   279
(** fact references **)
wenzelm@24574
   280
wenzelm@26346
   281
fun thm name = ProofContext.get_thm (the_local_context ()) name;
wenzelm@26346
   282
fun thms name = ProofContext.get_thms (the_local_context ()) name;
wenzelm@24574
   283
wenzelm@24574
   284
wenzelm@24574
   285
local
wenzelm@24574
   286
wenzelm@26336
   287
fun print_interval (Facts.FromTo arg) =
wenzelm@26336
   288
      "Facts.FromTo " ^ ML_Syntax.print_pair ML_Syntax.print_int ML_Syntax.print_int arg
wenzelm@26336
   289
  | print_interval (Facts.From i) = "Facts.From " ^ ML_Syntax.print_int i
wenzelm@26336
   290
  | print_interval (Facts.Single i) = "Facts.Single " ^ ML_Syntax.print_int i;
wenzelm@24574
   291
wenzelm@26368
   292
fun thm_antiq kind get get_name = value_antiq kind
wenzelm@26385
   293
  (context -- Scan.lift (Args.position Args.name -- Scan.option Args.thm_sel)
wenzelm@26374
   294
  >> (fn (ctxt, ((name, pos), sel)) =>
wenzelm@26368
   295
      let
wenzelm@26374
   296
        val _ = get ctxt (Facts.Named ((name, pos), sel));
wenzelm@26374
   297
        val txt =
wenzelm@26374
   298
          "(Facts.Named ((" ^ ML_Syntax.print_string name ^ ", Position.none), " ^
wenzelm@26374
   299
            ML_Syntax.print_option (ML_Syntax.print_list print_interval) sel ^ "))";
wenzelm@26374
   300
      in (name, get_name ^ " (ML_Context.the_local_context ()) " ^ txt) end));
wenzelm@24574
   301
wenzelm@24574
   302
in
wenzelm@24574
   303
wenzelm@24574
   304
val _ = add_keywords ["(", ")", "-", ","];
wenzelm@26368
   305
val _ = thm_antiq "thm" ProofContext.get_fact_single "ProofContext.get_fact_single";
wenzelm@26368
   306
val _ = thm_antiq "thms" ProofContext.get_fact "ProofContext.get_fact";
wenzelm@24574
   307
wenzelm@24574
   308
end;
wenzelm@24574
   309
wenzelm@24685
   310
val _ = proj_value_antiq "cpat" (Scan.lift Args.name >>
wenzelm@24685
   311
    (fn name => ("cpat",
wenzelm@26432
   312
      "Thm.cterm_of (ML_Context.the_global_context ()) (Syntax.read_term \
wenzelm@24685
   313
      \(ProofContext.set_mode ProofContext.mode_pattern (ML_Context.the_local_context ()))"
wenzelm@24685
   314
      ^ ML_Syntax.print_string name ^ ")", "")));
wenzelm@24574
   315
wenzelm@24574
   316
end;
wenzelm@24574
   317
wenzelm@24574
   318
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
wenzelm@24574
   319
open Basic_ML_Context;