| author | wenzelm | 
| Tue, 22 Mar 2022 19:19:09 +0100 | |
| changeset 75304 | 8f100a957f08 | 
| parent 74561 | 8e6c973003c8 | 
| child 76058 | d45a45eb1aee | 
| permissions | -rw-r--r-- | 
| 24114 | 1 | (* Title: Pure/config.ML | 
| 2 | Author: Makarius | |
| 3 | ||
| 4 | Configuration options as values within the local context. | |
| 5 | *) | |
| 6 | ||
| 7 | signature CONFIG = | |
| 8 | sig | |
| 40291 | 9 | datatype value = Bool of bool | Int of int | Real of real | String of string | 
| 24114 | 10 | val print_value: value -> string | 
| 11 | val print_type: value -> string | |
| 12 | type 'a T | |
| 69575 | 13 | val name_of: 'a T -> string | 
| 14 | val pos_of: 'a T -> Position.T | |
| 69576 | 15 | val apply: 'a T -> Proof.context -> 'a | 
| 24114 | 16 | val get: Proof.context -> 'a T -> 'a | 
| 17 |   val map: 'a T -> ('a -> 'a) -> Proof.context -> Proof.context
 | |
| 18 | val put: 'a T -> 'a -> Proof.context -> Proof.context | |
| 68827 | 19 | val restore: 'a T -> Proof.context -> Proof.context -> Proof.context | 
| 69576 | 20 | val apply_global: 'a T -> theory -> 'a | 
| 36787 
f60e4dd6d76f
renamed Config.get_thy to Config.get_global etc. to indicate that this is not the real thing;
 wenzelm parents: 
36002diff
changeset | 21 | val get_global: theory -> 'a T -> 'a | 
| 
f60e4dd6d76f
renamed Config.get_thy to Config.get_global etc. to indicate that this is not the real thing;
 wenzelm parents: 
36002diff
changeset | 22 |   val map_global: 'a T -> ('a -> 'a) -> theory -> theory
 | 
| 
f60e4dd6d76f
renamed Config.get_thy to Config.get_global etc. to indicate that this is not the real thing;
 wenzelm parents: 
36002diff
changeset | 23 | val put_global: 'a T -> 'a -> theory -> theory | 
| 68827 | 24 | val restore_global: 'a T -> theory -> theory -> theory | 
| 69576 | 25 | val apply_generic: 'a T -> Context.generic -> 'a | 
| 24114 | 26 | val get_generic: Context.generic -> 'a T -> 'a | 
| 27 |   val map_generic: 'a T -> ('a -> 'a) -> Context.generic -> Context.generic
 | |
