src/Pure/bires.ML
author paulson <lp15@cam.ac.uk>
Wed, 06 Aug 2025 10:01:05 +0100
changeset 82914 cbf3703f92ea
parent 82896 cc89d72e17b8
permissions -rw-r--r--
fixed a markup issue
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
82805
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/bires.ML
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
82806
7189368734cd tuned signature: more explicit types;
wenzelm
parents: 82805
diff changeset
     3
    Author:     Makarius
82805
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
     4
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
     5
Biresolution and resolution using nets.
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
     6
*)
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
     7
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
     8
signature BIRES =
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
     9
sig
82806
7189368734cd tuned signature: more explicit types;
wenzelm
parents: 82805
diff changeset
    10
  type rule = bool * thm
82808
cb93bd70c561 clarified modules;
wenzelm
parents: 82807
diff changeset
    11
  val subgoals_of: rule -> int
cb93bd70c561 clarified modules;
wenzelm
parents: 82807
diff changeset
    12
  val subgoals_ord: rule ord
cb93bd70c561 clarified modules;
wenzelm
parents: 82807
diff changeset
    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
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
    23
  eqtype kind
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
    24
  val intro_bang_kind: kind
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
    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
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
    27
  val intro_kind: kind
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
    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
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
    30
  val intro_query_kind: kind
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
    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
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
    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
e567fd948ff0 clarified signature;
wenzelm
parents: 82868
diff changeset
    38
  val kind_make_elim: kind -> thm -> thm
82827
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
    39
  val kind_domain: kind list
82828
05d2b8b675da clarified signature;
wenzelm
parents: 82827
diff changeset
    40
  val kind_values: 'a -> 'a list
82827
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
    41
  val kind_map: kind -> ('a -> 'a) -> 'a list -> 'a list
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
    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
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
    44
  val kind_title: kind -> string
82896
wenzelm
parents: 82895
diff changeset
    45
  val kind_name: kind -> string
82827
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
    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
2703f19d323e clarified output;
wenzelm
parents: 82863
diff changeset
    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
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
    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
051177553f16 more diagnostic operations;
wenzelm
parents: 82873
diff changeset
    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
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
    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
14f24aa1adb3 misc tuning and clarification;
wenzelm
parents: 82810
diff changeset
    69
14f24aa1adb3 misc tuning and clarification;
wenzelm
parents: 82810
diff changeset
    70
  type net = (int * thm) Net.net
14f24aa1adb3 misc tuning and clarification;
wenzelm
parents: 82810
diff changeset
    71
  val build_net: thm list -> net
14f24aa1adb3 misc tuning and clarification;
wenzelm
parents: 82810
diff changeset
    72
  val filt_resolve_from_net_tac: Proof.context -> int -> net -> int -> tactic
14f24aa1adb3 misc tuning and clarification;
wenzelm
parents: 82810
diff changeset
    73
  val resolve_from_net_tac: Proof.context -> net -> int -> tactic
14f24aa1adb3 misc tuning and clarification;
wenzelm
parents: 82810
diff changeset
    74
  val match_from_net_tac: Proof.context -> net -> int -> tactic
82805
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
    75
end
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
    76
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
    77
structure Bires: BIRES =
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
    78
struct
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
    79
82827
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
    80
(** Rule kinds and declarations **)
82808
cb93bd70c561 clarified modules;
wenzelm
parents: 82807
diff changeset
    81
82812
ea8d633fd4a8 just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents: 82811
diff changeset
    82
(* type rule *)
82806
7189368734cd tuned signature: more explicit types;
wenzelm
parents: 82805
diff changeset
    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
7189368734cd tuned signature: more explicit types;
wenzelm
parents: 82805
diff changeset
    85
82808
cb93bd70c561 clarified modules;
wenzelm
parents: 82807
diff changeset
    86
fun subgoals_of (true, thm) = Thm.nprems_of thm - 1
cb93bd70c561 clarified modules;
wenzelm
parents: 82807
diff changeset
    87
  | subgoals_of (false, thm) = Thm.nprems_of thm;
cb93bd70c561 clarified modules;
wenzelm
parents: 82807
diff changeset
    88
cb93bd70c561 clarified modules;
wenzelm
parents: 82807
diff changeset
    89
val subgoals_ord = int_ord o apply2 subgoals_of;
cb93bd70c561 clarified modules;
wenzelm
parents: 82807
diff changeset
    90
82809
b908d50f42d4 minor performance tuning;
wenzelm
parents: 82808
diff changeset
    91
