src/Pure/ML/ml_context.ML
author wenzelm
Mon, 08 Dec 2014 22:42:12 +0100
changeset 59112 e670969f34df
parent 59067 dd8ec9138112
child 59127 723b11f8ffbf
permissions -rw-r--r--
expand ML cartouches to Input.source; tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24581
6491d89ba76c tuned comments;
wenzelm
parents: 24574
diff changeset
     1
(*  Title:      Pure/ML/ml_context.ML
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
     3
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
     4
ML context and antiquotations.
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
     5
*)
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
     6
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
     7
signature ML_CONTEXT =
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
     8
sig
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
     9
  val the_generic_context: unit -> Context.generic
26432
095e448b95a0 renamed ML_Context.the_context to ML_Context.the_global_context;
wenzelm
parents: 26416
diff changeset
    10
  val the_global_context: unit -> theory
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    11
  val the_local_context: unit -> Proof.context
39164
e7e12555e763 ML_Context.thm and ML_Context.thms no longer pervasive;
wenzelm
parents: 39140
diff changeset
    12
  val thm: xstring -> thm
e7e12555e763 ML_Context.thm and ML_Context.thms no longer pervasive;
wenzelm
parents: 39140
diff changeset
    13
  val thms: xstring -> thm list
26455
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    14
  val exec: (unit -> unit) -> Context.generic -> Context.generic
56070
1bc0bea908c3 ML_Context.check_antiquotation still required;
wenzelm
parents: 56069
diff changeset
    15
  val check_antiquotation: Proof.context -> xstring * Position.T -> string
59112
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    16
  val variant: string -> Proof.context -> string * Proof.context
56205
ceb8a93460b7 clarified modules;
wenzelm
parents: 56203
diff changeset
    17
  type decl = Proof.context -> string * string
59112
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    18
  val value_decl: string -> string -> Proof.context -> decl * Proof.context
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57834
diff changeset
    19
  val add_antiquotation: binding -> (Token.src -> Proof.context -> decl * Proof.context) ->
56205
ceb8a93460b7 clarified modules;
wenzelm
parents: 56203
diff changeset
    20
    theory -> theory
56069
451d5b73f8cf simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents: 56032
diff changeset
    21
  val print_antiquotations: Proof.context -> unit
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
    22
  val eval_antiquotes: ML_Lex.token Antiquote.antiquote list * Position.T ->
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
    23
    Context.generic option -> (ML_Lex.token list * ML_Lex.token list) * Context.generic option
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56229
diff changeset
    24
  val eval: ML_Compiler.flags -> Position.T -> ML_Lex.token Antiquote.antiquote list -> unit
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56229
diff changeset
    25
  val eval_file: ML_Compiler.flags -> Path.T -> unit
59064
a8bcb5a446c8 more abstract type Input.source;
wenzelm
parents: 59058
diff changeset
    26
  val eval_source: ML_Compiler.flags -> Input.source -> unit
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56229
diff changeset
    27
  val eval_in: Proof.context option -> ML_Compiler.flags -> Position.T ->
37198
3af985b10550 replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents: 36959
diff changeset
    28
    ML_Lex.token Antiquote.antiquote list -> unit
59064
a8bcb5a446c8 more abstract type Input.source;
wenzelm
parents: 59058
diff changeset
    29
  val eval_source_in: Proof.context option -> ML_Compiler.flags -> Input.source -> unit
58991
92b6f4e68c5a more careful ML source positions, for improved PIDE markup;
wenzelm
parents: 58979
diff changeset
    30
  val expression: Position.range -> string -> string -> string ->
58978
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 58928
diff changeset
    31
    ML_Lex.token Antiquote.antiquote list -> Context.generic -> Context.generic
25204
36cf92f63a44 replaced Secure.evaluate by ML_Context.evaluate;
wenzelm
parents: 25142
diff changeset
    32
end
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    33
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    34
structure ML_Context: ML_CONTEXT =
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    35
struct
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    36
26416
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    37
(** implicit ML context **)
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    38
26416
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    39
val the_generic_context = Context.the_thread_data;
26432
095e448b95a0 renamed ML_Context.the_context to ML_Context.the_global_context;
wenzelm
parents: 26416
diff changeset
    40
val the_global_context = Context.theory_of o the_generic_context;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    41
val the_local_context = Context.proof_of o the_generic_context;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    42
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 41485
diff changeset
    43
fun thm name = Proof_Context.get_thm (the_local_context ()) name;
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 41485
diff changeset
    44
