src/Pure/Thy/ml_context.ML
author wenzelm
Mon Jul 23 16:45:03 2007 +0200 (2007-07-23)
changeset 23935 2a4e42ec9a54
parent 23033 a7e23f993c5e
child 23939 e543359fe8b6
permissions -rw-r--r--
PrintMode.with_modes;
wenzelm@22105
     1
(*  Title:      Pure/Thy/ml_context.ML
wenzelm@22105
     2
    ID:         $Id$
wenzelm@22105
     3
    Author:     Makarius
wenzelm@22105
     4
wenzelm@22105
     5
ML context and antiquotations.
wenzelm@22105
     6
*)
wenzelm@22105
     7
wenzelm@22105
     8
signature BASIC_ML_CONTEXT =
wenzelm@22105
     9
sig
wenzelm@22105
    10
  val the_context: unit -> theory
wenzelm@22105
    11
  val thm: xstring -> thm
wenzelm@22105
    12
  val thms: xstring -> thm list
wenzelm@22105
    13
end;
wenzelm@22105
    14
wenzelm@22105
    15
signature ML_CONTEXT =
wenzelm@22105
    16
sig
wenzelm@22105
    17
  include BASIC_ML_CONTEXT
wenzelm@22105
    18
  val get_context: unit -> Context.generic option
wenzelm@22105
    19
  val set_context: Context.generic option -> unit
wenzelm@22105
    20
  val setmp: Context.generic option -> ('a -> 'b) -> 'a -> 'b
wenzelm@22105
    21
  val the_generic_context: unit -> Context.generic
wenzelm@22105
    22
  val the_local_context: unit -> Proof.context
wenzelm@22105
    23
  val pass: Context.generic option -> ('a -> 'b) -> 'a -> 'b * Context.generic option
wenzelm@22105
    24
  val pass_context: Context.generic -> ('a -> 'b) -> 'a -> 'b * Context.generic
wenzelm@22105
    25
  val save: ('a -> 'b) -> 'a -> 'b
wenzelm@22105
    26
  val >> : (theory -> theory) -> unit
wenzelm@22105
    27
  val add_keywords: string list -> unit
wenzelm@22136
    28
  val inline_antiq: string ->
wenzelm@22136
    29
    (Context.generic * Args.T list -> string * (Context.generic * Args.T list)) -> unit
wenzelm@22136
    30
  val value_antiq: string ->
wenzelm@22136
    31
    (Context.generic * Args.T list -> (string * string) * (Context.generic * Args.T list)) -> unit
wenzelm@22652
    32
  val proj_value_antiq: string -> (Context.generic * Args.T list ->
wenzelm@22652
    33
      (string * string * string) * (Context.generic * Args.T list)) -> unit
wenzelm@22666
    34
  val eval_antiquotes_fn: (string -> string * string) ref  (* FIXME tmp *)
wenzelm@22105
    35
  val use_mltext: string -> bool -> Context.generic option -> unit
wenzelm@22105
    36
  val use_mltext_update: string -> bool -> Context.generic -> Context.generic
wenzelm@22105
    37
  val use_let: string -> string -> string -> Context.generic -> Context.generic
wenzelm@22146
    38
  val use: Path.T -> unit
wenzelm@22105
    39
end;
wenzelm@22105
    40
wenzelm@22105
    41
structure ML_Context: ML_CONTEXT =
wenzelm@22105
    42
struct
wenzelm@22105
    43
wenzelm@22105
    44
(** Implicit ML context **)
wenzelm@22105
    45
wenzelm@22105
    46
local
wenzelm@22105
    47
  val current_context = ref (NONE: Context.generic option);
wenzelm@22105
    48
in
wenzelm@22105
    49
  fun get_context () = ! current_context;
wenzelm@22105
    50
  fun set_context opt_context = current_context := opt_context;
wenzelm@22105
    51
  fun setmp opt_context f x = Library.setmp current_context opt_context f x;
wenzelm@22105
    52
end;
wenzelm@22105
    53
wenzelm@22105
    54
fun the_generic_context () =
wenzelm@22105
    55
  (case get_context () of
wenzelm@22105
    56
    SOME context => context
wenzelm@22105
    57
  | _ => error "Unknown context");
wenzelm@22105
    58
wenzelm@22136
    59
val the_local_context = Context.proof_of o the_generic_context;
wenzelm@22136
    60
wenzelm@22105
    61
val the_context = Context.theory_of o the_generic_context;
wenzelm@22136
    62
wenzelm@22105
    63
fun pass opt_context f x =
wenzelm@22105
    64
  setmp opt_context (fn x => let val y = f x in (y, get_context ()) end) x;
