src/Pure/Isar/context_rules.ML
author wenzelm
Tue, 22 Jan 2019 19:36:17 +0100
changeset 69719 331ef175a112
parent 67649 1e1782c1aedf
child 74561 8e6c973003c8
permissions -rw-r--r--
Backed out changeset 1bc422c08209 -- obsolete in AFP/5d11846ac6ab;
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
    Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
     3
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
     4
Declarations of intro/elim/dest rules in Pure (see also
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
     5
Provers/classical.ML for a more specialized version of the same idea).
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
     6
*)
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
     7
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
     8
signature CONTEXT_RULES =
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
     9
sig
33370
69531a7b55b6 tuned signature;
wenzelm
parents: 33369
diff changeset
    10
  type netpair = ((int * int) * (bool * thm)) Net.net * ((int * int) * (bool * thm)) Net.net
20289
ba7a7c56bed5 normalized Proof.context/method type aliases;
wenzelm
parents: 19482
diff changeset
    11
  val netpair_bang: Proof.context -> netpair
ba7a7c56bed5 normalized Proof.context/method type aliases;
wenzelm
parents: 19482
diff changeset
    12
  val netpair: Proof.context -> netpair
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    13
  val orderlist: ((int * int) * 'a) list -> 'a list
61049
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
    14
  val find_rules_netpair: Proof.context -> bool -> thm list -> term -> netpair -> thm list
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
    15
  val find_rules: Proof.context -> bool -> thm list -> term -> thm list list
21506
b2a673894ce5 prefer Proof.context over Context.generic;
wenzelm
parents: 20289
diff changeset
    16
  val print_rules: Proof.context -> unit
51798
ad3a241def73 uniform Proof.context for hyp_subst_tac;
wenzelm
parents: 51584
diff changeset
    17
  val addSWrapper: (Proof.context -> (int -> tactic) -> int -> tactic) -> theory -> theory
ad3a241def73 uniform Proof.context for hyp_subst_tac;
wenzelm
parents: 51584
diff changeset
    18
  val addWrapper: (Proof.context -> (int -> tactic) -> int -> tactic) -> theory -> theory
20289
ba7a7c56bed5 normalized Proof.context/method type aliases;
wenzelm
parents: 19482
diff changeset
    19
  val Swrap: Proof.context -> (int -> tactic) -> int -> tactic
ba7a7c56bed5 normalized Proof.context/method type aliases;
wenzelm
parents: 19482
diff changeset
    20
  val wrap: Proof.context -> (int -> tactic) -> int -> tactic
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    21
  val intro_bang: int option -> attribute
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    22
  val elim_bang: int option -> attribute
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    23
  val dest_bang: int option -> attribute
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    24
  val intro: int option -> attribute
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    25
  val elim: int option -> attribute
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    26
  val dest: int option -> attribute
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    27
  val intro_query: int option -> attribute
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    28
  val elim_query: int option -> attribute
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    29
  val dest_query: int option -> attribute
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    30
  val rule_del: attribute
30529
23d1892f8015 simplified attribute setup;
wenzelm
parents: 29606
diff changeset
    31
  val add: (int option -> attribute) -> (int option -> attribute) -> (int option -> attribute) ->
23d1892f8015 simplified attribute setup;
wenzelm
parents: 29606
diff changeset
    32
    attribute context_parser
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    33
end;
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    34
33369
470a7b233ee5 modernized structure Context_Rules;
wenzelm
parents: 32784
diff changeset
    35
structure Context_Rules: CONTEXT_RULES =
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    36
struct
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    37
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    38
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    39
(** rule declaration contexts **)
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    40
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    41
(* rule kinds *)
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    42
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    43
val intro_bangK = (0, false);
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    44
val elim_bangK = (0, true);
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    45
val introK = (1, false);
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    46
val elimK = (1, true);
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    47
val intro_queryK = (2, false);
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    48
val elim_queryK = (2, true);
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    49
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    50
val kind_names =
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    51
 [(intro_bangK, "safe introduction rules (intro!)"),
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    52
  (elim_bangK, "safe elimination rules (elim!)"),
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    53
  (introK, "introduction rules (intro)"),
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    54
  (elimK, "elimination rules (elim)"),
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    55
  (intro_queryK, "extra introduction rules (intro?)"),
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    56
  (elim_queryK, "extra elimination rules (elim?)")];
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    57
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    58
val rule_kinds = map #1 kind_names;
19046
bc5c6c9b114e removed distinct, renamed gen_distinct to distinct;
wenzelm
parents: 18977
diff changeset
    59
val rule_indexes = distinct (op =) (map #1 rule_kinds);
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    60
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    61
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
    62
(* context data *)
12350
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
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
    65
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
    66
33370
69531a7b55b6 tuned signature;
wenzelm
parents: 33369
diff changeset
    67
datatype rules = Rules of
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    68
 {next: int,
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    69
  rules: (int * ((int * bool) * thm)) list,
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    70
  netpairs: netpair list,
51798
ad3a241def73 uniform Proof.context for hyp_subst_tac;
wenzelm
parents: 51584
diff changeset
    71
  wrappers:
ad3a241def73 uniform Proof.context for hyp_subst_tac;
wenzelm
parents: 51584
diff changeset
    72
    ((Proof.context -> (int -> tactic) -> int -> tactic) * stamp) list *
ad3a241def73 uniform Proof.context for hyp_subst_tac;
wenzelm
parents: 51584
diff changeset
    73
    ((Proof.context -> (int -> tactic) -> int -> tactic) * stamp) list};
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    74
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    75
fun make_rules next rules netpairs wrappers =
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    76
  Rules {next = next, rules = rules, netpairs = netpairs, wrappers = wrappers};
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    77
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    78
fun add_rule (i, b) opt_w th (Rules {next, rules, netpairs, wrappers}) =
61049
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
    79
  let
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
    80
    val w = (case opt_w of SOME w => w | NONE => Tactic.subgoals_of_brl (b, th));
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
    81
    val th' = Thm.trim_context th;
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
    82
  in
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
    83
    make_rules (next - 1) ((w, ((i, b), th')) :: rules)
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
    84
      (nth_map i (Tactic.insert_tagged_brl ((w, next), (b, th'))) netpairs) wrappers
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    85
  end;
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    86
45375
7fe19930dfc9 more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents: 39557
diff changeset
    87
fun del_rule0 th (rs as Rules {next, rules, netpairs, wrappers}) =
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    88
  let
22360
26ead7ed4f4b moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents: 21506
diff changeset
    89
    fun eq_th (_, (_, th')) = Thm.eq_thm_prop (th, th');
23227
96f86d377dd9 tuned Tactic signature;
wenzelm
parents: 23178
diff changeset
    90
    fun del b netpair = Tactic.delete_tagged_brl (b, th) netpair handle Net.DELETE => netpair;
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    91
  in
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    92
    if not (exists eq_th rules) then rs
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    93
    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
    94
  end;
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
    95
45375
7fe19930dfc9 more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents: 39557
diff changeset
    96
fun del_rule th = del_rule0 th o del_rule0 (Tactic.make_elim th);
7fe19930dfc9 more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents: 39557
diff changeset
    97
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33370
diff changeset
    98
structure Rules = Generic_Data
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
    99
(
33370
69531a7b55b6 tuned signature;
wenzelm
parents: 33369
diff changeset
   100
  type T = rules;
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   101
  val empty = make_rules ~1 [] empty_netpairs ([], []);
16424
18a07ad8fea8 accomodate change of TheoryDataFun;
wenzelm
parents: 15973
diff changeset
   102
  val extend = I;
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33370
diff changeset
   103
  fun merge
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33370
diff changeset
   104
    (Rules {rules = rules1, wrappers = (ws1, ws1'), ...},
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   105
      Rules {rules = rules2, wrappers = (ws2, ws2'), ...}) =
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   106
    let
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   107
      val wrappers =
18921
f47c46d7d654 canonical member/insert/merge;
wenzelm
parents: 18874
diff changeset
   108
        (Library.merge (eq_snd (op =)) (ws1, ws2), Library.merge (eq_snd (op =)) (ws1', ws2'));
f47c46d7d654 canonical member/insert/merge;
wenzelm
parents: 18874
diff changeset
   109
      val rules = Library.merge (fn ((_, (k1, th1)), (_, (k2, th2))) =>
22360
26ead7ed4f4b moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents: 21506
diff changeset
   110
          k1 = k2 andalso Thm.eq_thm_prop (th1, th2)) (rules1, rules2);
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   111
      val next = ~ (length rules);
19473
wenzelm
parents: 19046
diff changeset
   112
      val netpairs = fold (fn (n, (w, ((i, b), th))) =>
23227
96f86d377dd9 tuned Tactic signature;
wenzelm
parents: 23178
diff changeset
   113
          nth_map i (Tactic.insert_tagged_brl ((w, n), (b, th))))
19473
wenzelm
parents: 19046
diff changeset
   114
        (next upto ~1 ~~ rules) empty_netpairs;
23227
96f86d377dd9 tuned Tactic signature;
wenzelm
parents: 23178
diff changeset
   115
    in make_rules (next - 1) rules netpairs wrappers end;
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   116
);
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   117
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22360
diff changeset
   118
fun print_rules ctxt =
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22360
diff changeset
   119
  let
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22360
diff changeset
   120
    val Rules {rules, ...} = Rules.get (Context.Proof ctxt);
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22360
diff changeset
   121
    fun prt_kind (i, b) =
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22360
diff changeset
   122
      Pretty.big_list ((the o AList.lookup (op =) kind_names) (i, b) ^ ":")
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22360
diff changeset
   123
        (map_filter (fn (_, (k, th)) =>
61268
abe08fb15a12 moved remaining display.ML to more_thm.ML;
wenzelm
parents: 61049
diff changeset
   124
            if k = (i, b) then SOME (Thm.pretty_thm_item ctxt th) else NONE)
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 56334
diff changeset
   125
          (sort (int_ord o apply2 fst) rules));
56334
6b3739fee456 some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents: 56204
diff changeset
   126
  in Pretty.writeln_chunks (map prt_kind rule_kinds) end;
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   127
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
(* access data *)
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   130
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   131
fun netpairs ctxt = let val Rules {netpairs, ...} = Rules.get (Context.Proof ctxt) in netpairs end;
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   132
val netpair_bang = hd o netpairs;
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   133
val netpair = hd o tl o netpairs;
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   134
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   135
12399
wenzelm
parents: 12380
diff changeset
   136
(* retrieving rules *)
wenzelm
parents: 12380
diff changeset
   137
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   138
fun untaglist [] = []
32784
1a5dde5079ac eliminated redundant bindings;
wenzelm
parents: 32091
diff changeset
   139
  | untaglist [(_ : int * int, x)] = [x]
1a5dde5079ac eliminated redundant bindings;
wenzelm
parents: 32091
diff changeset
   140
  | untaglist ((k, x) :: (rest as (k', _) :: _)) =
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   141
      if k = k' then untaglist rest
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   142
      else x :: untaglist rest;
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   143
16424
18a07ad8fea8 accomodate change of TheoryDataFun;
wenzelm
parents: 15973
diff changeset
   144
fun orderlist brls =
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 56334
diff changeset
   145
  untaglist (sort (prod_ord int_ord int_ord o apply2 fst) brls);
16424
18a07ad8fea8 accomodate change of TheoryDataFun;
wenzelm
parents: 15973
diff changeset
   146
18a07ad8fea8 accomodate change of TheoryDataFun;
wenzelm
parents: 15973
diff changeset
   147
fun orderlist_no_weight brls =
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 56334
diff changeset
   148
  untaglist (sort (int_ord o apply2 (snd o fst)) brls);
12399
wenzelm
parents: 12380
diff changeset
   149
61049
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
   150
local
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
   151
12399
wenzelm
parents: 12380
diff changeset
   152
fun may_unify weighted t net =
wenzelm
parents: 12380
diff changeset
   153
  map snd ((if weighted then orderlist else orderlist_no_weight) (Net.unify_term net t));
wenzelm
parents: 12380
diff changeset
   154
wenzelm
parents: 12380
diff changeset
   155
fun find_erules _ [] = K []
12805
3be853cf19cf Thm.prop_of;
wenzelm
parents: 12412
diff changeset
   156
  | find_erules w (fact :: _) = may_unify w (Logic.strip_assums_concl (Thm.prop_of fact));
16424
18a07ad8fea8 accomodate change of TheoryDataFun;
wenzelm
parents: 15973
diff changeset
   157
12399
wenzelm
parents: 12380
diff changeset
   158
fun find_irules w goal = may_unify w (Logic.strip_assums_concl goal);
12380
3402d300f5ef export low-level addXXs;
wenzelm
parents: 12350
diff changeset
   159
61049
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
   160
in
12380
3402d300f5ef export low-level addXXs;
wenzelm
parents: 12350
diff changeset
   161
61049
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
   162
fun find_rules_netpair ctxt weighted facts goal (inet, enet) =
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
   163
  find_erules weighted facts enet @ find_irules weighted goal inet
67649
1e1782c1aedf tuned signature;
wenzelm
parents: 67626
diff changeset
   164
  |> map (Thm.transfer' ctxt);
61049
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
   165
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
   166
fun find_rules ctxt weighted facts goal =
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
   167
  map (find_rules_netpair ctxt weighted facts goal) (netpairs ctxt);
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
   168
0d401f874942 trim context for persistent storage;
wenzelm
parents: 61037
diff changeset
   169
end;
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   170
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   171
12399
wenzelm
parents: 12380
diff changeset
   172
(* wrappers *)
wenzelm
parents: 12380
diff changeset
   173
18667
wenzelm
parents: 18637
diff changeset
   174
fun gen_add_wrapper upd w =
32784
1a5dde5079ac eliminated redundant bindings;
wenzelm
parents: 32091
diff changeset
   175
  Context.theory_map (Rules.map (fn Rules {next, rules, netpairs, wrappers} =>
18667
wenzelm
parents: 18637
diff changeset
   176
    make_rules next rules netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers)));
12350
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
val addSWrapper = gen_add_wrapper Library.apfst;
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   179
val addWrapper = gen_add_wrapper Library.apsnd;
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   180
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   181
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   182
fun gen_wrap which ctxt =
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   183
  let val Rules {wrappers, ...} = Rules.get (Context.Proof ctxt)
51798
ad3a241def73 uniform Proof.context for hyp_subst_tac;
wenzelm
parents: 51584
diff changeset
   184
  in fold_rev (fn (w, _) => w ctxt) (which wrappers) end;
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   185
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   186
val Swrap = gen_wrap #1;
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   187
val wrap = gen_wrap #2;
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   188
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   189
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   190
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   191
(** attributes **)
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
(* add and del rules *)
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   194
45375
7fe19930dfc9 more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents: 39557
diff changeset
   195
67626
wenzelm
parents: 67147
diff changeset
   196
val rule_del = Thm.declaration_attribute (Rules.map o del_rule);
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   197
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   198
fun rule_add k view opt_w =
45375
7fe19930dfc9 more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents: 39557
diff changeset
   199
  Thm.declaration_attribute (fn th => Rules.map (add_rule k opt_w (view th) o del_rule th));
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   200
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   201
val intro_bang  = rule_add intro_bangK I;
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   202
val elim_bang   = rule_add elim_bangK I;
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   203
val dest_bang   = rule_add elim_bangK Tactic.make_elim;
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   204
val intro       = rule_add introK I;
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   205
val elim        = rule_add elimK I;
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   206
val dest        = rule_add elimK Tactic.make_elim;
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   207
val intro_query = rule_add intro_queryK I;
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   208
val elim_query  = rule_add elim_queryK I;
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   209
val dest_query  = rule_add elim_queryK Tactic.make_elim;
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   210
53171
a5e54d4d9081 added Theory.setup convenience;
wenzelm
parents: 51798
diff changeset
   211
val _ = Theory.setup
a5e54d4d9081 added Theory.setup convenience;
wenzelm
parents: 51798
diff changeset
   212
  (snd o Global_Theory.add_thms [((Binding.empty, Drule.equal_intr_rule), [intro_query NONE])]);
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   213
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   214
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   215
(* concrete syntax *)
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   216
30529
23d1892f8015 simplified attribute setup;
wenzelm
parents: 29606
diff changeset
   217
fun add a b c x =
27809
a1e409db516b unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents: 26463
diff changeset
   218
  (Scan.lift ((Args.bang >> K a || Args.query >> K c || Scan.succeed b) --
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 33519
diff changeset
   219
    Scan.option Parse.nat) >> (fn (f, n) => f n)) x;
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18418
diff changeset
   220
53171
a5e54d4d9081 added Theory.setup convenience;
wenzelm
parents: 51798
diff changeset
   221
val _ = Theory.setup
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 61268
diff changeset
   222
 (Attrib.setup \<^binding>\<open>intro\<close> (add intro_bang intro intro_query)
30529
23d1892f8015 simplified attribute setup;
wenzelm
parents: 29606
diff changeset
   223
    "declaration of introduction rule" #>
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 61268
diff changeset
   224
  Attrib.setup \<^binding>\<open>elim\<close> (add elim_bang elim elim_query)
30529
23d1892f8015 simplified attribute setup;
wenzelm
parents: 29606
diff changeset
   225
    "declaration of elimination rule" #>
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 61268
diff changeset
   226
  Attrib.setup \<^binding>\<open>dest\<close> (add dest_bang dest dest_query)
30529
23d1892f8015 simplified attribute setup;
wenzelm
parents: 29606
diff changeset
   227
    "declaration of destruction rule" #>
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 61268
diff changeset
   228
  Attrib.setup \<^binding>\<open>rule\<close> (Scan.lift Args.del >> K rule_del)
53171
a5e54d4d9081 added Theory.setup convenience;
wenzelm
parents: 51798
diff changeset
   229
    "remove declaration of intro/elim/dest rule");
12350
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   230
5fad0e7129c3 renamed rule_context.ML to context_rules.ML;
wenzelm
parents:
diff changeset
   231
end;