src/Pure/ML/ml_context.ML
author wenzelm
Fri Sep 14 17:02:34 2007 +0200 (2007-09-14)
changeset 24574 e840872e9c7c
child 24581 6491d89ba76c
permissions -rw-r--r--
moved ML_XXX.ML files to Pure/ML;
wenzelm@24574
     1
(*  Title:      Pure/Thy/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@24574
    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@24574
    40
end;
wenzelm@24574
    41
wenzelm@24574
    42
structure ML_Context: ML_CONTEXT =
wenzelm@24574
    43
struct
wenzelm@24574
    44
wenzelm@24574
    45
(** Implicit ML context **)
wenzelm@24574
    46
wenzelm@24574
    47
local
wenzelm@24574
    48
  val current_context = ref (NONE: Context.generic option);
wenzelm@24574
    49
in
wenzelm@24574
    50
  fun get_context () = ! current_context;
wenzelm@24574
    51
  fun set_context opt_context = current_context := opt_context;
wenzelm@24574
    52
  fun setmp opt_context f x = Library.setmp current_context opt_context f x;
wenzelm@24574
    53
end;
wenzelm@24574
    54
wenzelm@24574
    55
fun the_generic_context () =
wenzelm@24574
    56
  (case get_context () of
wenzelm@24574
    57
    SOME context => context
wenzelm@24574
    58
  | _ => error "Unknown context");
wenzelm@24574
    59
wenzelm@24574
    60
val the_local_context = Context.proof_of o the_generic_context;
wenzelm@24574
    61
wenzelm@24574
    62
val the_context = Context.theory_of o the_generic_context;
wenzelm@24574
    63
wenzelm@24574
    64
fun pass opt_context f x =
wenzelm@24574
    65
  setmp opt_context (fn x => let val y = f x in (y, get_context ()) end) x;
wenzelm@24574
    66
wenzelm@24574
    67
fun pass_context context f x =
wenzelm@24574
    68
  (case pass (SOME context) f x of
wenzelm@24574
    69
    (y, SOME context') => (y, context')
wenzelm@24574
    70
  | (_, NONE) => error "Lost context in ML");
wenzelm@24574
    71
wenzelm@24574
    72
fun save f x = CRITICAL (fn () => setmp (get_context ()) f x);
wenzelm@24574
    73
wenzelm@24574
    74
fun change_theory f = CRITICAL (fn () =>
wenzelm@24574
    75
  set_context (SOME (Context.map_theory f (the_generic_context ()))));
wenzelm@24574
    76
wenzelm@24574
    77
wenzelm@24574
    78
wenzelm@24574
    79
(** ML antiquotations **)
wenzelm@24574
    80
wenzelm@24574
    81
(* maintain keywords *)
wenzelm@24574
    82
wenzelm@24574
    83
val global_lexicon = ref Scan.empty_lexicon;
wenzelm@24574
    84
wenzelm@24574
    85
fun add_keywords keywords = CRITICAL (fn () =>
wenzelm@24574
    86
  change global_lexicon (Scan.extend_lexicon (map Symbol.explode keywords)));
wenzelm@24574
    87
wenzelm@24574
    88
wenzelm@24574
    89
(* maintain commands *)
wenzelm@24574
    90
wenzelm@24574
    91
datatype antiq = Inline of string | ProjValue of string * string * string;
wenzelm@24574
    92
val global_parsers = ref (Symtab.empty:
wenzelm@24574
    93
  (Context.generic * Args.T list -> antiq * (Context.generic * Args.T list)) Symtab.table);
wenzelm@24574
    94
wenzelm@24574
    95
local
wenzelm@24574
    96
wenzelm@24574
    97
fun add_item kind name scan = CRITICAL (fn () =>
wenzelm@24574
    98
  change global_parsers (fn tab =>
wenzelm@24574
    99
   (if not (Symtab.defined tab name) then ()
wenzelm@24574
   100
    else warning ("Redefined ML antiquotation: " ^ quote name);
wenzelm@24574
   101
    Symtab.update (name, scan >> kind) tab)));
wenzelm@24574
   102
wenzelm@24574
   103
in
wenzelm@24574
   104
wenzelm@24574
   105
val inline_antiq = add_item Inline;
wenzelm@24574
   106
val proj_value_antiq = add_item ProjValue;
wenzelm@24574
   107
fun value_antiq name scan = proj_value_antiq name (scan >> (fn (a, s) => (a, s, "")));
wenzelm@24574
   108
wenzelm@24574
   109
end;
wenzelm@24574
   110
wenzelm@24574
   111
wenzelm@24574
   112
(* command syntax *)
wenzelm@24574
   113
wenzelm@24574
   114
fun syntax scan src =
wenzelm@24574
   115
  #1 (Args.context_syntax "ML antiquotation" scan src (the_local_context ()));
wenzelm@24574
   116
wenzelm@24574
   117
fun command src =
wenzelm@24574
   118
  let val ((name, _), pos) = Args.dest_src src in
wenzelm@24574
   119
    (case Symtab.lookup (! global_parsers) name of
wenzelm@24574
   120
      NONE => error ("Unknown ML antiquotation command: " ^ quote name ^ Position.str_of pos)
wenzelm@24574
   121
    | SOME scan => syntax scan src)
wenzelm@24574
   122
  end;
wenzelm@24574
   123
wenzelm@24574
   124
wenzelm@24574
   125
(* outer syntax *)
wenzelm@24574
   126
wenzelm@24574
   127
structure T = OuterLex;
wenzelm@24574
   128
structure P = OuterParse;
wenzelm@24574
   129
wenzelm@24574
   130
val antiq =
wenzelm@24574
   131
  P.!!! (P.position P.xname -- P.arguments --| Scan.ahead P.eof)
wenzelm@24574
   132
  >> (fn ((x, pos), y) => Args.src ((x, y), pos));
wenzelm@24574
   133
wenzelm@24574
   134
wenzelm@24574
   135
(* input/output wrappers *)
wenzelm@24574
   136
wenzelm@24574
   137
local
wenzelm@24574
   138
wenzelm@24574
   139
val isabelle_struct = enclose "structure Isabelle =\nstruct\n" "end;";
wenzelm@24574
   140
val isabelle_struct0 = isabelle_struct "";
wenzelm@24574
   141
wenzelm@24574
   142
fun eval_antiquotes txt = CRITICAL (fn () =>
wenzelm@24574
   143
  let
wenzelm@24574
   144
    val ants = Antiquote.scan_antiquotes (txt, Position.line 1);
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@24574
   148
          (case command (Antiquote.scan_arguments (! global_lexicon) 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@24574
   156
                  if f = "" then "Isabelle." ^ b
wenzelm@24574
   157
                  else "(" ^ f ^ " Isabelle." ^ b ^ ")";
wenzelm@24574
   158
              in ((binding, value), names') end);
wenzelm@24574
   159
wenzelm@24574
   160
    val (decls, body) =
wenzelm@24574
   161
      fold_map expand ants ML_Syntax.reserved
wenzelm@24574
   162
      |> #1 |> split_list |> pairself implode;
wenzelm@24574
   163
  in (isabelle_struct decls, body) end);
wenzelm@24574
   164
wenzelm@24574
   165
fun eval name verbose txt = use_text name Output.ml_output verbose txt;
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@24574
   173
fun eval_wrapper name verbose txt =
wenzelm@24574
   174
  let
wenzelm@24574
   175
    val (txt1, txt2) = ! eval_antiquotes_fn txt;
wenzelm@24574
   176
    val _ = if ! trace then tracing (cat_lines [txt1, txt2]) else ();
wenzelm@24574
   177
  in eval "" false txt1; eval name verbose txt2; eval "" false isabelle_struct0 end;
wenzelm@24574
   178
wenzelm@24574
   179
end;
wenzelm@24574
   180
wenzelm@24574
   181
wenzelm@24574
   182
(* ML evaluation *)
wenzelm@24574
   183
wenzelm@24574
   184
fun use_mltext txt verbose opt_context =
wenzelm@24574
   185
  setmp opt_context (fn () => eval_wrapper "ML" verbose txt) ();
wenzelm@24574
   186
wenzelm@24574
   187
fun use_mltext_update txt verbose context =
wenzelm@24574
   188
  #2 (pass_context context (eval_wrapper "ML" verbose) txt);
wenzelm@24574
   189
wenzelm@24574
   190
fun use_context txt = use_mltext_update
wenzelm@24574
   191
  ("ML_Context.set_context (SOME ((" ^ txt ^ ") (ML_Context.the_generic_context ())));") false;
wenzelm@24574
   192
wenzelm@24574
   193
fun use_let bind body txt =
wenzelm@24574
   194
  use_context ("let " ^ bind ^ " = " ^ txt ^ " in\n" ^ body ^ " end");
wenzelm@24574
   195
wenzelm@24574
   196
fun use path = eval_wrapper (Path.implode path) true (File.read path);
wenzelm@24574
   197
wenzelm@24574
   198
wenzelm@24574
   199
(* basic antiquotations *)
wenzelm@24574
   200
wenzelm@24574
   201
val _ = value_antiq "context" (Scan.succeed ("context", "ML_Context.the_local_context ()"));
wenzelm@24574
   202
wenzelm@24574
   203
val _ = inline_antiq "typ" (Args.typ >> (ML_Syntax.atomic o ML_Syntax.print_typ));
wenzelm@24574
   204
val _ = inline_antiq "term" (Args.term >> (ML_Syntax.atomic o ML_Syntax.print_term));
wenzelm@24574
   205
val _ = inline_antiq "prop" (Args.prop >> (ML_Syntax.atomic o ML_Syntax.print_term));
wenzelm@24574
   206
wenzelm@24574
   207
val _ = value_antiq "ctyp" (Args.typ >> (fn T =>
wenzelm@24574
   208
  ("ctyp",
wenzelm@24574
   209
    "Thm.ctyp_of (ML_Context.the_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_typ T))));
wenzelm@24574
   210
wenzelm@24574
   211
val _ = value_antiq "cterm" (Args.term >> (fn t =>
wenzelm@24574
   212
  ("cterm",
wenzelm@24574
   213
    "Thm.cterm_of (ML_Context.the_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_term t))));
wenzelm@24574
   214
wenzelm@24574
   215
val _ = value_antiq "cprop" (Args.prop >> (fn t =>
wenzelm@24574
   216
  ("cprop",
wenzelm@24574
   217
    "Thm.cterm_of (ML_Context.the_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_term t))));
wenzelm@24574
   218
wenzelm@24574
   219
wenzelm@24574
   220
fun const syn = (Scan.state >> Context.proof_of) -- Scan.lift Args.name >> (fn (ctxt, c) =>
wenzelm@24574
   221
  #1 (Term.dest_Const (Consts.read_const (ProofContext.consts_of ctxt) c))
wenzelm@24574
   222
  |> syn ? ProofContext.const_syntax_name ctxt
wenzelm@24574
   223
  |> ML_Syntax.print_string);
wenzelm@24574
   224
wenzelm@24574
   225
val _ = inline_antiq "const_name" (const false);
wenzelm@24574
   226
val _ = inline_antiq "const_syntax" (const true);
wenzelm@24574
   227
wenzelm@24574
   228
wenzelm@24574
   229
wenzelm@24574
   230
(** fact references **)
wenzelm@24574
   231
wenzelm@24574
   232
fun thm name = ProofContext.get_thm (the_local_context ()) (Name name);
wenzelm@24574
   233
fun thms name = ProofContext.get_thms (the_local_context ()) (Name name);
wenzelm@24574
   234
wenzelm@24574
   235
wenzelm@24574
   236
local
wenzelm@24574
   237
wenzelm@24574
   238
fun print_interval (FromTo arg) =
wenzelm@24574
   239
      "PureThy.FromTo " ^ ML_Syntax.print_pair ML_Syntax.print_int ML_Syntax.print_int arg
wenzelm@24574
   240
  | print_interval (From i) = "PureThy.From " ^ ML_Syntax.print_int i
wenzelm@24574
   241
  | print_interval (Single i) = "PureThy.Single " ^ ML_Syntax.print_int i;
wenzelm@24574
   242
wenzelm@24574
   243
fun thm_sel name =
wenzelm@24574
   244
  Args.thm_sel >> (fn is => "PureThy.NameSelection " ^
wenzelm@24574
   245
    ML_Syntax.print_pair ML_Syntax.print_string (ML_Syntax.print_list print_interval) (name, is))
wenzelm@24574
   246
  || Scan.succeed ("PureThy.Name " ^ ML_Syntax.print_string name);
wenzelm@24574
   247
wenzelm@24574
   248
fun thm_antiq kind = value_antiq kind
wenzelm@24574
   249
  (Scan.lift (Args.name :-- thm_sel) >> apsnd (fn sel =>
wenzelm@24574
   250
    "ProofContext.get_" ^ kind ^ " (ML_Context.the_local_context ()) " ^ ML_Syntax.atomic sel));
wenzelm@24574
   251
wenzelm@24574
   252
in
wenzelm@24574
   253
wenzelm@24574
   254
val _ = add_keywords ["(", ")", "-", ","];
wenzelm@24574
   255
val _ = thm_antiq "thm";
wenzelm@24574
   256
val _ = thm_antiq "thms";
wenzelm@24574
   257
wenzelm@24574
   258
end;
wenzelm@24574
   259
wenzelm@24574
   260
val _ = proj_value_antiq "cpat" (Scan.lift Args.name >> 
wenzelm@24574
   261
      (fn ns => ("cpat", 
wenzelm@24574
   262
      "((cterm_of (ML_Context.the_context ())) o Library.the_single o " ^ 
wenzelm@24574
   263
      "(ProofContext.read_term_pats TypeInfer.logicT (ML_Context.the_local_context ())))"
wenzelm@24574
   264
      ^ (ML_Syntax.print_list ML_Syntax.print_string [ns]), "")));
wenzelm@24574
   265
wenzelm@24574
   266
(*final declarations of this structure!*)
wenzelm@24574
   267
nonfix >>;
wenzelm@24574
   268
fun >> f = change_theory f;
wenzelm@24574
   269
wenzelm@24574
   270
end;
wenzelm@24574
   271
wenzelm@24574
   272
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
wenzelm@24574
   273
open Basic_ML_Context;