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