src/Pure/ML/ml_context.ML
author wenzelm
Tue Mar 18 13:36:28 2014 +0100 (2014-03-18)
changeset 56203 76c72f4d0667
parent 56201 dd2df97b379b
child 56205 ceb8a93460b7
permissions -rw-r--r--
clarified bootstrap process: switch to ML with context and antiquotations earlier;
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@56203
    25
  val eval_file: bool -> Path.T -> unit
wenzelm@55828
    26
  val eval_source: bool -> Symbol_Pos.source -> unit
wenzelm@37198
    27
  val eval_in: Proof.context option -> bool -> Position.T ->
wenzelm@37198
    28
    ML_Lex.token Antiquote.antiquote list -> unit
wenzelm@55828
    29
  val eval_source_in: Proof.context option -> bool -> 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@56069
    83
fun antiquotation name scan body =
wenzelm@56069
    84
  add_antiquotation name
wenzelm@56069
    85
    (fn src => fn orig_ctxt =>
wenzelm@56069
    86
      let val (x, _) = Args.syntax scan src orig_ctxt
wenzelm@56069
    87
      in body src x orig_ctxt end);
wenzelm@27343
    88
wenzelm@24574
    89
wenzelm@27343
    90
(* parsing and evaluation *)
wenzelm@24574
    91
wenzelm@27343
    92
local
wenzelm@27343
    93
wenzelm@24574
    94
val antiq =
wenzelm@56201
    95
  Parse.!!! (Parse.position Parse.xname -- Parse.args --| Scan.ahead Parse.eof)
wenzelm@56029
    96
  >> uncurry Args.src;
wenzelm@24574
    97
wenzelm@48776
    98
val begin_env0 = ML_Lex.tokenize "structure Isabelle =\nstruct\n";
wenzelm@52663
    99
wenzelm@52663
   100
fun begin_env visible =
wenzelm@48776
   101
  ML_Lex.tokenize
wenzelm@52663
   102
    ("structure Isabelle =\nstruct\n\
wenzelm@52663
   103
     \val ML_context = Context_Position.set_visible " ^ Bool.toString visible ^
wenzelm@52663
   104
     " (ML_Context.the_local_context ());\n");
wenzelm@48776
   105
wenzelm@30637
   106
val end_env = ML_Lex.tokenize "end;";
wenzelm@31334
   107
val reset_env = ML_Lex.tokenize "structure Isabelle = struct end";
wenzelm@28270
   108
wenzelm@30637
   109
in
wenzelm@30637
   110
wenzelm@30637
   111
fun eval_antiquotes (ants, pos) opt_context =
wenzelm@24574
   112
  let
wenzelm@52663
   113
    val visible =
wenzelm@52663
   114
      (case opt_context of
wenzelm@52663
   115
        SOME (Context.Proof ctxt) => Context_Position.is_visible ctxt
wenzelm@52663
   116
      | _ => true);
wenzelm@28270
   117
    val opt_ctxt = Option.map (Context.Proof o Context.proof_of) opt_context;
wenzelm@52663
   118
