src/Pure/ML/ml_env.ML
author wenzelm
Wed, 26 Nov 2014 20:05:34 +0100
changeset 59058 a78612c67ec0
parent 56618 874bdedb2313
child 59127 723b11f8ffbf
permissions -rw-r--r--
renamed "pairself" to "apply2", in accordance to @{apply 2};
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31325
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/ML/ml_env.ML
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
     3
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
     4
Toplevel environment for Standard ML and Isabelle/ML within the
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
     5
implicit context.
31325
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
     6
*)
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
     7
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
     8
signature ML_ENV =
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
     9
sig
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    10
  val inherit: Context.generic -> Context.generic -> Context.generic
56618
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    11
  val name_space: {SML: bool, exchange: bool} -> ML_Name_Space.T
31325
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    12
  val local_context: use_context
56618
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    13
  val local_name_space: ML_Name_Space.T
36163
823c9400eb62 proper checking of ML functors (in Poly/ML 5.2 or later);
wenzelm
parents: 33519
diff changeset
    14
  val check_functor: string -> unit
31325
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    15
end
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    16
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    17
structure ML_Env: ML_ENV =
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    18
struct
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    19
31328
0d3aa0aeb96f maintain tokens within common ML environment;
wenzelm
parents: 31325
diff changeset
    20
(* context data *)
0d3aa0aeb96f maintain tokens within common ML environment;
wenzelm
parents: 31325
diff changeset
    21
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    22
type tables =
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    23
  ML_Name_Space.valueVal Symtab.table *
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    24
  ML_Name_Space.typeVal Symtab.table *
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    25
  ML_Name_Space.fixityVal Symtab.table *
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    26
  ML_Name_Space.structureVal Symtab.table *
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    27
  ML_Name_Space.signatureVal Symtab.table *
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    28
  ML_Name_Space.functorVal Symtab.table;
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    29
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    30
fun merge_tables
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    31
  ((val1, type1, fixity1, structure1, signature1, functor1),
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    32
   (val2, type2, fixity2, structure2, signature2, functor2)) : tables =
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    33
  (Symtab.merge (K true) (val1, val2),
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    34
   Symtab.merge (K true) (type1, type2),
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    35
   Symtab.merge (K true) (fixity1, fixity2),
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    36
   Symtab.merge (K true) (structure1, structure2),
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    37
   Symtab.merge (K true) (signature1, signature2),
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    38
   Symtab.merge (K true) (functor1, functor2));
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    39
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    40
type data = {bootstrap: bool, tables: tables, sml_tables: tables};
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    41
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    42
fun make_data (bootstrap, tables, sml_tables) : data =
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    43
  {bootstrap = bootstrap, tables = tables, sml_tables = sml_tables};
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    44
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 31470
diff changeset
    45
