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