src/Pure/ML/ml_context.ML
author wenzelm
Sat, 15 May 2010 23:16:32 +0200
changeset 36950 75b8f26f2f07
parent 35021 c839a4c670c6
child 36959 f5417836dbea
permissions -rw-r--r--
refer directly to structure Keyword and Parse; eliminated old-style structure aliases K and P;
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
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    11
  val thm: xstring -> thm
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    12
  val thms: xstring -> thm list
25204
36cf92f63a44 replaced Secure.evaluate by ML_Context.evaluate;
wenzelm
parents: 25142
diff changeset
    13
end
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    14
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    15
signature ML_CONTEXT =
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    16
sig
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    17
  include BASIC_ML_CONTEXT
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    18
  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
    19
  val the_global_context: unit -> theory
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    20
  val the_local_context: unit -> Proof.context
26455
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    21
  val exec: (unit -> unit) -> Context.generic -> Context.generic
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31334
diff changeset
    22
  val stored_thms: thm list Unsynchronized.ref
26416
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    23
  val ml_store_thm: string * thm -> unit
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    24
  val ml_store_thms: string * thm list -> unit
35019
1ec0a3ff229e simplified interface for ML antiquotations, struct_name is always "Isabelle";
wenzelm
parents: 33222
diff changeset
    25
  type antiq = Proof.context -> (Proof.context -> string * string) * Proof.context
30513
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 29606
diff changeset
    26
  val add_antiq: string -> (Position.T -> antiq context_parser) -> unit
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31334
diff changeset
    27
  val trace: bool Unsynchronized.ref
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
    28
  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
    29
    Context.generic option -> (ML_Lex.token list * ML_Lex.token list) * Context.generic option
30574
b9bcc640ed58 more precise type Symbol_Pos.text;
wenzelm
parents: 30573
diff changeset
    30
  val eval: bool -> Position.T -> Symbol_Pos.text -> unit
26455
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    31
  val eval_file: Path.T -> unit
30574
b9bcc640ed58 more precise type Symbol_Pos.text;
wenzelm
parents: 30573
diff changeset
    32
  val eval_in: Proof.context option -> bool -> Position.T -> Symbol_Pos.text -> unit
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31334
diff changeset
    33
  val evaluate: Proof.context -> bool ->
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31334
diff changeset
    34
    string * (unit -> 'a) option Unsynchronized.ref -> string -> 'a
26455
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    35
  val expression: Position.T -> string -> string -> string -> Context.generic -> Context.generic
25204
36cf92f63a44 replaced Secure.evaluate by ML_Context.evaluate;
wenzelm
parents: 25142
diff changeset
    36
end
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    37
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    38
structure ML_Context: ML_CONTEXT =
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    39
struct
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    40
26416
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    41
(** implicit ML context **)
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    42
26416
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    43
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
    44
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
    45
val the_local_context = Context.proof_of o the_generic_context;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    46
26455
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    47
fun exec (e: unit -> unit) context =
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    48
  (case Context.setmp_thread_data (SOME context) (fn () => (e (); Context.thread_data ())) () of
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    49
    SOME context' => context'
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
    50
  | NONE => error "Missing context after execution");
26416
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    51
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    52
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    53
(* theorem bindings *)
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    54
33222
89ced80833ac critical comments;
wenzelm
parents: 32966
diff changeset
    55
val stored_thms: thm list Unsynchronized.ref = Unsynchronized.ref [];  (* FIXME via context!? *)
26416
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    56
26492
6e87cc839632 removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents: 26473
diff changeset
    57
fun ml_store sel (name, ths) =
6e87cc839632 removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents: 26473
diff changeset
    58
  let
29579
cb520b766e00 binding replaces bstring
haftmann
parents: 28412
diff changeset
    59
    val ths' = Context.>>> (Context.map_theory_result
cb520b766e00 binding replaces bstring
haftmann
parents: 28412
diff changeset
    60
      (PureThy.store_thms (Binding.name name, ths)));
26492
6e87cc839632 removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents: 26473
diff changeset
    61
    val _ =
6e87cc839632 removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents: 26473
diff changeset
    62
      if name = "" then ()
6e87cc839632 removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents: 26473
diff changeset
    63
      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
    64
        error ("Cannot bind theorem(s) " ^ quote name ^ " as ML value")
32966
5b21661fe618 indicate CRITICAL nature of various setmp combinators;
wenzelm
parents: 32738
diff changeset
    65
      else setmp_CRITICAL stored_thms ths' (fn () =>
