src/HOL/Tools/monomorph.ML
author blanchet
Tue, 31 May 2011 16:38:36 +0200
changeset 43107 5792d6bb4fb1
parent 43041 218e3943d504
child 43116 e0add071fa10
permissions -rw-r--r--
fixed comment
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
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
     4
Monomorphization of theorems, i.e., computation of all (necessary)
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
     5
instances.  This procedure is incomplete in general, but works well for
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
     6
most practical problems.
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
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
     8
For a list of universally closed theorems (without schematic term
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
     9
variables), monomorphization computes a list of theorems with schematic
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
    10
term variables: all polymorphic constants (i.e., constants occurring both
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
    11
with ground types and schematic type variables) are instantiated with all
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
    12
(necessary) ground types; thereby theorems containing these constants are
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
    13
copied.  To prevent nontermination, there is an upper limit for the number
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
    14
of iterations involved in the fixpoint construction.
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
    15
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
    16
The search for instances is performed on the constants with schematic
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
    17
types, which are extracted from the initial set of theorems.  The search
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
    18
constructs, for each theorem with those constants, a set of substitutions,
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
which, in the end, is applied to all corresponding theorems.  Remaining
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
    20
schematic type variables are substituted with fresh types.
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
    21
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
    22
Searching for necessary substitutions is an iterative fixpoint
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
    23
construction: each iteration computes all required instances required by
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
    24
the ground instances computed in the previous step and which haven't been
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
found before.  Computed substitutions are always nontrivial: schematic type
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
variables are never mapped to schematic type variables.
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
*)
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
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
    29
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
    30
sig
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
  (* utility function *)
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 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
    33
  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
    34
  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
    35
    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
    36
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
  (* 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
    38
  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
    39
  val max_new_instances: 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
    40
  val complete_instances: bool 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
    41
  val verbose: bool 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
    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 *)
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
    44
  val monomorph: (term -> typ list Symtab.table) -> (int * thm) list ->
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
    45
    Proof.context -> (int * thm) list list * Proof.context
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
fun fold_env _ [] y = y
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
  | fold_env f (x :: xs) y = fold_env f xs (f xs x y)
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
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
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
    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_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
    59
  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
    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 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
    62
  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
    63
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
    64
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
    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
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
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
    68
