src/Pure/config.ML
author wenzelm
Wed Aug 01 16:55:42 2007 +0200 (2007-08-01)
changeset 24114 1b0bc10019a5
child 24125 454a0c895735
permissions -rw-r--r--
renamed config_option.ML to config.ML;
moved attrib setup to attrib.ML;
     1 (*  Title:      Pure/config.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Configuration options as values within the local context.
     6 *)
     7 
     8 signature CONFIG =
     9 sig
    10   datatype value = Bool of bool | Int of int | String of string
    11   val print_value: value -> string
    12   val print_type: value -> string
    13   type 'a T
    14   val bool: value T -> bool T
    15   val int: value T -> int T
    16   val string: value T -> string T
    17   val get: Proof.context -> 'a T -> 'a
    18   val map: 'a T -> ('a -> 'a) -> Proof.context -> Proof.context
    19   val put: 'a T -> 'a -> Proof.context -> Proof.context
    20   val get_thy: theory -> 'a T -> 'a
    21   val map_thy: 'a T -> ('a -> 'a) -> theory -> theory
    22   val put_thy: 'a T -> 'a -> theory -> theory
    23   val get_generic: Context.generic -> 'a T -> 'a
    24   val map_generic: 'a T -> ('a -> 'a) -> Context.generic -> Context.generic
    25   val put_generic: 'a T -> 'a -> Context.generic -> Context.generic
    26   val declare: bool -> string -> value -> value T
    27 end;
    28 
    29 structure Config: CONFIG =
    30 struct
    31 
    32 (* simple values *)
    33 
    34 datatype value =
    35   Bool of bool |
    36   Int of int |
    37   String of string;
    38 
    39 fun print_value (Bool true) = "true"
    40   | print_value (Bool false) = "false"
    41   | print_value (Int i) = signed_string_of_int i
    42   | print_value (String s) = quote s;
    43 
    44 fun print_type (Bool _) = "boolean"
    45   | print_type (Int _) = "integer"
    46   | print_type (String _) = "string";
    47 
    48 fun same_type (Bool _) (Bool _) = true
    49   | same_type (Int _) (Int _) = true
    50   | same_type (String _) (String _) = true
    51   | same_type _ _ = false;
    52 
    53 fun type_check name f value =
    54   let
    55     val value' = f value;
    56     val _ = same_type value value' orelse
    57       error ("Ill-typed configuration option " ^ quote name ^ ": " ^
    58         print_type value ^ " expected,\nbut " ^ print_type value' ^ " was found");
    59   in value' end;
    60 
    61 
    62 (* abstract configuration options *)
    63 
    64 datatype 'a T = Config of
    65  {get_value: Context.generic -> 'a,
    66   map_value: ('a -> 'a) -> Context.generic -> Context.generic};
    67 
    68 fun coerce make dest (Config {get_value, map_value}) =
    69   Config {get_value = dest o get_value, map_value = fn f => map_value (make o f o dest)};
    70 
    71 val bool = coerce Bool (fn Bool b => b);
    72 val int = coerce Int (fn Int i => i);
    73 val string = coerce String (fn String s => s);
    74 
    75 fun get_generic context (Config {get_value, ...}) = get_value context;
    76 fun map_generic (Config {map_value, ...}) f context = map_value f context;
    77 fun put_generic config value = map_generic config (K value);
    78 
    79 fun get_ctxt ctxt = get_generic (Context.Proof ctxt);
    80 fun map_ctxt config f = Context.proof_map (map_generic config f);
    81 fun put_ctxt config value = map_ctxt config (K value);
    82 
    83 fun get_thy thy = get_generic (Context.Theory thy);
    84 fun map_thy config f = Context.theory_map (map_generic config f);
    85 fun put_thy config value = map_thy config (K value);
    86 
    87 
    88 (* context information *)
    89 
    90 structure Value = GenericDataFun
    91 (
    92   type T = value Inttab.table;
    93   val empty = Inttab.empty;
    94   val extend = I;
    95   fun merge _ = Inttab.merge (K true);
    96 );
    97 
    98 fun declare global name default =
    99   let
   100     val id = serial ();
   101 
   102     fun get_value context = the_default default (Inttab.lookup (Value.get context) id);
   103     fun modify_value f = Value.map (Inttab.map_default (id, default) (type_check name f));
   104 
   105     fun map_value f (context as Context.Proof _) =
   106           let val context' = modify_value f context in
   107             if global andalso
   108               get_value (Context.Theory (Context.theory_of context')) <> get_value context'
   109             then (warning ("Ignoring local change of global option " ^ quote name); context)
   110             else context'
   111           end
   112       | map_value f context = modify_value f context;
   113   in Config {get_value = get_value, map_value = map_value} end;
   114 
   115 
   116 (*final declarations of this structure!*)
   117 val get = get_ctxt;
   118 val map = map_ctxt;
   119 val put = put_ctxt;
   120 
   121 end;