src/HOL/Tools/monomorph.ML
author nipkow
Wed, 25 Jun 2025 16:16:26 +0200
changeset 82736 1ca8d9705e34
parent 76052 6a20d0ebd5b3
permissions -rw-r--r--
added lemmas
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43107
5792d6bb4fb1 fixed comment
blanchet
parents: 43041
diff changeset
     1
(*  Title:      HOL/Tools/monomorph.ML
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
     2
    Author:     Sascha Boehme, TU Muenchen
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
     3
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
     4
Monomorphization of theorems, i.e., computation of ground instances for
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
     5
theorems with type variables.  This procedure is incomplete in general,
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
     6
but works well for most practical problems.
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
     7
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
     8
Monomorphization is essentially an enumeration of substitutions that map
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
     9
schematic types to ground types. Applying these substitutions to theorems
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    10
with type variables results in monomorphized ground instances. The
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    11
enumeration is driven by schematic constants (constants occurring with
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    12
type variables) and all ground instances of such constants (occurrences
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    13
without type variables). The enumeration is organized in rounds in which
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    14
all substitutions for the schematic constants are computed that are induced
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    15
by the ground instances. Any new ground instance may induce further
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    16
substitutions in a subsequent round. To prevent nontermination, there is
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    17
an upper limit of rounds involved and of the number of monomorphized ground
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    18
instances computed.
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    19
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    20
Theorems to be monomorphized must be tagged with a number indicating the
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    21
initial round in which they participate first. The initial round is
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    22
ignored for theorems without type variables. For any other theorem, the
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    23
initial round must be greater than zero. Returned monomorphized theorems
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    24
carry a number showing from which monomorphization round they emerged.
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    25
*)
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    26
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    27
signature MONOMORPH =
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    28
sig
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    29
  (* utility functions *)
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    30
  val typ_has_tvars: typ -> bool
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    31
  val all_schematic_consts_of: term -> typ list Symtab.table
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    32
  val add_schematic_consts_of: term -> typ list Symtab.table ->
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    33
    typ list Symtab.table
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    34
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    35
  (* configuration options *)
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    36
  val max_rounds: int Config.T
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    37
  val max_new_instances: int Config.T
53480
247817dbb990 limit the number of instances of a single theorem
blanchet
parents: 51575
diff changeset
    38
  val max_thm_instances: int Config.T
76052
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
    39
  val max_schematics: int Config.T
54061
6807b8e95adb prevent explosion in monomorphizer (e.g. when the facts typerep_int_def typerep_num_def typerep_option_def typerep_node_def are selected)
blanchet
parents: 53833
diff changeset
    40
  val max_new_const_instances_per_round: int Config.T
73376
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
    41
  val max_duplicated_instances: int Config.T
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    42
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    43
  (* monomorphization *)
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    44
  val monomorph: (term -> typ list Symtab.table) -> Proof.context ->
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    45
    (int * thm) list -> (int * thm) list list
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    46
end
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    47
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    48
structure Monomorph: MONOMORPH =
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    49
struct
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    50
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    51
(* utility functions *)
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    52
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    53
val typ_has_tvars = Term.exists_subtype (fn TVar _ => true | _ => false)
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    54
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    55
fun add_schematic_const (c as (_, T)) =
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    56
  if typ_has_tvars T then Symtab.insert_list (op =) c else I
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    57
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    58
fun add_schematic_consts_of t =
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    59
  Term.fold_aterms (fn Const c => add_schematic_const c | _ => I) t
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    60
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    61
fun all_schematic_consts_of t = add_schematic_consts_of t Symtab.empty
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    62
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    63
fun clear_grounds grounds = Symtab.map (K (K [])) grounds
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    64
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    65
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    66
(* configuration options *)
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    67
67149
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 60642
diff changeset
    68