| 28 | val put_generic: 'a T -> 'a -> Context.generic -> Context.generic | |
| 68827 | 29 | val restore_generic: 'a T -> Context.generic -> Context.generic -> Context.generic | 
| 69575 | 30 | val bool: value T -> bool T | 
| 31 | val bool_value: bool T -> value T | |
| 32 | val int: value T -> int T | |
| 33 | val int_value: int T -> value T | |
| 34 | val real: value T -> real T | |
| 35 | val real_value: real T -> value T | |
| 36 | val string: value T -> string T | |
| 37 | val string_value: string T -> value T | |
| 38 | val declare: string * Position.T -> (Context.generic -> value) -> value T | |
| 39 | val declare_bool: string * Position.T -> (Context.generic -> bool) -> bool T | |
| 40 | val declare_int: string * Position.T -> (Context.generic -> int) -> int T | |
| 41 | val declare_real: string * Position.T -> (Context.generic -> real) -> real T | |
| 42 | val declare_string: string * Position.T -> (Context.generic -> string) -> string T | |
| 43 | val declare_option: string * Position.T -> value T | |
| 44 | val declare_option_bool: string * Position.T -> bool T | |
| 45 | val declare_option_int: string * Position.T -> int T | |
| 46 | val declare_option_real: string * Position.T -> real T | |
| 47 | val declare_option_string: string * Position.T -> string T | |
| 24114 | 48 | end; | 
| 49 | ||
| 50 | structure Config: CONFIG = | |
| 51 | struct | |
| 52 | ||
| 53 | (* simple values *) | |
| 54 | ||
| 55 | datatype value = | |
| 56 | Bool of bool | | |
| 57 | Int of int | | |
| 40291 | 58 | Real of real | | 
| 24114 | 59 | String of string; | 
| 60 | ||
| 61 | fun print_value (Bool true) = "true" | |
| 62 | | print_value (Bool false) = "false" | |
| 69574 | 63 | | print_value (Int i) = Value.print_int i | 
| 63806 | 64 | | print_value (Real x) = Value.print_real x | 
| 24114 | 65 | | print_value (String s) = quote s; | 
| 66 | ||
| 38804 | 67 | fun print_type (Bool _) = "bool" | 
| 68 | | print_type (Int _) = "int" | |
| 40291 | 69 | | print_type (Real _) = "real" | 
| 24114 | 70 | | print_type (String _) = "string"; | 
| 71 | ||
| 72 | fun same_type (Bool _) (Bool _) = true | |
| 73 | | same_type (Int _) (Int _) = true | |
| 40291 | 74 | | same_type (Real _) (Real _) = true | 
| 24114 | 75 | | same_type (String _) (String _) = true | 
| 76 | | same_type _ _ = false; | |
| 77 | ||
| 56438 | 78 | fun type_check (name, pos) f value = | 
| 24114 | 79 | let | 
| 80 | val value' = f value; | |
| 81 | val _ = same_type value value' orelse | |
| 56438 | 82 |       error ("Ill-typed configuration option " ^ quote name ^ Position.here pos ^ ": " ^
 | 
| 24114 | 83 | print_type value ^ " expected,\nbut " ^ print_type value' ^ " was found"); | 
| 84 | in value' end; | |
| 85 | ||
| 86 | ||
| 87 | (* abstract configuration options *) | |
| 88 | ||
| 89 | datatype 'a T = Config of | |
| 24125 | 90 |  {name: string,
 | 
| 56438 | 91 | pos: Position.T, | 
| 24125 | 92 | get_value: Context.generic -> 'a, | 
| 24114 | 93 |   map_value: ('a -> 'a) -> Context.generic -> Context.generic};
 | 
| 94 | ||
| 69575 | 95 | fun name_of (Config {name, ...}) = name;
 | 
| 96 | fun pos_of (Config {pos, ...}) = pos;
 | |
| 24114 | 97 | |
| 69576 | 98 | fun apply_generic (Config {get_value, ...}) = get_value;
 | 
| 99 | fun get_generic context config = apply_generic config context; | |
| 24114 | 100 | fun map_generic (Config {map_value, ...}) f context = map_value f context;
 | 
| 101 | fun put_generic config value = map_generic config (K value); | |
| 69576 | 102 | fun restore_generic config = put_generic config o apply_generic config; | 
| 24114 | 103 | |
| 69576 | 104 | fun apply_ctxt config = apply_generic config o Context.Proof; | 
| 24114 | 105 | fun get_ctxt ctxt = get_generic (Context.Proof ctxt); | 
| 106 | fun map_ctxt config f = Context.proof_map (map_generic config f); | |
| 107 | fun put_ctxt config value = map_ctxt config (K value); | |
| 69576 | 108 | fun restore_ctxt config = put_ctxt config o apply_ctxt config; | 
| 24114 | 109 | |
| 69576 | 110 | fun apply_global config = apply_generic config o Context.Theory; | 
| 36787 
f60e4dd6d76f
renamed Config.get_thy to Config.get_global etc. to indicate that this is not the real thing;
 wenzelm parents: 
36002diff
changeset | 111 | fun get_global thy = get_generic (Context.Theory thy); | 
| 
f60e4dd6d76f
renamed Config.get_thy to Config.get_global etc. to indicate that this is not the real thing;
 wenzelm parents: 
36002diff
changeset | 112 | fun map_global config f = Context.theory_map (map_generic config f); | 
| 
f60e4dd6d76f
renamed Config.get_thy to Config.get_global etc. to indicate that this is not the real thing;
 wenzelm parents: 
36002diff
changeset | 113 | fun put_global config value = map_global config (K value); | 
| 69576 | 114 | fun restore_global config = put_global config o apply_global config; | 
| 24114 | 115 | |
| 116 | ||
| 69575 | 117 | (* coerce type *) | 
| 118 | ||
| 119 | fun coerce make dest (Config {name, pos, get_value, map_value}) = Config
 | |
| 120 |  {name = name,
 | |
| 121 | pos = pos, | |
| 122 | get_value = dest o get_value, | |
| 123 | map_value = fn f => map_value (make o f o dest)}; | |
| 124 | ||
| 125 | fun coerce_pair make dest = (coerce make dest, coerce dest make); | |
| 126 | ||
| 127 | val (bool, bool_value) = coerce_pair Bool (fn Bool b => b); | |
| 128 | val (int, int_value) = coerce_pair Int (fn Int i => i); | |
| 129 | val (real, real_value) = coerce_pair Real (fn Real x => x); | |
| 130 | val (string, string_value) = coerce_pair String (fn String s => s); | |
| 131 | ||
| 132 | ||
| 133 | (* context data *) | |
| 24114 | 134 | |
| 63806 | 135 | structure Data = Generic_Data | 
| 24114 | 136 | ( | 
| 137 | type T = value Inttab.table; | |
| 138 | val empty = Inttab.empty; | |
| 33519 | 139 | fun merge data = Inttab.merge (K true) data; | 
| 24114 | 140 | ); | 
| 141 | ||
| 58951 
8b7caf447357
removed obsolete global-only options, which did not work out anyway (due to complexity of local_theory sandwich);
 wenzelm parents: 
57858diff
changeset | 142 | fun declare (name, pos) default = | 
| 24114 | 143 | let | 
| 144 | val id = serial (); | |
| 145 | ||
| 36000 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 146 | fun get_value context = | 
| 63806 | 147 | (case Inttab.lookup (Data.get context) id of | 
| 36000 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 148 | SOME value => value | 
| 36002 
f4f343500249
pass raw Context.generic, to avoid wasteful Context.proof_of -- Config.get_thy is often used in performance critical spots like unify.ML;
 wenzelm parents: 
36000diff
changeset | 149 | | NONE => default context); | 
| 36000 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 150 | |
| 58951 
8b7caf447357
removed obsolete global-only options, which did not work out anyway (due to complexity of local_theory sandwich);
 wenzelm parents: 
57858diff
changeset | 151 | fun map_value f context = | 
| 63806 | 152 | Data.map (Inttab.update (id, type_check (name, pos) f (get_value context))) context; | 
| 58951 
8b7caf447357
removed obsolete global-only options, which did not work out anyway (due to complexity of local_theory sandwich);
 wenzelm parents: 
57858diff
changeset | 153 | in | 
| 
8b7caf447357
removed obsolete global-only options, which did not work out anyway (due to complexity of local_theory sandwich);
 wenzelm parents: 
57858diff
changeset | 154 |     Config {name = name, pos = pos, get_value = get_value, map_value = map_value}
 | 
| 
8b7caf447357
removed obsolete global-only options, which did not work out anyway (due to complexity of local_theory sandwich);
 wenzelm parents: 
57858diff
changeset | 155 | end; | 
| 24114 | 156 | |
| 69575 | 157 | fun declare_bool name default = bool (declare name (Bool o default)); | 
| 158 | fun declare_int name default = int (declare name (Int o default)); | |
| 159 | fun declare_real name default = real (declare name (Real o default)); | |
| 160 | fun declare_string name default = string (declare name (String o default)); | |
| 161 | ||
| 162 | ||
| 163 | (* system options *) | |
| 164 | ||
| 58951 
8b7caf447357
removed obsolete global-only options, which did not work out anyway (due to complexity of local_theory sandwich);
 wenzelm parents: 
57858diff
changeset | 165 | fun declare_option (name, pos) = | 
| 51947 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 166 | let | 
| 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 167 | val typ = Options.default_typ name; | 
| 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 168 | val default = | 
| 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 169 | if typ = Options.boolT then fn _ => Bool (Options.default_bool name) | 
| 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 170 | else if typ = Options.intT then fn _ => Int (Options.default_int name) | 
| 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 171 | else if typ = Options.realT then fn _ => Real (Options.default_real name) | 
| 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 172 | else if typ = Options.stringT then fn _ => String (Options.default_string name) | 
| 56438 | 173 |       else error ("Unknown type for option " ^ quote name ^ Position.here pos ^ " : " ^ quote typ);
 | 
| 58951 
8b7caf447357
removed obsolete global-only options, which did not work out anyway (due to complexity of local_theory sandwich);
 wenzelm parents: 
57858diff
changeset | 174 | in declare (name, pos) default end; | 
| 51947 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 175 | |
| 69575 | 176 | val declare_option_bool = bool o declare_option; | 
| 177 | val declare_option_int = int o declare_option; | |
| 178 | val declare_option_real = real o declare_option; | |
| 179 | val declare_option_string = string o declare_option; | |
| 24114 | 180 | |
| 181 | (*final declarations of this structure!*) | |
| 69576 | 182 | val apply = apply_ctxt; | 
| 24114 | 183 | val get = get_ctxt; | 
| 184 | val map = map_ctxt; | |
| 185 | val put = put_ctxt; | |
| 68827 | 186 | val restore = restore_ctxt; | 
| 24114 | 187 | |
| 188 | end; |