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