val max_rounds = Attrib.setup_config_int \<^binding>\<open>monomorph_max_rounds\<close> (K 5)
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    69
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    70
val max_new_instances =
67149
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 60642
diff changeset
    71
  Attrib.setup_config_int \<^binding>\<open>monomorph_max_new_instances\<close> (K 500)
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    72
53480
247817dbb990 limit the number of instances of a single theorem
blanchet
parents: 51575
diff changeset
    73
val max_thm_instances =
67149
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 60642
diff changeset
    74
  Attrib.setup_config_int \<^binding>\<open>monomorph_max_thm_instances\<close> (K 20)
54061
6807b8e95adb prevent explosion in monomorphizer (e.g. when the facts typerep_int_def typerep_num_def typerep_option_def typerep_node_def are selected)
blanchet
parents: 53833
diff changeset
    75
76052
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
    76
val max_schematics =
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
    77
  Attrib.setup_config_int \<^binding>\<open>monomorph_max_schematics\<close> (K 1000)
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
    78
54061
6807b8e95adb prevent explosion in monomorphizer (e.g. when the facts typerep_int_def typerep_num_def typerep_option_def typerep_node_def are selected)
blanchet
parents: 53833
diff changeset
    79
val max_new_const_instances_per_round =
67149
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 60642
diff changeset
    80
  Attrib.setup_config_int \<^binding>\<open>monomorph_max_new_const_instances_per_round\<close> (K 5)
53480
247817dbb990 limit the number of instances of a single theorem
blanchet
parents: 51575
diff changeset
    81
73376
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
    82
val max_duplicated_instances =
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
    83
  Attrib.setup_config_int \<^binding>\<open>monomorph_max_duplicated_instances\<close> (K 16000)
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
    84
43116
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
    85