wenzelm@22105
    65
wenzelm@22105
    66
fun pass_context context f x =
wenzelm@22105
    67
  (case pass (SOME context) f x of
wenzelm@22105
    68
    (y, SOME context') => (y, context')
wenzelm@22105
    69
  | (_, NONE) => error "Lost context in ML");
wenzelm@22105
    70
wenzelm@22105
    71
fun save f x = setmp (get_context ()) f x;
wenzelm@22105
    72
wenzelm@22105
    73
fun change_theory f =
wenzelm@22105
    74
  set_context (SOME (Context.map_theory f (the_generic_context ())));
wenzelm@22105
    75
wenzelm@22105
    76
wenzelm@22105
    77
(** ML antiquotations **)
wenzelm@22105
    78
wenzelm@22105
    79
(* maintain keywords *)
wenzelm@22105
    80
wenzelm@22105
    81
val global_lexicon = ref Scan.empty_lexicon;
wenzelm@22105
    82
wenzelm@22105
    83
fun add_keywords keywords =
wenzelm@22105
    84
  change global_lexicon (Scan.extend_lexicon (map Symbol.explode keywords));
wenzelm@22105
    85
wenzelm@22105
    86
wenzelm@22105
    87
(* maintain commands *)
wenzelm@22105
    88
wenzelm@22652
    89
datatype antiq = Inline of string | ProjValue of string * string * string;
wenzelm@22136
    90
val global_parsers = ref (Symtab.empty:
wenzelm@22136
    91
  (Context.generic * Args.T list -> antiq * (Context.generic * Args.T list)) Symtab.table);
wenzelm@22105
    92
wenzelm@22105
    93
local
wenzelm@22105
    94
wenzelm@22105
    95
fun add_item kind name scan = change global_parsers (fn tab =>
wenzelm@22105
    96
 (if not (Symtab.defined tab name) then ()
wenzelm@22105
    97
  else warning ("Redefined ML antiquotation: " ^ quote name);
wenzelm@22105
    98
  Symtab.update (name, scan >> kind) tab));
wenzelm@22105
    99
wenzelm@22105
   100
in
wenzelm@22105
   101
wenzelm@22105
   102
val inline_antiq = add_item Inline;
wenzelm@22652
   103
val proj_value_antiq = add_item ProjValue;
wenzelm@22652
   104
fun value_antiq name scan = proj_value_antiq name (scan >> (fn (a, s) => (a, s, "")));
wenzelm@22105
   105
wenzelm@22105
   106
end;
wenzelm@22105
   107
wenzelm@22105
   108
wenzelm@22105
   109
(* command syntax *)
wenzelm@22105
   110
wenzelm@22105
   111
fun syntax scan src =
wenzelm@22136
   112
  #1 (Args.context_syntax "ML antiquotation" scan src (the_local_context ()));
wenzelm@22105
   113
wenzelm@22105
   114
fun command src =
wenzelm@22105
   115
  let val ((name, _), pos) = Args.dest_src src in
wenzelm@22105
   116
    (case Symtab.lookup (! global_parsers) name of
wenzelm@22105
   117
      NONE => error ("Unknown ML antiquotation command: " ^ quote name ^ Position.str_of pos)
wenzelm@22105
   118
    | SOME scan => transform_failure (curry Antiquote.ANTIQUOTE_FAIL (name, pos))
wenzelm@22105
   119
        (fn () => syntax scan src) ())
wenzelm@22105
   120
  end;
wenzelm@22105
   121
wenzelm@22105
   122
wenzelm@22105
   123
(* outer syntax *)
wenzelm@22105
   124
wenzelm@22105
   125
structure T = OuterLex;
wenzelm@22105
   126
structure P = OuterParse;
wenzelm@22105
   127
wenzelm@22105
   128
val antiq =
wenzelm@22105
   129
  P.!!! (P.position P.xname -- P.arguments --| Scan.ahead P.eof)
wenzelm@22105
   130
  >> (fn ((x, pos), y) => Args.src ((x, y), pos));
wenzelm@22105
   131
wenzelm@22105
   132
wenzelm@22105
   133
(* input/output wrappers *)
wenzelm@22105
   134
wenzelm@22105
   135
local
wenzelm@22105
   136
wenzelm@22105
   137
val isabelle_struct = enclose "structure Isabelle =\nstruct\n" "end;";
wenzelm@22105
   138
val isabelle_struct0 = isabelle_struct "";
wenzelm@22105
   139
wenzelm@22105
   140
fun eval_antiquotes txt =
wenzelm@22105
   141
  let
