src/Pure/Isar/context_rules.ML
author wenzelm
Fri Jan 13 01:13:05 2006 +0100 (2006-01-13 ago)
changeset 18667 85d04c28224a
parent 18637 33a6f6caa617
child 18692 2270e25e9128
permissions -rw-r--r--
tuned;
     1 (*  Title:      Pure/Isar/context_rules.ML
     2     ID:         $Id$
     3     Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
     4 
     5 Declarations of intro/elim/dest rules in Pure; see
     6 Provers/classical.ML for a more specialized version of the same idea.
     7 *)
     8 
     9 signature CONTEXT_RULES =
    10 sig
    11   type netpair
    12   type T
    13   val netpair_bang: ProofContext.context -> netpair
    14   val netpair: ProofContext.context -> netpair
    15   val orderlist: ((int * int) * 'a) list -> 'a list
    16   val find_rules_netpair: bool -> thm list -> term -> netpair -> thm list
    17   val find_rules: bool -> thm list -> term -> ProofContext.context -> thm list list
    18   val print_rules: Context.generic -> unit
    19   val addSWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory
    20   val addWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory
    21   val Swrap: ProofContext.context -> (int -> tactic) -> int -> tactic
    22   val wrap: ProofContext.context -> (int -> tactic) -> int -> tactic
    23   val intro_bang: int option -> Context.generic attribute
    24   val elim_bang: int option -> Context.generic attribute
    25   val dest_bang: int option -> Context.generic attribute
    26   val intro: int option -> Context.generic attribute
    27   val elim: int option -> Context.generic attribute
    28   val dest: int option -> Context.generic attribute
    29   val intro_query: int option -> Context.generic attribute
    30   val elim_query: int option -> Context.generic attribute
    31   val dest_query: int option -> Context.generic attribute
    32   val rule_del: Context.generic attribute
    33 end;
    34 
    35 structure ContextRules: 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 (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 T = 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 (curry 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')) = Drule.eq_thm_prop (th, th');
    86     fun del b netpair = 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 = GenericDataFun
    93 (
    94   val name = "Isar/rules";
    95   type T = T;
    96 
    97   val empty = make_rules ~1 [] empty_netpairs ([], []);
    98   val extend = I;
    99 
   100   fun merge _ (Rules {rules = rules1, wrappers = (ws1, ws1'), ...},
   101       Rules {rules = rules2, wrappers = (ws2, ws2'), ...}) =
   102     let
   103       val wrappers =
   104         (gen_merge_lists' (eq_snd (op =)) ws1 ws2, gen_merge_lists' (eq_snd (op =)) ws1' ws2');
   105       val rules = gen_merge_lists' (fn ((_, (k1, th1)), (_, (k2, th2))) =>
   106           k1 = k2 andalso Drule.eq_thm_prop (th1, th2)) rules1 rules2;
   107       val next = ~ (length rules);
   108       val netpairs = Library.foldl (fn (nps, (n, (w, ((i, b), th)))) =>
   109           nth_map i (curry insert_tagged_brl ((w, n), (b, th))) nps)
   110         (empty_netpairs, next upto ~1 ~~ rules);
   111     in make_rules (next - 1) rules netpairs wrappers end
   112 
   113   fun print generic (Rules {rules, ...}) =
   114     let
   115       val ctxt = Context.proof_of generic;
   116       fun prt_kind (i, b) =
   117         Pretty.big_list ((the o AList.lookup (op =) kind_names) (i, b) ^ ":")
   118           (List.mapPartial (fn (_, (k, th)) =>
   119               if k = (i, b) then SOME (ProofContext.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 val _ = Context.add_setup [Rules.init];
   125 val print_rules = Rules.print;
   126 
   127 
   128 (* access data *)
   129 
   130 fun netpairs ctxt = let val Rules {netpairs, ...} = Rules.get (Context.Proof ctxt) in netpairs end;
   131 val netpair_bang = hd o netpairs;
   132 val netpair = hd o tl o netpairs;
   133 
   134 
   135 (* retrieving rules *)
   136 
   137 fun untaglist [] = []
   138   | untaglist [(k : int * int, x)] = [x]
   139   | untaglist ((k, x) :: (rest as (k', x') :: _)) =
   140       if k = k' then untaglist rest
   141       else x :: untaglist rest;
   142 
   143 fun orderlist brls =
   144   untaglist (sort (prod_ord int_ord int_ord o pairself fst) brls);
   145 
   146 fun orderlist_no_weight brls =
   147   untaglist (sort (int_ord o pairself (snd o fst)) brls);
   148 
   149 fun may_unify weighted t net =
   150   map snd ((if weighted then orderlist else orderlist_no_weight) (Net.unify_term net t));
   151 
   152 fun find_erules _ [] = K []
   153   | find_erules w (fact :: _) = may_unify w (Logic.strip_assums_concl (Thm.prop_of fact));
   154 
   155 fun find_irules w goal = may_unify w (Logic.strip_assums_concl goal);
   156 
   157 fun find_rules_netpair weighted facts goal (inet, enet) =
   158   find_erules weighted facts enet @ find_irules weighted goal inet;
   159 
   160 fun find_rules weighted facts goals =
   161   map (find_rules_netpair weighted facts goals) o netpairs;
   162 
   163 
   164 (* wrappers *)
   165 
   166 fun gen_add_wrapper upd w =
   167   Context.map_theory (Rules.map (fn (rs as Rules {next, rules, netpairs, wrappers}) =>
   168     make_rules next rules netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers)));
   169 
   170 val addSWrapper = gen_add_wrapper Library.apfst;
   171 val addWrapper = gen_add_wrapper Library.apsnd;
   172 
   173 
   174 fun gen_wrap which ctxt =
   175   let val Rules {wrappers, ...} = Rules.get (Context.Proof ctxt)
   176   in fold_rev fst (which wrappers) end;
   177 
   178 val Swrap = gen_wrap #1;
   179 val wrap = gen_wrap #2;
   180 
   181 
   182 
   183 (** attributes **)
   184 
   185 (* add and del rules *)
   186 
   187 fun rule_del (x, th) =
   188   (Rules.map (del_rule th o del_rule (Tactic.make_elim th)) x, th);
   189 
   190 fun rule_add k view opt_w =
   191   (fn (x, th) => (Rules.map (add_rule k opt_w (view th)) x, th)) o rule_del;
   192 
   193 val intro_bang  = rule_add intro_bangK I;
   194 val elim_bang   = rule_add elim_bangK I;
   195 val dest_bang   = rule_add elim_bangK Tactic.make_elim;
   196 val intro       = rule_add introK I;
   197 val elim        = rule_add elimK I;
   198 val dest        = rule_add elimK Tactic.make_elim;
   199 val intro_query = rule_add intro_queryK I;
   200 val elim_query  = rule_add elim_queryK I;
   201 val dest_query  = rule_add elim_queryK Tactic.make_elim;
   202 
   203 val _ = Context.add_setup
   204   [snd o PureThy.add_thms [(("", Drule.equal_intr_rule), [Attrib.theory (intro_query NONE)])]];
   205 
   206 
   207 (* concrete syntax *)
   208 
   209 fun add_args a b c x = Attrib.syntax
   210   (Scan.lift ((Args.bang >> K a || Args.query >> K c || Scan.succeed b) -- (Scan.option Args.nat))
   211     >> (fn (f, n) => f n)) x;
   212 
   213 fun del_args att = Attrib.syntax (Scan.lift Args.del >> K att);
   214 
   215 val rule_atts =
   216  [("intro", Attrib.common (add_args intro_bang intro intro_query),
   217     "declaration of introduction rule"),
   218   ("elim", Attrib.common (add_args elim_bang elim elim_query),
   219     "declaration of elimination rule"),
   220   ("dest", Attrib.common (add_args dest_bang dest dest_query),
   221     "declaration of destruction rule"),
   222   ("rule", Attrib.common (del_args rule_del),
   223     "remove declaration of intro/elim/dest rule")];
   224 
   225 val _ = Context.add_setup [Attrib.add_attributes rule_atts];
   226 
   227 end;