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