src/Pure/config.ML
author wenzelm
Wed, 21 Jan 2009 23:21:44 +0100
changeset 29606 fedb8be05f24
parent 24125 454a0c895735
child 33519 e31a85f92ce9
permissions -rw-r--r--
removed Ids;
     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_thy: theory -> 'a T -> 'a
    20   val map_thy: 'a T -> ('a -> 'a) -> theory -> theory
    21   val put_thy: '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: bool -> string -> value -> value T
    26   val name_of: 'a T -> string
    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  {name: string,
    66   get_value: Context.generic -> 'a,
    67   map_value: ('a -> 'a) -> Context.generic -> Context.generic};
    68 
    69 fun coerce make dest (Config {name, get_value, map_value}) = Config
    70  {name = name,
    71   get_value = dest o get_value,
    72   map_value = fn f => map_value (make o f o dest)};
    73 
    74 val bool = coerce Bool (fn Bool b => b);
    75 val int = coerce Int (fn Int i => i);
    76 val string = coerce String (fn String s => s);
    77 
    78 fun get_generic context (Config {get_value, ...}) = get_value context;
    79 fun map_generic (Config {map_value, ...}) f context = map_value f context;
    80 fun put_generic config value = map_generic config (K value);
    81 
    82 fun get_ctxt ctxt = get_generic (Context.Proof ctxt);
    83 fun map_ctxt config f = Context.proof_map (map_generic config f);
    84 fun put_ctxt config value = map_ctxt config (K value);
    85 
    86 fun get_thy thy = get_generic (Context.Theory thy);
    87 fun map_thy config f = Context.theory_map (map_generic config f);
    88 fun put_thy config value = map_thy config (K value);
    89 
    90 
    91 (* context information *)
    92 
    93 structure Value = GenericDataFun
    94 (
    95   type T = value Inttab.table;
    96   val empty = Inttab.empty;
    97   val extend = I;
    98   fun merge _ = Inttab.merge (K true);
    99 );
   100 
   101 fun declare global name default =
   102   let
   103     val id = serial ();
   104 
   105     fun get_value context = the_default default (Inttab.lookup (Value.get context) id);
   106     fun modify_value f = Value.map (Inttab.map_default (id, default) (type_check name f));
   107 
   108     fun map_value f (context as Context.Proof _) =
   109           let val context' = modify_value f context in
   110             if global andalso
   111               get_value (Context.Theory (Context.theory_of context')) <> get_value context'
   112             then (warning ("Ignoring local change of global option " ^ quote name); context)
   113             else context'
   114           end
   115       | map_value f context = modify_value f context;
   116   in Config {name = name, get_value = get_value, map_value = map_value} end;
   117 
   118 fun name_of (Config {name, ...}) = name;
   119 
   120 
   121 (*final declarations of this structure!*)
   122 val get = get_ctxt;
   123 val map = map_ctxt;
   124 val put = put_ctxt;
   125 
   126 end;