src/Pure/ML/ml_context.ML
author wenzelm
Thu Jul 31 22:02:21 2014 +0200 (2014-07-31)
changeset 57834 0d295e339f52
parent 57832 5b48f2047c24
child 58011 bc6bced136e5
permissions -rw-r--r--
prefer dynamic ML_print_depth if context happens to be available;
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@56205
    16
  type decl = Proof.context -> string * string
wenzelm@56205
    17
  val add_antiquotation: binding -> (Args.src -> Proof.context -> decl * Proof.context) ->
wenzelm@56205
    18
    theory -> theory
wenzelm@56069
    19
  val print_antiquotations: Proof.context -> unit
wenzelm@30637
    20
  val eval_antiquotes: ML_Lex.token Antiquote.antiquote list * Position.T ->
wenzelm@30637
    21
    Context.generic option -> (ML_Lex.token list * ML_Lex.token list) * Context.generic option
wenzelm@56275
    22
  val eval: ML_Compiler.flags -> Position.T -> ML_Lex.token Antiquote.antiquote list -> unit
wenzelm@56275
    23
  val eval_file: ML_Compiler.flags -> Path.T -> unit
wenzelm@56275
    24
  val eval_source: ML_Compiler.flags -> Symbol_Pos.source -> unit
wenzelm@56275
    25
  val eval_in: Proof.context option -> ML_Compiler.flags -> Position.T ->
wenzelm@37198
    26
    ML_Lex.token Antiquote.antiquote list -> unit
wenzelm@56275
    27
  val eval_source_in: Proof.context option -> ML_Compiler.flags -> Symbol_Pos.source -> unit
wenzelm@37198
    28
  val expression: Position.T -> string -> string -> ML_Lex.token Antiquote.antiquote list ->
wenzelm@37198
    29
    Context.generic -> Context.generic
wenzelm@25204
    30
end
wenzelm@24574
    31
wenzelm@24574
    32
structure ML_Context: ML_CONTEXT =
wenzelm@24574
    33
struct
wenzelm@24574
    34
wenzelm@26416
    35
(** implicit ML context **)
wenzelm@24574
    36
wenzelm@26416
    37
val the_generic_context = Context.the_thread_data;
wenzelm@26432
    38
val the_global_context = Context.theory_of o the_generic_context;
wenzelm@24574
    39
val the_local_context = Context.proof_of o the_generic_context;
wenzelm@24574
    40
wenzelm@42360
    41
fun thm name = Proof_Context.get_thm (the_local_context ()) name;
wenzelm@42360
    42
fun thms name = Proof_Context.get_thms (the_local_context ()) name;
wenzelm@39164
    43
wenzelm@26455
    44
fun exec (e: unit -> unit) context =
wenzelm@26455
    45
  (case Context.setmp_thread_data (SOME context) (fn () => (e (); Context.thread_data ())) () of
wenzelm@26455
    46
    SOME context' => context'
wenzelm@26455
    47
  | NONE => error "Missing context after execution");
wenzelm@26416
    48
wenzelm@26416
    49
wenzelm@24574
    50
wenzelm@24574
    51
(** ML antiquotations **)
wenzelm@24574
    52
wenzelm@56069
    53
(* theory data *)
wenzelm@27343
    54
wenzelm@56069
    55
type decl = Proof.context -> string * string;  (*final context -> ML env, ML body*)
wenzelm@56069
    56
structure Antiquotations = Theory_Data
wenzelm@43560
    57
(
wenzelm@56069
    58
  type T = (Args.src -> Proof.context -> decl * Proof.context) Name_Space.table;
wenzelm@50201
    59
  val empty : T = Name_Space.empty_table Markup.ML_antiquotationN;
wenzelm@43560
    60
  val extend = I;
wenzelm@43560
    61
  fun merge data : T = Name_Space.merge_tables data;
wenzelm@43560
    62
);
wenzelm@27343
    63
wenzelm@56069
    64
val get_antiquotations = Antiquotations.get o Proof_Context.theory_of;
wenzelm@56069
    65
wenzelm@56070
    66
fun check_antiquotation ctxt =
wenzelm@56070
    67
  #1 o Name_Space.check (Context.Proof ctxt) (get_antiquotations ctxt);
wenzelm@56070
    68
wenzelm@56069
    69
