| author | wenzelm | 
| Wed, 10 Jun 2015 19:05:19 +0200 | |
| changeset 60419 | 7c2404ca7f49 | 
| parent 58951 | 8b7caf447357 | 
| child 63806 | c54a53ef1873 | 
| 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 | |
| 39163 
4d701c0388c3
more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
 wenzelm parents: 
39116diff
changeset | 13 | type raw = value T | 
| 
4d701c0388c3
more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
 wenzelm parents: 
39116diff
changeset | 14 | val bool: raw -> bool T | 
| 
4d701c0388c3
more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
 wenzelm parents: 
39116diff
changeset | 15 | val int: raw -> int T | 
| 40291 | 16 | val real: raw -> real T | 
| 39163 
4d701c0388c3
more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
 wenzelm parents: 
39116diff
changeset | 17 | val string: raw -> string T | 
| 24114 | 18 | val get: Proof.context -> 'a T -> 'a | 
| 19 |   val map: 'a T -> ('a -> 'a) -> Proof.context -> Proof.context
 | |
| 20 | val put: 'a T -> 'a -> Proof.context -> Proof.context | |
| 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 | 
| 24114 | 24 | val get_generic: Context.generic -> 'a T -> 'a | 
| 25 |   val map_generic: 'a T -> ('a -> 'a) -> Context.generic -> Context.generic
 | |