wenzelm@22105
   142
    val ants = Antiquote.scan_antiquotes (txt, Position.line 1);
wenzelm@22105
   143
wenzelm@22105
   144
    fun expand (Antiquote.Text s) names = (("", Symbol.escape s), names)
wenzelm@22105
   145
      | expand (Antiquote.Antiq x) names =
wenzelm@22105
   146
          (case command (Antiquote.scan_arguments (! global_lexicon) antiq x) of
wenzelm@22621
   147
            Inline s => (("", s), names)
wenzelm@22652
   148
          | ProjValue (a, s, f) =>
wenzelm@22105
   149
              let
wenzelm@22105
   150
                val a' = if ML_Syntax.is_identifier a then a else "val";
wenzelm@22105
   151
                val ([b], names') = Name.variants [a'] names;
wenzelm@22663
   152
                val binding = "val " ^ b ^ " = " ^ s ^ ";\n";
wenzelm@22663
   153
                val value =
wenzelm@22663
   154
                  if f = "" then "Isabelle." ^ b
wenzelm@22663
   155
                  else "(" ^ f ^ " Isabelle." ^ b ^ ")";
wenzelm@22663
   156
              in ((binding, value), names') end);
wenzelm@22105
   157
wenzelm@22105
   158
    val (decls, body) =
wenzelm@22105
   159
      fold_map expand ants ML_Syntax.reserved
wenzelm@22105
   160
      |> #1 |> split_list |> pairself implode;
wenzelm@22105
   161
  in (isabelle_struct decls, body) end;
wenzelm@22105
   162
wenzelm@22146
   163
fun eval name verbose txt =
wenzelm@22146
   164
  Output.ML_errors (use_text name Output.ml_output verbose) txt;
wenzelm@22105
   165
wenzelm@22105
   166
in
wenzelm@22105
   167
wenzelm@22666
   168
val eval_antiquotes_fn = ref eval_antiquotes;
wenzelm@22666
   169
wenzelm@22146
   170
fun eval_wrapper name verbose txt =
wenzelm@22136
   171
  let
wenzelm@22666
   172
    val (txt1, txt2) = ! eval_antiquotes_fn txt;
wenzelm@22332
   173
    val _ =
wenzelm@22332
   174
      if txt1 = isabelle_struct0 andalso txt2 = txt then ()
wenzelm@22332
   175
      else Output.debug (fn () => cat_lines [txt1, txt2]);
wenzelm@22146
   176
  in eval "" false txt1; eval name verbose txt2; eval "" false isabelle_struct0 end;
wenzelm@22105
   177
wenzelm@22105
   178
end;
wenzelm@22105
   179
wenzelm@22105
   180
wenzelm@22136
   181
(* ML evaluation *)
wenzelm@22105
   182
wenzelm@22146
   183
fun use_mltext txt verbose opt_context =
wenzelm@22146
   184
  setmp opt_context (fn () => eval_wrapper "ML" verbose txt) ();
wenzelm@22146
   185
wenzelm@22146
   186
fun use_mltext_update txt verbose context =
wenzelm@22146
   187
  #2 (pass_context context (eval_wrapper "ML" verbose) txt);
wenzelm@22105
   188
wenzelm@22105
   189
fun use_context txt = use_mltext_update
wenzelm@22105
   190
  ("ML_Context.set_context (SOME ((" ^ txt ^ ") (ML_Context.the_generic_context ())));") false;
wenzelm@22105
   191
wenzelm@22105
   192
fun use_let bind body txt =
wenzelm@22105
   193
  use_context ("let " ^ bind ^ " = " ^ txt ^ " in\n" ^ body ^ " end");
wenzelm@22105
   194
wenzelm@22146
   195
fun use path = eval_wrapper (Path.implode path) true (File.read path);
wenzelm@22146
   196
wenzelm@22105
   197
wenzelm@22136
   198
(* basic antiquotations *)
wenzelm@22105
   199
wenzelm@22155
   200
val _ = value_antiq "context" (Scan.succeed ("context", "ML_Context.the_local_context ()"));
wenzelm@22155
   201
wenzelm@22621
   202
val _ = inline_antiq "typ" (Args.typ >> (ML_Syntax.atomic o ML_Syntax.print_typ));
wenzelm@22621
   203
val _ = inline_antiq "term" (Args.term >> (ML_Syntax.atomic o ML_Syntax.print_term));
wenzelm@22621
   204
val _ = inline_antiq "prop" (Args.prop >> (ML_Syntax.atomic o ML_Syntax.print_term));
wenzelm@22105
   205
wenzelm@22136
   206
val _ = value_antiq "ctyp" (Args.typ >> (fn T =>
wenzelm@22155
   207
  ("ctyp",
wenzelm@22155
   208
    "Thm.ctyp_of (ML_Context.the_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_typ T))));
wenzelm@22136
   209
wenzelm@22136
   210
val _ = value_antiq "cterm" (Args.term >> (fn t =>
wenzelm@22155
   211
  ("cterm",
wenzelm@22155
   212
    "Thm.cterm_of (ML_Context.the_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_term t))));
wenzelm@22136
   213
wenzelm@22136
   214
val _ = value_antiq "cprop" (Args.prop >> (fn t =>
wenzelm@22155
   215
  ("cprop",
wenzelm@22155
   216
    "Thm.cterm_of (ML_Context.the_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_term t))));
wenzelm@22155
   217
wenzelm@22155
   218
wenzelm@22376
   219
fun const syn = (Scan.state >> Context.proof_of) -- Scan.lift Args.name >> (fn (ctxt, c) =>
wenzelm@22376
   220
  #1 (Term.dest_Const (Consts.read_const (ProofContext.consts_of ctxt) c))
wenzelm@22376
   221
  |> syn ? ProofContext.const_syntax_name ctxt
wenzelm@22376
   222
  |> ML_Syntax.print_string);
wenzelm@22376
   223
wenzelm@22376
   224
val _ = inline_antiq "const_name" (const false);
wenzelm@22376
   225
val _ = inline_antiq "const_syntax" (const true);
wenzelm@22376
   226
wenzelm@22376
   227
wenzelm@22155
   228
wenzelm@22155
   229
(** fact references **)
wenzelm@22105
   230
wenzelm@22155
   231
fun thm name = ProofContext.get_thm (the_local_context ()) (Name name);
wenzelm@22155
   232
fun thms name = ProofContext.get_thms (the_local_context ()) (Name name);
wenzelm@22155
   233
wenzelm@22155
   234
wenzelm@22155
   235
local
wenzelm@22155
   236
wenzelm@22155
   237
fun print_interval (FromTo arg) =
wenzelm@22155
   238
      "PureThy.FromTo " ^ ML_Syntax.print_pair ML_Syntax.print_int ML_Syntax.print_int arg
wenzelm@22155
   239
  | print_interval (From i) = "PureThy.From " ^ ML_Syntax.print_int i
wenzelm@22155
   240
  | print_interval (Single i) = "PureThy.Single " ^ ML_Syntax.print_int i;
wenzelm@22136
   241
wenzelm@22155
   242
fun thm_sel name =
wenzelm@22155
   243
  Args.thm_sel >> (fn is => "PureThy.NameSelection " ^
wenzelm@22155
   244
    ML_Syntax.print_pair ML_Syntax.print_string (ML_Syntax.print_list print_interval) (name, is))
wenzelm@22155
   245
  || Scan.succeed ("PureThy.Name " ^ ML_Syntax.print_string name);
wenzelm@22155
   246
wenzelm@22155
   247
fun thm_antiq kind = value_antiq kind
wenzelm@22155
   248
  (Scan.lift (Args.name :-- thm_sel) >> apsnd (fn sel =>
wenzelm@22155
   249
    "ProofContext.get_" ^ kind ^ " (ML_Context.the_local_context ()) " ^ ML_Syntax.atomic sel));
wenzelm@22105
   250
wenzelm@22155
   251
in
wenzelm@22155
   252
wenzelm@22155
   253
val _ = add_keywords ["(", ")", "-", ","];
wenzelm@22155
   254
val _ = thm_antiq "thm";
wenzelm@22155
   255
val _ = thm_antiq "thms";
wenzelm@22155
   256
wenzelm@22155
   257
end;
wenzelm@22155
   258
chaieb@23033
   259
val _ = proj_value_antiq "cpat" (Scan.lift Args.name >> 
chaieb@23033
   260
      (fn ns => ("cpat", 
chaieb@23033
   261
      "((cterm_of (ML_Context.the_context ())) o Library.the_single o " ^ 
chaieb@23033
   262
      "(ProofContext.read_term_pats TypeInfer.logicT (ML_Context.the_local_context ())))"
chaieb@23033
   263
      ^ (ML_Syntax.print_list ML_Syntax.print_string [ns]), "")));
wenzelm@22105
   264
wenzelm@22105
   265
(*final declarations of this structure!*)
wenzelm@22105
   266
nonfix >>;
wenzelm@22105
   267
fun >> f = change_theory f;
wenzelm@22105
   268
wenzelm@22105
   269
end;
wenzelm@22105
   270
wenzelm@22105
   271
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
wenzelm@22105
   272
open Basic_ML_Context;