(* 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
    69
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_rounds = Attrib.setup_config_int @{binding monomorph_max_rounds} (K 5)
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
    71
val max_new_instances =
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
  Attrib.setup_config_int @{binding monomorph_max_new_instances} (K 300)
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
    73
val complete_instances =
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
    74
  Attrib.setup_config_bool @{binding monomorph_complete_instances} (K true)
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
    75
val verbose = Attrib.setup_config_bool @{binding monomorph_verbose} (K true)
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
    76
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
    77
fun show_info ctxt msg = if Config.get ctxt verbose then tracing msg else ()
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
    78
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
    79
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
    80
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
    81
(* monomorphization *)
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
    82
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
    83
(** preparing the problem **)
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
    84
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
    85
datatype thm_info =
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
  Ground of thm |
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
  Schematic of {
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
    88
    index: int,
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
    89
    theorem: thm,
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
    tvars: (indexname * sort) list,
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
    91
    schematics: 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
    92
    initial_round: int }
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
    93
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
    94
fun make_thm_info index initial_round schematics thm =
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
    95
  if Symtab.is_empty schematics then Ground thm
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
    96
  else Schematic {
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
    97
    index = index,
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
    98
    theorem = thm,
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
    99
    tvars = Term.add_tvars (Thm.prop_of thm) [],
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
   100
    schematics = schematics,
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
   101
    initial_round = initial_round }
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
   102
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
   103
fun prepare schematic_consts_of rthms =
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
   104
  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
   105
    val empty_subst = ((0, false, false), Vartab.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
   106
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
   107
    fun prep (r, thm) ((i, idx), (consts, substs)) =
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
   108
      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
   109
        (* increase indices to avoid clashes of type variables *)
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
   110
        val thm' = Thm.incr_indexes idx thm
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
   111
        val idx' = Thm.maxidx_of thm' + 1
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
   112
        val schematics = schematic_consts_of (Thm.prop_of thm')
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
   113
        val consts' =
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
   114
          Symtab.fold (fn (n, _) => Symtab.update (n, [])) schematics consts
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
   115
        val substs' = Inttab.update (i, [empty_subst]) substs
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
   116
      in
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
   117
        (make_thm_info i r schematics thm', ((i+1, idx'), (consts', substs')))
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
   118
      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
   119
  in fold_map prep rthms ((0, 0), (Symtab.empty, Inttab.empty)) ||> snd 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
   120
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
   121
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
   122
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
   123
(** collecting substitutions **)
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
   124
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
   125
fun add_relevant_instances known_grounds (Const (c as (n, 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
   126
      if typ_has_tvars T orelse not (Symtab.defined known_grounds n) then 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
   127
      else if member (op =) (Symtab.lookup_list known_grounds n) T then 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
   128
      else Symtab.insert_list (op =) c
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
   129
  | add_relevant_instances _ _ = 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
   130
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
   131
fun collect_instances known_grounds thm =
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
   132
  Term.fold_aterms (add_relevant_instances known_grounds) (Thm.prop_of thm)
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
   133
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
   134
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
   135
fun exceeded_limit (limit, _, _) = (limit <= 0)
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
   136
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
   137
fun with_substs index f (limit, substitutions, next_grounds) =
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
   138
  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
   139
    val substs = Inttab.lookup_list substitutions index
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
   140
    val (limit', substs', next_grounds') = f (limit, substs, next_grounds)
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
   141
  in (limit', Inttab.update (index, substs') substitutions, next_grounds') 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
   142
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
   143
fun with_grounds grounds f cx =
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
   144
  if exceeded_limit cx then cx else Symtab.fold f grounds cx
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
   145
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
   146
fun with_all_combinations schematics f (n, Ts) cx =
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
   147
  if exceeded_limit cx then cx
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
   148
  else fold_product f (Symtab.lookup_list schematics n) Ts cx
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
   149
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
   150
fun with_partial_substs f T U (cx as (limit, substs, next_grounds)) =
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
   151
  if exceeded_limit cx then cx
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
   152
  else fold_env (f (T, U)) substs (limit, [], next_grounds)
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
   153
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
   154
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
   155
fun same_subst subst =
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
   156
  Vartab.forall (fn (n, (_, 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
   157
    Vartab.lookup subst n |> Option.map (equal T o snd) |> the_default 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
   158
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
   159
(* FIXME: necessary? would it have an impact?
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
   160
   comparing substitutions can be tricky ... *)
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
   161
fun known substs1 substs2 subst = 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
   162
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
   163
fun refine ctxt known_grounds new_grounds info =
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
   164
  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
   165
    val thy = Proof_Context.theory_of ctxt
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
   166
    val count_partial = Config.get ctxt complete_instances
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
   167
    val (round, index, _, tvars, schematics) = info
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
   168
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
   169
    fun refine_subst TU = try (Sign.typ_match thy TU)
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
   170
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
   171
    fun add_new_ground subst n 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
   172
      let val T' = Envir.subst_type subst 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
   173
      in
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
   174
        (* FIXME: maybe keep types in a table or net for known_grounds,
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
   175
           that might improve efficiency here
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
   176
        *)
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
   177
        if member (op =) (Symtab.lookup_list known_grounds n) T' then 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
   178
        else Symtab.cons_list (n, 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
   179
      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
   180
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
   181
    fun refine_step subst limit next_grounds substs =
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
   182
      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
   183
        val full = forall (Vartab.defined subst o fst) tvars
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
   184
        val limit' =
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
   185
          if full orelse count_partial then limit - 1 else limit
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
   186
        val sub = ((round, full, false), subst)
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
   187
        val next_grounds' =
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
   188
          (schematics, next_grounds)
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
   189
          |-> Symtab.fold (uncurry (fold o add_new_ground subst))
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
   190
      in (limit', sub :: substs, next_grounds') 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
   191
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
   192
    fun refine_substs TU substs sub (cx as (limit, substs', next_grounds)) =
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
   193
      let val ((generation, full, _), subst) = sub
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
   194
      in
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
   195
        if exceeded_limit cx orelse full then
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
   196
          (limit, sub :: substs', next_grounds)
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
   197
        else
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
   198
          (case refine_subst TU subst of
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
   199
            NONE => (limit, sub :: substs', next_grounds)
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
   200
          | SOME subst' =>
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
   201
              if (same_subst subst orf known substs substs') subst' then
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
   202
                (limit, sub :: substs', next_grounds)
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
   203
              else
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
   204
                substs'
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
   205
                |> cons ((generation, full, true), subst)
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
   206
                |> refine_step subst' limit next_grounds)
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
   207
      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
   208
  in
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
   209
    with_substs index (
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
   210
      with_grounds new_grounds (with_all_combinations schematics (
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
   211
        with_partial_substs refine_substs)))
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
   212
  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
   213
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
   214
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
   215
fun make_subst_ctxt ctxt thm_infos (known_grounds, substitutions) =
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
   216
  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
   217
    val limit = Config.get ctxt max_new_instances
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
   218
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
   219
    fun add_ground_consts (Ground thm) = collect_instances known_grounds thm
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
   220
      | add_ground_consts (Schematic _) = 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
   221
    val initial_grounds = fold add_ground_consts thm_infos 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
   222
  in (thm_infos, (known_grounds, (limit, substitutions, initial_grounds))) 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
   223
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
   224
fun with_new round f thm_info =
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
   225
  (case thm_info of
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
   226
    Schematic {index, theorem, tvars, schematics, initial_round} =>
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
   227
      if initial_round <> round then 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
   228
      else f (round, index, theorem, tvars, schematics)
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
   229
  | Ground _ => 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
   230
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
   231
fun with_active round f thm_info =
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
   232
  (case thm_info of
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
   233
    Schematic {index, theorem, tvars, schematics, initial_round} =>
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
   234
      if initial_round < round then 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
   235
      else f (round, index, theorem, tvars, schematics)
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
   236
  | Ground _ => 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
   237
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
   238
fun collect_substitutions ctxt round thm_infos (known_grounds, subst_ctxt) =
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
   239
  let val (limit, substitutions, next_grounds) = subst_ctxt
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
   240
  in
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
   241
    (*
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
   242
      'known_grounds' are all constant names known to occur schematically
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
   243
      associated with all ground instances considered so far
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
   244
    *)
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
   245
    if exceeded_limit subst_ctxt then (true, (known_grounds, subst_ctxt))
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
   246
    else
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
   247
      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
   248
        fun collect (_, _, thm, _, _) = collect_instances known_grounds thm
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
   249
        val new = fold (with_new round collect) thm_infos next_grounds
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
   250
        val known' = Symtab.merge_list (op =) (known_grounds, new)
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
   251
      in
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
   252
        if Symtab.is_empty new then (true, (known_grounds, subst_ctxt))
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
   253
        else
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
   254
          (limit, substitutions, 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
   255
          |> fold (with_active round (refine ctxt known_grounds new)) thm_infos
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
   256
          |> fold (with_new round (refine ctxt Symtab.empty known')) thm_infos
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
   257
          |> pair false o pair known'
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
   258
      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
   259
  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
   260
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
   261
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
   262
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
   263
(** instantiating schematic theorems **)
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
   264
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
   265
fun super_sort (Ground _) S = S
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
   266
  | super_sort (Schematic {tvars, ...}) S = merge (op =) (S, maps snd tvars)
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
   267
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
   268
fun new_super_type ctxt thm_infos =
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
   269
  let val S = fold super_sort thm_infos @{sort type}
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
   270
  in yield_singleton Variable.invent_types S ctxt |>> SOME o TFree 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
   271
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
   272
fun add_missing_tvar T (ix, S) subst =
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
   273
  if Vartab.defined subst ix then subst
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
   274
  else Vartab.update (ix, (S, T)) subst
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
   275
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
   276
fun complete tvars subst 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
   277
  subst
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
   278
  |> Vartab.map (K (apsnd (Term.map_atyps (fn TVar _ => T | U => U))))
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
   279
  |> fold (add_missing_tvar T) tvars
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
   280
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
   281
fun instantiate_all' (mT, ctxt) substitutions thm_infos =
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
   282
  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
   283
    val thy = Proof_Context.theory_of ctxt
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
   284
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
   285
    fun cert (ix, (S, T)) = pairself (Thm.ctyp_of thy) (TVar (ix, S), 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
   286
    fun cert' subst = Vartab.fold (cons o cert) subst []
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
   287
    fun instantiate thm subst = Thm.instantiate (cert' subst, []) thm
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
   288
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
   289
    fun with_subst tvars f ((generation, full, _), subst) =
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
   290
      if full then SOME (generation, f subst)
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
   291
      else Option.map (pair generation o f o complete tvars subst) mT
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
   292
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
   293
    fun inst (Ground thm) = [(0, thm)]
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
   294
      | inst (Schematic {theorem, tvars, index, ...}) =
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
   295
          Inttab.lookup_list substitutions index
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
   296
          |> map_filter (with_subst tvars (instantiate theorem))
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
   297
  in (map inst thm_infos, ctxt) 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
   298
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
   299
fun instantiate_all ctxt thm_infos (_, (_, substitutions, _)) =
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
   300
  if Config.get ctxt complete_instances then
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
   301
    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
   302
      fun refined ((_, _, true), _) = true
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
   303
        | refined _ = 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
   304
    in
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
   305
      (Inttab.map (K (filter_out refined)) substitutions, thm_infos)
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
   306
      |-> instantiate_all' (new_super_type ctxt thm_infos)
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
   307
    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
   308
  else instantiate_all' (NONE, ctxt) substitutions thm_infos
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
   309
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
   310
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
   311
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
   312
(** overall procedure **)
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
   313
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
   314
fun limit_rounds ctxt f thm_infos =
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
   315
  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
   316
    val max = Config.get ctxt max_rounds
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
   317
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
   318
    fun round _ (true, x) = (thm_infos, x)
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
   319
      | round i (_, x) =
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
   320
          if i <= max then round (i + 1) (f ctxt i thm_infos x)
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
   321
          else (
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
   322
            show_info ctxt "Warning: Monomorphization limit reached";
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
   323
            (thm_infos, x))
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
   324
  in round 1 o pair false 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
   325
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
   326
fun monomorph schematic_consts_of rthms ctxt =
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
   327
  rthms
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
   328
  |> prepare schematic_consts_of
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
   329
  |-> make_subst_ctxt ctxt
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
   330
  |-> limit_rounds ctxt collect_substitutions
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
   331
  |-> instantiate_all ctxt
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
   332
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
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
   334