src/Pure/ML/ml_env.ML
changeset 68820 2e4df245754e
parent 68818 6debac400787
child 68821 877534be1930
equal deleted inserted replaced
68819:9cfa4aa35719 68820:2e4df245754e
    17   val ML_read_global: bool Config.T
    17   val ML_read_global: bool Config.T
    18   val ML_write_global_raw: Config.raw
    18   val ML_write_global_raw: Config.raw
    19   val ML_write_global: bool Config.T
    19   val ML_write_global: bool Config.T
    20   val inherit: Context.generic -> Context.generic -> Context.generic
    20   val inherit: Context.generic -> Context.generic -> Context.generic
    21   val setup: string -> theory -> theory
    21   val setup: string -> theory -> theory
    22   val context_env: Context.generic -> string option -> string
    22   type environment = {read: string, write: string}
    23   val default_env: string option -> string
    23   val parse_environment: Context.generic option -> string -> environment
       
    24   val print_environment: environment -> string
       
    25   val SML_export: string
       
    26   val SML_import: string
    24   val forget_structure: string -> Context.generic -> Context.generic
    27   val forget_structure: string -> Context.generic -> Context.generic
    25   val bootstrap_name_space: Context.generic -> Context.generic
    28   val bootstrap_name_space: Context.generic -> Context.generic
    26   val add_name_space: string -> ML_Name_Space.T -> Context.generic -> Context.generic
    29   val add_name_space: string -> ML_Name_Space.T -> Context.generic -> Context.generic
    27   val make_name_space: {read: string option, write: string option} -> ML_Name_Space.T
    30   val make_name_space: string -> ML_Name_Space.T
    28   val context: ML_Compiler0.context
    31   val context: ML_Compiler0.context
    29   val name_space: ML_Name_Space.T
    32   val name_space: ML_Name_Space.T
    30   val check_functor: string -> unit
    33   val check_functor: string -> unit
    31   val get_breakpoint: Context.generic -> serial -> (bool Unsynchronized.ref * Position.T) option
    34   val get_breakpoint: Context.generic -> serial -> (bool Unsynchronized.ref * Position.T) option
    32   val add_breakpoints: (serial * (bool Unsynchronized.ref * Thread_Position.T)) list -> unit
    35   val add_breakpoints: (serial * (bool Unsynchronized.ref * Thread_Position.T)) list -> unit
   124       (#1 tables, #2 tables, #3 tables, Symtab.delete_safe name (#4 tables), #5 tables, #6 tables)));
   127       (#1 tables, #2 tables, #3 tables, Symtab.delete_safe name (#4 tables), #5 tables, #6 tables)));
   125 
   128 
   126 
   129 
   127 (* environment name *)
   130 (* environment name *)
   128 
   131 
   129 fun check_env opt_context name =
   132 type environment = {read: string, write: string};
   130   (case opt_context of
   133 
   131     NONE =>
   134 val separator = ">";
   132       if name = Isabelle then name
   135 
   133       else error ("Bad ML environment name " ^ quote name ^ " -- no context")
   136 fun parse_environment opt_context environment =
   134   | SOME context => if name = Isabelle then name else (get_env context name; name));
   137   let
   135 
   138     fun check name =
   136 fun context_env context opt_name =
   139       (case opt_context of
   137   check_env (SOME context) (the_default (Config.get_generic context ML_environment) opt_name);
   140         NONE =>
   138 
   141           if name = Isabelle then name
   139 fun default_env opt_name =
   142           else error ("Bad ML environment name " ^ quote name ^ " -- no context")
   140   let val opt_context = Context.get_generic_context () in
   143       | SOME context => if name = Isabelle then name else (get_env context name; name));
   141     check_env opt_context
   144 
   142       (case opt_name of
   145     val spec =
   143         SOME name => name
   146       if environment = "" then
   144       | NONE => (case opt_context of NONE => Isabelle | SOME context => context_env context NONE))
   147         (case opt_context of
   145   end;
   148           NONE => Isabelle
       
   149         | SOME context => Config.get_generic context ML_environment)
       
   150       else environment;
       
   151     val (read, write) =
       
   152       (case space_explode separator spec of
       
   153         [a] => (a, a)
       
   154       | [a, b] => (a, b)
       
   155       | _ => error ("Malformed ML environment specification: " ^ quote spec));
       
   156   in {read = check read, write = check write} end;
       
   157 
       
   158 fun print_environment {read, write} = read ^ separator ^ write;
       
   159 
       
   160 val SML_export = print_environment {read = SML, write = Isabelle};
       
   161 val SML_import = print_environment {read = Isabelle, write = SML};
   146 
   162 
   147 
   163 
   148 (* name space *)
   164 (* name space *)
   149 
   165 
   150 val bootstrap_name_space =
   166 val bootstrap_name_space =
   176       val structure2 = fold Symtab.update (#allStruct space ()) structure1;
   192       val structure2 = fold Symtab.update (#allStruct space ()) structure1;
   177       val signature2 = fold Symtab.update (#allSig space ()) signature1;
   193       val signature2 = fold Symtab.update (#allSig space ()) signature1;
   178       val functor2 = fold Symtab.update (#allFunct space ()) functor1;
   194       val functor2 = fold Symtab.update (#allFunct space ()) functor1;
   179     in (val2, type2, fixity2, structure2, signature2, functor2) end);
   195     in (val2, type2, fixity2, structure2, signature2, functor2) end);
   180 
   196 
   181 fun make_name_space {read, write} : ML_Name_Space.T =
   197 fun make_name_space environment : ML_Name_Space.T =
   182   let
   198   let
   183     val read_env = default_env read;
   199     val {read, write} = parse_environment (Context.get_generic_context ()) environment;
   184     val write_env = default_env write;
   200 
   185 
   201     fun lookup_env sel1 context name = Symtab.lookup (sel1 (get_env context read)) name;
   186     fun lookup_env sel1 context name = Symtab.lookup (sel1 (get_env context read_env)) name;
   202     fun dest_env sel1 context = Symtab.dest (sel1 (get_env context read));
   187     fun dest_env sel1 context = Symtab.dest (sel1 (get_env context read_env));
   203     fun enter_env ap1 entry = update_env write (ap1 (Symtab.update entry));
   188     fun enter_env ap1 entry = update_env write_env (ap1 (Symtab.update entry));
       
   189 
   204 
   190     fun lookup sel1 sel2 name =
   205     fun lookup sel1 sel2 name =
   191       if read_env = Isabelle then
   206       if read = Isabelle then
   192         (case Context.get_generic_context () of
   207         (case Context.get_generic_context () of
   193           NONE => sel2 ML_Name_Space.global name
   208           NONE => sel2 ML_Name_Space.global name
   194         | SOME context =>
   209         | SOME context =>
   195             (case lookup_env sel1 context name of
   210             (case lookup_env sel1 context name of
   196               NONE => if read_global context then sel2 ML_Name_Space.global name else NONE
   211               NONE => if read_global context then sel2 ML_Name_Space.global name else NONE
   197             | some => some))
   212             | some => some))
   198       else lookup_env sel1 (Context.the_generic_context ()) name;
   213       else lookup_env sel1 (Context.the_generic_context ()) name;
   199 
   214 
   200     fun all sel1 sel2 () =
   215     fun all sel1 sel2 () =
   201       sort_distinct (string_ord o apply2 #1)
   216       sort_distinct (string_ord o apply2 #1)
   202         (if read_env = Isabelle then
   217         (if read = Isabelle then
   203           (case Context.get_generic_context () of
   218           (case Context.get_generic_context () of
   204             NONE => sel2 ML_Name_Space.global ()
   219             NONE => sel2 ML_Name_Space.global ()
   205           | SOME context =>
   220           | SOME context =>
   206               dest_env sel1 context @
   221               dest_env sel1 context @
   207               (if read_global context then sel2 ML_Name_Space.global () else []))
   222               (if read_global context then sel2 ML_Name_Space.global () else []))
   208          else dest_env sel1 (Context.the_generic_context ()));
   223          else dest_env sel1 (Context.the_generic_context ()));
   209 
   224 
   210     fun enter ap1 sel2 entry =
   225     fun enter ap1 sel2 entry =
   211       if write_env = Isabelle then
   226       if write = Isabelle then
   212         (case Context.get_generic_context () of
   227         (case Context.get_generic_context () of
   213           NONE => sel2 ML_Name_Space.global entry
   228           NONE => sel2 ML_Name_Space.global entry
   214         | SOME context =>
   229         | SOME context =>
   215             (if write_global context then sel2 ML_Name_Space.global entry else ();
   230             (if write_global context then sel2 ML_Name_Space.global entry else ();
   216              Context.>> (enter_env ap1 entry)))
   231              Context.>> (enter_env ap1 entry)))
   235     allSig       = all #5 #allSig,
   250     allSig       = all #5 #allSig,
   236     allFunct     = all #6 #allFunct}
   251     allFunct     = all #6 #allFunct}
   237   end;
   252   end;
   238 
   253 
   239 val context: ML_Compiler0.context =
   254 val context: ML_Compiler0.context =
   240  {name_space = make_name_space {read = NONE, write = NONE},
   255  {name_space = make_name_space "",
   241   print_depth = NONE,
   256   print_depth = NONE,
   242   here = Position.here oo Position.line_file,
   257   here = Position.here oo Position.line_file,
   243   print = writeln,
   258   print = writeln,
   244   error = error};
   259   error = error};
   245 
   260