renamed config_option.ML to config.ML;
authorwenzelm
Wed Aug 01 16:55:42 2007 +0200 (2007-08-01)
changeset 241141b0bc10019a5
parent 24113 ec9e75a46e16
child 24115 39b407fd6e82
renamed config_option.ML to config.ML;
moved attrib setup to attrib.ML;
src/Pure/config.ML
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/config.ML	Wed Aug 01 16:55:42 2007 +0200
     1.3 @@ -0,0 +1,121 @@
     1.4 +(*  Title:      Pure/config.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Makarius
     1.7 +
     1.8 +Configuration options as values within the local context.
     1.9 +*)
    1.10 +
    1.11 +signature CONFIG =
    1.12 +sig
    1.13 +  datatype value = Bool of bool | Int of int | String of string
    1.14 +  val print_value: value -> string
    1.15 +  val print_type: value -> string
    1.16 +  type 'a T
    1.17 +  val bool: value T -> bool T
    1.18 +  val int: value T -> int T
    1.19 +  val string: value T -> string T
    1.20 +  val get: Proof.context -> 'a T -> 'a
    1.21 +  val map: 'a T -> ('a -> 'a) -> Proof.context -> Proof.context
    1.22 +  val put: 'a T -> 'a -> Proof.context -> Proof.context
    1.23 +  val get_thy: theory -> 'a T -> 'a
    1.24 +  val map_thy: 'a T -> ('a -> 'a) -> theory -> theory
    1.25 +  val put_thy: 'a T -> 'a -> theory -> theory
    1.26 +  val get_generic: Context.generic -> 'a T -> 'a
    1.27 +  val map_generic: 'a T -> ('a -> 'a) -> Context.generic -> Context.generic
    1.28 +  val put_generic: 'a T -> 'a -> Context.generic -> Context.generic
    1.29 +  val declare: bool -> string -> value -> value T
    1.30 +end;
    1.31 +
    1.32 +structure Config: CONFIG =
    1.33 +struct
    1.34 +
    1.35 +(* simple values *)
    1.36 +
    1.37 +datatype value =
    1.38 +  Bool of bool |
    1.39 +  Int of int |
    1.40 +  String of string;
    1.41 +
    1.42 +fun print_value (Bool true) = "true"
    1.43 +  | print_value (Bool false) = "false"
    1.44 +  | print_value (Int i) = signed_string_of_int i
    1.45 +  | print_value (String s) = quote s;
    1.46 +
    1.47 +fun print_type (Bool _) = "boolean"
    1.48 +  | print_type (Int _) = "integer"
    1.49 +  | print_type (String _) = "string";
    1.50 +
    1.51 +fun same_type (Bool _) (Bool _) = true
    1.52 +  | same_type (Int _) (Int _) = true
    1.53 +  | same_type (String _) (String _) = true
    1.54 +  | same_type _ _ = false;
    1.55 +
    1.56 +fun type_check name f value =
    1.57 +  let
    1.58 +    val value' = f value;
    1.59 +    val _ = same_type value value' orelse
    1.60 +      error ("Ill-typed configuration option " ^ quote name ^ ": " ^
    1.61 +        print_type value ^ " expected,\nbut " ^ print_type value' ^ " was found");
    1.62 +  in value' end;
    1.63 +
    1.64 +
    1.65 +(* abstract configuration options *)
    1.66 +
    1.67 +datatype 'a T = Config of
    1.68 + {get_value: Context.generic -> 'a,
    1.69 +  map_value: ('a -> 'a) -> Context.generic -> Context.generic};
    1.70 +
    1.71 +fun coerce make dest (Config {get_value, map_value}) =
    1.72 +  Config {get_value = dest o get_value, map_value = fn f => map_value (make o f o dest)};
    1.73 +
    1.74 +val bool = coerce Bool (fn Bool b => b);
    1.75 +val int = coerce Int (fn Int i => i);
    1.76 +val string = coerce String (fn String s => s);
    1.77 +
    1.78 +fun get_generic context (Config {get_value, ...}) = get_value context;
    1.79 +fun map_generic (Config {map_value, ...}) f context = map_value f context;
    1.80 +fun put_generic config value = map_generic config (K value);
    1.81 +
    1.82 +fun get_ctxt ctxt = get_generic (Context.Proof ctxt);
    1.83 +fun map_ctxt config f = Context.proof_map (map_generic config f);
    1.84 +fun put_ctxt config value = map_ctxt config (K value);
    1.85 +
    1.86 +fun get_thy thy = get_generic (Context.Theory thy);
    1.87 +fun map_thy config f = Context.theory_map (map_generic config f);
    1.88 +fun put_thy config value = map_thy config (K value);
    1.89 +
    1.90 +
    1.91 +(* context information *)
    1.92 +
    1.93 +structure Value = GenericDataFun
    1.94 +(
    1.95 +  type T = value Inttab.table;
    1.96 +  val empty = Inttab.empty;
    1.97 +  val extend = I;
    1.98 +  fun merge _ = Inttab.merge (K true);
    1.99 +);
   1.100 +
   1.101 +fun declare global name default =
   1.102 +  let
   1.103 +    val id = serial ();
   1.104 +
   1.105 +    fun get_value context = the_default default (Inttab.lookup (Value.get context) id);
   1.106 +    fun modify_value f = Value.map (Inttab.map_default (id, default) (type_check name f));
   1.107 +
   1.108 +    fun map_value f (context as Context.Proof _) =
   1.109 +          let val context' = modify_value f context in
   1.110 +            if global andalso
   1.111 +              get_value (Context.Theory (Context.theory_of context')) <> get_value context'
   1.112 +            then (warning ("Ignoring local change of global option " ^ quote name); context)
   1.113 +            else context'
   1.114 +          end
   1.115 +      | map_value f context = modify_value f context;
   1.116 +  in Config {get_value = get_value, map_value = map_value} end;
   1.117 +
   1.118 +
   1.119 +(*final declarations of this structure!*)
   1.120 +val get = get_ctxt;
   1.121 +val map = map_ctxt;
   1.122 +val put = put_ctxt;
   1.123 +
   1.124 +end;