src/Pure/Isar/context_rules.ML
author wenzelm
Tue May 07 14:26:32 2002 +0200 (2002-05-07 ago)
changeset 13105 3d1e7a199bdc
parent 12805 3be853cf19cf
child 13372 18790d503fe0
permissions -rw-r--r--
use eq_thm_prop instead of slightly inadequate eq_thm;
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
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@12350
     5
wenzelm@12350
     6
Declarations of intro/elim/dest rules in Pure; see
wenzelm@12350
     7
Provers/classical.ML for a more specialized version of the same idea.
wenzelm@12350
     8
*)
wenzelm@12350
     9
wenzelm@12350
    10
signature CONTEXT_RULES =
wenzelm@12350
    11
sig
wenzelm@12350
    12
  type netpair
wenzelm@12350
    13
  type T
wenzelm@12350
    14
  val netpair_bang: Proof.context -> netpair
wenzelm@12350
    15
  val netpair: Proof.context -> netpair
wenzelm@12350
    16
  val orderlist: ((int * int) * 'a) list -> 'a list
wenzelm@12399
    17
  val find_rules_netpair: bool -> thm list -> term -> netpair -> thm list
wenzelm@12399
    18
  val find_rules: bool -> thm list -> term -> Proof.context -> thm list list
wenzelm@12350
    19
  val print_global_rules: theory -> unit
wenzelm@12350
    20
  val print_local_rules: Proof.context -> unit
wenzelm@12350
    21
  val addSWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory
wenzelm@12350
    22
  val addWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory
wenzelm@12350
    23
  val Swrap: Proof.context -> (int -> tactic) -> int -> tactic
wenzelm@12350
    24
  val wrap: Proof.context -> (int -> tactic) -> int -> tactic
wenzelm@12350
    25
  val intro_bang_global: int option -> theory attribute
wenzelm@12350
    26
  val elim_bang_global: int option -> theory attribute
wenzelm@12350
    27
  val dest_bang_global: int option -> theory attribute
wenzelm@12350
    28
  val intro_global: int option -> theory attribute
wenzelm@12350
    29
  val elim_global: int option -> theory attribute
wenzelm@12350
    30
  val dest_global: int option -> theory attribute
wenzelm@12350
    31
  val intro_query_global: int option -> theory attribute
wenzelm@12350
    32
  val elim_query_global: int option -> theory attribute
wenzelm@12350
    33
  val dest_query_global: int option -> theory attribute
wenzelm@12350
    34
  val rule_del_global: theory attribute
wenzelm@12350
    35
  val intro_bang_local: int option -> Proof.context attribute
wenzelm@12350
    36
  val elim_bang_local: int option -> Proof.context attribute
wenzelm@12350
    37
  val dest_bang_local: int option -> Proof.context attribute
wenzelm@12350
    38
  val intro_local: int option -> Proof.context attribute
wenzelm@12350
    39
  val elim_local: int option -> Proof.context attribute
wenzelm@12350
    40
  val dest_local: int option -> Proof.context attribute
wenzelm@12350
    41
  val intro_query_local: int option -> Proof.context attribute
wenzelm@12350
    42
  val elim_query_local: int option -> Proof.context attribute
wenzelm@12350
    43
  val dest_query_local: int option -> Proof.context attribute
wenzelm@12350
    44
  val rule_del_local: Proof.context attribute
wenzelm@12380
    45
  val addXIs_global: theory * thm list -> theory
wenzelm@12380
    46
  val addXEs_global: theory * thm list -> theory
wenzelm@12380
    47
  val addXDs_global: theory * thm list -> theory
wenzelm@12380
    48
  val delrules_global: theory * thm list -> theory
wenzelm@12380
    49
  val addXIs_local: Proof.context * thm list -> Proof.context
wenzelm@12380
    50
  val addXEs_local: Proof.context * thm list -> Proof.context
wenzelm@12380
    51
  val addXDs_local: Proof.context * thm list -> Proof.context
wenzelm@12380
    52
  val delrules_local: Proof.context * thm list -> Proof.context
wenzelm@12350
    53
  val setup: (theory -> theory) list
wenzelm@12350
    54
end;
wenzelm@12350
    55
wenzelm@12350
    56
structure ContextRules: CONTEXT_RULES =
wenzelm@12350
    57
struct
wenzelm@12350
    58
wenzelm@12350
    59
wenzelm@12350
    60
(** rule declaration contexts **)
wenzelm@12350
    61
wenzelm@12350
    62
(* rule kinds *)
wenzelm@12350
    63
wenzelm@12350
    64
val intro_bangK = (0, false);
wenzelm@12350
    65
val elim_bangK = (0, true);
wenzelm@12350
    66
val introK = (1, false);
wenzelm@12350
    67
val elimK = (1, true);
wenzelm@12350
    68
val intro_queryK = (2, false);
wenzelm@12350
    69
val elim_queryK = (2, true);
wenzelm@12350
    70
wenzelm@12350
    71
val kind_names =
wenzelm@12350
    72
 [(intro_bangK, "safe introduction rules (intro!)"),
wenzelm@12350
    73
  (elim_bangK, "safe elimination rules (elim!)"),
wenzelm@12350
    74
  (introK, "introduction rules (intro)"),
wenzelm@12350
    75
  (elimK, "elimination rules (elim)"),
wenzelm@12350
    76
  (intro_queryK, "extra introduction rules (intro?)"),
wenzelm@12350
    77
  (elim_queryK, "extra elimination rules (elim?)")];
wenzelm@12350
    78
wenzelm@12350
    79
val rule_kinds = map #1 kind_names;
wenzelm@12350
    80
val rule_indexes = distinct (map #1 rule_kinds);
wenzelm@12350
    81
wenzelm@12350
    82
wenzelm@12350
    83
(* raw data *)
wenzelm@12350
    84
wenzelm@12350
    85
type netpair = ((int * int) * (bool * thm)) Net.net * ((int * int) * (bool * thm)) Net.net;
wenzelm@12350
    86
val empty_netpairs: netpair list = replicate (length rule_indexes) (Net.empty, Net.empty);
wenzelm@12350
    87
wenzelm@12350
    88
datatype T = Rules of
wenzelm@12350
    89
 {next: int,
wenzelm@12350
    90
  rules: (int * ((int * bool) * thm)) list,
wenzelm@12350
    91
  netpairs: netpair list,
wenzelm@12350
    92
  wrappers: (((int -> tactic) -> int -> tactic) * stamp) list *
wenzelm@12350
    93
    (((int -> tactic) -> int -> tactic) * stamp) list};
wenzelm@12350
    94
wenzelm@12350
    95
fun make_rules next rules netpairs wrappers =
wenzelm@12350
    96
  Rules {next = next, rules = rules, netpairs = netpairs, wrappers = wrappers};
wenzelm@12350
    97
wenzelm@12350
    98
fun add_rule (i, b) opt_w th (Rules {next, rules, netpairs, wrappers}) =
wenzelm@12350
    99
  let val w = (case opt_w of Some w => w | None => Tactic.subgoals_of_brl (b, th)) in
wenzelm@12350
   100
    make_rules (next - 1) ((w, ((i, b), th)) :: rules)
wenzelm@12350
   101
      (map_nth_elem i (curry insert_tagged_brl ((w, next), (b, th))) netpairs) wrappers
wenzelm@12350
   102
  end;
wenzelm@12350
   103
wenzelm@12350
   104
fun del_rule th (rs as Rules {next, rules, netpairs, wrappers}) =
wenzelm@12350
   105
  let
wenzelm@13105
   106
    fun eq_th (_, (_, th')) = Drule.eq_thm_prop (th, th');
wenzelm@12350
   107
    fun del b netpair = delete_tagged_brl ((b, th), netpair) handle Net.DELETE => netpair;
wenzelm@12350
   108
  in
wenzelm@12350
   109
    if not (exists eq_th rules) then rs
wenzelm@12350
   110
    else make_rules next (filter_out eq_th rules) (map (del false o del true) netpairs) wrappers
wenzelm@12350
   111
  end;
wenzelm@12350
   112
wenzelm@12350
   113
fun print_rules prt x (Rules {rules, ...}) =
wenzelm@12350
   114
  let
wenzelm@12350
   115
    fun prt_kind (i, b) =
wenzelm@12350
   116
      Pretty.big_list (the (assoc (kind_names, (i, b))) ^ ":")
wenzelm@12350
   117
        (mapfilter (fn (_, (k, th)) => if k = (i, b) then Some (prt x th) else None)
wenzelm@12350
   118
          (sort (int_ord o pairself fst) rules));
wenzelm@12350
   119
  in Pretty.writeln (Pretty.chunks (map prt_kind rule_kinds)) end;
wenzelm@12350
   120
wenzelm@12350
   121
wenzelm@12350
   122
(* theory and proof data *)
wenzelm@12350
   123
wenzelm@12350
   124
structure GlobalRulesArgs =
wenzelm@12350
   125
struct
wenzelm@12350
   126
  val name = "Isar/rule_context";
wenzelm@12350
   127
  type T = T;
wenzelm@12350
   128
wenzelm@12350
   129
  val empty = make_rules ~1 [] empty_netpairs ([], []);
wenzelm@12350
   130
  val copy = I;
wenzelm@12350
   131
  val prep_ext = I;
wenzelm@12350
   132
wenzelm@12350
   133
  fun merge (Rules {rules = rules1, wrappers = (ws1, ws1'), ...},
wenzelm@12350
   134
      Rules {rules = rules2, wrappers = (ws2, ws2'), ...}) =
wenzelm@12350
   135
    let
wenzelm@12350
   136
      val wrappers = (gen_merge_lists' eq_snd ws1 ws2, gen_merge_lists' eq_snd ws1' ws2');
wenzelm@12350
   137
      val rules = gen_merge_lists' (fn ((_, (k1, th1)), (_, (k2, th2))) =>
wenzelm@13105
   138
          k1 = k2 andalso Drule.eq_thm_prop (th1, th2)) rules1 rules2;
wenzelm@12350
   139
      val next = ~ (length rules);
wenzelm@12350
   140
      val netpairs = foldl (fn (nps, (n, (w, ((i, b), th)))) =>
wenzelm@12350
   141
          map_nth_elem i (curry insert_tagged_brl ((w, n), (b, th))) nps)
wenzelm@12350
   142
        (empty_netpairs, next upto ~1 ~~ rules);
wenzelm@12350
   143
    in make_rules (next - 1) rules netpairs wrappers end;
wenzelm@12350
   144
wenzelm@12350
   145
  val print = print_rules Display.pretty_thm_sg;
wenzelm@12350
   146
end;
wenzelm@12350
   147
wenzelm@12350
   148
structure GlobalRules = TheoryDataFun(GlobalRulesArgs);
wenzelm@12350
   149
val print_global_rules = GlobalRules.print;
wenzelm@12350
   150
wenzelm@12350
   151
structure LocalRulesArgs =
wenzelm@12350
   152
struct
wenzelm@12350
   153
  val name = GlobalRulesArgs.name;
wenzelm@12350
   154
  type T = GlobalRulesArgs.T;
wenzelm@12350
   155
  val init = GlobalRules.get;
wenzelm@12350
   156
  val print = print_rules ProofContext.pretty_thm;
wenzelm@12350
   157
end;
wenzelm@12350
   158
wenzelm@12350
   159
structure LocalRules = ProofDataFun(LocalRulesArgs);
wenzelm@12350
   160
val print_local_rules = LocalRules.print;
wenzelm@12350
   161
wenzelm@12350
   162
wenzelm@12350
   163
(* access data *)
wenzelm@12350
   164
wenzelm@12350
   165
fun netpairs ctxt = let val Rules {netpairs, ...} = LocalRules.get ctxt in netpairs end;
wenzelm@12350
   166
val netpair_bang = hd o netpairs;
wenzelm@12350
   167
val netpair = hd o tl o netpairs;
wenzelm@12350
   168
wenzelm@12350
   169
wenzelm@12399
   170
(* retrieving rules *)
wenzelm@12399
   171
wenzelm@12350
   172
fun untaglist [] = []
wenzelm@12350
   173
  | untaglist [(k : int * int, x)] = [x]
wenzelm@12350
   174
  | untaglist ((k, x) :: (rest as (k', x') :: _)) =
wenzelm@12350
   175
      if k = k' then untaglist rest
wenzelm@12350
   176
      else x :: untaglist rest;
wenzelm@12350
   177
wenzelm@12350
   178
fun orderlist brls = untaglist (sort (prod_ord int_ord int_ord o pairself fst) brls);
wenzelm@12350
   179
fun orderlist_no_weight brls = untaglist (sort (int_ord o pairself (snd o fst)) brls);
wenzelm@12399
   180
wenzelm@12399
   181
fun may_unify weighted t net =
wenzelm@12399
   182
  map snd ((if weighted then orderlist else orderlist_no_weight) (Net.unify_term net t));
wenzelm@12399
   183
wenzelm@12399
   184
fun find_erules _ [] = K []
wenzelm@12805
   185
  | find_erules w (fact :: _) = may_unify w (Logic.strip_assums_concl (Thm.prop_of fact));
wenzelm@12399
   186
fun find_irules w goal = may_unify w (Logic.strip_assums_concl goal);
wenzelm@12380
   187
wenzelm@12399
   188
fun find_rules_netpair weighted facts goal (inet, enet) =
wenzelm@12399
   189
  find_erules weighted facts enet @ find_irules weighted goal inet;
wenzelm@12380
   190
wenzelm@12399
   191
fun find_rules weighted facts goals = map (find_rules_netpair weighted facts goals) o netpairs;
wenzelm@12350
   192
wenzelm@12350
   193
wenzelm@12399
   194
(* wrappers *)
wenzelm@12399
   195
wenzelm@12350
   196
fun gen_add_wrapper upd w = GlobalRules.map (fn (rs as Rules {next, rules, netpairs, wrappers}) =>
wenzelm@12350
   197
  make_rules next rules netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers));
wenzelm@12350
   198
wenzelm@12350
   199
val addSWrapper = gen_add_wrapper Library.apfst;
wenzelm@12350
   200
val addWrapper = gen_add_wrapper Library.apsnd;
wenzelm@12350
   201
wenzelm@12350
   202
wenzelm@12350
   203
fun gen_wrap which ctxt =
wenzelm@12350
   204
  let val Rules {wrappers, ...} = LocalRules.get ctxt
wenzelm@12350
   205
  in fn tac => foldr (fn ((w, _), t) => w t) (which wrappers, tac) end;
wenzelm@12350
   206
wenzelm@12350
   207
val Swrap = gen_wrap #1;
wenzelm@12350
   208
val wrap = gen_wrap #2;
wenzelm@12350
   209
wenzelm@12350
   210
wenzelm@12350
   211
wenzelm@12350
   212
(** attributes **)
wenzelm@12350
   213
wenzelm@12350
   214
(* add and del rules *)
wenzelm@12350
   215
wenzelm@12350
   216
local
wenzelm@12350
   217
wenzelm@12350
   218
fun del map_data (x, th) =
wenzelm@12350
   219
  (map_data (del_rule th o del_rule (Tactic.make_elim th)) x, th);
wenzelm@12350
   220
wenzelm@12350
   221
fun add k view map_data opt_w =
wenzelm@12412
   222
  (fn (x, th) => (map_data (add_rule k opt_w (view th)) x, th)) o del map_data;
wenzelm@12350
   223
wenzelm@12350
   224
in
wenzelm@12350
   225
wenzelm@12350
   226
val intro_bang_global  = add intro_bangK I GlobalRules.map;
wenzelm@12350
   227
val elim_bang_global   = add elim_bangK I GlobalRules.map;
wenzelm@12350
   228
val dest_bang_global   = add elim_bangK Tactic.make_elim GlobalRules.map;
wenzelm@12350
   229
val intro_global       = add introK I GlobalRules.map;
wenzelm@12350
   230
val elim_global        = add elimK I GlobalRules.map;
wenzelm@12350
   231
val dest_global        = add elimK Tactic.make_elim GlobalRules.map;
wenzelm@12350
   232
val intro_query_global = add intro_queryK I GlobalRules.map;
wenzelm@12350
   233
val elim_query_global  = add elim_queryK I GlobalRules.map;
wenzelm@12350
   234
val dest_query_global  = add elim_queryK Tactic.make_elim GlobalRules.map;
wenzelm@12350
   235
val rule_del_global    = del GlobalRules.map;
wenzelm@12350
   236
wenzelm@12350
   237
val intro_bang_local   = add intro_bangK I LocalRules.map;
wenzelm@12350
   238
val elim_bang_local    = add elim_bangK I LocalRules.map;
wenzelm@12350
   239
val dest_bang_local    = add elim_bangK Tactic.make_elim LocalRules.map;
wenzelm@12350
   240
val intro_local        = add introK I LocalRules.map;
wenzelm@12350
   241
val elim_local         = add elimK I LocalRules.map;
wenzelm@12350
   242
val dest_local         = add elimK Tactic.make_elim LocalRules.map;
wenzelm@12350
   243
val intro_query_local  = add intro_queryK I LocalRules.map;
wenzelm@12350
   244
val elim_query_local   = add elim_queryK I LocalRules.map;
wenzelm@12350
   245
val dest_query_local   = add elim_queryK Tactic.make_elim LocalRules.map;
wenzelm@12350
   246
val rule_del_local     = del LocalRules.map;
wenzelm@12350
   247
wenzelm@12350
   248
end;
wenzelm@12350
   249
wenzelm@12350
   250
wenzelm@12380
   251
(* low-level modifiers *)
wenzelm@12380
   252
wenzelm@12380
   253
fun modifier att (x, ths) =
wenzelm@12380
   254
  #1 (Thm.applys_attributes ((x, rev ths), [att]));
wenzelm@12380
   255
wenzelm@12380
   256
val addXIs_global = modifier (intro_query_global None);
wenzelm@12380
   257
val addXEs_global = modifier (elim_query_global None);
wenzelm@12380
   258
val addXDs_global = modifier (dest_query_global None);
wenzelm@12380
   259
val delrules_global = modifier rule_del_global;
wenzelm@12380
   260
wenzelm@12380
   261
val addXIs_local = modifier (intro_query_local None);
wenzelm@12380
   262
val addXEs_local = modifier (elim_query_local None);
wenzelm@12380
   263
val addXDs_local = modifier (dest_query_local None);
wenzelm@12380
   264
val delrules_local = modifier rule_del_local;
wenzelm@12380
   265
wenzelm@12380
   266
wenzelm@12350
   267
(* concrete syntax *)
wenzelm@12350
   268
wenzelm@12350
   269
fun add_args a b c x = Attrib.syntax
wenzelm@12380
   270
  (Scan.lift ((Args.bang >> K a || Args.query >> K c || Scan.succeed b) -- (Scan.option Args.nat))
wenzelm@12380
   271
    >> (fn (f, n) => f n)) x;
wenzelm@12350
   272
wenzelm@12350
   273
fun del_args att = Attrib.syntax (Scan.lift Args.del >> K att);
wenzelm@12350
   274
wenzelm@12350
   275
wenzelm@12350
   276
val rule_atts =
wenzelm@12350
   277
 [("intro",
wenzelm@12350
   278
   (add_args intro_bang_global intro_global intro_query_global,
wenzelm@12350
   279
    add_args intro_bang_local intro_local intro_query_local),
wenzelm@12350
   280
    "declaration of introduction rule"),
wenzelm@12350
   281
  ("elim",
wenzelm@12350
   282
   (add_args elim_bang_global elim_global elim_query_global,
wenzelm@12350
   283
    add_args elim_bang_local elim_local elim_query_local),
wenzelm@12350
   284
    "declaration of elimination rule"),
wenzelm@12350
   285
  ("dest",
wenzelm@12350
   286
   (add_args dest_bang_global dest_global dest_query_global,
wenzelm@12350
   287
    add_args dest_bang_local dest_local dest_query_local),
wenzelm@12350
   288
    "declaration of destruction rule"),
wenzelm@12350
   289
  ("rule", (del_args rule_del_global, del_args rule_del_local),
wenzelm@12350
   290
    "remove declaration of intro/elim/dest rule")];
wenzelm@12350
   291
wenzelm@12350
   292
wenzelm@12350
   293
wenzelm@12350
   294
(** theory setup **)
wenzelm@12350
   295
wenzelm@12350
   296
val setup =
wenzelm@12350
   297
 [GlobalRules.init, LocalRules.init, Attrib.add_attributes rule_atts];
wenzelm@12350
   298
wenzelm@12350
   299
wenzelm@12350
   300
end;