src/Pure/Isar/context_rules.ML
author wenzelm
Mon Mar 31 12:35:39 2014 +0200 (2014-03-31 ago)
changeset 56334 6b3739fee456
parent 56204 f70e69208a8c
child 59058 a78612c67ec0
permissions -rw-r--r--
some shortcuts for chunks, which sometimes avoid bulky string output;
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@33370
    10
  type netpair = ((int * int) * (bool * thm)) Net.net * ((int * int) * (bool * thm)) Net.net
wenzelm@20289
    11
  val netpair_bang: Proof.context -> netpair
wenzelm@20289
    12
  val netpair: Proof.context -> netpair
wenzelm@12350
    13
  val orderlist: ((int * int) * 'a) list -> 'a list
wenzelm@12399
    14
  val find_rules_netpair: bool -> thm list -> term -> netpair -> thm list
wenzelm@20289
    15
  val find_rules: bool -> thm list -> term -> Proof.context -> thm list list
wenzelm@21506
    16
  val print_rules: Proof.context -> unit
wenzelm@51798
    17
  val addSWrapper: (Proof.context -> (int -> tactic) -> int -> tactic) -> theory -> theory
wenzelm@51798
    18
  val addWrapper: (Proof.context -> (int -> tactic) -> int -> tactic) -> theory -> theory
wenzelm@20289
    19
  val Swrap: Proof.context -> (int -> tactic) -> int -> tactic
wenzelm@20289
    20
  val wrap: Proof.context -> (int -> tactic) -> int -> tactic
wenzelm@18728
    21
  val intro_bang: int option -> attribute
wenzelm@18728
    22
  val elim_bang: int option -> attribute
wenzelm@18728
    23
  val dest_bang: int option -> attribute
wenzelm@18728
    24
  val intro: int option -> attribute
wenzelm@18728
    25
  val elim: int option -> attribute
wenzelm@18728
    26
  val dest: int option -> attribute
wenzelm@18728
    27
  val intro_query: int option -> attribute
wenzelm@18728
    28
  val elim_query: int option -> attribute
wenzelm@18728
    29
  val dest_query: int option -> attribute
wenzelm@18728
    30
  val rule_del: attribute
wenzelm@30529
    31
  val add: (int option -> attribute) -> (int option -> attribute) -> (int option -> attribute) ->
wenzelm@30529
    32
    attribute context_parser
wenzelm@12350
    33
end;
wenzelm@12350
    34
wenzelm@33369
    35
structure Context_Rules: CONTEXT_RULES =
wenzelm@12350
    36
struct
wenzelm@12350
    37
wenzelm@12350
    38
wenzelm@12350
    39
(** rule declaration contexts **)
wenzelm@12350
    40
wenzelm@12350
    41
(* rule kinds *)
wenzelm@12350
    42
wenzelm@12350
    43
val intro_bangK = (0, false);
wenzelm@12350
    44
val elim_bangK = (0, true);
wenzelm@12350
    45
val introK = (1, false);
wenzelm@12350
    46
val elimK = (1, true);
wenzelm@12350
    47
val intro_queryK = (2, false);
wenzelm@12350
    48
val elim_queryK = (2, true);
wenzelm@12350
    49
wenzelm@12350
    50
val kind_names =
wenzelm@12350
    51
 [(intro_bangK, "safe introduction rules (intro!)"),
wenzelm@12350
    52
  (elim_bangK, "safe elimination rules (elim!)"),
wenzelm@12350
    53
  (introK, "introduction rules (intro)"),
wenzelm@12350
    54
  (elimK, "elimination rules (elim)"),
wenzelm@12350
    55
  (intro_queryK, "extra introduction rules (intro?)"),
wenzelm@12350
    56
  (elim_queryK, "extra elimination rules (elim?)")];
wenzelm@12350
    57
wenzelm@12350
    58
val rule_kinds = map #1 kind_names;
wenzelm@19046
    59
val rule_indexes = distinct (op =) (map #1 rule_kinds);
wenzelm@12350
    60
wenzelm@12350
    61
wenzelm@18637
    62
(* context data *)
wenzelm@12350
    63
wenzelm@12350
    64
type netpair = ((int * int) * (bool * thm)) Net.net * ((int * int) * (bool * thm)) Net.net;
wenzelm@12350
    65
val empty_netpairs: netpair list = replicate (length rule_indexes) (Net.empty, Net.empty);
wenzelm@12350
    66
wenzelm@33370
    67
datatype rules = Rules of
wenzelm@12350
    68
 {next: int,
wenzelm@12350
    69
  rules: (int * ((int * bool) * thm)) list,
wenzelm@12350
    70
  netpairs: netpair list,
wenzelm@51798
    71
  wrappers:
wenzelm@51798
    72
    ((Proof.context -> (int -> tactic) -> int -> tactic) * stamp) list *
wenzelm@51798
    73
    ((Proof.context -> (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@45375
    84
fun del_rule0 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@45375
    93
fun del_rule th = del_rule0 th o del_rule0 (Tactic.make_elim th);
wenzelm@45375
    94
wenzelm@33519
    95
structure Rules = Generic_Data
wenzelm@18637
    96
(
wenzelm@33370
    97
  type T = rules;
wenzelm@12350
    98
  val empty = make_rules ~1 [] empty_netpairs ([], []);
wenzelm@16424
    99
  val extend = I;
wenzelm@33519
   100
  fun merge
wenzelm@33519
   101
    (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@51584
   121
            if k = (i, b) then SOME (Display.pretty_thm_item ctxt th) else NONE)
wenzelm@22846
   122
          (sort (int_ord o pairself fst) rules));
wenzelm@56334
   123
  in Pretty.writeln_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@32784
   136
  | untaglist [(_ : int * int, x)] = [x]
wenzelm@32784
   137
  | untaglist ((k, x) :: (rest as (k', _) :: _)) =
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@32784
   165
  Context.theory_map (Rules.map (fn 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@51798
   174
  in fold_rev (fn (w, _) => w ctxt) (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@45375
   185
wenzelm@45375
   186
val rule_del = Thm.declaration_attribute (fn th => Rules.map (del_rule th));
wenzelm@12350
   187
wenzelm@18637
   188
fun rule_add k view opt_w =
wenzelm@45375
   189
  Thm.declaration_attribute (fn th => Rules.map (add_rule k opt_w (view th) o del_rule th));
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@53171
   201
val _ = Theory.setup
wenzelm@53171
   202
  (snd o Global_Theory.add_thms [((Binding.empty, Drule.equal_intr_rule), [intro_query NONE])]);
wenzelm@18637
   203
wenzelm@12350
   204
wenzelm@18637
   205
(* concrete syntax *)
wenzelm@18637
   206
wenzelm@30529
   207
fun add a b c x =
wenzelm@27809
   208
  (Scan.lift ((Args.bang >> K a || Args.query >> K c || Scan.succeed b) --
wenzelm@36950
   209
    Scan.option Parse.nat) >> (fn (f, n) => f n)) x;
wenzelm@18637
   210
wenzelm@53171
   211
val _ = Theory.setup
wenzelm@56204
   212
 (Attrib.setup @{binding intro} (add intro_bang intro intro_query)
wenzelm@30529
   213
    "declaration of introduction rule" #>
wenzelm@56204
   214
  Attrib.setup @{binding elim} (add elim_bang elim elim_query)
wenzelm@30529
   215
    "declaration of elimination rule" #>
wenzelm@56204
   216
  Attrib.setup @{binding dest} (add dest_bang dest dest_query)
wenzelm@30529
   217
    "declaration of destruction rule" #>
wenzelm@56204
   218
  Attrib.setup @{binding rule} (Scan.lift Args.del >> K rule_del)
wenzelm@53171
   219
    "remove declaration of intro/elim/dest rule");
wenzelm@12350
   220
wenzelm@12350
   221
end;