31334
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
    66
        ML_Compiler.eval true Position.none
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
    67
          (ML_Lex.tokenize ("val " ^ name ^ " = " ^ sel ^ "(! ML_Context.stored_thms);"))) ();
26492
6e87cc839632 removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents: 26473
diff changeset
    68
  in () end;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    69
26492
6e87cc839632 removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents: 26473
diff changeset
    70
val ml_store_thms = ml_store "";
6e87cc839632 removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents: 26473
diff changeset
    71
fun ml_store_thm (name, th) = ml_store "hd" (name, [th]);
26416
a66f86ef7bb9 added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents: 26405
diff changeset
    72
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
    73
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
    74
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
    75
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
    76
fun thm name = ProofContext.get_thm (the_local_context ()) name;
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
    77
fun thms name = ProofContext.get_thms (the_local_context ()) name;
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
    78
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    79
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    80
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    81
(** ML antiquotations **)
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    82
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
    83
(* antiquotation commands *)
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
    84
35019
1ec0a3ff229e simplified interface for ML antiquotations, struct_name is always "Isabelle";
wenzelm
parents: 33222
diff changeset
    85
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
    86
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
    87
local
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    88
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31334
diff changeset
    89
val global_parsers =
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31334
diff changeset
    90
  Unsynchronized.ref (Symtab.empty: (Position.T -> antiq context_parser) Symtab.table);
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
    91
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
    92
in
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    93
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
    94
fun add_antiq name scan = CRITICAL (fn () =>
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31334
diff changeset
    95
  Unsynchronized.change global_parsers (fn tab =>
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
    96
   (if not (Symtab.defined tab name) then ()
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
    97
    else warning ("Redefined ML antiquotation: " ^ quote name);
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
    98
    Symtab.update (name, scan) tab)));
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
    99
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   100
fun antiquotation src ctxt =
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   101
  let val ((name, _), pos) = Args.dest_src src in
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   102
    (case Symtab.lookup (! global_parsers) name of
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   103
      NONE => error ("Unknown ML antiquotation command: " ^ quote name ^ Position.str_of pos)
28412
0608c04858c7 back to plain Position.report for regular references;
wenzelm
parents: 28407
diff changeset
   104
    | SOME scan => (Position.report (Markup.ML_antiq name) pos;
27895
e4f8763b971b report antiquotation names;
wenzelm
parents: 27878
diff changeset
   105
        Args.context_syntax "ML antiquotation" (scan pos) src ctxt))
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   106
  end;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   107
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   108
end;
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   109
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   110
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   111
(* parsing and evaluation *)
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
local
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   114
30588
05f81bbb2614 OuterLex.read_antiq;
wenzelm
parents: 30574
diff changeset
   115
structure T = OuterLex;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   116
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   117
val antiq =
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 35021
diff changeset
   118
  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
   119
  >> (fn ((x, pos), y) => Args.src ((x, y), pos));
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   120
35019
1ec0a3ff229e simplified interface for ML antiquotations, struct_name is always "Isabelle";
wenzelm
parents: 33222
diff changeset
   121
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
   122