structure Env = Generic_Data
31325
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    46
(
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    47
  type T = data
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    48
  val empty =
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    49
    make_data (true,
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    50
      (Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty),
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    51
      (Symtab.make ML_Name_Space.initial_val,
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    52
       Symtab.make ML_Name_Space.initial_type,
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    53
       Symtab.make ML_Name_Space.initial_fixity,
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    54
       Symtab.make ML_Name_Space.initial_structure,
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    55
       Symtab.make ML_Name_Space.initial_signature,
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    56
       Symtab.make ML_Name_Space.initial_functor));
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    57
  fun extend (data : T) = make_data (false, #tables data, #sml_tables data);
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    58
  fun merge (data : T * T) =
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    59
    make_data (false,
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 56618
diff changeset
    60
      merge_tables (apply2 #tables data),
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 56618
diff changeset
    61
      merge_tables (apply2 #sml_tables data));
31325
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    62
);
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    63
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    64
val inherit = Env.put o Env.get;
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    65
31328
0d3aa0aeb96f maintain tokens within common ML environment;
wenzelm
parents: 31325
diff changeset
    66
56618
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    67
(* name space *)
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    68
56618
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    69
fun name_space {SML, exchange} : ML_Name_Space.T =
31325
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    70
  let
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    71
    fun lookup sel1 sel2 name =
56618
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    72
      if SML then
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    73
        Context.the_thread_data ()
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    74
        |> (fn context => Symtab.lookup (sel1 (#sml_tables (Env.get context))) name)
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    75
      else
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    76
        Context.thread_data ()
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    77
        |> (fn NONE => NONE | SOME context => Symtab.lookup (sel1 (#tables (Env.get context))) name)
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    78
        |> (fn NONE => sel2 ML_Name_Space.global name | some => some);
31325
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    79
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    80
    fun all sel1 sel2 () =
56618
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    81
      (if SML then
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    82
        Context.the_thread_data ()
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    83
        |> (fn context => Symtab.dest (sel1 (#sml_tables (Env.get context))))
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    84
      else
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    85
        Context.thread_data ()
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    86
        |> (fn NONE => [] | SOME context => Symtab.dest (sel1 (#tables (Env.get context))))
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    87
        |> append (sel2 ML_Name_Space.global ()))
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 56618
diff changeset
    88
      |> sort_distinct (string_ord o apply2 #1);
31325
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    89
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
    90
    fun enter ap1 sel2 entry =
56618
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    91
      if SML <> exchange then
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    92
        Context.>> (Env.map (fn {bootstrap, tables, sml_tables} =>
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    93
          let val sml_tables' = ap1 (Symtab.update entry) sml_tables
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    94
          in make_data (bootstrap, tables, sml_tables') end))
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
    95
      else if is_some (Context.thread_data ()) then
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    96
        Context.>> (Env.map (fn {bootstrap, tables, sml_tables} =>
56203
76c72f4d0667 clarified bootstrap process: switch to ML with context and antiquotations earlier;
wenzelm
parents: 48992
diff changeset
    97
          let
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    98
            val _ = if bootstrap then sel2 ML_Name_Space.global entry else ();
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
    99
            val tables' = ap1 (Symtab.update entry) tables;
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56203
diff changeset
   100
          in make_data (bootstrap, tables', sml_tables) end))
31325
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   101
      else sel2 ML_Name_Space.global entry;
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   102
  in
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   103
   {lookupVal    = lookup #1 #lookupVal,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   104
    lookupType   = lookup #2 #lookupType,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   105
    lookupFix    = lookup #3 #lookupFix,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   106
    lookupStruct = lookup #4 #lookupStruct,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   107
    lookupSig    = lookup #5 #lookupSig,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   108
    lookupFunct  = lookup #6 #lookupFunct,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   109
    enterVal     = enter (fn h => fn (a, b, c, d, e, f) => (h a, b, c, d, e, f)) #enterVal,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   110
    enterType    = enter (fn h => fn (a, b, c, d, e, f) => (a, h b, c, d, e, f)) #enterType,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   111
    enterFix     = enter (fn h => fn (a, b, c, d, e, f) => (a, b, h c, d, e, f)) #enterFix,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   112
    enterStruct  = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, h d, e, f)) #enterStruct,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   113
    enterSig     = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, d, h e, f)) #enterSig,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   114
    enterFunct   = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, d, e, h f)) #enterFunct,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   115
    allVal       = all #1 #allVal,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   116
    allType      = all #2 #allType,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   117
    allFix       = all #3 #allFix,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   118
    allStruct    = all #4 #allStruct,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   119
    allSig       = all #5 #allSig,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   120
    allFunct     = all #6 #allFunct}
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   121
  end;
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   122
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   123
val local_context: use_context =
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   124
 {tune_source = ML_Parse.fix_ints,
56618
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
   125
  name_space = name_space {SML = false, exchange = false},
48992
0518bf89c777 renamed Position.str_of to Position.here;
wenzelm
parents: 36165
diff changeset
   126
  str_of_pos = Position.here oo Position.line_file,
31325
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   127
  print = writeln,
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   128
  error = error};
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   129
56618
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
   130
val local_name_space = #name_space local_context;
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
   131
874bdedb2313 added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents: 56275
diff changeset
   132
val is_functor = is_some o #lookupFunct local_name_space;
36163
823c9400eb62 proper checking of ML functors (in Poly/ML 5.2 or later);
wenzelm
parents: 33519
diff changeset
   133
823c9400eb62 proper checking of ML functors (in Poly/ML 5.2 or later);
wenzelm
parents: 33519
diff changeset
   134
fun check_functor name =
36165
310a3f2f0e7e proper masking of dummy name_space;
wenzelm
parents: 36163
diff changeset
   135
  if not (is_functor "Table") (*mask dummy version of name_space*) orelse is_functor name then ()
36163
823c9400eb62 proper checking of ML functors (in Poly/ML 5.2 or later);
wenzelm
parents: 33519
diff changeset
   136
  else error ("Unknown ML functor: " ^ quote name);
823c9400eb62 proper checking of ML functors (in Poly/ML 5.2 or later);
wenzelm
parents: 33519
diff changeset
   137
31325
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   138
end;
700951b53d21 moved local ML environment to separate module ML_Env;
wenzelm
parents:
diff changeset
   139