author | paulson <lp15@cam.ac.uk> |
Wed, 06 Aug 2025 10:01:05 +0100 | |
changeset 82914 | cbf3703f92ea |
parent 82896 | cc89d72e17b8 |
permissions | -rw-r--r-- |
82805 | 1 |
(* Title: Pure/bires.ML |
2 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
82806 | 3 |
Author: Makarius |
82805 | 4 |
|
5 |
Biresolution and resolution using nets. |
|
6 |
*) |
|
7 |
||
8 |
signature BIRES = |
|
9 |
sig |
|
82806 | 10 |
type rule = bool * thm |
82808 | 11 |
val subgoals_of: rule -> int |
12 |
val subgoals_ord: rule ord |
|
13 |
val no_subgoals: rule -> bool |
|
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
14 |
|
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
15 |
type tag = {weight: int, index: int} |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
16 |
val tag_weight_ord: tag ord |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
17 |
val tag_index_ord: tag ord |
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
18 |
val tag_ord: tag ord |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
19 |
val weighted_tag_ord: bool -> tag ord |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
20 |
val tag_order: (tag * 'a) list -> 'a list |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
21 |
val weight_tag: int -> tag |
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
22 |
|
82827 | 23 |
eqtype kind |
24 |
val intro_bang_kind: kind |
|
25 |
val elim_bang_kind: kind |
|
82868
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
26 |
val dest_bang_kind: kind |
82827 | 27 |
val intro_kind: kind |
28 |
val elim_kind: kind |
|
82868
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
29 |
val dest_kind: kind |
82827 | 30 |
val intro_query_kind: kind |
31 |
val elim_query_kind: kind |
|
82868
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
32 |
val dest_query_kind: kind |
82842
8aa1c98b948b
maintain collective rule declarations via type Bires.decls, with netpair operations derived from it;
wenzelm
parents:
82840
diff
changeset
|
33 |
val kind_safe: kind -> bool |
8aa1c98b948b
maintain collective rule declarations via type Bires.decls, with netpair operations derived from it;
wenzelm
parents:
82840
diff
changeset
|
34 |
val kind_unsafe: kind -> bool |
8aa1c98b948b
maintain collective rule declarations via type Bires.decls, with netpair operations derived from it;
wenzelm
parents:
82840
diff
changeset
|
35 |
val kind_extra: kind -> bool |
82827 | 36 |
val kind_index: kind -> int |
82868
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
37 |
val kind_is_elim: kind -> bool |
82873 | 38 |
val kind_make_elim: kind -> thm -> thm |
82827 | 39 |
val kind_domain: kind list |
82828 | 40 |
val kind_values: 'a -> 'a list |
82827 | 41 |
val kind_map: kind -> ('a -> 'a) -> 'a list -> 'a list |
42 |
val kind_rule: kind -> thm -> rule |
|
82842
8aa1c98b948b
maintain collective rule declarations via type Bires.decls, with netpair operations derived from it;
wenzelm
parents:
82840
diff
changeset
|
43 |
val kind_description: kind -> string |
82827 | 44 |
val kind_title: kind -> string |
82896 | 45 |
val kind_name: kind -> string |
82827 | 46 |
|
82829
57c331dc167d
clarified signature: anticipate use in src/Provers/classical.ML;
wenzelm
parents:
82828
diff
changeset
|
47 |
type 'a decl = {kind: kind, tag: tag, info: 'a} |
82891
372273ab6ebb
clarified natural decl_ord vs. slightly odd merge_decl_ord, following the historic status-quo of 53e56e6a67c3, which originally stems from c06d01f75764;
wenzelm
parents:
82890
diff
changeset
|
48 |
val decl_ord: 'a decl ord |
82829
57c331dc167d
clarified signature: anticipate use in src/Provers/classical.ML;
wenzelm
parents:
82828
diff
changeset
|
49 |
type 'a decls |
57c331dc167d
clarified signature: anticipate use in src/Provers/classical.ML;
wenzelm
parents:
82828
diff
changeset
|
50 |
val has_decls: 'a decls -> thm -> bool |
82839
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
51 |
val get_decls: 'a decls -> thm -> 'a decl list |
82844
ebfb0e011c95
clarified signature: prefer canonical order (latest declarations first);
wenzelm
parents:
82843
diff
changeset
|
52 |
val dest_decls: 'a decls -> (thm * 'a decl -> bool) -> (thm * 'a decl) list |
82864 | 53 |
val pretty_decls: Proof.context -> 'a decls -> Pretty.T list |
82839
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
54 |
val merge_decls: 'a decls * 'a decls -> 'a decl list * 'a decls |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
55 |
val extend_decls: thm * 'a decl -> 'a decls -> 'a decl option * 'a decls |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
56 |
val remove_decls: thm -> 'a decls -> 'a decl list * 'a decls |
82829
57c331dc167d
clarified signature: anticipate use in src/Provers/classical.ML;
wenzelm
parents:
82828
diff
changeset
|
57 |
val empty_decls: 'a decls |
82827 | 58 |
|
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
59 |
type netpair = (tag * rule) Net.net * (tag * rule) Net.net |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
60 |
val empty_netpair: netpair |
82888 | 61 |
val pretty_netpair: Proof.context -> string -> netpair -> Pretty.T list |
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
62 |
val insert_tagged_rule: tag * rule -> netpair -> netpair |
82848
0a8977dcb21a
more accurate delete operation via authentic index --- minor performance tuning;
wenzelm
parents:
82844
diff
changeset
|
63 |
val delete_tagged_rule: int * thm -> netpair -> netpair |
82827 | 64 |
|
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
65 |
val biresolution_from_nets_tac: Proof.context -> tag ord -> (rule -> bool) option -> |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
66 |
bool -> netpair -> int -> tactic |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
67 |
val biresolve_from_nets_tac: Proof.context -> netpair -> int -> tactic |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
68 |
val bimatch_from_nets_tac: Proof.context -> netpair -> int -> tactic |
82811 | 69 |
|
70 |
type net = (int * thm) Net.net |
|
71 |
val build_net: thm list -> net |
|
72 |
val filt_resolve_from_net_tac: Proof.context -> int -> net -> int -> tactic |
|
73 |
val resolve_from_net_tac: Proof.context -> net -> int -> tactic |
|
74 |
val match_from_net_tac: Proof.context -> net -> int -> tactic |
|
82805 | 75 |
end |
76 |
||
77 |
structure Bires: BIRES = |
|
78 |
struct |
|
79 |
||
82827 | 80 |
(** Rule kinds and declarations **) |
82808 | 81 |
|
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
82 |
(* type rule *) |
82806 | 83 |
|
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
84 |
type rule = bool * thm; (*see Thm.biresolution*) |
82806 | 85 |
|
82808 | 86 |
fun subgoals_of (true, thm) = Thm.nprems_of thm - 1 |
87 |
| subgoals_of (false, thm) = Thm.nprems_of thm; |
|
88 |
||
89 |
val subgoals_ord = int_ord o apply2 subgoals_of; |
|
90 |
||
82809 | 91 |
fun no_subgoals (true, thm) = Thm.one_prem thm |
92 |
| no_subgoals (false, thm) = Thm.no_prems thm; |
|
82808 | 93 |
|
82806 | 94 |
|
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
95 |
(* tagged rules *) |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
96 |
|
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
97 |
type tag = {weight: int, index: int}; |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
98 |
|
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
99 |
val tag_weight_ord: tag ord = int_ord o apply2 #weight; |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
100 |
val tag_index_ord: tag ord = int_ord o apply2 #index; |
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
101 |
|
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
102 |
val tag_ord: tag ord = tag_weight_ord ||| tag_index_ord; |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
103 |
|
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
104 |
fun weighted_tag_ord weighted = if weighted then tag_ord else tag_index_ord; |
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
105 |
|
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
106 |
fun tag_order list = make_order_list tag_ord NONE list; |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
107 |
|
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
108 |
fun weight_tag weight : tag = {weight = weight, index = 0}; |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
109 |
|
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
110 |
fun next_tag next ({weight, ...}: tag) = {weight = weight, index = next}; |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
111 |
|
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82811
diff
changeset
|
112 |
|
82868
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
113 |
(* kind: intro/elim/dest *) |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
114 |
|
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
115 |
datatype kind = Kind of {index: int, is_elim: bool, make_elim: bool}; |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
116 |
|
82868
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
117 |
fun make_intro_kind i = Kind {index = i, is_elim = false, make_elim = false}; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
118 |
fun make_elim_kind i = Kind {index = i, is_elim = true, make_elim = false}; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
119 |
fun make_dest_kind i = Kind {index = i, is_elim = true, make_elim = true}; |
82817
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
120 |
|
82868
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
121 |
val intro_bang_kind = make_intro_kind 0; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
122 |
val elim_bang_kind = make_elim_kind 0; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
123 |
val dest_bang_kind = make_dest_kind 0; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
124 |
|
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
125 |
val intro_kind = make_intro_kind 1; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
126 |
val elim_kind = make_elim_kind 1; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
127 |
val dest_kind = make_dest_kind 1; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
128 |
|
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
129 |
val intro_query_kind = make_intro_kind 2; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
130 |
val elim_query_kind = make_elim_kind 2; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
131 |
val dest_query_kind = make_dest_kind 2; |
82817
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
132 |
|
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
133 |
val kind_infos = |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
134 |
[(intro_bang_kind, ("safe introduction", "(intro!)")), |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
135 |
(elim_bang_kind, ("safe elimination", "(elim!)")), |
82868
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
136 |
(dest_bang_kind, ("safe destruction", "(dest!)")), |
82864 | 137 |
(intro_kind, ("unsafe introduction", "(intro)")), |
138 |
(elim_kind, ("unsafe elimination", "(elim)")), |
|
82868
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
139 |
(dest_kind, ("unsafe destruction", "(dest)")), |
82817
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
140 |
(intro_query_kind, ("extra introduction", "(intro?)")), |
82868
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
141 |
(elim_query_kind, ("extra elimination", "(elim?)")), |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
142 |
(dest_query_kind, ("extra destruction", "(dest?)"))]; |
82817
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
143 |
|
82868
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
144 |
fun kind_safe (Kind {index, ...}) = index = 0; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
145 |
fun kind_unsafe (Kind {index, ...}) = index = 1; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
146 |
fun kind_extra (Kind {index, ...}) = index = 2; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
147 |
fun kind_index (Kind {index, ...}) = index; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
148 |
fun kind_is_elim (Kind {is_elim, ...}) = is_elim; |
82873 | 149 |
fun kind_make_elim (Kind {make_elim, ...}) = make_elim ? Tactic.make_elim; |
82817
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
150 |
|
82891
372273ab6ebb
clarified natural decl_ord vs. slightly odd merge_decl_ord, following the historic status-quo of 53e56e6a67c3, which originally stems from c06d01f75764;
wenzelm
parents:
82890
diff
changeset
|
151 |
val kind_index_ord = int_ord o apply2 kind_index; |
372273ab6ebb
clarified natural decl_ord vs. slightly odd merge_decl_ord, following the historic status-quo of 53e56e6a67c3, which originally stems from c06d01f75764;
wenzelm
parents:
82890
diff
changeset
|
152 |
val kind_elim_ord = bool_ord o apply2 kind_is_elim; |
82889 | 153 |
|
82817
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
154 |
val kind_domain = map #1 kind_infos; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
155 |
|
82828 | 156 |
fun kind_values x = |
157 |
replicate (length (distinct (op =) (map kind_index kind_domain))) x; |
|
158 |
||
82817
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
159 |
fun kind_map kind f = nth_map (kind_index kind) f; |
82868
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
160 |
fun kind_rule kind thm : rule = (kind_is_elim kind, thm); |
82817
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
161 |
|
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
162 |
val the_kind_info = the o AList.lookup (op =) kind_infos; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
163 |
|
82842
8aa1c98b948b
maintain collective rule declarations via type Bires.decls, with netpair operations derived from it;
wenzelm
parents:
82840
diff
changeset
|
164 |
fun kind_description kind = |
8aa1c98b948b
maintain collective rule declarations via type Bires.decls, with netpair operations derived from it;
wenzelm
parents:
82840
diff
changeset
|
165 |
let val (a, b) = the_kind_info kind |
8aa1c98b948b
maintain collective rule declarations via type Bires.decls, with netpair operations derived from it;
wenzelm
parents:
82840
diff
changeset
|
166 |
in a ^ " " ^ b end; |
8aa1c98b948b
maintain collective rule declarations via type Bires.decls, with netpair operations derived from it;
wenzelm
parents:
82840
diff
changeset
|
167 |
|
82817
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
168 |
fun kind_title kind = |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
169 |
let val (a, b) = the_kind_info kind |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
170 |
in a ^ " rules " ^ b end; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
171 |
|
82896 | 172 |
fun kind_name (Kind {is_elim, make_elim, ...}) = |
173 |
if is_elim andalso make_elim then "destruction rule" |
|
174 |
else if is_elim then "elimination rule" |
|
175 |
else "introduction rule"; |
|
176 |
||
82817
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82812
diff
changeset
|
177 |
|
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
178 |
(* rule declarations in canonical order *) |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
179 |
|
82829
57c331dc167d
clarified signature: anticipate use in src/Provers/classical.ML;
wenzelm
parents:
82828
diff
changeset
|
180 |
type 'a decl = {kind: kind, tag: tag, info: 'a}; |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
181 |
|
82891
372273ab6ebb
clarified natural decl_ord vs. slightly odd merge_decl_ord, following the historic status-quo of 53e56e6a67c3, which originally stems from c06d01f75764;
wenzelm
parents:
82890
diff
changeset
|
182 |
fun decl_ord (args: 'a decl * 'a decl) = |
372273ab6ebb
clarified natural decl_ord vs. slightly odd merge_decl_ord, following the historic status-quo of 53e56e6a67c3, which originally stems from c06d01f75764;
wenzelm
parents:
82890
diff
changeset
|
183 |
(case kind_index_ord (apply2 #kind args) of |
372273ab6ebb
clarified natural decl_ord vs. slightly odd merge_decl_ord, following the historic status-quo of 53e56e6a67c3, which originally stems from c06d01f75764;
wenzelm
parents:
82890
diff
changeset
|
184 |
EQUAL => tag_index_ord (apply2 #tag args) |
372273ab6ebb
clarified natural decl_ord vs. slightly odd merge_decl_ord, following the historic status-quo of 53e56e6a67c3, which originally stems from c06d01f75764;
wenzelm
parents:
82890
diff
changeset
|
185 |
| ord => ord); |
372273ab6ebb
clarified natural decl_ord vs. slightly odd merge_decl_ord, following the historic status-quo of 53e56e6a67c3, which originally stems from c06d01f75764;
wenzelm
parents:
82890
diff
changeset
|
186 |
|
372273ab6ebb
clarified natural decl_ord vs. slightly odd merge_decl_ord, following the historic status-quo of 53e56e6a67c3, which originally stems from c06d01f75764;
wenzelm
parents:
82890
diff
changeset
|
187 |
fun decl_merge_ord (args: 'a decl * 'a decl) = |
372273ab6ebb
clarified natural decl_ord vs. slightly odd merge_decl_ord, following the historic status-quo of 53e56e6a67c3, which originally stems from c06d01f75764;
wenzelm
parents:
82890
diff
changeset
|
188 |
(case kind_elim_ord (apply2 #kind args) of |
372273ab6ebb
clarified natural decl_ord vs. slightly odd merge_decl_ord, following the historic status-quo of 53e56e6a67c3, which originally stems from c06d01f75764;
wenzelm
parents:
82890
diff
changeset
|
189 |
EQUAL => rev_order (tag_index_ord (apply2 #tag args)) |
82889 | 190 |
| ord => ord); |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
191 |
|
82829
57c331dc167d
clarified signature: anticipate use in src/Provers/classical.ML;
wenzelm
parents:
82828
diff
changeset
|
192 |
fun next_decl next ({kind, tag, info}: 'a decl) : 'a decl = |
57c331dc167d
clarified signature: anticipate use in src/Provers/classical.ML;
wenzelm
parents:
82828
diff
changeset
|
193 |
{kind = kind, tag = next_tag next tag, info = info}; |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
194 |
|
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
195 |
|
82855
df2d774bcf21
proper thm ord that conforms to Thm.eq_thm_prop (amending f5fd9b41188a): relevant for declarations in a locale contex with assumptions, e.g. "locale test = assumes True begin declare refl [rule del] end";
wenzelm
parents:
82854
diff
changeset
|
196 |
abstype 'a decls = Decls of {next: int, rules: 'a decl list Proptab.table} |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
197 |
with |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
198 |
|
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
199 |
local |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
200 |
|
82895 | 201 |
fun dup_decls (Decls {rules, ...}) (thm, {kind, ...}: 'a decl) = |
202 |
exists (fn {kind = kind', ...} => kind = kind') (Proptab.lookup_list rules thm); |
|
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
203 |
|
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
204 |
fun add_decls (thm, decl) (Decls {next, rules}) = |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
205 |
let |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
206 |
val decl' = next_decl next decl; |
82855
df2d774bcf21
proper thm ord that conforms to Thm.eq_thm_prop (amending f5fd9b41188a): relevant for declarations in a locale contex with assumptions, e.g. "locale test = assumes True begin declare refl [rule del] end";
wenzelm
parents:
82854
diff
changeset
|
207 |
val decls' = Decls {next = next - 1, rules = Proptab.cons_list (thm, decl') rules}; |
82839
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
208 |
in (decl', decls') end; |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
209 |
|
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
210 |
in |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
211 |
|
82855
df2d774bcf21
proper thm ord that conforms to Thm.eq_thm_prop (amending f5fd9b41188a): relevant for declarations in a locale contex with assumptions, e.g. "locale test = assumes True begin declare refl [rule del] end";
wenzelm
parents:
82854
diff
changeset
|
212 |
fun has_decls (Decls {rules, ...}) = Proptab.defined rules; |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
213 |
|
82855
df2d774bcf21
proper thm ord that conforms to Thm.eq_thm_prop (amending f5fd9b41188a): relevant for declarations in a locale contex with assumptions, e.g. "locale test = assumes True begin declare refl [rule del] end";
wenzelm
parents:
82854
diff
changeset
|
214 |
fun get_decls (Decls {rules, ...}) = Proptab.lookup_list rules; |
82839
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
215 |
|
82890
72707b844734
clarified merge order: accurately reproduce the stable status-quo from 53e56e6a67c3 --- e.g. relevant for smt proof reconstruction in (line 6705 of "$AFP/Modular_arithmetic_LLL_and_HNF_algorithms/HNF_Mod_Det_Soundness.thy") of AFP/f1299d4f896c;
wenzelm
parents:
82889
diff
changeset
|
216 |
fun dest_decls_ord ord (Decls {rules, ...}) pred = |
82855
df2d774bcf21
proper thm ord that conforms to Thm.eq_thm_prop (amending f5fd9b41188a): relevant for declarations in a locale contex with assumptions, e.g. "locale test = assumes True begin declare refl [rule del] end";
wenzelm
parents:
82854
diff
changeset
|
217 |
build (rules |> Proptab.fold (fn (th, ds) => ds |> fold (fn d => pred (th, d) ? cons (th, d)))) |
82890
72707b844734
clarified merge order: accurately reproduce the stable status-quo from 53e56e6a67c3 --- e.g. relevant for smt proof reconstruction in (line 6705 of "$AFP/Modular_arithmetic_LLL_and_HNF_algorithms/HNF_Mod_Det_Soundness.thy") of AFP/f1299d4f896c;
wenzelm
parents:
82889
diff
changeset
|
218 |
|> sort (ord o apply2 #2); |
72707b844734
clarified merge order: accurately reproduce the stable status-quo from 53e56e6a67c3 --- e.g. relevant for smt proof reconstruction in (line 6705 of "$AFP/Modular_arithmetic_LLL_and_HNF_algorithms/HNF_Mod_Det_Soundness.thy") of AFP/f1299d4f896c;
wenzelm
parents:
82889
diff
changeset
|
219 |
|
82891
372273ab6ebb
clarified natural decl_ord vs. slightly odd merge_decl_ord, following the historic status-quo of 53e56e6a67c3, which originally stems from c06d01f75764;
wenzelm
parents:
82890
diff
changeset
|
220 |
fun dest_decls decls = dest_decls_ord decl_ord decls; |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
221 |
|
82864 | 222 |
fun pretty_decls ctxt decls = |
223 |
kind_domain |> map_filter (fn kind => |
|
224 |
(case dest_decls decls (fn (_, {kind = kind', ...}) => kind = kind') of |
|
225 |
[] => NONE |
|
226 |
| ds => |
|
227 |
SOME (Pretty.big_list (kind_title kind ^ ":") |
|
228 |
(map (Thm.pretty_thm_item ctxt o #1) ds)))); |
|
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
229 |
|
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
230 |
fun merge_decls (decls1, decls2) = |
82891
372273ab6ebb
clarified natural decl_ord vs. slightly odd merge_decl_ord, following the historic status-quo of 53e56e6a67c3, which originally stems from c06d01f75764;
wenzelm
parents:
82890
diff
changeset
|
231 |
decls1 |> fold_map add_decls (dest_decls_ord decl_merge_ord decls2 (not o dup_decls decls1)); |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
232 |
|
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
233 |
fun extend_decls (thm, decl) decls = |
82839
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
234 |
if dup_decls decls (thm, decl) then (NONE, decls) |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
235 |
else add_decls (thm, decl) decls |>> SOME; |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
236 |
|
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
237 |
fun remove_decls thm (decls as Decls {next, rules}) = |
82839
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
238 |
(case get_decls decls thm of |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
239 |
[] => ([], decls) |
82855
df2d774bcf21
proper thm ord that conforms to Thm.eq_thm_prop (amending f5fd9b41188a): relevant for declarations in a locale contex with assumptions, e.g. "locale test = assumes True begin declare refl [rule del] end";
wenzelm
parents:
82854
diff
changeset
|
240 |
| old_decls => (old_decls, Decls {next = next, rules = Proptab.delete thm rules})); |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
241 |
|
82855
df2d774bcf21
proper thm ord that conforms to Thm.eq_thm_prop (amending f5fd9b41188a): relevant for declarations in a locale contex with assumptions, e.g. "locale test = assumes True begin declare refl [rule del] end";
wenzelm
parents:
82854
diff
changeset
|
242 |
val empty_decls = Decls {next = ~1, rules = Proptab.empty}; |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
243 |
|
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
244 |
end; |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
245 |
|
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
246 |
end; |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
247 |
|
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
248 |
|
82827 | 249 |
(* discrimination nets for intr/elim rules *) |
250 |
||
251 |
type netpair = (tag * rule) Net.net * (tag * rule) Net.net; |
|
252 |
||
253 |
val empty_netpair: netpair = (Net.empty, Net.empty); |
|
254 |
||
82888 | 255 |
fun pretty_netpair ctxt title (inet, enet) = |
256 |
let |
|
257 |
fun pretty_entry ({weight, ...}: tag, (_, thm): rule) = |
|
258 |
Pretty.item [Pretty.str (string_of_int weight), Pretty.brk 1, Thm.pretty_thm ctxt thm]; |
|
259 |
||
260 |
fun pretty_net elim net = |
|
261 |
(case Net.content net |> sort_distinct (tag_ord o apply2 #1) |> map pretty_entry of |
|
262 |
[] => NONE |
|
263 |
| prts => SOME (Pretty.big_list (title ^ " " ^ (if elim then "elim" else "intro")) prts)); |
|
264 |
in map_filter I [pretty_net false inet, pretty_net true enet] end; |
|
265 |
||
82827 | 266 |
|
267 |
(** Natural Deduction using (bires_flg, rule) pairs **) |
|
268 |
||
269 |
(** To preserve the order of the rules, tag them with decreasing integers **) |
|
270 |
||
271 |
fun insert_tagged_rule (tagged_rule as (_, (eres, th))) ((inet, enet): netpair) = |
|
272 |
if eres then |
|
273 |
(case try Thm.major_prem_of th of |
|
274 |
SOME prem => (inet, Net.insert_term (K false) (prem, tagged_rule) enet) |
|
275 |
| NONE => error "insert_tagged_rule: elimination rule with no premises") |
|
276 |
else (Net.insert_term (K false) (Thm.concl_of th, tagged_rule) inet, enet); |
|
277 |
||
82848
0a8977dcb21a
more accurate delete operation via authentic index --- minor performance tuning;
wenzelm
parents:
82844
diff
changeset
|
278 |
fun delete_tagged_rule (index, th) ((inet, enet): netpair) = |
0a8977dcb21a
more accurate delete operation via authentic index --- minor performance tuning;
wenzelm
parents:
82844
diff
changeset
|
279 |
let |
0a8977dcb21a
more accurate delete operation via authentic index --- minor performance tuning;
wenzelm
parents:
82844
diff
changeset
|
280 |
fun eq ((), ({index = index', ...}, _)) = index = index'; |
0a8977dcb21a
more accurate delete operation via authentic index --- minor performance tuning;
wenzelm
parents:
82844
diff
changeset
|
281 |
val inet' = Net.delete_term_safe eq (Thm.concl_of th, ()) inet; |
0a8977dcb21a
more accurate delete operation via authentic index --- minor performance tuning;
wenzelm
parents:
82844
diff
changeset
|
282 |
val enet' = |
0a8977dcb21a
more accurate delete operation via authentic index --- minor performance tuning;
wenzelm
parents:
82844
diff
changeset
|
283 |
(case try Thm.major_prem_of th of |
0a8977dcb21a
more accurate delete operation via authentic index --- minor performance tuning;
wenzelm
parents:
82844
diff
changeset
|
284 |
SOME prem => Net.delete_term_safe eq (prem, ()) enet |
0a8977dcb21a
more accurate delete operation via authentic index --- minor performance tuning;
wenzelm
parents:
82844
diff
changeset
|
285 |
| NONE => enet); |
0a8977dcb21a
more accurate delete operation via authentic index --- minor performance tuning;
wenzelm
parents:
82844
diff
changeset
|
286 |
in (inet', enet') end; |
82827 | 287 |
|
288 |
||
289 |
(*biresolution using a pair of nets rather than rules: |
|
290 |
boolean "match" indicates matching or unification.*) |
|
291 |
fun biresolution_from_nets_tac ctxt ord pred match ((inet, enet): netpair) = |
|
292 |
SUBGOAL |
|
293 |
(fn (prem, i) => |
|
294 |
let |
|
295 |
val hyps = Logic.strip_assums_hyp prem; |
|
296 |
val concl = Logic.strip_assums_concl prem; |
|
297 |
val tagged_rules = Net.unify_term inet concl @ maps (Net.unify_term enet) hyps; |
|
298 |
val order = make_order_list ord pred; |
|
299 |
in PRIMSEQ (Thm.biresolution (SOME ctxt) match (order tagged_rules) i) end); |
|
300 |
||
301 |
(*versions taking pre-built nets. No filtering of brls*) |
|
302 |
fun biresolve_from_nets_tac ctxt = biresolution_from_nets_tac ctxt tag_ord NONE false; |
|
303 |
fun bimatch_from_nets_tac ctxt = biresolution_from_nets_tac ctxt tag_ord NONE true; |
|
304 |
||
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82819
diff
changeset
|
305 |
|
82819 | 306 |
(** Simpler version for resolve_tac -- only one net, and no hyps **) |
82805 | 307 |
|
82811 | 308 |
type net = (int * thm) Net.net; |
82805 | 309 |
|
310 |
(*build a net of rules for resolution*) |
|
82811 | 311 |
fun build_net rls : net = |
312 |
fold_rev (fn (k, th) => Net.insert_term (K false) (Thm.concl_of th, (k, th))) |
|
313 |
(tag_list 1 rls) Net.empty; |
|
82805 | 314 |
|
315 |
(*resolution using a net rather than rules; pred supports filt_resolve_tac*) |
|
316 |
fun filt_resolution_from_net_tac ctxt match pred net = |
|
317 |
SUBGOAL (fn (prem, i) => |
|
318 |
let val krls = Net.unify_term net (Logic.strip_assums_concl prem) in |
|
319 |
if pred krls then |
|
320 |
PRIMSEQ (Thm.biresolution (SOME ctxt) match (map (pair false) (order_list krls)) i) |
|
321 |
else no_tac |
|
322 |
end); |
|
323 |
||
324 |
(*Resolve the subgoal using the rules (making a net) unless too flexible, |
|
325 |
which means more than maxr rules are unifiable. *) |
|
326 |
fun filt_resolve_from_net_tac ctxt maxr net = |
|
327 |
let fun pred krls = length krls <= maxr |
|
328 |
in filt_resolution_from_net_tac ctxt false pred net end; |
|
329 |
||
330 |
(*versions taking pre-built nets*) |
|
331 |
fun resolve_from_net_tac ctxt = filt_resolution_from_net_tac ctxt false (K true); |
|
332 |
fun match_from_net_tac ctxt = filt_resolution_from_net_tac ctxt true (K true); |
|
333 |
||
334 |
end; |