src/Pure/ML/ml_context.ML
author wenzelm
Wed Apr 06 16:33:33 2016 +0200 (2016-04-06)
changeset 62889 99c7f31615c2
parent 62878 1cec457e0a03
child 62900 c641bf9402fd
permissions -rw-r--r--
clarified modules;
tuned signature;
wenzelm@24581
     1
(*  Title:      Pure/ML/ml_context.ML
wenzelm@24574
     2
    Author:     Makarius
wenzelm@24574
     3
wenzelm@24574
     4
ML context and antiquotations.
wenzelm@24574
     5
*)
wenzelm@24574
     6
wenzelm@24574
     7
signature ML_CONTEXT =
wenzelm@24574
     8
sig
wenzelm@39164
     9
  val thm: xstring -> thm
wenzelm@39164
    10
  val thms: xstring -> thm list
wenzelm@26455
    11
  val exec: (unit -> unit) -> Context.generic -> Context.generic
wenzelm@56070
    12
  val check_antiquotation: Proof.context -> xstring * Position.T -> string
wenzelm@59127
    13
  val struct_name: Proof.context -> string
wenzelm@59112
    14
  val variant: string -> Proof.context -> string * Proof.context
wenzelm@56205
    15
  type decl = Proof.context -> string * string
wenzelm@59112
    16
  val value_decl: string -> string -> Proof.context -> decl * Proof.context
wenzelm@58011
    17
  val add_antiquotation: binding -> (Token.src -> Proof.context -> decl * Proof.context) ->
wenzelm@56205
    18
    theory -> theory
wenzelm@59917
    19
  val print_antiquotations: bool -> Proof.context -> unit
wenzelm@56275
    20
  val eval: ML_Compiler.flags -> Position.T -> ML_Lex.token Antiquote.antiquote list -> unit
wenzelm@56275
    21
  val eval_file: ML_Compiler.flags -> Path.T -> unit
wenzelm@59064
    22
  val eval_source: ML_Compiler.flags -> Input.source -> unit
wenzelm@56275
    23
  val eval_in: Proof.context option -> ML_Compiler.flags -> Position.T ->
wenzelm@37198
    24
    ML_Lex.token Antiquote.antiquote list -> unit
wenzelm@59064
    25
  val eval_source_in: Proof.context option -> ML_Compiler.flags -> Input.source -> unit
wenzelm@58991
    26
  val expression: Position.range -> string -> string -> string ->
wenzelm@58978
    27
    ML_Lex.token Antiquote.antiquote list -> Context.generic -> Context.generic
wenzelm@25204
    28
end
wenzelm@24574
    29
wenzelm@24574
    30
structure ML_Context: ML_CONTEXT =
wenzelm@24574
    31
struct
wenzelm@24574
    32
wenzelm@26416
    33
(** implicit ML context **)
wenzelm@24574
    34
wenzelm@62876
    35
fun thm name = Proof_Context.get_thm (Context.the_local_context ()) name;
wenzelm@62876
    36
fun thms name = Proof_Context.get_thms (Context.the_local_context ()) name;
wenzelm@39164
    37
wenzelm@26455
    38
fun exec (e: unit -> unit) context =
wenzelm@62889
    39
  (case Context.setmp_generic_context (SOME context)
wenzelm@62889
    40
      (fn () => (e (); Context.get_generic_context ())) () of
wenzelm@26455
    41
    SOME context' => context'
wenzelm@26455
    42
  | NONE => error "Missing context after execution");
wenzelm@26416
    43
wenzelm@26416
    44
wenzelm@24574
    45
wenzelm@24574
    46
(** ML antiquotations **)
wenzelm@24574
    47
wenzelm@59127
    48
(* names for generated environment *)
wenzelm@59112
    49
wenzelm@59112
    50
structure Names = Proof_Data
wenzelm@59112
    51
(
wenzelm@59127
    52
  type T = string * Name.context;
wenzelm@59112
    53
  val init_names = ML_Syntax.reserved |> fold Name.declare ["ML_context", "ML_print_depth"];
wenzelm@59127
    54
  fun init _ = ("Isabelle0", init_names);
wenzelm@59112
    55
);
wenzelm@59112
    56
