| author | huffman | 
| Tue, 04 May 2010 09:56:34 -0700 | |
| changeset 36659 | f794e92784aa | 
| parent 36002 | f4f343500249 | 
| child 36787 | f60e4dd6d76f | 
| 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 | |
| 9 | datatype value = Bool of bool | Int of int | String of string | |
| 10 | val print_value: value -> string | |
| 11 | val print_type: value -> string | |
| 12 | type 'a T | |
| 13 | val bool: value T -> bool T | |
| 14 | val int: value T -> int T | |
| 15 | val string: value T -> string T | |
| 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 | |
| 19 | val get_thy: theory -> 'a T -> 'a | |
| 20 |   val map_thy: 'a T -> ('a -> 'a) -> theory -> theory
 | |
| 21 | val put_thy: 'a T -> 'a -> theory -> theory | |
| 22 | val get_generic: Context.generic -> 'a T -> 'a | |
| 23 |   val map_generic: 'a T -> ('a -> 'a) -> Context.generic -> Context.generic
 | |
| 24 | val put_generic: 'a T -> 'a -> Context.generic -> Context.generic | |
| 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 | 25 | val declare: bool -> string -> (Context.generic -> value) -> value T | 
| 24125 | 26 | val name_of: 'a T -> string | 
| 24114 | 27 | end; | 
| 28 | ||
| 29 | structure Config: CONFIG = | |
| 30 | struct | |
| 31 | ||
| 32 | (* simple values *) | |
| 33 | ||
| 34 | datatype value = | |
| 35 | Bool of bool | | |
| 36 | Int of int | | |
| 37 | String of string; | |
| 38 | ||
| 39 | fun print_value (Bool true) = "true" | |
| 40 | | print_value (Bool false) = "false" | |
| 41 | | print_value (Int i) = signed_string_of_int i | |
| 42 | | print_value (String s) = quote s; | |
| 43 | ||
| 44 | fun print_type (Bool _) = "boolean" | |
| 45 | | print_type (Int _) = "integer" | |
| 46 | | print_type (String _) = "string"; | |
| 47 | ||
| 48 | fun same_type (Bool _) (Bool _) = true | |
| 49 | | same_type (Int _) (Int _) = true | |
| 50 | | same_type (String _) (String _) = true | |
| 51 | | same_type _ _ = false; | |
| 52 | ||
| 53 | fun type_check name f value = | |
| 54 | let | |
| 55 | val value' = f value; | |
| 56 | val _ = same_type value value' orelse | |
| 57 |       error ("Ill-typed configuration option " ^ quote name ^ ": " ^
 | |
| 58 | print_type value ^ " expected,\nbut " ^ print_type value' ^ " was found"); | |
| 59 | in value' end; | |
| 60 | ||
| 61 | ||
| 62 | (* abstract configuration options *) | |
| 63 | ||
| 64 | datatype 'a T = Config of | |
| 24125 | 65 |  {name: string,
 | 
| 66 | get_value: Context.generic -> 'a, | |
| 24114 | 67 |   map_value: ('a -> 'a) -> Context.generic -> Context.generic};
 | 
| 68 | ||
| 24125 | 69 | fun coerce make dest (Config {name, get_value, map_value}) = Config
 | 
| 70 |  {name = name,
 | |
| 71 | get_value = dest o get_value, | |
| 72 | map_value = fn f => map_value (make o f o dest)}; | |
| 24114 | 73 | |
| 74 | val bool = coerce Bool (fn Bool b => b); | |
| 75 | val int = coerce Int (fn Int i => i); | |
| 76 | val string = coerce String (fn String s => s); | |
| 77 | ||
| 78 | fun get_generic context (Config {get_value, ...}) = get_value context;
 | |
| 79 | fun map_generic (Config {map_value, ...}) f context = map_value f context;
 | |
| 80 | fun put_generic config value = map_generic config (K value); | |
| 81 | ||
| 82 | fun get_ctxt ctxt = get_generic (Context.Proof ctxt); | |
| 83 | fun map_ctxt config f = Context.proof_map (map_generic config f); | |
| 84 | fun put_ctxt config value = map_ctxt config (K value); | |
| 85 | ||
| 86 | fun get_thy thy = get_generic (Context.Theory thy); | |
| 87 | fun map_thy config f = Context.theory_map (map_generic config f); | |
| 88 | fun put_thy config value = map_thy config (K value); | |
| 89 | ||
| 90 | ||
| 91 | (* context information *) | |
| 92 | ||
| 33519 | 93 | structure Value = Generic_Data | 
| 24114 | 94 | ( | 
| 95 | type T = value Inttab.table; | |
| 96 | val empty = Inttab.empty; | |
| 97 | val extend = I; | |
| 33519 | 98 | fun merge data = Inttab.merge (K true) data; | 
| 24114 | 99 | ); | 
| 100 | ||
| 101 | fun declare global name default = | |
| 102 | let | |
| 103 | val id = serial (); | |
| 104 | ||
| 36000 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 105 | fun get_value context = | 
| 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 106 | (case Inttab.lookup (Value.get context) id of | 
| 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 107 | 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 | 108 | | NONE => default context); | 
| 36000 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 109 | |
| 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 110 | fun update_value f context = | 
| 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 111 | Value.map (Inttab.update (id, type_check name f (get_value context))) context; | 
| 24114 | 112 | |
| 113 | fun map_value f (context as Context.Proof _) = | |
| 36000 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 114 | let val context' = update_value f context in | 
| 24114 | 115 | if global andalso | 
| 116 | get_value (Context.Theory (Context.theory_of context')) <> get_value context' | |
| 117 |             then (warning ("Ignoring local change of global option " ^ quote name); context)
 | |
| 118 | else context' | |
| 119 | end | |
| 36000 
5560b2437789
configuration options admit dynamic default values;
 wenzelm parents: 
33519diff
changeset | 120 | | map_value f context = update_value f context; | 
| 24125 | 121 |   in Config {name = name, get_value = get_value, map_value = map_value} end;
 | 
| 122 | ||
| 123 | fun name_of (Config {name, ...}) = name;
 | |
| 24114 | 124 | |
| 125 | ||
| 126 | (*final declarations of this structure!*) | |
| 127 | val get = get_ctxt; | |
| 128 | val map = map_ctxt; | |
| 129 | val put = put_ctxt; | |
| 130 | ||
| 131 | end; |