src/Pure/Isar/context_rules.ML
author kleing
Mon Jun 21 10:25:57 2004 +0200 (2004-06-21 ago)
changeset 14981 e73f8140af78
parent 14472 cba7c0a3ffb3
child 15531 08c8dad8e399
permissions -rw-r--r--
Merged in license change from Isabelle2004
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
  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@13105
   105
    fun eq_th (_, (_, th')) = Drule.eq_thm_prop (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)
paulson@14472
   117
          (sort (Int.compare 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@13105
   137
          k1 = k2 andalso Drule.eq_thm_prop (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@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
paulson@14472
   177
fun orderlist brls = 
paulson@14472
   178
    untaglist (sort (prod_ord Int.compare Int.compare o pairself fst) brls);
paulson@14472
   179
fun orderlist_no_weight brls = 
paulson@14472
   180
    untaglist (sort (Int.compare o pairself (snd o fst)) brls);
wenzelm@12399
   181
wenzelm@12399
   182
fun may_unify weighted t net =
wenzelm@12399
   183
  map snd ((if weighted then orderlist else orderlist_no_weight) (Net.unify_term net t));
wenzelm@12399
   184
wenzelm@12399
   185
fun find_erules _ [] = K []
wenzelm@12805
   186
  | find_erules w (fact :: _) = may_unify w (Logic.strip_assums_concl (Thm.prop_of fact));
wenzelm@12399
   187
fun find_irules w goal = may_unify w (Logic.strip_assums_concl goal);
wenzelm@12380
   188
wenzelm@12399
   189
fun find_rules_netpair weighted facts goal (inet, enet) =
wenzelm@12399
   190
  find_erules weighted facts enet @ find_irules weighted goal inet;
wenzelm@12380
   191
wenzelm@12399
   192
fun find_rules weighted facts goals = map (find_rules_netpair weighted facts goals) o netpairs;
wenzelm@12350
   193
wenzelm@12350
   194
wenzelm@12399
   195
(* wrappers *)
wenzelm@12399
   196
wenzelm@12350
   197
fun gen_add_wrapper upd w = GlobalRules.map (fn (rs as Rules {next, rules, netpairs, wrappers}) =>
wenzelm@12350
   198
  make_rules next rules netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers));
wenzelm@12350
   199
wenzelm@12350
   200
val addSWrapper = gen_add_wrapper Library.apfst;
wenzelm@12350
   201
val addWrapper = gen_add_wrapper Library.apsnd;
wenzelm@12350
   202
wenzelm@12350
   203
wenzelm@12350
   204
fun gen_wrap which ctxt =
wenzelm@12350
   205
  let val Rules {wrappers, ...} = LocalRules.get ctxt
wenzelm@12350
   206
  in fn tac => foldr (fn ((w, _), t) => w t) (which wrappers, tac) end;
wenzelm@12350
   207
wenzelm@12350
   208
val Swrap = gen_wrap #1;
wenzelm@12350
   209
val wrap = gen_wrap #2;
wenzelm@12350
   210
wenzelm@12350
   211
wenzelm@12350
   212
wenzelm@12350
   213
(** attributes **)
wenzelm@12350
   214
wenzelm@12350
   215
(* add and del rules *)
wenzelm@12350
   216
wenzelm@12350
   217
local
wenzelm@12350
   218
wenzelm@12350
   219
fun del map_data (x, th) =
wenzelm@12350
   220
  (map_data (del_rule th o del_rule (Tactic.make_elim th)) x, th);
wenzelm@12350
   221
wenzelm@12350
   222
fun add k view map_data opt_w =
wenzelm@12412
   223
  (fn (x, th) => (map_data (add_rule k opt_w (view th)) x, th)) o del map_data;
wenzelm@12350
   224
wenzelm@12350
   225
in
wenzelm@12350
   226
wenzelm@12350
   227
val intro_bang_global  = add intro_bangK I GlobalRules.map;
wenzelm@12350
   228
val elim_bang_global   = add elim_bangK I GlobalRules.map;
wenzelm@12350
   229
val dest_bang_global   = add elim_bangK Tactic.make_elim GlobalRules.map;
wenzelm@12350
   230
val intro_global       = add introK I GlobalRules.map;
wenzelm@12350
   231
val elim_global        = add elimK I GlobalRules.map;
wenzelm@12350
   232
val dest_global        = add elimK Tactic.make_elim GlobalRules.map;
wenzelm@12350
   233
val intro_query_global = add intro_queryK I GlobalRules.map;
wenzelm@12350
   234
val elim_query_global  = add elim_queryK I GlobalRules.map;
wenzelm@12350
   235
val dest_query_global  = add elim_queryK Tactic.make_elim GlobalRules.map;
wenzelm@12350
   236
val rule_del_global    = del GlobalRules.map;
wenzelm@12350
   237
wenzelm@12350
   238
val intro_bang_local   = add intro_bangK I LocalRules.map;
wenzelm@12350
   239
val elim_bang_local    = add elim_bangK I LocalRules.map;
wenzelm@12350
   240
val dest_bang_local    = add elim_bangK Tactic.make_elim LocalRules.map;
wenzelm@12350
   241
val intro_local        = add introK I LocalRules.map;
wenzelm@12350
   242
val elim_local         = add elimK I LocalRules.map;
wenzelm@12350
   243
val dest_local         = add elimK Tactic.make_elim LocalRules.map;
wenzelm@12350
   244
val intro_query_local  = add intro_queryK I LocalRules.map;
wenzelm@12350
   245
val elim_query_local   = add elim_queryK I LocalRules.map;
wenzelm@12350
   246
val dest_query_local   = add elim_queryK Tactic.make_elim LocalRules.map;
wenzelm@12350
   247
val rule_del_local     = del LocalRules.map;
wenzelm@12350
   248
wenzelm@12350
   249
end;
wenzelm@12350
   250
wenzelm@12350
   251
wenzelm@12380
   252
(* low-level modifiers *)
wenzelm@12380
   253
wenzelm@12380
   254
fun modifier att (x, ths) =
wenzelm@12380
   255
  #1 (Thm.applys_attributes ((x, rev ths), [att]));
wenzelm@12380
   256
wenzelm@12380
   257
val addXIs_global = modifier (intro_query_global None);
wenzelm@12380
   258
val addXEs_global = modifier (elim_query_global None);
wenzelm@12380
   259
val addXDs_global = modifier (dest_query_global None);
wenzelm@12380
   260
val delrules_global = modifier rule_del_global;
wenzelm@12380
   261
wenzelm@12380
   262
val addXIs_local = modifier (intro_query_local None);
wenzelm@12380
   263
val addXEs_local = modifier (elim_query_local None);
wenzelm@12380
   264
val addXDs_local = modifier (dest_query_local None);
wenzelm@12380
   265
val delrules_local = modifier rule_del_local;
wenzelm@12380
   266
wenzelm@12380
   267
wenzelm@12350
   268
wenzelm@12350
   269
(** theory setup **)
wenzelm@12350
   270
wenzelm@12350
   271
val setup =
wenzelm@13372
   272
 [GlobalRules.init, LocalRules.init];
wenzelm@12350
   273
wenzelm@12350
   274
wenzelm@12350
   275
end;