wenzelm@59127
    57
fun struct_name ctxt = #1 (Names.get ctxt);
wenzelm@59127
    58
val struct_begin = (Names.map o apfst) (fn _ => "Isabelle" ^ serial_string ());
wenzelm@59127
    59
wenzelm@59112
    60
fun variant a ctxt =
wenzelm@59112
    61
  let
wenzelm@59127
    62
    val names = #2 (Names.get ctxt);
wenzelm@59112
    63
    val (b, names') = Name.variant (Name.desymbolize (SOME false) a) names;
wenzelm@59127
    64
    val ctxt' = (Names.map o apsnd) (K names') ctxt;
wenzelm@59112
    65
  in (b, ctxt') end;
wenzelm@59112
    66
wenzelm@59112
    67
wenzelm@59112
    68
(* decl *)
wenzelm@59112
    69
wenzelm@59112
    70
type decl = Proof.context -> string * string;  (*final context -> ML env, ML body*)
wenzelm@59112
    71
wenzelm@59112
    72
fun value_decl a s ctxt =
wenzelm@59112
    73
  let
wenzelm@59112
    74
    val (b, ctxt') = variant a ctxt;
wenzelm@59112
    75
    val env = "val " ^ b ^ " = " ^ s ^ ";\n";
wenzelm@59127
    76
    val body = struct_name ctxt ^ "." ^ b;
wenzelm@59112
    77
    fun decl (_: Proof.context) = (env, body);
wenzelm@59112
    78
  in (decl, ctxt') end;
wenzelm@59112
    79
wenzelm@59112
    80
wenzelm@56069
    81
(* theory data *)
wenzelm@27343
    82
wenzelm@56069
    83
structure Antiquotations = Theory_Data
wenzelm@43560
    84
(
wenzelm@58011
    85
  type T = (Token.src -> Proof.context -> decl * Proof.context) Name_Space.table;
wenzelm@50201
    86
  val empty : T = Name_Space.empty_table Markup.ML_antiquotationN;
wenzelm@43560
    87
  val extend = I;
wenzelm@43560
    88
  fun merge data : T = Name_Space.merge_tables data;
wenzelm@43560
    89
);
wenzelm@27343
    90
wenzelm@56069
    91
val get_antiquotations = Antiquotations.get o Proof_Context.theory_of;
wenzelm@56069
    92
wenzelm@56070
    93
fun check_antiquotation ctxt =
wenzelm@56070
    94
  #1 o Name_Space.check (Context.Proof ctxt) (get_antiquotations ctxt);
wenzelm@56070
    95
wenzelm@56069
    96
fun add_antiquotation name f thy = thy
wenzelm@56069
    97
  |> Antiquotations.map (Name_Space.define (Context.Theory thy) true (name, f) #> snd);
wenzelm@55743
    98
wenzelm@59917
    99
fun print_antiquotations verbose ctxt =
wenzelm@56069
   100
  Pretty.big_list "ML antiquotations:"
wenzelm@59917
   101
    (map (Pretty.mark_str o #1) (Name_Space.markup_table verbose ctxt (get_antiquotations ctxt)))
wenzelm@56069
   102
  |> Pretty.writeln;
wenzelm@27343
   103
wenzelm@56069
   104
fun apply_antiquotation src ctxt =
wenzelm@61814
   105
  let val (src', f) = Token.check_src ctxt get_antiquotations src
wenzelm@56069
   106
  in f src' ctxt end;
wenzelm@43563
   107
wenzelm@24574
   108
wenzelm@27343
   109
(* parsing and evaluation *)
wenzelm@24574
   110
wenzelm@27343
   111
local
wenzelm@27343
   112
wenzelm@24574
   113
val antiq =
wenzelm@61814
   114
  Parse.!!! ((Parse.token Parse.xname ::: Parse.args) --| Scan.ahead Parse.eof);
wenzelm@24574
   115
wenzelm@59127
   116
fun make_env name visible =
wenzelm@59127
   117
  (ML_Lex.tokenize
wenzelm@59127
   118
    ("structure " ^ name ^ " =\nstruct\n\
wenzelm@52663
   119
     \val ML_context = Context_Position.set_visible " ^ Bool.toString visible ^
wenzelm@62876
   120
     " (Context.the_local_context ());\n\
wenzelm@57834
   121
     \val ML_print_depth =\n\
wenzelm@62878
   122
     \  let val default = ML_Print_Depth.get_print_depth ()\n\
wenzelm@62878
   123
     \  in fn () => ML_Print_Depth.get_print_depth_default default end;\n"),
wenzelm@59127
   124
   ML_Lex.tokenize "end;");
wenzelm@48776
   125
wenzelm@59127
   126
fun reset_env name = ML_Lex.tokenize ("structure " ^ name ^ " = struct end");
wenzelm@28270
   127
wenzelm@30637
   128
fun eval_antiquotes (ants, pos) opt_context =
wenzelm@24574
   129
  let
wenzelm@52663
   130
    val visible =
wenzelm@52663
   131
      (case opt_context of
wenzelm@52663
   132
        SOME (Context.Proof ctxt) => Context_Position.is_visible ctxt
wenzelm@52663
   133
      | _ => true);
wenzelm@59127
   134
    val opt_ctxt = Option.map Context.proof_of opt_context;
wenzelm@52663
   135
wenzelm@27343
   136
    val ((ml_env, ml_body), opt_ctxt') =
wenzelm@61596
   137
      if forall (fn Antiquote.Text _ => true | _ => false) ants
wenzelm@59127
   138
      then (([], map (fn Antiquote.Text tok => tok) ants), opt_ctxt)
wenzelm@27343
   139
      else
wenzelm@27343
   140
        let
wenzelm@59112
   141
          fun tokenize range = apply2 (ML_Lex.tokenize #> map (ML_Lex.set_range range));
wenzelm@53167
   142
wenzelm@61471
   143
          fun expand_src range src ctxt =
wenzelm@61471
   144
            let val (decl, ctxt') = apply_antiquotation src ctxt
wenzelm@61471
   145
            in (decl #> tokenize range, ctxt') end;
wenzelm@61471
   146
wenzelm@61596
   147
          fun expand (Antiquote.Text tok) ctxt = (K ([], [tok]), ctxt)
wenzelm@61473
   148
            | expand (Antiquote.Control {name, range, body}) ctxt =
wenzelm@61595
   149
                expand_src range
wenzelm@61814
   150
                  (Token.make_src name (if null body then [] else [Token.read_cartouche body])) ctxt
wenzelm@61473
   151
            | expand (Antiquote.Antiq {range, body, ...}) ctxt =
wenzelm@61473
   152
                expand_src range
wenzelm@61473
   153
                  (Token.read_antiq (Thy_Header.get_keywords' ctxt) antiq (body, #1 range)) ctxt;
wenzelm@53167
   154
wenzelm@27343
   155
          val ctxt =
wenzelm@27343
   156
            (case opt_ctxt of
wenzelm@48992
   157
              NONE => error ("No context -- cannot expand ML antiquotations" ^ Position.here pos)
wenzelm@59127
   158
            | SOME ctxt => struct_begin ctxt);
wenzelm@27378
   159
wenzelm@59127
   160
          val (begin_env, end_env) = make_env (struct_name ctxt) visible;
wenzelm@53167
   161
          val (decls, ctxt') = fold_map expand ants ctxt;
wenzelm@52663
   162
          val (ml_env, ml_body) =
wenzelm@59058
   163
            decls |> map (fn decl => decl ctxt') |> split_list |> apply2 flat;
wenzelm@59127
   164
        in ((begin_env @ ml_env @ end_env, ml_body), SOME ctxt') end;
wenzelm@59127
   165
  in ((ml_env, ml_body), opt_ctxt') end;
wenzelm@59127
   166
wenzelm@59127
   167
in
wenzelm@24574
   168
wenzelm@56275
   169
fun eval flags pos ants =
wenzelm@24574
   170
  let
wenzelm@56304
   171
    val non_verbose = ML_Compiler.verbose false flags;
wenzelm@56275
   172
wenzelm@28270
   173
    (*prepare source text*)
wenzelm@62889
   174
    val ((env, body), env_ctxt) = eval_antiquotes (ants, pos) (Context.get_generic_context ());
wenzelm@31334
   175
    val _ =
wenzelm@59127
   176
      (case env_ctxt of
wenzelm@41485
   177
        SOME ctxt =>
wenzelm@56303
   178
          if Config.get ctxt ML_Options.source_trace andalso Context_Position.is_visible ctxt
wenzelm@56294
   179
          then tracing (cat_lines [ML_Lex.flatten env, ML_Lex.flatten body])
wenzelm@41375
   180
          else ()
wenzelm@41375
   181
      | NONE => ());
wenzelm@28270
   182
wenzelm@59127
   183
    (*prepare environment*)
wenzelm@41485
   184
    val _ =
wenzelm@62889
   185
      Context.setmp_generic_context
wenzelm@59127
   186
        (Option.map (Context.Proof o Context_Position.set_visible false) env_ctxt)
wenzelm@62889
   187
        (fn () =>
wenzelm@62889
   188
          (ML_Compiler.eval non_verbose Position.none env; Context.get_generic_context ())) ()
wenzelm@31325
   189
      |> (fn NONE => () | SOME context' => Context.>> (ML_Env.inherit context'));
wenzelm@28270
   190
wenzelm@59127
   191
    (*eval body*)
wenzelm@56275
   192
    val _ = ML_Compiler.eval flags pos body;
wenzelm@59127
   193
wenzelm@59127
   194
    (*clear environment*)
wenzelm@59127
   195
    val _ =
wenzelm@62889
   196
      (case (env_ctxt, is_some (Context.get_generic_context ())) of
wenzelm@59127
   197
        (SOME ctxt, true) =>
wenzelm@59127
   198
          let
wenzelm@59127
   199
            val name = struct_name ctxt;
wenzelm@59127
   200
            val _ = ML_Compiler.eval non_verbose Position.none (reset_env name);
wenzelm@59127
   201
            val _ = Context.>> (ML_Env.forget_structure name);
wenzelm@59127
   202
          in () end
wenzelm@59127
   203
      | _ => ());
wenzelm@28270
   204
  in () end;
wenzelm@24574
   205
wenzelm@24574
   206
end;
wenzelm@24574
   207
wenzelm@24574
   208
wenzelm@30672
   209
(* derived versions *)
wenzelm@24574
   210
wenzelm@56275
   211
fun eval_file flags path =
wenzelm@56203
   212
  let val pos = Path.position path
wenzelm@59067
   213
  in eval flags pos (ML_Lex.read_pos pos (File.read path)) end;
wenzelm@56203
   214
wenzelm@56275
   215
fun eval_source flags source =
wenzelm@62873
   216
  eval flags (Input.pos_of source) (ML_Lex.read_source (#SML_syntax flags) source);
wenzelm@37198
   217
wenzelm@56275
   218
fun eval_in ctxt flags pos ants =
wenzelm@62889
   219
  Context.setmp_generic_context (Option.map Context.Proof ctxt)
wenzelm@56275
   220
    (fn () => eval flags pos ants) ();
wenzelm@24574
   221
wenzelm@56275
   222
fun eval_source_in ctxt flags source =
wenzelm@62889
   223
  Context.setmp_generic_context (Option.map Context.Proof ctxt)
wenzelm@56275
   224
    (fn () => eval_source flags source) ();
wenzelm@37198
   225
wenzelm@58991
   226
fun expression range name constraint body ants =
wenzelm@59067
   227
  exec (fn () =>
wenzelm@59067
   228
    eval ML_Compiler.flags (#1 range)
wenzelm@62889
   229
     (ML_Lex.read "Context.put_generic_context (SOME (let val " @ ML_Lex.read_set_range range name @
wenzelm@59067
   230
      ML_Lex.read (": " ^ constraint ^ " =") @ ants @
wenzelm@62876
   231
      ML_Lex.read ("in " ^ body ^ " end (Context.the_generic_context ())));")));
wenzelm@37198
   232
wenzelm@24574
   233
end;
wenzelm@24574
   234
wenzelm@62850
   235
val ML = ML_Context.eval_source ML_Compiler.flags;