src/Pure/ML/ml_context.ML
author wenzelm
Thu Apr 07 13:54:02 2016 +0200 (2016-04-07)
changeset 62900 c641bf9402fd
parent 62889 99c7f31615c2
child 62902 3c0f53eae166
permissions -rw-r--r--
simplified default print_depth: context is usually available, in contrast to 0d295e339f52;
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@62900
    53
  val init_names = ML_Syntax.reserved |> Name.declare "ML_context";
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@62900
   120
     " (Context.the_local_context ());\n"),
wenzelm@59127
   121
   ML_Lex.tokenize "end;");
wenzelm@48776
   122
wenzelm@59127
   123
fun reset_env name = ML_Lex.tokenize ("structure " ^ name ^ " = struct end");
wenzelm@28270
   124
wenzelm@30637
   125
fun eval_antiquotes (ants, pos) opt_context =
wenzelm@24574
   126
  let
wenzelm@52663
   127
    val visible =
wenzelm@52663
   128
      (case opt_context of
wenzelm@52663
   129
        SOME (Context.Proof ctxt) => Context_Position.is_visible ctxt
wenzelm@52663
   130
      | _ => true);
wenzelm@59127
   131
    val opt_ctxt = Option.map Context.proof_of opt_context;
wenzelm@52663
   132
