| author | wenzelm | 
| Tue, 28 Aug 2018 11:40:11 +0200 | |
| changeset 68829 | 1a4fa494a4a8 | 
| parent 68823 | 5e7b1ae10eb8 | 
| child 68865 | dd44e31ca2c6 | 
| permissions | -rw-r--r-- | 
| 31325 | 1 | (* Title: Pure/ML/ml_env.ML | 
| 2 | Author: Makarius | |
| 3 | ||
| 56275 
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
 wenzelm parents: 
56203diff
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: 
56203diff
changeset | 5 | implicit context. | 
| 31325 | 6 | *) | 
| 7 | ||
| 8 | signature ML_ENV = | |
| 9 | sig | |
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 10 | val Isabelle: string | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 11 | val SML: string | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 12 | val ML_environment_raw: Config.raw | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 13 | val ML_environment: string Config.T | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 14 | val ML_read_global_raw: Config.raw | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 15 | val ML_read_global: bool Config.T | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 16 | val ML_write_global_raw: Config.raw | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 17 | val ML_write_global: bool Config.T | 
| 68817 | 18 | val inherit: Context.generic -> Context.generic -> Context.generic | 
| 68821 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 19 | type operations = | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 20 |    {read_source: Input.source -> ML_Lex.token Antiquote.antiquote list,
 | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 21 | explode_token: ML_Lex.token -> char list} | 
| 68820 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 22 |   type environment = {read: string, write: string}
 | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 23 | val parse_environment: Context.generic option -> string -> environment | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 24 | val print_environment: environment -> string | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 25 | val SML_export: string | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 26 | val SML_import: string | 
| 68821 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 27 | val setup: string -> operations -> theory -> theory | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 28 | val Isabelle_operations: operations | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 29 | val SML_operations: operations | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 30 | val operations: Context.generic option -> string -> operations | 
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 31 | val forget_structure: string -> Context.generic -> Context.generic | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 32 | val bootstrap_name_space: Context.generic -> Context.generic | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 33 | val add_name_space: string -> ML_Name_Space.T -> Context.generic -> Context.generic | 
| 68820 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 34 | val make_name_space: string -> ML_Name_Space.T | 
| 62495 | 35 | val context: ML_Compiler0.context | 
| 36 | val name_space: ML_Name_Space.T | |
| 36163 
823c9400eb62
proper checking of ML functors (in Poly/ML 5.2 or later);
 wenzelm parents: 
33519diff
changeset | 37 | val check_functor: string -> unit | 
| 68818 | 38 | val get_breakpoint: Context.generic -> serial -> (bool Unsynchronized.ref * Position.T) option | 
| 39 | val add_breakpoints: (serial * (bool Unsynchronized.ref * Thread_Position.T)) list -> unit | |
| 31325 | 40 | end | 
| 41 | ||
| 42 | structure ML_Env: ML_ENV = | |
| 43 | struct | |
| 44 | ||
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 45 | (* named environments *) | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 46 | |
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 47 | val Isabelle = "Isabelle"; | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 48 | val SML = "SML"; | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 49 | |
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 50 | val ML_environment_raw = Config.declare ("ML_environment", \<^here>) (fn _ => Config.String Isabelle);
 | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 51 | val ML_environment = Config.string ML_environment_raw; | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 52 | |
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 53 | |
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 54 | (* global read/write *) | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 55 | |
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 56 | val ML_read_global_raw = Config.declare ("ML_read_global", \<^here>) (fn _ => Config.Bool true);
 | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 57 | val ML_write_global_raw = Config.declare ("ML_write_global", \<^here>) (fn _ => Config.Bool true);
 | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 58 | |
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 59 | val ML_read_global = Config.bool ML_read_global_raw; | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 60 | val ML_write_global = Config.bool ML_write_global_raw; | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 61 | |
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 62 | fun read_global context = Config.get_generic context ML_read_global; | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 63 | fun write_global context = Config.get_generic context ML_write_global; | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 64 | |
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 65 | |
| 68814 | 66 | (* name space tables *) | 
| 31328 | 67 | |
| 56275 
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
 wenzelm parents: 
