src/Pure/ML/ml_env.ML
author wenzelm
Sat Nov 04 15:24:40 2017 +0100 (20 months ago)
changeset 67003 49850a679c2c
parent 64556 851ae0e7b09c
child 68276 cbee43ff4ceb
permissions -rw-r--r--
more robust sorted_entries;
wenzelm@31325
     1
(*  Title:      Pure/ML/ml_env.ML
wenzelm@31325
     2
    Author:     Makarius
wenzelm@31325
     3
wenzelm@56275
     4
Toplevel environment for Standard ML and Isabelle/ML within the
wenzelm@56275
     5
implicit context.
wenzelm@31325
     6
*)
wenzelm@31325
     7
wenzelm@31325
     8
signature ML_ENV =
wenzelm@31325
     9
sig
wenzelm@31325
    10
  val inherit: Context.generic -> Context.generic -> Context.generic
wenzelm@59127
    11
  val forget_structure: string -> Context.generic -> Context.generic
wenzelm@60746
    12
  val get_breakpoint: Context.generic -> serial -> (bool Unsynchronized.ref * Position.T) option
wenzelm@62941
    13
  val add_breakpoints: (serial * (bool Unsynchronized.ref * Thread_Position.T)) list -> unit
wenzelm@62873
    14
  val init_bootstrap: Context.generic -> Context.generic
wenzelm@62902
    15
  val SML_environment: bool Config.T
wenzelm@60858
    16
  val add_name_space: {SML: bool} -> ML_Name_Space.T -> Context.generic -> Context.generic
wenzelm@62495
    17
  val make_name_space: {SML: bool, exchange: bool} -> ML_Name_Space.T
wenzelm@62495
    18
  val context: ML_Compiler0.context
wenzelm@62495
    19
  val name_space: ML_Name_Space.T
wenzelm@36163
    20
  val check_functor: string -> unit
wenzelm@31325
    21
end
wenzelm@31325
    22
wenzelm@31325
    23
structure ML_Env: ML_ENV =
wenzelm@31325
    24
struct
wenzelm@31325
    25
wenzelm@31328
    26
(* context data *)
wenzelm@31328
    27
wenzelm@56275
    28
type tables =
wenzelm@62934
    29
  PolyML.NameSpace.Values.value Symtab.table *
wenzelm@62934
    30
  PolyML.NameSpace.TypeConstrs.typeConstr Symtab.table *
wenzelm@62934
    31
  PolyML.NameSpace.Infixes.fixity Symtab.table *
wenzelm@62934
    32
  PolyML.NameSpace.Structures.structureVal Symtab.table *
wenzelm@62934
    33
  PolyML.NameSpace.Signatures.signatureVal Symtab.table *
wenzelm@62934
    34
  PolyML.NameSpace.Functors.functorVal Symtab.table;
wenzelm@56275
    35
wenzelm@56275
    36
fun merge_tables
wenzelm@56275
    37
  ((val1, type1, fixity1, structure1, signature1, functor1),
wenzelm@56275
    38
   (val2, type2, fixity2, structure2, signature2, functor2)) : tables =
wenzelm@56275
    39
  (Symtab.merge (K true) (val1, val2),
wenzelm@56275
    40
   Symtab.merge (K true) (type1, type2),
wenzelm@56275
    41
   Symtab.merge (K true) (fixity1, fixity2),
wenzelm@56275
    42
   Symtab.merge (K true) (structure1, structure2),
wenzelm@56275
    43
   Symtab.merge (K true) (signature1, signature2),
wenzelm@56275
    44
   Symtab.merge (K true) (functor1, functor2));
wenzelm@56275
    45
wenzelm@60746
    46
type data =
wenzelm@60746
    47
 {bootstrap: bool,
wenzelm@60746
    48
  tables: tables,
wenzelm@60746
    49
  sml_tables: tables,
wenzelm@60746
    50
  breakpoints: (bool Unsynchronized.ref * Position.T) Inttab.table};
wenzelm@56275
    51
wenzelm@60746
    52
fun make_data (bootstrap, tables, sml_tables, breakpoints) : data =
wenzelm@60746
    53
  {bootstrap = bootstrap, tables = tables, sml_tables = sml_tables, breakpoints = breakpoints};
