src/Pure/Isar/context_rules.ML
author wenzelm
Fri Mar 29 22:14:27 2013 +0100 (2013-03-29 ago)
changeset 51580 64ef8260dc60
parent 45375 7fe19930dfc9
child 51584 98029ceda8ce
permissions -rw-r--r--
Pretty.item markup for improved readability of lists of items;
     1 (*  Title:      Pure/Isar/context_rules.ML
     2     Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
     3 
     4 Declarations of intro/elim/dest rules in Pure (see also
     5 Provers/classical.ML for a more specialized version of the same idea).
     6 *)
     7 
     8 signature CONTEXT_RULES =
     9 sig
    10   type netpair = ((int * int) * (bool * thm)) Net.net * ((int * int) * (bool * thm)) Net.net
    11   val netpair_bang: Proof.context -> netpair
    12   val netpair: Proof.context -> netpair
    13   val orderlist: ((int * int) * 'a) list -> 'a list
    14   val find_rules_netpair: bool -> thm list -> term -> netpair -> thm list
    15   val find_rules: bool -> thm list -> term -> Proof.context -> thm list list
    16   val print_rules: Proof.context -> unit
    17   val addSWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory
    18   val addWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory
    19   val Swrap: Proof.context -> (int -> tactic) -> int -> tactic
    20   val wrap: Proof.context -> (int -> tactic) -> int -> tactic
    21   val intro_bang: int option -> attribute
    22   val elim_bang: int option -> attribute
    23   val dest_bang: int option -> attribute
    24   val intro: int option -> attribute
    25   val elim: int option -> attribute
    26   val dest: int option -> attribute
    27   val intro_query: int option -> attribute
    28   val elim_query: int option -> attribute
    29   val dest_query: int option -> attribute
    30   val rule_del: attribute
    31   val add: (int option -> attribute) -> (int option -> attribute) -> (int option -> attribute) ->
    32     attribute context_parser
    33 end;
    34 
    35 structure Context_Rules: CONTEXT_RULES =
    36 struct
    37 
    38 
    39 (** rule declaration contexts **)
    40 
    41 (* rule kinds *)
    42 
    43 val intro_bangK = (0, false);
    44 val elim_bangK = (0, true);
    45 val introK = (1, false);
    46 val elimK = (1, true);
    47 val intro_queryK = (2, false);
    48 val elim_queryK = (2, true);
    49 
    50 val kind_names =
    51  [(intro_bangK, "safe introduction rules (intro!)"),
    52   (elim_bangK, "safe elimination rules (elim!)"),
    53   (introK, "introduction rules (intro)"),
    54   (elimK, "elimination rules (elim)"),
    55   (intro_queryK, "extra introduction rules (intro?)"),
    56   (elim_queryK, "extra elimination rules (elim?)")];
    57 
    58 val rule_kinds = map #1 kind_names;
    59 val rule_indexes = distinct (op =) (map #1 rule_kinds);
    60 
    61 
    62 (* context data *)
    63 
    64 type netpair = ((int * int) * (bool * thm)) Net.net * ((int * int) * (bool * thm)) Net.net;
    65 val empty_netpairs: netpair list = replicate (length rule_indexes) (Net.empty, Net.empty);
    66 
    67 datatype rules = Rules of
    68  {next: int,
    69   rules: (int * ((int * bool) * thm)) list,
    70   netpairs: netpair list,
    71   wrappers: (((int -> tactic) -> int -> tactic) * stamp) list *
    72     (((int -> tactic) -> int -> tactic) * stamp) list};
    73 
    74 fun make_rules next rules netpairs wrappers =
    75   Rules {next = next, rules = rules, netpairs = netpairs, wrappers = wrappers};
    76 
    77 fun add_rule (i, b) opt_w th (Rules {next, rules, netpairs, wrappers}) =
    78   let val w = (case opt_w of SOME w => w | NONE => Tactic.subgoals_of_brl (b, th)) in
    79     make_rules (next - 1) ((w, ((i, b), th)) :: rules)
    80       (nth_map i (Tactic.insert_tagged_brl ((w, next), (b, th))) netpairs) wrappers
    81   end;
    82 
    83 fun del_rule0 th (rs as Rules {next, rules, netpairs, wrappers}) =
    84   let
    85     fun eq_th (_, (_, th')) = Thm.eq_thm_prop (th, th');
    86     fun del b netpair = Tactic.delete_tagged_brl (b, th) netpair handle Net.DELETE => netpair;
    87   in
    88     if not (exists eq_th rules) then rs
    89     else make_rules next (filter_out eq_th rules) (map (del false o del true) netpairs) wrappers
    90   end;
    91 
    92 fun del_rule th = del_rule0 th o del_rule0 (Tactic.make_elim th);
    93 
    94 structure Rules = Generic_Data
    95 (
    96   type T = rules;
    97   val empty = make_rules ~1 [] empty_netpairs ([], []);
    98   val extend = I;
    99   fun merge
   100     (Rules {rules = rules1, wrappers = (ws1, ws1'), ...},
   101       Rules {rules = rules2, wrappers = (ws2, ws2'), ...}) =
   102     let
   103       val wrappers =
   104         (Library.merge (eq_snd (op =)) (ws1, ws2), Library.merge (eq_snd (op =)) (ws1', ws2'));
   105       val rules = Library.merge (fn ((_, (k1, th1)), (_, (k2, th2))) =>
   106           k1 = k2 andalso Thm.eq_thm_prop (th1, th2)) (rules1, rules2);
   107       val next = ~ (length rules);
   108       val netpairs = fold (fn (n, (w, ((i, b), th))) =>
   109           nth_map i (Tactic.insert_tagged_brl ((w, n), (b, th))))
   110         (next upto ~1 ~~ rules) empty_netpairs;
   111     in make_rules (next - 1) rules netpairs wrappers end;
   112 );
   113 
   114 fun print_rules ctxt =
   115   let
   116     val Rules {rules, ...} = Rules.get (Context.Proof ctxt);
   117     fun prt_kind (i, b) =
   118       Pretty.big_list ((the o AList.lookup (op =) kind_names) (i, b) ^ ":")
   119         (map_filter (fn (_, (k, th)) =>
   120             if k = (i, b) then SOME (Pretty.item [Display.pretty_thm ctxt th]) else NONE)
   121           (sort (int_ord o pairself fst) rules));
   122   in Pretty.writeln (Pretty.chunks (map prt_kind rule_kinds)) end;
   123 
   124 
   125 (* access data *)
   126 
   127 fun netpairs ctxt = let val Rules {netpairs, ...} = Rules.get (Context.Proof ctxt) in netpairs end;
   128 val netpair_bang = hd o netpairs;
   129 val netpair = hd o tl o netpairs;
   130 
   131 
   132 (* retrieving rules *)
   133 
   134 fun untaglist [] = []
   135   | untaglist [(_ : int * int, x)] = [x]
   136   | untaglist ((k, x) :: (rest as (k', _) :: _)) =
   137       if k = k' then untaglist rest
   138       else x :: untaglist rest;
   139 
   140 fun orderlist brls =
   141   untaglist (sort (prod_ord int_ord int_ord o pairself fst) brls);
   142 
   143 fun orderlist_no_weight brls =
   144   untaglist (sort (int_ord o pairself (snd o fst)) brls);
   145 
   146 fun may_unify weighted t net =
   147   map snd ((if weighted then orderlist else orderlist_no_weight) (Net.unify_term net t));
   148 
   149 fun find_erules _ [] = K []
   150   | find_erules w (fact :: _) = may_unify w (Logic.strip_assums_concl (Thm.prop_of fact));
   151 
   152 fun find_irules w goal = may_unify w (Logic.strip_assums_concl goal);
   153 
   154 fun find_rules_netpair weighted facts goal (inet, enet) =
   155   find_erules weighted facts enet @ find_irules weighted goal inet;
   156 
   157 fun find_rules weighted facts goals =
   158   map (find_rules_netpair weighted facts goals) o netpairs;
   159 
   160 
   161 (* wrappers *)
   162 
   163 fun gen_add_wrapper upd w =
   164   Context.theory_map (Rules.map (fn Rules {next, rules, netpairs, wrappers} =>
   165     make_rules next rules netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers)));
   166 
   167 val addSWrapper = gen_add_wrapper Library.apfst;
   168 val addWrapper = gen_add_wrapper Library.apsnd;
   169 
   170 
   171 fun gen_wrap which ctxt =
   172   let val Rules {wrappers, ...} = Rules.get (Context.Proof ctxt)
   173   in fold_rev fst (which wrappers) end;
   174 
   175 val Swrap = gen_wrap #1;
   176 val wrap = gen_wrap #2;
   177 
   178 
   179 
   180 (** attributes **)
   181 
   182 (* add and del rules *)
   183 
   184 
   185 val rule_del = Thm.declaration_attribute (fn th => Rules.map (del_rule th));
   186 
   187 fun rule_add k view opt_w =
   188   Thm.declaration_attribute (fn th => Rules.map (add_rule k opt_w (view th) o del_rule th));
   189 
   190 val intro_bang  = rule_add intro_bangK I;
   191 val elim_bang   = rule_add elim_bangK I;
   192 val dest_bang   = rule_add elim_bangK Tactic.make_elim;
   193 val intro       = rule_add introK I;
   194 val elim        = rule_add elimK I;
   195 val dest        = rule_add elimK Tactic.make_elim;
   196 val intro_query = rule_add intro_queryK I;
   197 val elim_query  = rule_add elim_queryK I;
   198 val dest_query  = rule_add elim_queryK Tactic.make_elim;
   199 
   200 val _ = Context.>> (Context.map_theory
   201   (snd o Global_Theory.add_thms [((Binding.empty, Drule.equal_intr_rule), [intro_query NONE])]));
   202 
   203 
   204 (* concrete syntax *)
   205 
   206 fun add a b c x =
   207   (Scan.lift ((Args.bang >> K a || Args.query >> K c || Scan.succeed b) --
   208     Scan.option Parse.nat) >> (fn (f, n) => f n)) x;
   209 
   210 val _ = Context.>> (Context.map_theory
   211  (Attrib.setup (Binding.name "intro") (add intro_bang intro intro_query)
   212     "declaration of introduction rule" #>
   213   Attrib.setup (Binding.name "elim") (add elim_bang elim elim_query)
   214     "declaration of elimination rule" #>
   215   Attrib.setup (Binding.name "dest") (add dest_bang dest dest_query)
   216     "declaration of destruction rule" #>
   217   Attrib.setup (Binding.name "rule") (Scan.lift Args.del >> K rule_del)
   218     "remove declaration of intro/elim/dest rule"));
   219 
   220 end;