fun thms name = Proof_Context.get_thms (the_local_context ()) name;
39164
e7e12555e763 ML_Context.thm and ML_Context.thms no longer pervasive;
wenzelm
parents: 39140
diff changeset
    45
26455
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    46
fun exec (e: unit -> unit) context =
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    47
  (case Context.setmp_thread_data (SOME context) (fn () => (e (); Context.thread_data ())) () of
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    48
    SOME context' => context'
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    49
  | NONE => error "Missing context after execution");
26416
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    50
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    51
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    52
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    53
(** ML antiquotations **)
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    54
59112
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    55
(* unique names *)
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    56
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    57
structure Names = Proof_Data
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    58
(
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    59
  type T = Name.context;
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    60
  val init_names = ML_Syntax.reserved |> fold Name.declare ["ML_context", "ML_print_depth"];
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    61
  fun init _ = init_names;
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    62
);
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    63
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    64
fun variant a ctxt =
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    65
  let
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    66
    val names = Names.get ctxt;
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    67
    val (b, names') = Name.variant (Name.desymbolize (SOME false) a) names;
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    68
    val ctxt' = Names.put names' ctxt;
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    69
  in (b, ctxt') end;
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    70
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    71
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    72
(* decl *)
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    73
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    74
type decl = Proof.context -> string * string;  (*final context -> ML env, ML body*)
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    75
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    76
fun value_decl a s ctxt =
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    77
  let
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    78
    val (b, ctxt') = variant a ctxt;
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    79
    val env = "val " ^ b ^ " = " ^ s ^ ";\n";
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    80
    val body = "Isabelle." ^ b;
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    81
    fun decl (_: Proof.context) = (env, body);
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    82
  in (decl, ctxt') end;
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    83
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
    84
56069
451d5b73f8cf simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents: 56032
diff changeset
    85
(* theory data *)
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
    86
56069
451d5b73f8cf simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents: 56032
diff changeset
    87
structure Antiquotations = Theory_Data
43560
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
    88
(
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57834
diff changeset
    89
  type T = (Token.src -> Proof.context -> decl * Proof.context) Name_Space.table;
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 48992
diff changeset
    90
  val empty : T = Name_Space.empty_table Markup.ML_antiquotationN;
43560
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
    91
  val extend = I;
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
    92
  fun merge data : T = Name_Space.merge_tables data;
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
    93
);
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
    94
56069
451d5b73f8cf simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents: 56032
diff changeset
    95
val get_antiquotations = Antiquotations.get o Proof_Context.theory_of;
451d5b73f8cf simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents: 56032
diff changeset
    96
56070
1bc0bea908c3 ML_Context.check_antiquotation still required;
wenzelm
parents: 56069
diff changeset
    97
fun check_antiquotation ctxt =
1bc0bea908c3 ML_Context.check_antiquotation still required;
wenzelm
parents: 56069
diff changeset
    98
  #1 o Name_Space.check (Context.Proof ctxt) (get_antiquotations ctxt);
1bc0bea908c3 ML_Context.check_antiquotation still required;
wenzelm
parents: 56069
diff changeset
    99
56069
451d5b73f8cf simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents: 56032
diff changeset
   100
fun add_antiquotation name f thy = thy
451d5b73f8cf simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents: 56032
diff changeset
   101
  |> Antiquotations.map (Name_Space.define (Context.Theory thy) true (name, f) #> snd);
55743
225a060e7445 proper context for global data;
wenzelm
parents: 55526
diff changeset
   102
56069
451d5b73f8cf simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents: 56032
diff changeset
   103
fun print_antiquotations ctxt =
451d5b73f8cf simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents: 56032
diff changeset
   104
  Pretty.big_list "ML antiquotations:"
451d5b73f8cf simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents: 56032
diff changeset
   105
    (map (Pretty.mark_str o #1) (Name_Space.markup_table ctxt (get_antiquotations ctxt)))
451d5b73f8cf simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents: 56032
diff changeset
   106
  |> Pretty.writeln;
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   107
56069
451d5b73f8cf simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents: 56032
diff changeset
   108
fun apply_antiquotation src ctxt =
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57834
diff changeset
   109
  let val (src', f) = Token.check_src ctxt (get_antiquotations ctxt) src
56069
451d5b73f8cf simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents: 56032
diff changeset
   110
  in f src' ctxt end;
43563
aeabb735883a proper checking of @{ML_antiquotation};
wenzelm
parents: 43560
diff changeset
   111
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   112
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   113
(* parsing and evaluation *)
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   114
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   115
local
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   116
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   117
val antiq =
56201
dd2df97b379b tuned signature;
wenzelm
parents: 56199
diff changeset
   118
  Parse.!!! (Parse.position Parse.xname -- Parse.args --| Scan.ahead Parse.eof)
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57834
diff changeset
   119
  >> uncurry Token.src;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   120
48776
37cd53e69840 faster compilation of ML with antiquotations: static ML_context is bound once in auxiliary structure Isabelle;
wenzelm
parents: 47005
diff changeset
   121
val begin_env0 = ML_Lex.tokenize "structure Isabelle =\nstruct\n";
52663
6e71d43775e5 retain compile-time context visibility, which is particularly important for "apply tactic";
wenzelm
parents: 50201
diff changeset
   122
6e71d43775e5 retain compile-time context visibility, which is particularly important for "apply tactic";
wenzelm
parents: 50201
diff changeset
   123
fun begin_env visible =
48776
37cd53e69840 faster compilation of ML with antiquotations: static ML_context is bound once in auxiliary structure Isabelle;
wenzelm
parents: 47005
diff changeset
   124
  ML_Lex.tokenize
52663
6e71d43775e5 retain compile-time context visibility, which is particularly important for "apply tactic";
wenzelm
parents: 50201
diff changeset
   125
    ("structure Isabelle =\nstruct\n\
6e71d43775e5 retain compile-time context visibility, which is particularly important for "apply tactic";
wenzelm
parents: 50201
diff changeset
   126
     \val ML_context = Context_Position.set_visible " ^ Bool.toString visible ^
57832
5b48f2047c24 clarified compile-time use of ML_print_depth;
wenzelm
parents: 56434
diff changeset
   127
     " (ML_Context.the_local_context ());\n\
57834
0d295e339f52 prefer dynamic ML_print_depth if context happens to be available;
wenzelm
parents: 57832
diff changeset
   128
     \val ML_print_depth =\n\
0d295e339f52 prefer dynamic ML_print_depth if context happens to be available;
wenzelm
parents: 57832
diff changeset
   129
     \  let val default = ML_Options.get_print_depth ()\n\
0d295e339f52 prefer dynamic ML_print_depth if context happens to be available;
wenzelm
parents: 57832
diff changeset
   130
     \  in fn () => ML_Options.get_print_depth_default default end;\n");
48776
37cd53e69840 faster compilation of ML with antiquotations: static ML_context is bound once in auxiliary structure Isabelle;
wenzelm
parents: 47005
diff changeset
   131
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   132
val end_env = ML_Lex.tokenize "end;";
31334
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
   133
val reset_env = ML_Lex.tokenize "structure Isabelle = struct end";
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   134
59112
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   135
fun expanding (Antiquote.Text tok) = ML_Lex.is_cartouche tok
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   136
  | expanding (Antiquote.Antiq _) = true;
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   137
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   138
in
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   139
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   140
fun eval_antiquotes (ants, pos) opt_context =
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   141
  let
52663
6e71d43775e5 retain compile-time context visibility, which is particularly important for "apply tactic";
wenzelm
parents: 50201
diff changeset
   142
    val visible =
6e71d43775e5 retain compile-time context visibility, which is particularly important for "apply tactic";
wenzelm
parents: 50201
diff changeset
   143
      (case opt_context of
6e71d43775e5 retain compile-time context visibility, which is particularly important for "apply tactic";
wenzelm
parents: 50201
diff changeset
   144
        SOME (Context.Proof ctxt) => Context_Position.is_visible ctxt
6e71d43775e5 retain compile-time context visibility, which is particularly important for "apply tactic";
wenzelm
parents: 50201
diff changeset
   145
      | _ => true);
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   146
    val opt_ctxt = Option.map (Context.Proof o Context.proof_of) opt_context;
52663
6e71d43775e5 retain compile-time context visibility, which is particularly important for "apply tactic";
wenzelm
parents: 50201
diff changeset
   147
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   148
    val ((ml_env, ml_body), opt_ctxt') =
59112
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   149
      if forall (not o expanding) ants
48776
37cd53e69840 faster compilation of ML with antiquotations: static ML_context is bound once in auxiliary structure Isabelle;
wenzelm
parents: 47005
diff changeset
   150
      then ((begin_env0, map (fn Antiquote.Text tok => tok) ants), opt_ctxt)
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   151
      else
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   152
        let
59112
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   153
          fun tokenize range = apply2 (ML_Lex.tokenize #> map (ML_Lex.set_range range));
53167
4e7ddd76e632 discontinued unused antiquotation blocks;
wenzelm
parents: 53044
diff changeset
   154
59112
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   155
          fun expand (Antiquote.Antiq (ss, {range, ...})) ctxt =
53167
4e7ddd76e632 discontinued unused antiquotation blocks;
wenzelm
parents: 53044
diff changeset
   156
                let
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58903
diff changeset
   157
                  val keywords = Thy_Header.get_keywords' ctxt;
56069
451d5b73f8cf simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents: 56032
diff changeset
   158
                  val (decl, ctxt') =
58903
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58011
diff changeset
   159
                    apply_antiquotation (Token.read_antiq keywords antiq (ss, #1 range)) ctxt;
59112
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   160
                in (decl #> tokenize range, ctxt') end
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   161
            | expand (Antiquote.Text tok) ctxt =
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   162
                if ML_Lex.is_cartouche tok then
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   163
                  let
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   164
                    val range = ML_Lex.range_of tok;
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   165
                    val text =
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   166
                      Symbol_Pos.explode (ML_Lex.content_of tok, #1 range)
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   167
                      |> Symbol_Pos.cartouche_content
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   168
                      |> Symbol_Pos.implode_range range |> #1;
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   169
                    val (decl, ctxt') =
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   170
                      value_decl "input"
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   171
                        ("Input.source true " ^ ML_Syntax.print_string text  ^ " " ^
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   172
                          ML_Syntax.atomic (ML_Syntax.print_range range)) ctxt;
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   173
                  in (decl #> tokenize range, ctxt') end
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59067
diff changeset
   174
                else (K ([], [tok]), ctxt);
53167
4e7ddd76e632 discontinued unused antiquotation blocks;
wenzelm
parents: 53044
diff changeset
   175
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   176
          val ctxt =
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   177
            (case opt_ctxt of
48992
0518bf89c777 renamed Position.str_of to Position.here;
wenzelm
parents: 48781
diff changeset
   178
              NONE => error ("No context -- cannot expand ML antiquotations" ^ Position.here pos)
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   179
            | SOME ctxt => Context.proof_of ctxt);
27378
wenzelm
parents: 27359
diff changeset
   180
53167
4e7ddd76e632 discontinued unused antiquotation blocks;
wenzelm
parents: 53044
diff changeset
   181
          val (decls, ctxt') = fold_map expand ants ctxt;
52663
6e71d43775e5 retain compile-time context visibility, which is particularly important for "apply tactic";
wenzelm
parents: 50201
diff changeset
   182
          val (ml_env, ml_body) =
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58991
diff changeset
   183
            decls |> map (fn decl => decl ctxt') |> split_list |> apply2 flat;
52663
6e71d43775e5 retain compile-time context visibility, which is particularly important for "apply tactic";
wenzelm
parents: 50201
diff changeset
   184
        in ((begin_env visible @ ml_env, ml_body), SOME (Context.Proof ctxt')) end;
48776
37cd53e69840 faster compilation of ML with antiquotations: static ML_context is bound once in auxiliary structure Isabelle;
wenzelm
parents: 47005
diff changeset
   185
  in ((ml_env @ end_env, ml_body), opt_ctxt') end;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   186
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56229
diff changeset
   187
fun eval flags pos ants =
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   188
  let
56304
40274e4f5ebf redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
wenzelm
parents: 56303
diff changeset
   189
    val non_verbose = ML_Compiler.verbose false flags;
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56229
diff changeset
   190
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   191
    (*prepare source text*)
41485
6c0de045d127 ML_trace: observe context visibility flag (import for Latex mode, for example);
wenzelm
parents: 41375
diff changeset
   192
    val ((env, body), env_ctxt) = eval_antiquotes (ants, pos) (Context.thread_data ());
31334
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
   193
    val _ =
41485
6c0de045d127 ML_trace: observe context visibility flag (import for Latex mode, for example);
wenzelm
parents: 41375
diff changeset
   194
      (case Option.map Context.proof_of env_ctxt of
6c0de045d127 ML_trace: observe context visibility flag (import for Latex mode, for example);
wenzelm
parents: 41375
diff changeset
   195
        SOME ctxt =>
56303
4cc3f4db3447 clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
wenzelm
parents: 56294
diff changeset
   196
          if Config.get ctxt ML_Options.source_trace andalso Context_Position.is_visible ctxt
56294
85911b8a6868 prefer Context_Position where a context is available;
wenzelm
parents: 56279
diff changeset
   197
          then tracing (cat_lines [ML_Lex.flatten env, ML_Lex.flatten body])
41375
7a89b4b94817 configuration option "ML_trace";
wenzelm
parents: 39911
diff changeset
   198
          else ()
7a89b4b94817 configuration option "ML_trace";
wenzelm
parents: 39911
diff changeset
   199
      | NONE => ());
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   200
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   201
    (*prepare static ML environment*)
41485
6c0de045d127 ML_trace: observe context visibility flag (import for Latex mode, for example);
wenzelm
parents: 41375
diff changeset
   202
    val _ =
6c0de045d127 ML_trace: observe context visibility flag (import for Latex mode, for example);
wenzelm
parents: 41375
diff changeset
   203
      Context.setmp_thread_data
6c0de045d127 ML_trace: observe context visibility flag (import for Latex mode, for example);
wenzelm
parents: 41375
diff changeset
   204
        (Option.map (Context.mapping I (Context_Position.set_visible false)) env_ctxt)
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56229
diff changeset
   205
        (fn () => (ML_Compiler.eval non_verbose Position.none env; Context.thread_data ())) ()
31325
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents: 30683
diff changeset
   206
      |> (fn NONE => () | SOME context' => Context.>> (ML_Env.inherit context'));
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   207
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56229
diff changeset
   208
    val _ = ML_Compiler.eval flags pos body;
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56229
diff changeset
   209
    val _ = ML_Compiler.eval non_verbose Position.none reset_env;
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   210
  in () end;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   211
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   212
end;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   213
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   214
30672
beaadd5af500 more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
wenzelm
parents: 30643
diff changeset
   215
(* derived versions *)
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   216
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56229
diff changeset
   217
fun eval_file flags path =
56203
76c72f4d0667 clarified bootstrap process: switch to ML with context and antiquotations earlier;
wenzelm
parents: 56201
diff changeset
   218
  let val pos = Path.position path
59067
dd8ec9138112 tuned signature;
wenzelm
parents: 59064
diff changeset
   219
  in eval flags pos (ML_Lex.read_pos pos (File.read path)) end;
56203
76c72f4d0667 clarified bootstrap process: switch to ML with context and antiquotations earlier;
wenzelm
parents: 56201
diff changeset
   220
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56229
diff changeset
   221
fun eval_source flags source =
59064
a8bcb5a446c8 more abstract type Input.source;
wenzelm
parents: 59058
diff changeset
   222
  eval flags (Input.pos_of source) (ML_Lex.read_source (#SML flags) source);
37198
3af985b10550 replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents: 36959
diff changeset
   223
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56229
diff changeset
   224
fun eval_in ctxt flags pos ants =
56069
451d5b73f8cf simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents: 56032
diff changeset
   225
  Context.setmp_thread_data (Option.map Context.Proof ctxt)
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56229
diff changeset
   226
    (fn () => eval flags pos ants) ();
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   227
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56229
diff changeset
   228
fun eval_source_in ctxt flags source =
56069
451d5b73f8cf simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents: 56032
diff changeset
   229
  Context.setmp_thread_data (Option.map Context.Proof ctxt)
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56229
diff changeset
   230
    (fn () => eval_source flags source) ();
37198
3af985b10550 replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents: 36959
diff changeset
   231
58991
92b6f4e68c5a more careful ML source positions, for improved PIDE markup;
wenzelm
parents: 58979
diff changeset
   232
fun expression range name constraint body ants =
59067
dd8ec9138112 tuned signature;
wenzelm
parents: 59064
diff changeset
   233
  exec (fn () =>
dd8ec9138112 tuned signature;
wenzelm
parents: 59064
diff changeset
   234
    eval ML_Compiler.flags (#1 range)
dd8ec9138112 tuned signature;
wenzelm
parents: 59064
diff changeset
   235
     (ML_Lex.read "Context.set_thread_data (SOME (let val " @ ML_Lex.read_set_range range name @
dd8ec9138112 tuned signature;
wenzelm
parents: 59064
diff changeset
   236
      ML_Lex.read (": " ^ constraint ^ " =") @ ants @
dd8ec9138112 tuned signature;
wenzelm
parents: 59064
diff changeset
   237
      ML_Lex.read ("in " ^ body ^ " end (ML_Context.the_generic_context ())));")));
37198
3af985b10550 replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
wenzelm
parents: 36959
diff changeset
   238
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   239
end;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   240