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