wenzelm@27343
   119
    val ((ml_env, ml_body), opt_ctxt') =
wenzelm@30637
   120
      if forall Antiquote.is_text ants
wenzelm@48776
   121
      then ((begin_env0, map (fn Antiquote.Text tok => tok) ants), opt_ctxt)
wenzelm@27343
   122
      else
wenzelm@27343
   123
        let
wenzelm@53167
   124
          val lex = #1 (Keyword.get_lexicons ());
wenzelm@53167
   125
          fun no_decl _ = ([], []);
wenzelm@53167
   126
wenzelm@53167
   127
          fun expand (Antiquote.Text tok) ctxt = (K ([], [tok]), ctxt)
wenzelm@55526
   128
            | expand (Antiquote.Antiq (ss, {range, ...})) ctxt =
wenzelm@53167
   129
                let
wenzelm@56069
   130
                  val (decl, ctxt') =
wenzelm@56069
   131
                    apply_antiquotation (Token.read_antiq lex antiq (ss, #1 range)) ctxt;
wenzelm@53167
   132
                  val decl' = decl #> pairself (ML_Lex.tokenize #> map (ML_Lex.set_range range));
wenzelm@53167
   133
                in (decl', ctxt') end;
wenzelm@53167
   134
wenzelm@27343
   135
          val ctxt =
wenzelm@27343
   136
            (case opt_ctxt of
wenzelm@48992
   137
              NONE => error ("No context -- cannot expand ML antiquotations" ^ Position.here pos)
wenzelm@28270
   138
            | SOME ctxt => Context.proof_of ctxt);
wenzelm@27378
   139
wenzelm@53167
   140
          val (decls, ctxt') = fold_map expand ants ctxt;
wenzelm@52663
   141
          val (ml_env, ml_body) =
wenzelm@52663
   142
            decls |> map (fn decl => decl ctxt') |> split_list |> pairself flat;
wenzelm@52663
   143
        in ((begin_env visible @ ml_env, ml_body), SOME (Context.Proof ctxt')) end;
wenzelm@48776
   144
  in ((ml_env @ end_env, ml_body), opt_ctxt') end;
wenzelm@24574
   145
wenzelm@41375
   146
val trace_raw = Config.declare "ML_trace" (fn _ => Config.Bool false);
wenzelm@41375
   147
val trace = Config.bool trace_raw;
wenzelm@24574
   148
wenzelm@37198
   149
fun eval verbose pos ants =
wenzelm@24574
   150
  let
wenzelm@28270
   151
    (*prepare source text*)
wenzelm@41485
   152
    val ((env, body), env_ctxt) = eval_antiquotes (ants, pos) (Context.thread_data ());
wenzelm@31334
   153
    val _ =
wenzelm@41485
   154
      (case Option.map Context.proof_of env_ctxt of
wenzelm@41485
   155
        SOME ctxt =>
wenzelm@41485
   156
          if Config.get ctxt trace then
wenzelm@41485
   157
            Context_Position.if_visible ctxt
wenzelm@41485
   158
              tracing (cat_lines [ML_Lex.flatten env, ML_Lex.flatten body])
wenzelm@41375
   159
          else ()
wenzelm@41375
   160
      | NONE => ());
wenzelm@28270
   161
wenzelm@28270
   162
    (*prepare static ML environment*)
wenzelm@41485
   163
    val _ =
wenzelm@41485
   164
      Context.setmp_thread_data
wenzelm@41485
   165
        (Option.map (Context.mapping I (Context_Position.set_visible false)) env_ctxt)
wenzelm@31334
   166
        (fn () => (ML_Compiler.eval false Position.none env; Context.thread_data ())) ()
wenzelm@31325
   167
      |> (fn NONE => () | SOME context' => Context.>> (ML_Env.inherit context'));
wenzelm@28270
   168
wenzelm@31334
   169
    val _ = ML_Compiler.eval verbose pos body;
wenzelm@31334
   170
    val _ = ML_Compiler.eval false Position.none reset_env;
wenzelm@28270
   171
  in () end;
wenzelm@24574
   172
wenzelm@24574
   173
end;
wenzelm@24574
   174
wenzelm@24574
   175
wenzelm@30672
   176
(* derived versions *)
wenzelm@24574
   177
wenzelm@56203
   178
fun eval_file verbose path =
wenzelm@56203
   179
  let val pos = Path.position path
wenzelm@56203
   180
  in eval verbose pos (ML_Lex.read pos (File.read path)) end;
wenzelm@56203
   181
wenzelm@55828
   182
fun eval_source verbose source =
wenzelm@55828
   183
  eval verbose (#pos source) (ML_Lex.read_source source);
wenzelm@37198
   184
wenzelm@37198
   185
fun eval_in ctxt verbose pos ants =
wenzelm@56069
   186
  Context.setmp_thread_data (Option.map Context.Proof ctxt)
wenzelm@56069
   187
    (fn () => eval verbose pos ants) ();
wenzelm@24574
   188
wenzelm@55828
   189
fun eval_source_in ctxt verbose source =
wenzelm@56069
   190
  Context.setmp_thread_data (Option.map Context.Proof ctxt)
wenzelm@56069
   191
    (fn () => eval_source verbose source) ();
wenzelm@37198
   192
wenzelm@37198
   193
fun expression pos bind body ants =
wenzelm@37198
   194
  exec (fn () => eval false pos
wenzelm@37198
   195
    (ML_Lex.read Position.none ("Context.set_thread_data (SOME (let " ^ bind ^ " = ") @ ants @
wenzelm@37198
   196
      ML_Lex.read Position.none (" in " ^ body ^ " end (ML_Context.the_generic_context ())));")));
wenzelm@37198
   197
wenzelm@24574
   198
end;
wenzelm@24574
   199