wenzelm@27343
   133
    val ((ml_env, ml_body), opt_ctxt') =
wenzelm@61596
   134
      if forall (fn Antiquote.Text _ => true | _ => false) ants
wenzelm@59127
   135
      then (([], map (fn Antiquote.Text tok => tok) ants), opt_ctxt)
wenzelm@27343
   136
      else
wenzelm@27343
   137
        let
wenzelm@59112
   138
          fun tokenize range = apply2 (ML_Lex.tokenize #> map (ML_Lex.set_range range));
wenzelm@53167
   139
wenzelm@61471
   140
          fun expand_src range src ctxt =
wenzelm@61471
   141
            let val (decl, ctxt') = apply_antiquotation src ctxt
wenzelm@61471
   142
            in (decl #> tokenize range, ctxt') end;
wenzelm@61471
   143
wenzelm@61596
   144
          fun expand (Antiquote.Text tok) ctxt = (K ([], [tok]), ctxt)
wenzelm@61473
   145
            | expand (Antiquote.Control {name, range, body}) ctxt =
wenzelm@61595
   146
                expand_src range
wenzelm@61814
   147
                  (Token.make_src name (if null body then [] else [Token.read_cartouche body])) ctxt
wenzelm@61473
   148
            | expand (Antiquote.Antiq {range, body, ...}) ctxt =
wenzelm@61473
   149
                expand_src range
wenzelm@61473
   150
                  (Token.read_antiq (Thy_Header.get_keywords' ctxt) antiq (body, #1 range)) ctxt;
wenzelm@53167
   151
wenzelm@27343
   152
          val ctxt =
wenzelm@27343
   153
            (case opt_ctxt of
wenzelm@48992
   154
              NONE => error ("No context -- cannot expand ML antiquotations" ^ Position.here pos)
wenzelm@59127
   155
            | SOME ctxt => struct_begin ctxt);
wenzelm@27378
   156
wenzelm@59127
   157
          val (begin_env, end_env) = make_env (struct_name ctxt) visible;
wenzelm@53167
   158
          val (decls, ctxt') = fold_map expand ants ctxt;
wenzelm@52663
   159
          val (ml_env, ml_body) =
wenzelm@59058
   160
            decls |> map (fn decl => decl ctxt') |> split_list |> apply2 flat;
wenzelm@59127
   161
        in ((begin_env @ ml_env @ end_env, ml_body), SOME ctxt') end;
wenzelm@59127
   162
  in ((ml_env, ml_body), opt_ctxt') end;
wenzelm@59127
   163
wenzelm@59127
   164
in
wenzelm@24574
   165
wenzelm@56275
   166
fun eval flags pos ants =
wenzelm@24574
   167
  let
wenzelm@56304
   168
    val non_verbose = ML_Compiler.verbose false flags;
wenzelm@56275
   169
wenzelm@28270
   170
    (*prepare source text*)
wenzelm@62889
   171
    val ((env, body), env_ctxt) = eval_antiquotes (ants, pos) (Context.get_generic_context ());
wenzelm@31334
   172
    val _ =
wenzelm@59127
   173
      (case env_ctxt of
wenzelm@41485
   174
        SOME ctxt =>
wenzelm@56303
   175
          if Config.get ctxt ML_Options.source_trace andalso Context_Position.is_visible ctxt
wenzelm@56294
   176
          then tracing (cat_lines [ML_Lex.flatten env, ML_Lex.flatten body])
wenzelm@41375
   177
          else ()
wenzelm@41375
   178
      | NONE => ());
wenzelm@28270
   179
wenzelm@59127
   180
    (*prepare environment*)
wenzelm@41485
   181
    val _ =
wenzelm@62889
   182
      Context.setmp_generic_context
wenzelm@59127
   183
        (Option.map (Context.Proof o Context_Position.set_visible false) env_ctxt)
wenzelm@62889
   184
        (fn () =>
wenzelm@62889
   185
          (ML_Compiler.eval non_verbose Position.none env; Context.get_generic_context ())) ()
wenzelm@31325
   186
      |> (fn NONE => () | SOME context' => Context.>> (ML_Env.inherit context'));
wenzelm@28270
   187
wenzelm@59127
   188
    (*eval body*)
wenzelm@56275
   189
    val _ = ML_Compiler.eval flags pos body;
wenzelm@59127
   190
wenzelm@59127
   191
    (*clear environment*)
wenzelm@59127
   192
    val _ =
wenzelm@62889
   193
      (case (env_ctxt, is_some (Context.get_generic_context ())) of
wenzelm@59127
   194
        (SOME ctxt, true) =>
wenzelm@59127
   195
          let
wenzelm@59127
   196
            val name = struct_name ctxt;
wenzelm@59127
   197
            val _ = ML_Compiler.eval non_verbose Position.none (reset_env name);
wenzelm@59127
   198
            val _ = Context.>> (ML_Env.forget_structure name);
wenzelm@59127
   199
          in () end
wenzelm@59127
   200
      | _ => ());
wenzelm@28270
   201
  in () end;
wenzelm@24574
   202
wenzelm@24574
   203
end;
wenzelm@24574
   204
wenzelm@24574
   205
wenzelm@30672
   206
(* derived versions *)
wenzelm@24574
   207
wenzelm@56275
   208
fun eval_file flags path =
wenzelm@56203
   209
  let val pos = Path.position path
wenzelm@59067
   210
  in eval flags pos (ML_Lex.read_pos pos (File.read path)) end;
wenzelm@56203
   211
wenzelm@56275
   212
fun eval_source flags source =
wenzelm@62873
   213
  eval flags (Input.pos_of source) (ML_Lex.read_source (#SML_syntax flags) source);
wenzelm@37198
   214
wenzelm@56275
   215
fun eval_in ctxt flags pos ants =
wenzelm@62889
   216
  Context.setmp_generic_context (Option.map Context.Proof ctxt)
wenzelm@56275
   217
    (fn () => eval flags pos ants) ();
wenzelm@24574
   218
wenzelm@56275
   219
fun eval_source_in ctxt flags source =
wenzelm@62889
   220
  Context.setmp_generic_context (Option.map Context.Proof ctxt)
wenzelm@56275
   221
    (fn () => eval_source flags source) ();
wenzelm@37198
   222
wenzelm@58991
   223
fun expression range name constraint body ants =
wenzelm@59067
   224
  exec (fn () =>
wenzelm@59067
   225
    eval ML_Compiler.flags (#1 range)
wenzelm@62889
   226
     (ML_Lex.read "Context.put_generic_context (SOME (let val " @ ML_Lex.read_set_range range name @
wenzelm@59067
   227
      ML_Lex.read (": " ^ constraint ^ " =") @ ants @
wenzelm@62876
   228
      ML_Lex.read ("in " ^ body ^ " end (Context.the_generic_context ())));")));
wenzelm@37198
   229
wenzelm@24574
   230
end;
wenzelm@24574
   231
wenzelm@62850
   232
val ML = ML_Context.eval_source ML_Compiler.flags;