wenzelm@56275
    54
wenzelm@33519
    55
structure Env = Generic_Data
wenzelm@31325
    56
(
wenzelm@56275
    57
  type T = data
wenzelm@56275
    58
  val empty =
wenzelm@56275
    59
    make_data (true,
wenzelm@56275
    60
      (Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty),
wenzelm@62839
    61
      (Symtab.make ML_Name_Space.sml_val,
wenzelm@62839
    62
       Symtab.make ML_Name_Space.sml_type,
wenzelm@62839
    63
       Symtab.make ML_Name_Space.sml_fixity,
wenzelm@62839
    64
       Symtab.make ML_Name_Space.sml_structure,
wenzelm@62839
    65
       Symtab.make ML_Name_Space.sml_signature,
wenzelm@62839
    66
       Symtab.make ML_Name_Space.sml_functor),
wenzelm@60746
    67
      Inttab.empty);
wenzelm@60746
    68
  fun extend (data : T) = make_data (false, #tables data, #sml_tables data, #breakpoints data);
wenzelm@56275
    69
  fun merge (data : T * T) =
wenzelm@56275
    70
    make_data (false,
wenzelm@59058
    71
      merge_tables (apply2 #tables data),
wenzelm@60746
    72
      merge_tables (apply2 #sml_tables data),
wenzelm@60746
    73
      Inttab.merge (K true) (apply2 #breakpoints data));
wenzelm@31325
    74
);
wenzelm@31325
    75
wenzelm@31325
    76
val inherit = Env.put o Env.get;
wenzelm@31325
    77
wenzelm@59127
    78
fun forget_structure name =
wenzelm@60746
    79
  Env.map (fn {bootstrap, tables, sml_tables, breakpoints} =>
wenzelm@59127
    80
    let
wenzelm@62657
    81
      val _ = if bootstrap then ML_Name_Space.forget_structure name else ();
wenzelm@59127
    82
      val tables' =
wenzelm@59127
    83
        (#1 tables, #2 tables, #3 tables, Symtab.delete_safe name (#4 tables), #5 tables, #6 tables);
wenzelm@60746
    84
    in make_data (bootstrap, tables', sml_tables, breakpoints) end);
wenzelm@60746
    85
wenzelm@62941
    86
val get_breakpoint = Inttab.lookup o #breakpoints o Env.get;
wenzelm@60746
    87
wenzelm@62941
    88
fun add_breakpoints more_breakpoints =
wenzelm@62941
    89
  if is_some (Context.get_generic_context ()) then
wenzelm@62941
    90
    Context.>>
wenzelm@62941
    91
      (Env.map (fn {bootstrap, tables, sml_tables, breakpoints} =>
wenzelm@62941
    92
        let val breakpoints' =
wenzelm@62941
    93
          fold (Inttab.update_new o (apsnd o apsnd) Position.make) more_breakpoints breakpoints;
wenzelm@62941
    94
        in make_data (bootstrap, tables, sml_tables, breakpoints') end))
wenzelm@62941
    95
  else ();
wenzelm@59127
    96
wenzelm@31328
    97
wenzelm@62902
    98
(* SML environment for Isabelle/ML *)
wenzelm@62902
    99
wenzelm@62902
   100
val SML_environment =
wenzelm@64556
   101
  Config.bool (Config.declare ("SML_environment", \<^here>) (fn _ => Config.Bool false));
wenzelm@62902
   102
wenzelm@62902
   103
fun sml_env SML =
wenzelm@62902
   104
  SML orelse
wenzelm@62902
   105
    (case Context.get_generic_context () of
wenzelm@62902
   106
      NONE => false
wenzelm@62902
   107
    | SOME context => Config.get_generic context SML_environment);
wenzelm@62902
   108
wenzelm@62902
   109
wenzelm@56618
   110
(* name space *)
wenzelm@56275
   111
wenzelm@62873
   112
val init_bootstrap =
wenzelm@62873
   113
  Env.map (fn {bootstrap, tables, sml_tables, breakpoints} =>
wenzelm@62873
   114
    let
wenzelm@62873
   115
      val sml_tables' =
wenzelm@62873
   116
        sml_tables |> (fn (val1, type1, fixity1, structure1, signature1, functor1) =>
wenzelm@62873
   117
            let
wenzelm@62873
   118
              val val2 =
wenzelm@62873
   119
                fold (fn (x, y) =>
wenzelm@62875
   120
                  member (op =) ML_Name_Space.bootstrap_values x ? Symtab.update (x, y))
wenzelm@62873
   121
                (#allVal ML_Name_Space.global ()) val1;
wenzelm@62873
   122
              val structure2 =
wenzelm@62873
   123
                fold (fn (x, y) =>
wenzelm@62875
   124
                  member (op =) ML_Name_Space.bootstrap_structures x ? Symtab.update (x, y))
wenzelm@62873
   125
                (#allStruct ML_Name_Space.global ()) structure1;
wenzelm@62910
   126
              val signature2 =
wenzelm@62910
   127
                fold (fn (x, y) =>
wenzelm@62910
   128
                  member (op =) ML_Name_Space.bootstrap_signatures x ? Symtab.update (x, y))
wenzelm@62910
   129
                (#allSig ML_Name_Space.global ()) signature1;
wenzelm@62910
   130
            in (val2, type1, fixity1, structure2, signature2, functor1) end);
wenzelm@62873
   131
    in make_data (bootstrap, tables, sml_tables', breakpoints) end);
wenzelm@62873
   132
wenzelm@60858
   133
fun add_name_space {SML} (space: ML_Name_Space.T) =
wenzelm@60858
   134
  Env.map (fn {bootstrap, tables, sml_tables, breakpoints} =>
wenzelm@60858
   135
    let
wenzelm@60858
   136
      val (tables', sml_tables') =
wenzelm@62902
   137
        (tables, sml_tables) |> (if sml_env SML then apsnd else apfst)
wenzelm@60858
   138
          (fn (val1, type1, fixity1, structure1, signature1, functor1) =>
wenzelm@60858
   139
            let
wenzelm@60858
   140
              val val2 = fold Symtab.update (#allVal space ()) val1;
wenzelm@60858
   141
              val type2 = fold Symtab.update (#allType space ()) type1;
wenzelm@60858
   142
              val fixity2 = fold Symtab.update (#allFix space ()) fixity1;
wenzelm@60858
   143
              val structure2 = fold Symtab.update (#allStruct space ()) structure1;
wenzelm@60858
   144
              val signature2 = fold Symtab.update (#allSig space ()) signature1;
wenzelm@60858
   145
              val functor2 = fold Symtab.update (#allFunct space ()) functor1;
wenzelm@60858
   146
            in (val2, type2, fixity2, structure2, signature2, functor2) end);
wenzelm@60858
   147
    in make_data (bootstrap, tables', sml_tables', breakpoints) end);
wenzelm@60858
   148
wenzelm@62495
   149
fun make_name_space {SML, exchange} : ML_Name_Space.T =
wenzelm@31325
   150
  let
wenzelm@31325
   151
    fun lookup sel1 sel2 name =
wenzelm@62902
   152
      if sml_env SML then
wenzelm@62876
   153
        Context.the_generic_context ()
wenzelm@56618
   154
        |> (fn context => Symtab.lookup (sel1 (#sml_tables (Env.get context))) name)
wenzelm@56618
   155
      else
wenzelm@62889
   156
        Context.get_generic_context ()
wenzelm@56618
   157
        |> (fn NONE => NONE | SOME context => Symtab.lookup (sel1 (#tables (Env.get context))) name)
wenzelm@56618
   158
        |> (fn NONE => sel2 ML_Name_Space.global name | some => some);
wenzelm@31325
   159
wenzelm@31325
   160
    fun all sel1 sel2 () =
wenzelm@62902
   161
      (if sml_env SML then
wenzelm@62876
   162
        Context.the_generic_context ()
wenzelm@56618
   163
        |> (fn context => Symtab.dest (sel1 (#sml_tables (Env.get context))))
wenzelm@56618
   164
      else
wenzelm@62889
   165
        Context.get_generic_context ()
wenzelm@56618
   166
        |> (fn NONE => [] | SOME context => Symtab.dest (sel1 (#tables (Env.get context))))
wenzelm@56618
   167
        |> append (sel2 ML_Name_Space.global ()))
wenzelm@59058
   168
      |> sort_distinct (string_ord o apply2 #1);
wenzelm@31325
   169
wenzelm@31325
   170
    fun enter ap1 sel2 entry =
wenzelm@62902
   171
      if sml_env SML <> exchange then
wenzelm@60746
   172
        Context.>> (Env.map (fn {bootstrap, tables, sml_tables, breakpoints} =>
wenzelm@56618
   173
          let val sml_tables' = ap1 (Symtab.update entry) sml_tables
wenzelm@60746
   174
          in make_data (bootstrap, tables, sml_tables', breakpoints) end))
wenzelm@62889
   175
      else if is_some (Context.get_generic_context ()) then
wenzelm@60746
   176
        Context.>> (Env.map (fn {bootstrap, tables, sml_tables, breakpoints} =>
wenzelm@56203
   177
          let
wenzelm@56275
   178
            val _ = if bootstrap then sel2 ML_Name_Space.global entry else ();
wenzelm@56275
   179
            val tables' = ap1 (Symtab.update entry) tables;
wenzelm@60746
   180
          in make_data (bootstrap, tables', sml_tables, breakpoints) end))
wenzelm@31325
   181
      else sel2 ML_Name_Space.global entry;
wenzelm@31325
   182
  in
wenzelm@31325
   183
   {lookupVal    = lookup #1 #lookupVal,
wenzelm@31325
   184
    lookupType   = lookup #2 #lookupType,
wenzelm@31325
   185
    lookupFix    = lookup #3 #lookupFix,
wenzelm@31325
   186
    lookupStruct = lookup #4 #lookupStruct,
wenzelm@31325
   187
    lookupSig    = lookup #5 #lookupSig,
wenzelm@31325
   188
    lookupFunct  = lookup #6 #lookupFunct,
wenzelm@31325
   189
    enterVal     = enter (fn h => fn (a, b, c, d, e, f) => (h a, b, c, d, e, f)) #enterVal,
wenzelm@31325
   190
    enterType    = enter (fn h => fn (a, b, c, d, e, f) => (a, h b, c, d, e, f)) #enterType,
wenzelm@31325
   191
    enterFix     = enter (fn h => fn (a, b, c, d, e, f) => (a, b, h c, d, e, f)) #enterFix,
wenzelm@31325
   192
    enterStruct  = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, h d, e, f)) #enterStruct,
wenzelm@31325
   193
    enterSig     = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, d, h e, f)) #enterSig,
wenzelm@31325
   194
    enterFunct   = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, d, e, h f)) #enterFunct,
wenzelm@31325
   195
    allVal       = all #1 #allVal,
wenzelm@31325
   196
    allType      = all #2 #allType,
wenzelm@31325
   197
    allFix       = all #3 #allFix,
wenzelm@31325
   198
    allStruct    = all #4 #allStruct,
wenzelm@31325
   199
    allSig       = all #5 #allSig,
wenzelm@31325
   200
    allFunct     = all #6 #allFunct}
wenzelm@31325
   201
  end;
wenzelm@31325
   202
wenzelm@62495
   203
val context: ML_Compiler0.context =
wenzelm@62495
   204
 {name_space = make_name_space {SML = false, exchange = false},
wenzelm@62716
   205
  print_depth = NONE,
wenzelm@62493
   206
  here = Position.here oo Position.line_file,
wenzelm@31325
   207
  print = writeln,
wenzelm@31325
   208
  error = error};
wenzelm@31325
   209
wenzelm@62495
   210
val name_space = #name_space context;
wenzelm@56618
   211
wenzelm@62495
   212
val is_functor = is_some o #lookupFunct name_space;
wenzelm@36163
   213
wenzelm@36163
   214
fun check_functor name =
wenzelm@36165
   215
  if not (is_functor "Table") (*mask dummy version of name_space*) orelse is_functor name then ()
wenzelm@36163
   216
  else error ("Unknown ML functor: " ^ quote name);
wenzelm@36163
   217
wenzelm@31325
   218
end;