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