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