src/Pure/ML/ml_context.ML
author wenzelm
Thu Mar 20 17:38:55 2008 +0100 (2008-03-20 ago)
changeset 26368 3e74b09ce466
parent 26361 7946f459c6c8
child 26374 be47e9a83b4f
permissions -rw-r--r--
thm_antiq: produce error at runtime, not compile time;
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@24574
    10
  val the_context: unit -> theory
wenzelm@24574
    11
  val thm: xstring -> thm
wenzelm@24574
    12
  val thms: xstring -> thm list
wenzelm@25204
    13
end
wenzelm@24574
    14
wenzelm@24574
    15
signature ML_CONTEXT =
wenzelm@24574
    16
sig
wenzelm@24574
    17
  include BASIC_ML_CONTEXT
wenzelm@24574
    18
  val get_context: unit -> Context.generic option
wenzelm@24574
    19
  val set_context: Context.generic option -> unit
wenzelm@24574
    20
  val setmp: Context.generic option -> ('a -> 'b) -> 'a -> 'b
wenzelm@24574
    21
  val the_generic_context: unit -> Context.generic
wenzelm@24574
    22
  val the_local_context: unit -> Proof.context
wenzelm@24574
    23
  val pass: Context.generic option -> ('a -> 'b) -> 'a -> 'b * Context.generic option
wenzelm@24574
    24
  val pass_context: Context.generic -> ('a -> 'b) -> 'a -> 'b * Context.generic
wenzelm@24574
    25
  val save: ('a -> 'b) -> 'a -> 'b
wenzelm@24574
    26
  val >> : (theory -> theory) -> unit
wenzelm@24574
    27
  val add_keywords: string list -> unit
wenzelm@24574
    28
  val inline_antiq: string ->
wenzelm@24574
    29
    (Context.generic * Args.T list -> string * (Context.generic * Args.T list)) -> unit
wenzelm@24574
    30
  val value_antiq: string ->
wenzelm@24574
    31
    (Context.generic * Args.T list -> (string * string) * (Context.generic * Args.T list)) -> unit
wenzelm@24574
    32
  val proj_value_antiq: string -> (Context.generic * Args.T list ->
wenzelm@24574
    33
      (string * string * string) * (Context.generic * Args.T list)) -> unit
wenzelm@24574
    34
  val eval_antiquotes_fn: (string -> string * string) ref  (* FIXME tmp *)
wenzelm@24574
    35
  val trace: bool ref
wenzelm@24574
    36
  val use_mltext: string -> bool -> Context.generic option -> unit
wenzelm@24574
    37
  val use_mltext_update: string -> bool -> Context.generic -> Context.generic
wenzelm@24574
    38
  val use_let: string -> string -> string -> Context.generic -> Context.generic
wenzelm@24574
    39
  val use: Path.T -> unit
