src/Pure/config.ML
author wenzelm
Fri Sep 03 16:09:12 2010 +0200 (2010-09-03)
changeset 39116 f14735a88886
parent 38804 99cc7e748ab4
child 39163 4d701c0388c3
permissions -rw-r--r--
more explicit Config.declare vs. Config.declare_global;
wenzelm@24114
     1
(*  Title:      Pure/config.ML
wenzelm@24114
     2
    Author:     Makarius
wenzelm@24114
     3
wenzelm@24114
     4
Configuration options as values within the local context.
wenzelm@24114
     5
*)
wenzelm@24114
     6
wenzelm@24114
     7
signature CONFIG =
wenzelm@24114
     8
sig
wenzelm@24114
     9
  datatype value = Bool of bool | Int of int | String of string
wenzelm@24114
    10
  val print_value: value -> string
wenzelm@24114
    11
  val print_type: value -> string
wenzelm@24114
    12
  type 'a T
wenzelm@24114
    13
  val bool: value T -> bool T
wenzelm@24114
    14
  val int: value T -> int T
wenzelm@24114
    15
  val string: value T -> string T
wenzelm@24114
    16
  val get: Proof.context -> 'a T -> 'a
wenzelm@24114
    17
  val map: 'a T -> ('a -> 'a) -> Proof.context -> Proof.context
wenzelm@24114
    18
  val put: 'a T -> 'a -> Proof.context -> Proof.context
wenzelm@36787
    19
  val get_global: theory -> 'a T -> 'a
wenzelm@36787
    20
  val map_global: 'a T -> ('a -> 'a) -> theory -> theory
wenzelm@36787
    21
  val put_global: 'a T -> 'a -> theory -> theory
wenzelm@24114
    22
  val get_generic: Context.generic -> 'a T -> 'a
wenzelm@24114
    23
  val map_generic: 'a T -> ('a -> 'a) -> Context.generic -> Context.generic
wenzelm@24114
    24
  val put_generic: 'a T -> 'a -> Context.generic -> Context.generic
wenzelm@39116
    25
  val declare_generic: {global: bool} -> string -> (Context.generic -> value) -> value T
wenzelm@39116
    26
  val declare_global: string -> (Context.generic -> value) -> value T
wenzelm@39116
    27
  val declare: string -> (Context.generic -> value) -> value T
wenzelm@24125
    28
  val name_of: 'a T -> string
wenzelm@24114
    29
end;
wenzelm@24114
    30
wenzelm@24114
    31
structure Config: CONFIG =
wenzelm@24114
    32
struct
wenzelm@24114
    33
wenzelm@24114
    34
(* simple values *)
wenzelm@24114
    35
wenzelm@24114
    36
datatype value =
wenzelm@24114
    37
  Bool of bool |
wenzelm@24114
    38
  Int of int |
wenzelm@24114
    39
  String of string;
wenzelm@24114
    40
wenzelm@24114
    41
fun print_value (Bool true) = "true"
wenzelm@24114
    42
  | print_value (Bool false) = "false"
wenzelm@24114
    43
  | print_value (Int i) = signed_string_of_int i
wenzelm@24114
    44
  | print_value (String s) = quote s;
wenzelm@24114
    45
wenzelm@38804
    46
fun print_type (Bool _) = "bool"
wenzelm@38804
    47
  | print_type (Int _) = "int"
wenzelm@24114
    48
  | print_type (String _) = "string";
wenzelm@24114
    49
wenzelm@24114
    50
fun same_type (Bool _) (Bool _) = true
wenzelm@24114
    51
  | same_type (Int _) (Int _) = true
wenzelm@24114
    52
  | same_type (String _) (String _) = true
wenzelm@24114
    53
  | same_type _ _ = false;
wenzelm@24114
    54
wenzelm@24114
    55
fun type_check name f value =
wenzelm@24114
    56
  let
wenzelm@24114
    57
    val value' = f value;
wenzelm@24114
    58
    val _ = same_type value value' orelse
