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