src/Pure/Isar/context_rules.ML
author wenzelm
Sat Jan 21 23:02:14 2006 +0100 (2006-01-21 ago)
changeset 18728 6790126ab5f6
parent 18708 4b3dadb4fe33
child 18874 05585eee8d74
permissions -rw-r--r--
simplified type attribute;
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@13372
    13
  val netpair_bang: ProofContext.context -> netpair
wenzelm@13372
    14
  val netpair: ProofContext.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@13372
    17
  val find_rules: bool -> thm list -> term -> ProofContext.context -> thm list list
wenzelm@18637
    18
  val print_rules: Context.generic -> unit
wenzelm@12350
    19
  val addSWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory
wenzelm@12350
    20
  val addWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory
wenzelm@13372
    21
  val Swrap: ProofContext.context -> (int -> tactic) -> int -> tactic
wenzelm@13372
    22
  val wrap: ProofContext.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@12350
    62
val rule_indexes = distinct (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)
haftmann@18011
    83
      (nth_map i (curry 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@13105
    88
    fun eq_th (_, (_, th')) = Drule.eq_thm_prop (th, th');
wenzelm@12350
    89
    fun del b netpair = 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@17511
    97
  val name = "Isar/rules";
wenzelm@12350
    98
  type T = T;
wenzelm@12350
    99
wenzelm@12350
   100
  val empty = make_rules ~1 [] empty_netpairs ([], []);
wenzelm@16424
   101
  val extend = I;
wenzelm@12350
   102
wenzelm@16424
   103
  fun merge _ (Rules {rules = rules1, wrappers = (ws1, ws1'), ...},
wenzelm@12350
   104
      Rules {rules = rules2, wrappers = (ws2, ws2'), ...}) =
wenzelm@12350
   105
    let
wenzelm@18637
   106
      val wrappers =
wenzelm@18637
   107
        (gen_merge_lists' (eq_snd (op =)) ws1 ws2, gen_merge_lists' (eq_snd (op =)) ws1' ws2');
wenzelm@12350
   108
      val rules = gen_merge_lists' (fn ((_, (k1, th1)), (_, (k2, th2))) =>
wenzelm@13105
   109
          k1 = k2 andalso Drule.eq_thm_prop (th1, th2)) rules1 rules2;
wenzelm@12350
   110
      val next = ~ (length rules);
skalberg@15570
   111
      val netpairs = Library.foldl (fn (nps, (n, (w, ((i, b), th)))) =>
haftmann@18011
   112
          nth_map i (curry insert_tagged_brl ((w, n), (b, th))) nps)
wenzelm@12350
   113
        (empty_netpairs, next upto ~1 ~~ rules);
wenzelm@18637
   114
    in make_rules (next - 1) rules netpairs wrappers end
wenzelm@12350
   115
wenzelm@18637
   116
  fun print generic (Rules {rules, ...}) =
wenzelm@18637
   117
    let
wenzelm@18637
   118
      val ctxt = Context.proof_of generic;
wenzelm@18637
   119
      fun prt_kind (i, b) =
wenzelm@18637
   120
        Pretty.big_list ((the o AList.lookup (op =) kind_names) (i, b) ^ ":")
wenzelm@18637
   121
          (List.mapPartial (fn (_, (k, th)) =>
wenzelm@18637
   122
              if k = (i, b) then SOME (ProofContext.pretty_thm ctxt th) else NONE)
wenzelm@18637
   123
            (sort (int_ord o pairself fst) rules));
wenzelm@18637
   124
    in Pretty.writeln (Pretty.chunks (map prt_kind rule_kinds)) end;
wenzelm@18637
   125
);
wenzelm@12350
   126
wenzelm@18708
   127
val _ = Context.add_setup Rules.init;
wenzelm@18637
   128
val print_rules = Rules.print;
wenzelm@12350
   129
wenzelm@12350
   130
wenzelm@12350
   131
(* access data *)
wenzelm@12350
   132
wenzelm@18637
   133
fun netpairs ctxt = let val Rules {netpairs, ...} = Rules.get (Context.Proof ctxt) in netpairs end;
wenzelm@12350
   134
val netpair_bang = hd o netpairs;
wenzelm@12350
   135
val netpair = hd o tl o netpairs;
wenzelm@12350
   136
wenzelm@12350
   137
wenzelm@12399
   138
(* retrieving rules *)
wenzelm@12399
   139
wenzelm@12350
   140
fun untaglist [] = []
wenzelm@12350
   141
  | untaglist [(k : int * int, x)] = [x]
wenzelm@12350
   142
  | untaglist ((k, x) :: (rest as (k', x') :: _)) =
wenzelm@12350
   143
      if k = k' then untaglist rest
wenzelm@12350
   144
      else x :: untaglist rest;
wenzelm@12350
   145
wenzelm@16424
   146
fun orderlist brls =
wenzelm@16512
   147
  untaglist (sort (prod_ord int_ord int_ord o pairself fst) brls);
wenzelm@16424
   148
wenzelm@16424
   149
fun orderlist_no_weight brls =
wenzelm@16512
   150
  untaglist (sort (int_ord o pairself (snd o fst)) brls);
wenzelm@12399
   151
wenzelm@12399
   152
fun may_unify weighted t net =
wenzelm@12399
   153
  map snd ((if weighted then orderlist else orderlist_no_weight) (Net.unify_term net t));
wenzelm@12399
   154
wenzelm@12399
   155
fun find_erules _ [] = K []
wenzelm@12805
   156
  | find_erules w (fact :: _) = may_unify w (Logic.strip_assums_concl (Thm.prop_of fact));
wenzelm@16424
   157
wenzelm@12399
   158
fun find_irules w goal = may_unify w (Logic.strip_assums_concl goal);
wenzelm@12380
   159
wenzelm@12399
   160
fun find_rules_netpair weighted facts goal (inet, enet) =
wenzelm@12399
   161
  find_erules weighted facts enet @ find_irules weighted goal inet;
wenzelm@12380
   162
wenzelm@16424
   163
fun find_rules weighted facts goals =
wenzelm@16424
   164
  map (find_rules_netpair weighted facts goals) o netpairs;
wenzelm@12350
   165
wenzelm@12350
   166
wenzelm@12399
   167
(* wrappers *)
wenzelm@12399
   168
wenzelm@18667
   169
fun gen_add_wrapper upd w =
wenzelm@18728
   170
  Context.theory_map (Rules.map (fn (rs as Rules {next, rules, netpairs, wrappers}) =>
wenzelm@18667
   171
    make_rules next rules netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers)));
wenzelm@12350
   172
wenzelm@12350
   173
val addSWrapper = gen_add_wrapper Library.apfst;
wenzelm@12350
   174
val addWrapper = gen_add_wrapper Library.apsnd;
wenzelm@12350
   175
wenzelm@12350
   176
wenzelm@12350
   177
fun gen_wrap which ctxt =
wenzelm@18637
   178
  let val Rules {wrappers, ...} = Rules.get (Context.Proof ctxt)
wenzelm@17351
   179
  in fold_rev fst (which wrappers) end;
wenzelm@12350
   180
wenzelm@12350
   181
val Swrap = gen_wrap #1;
wenzelm@12350
   182
val wrap = gen_wrap #2;
wenzelm@12350
   183
wenzelm@12350
   184
wenzelm@12350
   185
wenzelm@12350
   186
(** attributes **)
wenzelm@12350
   187
wenzelm@12350
   188
(* add and del rules *)
wenzelm@12350
   189
wenzelm@18637
   190
fun rule_del (x, th) =
wenzelm@18637
   191
  (Rules.map (del_rule th o del_rule (Tactic.make_elim th)) x, th);
wenzelm@12350
   192
wenzelm@18637
   193
fun rule_add k view opt_w =
wenzelm@18637
   194
  (fn (x, th) => (Rules.map (add_rule k opt_w (view th)) x, th)) o rule_del;
wenzelm@12350
   195
wenzelm@18637
   196
val intro_bang  = rule_add intro_bangK I;
wenzelm@18637
   197
val elim_bang   = rule_add elim_bangK I;
wenzelm@18637
   198
val dest_bang   = rule_add elim_bangK Tactic.make_elim;
wenzelm@18637
   199
val intro       = rule_add introK I;
wenzelm@18637
   200
val elim        = rule_add elimK I;
wenzelm@18637
   201
val dest        = rule_add elimK Tactic.make_elim;
wenzelm@18637
   202
val intro_query = rule_add intro_queryK I;
wenzelm@18637
   203
val elim_query  = rule_add elim_queryK I;
wenzelm@18637
   204
val dest_query  = rule_add elim_queryK Tactic.make_elim;
wenzelm@12350
   205
wenzelm@18637
   206
val _ = Context.add_setup
wenzelm@18728
   207
  (snd o PureThy.add_thms [(("", Drule.equal_intr_rule), [intro_query NONE])]);
wenzelm@18637
   208
wenzelm@12350
   209
wenzelm@18637
   210
(* concrete syntax *)
wenzelm@18637
   211
wenzelm@18637
   212
fun add_args a b c x = Attrib.syntax
wenzelm@18692
   213
  (Scan.lift ((Args.bang >> K a || Args.query >> K c || Scan.succeed b) -- Scan.option Args.nat)
wenzelm@18637
   214
    >> (fn (f, n) => f n)) x;
wenzelm@18637
   215
wenzelm@18637
   216
val rule_atts =
wenzelm@18728
   217
 [("intro", add_args intro_bang intro intro_query, "declaration of introduction rule"),
wenzelm@18728
   218
  ("elim", add_args elim_bang elim elim_query, "declaration of elimination rule"),
wenzelm@18728
   219
  ("dest", add_args dest_bang dest dest_query, "declaration of destruction rule"),
wenzelm@18728
   220
  ("rule", Attrib.syntax (Scan.lift Args.del >> K rule_del),
wenzelm@18637
   221
    "remove declaration of intro/elim/dest rule")];
wenzelm@18637
   222
wenzelm@18708
   223
val _ = Context.add_setup (Attrib.add_attributes rule_atts);
wenzelm@12350
   224
wenzelm@12350
   225
end;