src/Pure/Isar/context_rules.ML
author haftmann
Fri Oct 28 16:35:40 2005 +0200 (2005-10-28 ago)
changeset 18011 685d95c793ff
parent 17511 51314f4bd01d
child 18377 0e1d025d57b3
permissions -rw-r--r--
cleaned up nth, nth_update, nth_map and nth_string functions
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@12350
     5
Declarations of intro/elim/dest rules in Pure; see
wenzelm@12350
     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@12350
    18
  val print_global_rules: theory -> unit
wenzelm@13372
    19
  val print_local_rules: ProofContext.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@13372
    22
  val Swrap: ProofContext.context -> (int -> tactic) -> int -> tactic
wenzelm@13372
    23
  val wrap: ProofContext.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@13372
    34
  val intro_bang_local: int option -> ProofContext.context attribute
wenzelm@13372
    35
  val elim_bang_local: int option -> ProofContext.context attribute
wenzelm@13372
    36
  val dest_bang_local: int option -> ProofContext.context attribute
wenzelm@13372
    37
  val intro_local: int option -> ProofContext.context attribute
wenzelm@13372
    38
  val elim_local: int option -> ProofContext.context attribute
wenzelm@13372
    39
  val dest_local: int option -> ProofContext.context attribute
wenzelm@13372
    40
  val intro_query_local: int option -> ProofContext.context attribute
wenzelm@13372
    41
  val elim_query_local: int option -> ProofContext.context attribute
wenzelm@13372
    42
  val dest_query_local: int option -> ProofContext.context attribute
wenzelm@13372
    43
  val rule_del_local: ProofContext.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@13372
    48
  val addXIs_local: ProofContext.context * thm list -> ProofContext.context
wenzelm@13372
    49
  val addXEs_local: ProofContext.context * thm list -> ProofContext.context
wenzelm@13372
    50
  val addXDs_local: ProofContext.context * thm list -> ProofContext.context
wenzelm@13372
    51
  val delrules_local: ProofContext.context * thm list -> ProofContext.context
wenzelm@12350
    52
end;
wenzelm@12350
    53
wenzelm@12350
    54
structure ContextRules: CONTEXT_RULES =
wenzelm@12350
    55
struct
wenzelm@12350
    56
wenzelm@12350
    57
wenzelm@12350
    58
(** rule declaration contexts **)
wenzelm@12350
    59
wenzelm@12350
    60
(* rule kinds *)
wenzelm@12350
    61
wenzelm@12350
    62
val intro_bangK = (0, false);
wenzelm@12350
    63
val elim_bangK = (0, true);
wenzelm@12350
    64
val introK = (1, false);
wenzelm@12350
    65
val elimK = (1, true);
wenzelm@12350
    66
val intro_queryK = (2, false);
wenzelm@12350
    67
val elim_queryK = (2, true);
wenzelm@12350
    68
wenzelm@12350
    69
val kind_names =
wenzelm@12350
    70
 [(intro_bangK, "safe introduction rules (intro!)"),
wenzelm@12350
    71
  (elim_bangK, "safe elimination rules (elim!)"),
wenzelm@12350
    72
  (introK, "introduction rules (intro)"),
wenzelm@12350
    73
  (elimK, "elimination rules (elim)"),
wenzelm@12350
    74
  (intro_queryK, "extra introduction rules (intro?)"),
wenzelm@12350
    75
  (elim_queryK, "extra elimination rules (elim?)")];
wenzelm@12350
    76
wenzelm@12350
    77
val rule_kinds = map #1 kind_names;
wenzelm@12350
    78