wenzelm@24114
    59
      error ("Ill-typed configuration option " ^ quote name ^ ": " ^
wenzelm@24114
    60
        print_type value ^ " expected,\nbut " ^ print_type value' ^ " was found");
wenzelm@24114
    61
  in value' end;
wenzelm@24114
    62
wenzelm@24114
    63
wenzelm@24114
    64
(* abstract configuration options *)
wenzelm@24114
    65
wenzelm@24114
    66
datatype 'a T = Config of
wenzelm@24125
    67
 {name: string,
wenzelm@24125
    68
  get_value: Context.generic -> 'a,
wenzelm@24114
    69
  map_value: ('a -> 'a) -> Context.generic -> Context.generic};
wenzelm@24114
    70
wenzelm@24125
    71
fun coerce make dest (Config {name, get_value, map_value}) = Config
wenzelm@24125
    72
 {name = name,
wenzelm@24125
    73
  get_value = dest o get_value,
wenzelm@24125
    74
  map_value = fn f => map_value (make o f o dest)};
wenzelm@24114
    75
wenzelm@24114
    76
val bool = coerce Bool (fn Bool b => b);
wenzelm@24114
    77
val int = coerce Int (fn Int i => i);
wenzelm@24114
    78
val string = coerce String (fn String s => s);
wenzelm@24114
    79
wenzelm@24114
    80
fun get_generic context (Config {get_value, ...}) = get_value context;
wenzelm@24114
    81
fun map_generic (Config {map_value, ...}) f context = map_value f context;
wenzelm@24114
    82
fun put_generic config value = map_generic config (K value);
wenzelm@24114
    83
wenzelm@24114
    84
fun get_ctxt ctxt = get_generic (Context.Proof ctxt);
wenzelm@24114
    85
fun map_ctxt config f = Context.proof_map (map_generic config f);
wenzelm@24114
    86
fun put_ctxt config value = map_ctxt config (K value);
wenzelm@24114
    87
wenzelm@36787
    88
fun get_global thy = get_generic (Context.Theory thy);
wenzelm@36787
    89
fun map_global config f = Context.theory_map (map_generic config f);
wenzelm@36787
    90
fun put_global config value = map_global config (K value);
wenzelm@24114
    91
wenzelm@24114
    92
wenzelm@24114
    93
(* context information *)
wenzelm@24114
    94
wenzelm@33519
    95
structure Value = Generic_Data
wenzelm@24114
    96
(
wenzelm@24114
    97
  type T = value Inttab.table;
wenzelm@24114
    98
  val empty = Inttab.empty;
wenzelm@24114
    99
  val extend = I;
wenzelm@33519
   100
  fun merge data = Inttab.merge (K true) data;
wenzelm@24114
   101
);
wenzelm@24114
   102
wenzelm@39116
   103
fun declare_generic {global} name default =
wenzelm@24114
   104
  let
wenzelm@24114
   105
    val id = serial ();
wenzelm@24114
   106
wenzelm@36000
   107
    fun get_value context =
wenzelm@36000
   108
      (case Inttab.lookup (Value.get context) id of
wenzelm@36000
   109
        SOME value => value
wenzelm@36002
   110
      | NONE => default context);
wenzelm@36000
   111
wenzelm@36000
   112
    fun update_value f context =
wenzelm@36000
   113
      Value.map (Inttab.update (id, type_check name f (get_value context))) context;
wenzelm@24114
   114
wenzelm@24114
   115
    fun map_value f (context as Context.Proof _) =
wenzelm@36000
   116
          let val context' = update_value f context in
wenzelm@24114
   117
            if global andalso
wenzelm@24114
   118
              get_value (Context.Theory (Context.theory_of context')) <> get_value context'
wenzelm@24114
   119
            then (warning ("Ignoring local change of global option " ^ quote name); context)
wenzelm@24114
   120
            else context'
wenzelm@24114
   121
          end
wenzelm@36000
   122
      | map_value f context = update_value f context;
wenzelm@24125
   123
  in Config {name = name, get_value = get_value, map_value = map_value} end;
wenzelm@24125
   124
wenzelm@39116
   125
val declare_global = declare_generic {global = true};
wenzelm@39116
   126
val declare = declare_generic {global = false};
wenzelm@39116
   127
wenzelm@24125
   128
fun name_of (Config {name, ...}) = name;
wenzelm@24114
   129
wenzelm@24114
   130
wenzelm@24114
   131
(*final declarations of this structure!*)
wenzelm@24114
   132
val get = get_ctxt;
wenzelm@24114
   133
val map = map_ctxt;
wenzelm@24114
   134
val put = put_ctxt;
wenzelm@24114
   135
wenzelm@24114
   136
end;