src/Pure/ML/ml_context.ML
author wenzelm
Mon, 27 Jun 2011 17:51:28 +0200
changeset 43563 aeabb735883a
parent 43560 d1650e3720fd
child 43700 e4ece46a9ca7
permissions -rw-r--r--
proper checking of @{ML_antiquotation};
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 BASIC_ML_CONTEXT =
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
     8
sig
26416
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
     9
  val bind_thm: string * thm -> unit
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    10
  val bind_thms: string * thm list -> unit
25204
36cf92f63a44 replaced Secure.evaluate by ML_Context.evaluate;
wenzelm
parents: 25142
diff changeset
    11
end
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    12
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    13
signature ML_CONTEXT =
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    14
sig
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    15
  include BASIC_ML_CONTEXT
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    16
  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
    17
  val the_global_context: unit -> theory
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    18
  val the_local_context: unit -> Proof.context
39164
e7e12555e763 ML_Context.thm and ML_Context.thms no longer pervasive;
wenzelm
parents: 39140
diff changeset
    19
  val thm: xstring -> thm
e7e12555e763 ML_Context.thm and ML_Context.thms no longer pervasive;
wenzelm
parents: 39140
diff changeset
    20
  val thms: xstring -> thm list
26455
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    21
  val exec: (unit -> unit) -> Context.generic -> Context.generic
39140
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    22
  val get_stored_thms: unit -> thm list
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    23
  val get_stored_thm: unit -> thm
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    24
  val ml_store_thms: string * thm list -> unit
26416
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    25
  val ml_store_thm: string * thm -> unit
35019
1ec0a3ff229e simplified interface for ML antiquotations, struct_name is always "Isabelle";
wenzelm
parents: 33222
diff changeset
    26
  type antiq = Proof.context -> (Proof.context -> string * string) * Proof.context
43560
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
    27
  val add_antiq: binding -> (Position.T -> antiq context_parser) -> theory -> theory
43563
aeabb735883a proper checking of @{ML_antiquotation};
wenzelm
parents: 43560
diff changeset
    28
  val intern_antiq: theory -> xstring -> string
aeabb735883a proper checking of @{ML_antiquotation};
wenzelm
parents: 43560
diff changeset
    29
  val defined_antiq: theory -> string -> bool
41375
7a89b4b94817 configuration option "ML_trace";
wenzelm
parents: 39911
diff changeset
    30
  val trace_raw: Config.raw
7a89b4b94817 configuration option "ML_trace";
wenzelm
parents: 39911
diff changeset
    31
  val trace: bool Config.T
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
    32
  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
    33
    Context.generic option -> (ML_Lex.token list * ML_Lex.token list) * Context.generic option
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
    34
  val eval: bool -> Position.T -> ML_Lex.token Antiquote.antiquote list -> unit
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
    35
  val eval_text: bool -> Position.T -> Symbol_Pos.text -> unit
26455
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    36
  val eval_file: Path.T -> unit
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
    37
  val eval_in: Proof.context option -> bool -> Position.T ->
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
    38
    ML_Lex.token Antiquote.antiquote list -> unit
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
    39
  val eval_text_in: Proof.context option -> bool -> Position.T -> Symbol_Pos.text -> unit
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
    40
  val expression: Position.T -> string -> string -> ML_Lex.token Antiquote.antiquote list ->
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
    41
    Context.generic -> Context.generic
25204
36cf92f63a44 replaced Secure.evaluate by ML_Context.evaluate;
wenzelm
parents: 25142
diff changeset
    42
end
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    43
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    44
structure ML_Context: ML_CONTEXT =
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    45
struct
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    46
26416
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    47
(** implicit ML context **)
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    48
26416
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    49
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
    50
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
    51
val the_local_context = Context.proof_of o the_generic_context;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    52
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 41485
diff changeset
    53
fun thm name = Proof_Context.get_thm (the_local_context ()) name;
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 41485
diff changeset
    54
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
    55
