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