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