fun no_subgoals (true, thm) = Thm.one_prem thm
b908d50f42d4 minor performance tuning;
wenzelm
parents: 82808
diff changeset
    92
  | no_subgoals (false, thm) = Thm.no_prems thm;
82808
cb93bd70c561 clarified modules;
wenzelm
parents: 82807
diff changeset
    93
82806
7189368734cd tuned signature: more explicit types;
wenzelm
parents: 82805
diff changeset
    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
2703f19d323e clarified output;
wenzelm
parents: 82863
diff changeset
   137
  (intro_kind, ("unsafe introduction", "(intro)")),
2703f19d323e clarified output;
wenzelm
parents: 82863
diff changeset
   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
e567fd948ff0 clarified signature;
wenzelm
parents: 82868
diff changeset
   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
a299162422f0 clarified decl_ord wrt. kind_ord;
wenzelm
parents: 82888
diff changeset
   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
05d2b8b675da clarified signature;
wenzelm
parents: 82827
diff changeset
   156
fun kind_values x =
05d2b8b675da clarified signature;
wenzelm
parents: 82827
diff changeset
   157
  replicate (length (distinct (op =) (map kind_index kind_domain))) x;
05d2b8b675da clarified signature;
wenzelm
parents: 82827
diff changeset
   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
wenzelm
parents: 82895
diff changeset
   172
fun kind_name (Kind {is_elim, make_elim, ...}) =
wenzelm
parents: 82895
diff changeset
   173
  if is_elim andalso make_elim then "destruction rule"
wenzelm
parents: 82895
diff changeset
   174
  else if is_elim then "elimination rule"
wenzelm
parents: 82895
diff changeset
   175
  else "introduction rule";
wenzelm
parents: 82895
diff changeset
   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
a299162422f0 clarified decl_ord wrt. kind_ord;
wenzelm
parents: 82888
diff changeset
   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
wenzelm
parents: 82891
diff changeset
   201
