src/Pure/Isar/context_rules.ML
author wenzelm
Mon May 03 14:25:56 2010 +0200 (2010-05-03)
changeset 36610 bafd82950e24
parent 33519 e31a85f92ce9
child 36950 75b8f26f2f07
permissions -rw-r--r--
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
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@12350
    17
  val addSWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory
wenzelm@12350
    18
  val addWrapper: ((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@12350
    71
  wrappers: (((int -> tactic) -> int -> tactic) * stamp) list *
wenzelm@12350
    72
    (((int -> tactic) -> int -> tactic) * stamp) list};
wenzelm@12350
    73
wenzelm@12350
    74
fun make_rules next rules netpairs wrappers =
wenzelm@12350
    75
  Rules {next = next, rules = rules, netpairs = netpairs, wrappers = wrappers};
wenzelm@12350
    76
wenzelm@12350
    77
fun add_rule (i, b) opt_w th (Rules {next, rules, netpairs, wrappers}) =
skalberg@15531
    78
  let val w = (case opt_w of SOME w => w | NONE => Tactic.subgoals_of_brl (b, th)) in
wenzelm@12350
    79
    make_rules (next - 1) ((w, ((i, b), th)) :: rules)
wenzelm@23227
    80
      (nth_map i (Tactic.insert_tagged_brl ((w, next), (b, th))) netpairs) wrappers
wenzelm@12350
    81
  end;
wenzelm@12350
    82
wenzelm@12350
    83
fun del_rule th (rs as Rules {next, rules, netpairs, wrappers}) =
wenzelm@12350
    84
  let
wenzelm@22360
    85
    fun eq_th (_, (_, th')) = Thm.eq_thm_prop (th, th');
wenzelm@23227
    86
    fun del b netpair = Tactic.delete_tagged_brl (b, th) netpair handle Net.DELETE => netpair;
wenzelm@12350
    87
  in
wenzelm@12350
    88
    if not (exists eq_th rules) then rs
wenzelm@12350
    89
    else make_rules next (filter_out eq_th rules) (map (del false o del true) netpairs) wrappers
wenzelm@12350
    90
  end;
wenzelm@12350
    91
wenzelm@33519
    92
structure Rules = Generic_Data
wenzelm@18637
    93
(
wenzelm@33370
    94
  type T = rules;
wenzelm@12350
    95
  val empty = make_rules ~1 [] empty_netpairs ([], []);
wenzelm@16424
    96
  val extend = I;
wenzelm@33519
    97
  fun merge
wenzelm@33519
    98
    (Rules {rules = rules1, wrappers = (ws1, ws1'), ...},
wenzelm@12350
    99
      Rules {rules = rules2, wrappers = (ws2, ws2'), ...}) =
wenzelm@12350
   100
    let
wenzelm@18637
   101
      val wrappers =
wenzelm@18921
   102
        (Library.merge (eq_snd (op =)) (ws1, ws2), Library.merge (eq_snd (op =)) (ws1', ws2'));
wenzelm@18921
   103
      val rules = Library.merge (fn ((_, (k1, th1)), (_, (k2, th2))) =>
wenzelm@22360
   104
          k1 = k2 andalso Thm.eq_thm_prop (th1, th2)) (rules1, rules2);
wenzelm@12350
   105
      val next = ~ (length rules);
wenzelm@19473
   106
      val netpairs = fold (fn (n, (w, ((i, b), th))) =>
wenzelm@23227
   107
          nth_map i (Tactic.insert_tagged_brl ((w, n), (b, th))))
wenzelm@19473
   108
        (next upto ~1 ~~ rules) empty_netpairs;
wenzelm@23227
   109
    in make_rules (next - 1) rules netpairs wrappers end;
wenzelm@18637
   110
);
wenzelm@12350
   111
wenzelm@22846
   112
fun print_rules ctxt =
wenzelm@22846
   113
  let
wenzelm@22846
   114
    val Rules {rules, ...} = Rules.get (Context.Proof ctxt);
wenzelm@22846
   115
    fun prt_kind (i, b) =
wenzelm@22846
   116
      Pretty.big_list ((the o AList.lookup (op =) kind_names) (i, b) ^ ":")
wenzelm@22846
   117
        (map_filter (fn (_, (k, th)) =>
wenzelm@32091
   118
            if k = (i, b) then SOME (Display.pretty_thm ctxt th) else NONE)
wenzelm@22846
   119
          (sort (int_ord o pairself fst) rules));
wenzelm@22846
   120
  in Pretty.writeln (Pretty.chunks (map prt_kind rule_kinds)) end;
wenzelm@12350
   121
wenzelm@12350
   122
wenzelm@12350
   123
(* access data *)
wenzelm@12350
   124
wenzelm@18637
   125
fun netpairs ctxt = let val Rules {netpairs, ...} = Rules.get (Context.Proof ctxt) in netpairs end;
wenzelm@12350
   126
val netpair_bang = hd o netpairs;
wenzelm@12350
   127
val netpair = hd o tl o netpairs;
wenzelm@12350
   128
wenzelm@12350
   129
wenzelm@12399
   130
(* retrieving rules *)
wenzelm@12399
   131
wenzelm@12350
   132
fun untaglist [] = []
wenzelm@32784
   133
  | untaglist [(_ : int * int, x)] = [x]
wenzelm@32784
   134
  | untaglist ((k, x) :: (rest as (k', _) :: _)) =
wenzelm@12350
   135
      if k = k' then untaglist rest
wenzelm@12350
   136
      else x :: untaglist rest;
wenzelm@12350
   137
wenzelm@16424
   138
fun orderlist brls =
wenzelm@16512
   139
  untaglist (sort (prod_ord int_ord int_ord o pairself fst) brls);
wenzelm@16424
   140
wenzelm@16424
   141
fun orderlist_no_weight brls =
wenzelm@16512
   142
  untaglist (sort (int_ord o pairself (snd o fst)) brls);
wenzelm@12399
   143
wenzelm@12399
   144
fun may_unify weighted t net =
wenzelm@12399
   145
  map snd ((if weighted then orderlist else orderlist_no_weight) (Net.unify_term net t));
wenzelm@12399
   146
wenzelm@12399
   147
fun find_erules _ [] = K []
wenzelm@12805
   148
  | find_erules w (fact :: _) = may_unify w (Logic.strip_assums_concl (Thm.prop_of fact));
wenzelm@16424
   149
wenzelm@12399
   150
fun find_irules w goal = may_unify w (Logic.strip_assums_concl goal);
wenzelm@12380
   151
wenzelm@12399
   152
fun find_rules_netpair weighted facts goal (inet, enet) =
wenzelm@12399
   153
  find_erules weighted facts enet @ find_irules weighted goal inet;
wenzelm@12380
   154
wenzelm@16424
   155
fun find_rules weighted facts goals =
wenzelm@16424
   156
  map (find_rules_netpair weighted facts goals) o netpairs;
wenzelm@12350
   157
wenzelm@12350
   158
wenzelm@12399
   159
(* wrappers *)
wenzelm@12399
   160
wenzelm@18667
   161
fun gen_add_wrapper upd w =
wenzelm@32784
   162
  Context.theory_map (Rules.map (fn Rules {next, rules, netpairs, wrappers} =>
wenzelm@18667
   163
    make_rules next rules netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers)));
wenzelm@12350
   164
wenzelm@12350
   165
val addSWrapper = gen_add_wrapper Library.apfst;
wenzelm@12350
   166
val addWrapper = gen_add_wrapper Library.apsnd;
wenzelm@12350
   167
wenzelm@12350
   168
wenzelm@12350
   169
fun gen_wrap which ctxt =
wenzelm@18637
   170
  let val Rules {wrappers, ...} = Rules.get (Context.Proof ctxt)
wenzelm@17351
   171
  in fold_rev fst (which wrappers) end;
wenzelm@12350
   172
wenzelm@12350
   173
val Swrap = gen_wrap #1;
wenzelm@12350
   174
val wrap = gen_wrap #2;
wenzelm@12350
   175
wenzelm@12350
   176
wenzelm@12350
   177
wenzelm@12350
   178
(** attributes **)
wenzelm@12350
   179
wenzelm@12350
   180
(* add and del rules *)
wenzelm@12350
   181
wenzelm@18637
   182
fun rule_del (x, th) =
wenzelm@18637
   183
  (Rules.map (del_rule th o del_rule (Tactic.make_elim th)) x, th);
wenzelm@12350
   184
wenzelm@18637
   185
fun rule_add k view opt_w =
wenzelm@18637
   186
  (fn (x, th) => (Rules.map (add_rule k opt_w (view th)) x, th)) o rule_del;
wenzelm@12350
   187
wenzelm@18637
   188
val intro_bang  = rule_add intro_bangK I;
wenzelm@18637
   189
val elim_bang   = rule_add elim_bangK I;
wenzelm@18637
   190
val dest_bang   = rule_add elim_bangK Tactic.make_elim;
wenzelm@18637
   191
val intro       = rule_add introK I;
wenzelm@18637
   192
val elim        = rule_add elimK I;
wenzelm@18637
   193
val dest        = rule_add elimK Tactic.make_elim;
wenzelm@18637
   194
val intro_query = rule_add intro_queryK I;
wenzelm@18637
   195
val elim_query  = rule_add elim_queryK I;
wenzelm@18637
   196
val dest_query  = rule_add elim_queryK Tactic.make_elim;
wenzelm@12350
   197
wenzelm@26463
   198
val _ = Context.>> (Context.map_theory
haftmann@29582
   199
  (snd o PureThy.add_thms [((Binding.empty, Drule.equal_intr_rule), [intro_query NONE])]));
wenzelm@18637
   200
wenzelm@12350
   201
wenzelm@18637
   202
(* concrete syntax *)
wenzelm@18637
   203
wenzelm@30529
   204
fun add a b c x =
wenzelm@27809
   205
  (Scan.lift ((Args.bang >> K a || Args.query >> K c || Scan.succeed b) --
wenzelm@30529
   206
    Scan.option OuterParse.nat) >> (fn (f, n) => f n)) x;
wenzelm@18637
   207
wenzelm@30529
   208
val _ = Context.>> (Context.map_theory
wenzelm@30529
   209
 (Attrib.setup (Binding.name "intro") (add intro_bang intro intro_query)
wenzelm@30529
   210
    "declaration of introduction rule" #>
wenzelm@30529
   211
  Attrib.setup (Binding.name "elim") (add elim_bang elim elim_query)
wenzelm@30529
   212
    "declaration of elimination rule" #>
wenzelm@30529
   213
  Attrib.setup (Binding.name "dest") (add dest_bang dest dest_query)
wenzelm@30529
   214
    "declaration of destruction rule" #>
wenzelm@30529
   215
  Attrib.setup (Binding.name "rule") (Scan.lift Args.del >> K rule_del)
wenzelm@30529
   216
    "remove declaration of intro/elim/dest rule"));
wenzelm@12350
   217
wenzelm@12350
   218
end;