val end_env = ML_Lex.tokenize "end;";
31334
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
   123
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
   124
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   125
in
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   126
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   127
fun eval_antiquotes (ants, pos) opt_context =
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   128
  let
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   129
    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
   130
    val ((ml_env, ml_body), opt_ctxt') =
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   131
      if forall Antiquote.is_text ants
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   132
      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
   133
      else
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   134
        let
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   135
          val ctxt =
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   136
            (case opt_ctxt of
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   137
              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
   138
            | SOME ctxt => Context.proof_of ctxt);
27378
wenzelm
parents: 27359
diff changeset
   139
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 35021
diff changeset
   140
          val lex = #1 (Keyword.get_lexicons ());
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   141
          fun no_decl _ = ([], []);
27378
wenzelm
parents: 27359
diff changeset
   142
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   143
          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
   144
            | expand (Antiquote.Antiq (ss, range)) (scope, background) =
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   145
                let
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   146
                  val context = Stack.top scope;
30683
e8ac1f9d9469 datatype antiquote: maintain original Position.range, which is eventually attached to the resulting ML tokens;
wenzelm
parents: 30672
diff changeset
   147
                  val (f, context') = antiquotation (T.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
   148
                  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
   149
                  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
   150
                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
   151
            | expand (Antiquote.Open _) (scope, background) =
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   152
                (no_decl, (Stack.push scope, background))
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   153
            | expand (Antiquote.Close _) (scope, background) =
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   154
                (no_decl, (Stack.pop scope, background));
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   155
27343
4b28b80dd1f8 add_antiq: more general notion of ML antiquotation;
wenzelm
parents: 26881
diff changeset
   156
          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
   157
          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
   158
        in (ml, SOME (Context.Proof ctxt')) end;
30637
3e3c2cd88cf1 export eval_antiquotes: refined version that operates on ML tokens;
wenzelm
parents: 30614
diff changeset
   159
  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
   160
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31334
diff changeset
   161
val trace = Unsynchronized.ref false;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   162
30672
beaadd5af500 more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
wenzelm
parents: 30643
diff changeset
   163
fun eval verbose pos txt =
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   164
  let
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   165
    (*prepare source text*)
27878
1ba19c9edd18 report ML_source;
wenzelm
parents: 27874
diff changeset
   166
    val _ = Position.report Markup.ML_source pos;
30643
955830462054 ML_Lex.read_antiq;
wenzelm
parents: 30637
diff changeset
   167
    val ants = ML_Lex.read_antiq (Symbol_Pos.explode (txt, pos), pos);
31334
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
   168
    val ((env, body), env_ctxt) = eval_antiquotes (ants, pos) (Context.thread_data ());
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
   169
    val _ =
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
   170
      if ! trace then tracing (cat_lines [ML_Lex.flatten env, ML_Lex.flatten body])
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
   171
      else ();
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   172
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   173
    (*prepare static ML environment*)
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   174
    val _ = Context.setmp_thread_data env_ctxt
31334
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
   175
        (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
   176
      |> (fn NONE => () | SOME context' => Context.>> (ML_Env.inherit context'));
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   177
31334
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
   178
    val _ = ML_Compiler.eval verbose pos body;
999fa9e1dbdd structure ML_Compiler;
wenzelm
parents: 31325
diff changeset
   179
    val _ = ML_Compiler.eval false Position.none reset_env;
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   180
  in () end;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   181
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   182
end;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   183
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   184
30672
beaadd5af500 more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
wenzelm
parents: 30643
diff changeset
   185
(* derived versions *)
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   186
26881
bb68f50644a9 renamed Position.path to Path.position;
wenzelm
parents: 26658
diff changeset
   187
fun eval_file path = eval true (Path.position path) (File.read path);
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   188
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   189
fun eval_in ctxt verbose pos txt =
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   190
  Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval verbose pos txt) ();
25204
36cf92f63a44 replaced Secure.evaluate by ML_Context.evaluate;
wenzelm
parents: 25142
diff changeset
   191
33222
89ced80833ac critical comments;
wenzelm
parents: 32966
diff changeset
   192
(* FIXME not thread-safe -- reference can be accessed directly *)
30672
beaadd5af500 more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
wenzelm
parents: 30643
diff changeset
   193
fun evaluate ctxt verbose (ref_name, r) txt = NAMED_CRITICAL "ML" (fn () =>
25204
36cf92f63a44 replaced Secure.evaluate by ML_Context.evaluate;
wenzelm
parents: 25142
diff changeset
   194
  let
36cf92f63a44 replaced Secure.evaluate by ML_Context.evaluate;
wenzelm
parents: 25142
diff changeset
   195
    val _ = r := NONE;
28270
7ada24ebea94 explicit handling of ML environment within generic context;
wenzelm
parents: 27895
diff changeset
   196
    val _ = Context.setmp_thread_data (SOME (Context.Proof ctxt)) (fn () =>
30672
beaadd5af500 more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
wenzelm
parents: 30643
diff changeset
   197
      eval verbose Position.none ("val _ = (" ^ ref_name ^ " := SOME (fn () => " ^ txt ^ "))")) ();
25204
36cf92f63a44 replaced Secure.evaluate by ML_Context.evaluate;
wenzelm
parents: 25142
diff changeset
   198
  in (case ! r of NONE => error ("Bad evaluation for " ^ ref_name) | SOME e => e) end) ();
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   199
26455
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
   200
fun expression pos bind body txt =
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
   201
  exec (fn () => eval false pos
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
   202
    ("Context.set_thread_data (SOME (let " ^ bind ^ " = " ^ txt ^ " in " ^ body ^
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
   203
      " end (ML_Context.the_generic_context ())));"));
1757a6e049f4 reorganized signature of ML_Context;
wenzelm
parents: 26432
diff changeset
   204
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   205
end;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   206
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   207
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   208
open Basic_ML_Context;