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