56203diff
changeset | 68 | type tables = | 
| 62934 | 69 | PolyML.NameSpace.Values.value Symtab.table * | 
| 70 | PolyML.NameSpace.TypeConstrs.typeConstr Symtab.table * | |
| 71 | PolyML.NameSpace.Infixes.fixity Symtab.table * | |
| 72 | PolyML.NameSpace.Structures.structureVal Symtab.table * | |
| 73 | PolyML.NameSpace.Signatures.signatureVal Symtab.table * | |
| 74 | PolyML.NameSpace.Functors.functorVal Symtab.table; | |
| 56275 
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
 wenzelm parents: 
56203diff
changeset | 75 | |
| 68814 | 76 | val empty_tables: tables = | 
| 77 | (Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty); | |
| 78 | ||
| 56275 
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
 wenzelm parents: 
56203diff
changeset | 79 | fun merge_tables | 
| 
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
 wenzelm parents: 
56203diff
changeset | 80 | ((val1, type1, fixity1, structure1, signature1, functor1), | 
| 
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
 wenzelm parents: 
56203diff
changeset | 81 | (val2, type2, fixity2, structure2, signature2, functor2)) : tables = | 
| 
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
 wenzelm parents: 
56203diff
changeset | 82 | (Symtab.merge (K true) (val1, val2), | 
| 
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
 wenzelm parents: 
56203diff
changeset | 83 | Symtab.merge (K true) (type1, type2), | 
| 
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
 wenzelm parents: 
56203diff
changeset | 84 | Symtab.merge (K true) (fixity1, fixity2), | 
| 
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
 wenzelm parents: 
56203diff
changeset | 85 | Symtab.merge (K true) (structure1, structure2), | 
| 
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
 wenzelm parents: 
56203diff
changeset | 86 | Symtab.merge (K true) (signature1, signature2), | 
| 
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
 wenzelm parents: 
56203diff
changeset | 87 | Symtab.merge (K true) (functor1, functor2)); | 
| 
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
 wenzelm parents: 
56203diff
changeset | 88 | |
| 68814 | 89 | val sml_tables: tables = | 
| 90 | (Symtab.make ML_Name_Space.sml_val, | |
| 91 | Symtab.make ML_Name_Space.sml_type, | |
| 92 | Symtab.make ML_Name_Space.sml_fixity, | |
| 93 | Symtab.make ML_Name_Space.sml_structure, | |
| 94 | Symtab.make ML_Name_Space.sml_signature, | |
| 95 | Symtab.make ML_Name_Space.sml_functor); | |
| 96 | ||
| 97 | ||
| 98 | (* context data *) | |
| 99 | ||
| 68821 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 100 | type operations = | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 101 |  {read_source: Input.source -> ML_Lex.token Antiquote.antiquote list,
 | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 102 | explode_token: ML_Lex.token -> char list}; | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 103 | |
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 104 | type env = tables * operations; | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 105 | |
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 106 | structure Data = Generic_Data | 
| 31325 | 107 | ( | 
| 68821 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 108 | type T = env Name_Space.table * (bool Unsynchronized.ref * Position.T) Inttab.table; | 
| 68818 | 109 | val empty: T = (Name_Space.empty_table "ML_environment", Inttab.empty); | 
| 110 | val extend = I; | |
| 111 | fun merge ((envs1, breakpoints1), (envs2, breakpoints2)) : T = | |
| 68821 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 112 | ((envs1, envs2) |> Name_Space.join_tables | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 113 | (K (fn ((tables1, ops), (tables2, _)) => (merge_tables (tables1, tables2), ops))), | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 114 | Inttab.merge (K true) (breakpoints1, breakpoints2)); | 
| 31325 | 115 | ); | 
| 116 | ||
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 117 | val inherit = Data.put o Data.get; | 
| 31325 | 118 | |
| 68821 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 119 | val get_env = Name_Space.get o #1 o Data.get; | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 120 | val get_tables = #1 oo get_env; | 
| 60746 | 121 | |
| 68821 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 122 | fun update_tables env_name f context = context |> (Data.map o apfst) (fn envs => | 
| 68818 | 123 | let val _ = Name_Space.get envs env_name; | 
| 68821 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 124 | in Name_Space.map_table_entry env_name (apfst f) envs end); | 
| 62902 
3c0f53eae166
more conventional theory syntax for ML bootstrap, with 'ML_file' instead of 'use';
 wenzelm parents: 