| 26 | val put_generic: 'a T -> 'a -> Context.generic -> Context.generic | |
| 56438 | 27 | val declare: string * Position.T -> (Context.generic -> value) -> raw | 
| 28 | val declare_option: string * Position.T -> raw | |
| 24125 | 29 | val name_of: 'a T -> string | 
| 56438 | 30 | val pos_of: 'a T -> Position.T | 
| 24114 | 31 | end; | 
| 32 | ||
| 33 | structure Config: CONFIG = | |
| 34 | struct | |
| 35 | ||
| 36 | (* simple values *) | |
| 37 | ||
| 38 | datatype value = | |
| 39 | Bool of bool | | |
| 40 | Int of int | | |
| 40291 | 41 | Real of real | | 
| 24114 | 42 | String of string; | 
| 43 | ||
| 44 | fun print_value (Bool true) = "true" | |
| 45 | | print_value (Bool false) = "false" | |
| 46 | | print_value (Int i) = signed_string_of_int i | |
| 51990 | 47 | | print_value (Real x) = Markup.print_real x | 
| 24114 | 48 | | print_value (String s) = quote s; | 
| 49 | ||
| 38804 | 50 | fun print_type (Bool _) = "bool" | 
| 51 | | print_type (Int _) = "int" | |
| 40291 | 52 | | print_type (Real _) = "real" | 
| 24114 | 53 | | print_type (String _) = "string"; | 
| 54 | ||
| 55 | fun same_type (Bool _) (Bool _) = true | |
| 56 | | same_type (Int _) (Int _) = true | |
| 40291 | 57 | | same_type (Real _) (Real _) = true | 
| 24114 | 58 | | same_type (String _) (String _) = true | 
| 59 | | same_type _ _ = false; | |
| 60 | ||
| 56438 | 61 | fun type_check (name, pos) f value = | 
| 24114 | 62 | let | 
| 63 | val value' = f value; | |
| 64 | val _ = same_type value value' orelse | |
| 56438 | 65 |       error ("Ill-typed configuration option " ^ quote name ^ Position.here pos ^ ": " ^
 | 
| 24114 | 66 | print_type value ^ " expected,\nbut " ^ print_type value' ^ " was found"); | 
| 67 | in value' end; | |
| 68 | ||
| 69 | ||
| 70 | (* abstract configuration options *) | |
| 71 | ||
| 72 | datatype 'a T = Config of | |
| 24125 | 73 |  {name: string,
 | 
| 56438 | 74 | pos: Position.T, | 
| 24125 | 75 | get_value: Context.generic -> 'a, | 
| 24114 | 76 |   map_value: ('a -> 'a) -> Context.generic -> Context.generic};
 | 
| 77 | ||
| 39163 
4d701c0388c3
more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
 wenzelm parents: 
39116diff
changeset | 78 | type raw = value T; | 
| 
4d701c0388c3
more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
 wenzelm parents: 
39116diff
changeset | 79 | |
| 56438 | 80 | fun coerce make dest (Config {name, pos, get_value, map_value}) = Config
 | 
| 24125 | 81 |  {name = name,
 | 
| 56438 | 82 | pos = pos, | 
| 24125 | 83 | get_value = dest o get_value, | 
| 84 | map_value = fn f => map_value (make o f o dest)}; | |
| 24114 | 85 | |
| 86 | val bool = coerce Bool (fn Bool b => b); | |
| 87 | val int = coerce Int (fn Int i => i); | |
| 40291 | 88 | val real = coerce Real (fn Real x => x); | 
| 24114 | 89 | val string = coerce String (fn String s => s); | 
| 90 | ||
| 91 | fun get_generic context (Config {get_value, ...}) = get_value context;
 | |
| 92 | fun map_generic (Config {map_value, ...}) f context = map_value f context;
 | |
| 93 | fun put_generic config value = map_generic config (K value); | |
| 94 | ||
| 95 | fun get_ctxt ctxt = get_generic (Context.Proof ctxt); | |
| 96 | fun map_ctxt config f = Context.proof_map (map_generic config f); | |
| 97 | fun put_ctxt config value = map_ctxt config (K value); | |
| 98 | ||
| 36787 
f60e4dd6d76f
renamed Config.get_thy to Config.get_global etc. to indicate that this is not the real thing;
 wenzelm parents: 
36002diff
changeset | 99 | 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 | 100 | 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 | 101 | fun put_global config value = map_global config (K value); | 
| 24114 | 102 | |
| 103 | ||
| 104 | (* context information *) | |
| 105 | ||
| 33519 | 106 | structure Value = Generic_Data | 
| 24114 | 107 | ( | 
| 108 | type T = value Inttab.table; | |
| 109 | val empty = Inttab.empty; | |
| 110 | val extend = I; | |
| 33519 | 111 | fun merge data = Inttab.merge (K true) data; | 
| 24114 | 112 | ); | 
| 113 | ||
| 58951 
8b7caf447357
removed obsolete global-only options, which did not work out anyway (due to complexity of local_theory sandwich);
 wenzelm parents: 
57858diff
changeset | 114 | fun declare (name, pos) default = | 
| 24114 | 115 | let | 
| 116 | val id = serial (); | |
| 117 | ||
| 36000 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 118 | fun get_value context = | 
| 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 119 | (case Inttab.lookup (Value.get context) id of | 
| 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 120 | 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 | 121 | | NONE => default context); | 
| 36000 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 122 | |
| 58951 
8b7caf447357
removed obsolete global-only options, which did not work out anyway (due to complexity of local_theory sandwich);
 wenzelm parents: 
57858diff
changeset | 123 | fun map_value f context = | 
| 56438 | 124 | Value.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 | 125 | in | 
| 
8b7caf447357
removed obsolete global-only options, which did not work out anyway (due to complexity of local_theory sandwich);
 wenzelm parents: 
57858diff
changeset | 126 |     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 | 127 | end; | 
| 24114 | 128 | |
| 58951 
8b7caf447357
removed obsolete global-only options, which did not work out anyway (due to complexity of local_theory sandwich);
 wenzelm parents: 
57858diff
changeset | 129 | fun declare_option (name, pos) = | 
| 51947 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 130 | let | 
| 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 131 | val typ = Options.default_typ name; | 
| 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 132 | val default = | 
| 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 133 | 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 | 134 | 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 | 135 | 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 | 136 | else if typ = Options.stringT then fn _ => String (Options.default_string name) | 
| 56438 | 137 |       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 | 138 | in declare (name, pos) default end; | 
| 51947 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 139 | |
| 24125 | 140 | fun name_of (Config {name, ...}) = name;
 | 
| 56438 | 141 | fun pos_of (Config {pos, ...}) = pos;
 | 
| 24114 | 142 | |
| 143 | ||
| 144 | (*final declarations of this structure!*) | |
| 145 | val get = get_ctxt; | |
| 146 | val map = map_ctxt; | |
| 147 | val put = put_ctxt; | |
| 148 | ||
| 149 | end; |