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