wenzelm@25204
    40
  val evaluate: (string -> unit) * (string -> 'b) -> bool ->
wenzelm@25204
    41
    string * (unit -> 'a) option ref -> string -> 'a
wenzelm@25204
    42
end
wenzelm@24574
    43
wenzelm@24574
    44
structure ML_Context: ML_CONTEXT =
wenzelm@24574
    45
struct
wenzelm@24574
    46
wenzelm@24574
    47
(** Implicit ML context **)
wenzelm@24574
    48
wenzelm@24574
    49
local
wenzelm@24574
    50
  val current_context = ref (NONE: Context.generic option);
wenzelm@24574
    51
in
wenzelm@24574
    52
  fun get_context () = ! current_context;
wenzelm@24574
    53
  fun set_context opt_context = current_context := opt_context;
wenzelm@24574
    54
  fun setmp opt_context f x = Library.setmp current_context opt_context f x;
wenzelm@24574
    55
end;
wenzelm@24574
    56
wenzelm@24574
    57
fun the_generic_context () =
wenzelm@24574
    58
  (case get_context () of
wenzelm@24574
    59
    SOME context => context
wenzelm@24574
    60
  | _ => error "Unknown context");
wenzelm@24574
    61
wenzelm@24574
    62
val the_local_context = Context.proof_of o the_generic_context;
wenzelm@24574
    63
wenzelm@24574
    64
val the_context = Context.theory_of o the_generic_context;
wenzelm@24574
    65
wenzelm@24574
    66
fun pass opt_context f x =
wenzelm@24574
    67
  setmp opt_context (fn x => let val y = f x in (y, get_context ()) end) x;
wenzelm@24574
    68
wenzelm@24574
    69
fun pass_context context f x =
wenzelm@24574
    70
  (case pass (SOME context) f x of
wenzelm@24574
    71
    (y, SOME context') => (y, context')
wenzelm@24574
    72
  | (_, NONE) => error "Lost context in ML");
wenzelm@24574
    73
wenzelm@25700
    74
fun save f x = NAMED_CRITICAL "ML" (fn () => setmp (get_context ()) f x);
wenzelm@24574
    75
wenzelm@25700
    76
fun change_theory f = NAMED_CRITICAL "ML" (fn () =>
wenzelm@24574
    77
  set_context (SOME (Context.map_theory f (the_generic_context ()))));
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@24574
   141
val isabelle_struct = enclose "structure Isabelle =\nstruct\n" "end;";
wenzelm@24574
   142
val isabelle_struct0 = isabelle_struct "";
wenzelm@24574
   143
wenzelm@25700
   144
fun eval_antiquotes txt =
wenzelm@24574
   145
  let
wenzelm@24574
   146
    val ants = Antiquote.scan_antiquotes (txt, Position.line 1);
wenzelm@24574
   147
wenzelm@24574
   148
    fun expand (Antiquote.Text s) names = (("", Symbol.escape s), names)
wenzelm@24574
   149
      | expand (Antiquote.Antiq x) names =
wenzelm@24574
   150
          (case command (Antiquote.scan_arguments (! global_lexicon) antiq x) of
wenzelm@24574
   151
            Inline s => (("", s), names)
wenzelm@24574
   152
          | ProjValue (a, s, f) =>
wenzelm@24574
   153
              let
wenzelm@24574
   154
                val a' = if ML_Syntax.is_identifier a then a else "val";
wenzelm@24574
   155
                val ([b], names') = Name.variants [a'] names;
wenzelm@24574
   156
                val binding = "val " ^ b ^ " = " ^ s ^ ";\n";
wenzelm@24574
   157
                val value =
wenzelm@24574
   158
                  if f = "" then "Isabelle." ^ b
wenzelm@24574
   159
                  else "(" ^ f ^ " Isabelle." ^ b ^ ")";
wenzelm@24574
   160
              in ((binding, value), names') end);
wenzelm@24574
   161
wenzelm@24574
   162
    val (decls, body) =
wenzelm@25700
   163
      NAMED_CRITICAL "ML" (fn () => fold_map expand ants ML_Syntax.reserved)
wenzelm@24574
   164
      |> #1 |> split_list |> pairself implode;
wenzelm@25700
   165
  in (isabelle_struct decls, body) end;
wenzelm@24574
   166
wenzelm@24574
   167
in
wenzelm@24574
   168
wenzelm@24574
   169
val eval_antiquotes_fn = ref eval_antiquotes;
wenzelm@24574
   170
wenzelm@24574
   171
val trace = ref false;
wenzelm@24574
   172
wenzelm@25204
   173
fun eval_wrapper name pr verbose txt =
wenzelm@24574
   174
  let
wenzelm@25204
   175
    val (txt1, txt2) = ! eval_antiquotes_fn txt;  (* FIXME tmp hook *)
wenzelm@24574
   176
    val _ = if ! trace then tracing (cat_lines [txt1, txt2]) else ();
wenzelm@25204
   177
    fun eval nm = use_text nm pr;
wenzelm@25755
   178
  in
wenzelm@25755
   179
    NAMED_CRITICAL "ML" (fn () =>
wenzelm@25755
   180
      (eval "" false txt1; eval name verbose txt2; eval "" false isabelle_struct0))
wenzelm@25755
   181
  end;
wenzelm@24574
   182
wenzelm@25204
   183
fun ML_wrapper pr = eval_wrapper "ML" pr;
wenzelm@25204
   184
wenzelm@24574
   185
end;
wenzelm@24574
   186
wenzelm@24574
   187
wenzelm@24574
   188
(* ML evaluation *)
wenzelm@24574
   189
wenzelm@24574
   190
fun use_mltext txt verbose opt_context =
wenzelm@25204
   191
  setmp opt_context (fn () => ML_wrapper Output.ml_output verbose txt) ();
wenzelm@24574
   192
wenzelm@24574
   193
fun use_mltext_update txt verbose context =
wenzelm@25755
   194
  #2 (pass_context context (fn () => ML_wrapper Output.ml_output verbose txt) ());
wenzelm@24574
   195
wenzelm@24574
   196
fun use_context txt = use_mltext_update
wenzelm@24574
   197
  ("ML_Context.set_context (SOME ((" ^ txt ^ ") (ML_Context.the_generic_context ())));") false;
wenzelm@24574
   198
wenzelm@24574
   199
fun use_let bind body txt =
wenzelm@24574
   200
  use_context ("let " ^ bind ^ " = " ^ txt ^ " in\n" ^ body ^ " end");
wenzelm@24574
   201
wenzelm@25204
   202
fun use path = eval_wrapper (Path.implode path) Output.ml_output true (File.read path);
wenzelm@25204
   203
wenzelm@25700
   204
fun evaluate pr verbose (ref_name, r) txt = NAMED_CRITICAL "ML" (fn () =>
wenzelm@25204
   205
  let
wenzelm@25204
   206
    val _ = r := NONE;
wenzelm@25204
   207
    val _ = ML_wrapper pr verbose ("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@24574
   210
wenzelm@24574
   211
(* basic antiquotations *)
wenzelm@24574
   212
wenzelm@25142
   213
local
wenzelm@25142
   214
wenzelm@25142
   215
fun context x = (Scan.state >> Context.proof_of) x;
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@24574
   228
    "Thm.ctyp_of (ML_Context.the_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@24574
   232
    "Thm.cterm_of (ML_Context.the_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@24574
   236
    "Thm.cterm_of (ML_Context.the_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@24574
   246
fun const syn = (Scan.state >> Context.proof_of) -- 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@26188
   255
  ((Scan.state >> Context.proof_of) -- 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@26188
   258
      let
wenzelm@26188
   259
        val (c, _) = Term.dest_Const (ProofContext.read_const_proper ctxt c);
haftmann@26268
   260
      in ML_Syntax.atomic (ML_Syntax.print_term (ProofContext.mk_const ctxt (c, Ts))) end));
wenzelm@26188
   261
wenzelm@25142
   262
in val _ = () end;
wenzelm@24574
   263
wenzelm@24574
   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 print_named args =
wenzelm@26368
   279
  "Facts.Named " ^
wenzelm@26361
   280
    ML_Syntax.print_pair (ML_Syntax.print_pair ML_Syntax.print_string ML_Syntax.print_position)
wenzelm@26368
   281
      (ML_Syntax.print_option (ML_Syntax.print_list print_interval)) args;
wenzelm@24574
   282
wenzelm@26368
   283
fun thm_antiq kind get get_name = value_antiq kind
wenzelm@26368
   284
  ((Scan.state >> Context.proof_of) --
wenzelm@26368
   285
    Scan.lift (Args.position Args.name -- Scan.option Args.thm_sel)
wenzelm@26368
   286
  >> (fn (ctxt, thmspec as ((name, _), _)) =>
wenzelm@26368
   287
      let
wenzelm@26368
   288
        val _ = get ctxt (Facts.Named thmspec);
wenzelm@26368
   289
        val thmspec_txt = ML_Syntax.atomic (print_named thmspec);
wenzelm@26368
   290
      in (name, get_name ^ " (ML_Context.the_local_context ()) " ^ thmspec_txt) end));
wenzelm@24574
   291
wenzelm@24574
   292
in
wenzelm@24574
   293
wenzelm@24574
   294
val _ = add_keywords ["(", ")", "-", ","];
wenzelm@26368
   295
val _ = thm_antiq "thm" ProofContext.get_fact_single "ProofContext.get_fact_single";
wenzelm@26368
   296
val _ = thm_antiq "thms" ProofContext.get_fact "ProofContext.get_fact";
wenzelm@24574
   297
wenzelm@24574
   298
end;
wenzelm@24574
   299
wenzelm@24685
   300
val _ = proj_value_antiq "cpat" (Scan.lift Args.name >>
wenzelm@24685
   301
    (fn name => ("cpat",
wenzelm@24685
   302
      "Thm.cterm_of (ML_Context.the_context ()) (Syntax.read_term \
wenzelm@24685
   303
      \(ProofContext.set_mode ProofContext.mode_pattern (ML_Context.the_local_context ()))"
wenzelm@24685
   304
      ^ ML_Syntax.print_string name ^ ")", "")));
wenzelm@24574
   305
wenzelm@24574
   306
(*final declarations of this structure!*)
wenzelm@24574
   307
nonfix >>;
wenzelm@24574
   308
fun >> f = change_theory f;
wenzelm@24574
   309
wenzelm@24574
   310
end;
wenzelm@24574
   311
wenzelm@24574
   312
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
wenzelm@24574
   313
open Basic_ML_Context;