src/Pure/config_option.ML
author wenzelm
Sun, 29 Jul 2007 17:28:55 +0200
changeset 24058 81aafd465662
parent 24028 22614d7b71bc
child 24077 e7ba448bc571
permissions -rw-r--r--
NAMED_CRITICAL;
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
24028
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    23
  val the_option: theory -> xstring -> value T * value
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    24
  val bool: string -> bool -> bool T * (theory -> theory)
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    25
  val int: string -> int -> int T * (theory -> theory)
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    26
  val string: string -> string -> string T * (theory -> theory)
24007
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
24028
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    32
(* simple values *)
24007
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
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    62
(* abstract configuration options *)
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    63
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    64
datatype 'a T = ConfigOption of
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    65
 {get_value: Context.generic -> 'a,
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    66
  map_value: ('a -> 'a) -> Context.generic -> Context.generic};
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
fun get_generic context (ConfigOption {get_value, ...}) = get_value context;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    69
fun get_ctxt ctxt = get_generic (Context.Proof ctxt);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    70
fun get_thy thy = get_generic (Context.Theory thy);
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
fun map_generic (ConfigOption {map_value, ...}) f context = map_value f context;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    73
fun map_ctxt config f = Context.proof_map (map_generic config f);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    74
fun map_thy config f = Context.theory_map (map_generic config f);
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 put_generic config value = map_generic config (K value);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    77
fun put_ctxt config value = map_ctxt config (K value);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    78
fun put_thy config value = map_thy config (K value);
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
24028
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    81
(* context information *)
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    82
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    83
fun err_dup_config name =
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    84
  error ("Duplicate declaration of configuration option " ^ quote name);
24007
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
    85
24028
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    86
(*global declarations: name, default value and type*)
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    87
structure Declaration = TheoryDataFun
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    88
(struct
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    89
  type T = ((value T * value) * serial) NameSpace.table;
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    90
  val empty = NameSpace.empty_table;
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    91
  val copy = I;
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    92
  val extend = I;
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    93
  fun merge _ tabs = NameSpace.merge_tables (eq_snd (op =)) tabs
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    94
    handle Symtab.DUP dup => err_dup_config dup;
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    95
end);
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    96
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    97
(*local values*)
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    98
structure Value = GenericDataFun
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
    99
(
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   100
  type T = value Inttab.table;
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   101
  val empty = Inttab.empty;
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   102
  val extend = I;
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   103
  fun merge _ = Inttab.merge (K true);
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   104
);
24007
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   105
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   106
fun print_options ctxt =
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   107
  let
24028
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   108
    val thy = ProofContext.theory_of ctxt;
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   109
    fun prt (name, ((config, default), _)) =
24007
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   110
      Pretty.block [Pretty.str (name ^ ": " ^ print_type default ^ " ="), Pretty.brk 1,
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   111
        Pretty.str (print_value (get_ctxt ctxt config))];
24028
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   112
    val configs = NameSpace.extern_table (Declaration.get thy);
24007
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   113
  in Pretty.writeln (Pretty.big_list "configuration options" (map prt configs)) end;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   114
24028
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   115
fun the_option thy xname =
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   116
  let val (space, tab) = Declaration.get thy in
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   117
    (case Symtab.lookup tab (NameSpace.intern space xname) of
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   118
      SOME (config, _) => config
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   119
    | NONE => error ("Unknown configuration option " ^ quote xname))
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   120
  end;
24007
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   121
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   122
fun declare make dest name default =
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   123
  let
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   124
    val id = serial ();
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 default_value = make default;
24028
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   127
    fun get_value context = the_default default_value (Inttab.lookup (Value.get context) id);
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   128
    fun map_value f = Value.map (Inttab.map_default (id, default_value) (type_check f));
24007
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   129
    val config_value = ConfigOption {get_value = get_value, map_value = map_value};
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   130
24028
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   131
    val config =
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   132
      ConfigOption {get_value = dest o get_value, map_value = fn f => map_value (make o f o dest)};
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   133
    fun setup thy = thy |> Declaration.map (fn tab =>
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   134
      NameSpace.extend_table (Sign.naming_of thy) [(name, ((config_value, default_value), id))] tab
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   135
        handle Symtab.DUP dup => err_dup_config dup);
22614d7b71bc declaration: proper naming within the theory;
wenzelm
parents: 24007
diff changeset
   136
  in (config, setup) end;
24007
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   137
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   138
val bool = declare Bool (fn Bool b => b);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   139
val int = declare Int (fn Int i => i);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   140
val string = declare String (fn String s => s);
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   141
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   142
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   143
(*final declarations of this structure!*)
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   144
val get = get_ctxt;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   145
val map = map_ctxt;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   146
val put = put_ctxt;
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   147
8f40e6fdb376 renamed config.ML to config_option.ML;
wenzelm
parents:
diff changeset
   148
end;