src/Pure/Isar/context_rules.ML
changeset 18637 33a6f6caa617
parent 18418 bf448d999b7e
child 18667 85d04c28224a
     1.1 --- a/src/Pure/Isar/context_rules.ML	Tue Jan 10 19:33:35 2006 +0100
     1.2 +++ b/src/Pure/Isar/context_rules.ML	Tue Jan 10 19:33:36 2006 +0100
     1.3 @@ -15,40 +15,21 @@
     1.4    val orderlist: ((int * int) * 'a) list -> 'a list
     1.5    val find_rules_netpair: bool -> thm list -> term -> netpair -> thm list
     1.6    val find_rules: bool -> thm list -> term -> ProofContext.context -> thm list list
     1.7 -  val print_global_rules: theory -> unit
     1.8 -  val print_local_rules: ProofContext.context -> unit
     1.9 +  val print_rules: Context.generic -> unit
    1.10    val addSWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory
    1.11    val addWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory
    1.12    val Swrap: ProofContext.context -> (int -> tactic) -> int -> tactic
    1.13    val wrap: ProofContext.context -> (int -> tactic) -> int -> tactic
    1.14 -  val intro_bang_global: int option -> theory attribute
    1.15 -  val elim_bang_global: int option -> theory attribute
    1.16 -  val dest_bang_global: int option -> theory attribute
    1.17 -  val intro_global: int option -> theory attribute
    1.18 -  val elim_global: int option -> theory attribute
    1.19 -  val dest_global: int option -> theory attribute
    1.20 -  val intro_query_global: int option -> theory attribute
    1.21 -  val elim_query_global: int option -> theory attribute
    1.22 -  val dest_query_global: int option -> theory attribute
    1.23 -  val rule_del_global: theory attribute
    1.24 -  val intro_bang_local: int option -> ProofContext.context attribute
    1.25 -  val elim_bang_local: int option -> ProofContext.context attribute
    1.26 -  val dest_bang_local: int option -> ProofContext.context attribute
    1.27 -  val intro_local: int option -> ProofContext.context attribute
    1.28 -  val elim_local: int option -> ProofContext.context attribute
    1.29 -  val dest_local: int option -> ProofContext.context attribute
    1.30 -  val intro_query_local: int option -> ProofContext.context attribute
    1.31 -  val elim_query_local: int option -> ProofContext.context attribute
    1.32 -  val dest_query_local: int option -> ProofContext.context attribute
    1.33 -  val rule_del_local: ProofContext.context attribute
    1.34 -  val addXIs_global: theory * thm list -> theory
    1.35 -  val addXEs_global: theory * thm list -> theory
    1.36 -  val addXDs_global: theory * thm list -> theory
    1.37 -  val delrules_global: theory * thm list -> theory
    1.38 -  val addXIs_local: ProofContext.context * thm list -> ProofContext.context
    1.39 -  val addXEs_local: ProofContext.context * thm list -> ProofContext.context
    1.40 -  val addXDs_local: ProofContext.context * thm list -> ProofContext.context
    1.41 -  val delrules_local: ProofContext.context * thm list -> ProofContext.context
    1.42 +  val intro_bang: int option -> Context.generic attribute
    1.43 +  val elim_bang: int option -> Context.generic attribute
    1.44 +  val dest_bang: int option -> Context.generic attribute
    1.45 +  val intro: int option -> Context.generic attribute
    1.46 +  val elim: int option -> Context.generic attribute
    1.47 +  val dest: int option -> Context.generic attribute
    1.48 +  val intro_query: int option -> Context.generic attribute
    1.49 +  val elim_query: int option -> Context.generic attribute
    1.50 +  val dest_query: int option -> Context.generic attribute
    1.51 +  val rule_del: Context.generic attribute
    1.52  end;
    1.53  
    1.54  structure ContextRules: CONTEXT_RULES =
    1.55 @@ -78,7 +59,7 @@
    1.56  val rule_indexes = distinct (map #1 rule_kinds);
    1.57  
    1.58  
    1.59 -(* raw data *)
    1.60 +(* context data *)
    1.61  
    1.62  type netpair = ((int * int) * (bool * thm)) Net.net * ((int * int) * (bool * thm)) Net.net;
    1.63  val empty_netpairs: netpair list = replicate (length rule_indexes) (Net.empty, Net.empty);
    1.64 @@ -108,60 +89,45 @@
    1.65      else make_rules next (filter_out eq_th rules) (map (del false o del true) netpairs) wrappers
    1.66    end;
    1.67  
    1.68 -fun print_rules prt x (Rules {rules, ...}) =
    1.69 -  let
    1.70 -    fun prt_kind (i, b) =
    1.71 -      Pretty.big_list ((the o AList.lookup (op =) kind_names) (i, b) ^ ":")
    1.72 -        (List.mapPartial (fn (_, (k, th)) => if k = (i, b) then SOME (prt x th) else NONE)
    1.73 -          (sort (int_ord o pairself fst) rules));
    1.74 -  in Pretty.writeln (Pretty.chunks (map prt_kind rule_kinds)) end;
    1.75 -
    1.76 -
    1.77 -(* theory and proof data *)
    1.78 -
    1.79 -structure GlobalRulesArgs =
    1.80 -struct
    1.81 +structure Rules = GenericDataFun
    1.82 +(
    1.83    val name = "Isar/rules";
    1.84    type T = T;
    1.85  
    1.86    val empty = make_rules ~1 [] empty_netpairs ([], []);
    1.87 -  val copy = I;
    1.88    val extend = I;
    1.89  
    1.90    fun merge _ (Rules {rules = rules1, wrappers = (ws1, ws1'), ...},
    1.91        Rules {rules = rules2, wrappers = (ws2, ws2'), ...}) =
    1.92      let
    1.93 -      val wrappers = (gen_merge_lists' (eq_snd (op =)) ws1 ws2, gen_merge_lists' (eq_snd (op =)) ws1' ws2');
    1.94 +      val wrappers =
    1.95 +        (gen_merge_lists' (eq_snd (op =)) ws1 ws2, gen_merge_lists' (eq_snd (op =)) ws1' ws2');
    1.96        val rules = gen_merge_lists' (fn ((_, (k1, th1)), (_, (k2, th2))) =>
    1.97            k1 = k2 andalso Drule.eq_thm_prop (th1, th2)) rules1 rules2;
    1.98        val next = ~ (length rules);
    1.99        val netpairs = Library.foldl (fn (nps, (n, (w, ((i, b), th)))) =>
   1.100            nth_map i (curry insert_tagged_brl ((w, n), (b, th))) nps)
   1.101          (empty_netpairs, next upto ~1 ~~ rules);
   1.102 -    in make_rules (next - 1) rules netpairs wrappers end;
   1.103 -
   1.104 -  val print = print_rules Display.pretty_thm_sg;
   1.105 -end;
   1.106 -
   1.107 -structure GlobalRules = TheoryDataFun(GlobalRulesArgs);
   1.108 -val _ = Context.add_setup [GlobalRules.init];
   1.109 -val print_global_rules = GlobalRules.print;
   1.110 +    in make_rules (next - 1) rules netpairs wrappers end
   1.111  
   1.112 -structure LocalRules = ProofDataFun
   1.113 -(struct
   1.114 -  val name = GlobalRulesArgs.name;
   1.115 -  type T = GlobalRulesArgs.T;
   1.116 -  val init = GlobalRules.get;
   1.117 -  val print = print_rules ProofContext.pretty_thm;
   1.118 -end);
   1.119 +  fun print generic (Rules {rules, ...}) =
   1.120 +    let
   1.121 +      val ctxt = Context.proof_of generic;
   1.122 +      fun prt_kind (i, b) =
   1.123 +        Pretty.big_list ((the o AList.lookup (op =) kind_names) (i, b) ^ ":")
   1.124 +          (List.mapPartial (fn (_, (k, th)) =>
   1.125 +              if k = (i, b) then SOME (ProofContext.pretty_thm ctxt th) else NONE)
   1.126 +            (sort (int_ord o pairself fst) rules));
   1.127 +    in Pretty.writeln (Pretty.chunks (map prt_kind rule_kinds)) end;
   1.128 +);
   1.129  
   1.130 -val _ = Context.add_setup [LocalRules.init];
   1.131 -val print_local_rules = LocalRules.print;
   1.132 +val _ = Context.add_setup [Rules.init];
   1.133 +val print_rules = Rules.print;
   1.134  
   1.135  
   1.136  (* access data *)
   1.137  
   1.138 -fun netpairs ctxt = let val Rules {netpairs, ...} = LocalRules.get ctxt in netpairs end;
   1.139 +fun netpairs ctxt = let val Rules {netpairs, ...} = Rules.get (Context.Proof ctxt) in netpairs end;
   1.140  val netpair_bang = hd o netpairs;
   1.141  val netpair = hd o tl o netpairs;
   1.142  
   1.143 @@ -197,15 +163,16 @@
   1.144  
   1.145  (* wrappers *)
   1.146  
   1.147 -fun gen_add_wrapper upd w = GlobalRules.map (fn (rs as Rules {next, rules, netpairs, wrappers}) =>
   1.148 -  make_rules next rules netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers));
   1.149 +fun gen_add_wrapper upd w = Context.the_theory o
   1.150 +  Rules.map (fn (rs as Rules {next, rules, netpairs, wrappers}) =>
   1.151 +    make_rules next rules netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers)) o Context.Theory;
   1.152  
   1.153  val addSWrapper = gen_add_wrapper Library.apfst;
   1.154  val addWrapper = gen_add_wrapper Library.apsnd;
   1.155  
   1.156  
   1.157  fun gen_wrap which ctxt =
   1.158 -  let val Rules {wrappers, ...} = LocalRules.get ctxt
   1.159 +  let val Rules {wrappers, ...} = Rules.get (Context.Proof ctxt)
   1.160    in fold_rev fst (which wrappers) end;
   1.161  
   1.162  val Swrap = gen_wrap #1;
   1.163 @@ -217,57 +184,44 @@
   1.164  
   1.165  (* add and del rules *)
   1.166  
   1.167 -local
   1.168 +fun rule_del (x, th) =
   1.169 +  (Rules.map (del_rule th o del_rule (Tactic.make_elim th)) x, th);
   1.170  
   1.171 -fun del map_data (x, th) =
   1.172 -  (map_data (del_rule th o del_rule (Tactic.make_elim th)) x, th);
   1.173 +fun rule_add k view opt_w =
   1.174 +  (fn (x, th) => (Rules.map (add_rule k opt_w (view th)) x, th)) o rule_del;
   1.175  
   1.176 -fun add k view map_data opt_w =
   1.177 -  (fn (x, th) => (map_data (add_rule k opt_w (view th)) x, th)) o del map_data;
   1.178 +val intro_bang  = rule_add intro_bangK I;
   1.179 +val elim_bang   = rule_add elim_bangK I;
   1.180 +val dest_bang   = rule_add elim_bangK Tactic.make_elim;
   1.181 +val intro       = rule_add introK I;
   1.182 +val elim        = rule_add elimK I;
   1.183 +val dest        = rule_add elimK Tactic.make_elim;
   1.184 +val intro_query = rule_add intro_queryK I;
   1.185 +val elim_query  = rule_add elim_queryK I;
   1.186 +val dest_query  = rule_add elim_queryK Tactic.make_elim;
   1.187  
   1.188 -in
   1.189 +val _ = Context.add_setup
   1.190 +  [snd o PureThy.add_thms [(("", Drule.equal_intr_rule), [Attrib.theory (intro_query NONE)])]];
   1.191 +
   1.192  
   1.193 -val intro_bang_global  = add intro_bangK I GlobalRules.map;
   1.194 -val elim_bang_global   = add elim_bangK I GlobalRules.map;
   1.195 -val dest_bang_global   = add elim_bangK Tactic.make_elim GlobalRules.map;
   1.196 -val intro_global       = add introK I GlobalRules.map;
   1.197 -val elim_global        = add elimK I GlobalRules.map;
   1.198 -val dest_global        = add elimK Tactic.make_elim GlobalRules.map;
   1.199 -val intro_query_global = add intro_queryK I GlobalRules.map;
   1.200 -val elim_query_global  = add elim_queryK I GlobalRules.map;
   1.201 -val dest_query_global  = add elim_queryK Tactic.make_elim GlobalRules.map;
   1.202 -val rule_del_global    = del GlobalRules.map;
   1.203 +(* concrete syntax *)
   1.204 +
   1.205 +fun add_args a b c x = Attrib.syntax
   1.206 +  (Scan.lift ((Args.bang >> K a || Args.query >> K c || Scan.succeed b) -- (Scan.option Args.nat))
   1.207 +    >> (fn (f, n) => f n)) x;
   1.208 +
   1.209 +fun del_args att = Attrib.syntax (Scan.lift Args.del >> K att);
   1.210  
   1.211 -val intro_bang_local   = add intro_bangK I LocalRules.map;
   1.212 -val elim_bang_local    = add elim_bangK I LocalRules.map;
   1.213 -val dest_bang_local    = add elim_bangK Tactic.make_elim LocalRules.map;
   1.214 -val intro_local        = add introK I LocalRules.map;
   1.215 -val elim_local         = add elimK I LocalRules.map;
   1.216 -val dest_local         = add elimK Tactic.make_elim LocalRules.map;
   1.217 -val intro_query_local  = add intro_queryK I LocalRules.map;
   1.218 -val elim_query_local   = add elim_queryK I LocalRules.map;
   1.219 -val dest_query_local   = add elim_queryK Tactic.make_elim LocalRules.map;
   1.220 -val rule_del_local     = del LocalRules.map;
   1.221 +val rule_atts =
   1.222 + [("intro", Attrib.common (add_args intro_bang intro intro_query),
   1.223 +    "declaration of introduction rule"),
   1.224 +  ("elim", Attrib.common (add_args elim_bang elim elim_query),
   1.225 +    "declaration of elimination rule"),
   1.226 +  ("dest", Attrib.common (add_args dest_bang dest dest_query),
   1.227 +    "declaration of destruction rule"),
   1.228 +  ("rule", Attrib.common (del_args rule_del),
   1.229 +    "remove declaration of intro/elim/dest rule")];
   1.230 +
   1.231 +val _ = Context.add_setup [Attrib.add_attributes rule_atts];
   1.232  
   1.233  end;
   1.234 -
   1.235 -val _ = Context.add_setup
   1.236 -  [snd o PureThy.add_thms [(("", Drule.equal_intr_rule), [intro_query_global NONE])]];
   1.237 -
   1.238 -
   1.239 -(* low-level modifiers *)
   1.240 -
   1.241 -fun modifier att (x, ths) =
   1.242 -  fst (Thm.applys_attributes [att] (x, rev ths));
   1.243 -
   1.244 -val addXIs_global = modifier (intro_query_global NONE);
   1.245 -val addXEs_global = modifier (elim_query_global NONE);
   1.246 -val addXDs_global = modifier (dest_query_global NONE);
   1.247 -val delrules_global = modifier rule_del_global;
   1.248 -
   1.249 -val addXIs_local = modifier (intro_query_local NONE);
   1.250 -val addXEs_local = modifier (elim_query_local NONE);
   1.251 -val addXDs_local = modifier (dest_query_local NONE);
   1.252 -val delrules_local = modifier rule_del_local;
   1.253 -
   1.254 -end;