fun limit_rounds ctxt f =
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    86
  let
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    87
    val max = Config.get ctxt max_rounds
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
    88
    fun round i x = if i > max then x else round (i + 1) (f ctxt i x)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
    89
  in round 1 end
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    90
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    91
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    92
(* theorem information and related functions *)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    93
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    94
datatype thm_info =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    95
  Ground of thm |
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    96
  Ignored |
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    97
  Schematic of {
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    98
    id: int,
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
    99
    theorem: thm,
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   100
    tvars: (indexname * sort) list,
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   101
    schematics: (string * typ) list,
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   102
    initial_round: int}
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   103
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   104
fun fold_grounds f = fold (fn Ground thm => f thm | _ => I)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   105
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   106
fun fold_schematic f thm_info =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   107
  (case thm_info of
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   108
    Schematic {id, theorem, tvars, schematics, initial_round} =>
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   109
      f id theorem tvars schematics initial_round
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   110
  | _ => I)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   111
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   112
fun fold_schematics pred f =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   113
  let
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   114
    fun apply id thm tvars schematics initial_round x =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   115
      if pred initial_round then f id thm tvars schematics x else x
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   116
  in fold (fold_schematic apply) end
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   117
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   118
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   119
(* collecting data *)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   120
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   121
(*
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   122
  Theorems with type variables that cannot be instantiated should be ignored.
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   123
  A type variable has only a chance to be instantiated if it occurs in the
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   124
  type of one of the schematic constants.
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   125
*)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   126
fun groundable all_tvars schematics =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   127
  let val tvars' = Symtab.fold (fold Term.add_tvarsT o snd) schematics []
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   128
  in forall (member (op =) tvars') all_tvars end
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   129
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   130
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   131
fun prepare schematic_consts_of rthms =
43116
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   132
  let
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   133
    fun prep (initial_round, thm) ((id, idx), consts) =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   134
      if Term.exists_type typ_has_tvars (Thm.prop_of thm) then
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   135
        let
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   136
          (* increase indices to avoid clashes of type variables *)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   137
          val thm' = Thm.incr_indexes idx thm
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   138
          val idx' = Thm.maxidx_of thm' + 1
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   139
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   140
          val tvars = Term.add_tvars (Thm.prop_of thm') []
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   141
          val schematics = schematic_consts_of (Thm.prop_of thm')
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   142
          val schematics' =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   143
            Symtab.fold (fn (n, Ts) => fold (cons o pair n) Ts) schematics []
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   144
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   145
          (* collect the names of all constants that need to be instantiated *)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   146
          val consts' =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   147
            consts
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   148
            |> Symtab.fold (fn (n, _) => Symtab.update (n, [])) schematics
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   149
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   150
          val thm_info =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   151
            if not (groundable tvars schematics) then Ignored
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   152
            else
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   153
              Schematic {
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   154
                id = id,
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   155
                theorem = thm',
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   156
                tvars = tvars,
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   157
                schematics = schematics',
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   158
                initial_round = initial_round}
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   159
        in (thm_info, ((id + 1, idx'), consts')) end
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   160
      else (Ground thm, ((id + 1, idx + Thm.maxidx_of thm + 1), consts))
57209
7ffa0f7e2775 removed '_new' sufffix in SMT2 solver names (in some cases)
blanchet
parents: 54061
diff changeset
   161
  in
7ffa0f7e2775 removed '_new' sufffix in SMT2 solver names (in some cases)
blanchet
parents: 54061
diff changeset
   162
    fold_map prep rthms ((0, 0), Symtab.empty) ||> snd
7ffa0f7e2775 removed '_new' sufffix in SMT2 solver names (in some cases)
blanchet
parents: 54061
diff changeset
   163
  end
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   164
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   165
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   166
(* collecting instances *)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   167
59642
929984c529d3 clarified context;
wenzelm
parents: 59621
diff changeset
   168
fun instantiate ctxt subst =
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   169
  let
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 59642
diff changeset
   170
    fun cert (ix, (S, T)) = ((ix, S), Thm.ctyp_of ctxt T)
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   171
    fun cert' subst = Vartab.fold (cons o cert) subst []
74282
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 73376
diff changeset
   172
  in Thm.instantiate (TVars.make (cert' subst), Vars.empty) end
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   173
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   174
fun add_new_grounds used_grounds new_grounds thm =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   175
  let
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   176
    fun mem tab (n, T) = member (op =) (Symtab.lookup_list tab n) T
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   177
    fun add (Const (c as (n, _))) =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   178
          if mem used_grounds c orelse mem new_grounds c then I
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   179
          else if not (Symtab.defined used_grounds n) then I
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   180
          else Symtab.insert_list (op =) c
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   181
      | add _ = I
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   182
  in Term.fold_aterms add (Thm.prop_of thm) end
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   183
76052
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
   184
fun add_insts max_instances max_duplicated_instances max_thm_insts max_schematics ctxt round
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
   185
    used_grounds new_grounds id thm tvars schematics cx =
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   186
  let
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   187
    exception ENOUGH of
73376
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   188
      typ list Symtab.table * (int * int * ((int * (sort * typ) Vartab.table) * thm) list Inttab.table)
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   189
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   190
    val thy = Proof_Context.theory_of ctxt
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   191
73376
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   192
    fun add subst (cx as (next_grounds, (hits, misses, insts))) =
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   193
      if hits >= max_instances orelse misses >= max_duplicated_instances then
53481
0721fc9d0fe7 made semantics of "max_new_instances" be what the name suggests, the previous implementation did, and the Sledgehammer manual documents
blanchet
parents: 53480
diff changeset
   194
        raise ENOUGH cx
0721fc9d0fe7 made semantics of "max_new_instances" be what the name suggests, the previous implementation did, and the Sledgehammer manual documents
blanchet
parents: 53480
diff changeset
   195
      else
0721fc9d0fe7 made semantics of "max_new_instances" be what the name suggests, the previous implementation did, and the Sledgehammer manual documents
blanchet
parents: 53480
diff changeset
   196
        let
59642
929984c529d3 clarified context;
wenzelm
parents: 59621
diff changeset
   197
          val thm' = instantiate ctxt subst thm
53823
191ec7f873d5 when "max_thm_instances" is hit, choose more carefully which instances should be kept
blanchet
parents: 53482
diff changeset
   198
          val rthm = ((round, subst), thm')
73376
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   199
          val rthms = Inttab.lookup_list insts id
53481
0721fc9d0fe7 made semantics of "max_new_instances" be what the name suggests, the previous implementation did, and the Sledgehammer manual documents
blanchet
parents: 53480
diff changeset
   200
          val n_insts' =
53823
191ec7f873d5 when "max_thm_instances" is hit, choose more carefully which instances should be kept
blanchet
parents: 53482
diff changeset
   201
            if member (eq_snd Thm.eq_thm) rthms rthm then
73376
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   202
              (hits, misses + 1, insts)
53481
0721fc9d0fe7 made semantics of "max_new_instances" be what the name suggests, the previous implementation did, and the Sledgehammer manual documents
blanchet
parents: 53480
diff changeset
   203
            else
73376
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   204
              let
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   205
                val (hits', misses') =
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   206
                  if length rthms >= max_thm_insts then (hits, misses + 1) else (hits + 1, misses)
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   207
              in
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   208
                (hits', misses', Inttab.cons_list (id, rthm) insts)
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   209
              end
53481
0721fc9d0fe7 made semantics of "max_new_instances" be what the name suggests, the previous implementation did, and the Sledgehammer manual documents
blanchet
parents: 53480
diff changeset
   210
          val next_grounds' =
0721fc9d0fe7 made semantics of "max_new_instances" be what the name suggests, the previous implementation did, and the Sledgehammer manual documents
blanchet
parents: 53480
diff changeset
   211
            add_new_grounds used_grounds new_grounds thm' next_grounds
0721fc9d0fe7 made semantics of "max_new_instances" be what the name suggests, the previous implementation did, and the Sledgehammer manual documents
blanchet
parents: 53480
diff changeset
   212
        in (next_grounds', n_insts') end
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   213
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   214
    fun with_grounds (n, T) f subst (n', Us) =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   215
      let
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   216
        fun matching U = (* one-step refinement of the given substitution *)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   217
          (case try (Sign.typ_match thy (T, U)) subst of
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   218
            NONE => I
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   219
          | SOME subst' => f subst')
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   220
      in if n = n' then fold matching Us else I end
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   221
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   222
    fun with_matching_ground c subst f =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   223
      (* Try new grounds before already used grounds. Otherwise only
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   224
         substitutions already seen in previous rounds get enumerated. *)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   225
      Symtab.fold (with_grounds c (f true) subst) new_grounds #>
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   226
      Symtab.fold (with_grounds c (f false) subst) used_grounds
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   227
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   228
    fun is_complete subst =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   229
      (* Check if a substitution is defined for all TVars of the theorem,
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   230
         which guarantees that the instantiation with this substitution results
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   231
         in a ground theorem since all matchings that led to this substitution
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   232
         are with ground types only. *)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   233
      subset (op =) (tvars, Vartab.fold (cons o apsnd fst) subst [])
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   234
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   235
    fun for_schematics _ [] _ = I
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   236
      | for_schematics used_new (c :: cs) subst =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   237
          with_matching_ground c subst (fn new => fn subst' =>
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   238
            if is_complete subst' then
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   239
              if used_new orelse new then add subst'
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   240
              else I
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   241
            else for_schematics (used_new orelse new) cs subst') #>
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   242
          for_schematics used_new cs subst
43116
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   243
  in
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   244
    (* Enumerate all substitutions that lead to a ground instance of the
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   245
       theorem not seen before. A necessary condition for such a new ground
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   246
       instance is the usage of at least one ground from the new_grounds
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   247
       table. The approach used here is to match all schematics of the theorem
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   248
       with all relevant grounds. *)
76052
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
   249
    if length schematics > max_schematics then cx
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
   250
    else for_schematics false schematics Vartab.empty cx
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   251
    handle ENOUGH cx' => cx'
43116
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   252
  end
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   253
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   254
fun is_new round initial_round = (round = initial_round)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   255
fun is_active round initial_round = (round > initial_round)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   256
76052
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
   257
fun find_instances max_instances max_duplicated_instances max_thm_insts max_schematics
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
   258
    max_new_grounds thm_infos ctxt round (known_grounds, new_grounds0, insts) =
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   259
  let
54061
6807b8e95adb prevent explosion in monomorphizer (e.g. when the facts typerep_int_def typerep_num_def typerep_option_def typerep_node_def are selected)
blanchet
parents: 53833
diff changeset
   260
    val new_grounds =
6807b8e95adb prevent explosion in monomorphizer (e.g. when the facts typerep_int_def typerep_num_def typerep_option_def typerep_node_def are selected)
blanchet
parents: 53833
diff changeset
   261
      Symtab.map (fn _ => fn grounds =>
6807b8e95adb prevent explosion in monomorphizer (e.g. when the facts typerep_int_def typerep_num_def typerep_option_def typerep_node_def are selected)
blanchet
parents: 53833
diff changeset
   262
        if length grounds <= max_new_grounds then grounds
6807b8e95adb prevent explosion in monomorphizer (e.g. when the facts typerep_int_def typerep_num_def typerep_option_def typerep_node_def are selected)
blanchet
parents: 53833
diff changeset
   263
        else take max_new_grounds (sort Term_Ord.typ_ord grounds)) new_grounds0
6807b8e95adb prevent explosion in monomorphizer (e.g. when the facts typerep_int_def typerep_num_def typerep_option_def typerep_node_def are selected)
blanchet
parents: 53833
diff changeset
   264
76052
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
   265
    val add_new = add_insts max_instances max_duplicated_instances max_thm_insts max_schematics
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
   266
      ctxt round
73376
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   267
    fun consider_all pred f (cx as (_, (hits, misses, _))) =
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   268
      if hits >= max_instances orelse misses >= max_duplicated_instances then
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   269
        cx
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   270
      else
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   271
        fold_schematics pred f thm_infos cx
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   272
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   273
    val known_grounds' = Symtab.merge_list (op =) (known_grounds, new_grounds)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   274
    val empty_grounds = clear_grounds known_grounds'
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   275
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   276
    val (new_grounds', insts') =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   277
      (Symtab.empty, insts)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   278
      |> consider_all (is_active round) (add_new known_grounds new_grounds)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   279
      |> consider_all (is_new round) (add_new empty_grounds known_grounds')
57209
7ffa0f7e2775 removed '_new' sufffix in SMT2 solver names (in some cases)
blanchet
parents: 54061
diff changeset
   280
  in
7ffa0f7e2775 removed '_new' sufffix in SMT2 solver names (in some cases)
blanchet
parents: 54061
diff changeset
   281
    (known_grounds', new_grounds', insts')
7ffa0f7e2775 removed '_new' sufffix in SMT2 solver names (in some cases)
blanchet
parents: 54061
diff changeset
   282
  end
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   283
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   284
fun add_ground_types thm =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   285
  let fun add (n, T) = Symtab.map_entry n (insert (op =) T)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   286
  in Term.fold_aterms (fn Const c => add c | _ => I) (Thm.prop_of thm) end
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   287
76052
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
   288
fun collect_instances ctxt max_thm_insts max_schematics max_new_grounds thm_infos consts =
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   289
  let
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   290
    val known_grounds = fold_grounds add_ground_types thm_infos consts
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   291
    val empty_grounds = clear_grounds known_grounds
53482
blanchet
parents: 53481
diff changeset
   292
    val max_instances = Config.get ctxt max_new_instances
53481
0721fc9d0fe7 made semantics of "max_new_instances" be what the name suggests, the previous implementation did, and the Sledgehammer manual documents
blanchet
parents: 53480
diff changeset
   293
      |> fold (fn Schematic _ => Integer.add 1 | _ => I) thm_infos
73376
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   294
    val max_duplicated_instances = Config.get ctxt max_duplicated_instances
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   295
    val (_, _, (_, _, insts)) =
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   296
      limit_rounds ctxt (find_instances max_instances max_duplicated_instances max_thm_insts
76052
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
   297
          max_schematics max_new_grounds thm_infos)
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
   298
        (empty_grounds, known_grounds, (0, 0, Inttab.empty))
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   299
  in
73376
96ef620c8b1e added upper bound on monomorphisation duplicate instances
desharna
parents: 67149
diff changeset
   300
    insts
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   301
  end
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   302
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   303
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   304
(* monomorphization *)
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   305
53823
191ec7f873d5 when "max_thm_instances" is hit, choose more carefully which instances should be kept
blanchet
parents: 53482
diff changeset
   306
fun size_of_subst subst =
191ec7f873d5 when "max_thm_instances" is hit, choose more carefully which instances should be kept
blanchet
parents: 53482
diff changeset
   307
  Vartab.fold (Integer.add o size_of_typ o snd o snd) subst 0
191ec7f873d5 when "max_thm_instances" is hit, choose more carefully which instances should be kept
blanchet
parents: 53482
diff changeset
   308
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 57209
diff changeset
   309
fun subst_ord subst = int_ord (apply2 size_of_subst subst)
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   310
53823
191ec7f873d5 when "max_thm_instances" is hit, choose more carefully which instances should be kept
blanchet
parents: 53482
diff changeset
   311
fun instantiated_thms _ _ (Ground thm) = [(0, thm)]
191ec7f873d5 when "max_thm_instances" is hit, choose more carefully which instances should be kept
blanchet
parents: 53482
diff changeset
   312
  | instantiated_thms _ _ Ignored = []
191ec7f873d5 when "max_thm_instances" is hit, choose more carefully which instances should be kept
blanchet
parents: 53482
diff changeset
   313
  | instantiated_thms max_thm_insts insts (Schematic {id, ...}) =
191ec7f873d5 when "max_thm_instances" is hit, choose more carefully which instances should be kept
blanchet
parents: 53482
diff changeset
   314
    Inttab.lookup_list insts id
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 57209
diff changeset
   315
    |> (fn rthms =>
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 57209
diff changeset
   316
      if length rthms <= max_thm_insts then rthms
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 57209
diff changeset
   317
      else take max_thm_insts (sort (prod_ord int_ord subst_ord o apply2 fst) rthms))
53823
191ec7f873d5 when "max_thm_instances" is hit, choose more carefully which instances should be kept
blanchet
parents: 53482
diff changeset
   318
    |> map (apfst fst)
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   319
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   320
fun monomorph schematic_consts_of ctxt rthms =
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   321
  let
53823
191ec7f873d5 when "max_thm_instances" is hit, choose more carefully which instances should be kept
blanchet
parents: 53482
diff changeset
   322
    val max_thm_insts = Config.get ctxt max_thm_instances
76052
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
   323
    val max_schematics = Config.get ctxt max_schematics
54061
6807b8e95adb prevent explosion in monomorphizer (e.g. when the facts typerep_int_def typerep_num_def typerep_option_def typerep_node_def are selected)
blanchet
parents: 53833
diff changeset
   324
    val max_new_grounds = Config.get ctxt max_new_const_instances_per_round
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   325
    val (thm_infos, consts) = prepare schematic_consts_of rthms
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   326
    val insts =
76052
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
   327
      if Symtab.is_empty consts then
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
   328
        Inttab.empty
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
   329
      else
6a20d0ebd5b3 added a bound in SMT on the number of schematic constants considered -- the code (in for_schematics) is exponential in that number
blanchet
parents: 74282
diff changeset
   330
        collect_instances ctxt max_thm_insts max_schematics max_new_grounds thm_infos consts
53823
191ec7f873d5 when "max_thm_instances" is hit, choose more carefully which instances should be kept
blanchet
parents: 53482
diff changeset
   331
  in map (instantiated_thms max_thm_insts insts) thm_infos end
51575
907efc894051 new, simpler implementation of monomorphization;
boehmes
parents: 44717
diff changeset
   332
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
   333
end