62889diff
changeset | 125 | |
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 126 | fun forget_structure name context = | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 127 | (if write_global context then ML_Name_Space.forget_structure name else (); | 
| 68821 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 128 | context |> update_tables Isabelle (fn tables => | 
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 129 | (#1 tables, #2 tables, #3 tables, Symtab.delete_safe name (#4 tables), #5 tables, #6 tables))); | 
| 62902 
3c0f53eae166
more conventional theory syntax for ML bootstrap, with 'ML_file' instead of 'use';
 wenzelm parents: 
62889diff
changeset | 130 | |
| 
3c0f53eae166
more conventional theory syntax for ML bootstrap, with 'ML_file' instead of 'use';
 wenzelm parents: 
62889diff
changeset | 131 | |
| 68817 | 132 | (* environment name *) | 
| 133 | ||
| 68820 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 134 | type environment = {read: string, write: string};
 | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 135 | |
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 136 | val separator = ">"; | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 137 | |
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 138 | fun parse_environment opt_context environment = | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 139 | let | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 140 | fun check name = | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 141 | (case opt_context of | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 142 | NONE => | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 143 | if name = Isabelle then name | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 144 |           else error ("Bad ML environment name " ^ quote name ^ " -- no context")
 | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 145 | | SOME context => if name = Isabelle then name else (get_env context name; name)); | 
| 68817 | 146 | |
| 68820 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 147 | val spec = | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 148 | if environment = "" then | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 149 | (case opt_context of | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 150 | NONE => Isabelle | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 151 | | SOME context => Config.get_generic context ML_environment) | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 152 | else environment; | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 153 | val (read, write) = | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 154 | (case space_explode separator spec of | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 155 | [a] => (a, a) | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 156 | | [a, b] => (a, b) | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 157 |       | _ => error ("Malformed ML environment specification: " ^ quote spec));
 | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 158 |   in {read = check read, write = check write} end;
 | 
| 68817 | 159 | |
| 68820 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 160 | fun print_environment {read, write} = read ^ separator ^ write;
 | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 161 | |
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 162 | val SML_export = print_environment {read = SML, write = Isabelle};
 | 
| 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 163 | val SML_import = print_environment {read = Isabelle, write = SML};
 | 
| 68817 | 164 | |
| 165 | ||
| 68821 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 166 | (* setup operations *) | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 167 | |
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 168 | fun setup env_name ops thy = | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 169 | thy |> (Context.theory_map o Data.map o apfst) (fn envs => | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 170 | let | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 171 | val thy' = Sign.map_naming (K Name_Space.global_naming) thy; | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 172 | val tables = if env_name = Isabelle then empty_tables else sml_tables; | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 173 | val (_, envs') = | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 174 | Name_Space.define (Context.Theory thy') true (Binding.name env_name, (tables, ops)) envs; | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 175 | in envs' end); | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 176 | |
| 68823 | 177 | val Isabelle_operations: operations = | 
| 178 |  {read_source = ML_Lex.read_source,
 | |
| 179 | explode_token = ML_Lex.check_content_of #> Symbol.explode #> maps (Symbol.esc #> String.explode)}; | |
| 68821 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 180 | |
| 68823 | 181 | val SML_operations: operations = | 
| 182 |  {read_source = ML_Lex.read_source_sml,
 | |
| 183 | explode_token = ML_Lex.check_content_of #> String.explode}; | |
| 68821 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 184 | |
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 185 | val _ = Theory.setup (setup Isabelle Isabelle_operations #> setup SML SML_operations); | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 186 | |
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 187 | fun operations opt_context environment = | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 188 | let val env = #read (parse_environment opt_context environment) in | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 189 | (case opt_context of | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 190 | NONE => Isabelle_operations | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 191 | | SOME context => #2 (get_env context env)) | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 192 | end; | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 193 | |
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 194 | |
| 56618 
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
 wenzelm parents: 
56275diff
changeset | 195 | (* name space *) | 
| 56275 
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
 wenzelm parents: 
56203diff
changeset | 196 | |
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 197 | val bootstrap_name_space = | 
| 68821 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 198 | update_tables Isabelle (fn (tables: tables) => | 
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 199 | let | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 200 | fun update entries (x, y) = member (op =) entries x ? Symtab.update (x, y); | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 201 | val bootstrap_val = update ML_Name_Space.bootstrap_values; | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 202 | val bootstrap_structure = update ML_Name_Space.bootstrap_structures; | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 203 | val bootstrap_signature = update ML_Name_Space.bootstrap_signatures; | 
| 62873 
2f9c8a18f832
support bootstrap from fresh SML environment, with syntax of Isabelle/ML or SML;
 wenzelm parents: 
62839diff
changeset | 204 | |
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 205 | val (val1, type1, fixity1, structure1, signature1, functor1) = sml_tables; | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 206 | val val2 = val1 | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 207 | |> fold bootstrap_val (#allVal ML_Name_Space.global ()) | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 208 | |> Symtab.fold bootstrap_val (#1 tables); | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 209 | val structure2 = structure1 | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 210 | |> fold bootstrap_structure (#allStruct ML_Name_Space.global ()) | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 211 | |> Symtab.fold bootstrap_structure (#4 tables); | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 212 | val signature2 = signature1 | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 213 | |> fold bootstrap_signature (#allSig ML_Name_Space.global ()) | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 214 | |> Symtab.fold bootstrap_signature (#5 tables); | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 215 | in (val2, type1, fixity1, structure2, signature2, functor1) end); | 
| 68276 | 216 | |
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 217 | fun add_name_space env (space: ML_Name_Space.T) = | 
| 68821 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 218 | update_tables env (fn (val1, type1, fixity1, structure1, signature1, functor1) => | 
| 60858 | 219 | let | 
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 220 | val val2 = fold Symtab.update (#allVal space ()) val1; | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 221 | val type2 = fold Symtab.update (#allType space ()) type1; | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 222 | val fixity2 = fold Symtab.update (#allFix space ()) fixity1; | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 223 | val structure2 = fold Symtab.update (#allStruct space ()) structure1; | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 224 | val signature2 = fold Symtab.update (#allSig space ()) signature1; | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 225 | val functor2 = fold Symtab.update (#allFunct space ()) functor1; | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 226 | in (val2, type2, fixity2, structure2, signature2, functor2) end); | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 227 | |
| 68820 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 228 | fun make_name_space environment : ML_Name_Space.T = | 
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 229 | let | 
| 68820 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 230 |     val {read, write} = parse_environment (Context.get_generic_context ()) environment;
 | 
| 60858 | 231 | |
| 68821 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 232 | fun lookup_env sel1 context name = Symtab.lookup (sel1 (get_tables context read)) name; | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 233 | fun dest_env sel1 context = Symtab.dest (sel1 (get_tables context read)); | 
| 
877534be1930
explicit setup of operations: avoid hardwired stuff;
 wenzelm parents: 
68820diff
changeset | 234 | fun enter_env ap1 entry = update_tables write (ap1 (Symtab.update entry)); | 
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 235 | |
| 31325 | 236 | fun lookup sel1 sel2 name = | 
| 68820 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 237 | if read = Isabelle then | 
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 238 | (case Context.get_generic_context () of | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 239 | NONE => sel2 ML_Name_Space.global name | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 240 | | SOME context => | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 241 | (case lookup_env sel1 context name of | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 242 | NONE => if read_global context then sel2 ML_Name_Space.global name else NONE | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 243 | | some => some)) | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 244 | else lookup_env sel1 (Context.the_generic_context ()) name; | 
| 31325 | 245 | |
| 246 | fun all sel1 sel2 () = | |
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 247 | sort_distinct (string_ord o apply2 #1) | 
| 68820 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 248 | (if read = Isabelle then | 
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 249 | (case Context.get_generic_context () of | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 250 | NONE => sel2 ML_Name_Space.global () | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 251 | | SOME context => | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 252 | dest_env sel1 context @ | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 253 | (if read_global context then sel2 ML_Name_Space.global () else [])) | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 254 | else dest_env sel1 (Context.the_generic_context ())); | 
| 31325 | 255 | |
| 256 | fun enter ap1 sel2 entry = | |
| 68820 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 257 | if write = Isabelle then | 
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 258 | (case Context.get_generic_context () of | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 259 | NONE => sel2 ML_Name_Space.global entry | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 260 | | SOME context => | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 261 | (if write_global context then sel2 ML_Name_Space.global entry else (); | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 262 | Context.>> (enter_env ap1 entry))) | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 263 | else Context.>> (enter_env ap1 entry); | 
| 31325 | 264 | in | 
| 265 |    {lookupVal    = lookup #1 #lookupVal,
 | |
| 266 | lookupType = lookup #2 #lookupType, | |
| 267 | lookupFix = lookup #3 #lookupFix, | |
| 268 | lookupStruct = lookup #4 #lookupStruct, | |
| 269 | lookupSig = lookup #5 #lookupSig, | |
| 270 | lookupFunct = lookup #6 #lookupFunct, | |
| 271 | enterVal = enter (fn h => fn (a, b, c, d, e, f) => (h a, b, c, d, e, f)) #enterVal, | |
| 272 | enterType = enter (fn h => fn (a, b, c, d, e, f) => (a, h b, c, d, e, f)) #enterType, | |
| 273 | enterFix = enter (fn h => fn (a, b, c, d, e, f) => (a, b, h c, d, e, f)) #enterFix, | |
| 274 | enterStruct = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, h d, e, f)) #enterStruct, | |
| 275 | enterSig = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, d, h e, f)) #enterSig, | |
| 276 | enterFunct = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, d, e, h f)) #enterFunct, | |
| 277 | allVal = all #1 #allVal, | |
| 278 | allType = all #2 #allType, | |
| 279 | allFix = all #3 #allFix, | |
| 280 | allStruct = all #4 #allStruct, | |
| 281 | allSig = all #5 #allSig, | |
| 282 | allFunct = all #6 #allFunct} | |
| 283 | end; | |
| 284 | ||
| 62495 | 285 | val context: ML_Compiler0.context = | 
| 68820 
2e4df245754e
clarified environment: allow "read>write" specification;
 wenzelm parents: 
68818diff
changeset | 286 |  {name_space = make_name_space "",
 | 
| 62716 
d80b9f4990e4
explicit print_depth for the sake of Spec_Check.determine_type;
 wenzelm parents: 
62657diff
changeset | 287 | print_depth = NONE, | 
| 62493 | 288 | here = Position.here oo Position.line_file, | 
| 31325 | 289 | print = writeln, | 
| 290 | error = error}; | |
| 291 | ||
| 62495 | 292 | val name_space = #name_space context; | 
| 56618 
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
 wenzelm parents: 
56275diff
changeset | 293 | |
| 62495 | 294 | val is_functor = is_some o #lookupFunct name_space; | 
| 36163 
823c9400eb62
proper checking of ML functors (in Poly/ML 5.2 or later);
 wenzelm parents: 
33519diff
changeset | 295 | |
| 
823c9400eb62
proper checking of ML functors (in Poly/ML 5.2 or later);
 wenzelm parents: 
33519diff
changeset | 296 | fun check_functor name = | 
| 36165 | 297 | 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: 
33519diff
changeset | 298 |   else error ("Unknown ML functor: " ^ quote name);
 | 
| 
823c9400eb62
proper checking of ML functors (in Poly/ML 5.2 or later);
 wenzelm parents: 
33519diff
changeset | 299 | |
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 300 | |
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 301 | (* breakpoints *) | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 302 | |
| 68818 | 303 | val get_breakpoint = Inttab.lookup o #2 o Data.get; | 
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 304 | |
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 305 | fun add_breakpoints more_breakpoints = | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 306 | if is_some (Context.get_generic_context ()) then | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 307 | Context.>> | 
| 68818 | 308 | ((Data.map o apsnd) | 
| 309 | (fold (Inttab.update_new o (apsnd o apsnd) Position.make) more_breakpoints)) | |
| 68816 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 310 | else (); | 
| 
5a53724fe247
support named ML environments, notably "Isabelle", "SML";
 wenzelm parents: 
68814diff
changeset | 311 | |
| 31325 | 312 | end; |