1 (* Title: Pure/config.ML
4 Configuration options as values within the local context.
9 datatype value = Bool of bool | Int of int | String of string
10 val print_value: value -> string
11 val print_type: value -> string
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
25 val declare: bool -> string -> value -> value T
26 val name_of: 'a T -> string
29 structure Config: CONFIG =
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;
44 fun print_type (Bool _) = "boolean"
45 | print_type (Int _) = "integer"
46 | print_type (String _) = "string";
48 fun same_type (Bool _) (Bool _) = true
49 | same_type (Int _) (Int _) = true
50 | same_type (String _) (String _) = true
51 | same_type _ _ = false;
53 fun type_check name 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");
62 (* abstract configuration options *)
64 datatype 'a T = Config of
66 get_value: Context.generic -> 'a,
67 map_value: ('a -> 'a) -> Context.generic -> Context.generic};
69 fun coerce make dest (Config {name, get_value, map_value}) = Config
71 get_value = dest o get_value,
72 map_value = fn f => map_value (make o f o dest)};
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);
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);
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);
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);
91 (* context information *)
93 structure Value = GenericDataFun
95 type T = value Inttab.table;
96 val empty = Inttab.empty;
98 fun merge _ = Inttab.merge (K true);
101 fun declare global name default =
105 fun get_value context = the_default default (Inttab.lookup (Value.get context) id);
106 fun modify_value f = Value.map (Inttab.map_default (id, default) (type_check name f));
108 fun map_value f (context as Context.Proof _) =
109 let val context' = modify_value f context in
111 get_value (Context.Theory (Context.theory_of context')) <> get_value context'
112 then (warning ("Ignoring local change of global option " ^ quote name); context)
115 | map_value f context = modify_value f context;
116 in Config {name = name, get_value = get_value, map_value = map_value} end;
118 fun name_of (Config {name, ...}) = name;
121 (*final declarations of this structure!*)