fun dup_decls (Decls {rules, ...}) (thm, {kind, ...}: 'a decl) =
wenzelm
parents: 82891
diff changeset
   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
2703f19d323e clarified output;
wenzelm
parents: 82863
diff changeset
   222
fun pretty_decls ctxt decls =
2703f19d323e clarified output;
wenzelm
parents: 82863
diff changeset
   223
  kind_domain |> map_filter (fn kind =>
2703f19d323e clarified output;
wenzelm
parents: 82863
diff changeset
   224
    (case dest_decls decls (fn (_, {kind = kind', ...}) => kind = kind') of
2703f19d323e clarified output;
wenzelm
parents: 82863
diff changeset
   225
      [] => NONE
2703f19d323e clarified output;
wenzelm
parents: 82863
diff changeset
   226
    | ds =>
2703f19d323e clarified output;
wenzelm
parents: 82863
diff changeset
   227
        SOME (Pretty.big_list (kind_title kind ^ ":")
2703f19d323e clarified output;
wenzelm
parents: 82863
diff changeset
   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
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   249
(* discrimination nets for intr/elim rules *)
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   250
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   251
type netpair = (tag * rule) Net.net * (tag * rule) Net.net;
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   252
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   253
val empty_netpair: netpair = (Net.empty, Net.empty);
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   254
82888
051177553f16 more diagnostic operations;
wenzelm
parents: 82873
diff changeset
   255
fun pretty_netpair ctxt title (inet, enet) =
051177553f16 more diagnostic operations;
wenzelm
parents: 82873
diff changeset
   256
  let
051177553f16 more diagnostic operations;
wenzelm
parents: 82873
diff changeset
   257
    fun pretty_entry ({weight, ...}: tag, (_, thm): rule) =
051177553f16 more diagnostic operations;
wenzelm
parents: 82873
diff changeset
   258
      Pretty.item [Pretty.str (string_of_int weight), Pretty.brk 1, Thm.pretty_thm ctxt thm];
051177553f16 more diagnostic operations;
wenzelm
parents: 82873
diff changeset
   259
051177553f16 more diagnostic operations;
wenzelm
parents: 82873
diff changeset
   260
    fun pretty_net elim net =
051177553f16 more diagnostic operations;
wenzelm
parents: 82873
diff changeset
   261
      (case Net.content net |> sort_distinct (tag_ord o apply2 #1) |> map pretty_entry of
051177553f16 more diagnostic operations;
wenzelm
parents: 82873
diff changeset
   262
        [] => NONE
051177553f16 more diagnostic operations;
wenzelm
parents: 82873
diff changeset
   263
      | prts => SOME (Pretty.big_list (title ^ " " ^ (if elim then "elim" else "intro")) prts));
051177553f16 more diagnostic operations;
wenzelm
parents: 82873
diff changeset
   264
  in map_filter I [pretty_net false inet, pretty_net true enet] end;
051177553f16 more diagnostic operations;
wenzelm
parents: 82873
diff changeset
   265
82827
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   266
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   267
(** Natural Deduction using (bires_flg, rule) pairs **)
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   268
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   269
(** To preserve the order of the rules, tag them with decreasing integers **)
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   270
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   271
fun insert_tagged_rule (tagged_rule as (_, (eres, th))) ((inet, enet): netpair) =
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   272
  if eres then
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   273
    (case try Thm.major_prem_of th of
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   274
      SOME prem => (inet, Net.insert_term (K false) (prem, tagged_rule) enet)
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   275
    | NONE => error "insert_tagged_rule: elimination rule with no premises")
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   276
  else (Net.insert_term (K false) (Thm.concl_of th, tagged_rule) inet, enet);
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   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
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   287
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   288
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   289
(*biresolution using a pair of nets rather than rules:
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   290
   boolean "match" indicates matching or unification.*)
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   291
fun biresolution_from_nets_tac ctxt ord pred match ((inet, enet): netpair) =
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   292
  SUBGOAL
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   293
    (fn (prem, i) =>
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   294
      let
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   295
        val hyps = Logic.strip_assums_hyp prem;
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   296
        val concl = Logic.strip_assums_concl prem;
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   297
        val tagged_rules = Net.unify_term inet concl @ maps (Net.unify_term enet) hyps;
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   298
        val order = make_order_list ord pred;
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   299
      in PRIMSEQ (Thm.biresolution (SOME ctxt) match (order tagged_rules) i) end);
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   300
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   301
(*versions taking pre-built nets.  No filtering of brls*)
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   302
fun biresolve_from_nets_tac ctxt = biresolution_from_nets_tac ctxt tag_ord NONE false;
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   303
fun bimatch_from_nets_tac ctxt = biresolution_from_nets_tac ctxt tag_ord NONE true;
b7c1c23058cf tuned source structure;
wenzelm
parents: 82826
diff changeset
   304
82826
f5fd9b41188a efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents: 82819
diff changeset
   305
82819
f7a6b012600e tuned comments;
wenzelm
parents: 82817
diff changeset
   306
(** Simpler version for resolve_tac -- only one net, and no hyps **)
82805
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   307
82811
14f24aa1adb3 misc tuning and clarification;
wenzelm
parents: 82810
diff changeset
   308
type net = (int * thm) Net.net;
82805
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   309
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   310
(*build a net of rules for resolution*)
82811
14f24aa1adb3 misc tuning and clarification;
wenzelm
parents: 82810
diff changeset
   311
fun build_net rls : net =
14f24aa1adb3 misc tuning and clarification;
wenzelm
parents: 82810
diff changeset
   312
  fold_rev (fn (k, th) => Net.insert_term (K false) (Thm.concl_of th, (k, th)))
14f24aa1adb3 misc tuning and clarification;
wenzelm
parents: 82810
diff changeset
   313
    (tag_list 1 rls) Net.empty;
82805
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   314
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   315
(*resolution using a net rather than rules; pred supports filt_resolve_tac*)
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   316
fun filt_resolution_from_net_tac ctxt match pred net =
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   317
  SUBGOAL (fn (prem, i) =>
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   318
    let val krls = Net.unify_term net (Logic.strip_assums_concl prem) in
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   319
      if pred krls then
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   320
        PRIMSEQ (Thm.biresolution (SOME ctxt) match (map (pair false) (order_list krls)) i)
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   321
      else no_tac
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   322
    end);
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   323
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   324
(*Resolve the subgoal using the rules (making a net) unless too flexible,
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   325
   which means more than maxr rules are unifiable.      *)
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   326
fun filt_resolve_from_net_tac ctxt maxr net =
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   327
  let fun pred krls = length krls <= maxr
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   328
  in filt_resolution_from_net_tac ctxt false pred net end;
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   329
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   330
(*versions taking pre-built nets*)
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   331
fun resolve_from_net_tac ctxt = filt_resolution_from_net_tac ctxt false (K true);
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   332
fun match_from_net_tac ctxt = filt_resolution_from_net_tac ctxt true (K true);
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   333
61aae966dd95 clarified modules: explicit structure Bires;
wenzelm
parents:
diff changeset
   334
end;