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