src/Pure/config.ML
changeset 69575 f77cc54f6d47
parent 69574 b4ea943ce0b7
child 69576 cfac69e7b962
equal deleted inserted replaced
69574:b4ea943ce0b7 69575:f77cc54f6d47
     8 sig
     8 sig
     9   datatype value = Bool of bool | Int of int | Real of real | String of string
     9   datatype value = Bool of bool | Int of int | Real of real | String of string
    10   val print_value: value -> string
    10   val print_value: value -> string
    11   val print_type: value -> string
    11   val print_type: value -> string
    12   type 'a T
    12   type 'a T
    13   type raw = value T
    13   val name_of: 'a T -> string
    14   val bool: raw -> bool T
    14   val pos_of: 'a T -> Position.T
    15   val int: raw -> int T
       
    16   val real: raw -> real T
       
    17   val string: raw -> string T
       
    18   val get: Proof.context -> 'a T -> 'a
    15   val get: Proof.context -> 'a T -> 'a
    19   val map: 'a T -> ('a -> 'a) -> Proof.context -> Proof.context
    16   val map: 'a T -> ('a -> 'a) -> Proof.context -> Proof.context
    20   val put: 'a T -> 'a -> Proof.context -> Proof.context
    17   val put: 'a T -> 'a -> Proof.context -> Proof.context
    21   val restore: 'a T -> Proof.context -> Proof.context -> Proof.context
    18   val restore: 'a T -> Proof.context -> Proof.context -> Proof.context
    22   val get_global: theory -> 'a T -> 'a
    19   val get_global: theory -> 'a T -> 'a
    25   val restore_global: 'a T -> theory -> theory -> theory
    22   val restore_global: 'a T -> theory -> theory -> theory
    26   val get_generic: Context.generic -> 'a T -> 'a
    23   val get_generic: Context.generic -> 'a T -> 'a
    27   val map_generic: 'a T -> ('a -> 'a) -> Context.generic -> Context.generic
    24   val map_generic: 'a T -> ('a -> 'a) -> Context.generic -> Context.generic
    28   val put_generic: 'a T -> 'a -> Context.generic -> Context.generic
    25   val put_generic: 'a T -> 'a -> Context.generic -> Context.generic
    29   val restore_generic: 'a T -> Context.generic -> Context.generic -> Context.generic
    26   val restore_generic: 'a T -> Context.generic -> Context.generic -> Context.generic
    30   val declare: string * Position.T -> (Context.generic -> value) -> raw
    27   val bool: value T -> bool T
    31   val declare_option: string * Position.T -> raw
    28   val bool_value: bool T -> value T
    32   val name_of: 'a T -> string
    29   val int: value T -> int T
    33   val pos_of: 'a T -> Position.T
    30   val int_value: int T -> value T
       
    31   val real: value T -> real T
       
    32   val real_value: real T -> value T
       
    33   val string: value T -> string T
       
    34   val string_value: string T -> value T
       
    35   val declare: string * Position.T -> (Context.generic -> value) -> value T
       
    36   val declare_bool: string * Position.T -> (Context.generic -> bool) -> bool T
       
    37   val declare_int: string * Position.T -> (Context.generic -> int) -> int T
       
    38   val declare_real: string * Position.T -> (Context.generic -> real) -> real T
       
    39   val declare_string: string * Position.T -> (Context.generic -> string) -> string T
       
    40   val declare_option: string * Position.T -> value T
       
    41   val declare_option_bool: string * Position.T -> bool T
       
    42   val declare_option_int: string * Position.T -> int T
       
    43   val declare_option_real: string * Position.T -> real T
       
    44   val declare_option_string: string * Position.T -> string T
    34 end;
    45 end;
    35 
    46 
    36 structure Config: CONFIG =
    47 structure Config: CONFIG =
    37 struct
    48 struct
    38 
    49 
    76  {name: string,
    87  {name: string,
    77   pos: Position.T,
    88   pos: Position.T,
    78   get_value: Context.generic -> 'a,
    89   get_value: Context.generic -> 'a,
    79   map_value: ('a -> 'a) -> Context.generic -> Context.generic};
    90   map_value: ('a -> 'a) -> Context.generic -> Context.generic};
    80 
    91 
    81 type raw = value T;
    92 fun name_of (Config {name, ...}) = name;
    82 
    93 fun pos_of (Config {pos, ...}) = pos;
    83 fun coerce make dest (Config {name, pos, get_value, map_value}) = Config
       
    84  {name = name,
       
    85   pos = pos,
       
    86   get_value = dest o get_value,
       
    87   map_value = fn f => map_value (make o f o dest)};
       
    88 
       
    89 val bool = coerce Bool (fn Bool b => b);
       
    90 val int = coerce Int (fn Int i => i);
       
    91 val real = coerce Real (fn Real x => x);
       
    92 val string = coerce String (fn String s => s);
       
    93 
    94 
    94 fun get_generic context (Config {get_value, ...}) = get_value context;
    95 fun get_generic context (Config {get_value, ...}) = get_value context;
    95 fun map_generic (Config {map_value, ...}) f context = map_value f context;
    96 fun map_generic (Config {map_value, ...}) f context = map_value f context;
    96 fun put_generic config value = map_generic config (K value);
    97 fun put_generic config value = map_generic config (K value);
    97 fun restore_generic config context = put_generic config (get_generic context config);
    98 fun restore_generic config context = put_generic config (get_generic context config);
   105 fun map_global config f = Context.theory_map (map_generic config f);
   106 fun map_global config f = Context.theory_map (map_generic config f);
   106 fun put_global config value = map_global config (K value);
   107 fun put_global config value = map_global config (K value);
   107 fun restore_global config thy = put_global config (get_global thy config);
   108 fun restore_global config thy = put_global config (get_global thy config);
   108 
   109 
   109 
   110 
   110 (* context information *)
   111 (* coerce type *)
       
   112 
       
   113 fun coerce make dest (Config {name, pos, get_value, map_value}) = Config
       
   114  {name = name,
       
   115   pos = pos,
       
   116   get_value = dest o get_value,
       
   117   map_value = fn f => map_value (make o f o dest)};
       
   118 
       
   119 fun coerce_pair make dest = (coerce make dest, coerce dest make);
       
   120 
       
   121 val (bool, bool_value) = coerce_pair Bool (fn Bool b => b);
       
   122 val (int, int_value) = coerce_pair Int (fn Int i => i);
       
   123 val (real, real_value) = coerce_pair Real (fn Real x => x);
       
   124 val (string, string_value) = coerce_pair String (fn String s => s);
       
   125 
       
   126 
       
   127 (* context data *)
   111 
   128 
   112 structure Data = Generic_Data
   129 structure Data = Generic_Data
   113 (
   130 (
   114   type T = value Inttab.table;
   131   type T = value Inttab.table;
   115   val empty = Inttab.empty;
   132   val empty = Inttab.empty;
   130       Data.map (Inttab.update (id, type_check (name, pos) f (get_value context))) context;
   147       Data.map (Inttab.update (id, type_check (name, pos) f (get_value context))) context;
   131   in
   148   in
   132     Config {name = name, pos = pos, get_value = get_value, map_value = map_value}
   149     Config {name = name, pos = pos, get_value = get_value, map_value = map_value}
   133   end;
   150   end;
   134 
   151 
       
   152 fun declare_bool name default = bool (declare name (Bool o default));
       
   153 fun declare_int name default = int (declare name (Int o default));
       
   154 fun declare_real name default = real (declare name (Real o default));
       
   155 fun declare_string name default = string (declare name (String o default));
       
   156 
       
   157 
       
   158 (* system options *)
       
   159 
   135 fun declare_option (name, pos) =
   160 fun declare_option (name, pos) =
   136   let
   161   let
   137     val typ = Options.default_typ name;
   162     val typ = Options.default_typ name;
   138     val default =
   163     val default =
   139       if typ = Options.boolT then fn _ => Bool (Options.default_bool name)
   164       if typ = Options.boolT then fn _ => Bool (Options.default_bool name)
   141       else if typ = Options.realT then fn _ => Real (Options.default_real name)
   166       else if typ = Options.realT then fn _ => Real (Options.default_real name)
   142       else if typ = Options.stringT then fn _ => String (Options.default_string name)
   167       else if typ = Options.stringT then fn _ => String (Options.default_string name)
   143       else error ("Unknown type for option " ^ quote name ^ Position.here pos ^ " : " ^ quote typ);
   168       else error ("Unknown type for option " ^ quote name ^ Position.here pos ^ " : " ^ quote typ);
   144   in declare (name, pos) default end;
   169   in declare (name, pos) default end;
   145 
   170 
   146 fun name_of (Config {name, ...}) = name;
   171 val declare_option_bool = bool o declare_option;
   147 fun pos_of (Config {pos, ...}) = pos;
   172 val declare_option_int = int o declare_option;
   148 
   173 val declare_option_real = real o declare_option;
       
   174 val declare_option_string = string o declare_option;
   149 
   175 
   150 (*final declarations of this structure!*)
   176 (*final declarations of this structure!*)
   151 val get = get_ctxt;
   177 val get = get_ctxt;
   152 val map = map_ctxt;
   178 val map = map_ctxt;
   153 val put = put_ctxt;
   179 val put = put_ctxt;