val rule_indexes = distinct (map #1 rule_kinds);
wenzelm@12350
    79
wenzelm@12350
    80
wenzelm@12350
    81
(* raw data *)
wenzelm@12350
    82
wenzelm@12350
    83
type netpair = ((int * int) * (bool * thm)) Net.net * ((int * int) * (bool * thm)) Net.net;
wenzelm@12350
    84
val empty_netpairs: netpair list = replicate (length rule_indexes) (Net.empty, Net.empty);
wenzelm@12350
    85
wenzelm@12350
    86
datatype T = Rules of
wenzelm@12350
    87
 {next: int,
wenzelm@12350
    88
  rules: (int * ((int * bool) * thm)) list,
wenzelm@12350
    89
  netpairs: netpair list,
wenzelm@12350
    90
  wrappers: (((int -> tactic) -> int -> tactic) * stamp) list *
wenzelm@12350
    91
    (((int -> tactic) -> int -> tactic) * stamp) list};
wenzelm@12350
    92
wenzelm@12350
    93
fun make_rules next rules netpairs wrappers =
wenzelm@12350
    94
  Rules {next = next, rules = rules, netpairs = netpairs, wrappers = wrappers};
wenzelm@12350
    95
wenzelm@12350
    96
fun add_rule (i, b) opt_w th (Rules {next, rules, netpairs, wrappers}) =
skalberg@15531
    97
  let val w = (case opt_w of SOME w => w | NONE => Tactic.subgoals_of_brl (b, th)) in
wenzelm@12350
    98
    make_rules (next - 1) ((w, ((i, b), th)) :: rules)
haftmann@18011
    99
      (nth_map i (curry insert_tagged_brl ((w, next), (b, th))) netpairs) wrappers
wenzelm@12350
   100
  end;
wenzelm@12350
   101
wenzelm@12350
   102
fun del_rule th (rs as Rules {next, rules, netpairs, wrappers}) =
wenzelm@12350
   103
  let
wenzelm@13105
   104
    fun eq_th (_, (_, th')) = Drule.eq_thm_prop (th, th');
wenzelm@12350
   105
    fun del b netpair = delete_tagged_brl ((b, th), netpair) handle Net.DELETE => netpair;
wenzelm@12350
   106
  in
wenzelm@12350
   107
    if not (exists eq_th rules) then rs
wenzelm@12350
   108
    else make_rules next (filter_out eq_th rules) (map (del false o del true) netpairs) wrappers
wenzelm@12350
   109
  end;
wenzelm@12350
   110
wenzelm@12350
   111
fun print_rules prt x (Rules {rules, ...}) =
wenzelm@12350
   112
  let
wenzelm@12350
   113
    fun prt_kind (i, b) =
haftmann@17314
   114
      Pretty.big_list ((the o AList.lookup (op =) kind_names) (i, b) ^ ":")
skalberg@15570
   115
        (List.mapPartial (fn (_, (k, th)) => if k = (i, b) then SOME (prt x th) else NONE)
wenzelm@16512
   116
          (sort (int_ord o pairself fst) rules));
wenzelm@12350
   117
  in Pretty.writeln (Pretty.chunks (map prt_kind rule_kinds)) end;
wenzelm@12350
   118
wenzelm@12350
   119
wenzelm@12350
   120
(* theory and proof data *)
wenzelm@12350
   121
wenzelm@12350
   122
structure GlobalRulesArgs =
wenzelm@12350
   123
struct
wenzelm@17511
   124
  val name = "Isar/rules";
wenzelm@12350
   125
  type T = T;
wenzelm@12350
   126
wenzelm@12350
   127
  val empty = make_rules ~1 [] empty_netpairs ([], []);
wenzelm@12350
   128
  val copy = I;
wenzelm@16424
   129
  val extend = I;
wenzelm@12350
   130
wenzelm@16424
   131
  fun merge _ (Rules {rules = rules1, wrappers = (ws1, ws1'), ...},
wenzelm@12350
   132
      Rules {rules = rules2, wrappers = (ws2, ws2'), ...}) =
wenzelm@12350
   133
    let
haftmann@17496
   134
      val wrappers = (gen_merge_lists' (eq_snd (op =)) ws1 ws2, gen_merge_lists' (eq_snd (op =)) ws1' ws2');
wenzelm@12350
   135
      val rules = gen_merge_lists' (fn ((_, (k1, th1)), (_, (k2, th2))) =>
wenzelm@13105
   136
          k1 = k2 andalso Drule.eq_thm_prop (th1, th2)) rules1 rules2;
wenzelm@12350
   137
      val next = ~ (length rules);
skalberg@15570
   138
      val netpairs = Library.foldl (fn (nps, (n, (w, ((i, b), th)))) =>
haftmann@18011
   139
          nth_map i (curry insert_tagged_brl ((w, n), (b, th))) nps)
wenzelm@12350
   140
        (empty_netpairs, next upto ~1 ~~ rules);
wenzelm@12350
   141
    in make_rules (next - 1) rules netpairs wrappers end;
wenzelm@12350
   142
wenzelm@12350
   143
  val print = print_rules Display.pretty_thm_sg;
wenzelm@12350
   144
end;
wenzelm@12350
   145
wenzelm@12350
   146
structure GlobalRules = TheoryDataFun(GlobalRulesArgs);
wenzelm@15801
   147
val _ = Context.add_setup [GlobalRules.init];
wenzelm@12350
   148
val print_global_rules = GlobalRules.print;
wenzelm@12350
   149
wenzelm@16424
   150
structure LocalRules = ProofDataFun
wenzelm@16424
   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@16424
   156
end);
wenzelm@12350
   157
wenzelm@15801
   158
val _ = Context.add_setup [LocalRules.init];
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@12399
   169
(* retrieving rules *)
wenzelm@12399
   170
wenzelm@12350
   171
fun untaglist [] = []
wenzelm@12350
   172
  | untaglist [(k : int * int, x)] = [x]
wenzelm@12350
   173
  | untaglist ((k, x) :: (rest as (k', x') :: _)) =
wenzelm@12350
   174
      if k = k' then untaglist rest
wenzelm@12350
   175
      else x :: untaglist rest;
wenzelm@12350
   176
wenzelm@16424
   177
fun orderlist brls =
wenzelm@16512
   178
  untaglist (sort (prod_ord int_ord int_ord o pairself fst) brls);
wenzelm@16424
   179
wenzelm@16424
   180
fun orderlist_no_weight brls =
wenzelm@16512
   181
  untaglist (sort (int_ord o pairself (snd o fst)) brls);
wenzelm@12399
   182
wenzelm@12399
   183
fun may_unify weighted t net =
wenzelm@12399
   184
  map snd ((if weighted then orderlist else orderlist_no_weight) (Net.unify_term net t));
wenzelm@12399
   185
wenzelm@12399
   186
fun find_erules _ [] = K []
wenzelm@12805
   187
  | find_erules w (fact :: _) = may_unify w (Logic.strip_assums_concl (Thm.prop_of fact));
wenzelm@16424
   188
wenzelm@12399
   189
fun find_irules w goal = may_unify w (Logic.strip_assums_concl goal);
wenzelm@12380
   190
wenzelm@12399
   191
fun find_rules_netpair weighted facts goal (inet, enet) =
wenzelm@12399
   192
  find_erules weighted facts enet @ find_irules weighted goal inet;
wenzelm@12380
   193
wenzelm@16424
   194
fun find_rules weighted facts goals =
wenzelm@16424
   195
  map (find_rules_netpair weighted facts goals) o netpairs;
wenzelm@12350
   196
wenzelm@12350
   197
wenzelm@12399
   198
(* wrappers *)
wenzelm@12399
   199
wenzelm@12350
   200
fun gen_add_wrapper upd w = GlobalRules.map (fn (rs as Rules {next, rules, netpairs, wrappers}) =>
wenzelm@12350
   201
  make_rules next rules netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers));
wenzelm@12350
   202
wenzelm@12350
   203
val addSWrapper = gen_add_wrapper Library.apfst;
wenzelm@12350
   204
val addWrapper = gen_add_wrapper Library.apsnd;
wenzelm@12350
   205
wenzelm@12350
   206
wenzelm@12350
   207
fun gen_wrap which ctxt =
wenzelm@12350
   208
  let val Rules {wrappers, ...} = LocalRules.get ctxt
wenzelm@17351
   209
  in fold_rev fst (which wrappers) end;
wenzelm@12350
   210
wenzelm@12350
   211
val Swrap = gen_wrap #1;
wenzelm@12350
   212
val wrap = gen_wrap #2;
wenzelm@12350
   213
wenzelm@12350
   214
wenzelm@12350
   215
wenzelm@12350
   216
(** attributes **)
wenzelm@12350
   217
wenzelm@12350
   218
(* add and del rules *)
wenzelm@12350
   219
wenzelm@12350
   220
local
wenzelm@12350
   221
wenzelm@12350
   222
fun del map_data (x, th) =
wenzelm@12350
   223
  (map_data (del_rule th o del_rule (Tactic.make_elim th)) x, th);
wenzelm@12350
   224
wenzelm@12350
   225
fun add k view map_data opt_w =
wenzelm@12412
   226
  (fn (x, th) => (map_data (add_rule k opt_w (view th)) x, th)) o del map_data;
wenzelm@12350
   227
wenzelm@12350
   228
in
wenzelm@12350
   229
wenzelm@12350
   230
val intro_bang_global  = add intro_bangK I GlobalRules.map;
wenzelm@12350
   231
val elim_bang_global   = add elim_bangK I GlobalRules.map;
wenzelm@12350
   232
val dest_bang_global   = add elim_bangK Tactic.make_elim GlobalRules.map;
wenzelm@12350
   233
val intro_global       = add introK I GlobalRules.map;
wenzelm@12350
   234
val elim_global        = add elimK I GlobalRules.map;
wenzelm@12350
   235
val dest_global        = add elimK Tactic.make_elim GlobalRules.map;
wenzelm@12350
   236
val intro_query_global = add intro_queryK I GlobalRules.map;
wenzelm@12350
   237
val elim_query_global  = add elim_queryK I GlobalRules.map;
wenzelm@12350
   238
val dest_query_global  = add elim_queryK Tactic.make_elim GlobalRules.map;
wenzelm@12350
   239
val rule_del_global    = del GlobalRules.map;
wenzelm@12350
   240
wenzelm@12350
   241
val intro_bang_local   = add intro_bangK I LocalRules.map;
wenzelm@12350
   242
val elim_bang_local    = add elim_bangK I LocalRules.map;
wenzelm@12350
   243
val dest_bang_local    = add elim_bangK Tactic.make_elim LocalRules.map;
wenzelm@12350
   244
val intro_local        = add introK I LocalRules.map;
wenzelm@12350
   245
val elim_local         = add elimK I LocalRules.map;
wenzelm@12350
   246
val dest_local         = add elimK Tactic.make_elim LocalRules.map;
wenzelm@12350
   247
val intro_query_local  = add intro_queryK I LocalRules.map;
wenzelm@12350
   248
val elim_query_local   = add elim_queryK I LocalRules.map;
wenzelm@12350
   249
val dest_query_local   = add elim_queryK Tactic.make_elim LocalRules.map;
wenzelm@12350
   250
val rule_del_local     = del LocalRules.map;
wenzelm@12350
   251
wenzelm@12350
   252
end;
wenzelm@12350
   253
wenzelm@15801
   254
val _ = Context.add_setup
wenzelm@15801
   255
  [#1 o PureThy.add_thms [(("", Drule.equal_intr_rule), [intro_query_global NONE])]];
wenzelm@15801
   256
wenzelm@12350
   257
wenzelm@12380
   258
(* low-level modifiers *)
wenzelm@12380
   259
wenzelm@12380
   260
fun modifier att (x, ths) =
wenzelm@12380
   261
  #1 (Thm.applys_attributes ((x, rev ths), [att]));
wenzelm@12380
   262
skalberg@15531
   263
val addXIs_global = modifier (intro_query_global NONE);
skalberg@15531
   264
val addXEs_global = modifier (elim_query_global NONE);
skalberg@15531
   265
val addXDs_global = modifier (dest_query_global NONE);
wenzelm@12380
   266
val delrules_global = modifier rule_del_global;
wenzelm@12380
   267
skalberg@15531
   268
val addXIs_local = modifier (intro_query_local NONE);
skalberg@15531
   269
val addXEs_local = modifier (elim_query_local NONE);
skalberg@15531
   270
val addXDs_local = modifier (dest_query_local NONE);
wenzelm@12380
   271
val delrules_local = modifier rule_del_local;
wenzelm@12380
   272
wenzelm@12350
   273
end;