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