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