src/Pure/config_option.ML
author wenzelm
Fri, 27 Jul 2007 21:55:17 +0200
changeset 24007 8f40e6fdb376
child 24028 22614d7b71bc
permissions -rw-r--r--
renamed config.ML to config_option.ML;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24007
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/config_option.ML
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
     4
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
     5
Configuration options as values within the local context.  Global
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
     6
environment of named options, with type declaration.
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
     7
*)
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
     8
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
     9
signature CONFIG_OPTION =
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    10
sig
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    11
  datatype value = Bool of bool | Int of int | String of string
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    12
  type 'a T
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    13
  val get: Proof.context -> 'a T -> 'a
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    14
  val get_thy: theory -> 'a T -> 'a
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    15
  val get_generic: Context.generic -> 'a T -> 'a
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    16
  val map: 'a T -> ('a -> 'a) -> Proof.context -> Proof.context
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    17
  val map_thy: 'a T -> ('a -> 'a) -> theory -> theory
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    18
  val map_generic: 'a T -> ('a -> 'a) -> Context.generic -> Context.generic
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    19
  val put: 'a T -> 'a -> Proof.context -> Proof.context
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    20
  val put_thy: 'a T -> 'a -> theory -> theory
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    21
  val put_generic: 'a T -> 'a -> Context.generic -> Context.generic
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    22
  val print_options: Proof.context -> unit
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    23
  val the_option: string -> value T * value
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    24
  val bool: string -> bool -> bool T
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    25
  val int: string -> int -> int T
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    26
  val string: string -> string -> string T
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    27
end;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    28
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    29
structure ConfigOption: CONFIG_OPTION =
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    30
struct
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    31
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    32
(* mixed values *)
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    33
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    34
datatype value =
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    35
  Bool of bool |
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    36
  Int of int |
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    37
  String of string;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    38
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    39
fun print_value (Bool true) = "true"
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    40
  | print_value (Bool false) = "false"
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    41
  | print_value (Int i) = signed_string_of_int i
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    42
  | print_value (String s) = quote s;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    43
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    44
fun print_type (Bool _) = "boolean"
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    45
  | print_type (Int _) = "integer"
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    46
  | print_type (String _) = "string";
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    47
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    48
fun same_type (Bool _) (Bool _) = true
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    49
  | same_type (Int _) (Int _) = true
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    50
  | same_type (String _) (String _) = true
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    51
  | same_type _ _ = false;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    52
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    53
fun type_check f value =
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    54
  let
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    55
    val value' = f value;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    56
    val _ = same_type value value' orelse
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    57
      error ("Ill-typed configuration option: " ^ print_type value ^
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    58
        " expected,\nbut " ^ print_type value' ^ " was found");
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    59
  in value' end;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    60
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    61
structure ConfigOptionData = GenericDataFun
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    62
(
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    63
  type T = value Inttab.table;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    64
  val empty = Inttab.empty;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    65
  val extend = I;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    66
  fun merge _ = Inttab.merge (K true);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    67
);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    68
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    69
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    70
(* abstract configuration options *)
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    71
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    72
datatype 'a T = ConfigOption of
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    73
 {get_value: Context.generic -> 'a,
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    74
  map_value: ('a -> 'a) -> Context.generic -> Context.generic};
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    75
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    76
fun get_generic context (ConfigOption {get_value, ...}) = get_value context;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    77
fun get_ctxt ctxt = get_generic (Context.Proof ctxt);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    78
fun get_thy thy = get_generic (Context.Theory thy);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    79
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    80
fun map_generic (ConfigOption {map_value, ...}) f context = map_value f context;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    81
fun map_ctxt config f = Context.proof_map (map_generic config f);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    82
fun map_thy config f = Context.theory_map (map_generic config f);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    83
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    84
fun put_generic config value = map_generic config (K value);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    85
fun put_ctxt config value = map_ctxt config (K value);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    86
fun put_thy config value = map_thy config (K value);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    87
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    88
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    89
(* global declarations *)
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    90
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    91
local val global_configs = ref (Symtab.empty: (value T * value) Symtab.table) in
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    92
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    93
fun print_options ctxt =
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    94
  let
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    95
    fun prt (name, (config, default)) =
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    96
      Pretty.block [Pretty.str (name ^ ": " ^ print_type default ^ " ="), Pretty.brk 1,
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    97
        Pretty.str (print_value (get_ctxt ctxt config))];
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    98
    val configs = sort_wrt #1 (Symtab.dest (! global_configs));
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    99
  in Pretty.writeln (Pretty.big_list "configuration options" (map prt configs)) end;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   100
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   101
fun the_option name =
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   102
  (case Symtab.lookup (! global_configs) name of SOME cfg => cfg
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   103
  | NONE => error ("Unknown configuration option " ^ quote name));
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   104
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   105
fun declare make dest name default =
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   106
  let
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   107
    val id = serial ();
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   108
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   109
    val default_value = make default;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   110
    fun get_value context =
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   111
      the_default default_value (Inttab.lookup (ConfigOptionData.get context) id);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   112
    fun map_value f = ConfigOptionData.map (Inttab.map_default (id, default_value) (type_check f));
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   113
    val config_value = ConfigOption {get_value = get_value, map_value = map_value};
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   114
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   115
    val _ = CRITICAL (fn () =>
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   116
     (if Symtab.defined (! global_configs) name
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   117
      then warning ("Hiding existing configuration option " ^ quote name) else ();
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   118
      change global_configs (Symtab.update (name, (config_value, default_value)))));
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   119
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   120
  in
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   121
    ConfigOption {get_value = dest o get_value, map_value = fn f => map_value (make o f o dest)}
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   122
  end;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   123
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   124
end;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   125
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   126
val bool = declare Bool (fn Bool b => b);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   127
val int = declare Int (fn Int i => i);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   128
val string = declare String (fn String s => s);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   129
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   130
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   131
(*final declarations of this structure!*)
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   132
val get = get_ctxt;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   133
val map = map_ctxt;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   134
val put = put_ctxt;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   135
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   136
end;