26455
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    56
fun exec (e: unit -> unit) context =
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    57
  (case Context.setmp_thread_data (SOME context) (fn () => (e (); Context.thread_data ())) () of
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    58
    SOME context' => context'
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    59
  | NONE => error "Missing context after execution");
26416
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    60
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    61
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    62
(* theorem bindings *)
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    63
39140
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    64
structure Stored_Thms = Theory_Data
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    65
(
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    66
  type T = thm list;
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    67
  val empty = [];
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    68
  fun extend _ = [];
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    69
  fun merge _ = [];
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    70
);
26416
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    71
39140
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    72
fun get_stored_thms () = Stored_Thms.get (the_global_context ());
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    73
val get_stored_thm = hd o get_stored_thms;
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    74
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    75
fun ml_store get (name, ths) =
26492
6e87cc839632 removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents: 26473
diff changeset
    76
  let
29579
cb520b766e00 binding replaces bstring
haftmann
parents: 28412
diff changeset
    77
    val ths' = Context.>>> (Context.map_theory_result
39557
fe5722fce758 renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents: 39507
diff changeset
    78
      (Global_Theory.store_thms (Binding.name name, ths)));
39140
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    79
    val _ = Context.>> (Context.map_theory (Stored_Thms.put ths'));
26492
6e87cc839632 removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents: 26473
diff changeset
    80
    val _ =
6e87cc839632 removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents: 26473
diff changeset
    81
      if name = "" then ()
6e87cc839632 removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents: 26473
diff changeset
    82
      else if not (ML_Syntax.is_identifier name) then
6e87cc839632 removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents: 26473
diff changeset
    83
        error ("Cannot bind theorem(s) " ^ quote name ^ " as ML value")
39140
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    84
      else
31334
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
    85
        ML_Compiler.eval true Position.none
39140
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    86
          (ML_Lex.tokenize ("val " ^ name ^ " = " ^ get ^ " ();"));
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    87
    val _ = Context.>> (Context.map_theory (Stored_Thms.put []));
26492
6e87cc839632 removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents: 26473
diff changeset
    88
  in () end;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    89
39140
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    90
val ml_store_thms = ml_store "ML_Context.get_stored_thms";
8a2fb4ce1f39 ML_Context.ml_store_thm(s): operate on thread context instead of Unsynchronized.ref;
wenzelm
parents: 38931
diff changeset
    91
fun ml_store_thm (name, th) = ml_store "ML_Context.get_stored_thm" (name, [th]);
26416
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    92
35021
c839a4c670c6 renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents: 35019
diff changeset
    93
fun bind_thm (name, thm) = ml_store_thm (name, Drule.export_without_context thm);
c839a4c670c6 renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents: 35019
diff changeset
    94
fun bind_thms (name, thms) = ml_store_thms (name, map Drule.export_without_context thms);
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    95
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    96
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    97
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    98
(** ML antiquotations **)
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    99
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   100
(* antiquotation commands *)
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   101
35019
1ec0a3ff229e simplified interface for ML antiquotations, struct_name is always "Isabelle";
wenzelm
parents: 33222
diff changeset
   102
type antiq = Proof.context -> (Proof.context -> string * string) * Proof.context;
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   103
43560
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
   104
structure Antiq_Parsers = Theory_Data
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
   105
(
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
   106
  type T = (Position.T -> antiq context_parser) Name_Space.table;
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
   107
  val empty : T = Name_Space.empty_table Markup.ML_antiquotationN;
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
   108
  val extend = I;
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
   109
  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
   110
);
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   111
43560
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
   112
fun add_antiq name scan thy = thy
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
   113
  |> Antiq_Parsers.map
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
   114
    (Name_Space.define (Proof_Context.init_global thy) true (Sign.naming_of thy)
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
   115
      (name, scan) #> snd);
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   116
43563
aeabb735883a proper checking of @{ML_antiquotation};
wenzelm
parents: 43560
diff changeset
   117
val intern_antiq = Name_Space.intern o #1 o Antiq_Parsers.get;
aeabb735883a proper checking of @{ML_antiquotation};
wenzelm
parents: 43560
diff changeset
   118
val defined_antiq = Symtab.defined o #2 o Antiq_Parsers.get;
aeabb735883a proper checking of @{ML_antiquotation};
wenzelm
parents: 43560
diff changeset
   119
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   120
fun antiquotation src ctxt =
43560
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
   121
  let
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
   122
    val thy = Proof_Context.theory_of ctxt;
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
   123
    val ((xname, _), pos) = Args.dest_src src;
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
   124
    val (_, scan) = Name_Space.check ctxt (Antiq_Parsers.get thy) (xname, pos);
d1650e3720fd ML antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 42360
diff changeset
   125
  in Args.context_syntax Markup.ML_antiquotationN (scan pos) src ctxt end;
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   126
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   127
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   128
(* parsing and evaluation *)
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   129
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   130
local
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   131
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   132
val antiq =
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 35021
diff changeset
   133
  Parse.!!! (Parse.position Parse.xname -- Args.parse --| Scan.ahead Parse.eof)
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   134
  >> (fn ((x, pos), y) => Args.src ((x, y), pos));
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   135
35019
1ec0a3ff229e simplified interface for ML antiquotations, struct_name is always "Isabelle";
wenzelm
parents: 33222
diff changeset
   136
val begin_env = ML_Lex.tokenize "structure Isabelle =\nstruct\n";
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   137
val end_env = ML_Lex.tokenize "end;";
31334
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
   138
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
   139
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   140
in
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   141
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   142
fun eval_antiquotes (ants, pos) opt_context =
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   143
  let
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   144
    val opt_ctxt = Option.map (Context.Proof o Context.proof_of) opt_context;
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   145
    val ((ml_env, ml_body), opt_ctxt') =
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   146
      if forall Antiquote.is_text ants
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   147
      then (([], map (fn Antiquote.Text tok => tok) ants), opt_ctxt)
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   148
      else
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   149
        let
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   150
          val ctxt =
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   151
            (case opt_ctxt of
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   152
              NONE => error ("No context -- cannot expand ML antiquotations" ^ Position.str_of pos)
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   153
            | SOME ctxt => Context.proof_of ctxt);
27378
wenzelm
parents: 27359
diff changeset
   154
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 35021
diff changeset
   155
          val lex = #1 (Keyword.get_lexicons ());
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   156
          fun no_decl _ = ([], []);
27378
wenzelm
parents: 27359
diff changeset
   157
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   158
          fun expand (Antiquote.Text tok) state = (K ([], [tok]), state)
30683
e8ac1f9d9469 datatype antiquote: maintain original Position.range, which is eventually attached to the resulting ML tokens;
wenzelm
parents: 30672
diff changeset
   159
            | expand (Antiquote.Antiq (ss, range)) (scope, background) =
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   160
                let
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   161
                  val context = Stack.top scope;
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   162
                  val (f, context') =
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   163
                    antiquotation (Token.read_antiq lex antiq (ss, #1 range)) context;
35019
1ec0a3ff229e simplified interface for ML antiquotations, struct_name is always "Isabelle";
wenzelm
parents: 33222
diff changeset
   164
                  val (decl, background') = f background;
30683
e8ac1f9d9469 datatype antiquote: maintain original Position.range, which is eventually attached to the resulting ML tokens;
wenzelm
parents: 30672
diff changeset
   165
                  val decl' = decl #> pairself (ML_Lex.tokenize #> map (ML_Lex.set_range range));
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   166
                in (decl', (Stack.map_top (K context') scope, background')) end
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   167
            | expand (Antiquote.Open _) (scope, background) =
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   168
                (no_decl, (Stack.push scope, background))
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   169
            | expand (Antiquote.Close _) (scope, background) =
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   170
                (no_decl, (Stack.pop scope, background));
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   171
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   172
          val (decls, (_, ctxt')) = fold_map expand ants (Stack.init ctxt, ctxt);
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   173
          val ml = decls |> map (fn decl => decl ctxt') |> split_list |> pairself flat;
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   174
        in (ml, SOME (Context.Proof ctxt')) end;
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   175
  in ((begin_env @ ml_env @ end_env, ml_body), opt_ctxt') end;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   176
41375
7a89b4b94817 configuration option "ML_trace";
wenzelm
parents: 39911
diff changeset
   177
val trace_raw = Config.declare "ML_trace" (fn _ => Config.Bool false);
7a89b4b94817 configuration option "ML_trace";
wenzelm
parents: 39911
diff changeset
   178
val trace = Config.bool trace_raw;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   179
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
   180
fun eval verbose pos ants =
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   181
  let
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   182
    (*prepare source text*)
41485
6c0de045d127 ML_trace: observe context visibility flag (import for Latex mode, for example);
wenzelm
parents: 41375
diff changeset
   183
    val ((env, body), env_ctxt) = eval_antiquotes (ants, pos) (Context.thread_data ());
31334
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
   184
    val _ =
41485
6c0de045d127 ML_trace: observe context visibility flag (import for Latex mode, for example);
wenzelm
parents: 41375
diff changeset
   185
      (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
   186
        SOME ctxt =>
6c0de045d127 ML_trace: observe context visibility flag (import for Latex mode, for example);
wenzelm
parents: 41375
diff changeset
   187
          if Config.get ctxt trace then
6c0de045d127 ML_trace: observe context visibility flag (import for Latex mode, for example);
wenzelm
parents: 41375
diff changeset
   188
            Context_Position.if_visible ctxt
6c0de045d127 ML_trace: observe context visibility flag (import for Latex mode, for example);
wenzelm
parents: 41375
diff changeset
   189
              tracing (cat_lines [ML_Lex.flatten env, ML_Lex.flatten body])
41375
7a89b4b94817 configuration option "ML_trace";
wenzelm
parents: 39911
diff changeset
   190
          else ()
7a89b4b94817 configuration option "ML_trace";
wenzelm
parents: 39911
diff changeset
   191
      | NONE => ());
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   192
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   193
    (*prepare static ML environment*)
41485
6c0de045d127 ML_trace: observe context visibility flag (import for Latex mode, for example);
wenzelm
parents: 41375
diff changeset
   194
    val _ =
6c0de045d127 ML_trace: observe context visibility flag (import for Latex mode, for example);
wenzelm
parents: 41375
diff changeset
   195
      Context.setmp_thread_data
6c0de045d127 ML_trace: observe context visibility flag (import for Latex mode, for example);
wenzelm
parents: 41375
diff changeset
   196
        (Option.map (Context.mapping I (Context_Position.set_visible false)) env_ctxt)
31334
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
   197
        (fn () => (ML_Compiler.eval false Position.none env; Context.thread_data ())) ()
31325
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents: 30683
diff changeset
   198
      |> (fn NONE => () | SOME context' => Context.>> (ML_Env.inherit context'));
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   199
31334
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
   200
    val _ = ML_Compiler.eval verbose pos body;
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
   201
    val _ = ML_Compiler.eval false Position.none reset_env;
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   202
  in () end;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   203
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   204
end;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   205
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   206
30672
beaadd5af500 more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
wenzelm
parents: 30643
diff changeset
   207
(* derived versions *)
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   208
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
   209
fun eval_text verbose pos txt = eval verbose pos (ML_Lex.read pos txt);
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
   210
fun eval_file path = eval_text true (Path.position path) (File.read path);
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
   211
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
   212
fun eval_in ctxt verbose pos ants =
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
   213
  Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval verbose pos ants) ();
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   214
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
   215
fun eval_text_in ctxt verbose pos txt =
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
   216
  Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval_text verbose pos txt) ();
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
   217
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
   218
fun expression pos bind body ants =
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
   219
  exec (fn () => eval false pos
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
   220
    (ML_Lex.read Position.none ("Context.set_thread_data (SOME (let " ^ bind ^ " = ") @ ants @
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
   221
      ML_Lex.read Position.none (" in " ^ body ^ " end (ML_Context.the_generic_context ())));")));
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
   222
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   223
end;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   224
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   225
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   226
open Basic_ML_Context;