| author | wenzelm | 
| Tue, 05 Aug 2014 12:01:32 +0200 | |
| changeset 57859 | 29e728588163 | 
| parent 57858 | 39d9c7f175e0 | 
| child 58951 | 8b7caf447357 | 
| 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_global: string * Position.T -> (Context.generic -> value) -> raw | |
| 29 | val declare_option: string * Position.T -> raw | |
| 30 | val declare_option_global: string * Position.T -> raw | |
| 24125 | 31 | val name_of: 'a T -> string | 
| 56438 | 32 | val pos_of: 'a T -> Position.T | 
| 24114 | 33 | end; | 
| 34 | ||
| 35 | structure Config: CONFIG = | |
| 36 | struct | |
| 37 | ||
| 38 | (* simple values *) | |
| 39 | ||
| 40 | datatype value = | |
| 41 | Bool of bool | | |
| 42 | Int of int | | |
| 40291 | 43 | Real of real | | 
| 24114 | 44 | String of string; | 
| 45 | ||
| 46 | fun print_value (Bool true) = "true" | |
| 47 | | print_value (Bool false) = "false" | |
| 48 | | print_value (Int i) = signed_string_of_int i | |
| 51990 | 49 | | print_value (Real x) = Markup.print_real x | 
| 24114 | 50 | | print_value (String s) = quote s; | 
| 51 | ||
| 38804 | 52 | fun print_type (Bool _) = "bool" | 
| 53 | | print_type (Int _) = "int" | |
| 40291 | 54 | | print_type (Real _) = "real" | 
| 24114 | 55 | | print_type (String _) = "string"; | 
| 56 | ||
| 57 | fun same_type (Bool _) (Bool _) = true | |
| 58 | | same_type (Int _) (Int _) = true | |
| 40291 | 59 | | same_type (Real _) (Real _) = true | 
| 24114 | 60 | | same_type (String _) (String _) = true | 
| 61 | | same_type _ _ = false; | |
| 62 | ||
| 56438 | 63 | fun type_check (name, pos) f value = | 
| 24114 | 64 | let | 
| 65 | val value' = f value; | |
| 66 | val _ = same_type value value' orelse | |
| 56438 | 67 |       error ("Ill-typed configuration option " ^ quote name ^ Position.here pos ^ ": " ^
 | 
| 24114 | 68 | print_type value ^ " expected,\nbut " ^ print_type value' ^ " was found"); | 
| 69 | in value' end; | |
| 70 | ||
| 71 | ||
| 72 | (* abstract configuration options *) | |
| 73 | ||
| 74 | datatype 'a T = Config of | |
| 24125 | 75 |  {name: string,
 | 
| 56438 | 76 | pos: Position.T, | 
| 24125 | 77 | get_value: Context.generic -> 'a, | 
| 24114 | 78 |   map_value: ('a -> 'a) -> Context.generic -> Context.generic};
 | 
| 79 | ||
| 39163 
4d701c0388c3
more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
 wenzelm parents: 
39116diff
changeset | 80 | type raw = value T; | 
| 
4d701c0388c3
more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
 wenzelm parents: 
39116diff
changeset | 81 | |
| 56438 | 82 | fun coerce make dest (Config {name, pos, get_value, map_value}) = Config
 | 
| 24125 | 83 |  {name = name,
 | 
| 56438 | 84 | pos = pos, | 
| 24125 | 85 | get_value = dest o get_value, | 
| 86 | map_value = fn f => map_value (make o f o dest)}; | |
| 24114 | 87 | |
| 88 | val bool = coerce Bool (fn Bool b => b); | |
| 89 | val int = coerce Int (fn Int i => i); | |
| 40291 | 90 | val real = coerce Real (fn Real x => x); | 
| 24114 | 91 | val string = coerce String (fn String s => s); | 
| 92 | ||
| 93 | fun get_generic context (Config {get_value, ...}) = get_value context;
 | |
| 94 | fun map_generic (Config {map_value, ...}) f context = map_value f context;
 | |
| 95 | fun put_generic config value = map_generic config (K value); | |
| 96 | ||
| 97 | fun get_ctxt ctxt = get_generic (Context.Proof ctxt); | |
| 98 | fun map_ctxt config f = Context.proof_map (map_generic config f); | |
| 99 | fun put_ctxt config value = map_ctxt config (K value); | |
| 100 | ||
| 36787 
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 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 | 102 | 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 | 103 | fun put_global config value = map_global config (K value); | 
| 24114 | 104 | |
| 105 | ||
| 106 | (* context information *) | |
| 107 | ||
| 33519 | 108 | structure Value = Generic_Data | 
| 24114 | 109 | ( | 
| 110 | type T = value Inttab.table; | |
| 111 | val empty = Inttab.empty; | |
| 112 | val extend = I; | |
| 33519 | 113 | fun merge data = Inttab.merge (K true) data; | 
| 24114 | 114 | ); | 
| 115 | ||
| 52469 | 116 | local | 
| 117 | ||
| 56438 | 118 | fun declare_generic global (name, pos) default = | 
| 24114 | 119 | let | 
| 120 | val id = serial (); | |
| 121 | ||
| 36000 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 122 | fun get_value context = | 
| 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 123 | (case Inttab.lookup (Value.get context) id of | 
| 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 124 | 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 | 125 | | NONE => default context); | 
| 36000 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 126 | |
| 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 127 | fun update_value f context = | 
| 56438 | 128 | Value.map (Inttab.update (id, type_check (name, pos) f (get_value context))) context; | 
| 24114 | 129 | |
| 47814 
53668571d300
avoid spurious warning in invisible context, notably Haftmann-Wenzel sandwich;
 wenzelm parents: 
40291diff
changeset | 130 | fun map_value f (context as Context.Proof ctxt) = | 
| 36000 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 131 | let val context' = update_value f context in | 
| 24114 | 132 | if global andalso | 
| 57858 
39d9c7f175e0
refined context visibility again (amending f5f9fad3321c, 8e3e004f1c31): avoid spurious warning due to global config options;
 wenzelm parents: 
56438diff
changeset | 133 | Context_Position.is_really_visible ctxt andalso | 
| 40291 | 134 | print_value (get_value (Context.Theory (Context.theory_of context'))) <> | 
| 135 | print_value (get_value context') | |
| 57858 
39d9c7f175e0
refined context visibility again (amending f5f9fad3321c, 8e3e004f1c31): avoid spurious warning due to global config options;
 wenzelm parents: 
56438diff
changeset | 136 |             then (warning ("Ignoring local change of global option " ^ quote name); context)
 | 
| 24114 | 137 | else context' | 
| 138 | end | |
| 36000 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 139 | | map_value f context = update_value f context; | 
| 56438 | 140 |   in Config {name = name, pos = pos, get_value = get_value, map_value = map_value} end;
 | 
| 24125 | 141 | |
| 56438 | 142 | fun declare_option_generic global (name, pos) = | 
| 51947 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 143 | let | 
| 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 144 | val typ = Options.default_typ name; | 
| 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 145 | val default = | 
| 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 146 | 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 | 147 | 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 | 148 | 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 | 149 | else if typ = Options.stringT then fn _ => String (Options.default_string name) | 
| 56438 | 150 |       else error ("Unknown type for option " ^ quote name ^ Position.here pos ^ " : " ^ quote typ);
 | 
| 151 | in declare_generic global (name, pos) default end; | |
| 52469 | 152 | |
| 153 | in | |
| 154 | ||
| 155 | val declare = declare_generic false; | |
| 156 | val declare_global = declare_generic true; | |
| 157 | val declare_option = declare_option_generic false; | |
| 158 | val declare_option_global = declare_option_generic true; | |
| 159 | ||
| 160 | end; | |
| 51947 
3301612c4893
support for system options as context-sensitive config options;
 wenzelm parents: 
47814diff
changeset | 161 | |
| 24125 | 162 | fun name_of (Config {name, ...}) = name;
 | 
| 56438 | 163 | fun pos_of (Config {pos, ...}) = pos;
 | 
| 24114 | 164 | |
| 165 | ||
| 166 | (*final declarations of this structure!*) | |
| 167 | val get = get_ctxt; | |
| 168 | val map = map_ctxt; | |
| 169 | val put = put_ctxt; | |
| 170 | ||
| 171 | end; |