fun add_antiquotation name f thy = thy
wenzelm@56069
    70
  |> Antiquotations.map (Name_Space.define (Context.Theory thy) true (name, f) #> snd);
wenzelm@55743
    71
wenzelm@56069
    72
fun print_antiquotations ctxt =
wenzelm@56069
    73
  Pretty.big_list "ML antiquotations:"
wenzelm@56069
    74
    (map (Pretty.mark_str o #1) (Name_Space.markup_table ctxt (get_antiquotations ctxt)))
wenzelm@56069
    75
  |> Pretty.writeln;
wenzelm@27343
    76
wenzelm@56069
    77
fun apply_antiquotation src ctxt =
wenzelm@56069
    78
  let val (src', f) = Args.check_src ctxt (get_antiquotations ctxt) src
wenzelm@56069
    79
  in f src' ctxt end;
wenzelm@43563
    80
wenzelm@24574
    81
wenzelm@27343
    82
(* parsing and evaluation *)
wenzelm@24574
    83
wenzelm@27343
    84
local
wenzelm@27343
    85
wenzelm@24574
    86
val antiq =
wenzelm@56201
    87
  Parse.!!! (Parse.position Parse.xname -- Parse.args --| Scan.ahead Parse.eof)
wenzelm@56029
    88
  >> uncurry Args.src;
wenzelm@24574
    89
wenzelm@48776
    90
val begin_env0 = ML_Lex.tokenize "structure Isabelle =\nstruct\n";
wenzelm@52663
    91
wenzelm@52663
    92
fun begin_env visible =
wenzelm@48776
    93
  ML_Lex.tokenize
wenzelm@52663
    94
    ("structure Isabelle =\nstruct\n\
wenzelm@52663
    95
     \val ML_context = Context_Position.set_visible " ^ Bool.toString visible ^
wenzelm@57832
    96
     " (ML_Context.the_local_context ());\n\
wenzelm@57834
    97
     \val ML_print_depth =\n\
wenzelm@57834
    98
     \  let val default = ML_Options.get_print_depth ()\n\
wenzelm@57834
    99
     \  in fn () => ML_Options.get_print_depth_default default end;\n");
wenzelm@48776
   100
wenzelm@30637
   101
val end_env = ML_Lex.tokenize "end;";
wenzelm@31334
   102
val reset_env = ML_Lex.tokenize "structure Isabelle = struct end";
wenzelm@28270
   103
wenzelm@30637
   104
in
wenzelm@30637
   105
wenzelm@30637
   106
fun eval_antiquotes (ants, pos) opt_context =
wenzelm@24574
   107
  let
wenzelm@52663
   108
    val visible =
wenzelm@52663
   109
      (case opt_context of
wenzelm@52663
   110
        SOME (Context.Proof ctxt) => Context_Position.is_visible ctxt
wenzelm@52663
   111
      | _ => true);
wenzelm@28270
   112
    val opt_ctxt = Option.map (Context.Proof o Context.proof_of) opt_context;
wenzelm@52663
   113
wenzelm@27343
   114
    val ((ml_env, ml_body), opt_ctxt') =
wenzelm@30637
   115
      if forall Antiquote.is_text ants
wenzelm@48776
   116
      then ((begin_env0, map (fn Antiquote.Text tok => tok) ants), opt_ctxt)
wenzelm@27343
   117
      else
wenzelm@27343
   118
        let
wenzelm@53167
   119
          val lex = #1 (Keyword.get_lexicons ());
wenzelm@53167
   120
          fun no_decl _ = ([], []);
wenzelm@53167
   121
wenzelm@53167
   122
          fun expand (Antiquote.Text tok) ctxt = (K ([], [tok]), ctxt)
wenzelm@55526
   123
            | expand (Antiquote.Antiq (ss, {range, ...})) ctxt =
wenzelm@53167
   124
                let
wenzelm@56069
   125
                  val (decl, ctxt') =
wenzelm@56069
   126
                    apply_antiquotation (Token.read_antiq lex antiq (ss, #1 range)) ctxt;
wenzelm@53167
   127
                  val decl' = decl #> pairself (ML_Lex.tokenize #> map (ML_Lex.set_range range));
wenzelm@53167
   128
                in (decl', ctxt') end;
wenzelm@53167
   129
wenzelm@27343
   130
          val ctxt =
wenzelm@27343
   131
            (case opt_ctxt of
wenzelm@48992
   132
              NONE => error ("No context -- cannot expand ML antiquotations" ^ Position.here pos)
wenzelm@28270
   133
            | SOME ctxt => Context.proof_of ctxt);
wenzelm@27378
   134
wenzelm@53167
   135
          val (decls, ctxt') = fold_map expand ants ctxt;
wenzelm@52663
   136
          val (ml_env, ml_body) =
wenzelm@52663
   137
            decls |> map (fn decl => decl ctxt') |> split_list |> pairself flat;
wenzelm@52663
   138
        in ((begin_env visible @ ml_env, ml_body), SOME (Context.Proof ctxt')) end;
wenzelm@48776
   139
  in ((ml_env @ end_env, ml_body), opt_ctxt') end;
wenzelm@24574
   140
wenzelm@56275
   141
fun eval flags pos ants =
wenzelm@24574
   142
  let
wenzelm@56304
   143
    val non_verbose = ML_Compiler.verbose false flags;
wenzelm@56275
   144
wenzelm@28270
   145
    (*prepare source text*)
wenzelm@41485
   146
    val ((env, body), env_ctxt) = eval_antiquotes (ants, pos) (Context.thread_data ());
wenzelm@31334
   147
    val _ =
wenzelm@41485
   148
      (case Option.map Context.proof_of env_ctxt of
wenzelm@41485
   149
        SOME ctxt =>
wenzelm@56303
   150
          if Config.get ctxt ML_Options.source_trace andalso Context_Position.is_visible ctxt
wenzelm@56294
   151
          then tracing (cat_lines [ML_Lex.flatten env, ML_Lex.flatten body])
wenzelm@41375
   152
          else ()
wenzelm@41375
   153
      | NONE => ());
wenzelm@28270
   154
wenzelm@28270
   155
    (*prepare static ML environment*)
wenzelm@41485
   156
    val _ =
wenzelm@41485
   157
      Context.setmp_thread_data
wenzelm@41485
   158
        (Option.map (Context.mapping I (Context_Position.set_visible false)) env_ctxt)
wenzelm@56275
   159
        (fn () => (ML_Compiler.eval non_verbose Position.none env; Context.thread_data ())) ()
wenzelm@31325
   160
      |> (fn NONE => () | SOME context' => Context.>> (ML_Env.inherit context'));
wenzelm@28270
   161
wenzelm@56275
   162
    val _ = ML_Compiler.eval flags pos body;
wenzelm@56275
   163
    val _ = ML_Compiler.eval non_verbose Position.none reset_env;
wenzelm@28270
   164
  in () end;
wenzelm@24574
   165
wenzelm@24574
   166
end;
wenzelm@24574
   167
wenzelm@24574
   168
wenzelm@30672
   169
(* derived versions *)
wenzelm@24574
   170
wenzelm@56275
   171
fun eval_file flags path =
wenzelm@56203
   172
  let val pos = Path.position path
wenzelm@56275
   173
  in eval flags pos (ML_Lex.read pos (File.read path)) end;
wenzelm@56203
   174
wenzelm@56275
   175
fun eval_source flags source =
wenzelm@56278
   176
  eval flags (#pos source) (ML_Lex.read_source (#SML flags) source);
wenzelm@37198
   177
wenzelm@56275
   178
fun eval_in ctxt flags pos ants =
wenzelm@56069
   179
  Context.setmp_thread_data (Option.map Context.Proof ctxt)
wenzelm@56275
   180
    (fn () => eval flags pos ants) ();
wenzelm@24574
   181
wenzelm@56275
   182
fun eval_source_in ctxt flags source =
wenzelm@56069
   183
  Context.setmp_thread_data (Option.map Context.Proof ctxt)
wenzelm@56275
   184
    (fn () => eval_source flags source) ();
wenzelm@37198
   185
wenzelm@37198
   186
fun expression pos bind body ants =
wenzelm@56304
   187
  exec (fn () =>
wenzelm@56304
   188
    eval ML_Compiler.flags pos
wenzelm@56304
   189
     (ML_Lex.read Position.none ("Context.set_thread_data (SOME (let " ^ bind ^ " = ") @ ants @
wenzelm@37198
   190
      ML_Lex.read Position.none (" in " ^ body ^ " end (ML_Context.the_generic_context ())));")));
wenzelm@37198
   191
wenzelm@24574
   192
end;
wenzelm@24574
   193