src/Pure/Isar/context_rules.ML
author wenzelm
Fri Apr 08 16:34:14 2011 +0200 (2011-04-08)
changeset 42290 b1f544c84040
parent 39557 fe5722fce758
child 45375 7fe19930dfc9
permissions -rw-r--r--
discontinued special treatment of structure Lexicon;
     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_rule 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 structure Rules = Generic_Data
    93 (
    94   type T = rules;
    95   val empty = make_rules ~1 [] empty_netpairs ([], []);
    96   val extend = I;
    97   fun merge
    98     (Rules {rules = rules1, wrappers = (ws1, ws1'), ...},
    99       Rules {rules = rules2, wrappers = (ws2, ws2'), ...}) =
   100     let
   101       val wrappers =
   102         (Library.merge (eq_snd (op =)) (ws1, ws2), Library.merge (eq_snd (op =)) (ws1', ws2'));
   103       val rules = Library.merge (fn ((_, (k1, th1)), (_, (k2, th2))) =>
   104           k1 = k2 andalso Thm.eq_thm_prop (th1, th2)) (rules1, rules2);
   105       val next = ~ (length rules);
   106       val netpairs = fold (fn (n, (w, ((i, b), th))) =>
   107           nth_map i (Tactic.insert_tagged_brl ((w, n), (b, th))))
   108         (next upto ~1 ~~ rules) empty_netpairs;
   109     in make_rules (next - 1) rules netpairs wrappers end;
   110 );
   111 
   112 fun print_rules ctxt =
   113   let
   114     val Rules {rules, ...} = Rules.get (Context.Proof ctxt);
   115     fun prt_kind (i, b) =
   116       Pretty.big_list ((the o AList.lookup (op =) kind_names) (i, b) ^ ":")
   117         (map_filter (fn (_, (k, th)) =>
   118             if k = (i, b) then SOME (Display.pretty_thm ctxt th) else NONE)
   119           (sort (int_ord o pairself fst) rules));
   120   in Pretty.writeln (Pretty.chunks (map prt_kind rule_kinds)) end;
   121 
   122 
   123 (* access data *)
   124 
   125 fun netpairs ctxt = let val Rules {netpairs, ...} = Rules.get (Context.Proof ctxt) in netpairs end;
   126 val netpair_bang = hd o netpairs;
   127 val netpair = hd o tl o netpairs;
   128 
   129 
   130 (* retrieving rules *)
   131 
   132 fun untaglist [] = []
   133   | untaglist [(_ : int * int, x)] = [x]
   134   | untaglist ((k, x) :: (rest as (k', _) :: _)) =
   135       if k = k' then untaglist rest
   136       else x :: untaglist rest;
   137 
   138 fun orderlist brls =
   139   untaglist (sort (prod_ord int_ord int_ord o pairself fst) brls);
   140 
   141 fun orderlist_no_weight brls =
   142   untaglist (sort (int_ord o pairself (snd o fst)) brls);
   143 
   144 fun may_unify weighted t net =
   145   map snd ((if weighted then orderlist else orderlist_no_weight) (Net.unify_term net t));
   146 
   147 fun find_erules _ [] = K []
   148   | find_erules w (fact :: _) = may_unify w (Logic.strip_assums_concl (Thm.prop_of fact));
   149 
   150 fun find_irules w goal = may_unify w (Logic.strip_assums_concl goal);
   151 
   152 fun find_rules_netpair weighted facts goal (inet, enet) =
   153   find_erules weighted facts enet @ find_irules weighted goal inet;
   154 
   155 fun find_rules weighted facts goals =
   156   map (find_rules_netpair weighted facts goals) o netpairs;
   157 
   158 
   159 (* wrappers *)
   160 
   161 fun gen_add_wrapper upd w =
   162   Context.theory_map (Rules.map (fn Rules {next, rules, netpairs, wrappers} =>
   163     make_rules next rules netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers)));
   164 
   165 val addSWrapper = gen_add_wrapper Library.apfst;
   166 val addWrapper = gen_add_wrapper Library.apsnd;
   167 
   168 
   169 fun gen_wrap which ctxt =
   170   let val Rules {wrappers, ...} = Rules.get (Context.Proof ctxt)
   171   in fold_rev fst (which wrappers) end;
   172 
   173 val Swrap = gen_wrap #1;
   174 val wrap = gen_wrap #2;
   175 
   176 
   177 
   178 (** attributes **)
   179 
   180 (* add and del rules *)
   181 
   182 fun rule_del (x, th) =
   183   (Rules.map (del_rule th o del_rule (Tactic.make_elim th)) x, th);
   184 
   185 fun rule_add k view opt_w =
   186   (fn (x, th) => (Rules.map (add_rule k opt_w (view th)) x, th)) o rule_del;
   187 
   188 val intro_bang  = rule_add intro_bangK I;
   189 val elim_bang   = rule_add elim_bangK I;
   190 val dest_bang   = rule_add elim_bangK Tactic.make_elim;
   191 val intro       = rule_add introK I;
   192 val elim        = rule_add elimK I;
   193 val dest        = rule_add elimK Tactic.make_elim;
   194 val intro_query = rule_add intro_queryK I;
   195 val elim_query  = rule_add elim_queryK I;
   196 val dest_query  = rule_add elim_queryK Tactic.make_elim;
   197 
   198 val _ = Context.>> (Context.map_theory
   199   (snd o Global_Theory.add_thms [((Binding.empty, Drule.equal_intr_rule), [intro_query NONE])]));
   200 
   201 
   202 (* concrete syntax *)
   203 
   204 fun add a b c x =
   205   (Scan.lift ((Args.bang >> K a || Args.query >> K c || Scan.succeed b) --
   206     Scan.option Parse.nat) >> (fn (f, n) => f n)) x;
   207 
   208 val _ = Context.>> (Context.map_theory
   209  (Attrib.setup (Binding.name "intro") (add intro_bang intro intro_query)
   210     "declaration of introduction rule" #>
   211   Attrib.setup (Binding.name "elim") (add elim_bang elim elim_query)
   212     "declaration of elimination rule" #>
   213   Attrib.setup (Binding.name "dest") (add dest_bang dest dest_query)
   214     "declaration of destruction rule" #>
   215   Attrib.setup (Binding.name "rule") (Scan.lift Args.del >> K rule_del)
   216     "remove declaration of intro/elim/dest rule"));
   217 
   218 end;