src/Pure/Isar/context_rules.ML
author wenzelm
Wed Dec 05 03:18:03 2001 +0100 (2001-12-05 ago)
changeset 12385 389d11fb62c8
parent 12380 3402d300f5ef
child 12399 2ba27248af7f
permissions -rw-r--r--
removed unused functionality (weight etc.);
     1 (*  Title:      Pure/Isar/context_rules.ML
     2     ID:         $Id$
     3     Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Declarations of intro/elim/dest rules in Pure; see
     7 Provers/classical.ML for a more specialized version of the same idea.
     8 *)
     9 
    10 signature CONTEXT_RULES =
    11 sig
    12   type netpair
    13   type T
    14   val netpair_bang: Proof.context -> netpair
    15   val netpair: Proof.context -> netpair
    16   val orderlist: ((int * int) * 'a) list -> 'a list
    17   val find_rules: Proof.context -> thm list -> term -> thm list list
    18   val print_global_rules: theory -> unit
    19   val print_local_rules: Proof.context -> unit
    20   val addSWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory
    21   val addWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory
    22   val Swrap: Proof.context -> (int -> tactic) -> int -> tactic
    23   val wrap: Proof.context -> (int -> tactic) -> int -> tactic
    24   val intro_bang_global: int option -> theory attribute
    25   val elim_bang_global: int option -> theory attribute
    26   val dest_bang_global: int option -> theory attribute
    27   val intro_global: int option -> theory attribute
    28   val elim_global: int option -> theory attribute
    29   val dest_global: int option -> theory attribute
    30   val intro_query_global: int option -> theory attribute
    31   val elim_query_global: int option -> theory attribute
    32   val dest_query_global: int option -> theory attribute
    33   val rule_del_global: theory attribute
    34   val intro_bang_local: int option -> Proof.context attribute
    35   val elim_bang_local: int option -> Proof.context attribute
    36   val dest_bang_local: int option -> Proof.context attribute
    37   val intro_local: int option -> Proof.context attribute
    38   val elim_local: int option -> Proof.context attribute
    39   val dest_local: int option -> Proof.context attribute
    40   val intro_query_local: int option -> Proof.context attribute
    41   val elim_query_local: int option -> Proof.context attribute
    42   val dest_query_local: int option -> Proof.context attribute
    43   val rule_del_local: Proof.context attribute
    44   val addXIs_global: theory * thm list -> theory
    45   val addXEs_global: theory * thm list -> theory
    46   val addXDs_global: theory * thm list -> theory
    47   val delrules_global: theory * thm list -> theory
    48   val addXIs_local: Proof.context * thm list -> Proof.context
    49   val addXEs_local: Proof.context * thm list -> Proof.context
    50   val addXDs_local: Proof.context * thm list -> Proof.context
    51   val delrules_local: Proof.context * thm list -> Proof.context
    52   val setup: (theory -> theory) list
    53 end;
    54 
    55 structure ContextRules: CONTEXT_RULES =
    56 struct
    57 
    58 
    59 (** rule declaration contexts **)
    60 
    61 (* rule kinds *)
    62 
    63 val intro_bangK = (0, false);
    64 val elim_bangK = (0, true);
    65 val introK = (1, false);
    66 val elimK = (1, true);
    67 val intro_queryK = (2, false);
    68 val elim_queryK = (2, true);
    69 
    70 val kind_names =
    71  [(intro_bangK, "safe introduction rules (intro!)"),
    72   (elim_bangK, "safe elimination rules (elim!)"),
    73   (introK, "introduction rules (intro)"),
    74   (elimK, "elimination rules (elim)"),
    75   (intro_queryK, "extra introduction rules (intro?)"),
    76   (elim_queryK, "extra elimination rules (elim?)")];
    77 
    78 val rule_kinds = map #1 kind_names;
    79 val rule_indexes = distinct (map #1 rule_kinds);
    80 
    81 
    82 (* raw data *)
    83 
    84 type netpair = ((int * int) * (bool * thm)) Net.net * ((int * int) * (bool * thm)) Net.net;
    85 val empty_netpairs: netpair list = replicate (length rule_indexes) (Net.empty, Net.empty);
    86 
    87 datatype T = Rules of
    88  {next: int,
    89   rules: (int * ((int * bool) * thm)) list,
    90   netpairs: netpair list,
    91   wrappers: (((int -> tactic) -> int -> tactic) * stamp) list *
    92     (((int -> tactic) -> int -> tactic) * stamp) list};
    93 
    94 fun make_rules next rules netpairs wrappers =
    95   Rules {next = next, rules = rules, netpairs = netpairs, wrappers = wrappers};
    96 
    97 fun add_rule (i, b) opt_w th (Rules {next, rules, netpairs, wrappers}) =
    98   let val w = (case opt_w of Some w => w | None => Tactic.subgoals_of_brl (b, th)) in
    99     make_rules (next - 1) ((w, ((i, b), th)) :: rules)
   100       (map_nth_elem i (curry insert_tagged_brl ((w, next), (b, th))) netpairs) wrappers
   101   end;
   102 
   103 fun del_rule th (rs as Rules {next, rules, netpairs, wrappers}) =
   104   let
   105     fun eq_th (_, (_, th')) = Thm.eq_thm (th, th');
   106     fun del b netpair = delete_tagged_brl ((b, th), netpair) handle Net.DELETE => netpair;
   107   in
   108     if not (exists eq_th rules) then rs
   109     else make_rules next (filter_out eq_th rules) (map (del false o del true) netpairs) wrappers
   110   end;
   111 
   112 fun print_rules prt x (Rules {rules, ...}) =
   113   let
   114     fun prt_kind (i, b) =
   115       Pretty.big_list (the (assoc (kind_names, (i, b))) ^ ":")
   116         (mapfilter (fn (_, (k, th)) => if k = (i, b) then Some (prt x th) else None)
   117           (sort (int_ord o pairself fst) rules));
   118   in Pretty.writeln (Pretty.chunks (map prt_kind rule_kinds)) end;
   119 
   120 
   121 (* theory and proof data *)
   122 
   123 structure GlobalRulesArgs =
   124 struct
   125   val name = "Isar/rule_context";
   126   type T = T;
   127 
   128   val empty = make_rules ~1 [] empty_netpairs ([], []);
   129   val copy = I;
   130   val prep_ext = I;
   131 
   132   fun merge (Rules {rules = rules1, wrappers = (ws1, ws1'), ...},
   133       Rules {rules = rules2, wrappers = (ws2, ws2'), ...}) =
   134     let
   135       val wrappers = (gen_merge_lists' eq_snd ws1 ws2, gen_merge_lists' eq_snd ws1' ws2');
   136       val rules = gen_merge_lists' (fn ((_, (k1, th1)), (_, (k2, th2))) =>
   137           k1 = k2 andalso Thm.eq_thm (th1, th2)) rules1 rules2;
   138       val next = ~ (length rules);
   139       val netpairs = foldl (fn (nps, (n, (w, ((i, b), th)))) =>
   140           map_nth_elem i (curry insert_tagged_brl ((w, n), (b, th))) nps)
   141         (empty_netpairs, next upto ~1 ~~ rules);
   142     in make_rules (next - 1) rules netpairs wrappers end;
   143 
   144   val print = print_rules Display.pretty_thm_sg;
   145 end;
   146 
   147 structure GlobalRules = TheoryDataFun(GlobalRulesArgs);
   148 val print_global_rules = GlobalRules.print;
   149 
   150 structure LocalRulesArgs =
   151 struct
   152   val name = GlobalRulesArgs.name;
   153   type T = GlobalRulesArgs.T;
   154   val init = GlobalRules.get;
   155   val print = print_rules ProofContext.pretty_thm;
   156 end;
   157 
   158 structure LocalRules = ProofDataFun(LocalRulesArgs);
   159 val print_local_rules = LocalRules.print;
   160 
   161 
   162 (* access data *)
   163 
   164 fun netpairs ctxt = let val Rules {netpairs, ...} = LocalRules.get ctxt in netpairs end;
   165 val netpair_bang = hd o netpairs;
   166 val netpair = hd o tl o netpairs;
   167 
   168 
   169 fun untaglist [] = []
   170   | untaglist [(k : int * int, x)] = [x]
   171   | untaglist ((k, x) :: (rest as (k', x') :: _)) =
   172       if k = k' then untaglist rest
   173       else x :: untaglist rest;
   174 
   175 fun orderlist brls = untaglist (sort (prod_ord int_ord int_ord o pairself fst) brls);
   176 fun orderlist_no_weight brls = untaglist (sort (int_ord o pairself (snd o fst)) brls);
   177 fun may_unify t net = map snd (orderlist_no_weight (Net.unify_term net t));
   178 
   179 fun find_erules [] = K []
   180   | find_erules (fact :: _) = may_unify (Logic.strip_assums_concl (#prop (Thm.rep_thm fact)));
   181 fun find_irules goal = may_unify (Logic.strip_assums_concl goal);
   182 
   183 fun find_rules ctxt facts goal =
   184   map (fn (inet, enet) => find_erules facts enet @ find_irules goal inet) (netpairs ctxt);
   185 
   186 
   187 fun gen_add_wrapper upd w = GlobalRules.map (fn (rs as Rules {next, rules, netpairs, wrappers}) =>
   188   make_rules next rules netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers));
   189 
   190 val addSWrapper = gen_add_wrapper Library.apfst;
   191 val addWrapper = gen_add_wrapper Library.apsnd;
   192 
   193 
   194 fun gen_wrap which ctxt =
   195   let val Rules {wrappers, ...} = LocalRules.get ctxt
   196   in fn tac => foldr (fn ((w, _), t) => w t) (which wrappers, tac) end;
   197 
   198 val Swrap = gen_wrap #1;
   199 val wrap = gen_wrap #2;
   200 
   201 
   202 
   203 (** attributes **)
   204 
   205 (* add and del rules *)
   206 
   207 local
   208 
   209 fun del map_data (x, th) =
   210   (map_data (del_rule th o del_rule (Tactic.make_elim th)) x, th);
   211 
   212 fun add k view map_data opt_w =
   213   (fn (x, th) => (map_data (add_rule k opt_w th) x, th)) o del map_data;
   214 
   215 in
   216 
   217 val intro_bang_global  = add intro_bangK I GlobalRules.map;
   218 val elim_bang_global   = add elim_bangK I GlobalRules.map;
   219 val dest_bang_global   = add elim_bangK Tactic.make_elim GlobalRules.map;
   220 val intro_global       = add introK I GlobalRules.map;
   221 val elim_global        = add elimK I GlobalRules.map;
   222 val dest_global        = add elimK Tactic.make_elim GlobalRules.map;
   223 val intro_query_global = add intro_queryK I GlobalRules.map;
   224 val elim_query_global  = add elim_queryK I GlobalRules.map;
   225 val dest_query_global  = add elim_queryK Tactic.make_elim GlobalRules.map;
   226 val rule_del_global    = del GlobalRules.map;
   227 
   228 val intro_bang_local   = add intro_bangK I LocalRules.map;
   229 val elim_bang_local    = add elim_bangK I LocalRules.map;
   230 val dest_bang_local    = add elim_bangK Tactic.make_elim LocalRules.map;
   231 val intro_local        = add introK I LocalRules.map;
   232 val elim_local         = add elimK I LocalRules.map;
   233 val dest_local         = add elimK Tactic.make_elim LocalRules.map;
   234 val intro_query_local  = add intro_queryK I LocalRules.map;
   235 val elim_query_local   = add elim_queryK I LocalRules.map;
   236 val dest_query_local   = add elim_queryK Tactic.make_elim LocalRules.map;
   237 val rule_del_local     = del LocalRules.map;
   238 
   239 end;
   240 
   241 
   242 (* low-level modifiers *)
   243 
   244 fun modifier att (x, ths) =
   245   #1 (Thm.applys_attributes ((x, rev ths), [att]));
   246 
   247 val addXIs_global = modifier (intro_query_global None);
   248 val addXEs_global = modifier (elim_query_global None);
   249 val addXDs_global = modifier (dest_query_global None);
   250 val delrules_global = modifier rule_del_global;
   251 
   252 val addXIs_local = modifier (intro_query_local None);
   253 val addXEs_local = modifier (elim_query_local None);
   254 val addXDs_local = modifier (dest_query_local None);
   255 val delrules_local = modifier rule_del_local;
   256 
   257 
   258 (* concrete syntax *)
   259 
   260 fun add_args a b c x = Attrib.syntax
   261   (Scan.lift ((Args.bang >> K a || Args.query >> K c || Scan.succeed b) -- (Scan.option Args.nat))
   262     >> (fn (f, n) => f n)) x;
   263 
   264 fun del_args att = Attrib.syntax (Scan.lift Args.del >> K att);
   265 
   266 
   267 val rule_atts =
   268  [("intro",
   269    (add_args intro_bang_global intro_global intro_query_global,
   270     add_args intro_bang_local intro_local intro_query_local),
   271     "declaration of introduction rule"),
   272   ("elim",
   273    (add_args elim_bang_global elim_global elim_query_global,
   274     add_args elim_bang_local elim_local elim_query_local),
   275     "declaration of elimination rule"),
   276   ("dest",
   277    (add_args dest_bang_global dest_global dest_query_global,
   278     add_args dest_bang_local dest_local dest_query_local),
   279     "declaration of destruction rule"),
   280   ("rule", (del_args rule_del_global, del_args rule_del_local),
   281     "remove declaration of intro/elim/dest rule")];
   282 
   283 
   284 
   285 (** theory setup **)
   286 
   287 val setup =
   288  [GlobalRules.init, LocalRules.init, Attrib.add_attributes rule_atts];
   289 
   290 
   291 end;