src/HOL/Tools/monomorph.ML
author boehmes
Tue, 31 May 2011 19:27:19 +0200
changeset 43117 5de84843685f
parent 43116 e0add071fa10
child 43225 142b58087974
permissions -rw-r--r--
proper nesting of loops in new monomorphizer; less duplication of code in new monomorphizer; drop output of warnings of new monomorphizer
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
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
  (* 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
    43
  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
    44
    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
    45
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
    46
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
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
    48
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
    49
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
(* 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
    51
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
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
    53
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
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
    55
  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
    56
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
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
    58
  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
    59
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
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
    61
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
(* 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
    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
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
    67
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
    68
  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
    69
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
    70
  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
    71
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
(* 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
    75
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
(** 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
    77
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
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
    79
  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
    80
  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
    81
    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
    82
    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
    83
    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
    84
    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
    85
    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
    86
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
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
    88
  let
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
    89
    val empty_sub = ((0, false, false), Vartab.empty)
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
    90
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
    91
    fun prep (r, thm) ((i, idx), (consts, subs)) =
43116
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
    92
      if not (Term.exists_type typ_has_tvars (Thm.prop_of thm)) then
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
    93
        (Ground thm, ((i+1, idx + Thm.maxidx_of thm + 1), (consts, subs)))
43116
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
    94
      else
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
    95
        let
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
    96
          (* increase indices to avoid clashes of type variables *)
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
    97
          val thm' = Thm.incr_indexes idx thm
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
    98
          val idx' = Thm.maxidx_of thm' + 1
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
    99
          val schematics = schematic_consts_of (Thm.prop_of thm')
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   100
          val consts' =
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   101
            Symtab.fold (fn (n, _) => Symtab.update (n, [])) schematics consts
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   102
          val subs' = Inttab.update (i, [empty_sub]) subs
43116
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   103
          val thm_info = Schematic {
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   104
            index = i,
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   105
            theorem = thm',
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   106
            tvars = Term.add_tvars (Thm.prop_of thm') [],
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   107
            schematics = schematics,
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   108
            initial_round = r }
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   109
      in (thm_info, ((i+1, idx'), (consts', subs'))) end
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
   110
  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
   111
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
(** 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
   115
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   116
fun exceeded limit = (limit <= 0)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   117
fun exceeded_limit (limit, _, _) = exceeded limit
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   118
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   119
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   120
fun with_all_grounds cx grounds f =
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   121
  if exceeded_limit cx then I else Symtab.fold f grounds
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   122
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   123
fun with_all_type_combinations cx schematics f (n, Ts) =
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   124
  if exceeded_limit cx then I
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   125
  else fold_product f (Symtab.lookup_list schematics n) Ts
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   126
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   127
fun derive_new_substs thy cx new_grounds schematics subst =
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   128
  with_all_grounds cx new_grounds
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   129
    (with_all_type_combinations cx schematics (fn T => fn U =>
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   130
      (case try (Sign.typ_match thy (T, U)) subst of
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   131
        NONE => I
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   132
      | SOME subst' => cons subst'))) []
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   133
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   134
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   135
fun same_subst subst' (_, subst) = subst' |> Vartab.forall (fn (n, (_, T)) => 
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   136
  Vartab.lookup subst n |> Option.map (equal T o snd) |> the_default false)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   137
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   138
fun known_subst sub subs1 subs2 subst =
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   139
  same_subst subst sub orelse exists (same_subst subst) subs1 orelse
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   140
  exists (same_subst subst) subs2
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   141
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   142
fun within_limit f cx = if exceeded_limit cx then cx else f cx
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   143
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   144
fun fold_partial_substs derive add = within_limit (
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   145
  let
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   146
    fun fold_partial [] cx = cx
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   147
      | fold_partial (sub :: subs) (limit, subs', next) =
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   148
          if exceeded limit then (limit, sub :: subs @ subs', next)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   149
          else sub |> (fn ((generation, full, _), subst) =>
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   150
            if full then fold_partial subs (limit, sub :: subs', next)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   151
            else
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   152
              (case filter_out (known_subst sub subs subs') (derive subst) of
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   153
                [] => fold_partial subs (limit, sub :: subs', next)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   154
              | substs =>
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   155
                  (limit, ((generation, full, true), subst) :: subs', next)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   156
                  |> fold (within_limit o add) substs
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   157
                  |> fold_partial subs))
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   158
  in (fn (limit, subs, next) => fold_partial subs (limit, [], next)) end)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   159
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   160
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   161
fun refine ctxt round known_grounds new_grounds (tvars, schematics) cx =
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   162
  let
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   163
    val thy = Proof_Context.theory_of ctxt
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   164
    val count_partial = Config.get ctxt complete_instances
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   165
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   166
    fun add_new_ground subst n T =
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   167
      let val T' = Envir.subst_type subst T
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   168
      in
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   169
        (* FIXME: maybe keep types in a table or net for known_grounds,
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   170
           that might improve efficiency here
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   171
        *)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   172
        if typ_has_tvars T' then I
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   173
        else if member (op =) (Symtab.lookup_list known_grounds n) T' then I
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   174
        else Symtab.cons_list (n, T')
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   175
      end
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   176
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   177
    fun add_new_subst subst (limit, subs, next_grounds) =
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   178
      let
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   179
        val full = forall (Vartab.defined subst o fst) tvars
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   180
        val limit' =
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   181
          if full orelse count_partial then limit - 1 else limit
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   182
        val sub = ((round, full, false), subst)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   183
        val next_grounds' =
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   184
          (schematics, next_grounds)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   185
          |-> Symtab.fold (uncurry (fold o add_new_ground subst))
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   186
      in (limit', sub :: subs, next_grounds') end
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   187
  in
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   188
    fold_partial_substs (derive_new_substs thy cx new_grounds schematics)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   189
      add_new_subst cx
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   190
  end
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   191
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   192
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   193
(*
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   194
  'known_grounds' are all constant names known to occur schematically
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   195
  associated with all ground instances considered so far
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   196
*)
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
   197
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
   198
      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
   199
      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
   200
      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
   201
  | 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
   202
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
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
   204
  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
   205
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
43116
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   207
fun make_subst_ctxt ctxt thm_infos known_grounds substitutions =
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
   208
  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
   209
    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
   210
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
    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
   212
      | 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
   213
    val initial_grounds = fold add_ground_consts thm_infos Symtab.empty
43116
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   214
  in (known_grounds, (limit, substitutions, initial_grounds)) end
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
   215
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   216
fun is_new round initial_round = (round = initial_round)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   217
fun is_active round initial_round = (round > initial_round)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   218
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   219
fun fold_schematic pred f = fold (fn
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
   220
    Schematic {index, theorem, tvars, schematics, initial_round} =>
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   221
      if pred initial_round then f theorem (index, tvars, schematics) else I
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
   222
  | 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
   223
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   224
fun focus f _ (index, tvars, schematics) (limit, subs, next_grounds) =
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   225
  let
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   226
    val (limit', isubs', next_grounds') =
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   227
      (limit, Inttab.lookup_list subs index, next_grounds)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   228
      |> f (tvars, schematics)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   229
  in (limit', Inttab.update (index, isubs') subs, next_grounds') end
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   230
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   231
fun collect_substitutions thm_infos ctxt round subst_ctxt =
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   232
  let val (known_grounds, (limit, subs, next_grounds)) = subst_ctxt
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
   233
  in
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   234
    if exceeded limit then subst_ctxt
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
   235
    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
   236
      let
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   237
        fun collect thm _ = collect_instances known_grounds thm
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   238
        val new = fold_schematic (is_new round) collect thm_infos next_grounds
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   239
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
   240
        val known' = Symtab.merge_list (op =) (known_grounds, new)
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   241
        val step = focus o refine ctxt round known'
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
   242
      in
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   243
        (limit, subs, Symtab.empty)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   244
        |> not (Symtab.is_empty new) ?
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   245
            fold_schematic (is_active round) (step new) thm_infos
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   246
        |> fold_schematic (is_new round) (step known') thm_infos
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   247
        |> pair known'
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
   248
      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
   249
  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
   250
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
(** 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
   254
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
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
   256
  | 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
   257
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
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
   259
  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
   260
  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
   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
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
   263
  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
   264
  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
   265
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
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
   267
  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
   268
  |> 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
   269
  |> 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
   270
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   271
fun instantiate_all' (mT, ctxt) subs thm_infos =
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
   272
  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
   273
    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
   274
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
    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
   276
    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
   277
    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
   278
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
    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
   280
      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
   281
      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
   282
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
    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
   284
      | inst (Schematic {theorem, tvars, index, ...}) =
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   285
          Inttab.lookup_list subs index
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
   286
          |> 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
   287
  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
   288
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   289
fun instantiate_all ctxt thm_infos (_, (_, subs, _)) =
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
   290
  if Config.get ctxt complete_instances then
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   291
    let fun is_refined ((_, _, refined), _) = refined
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
   292
    in
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   293
      (Inttab.map (K (filter_out is_refined)) subs, thm_infos)
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
   294
      |-> 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
   295
    end
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   296
  else instantiate_all' (NONE, ctxt) subs thm_infos
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
   297
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
(** 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
   301
43116
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   302
fun limit_rounds ctxt f =
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
   303
  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
   304
    val max = Config.get ctxt max_rounds
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   305
    fun round i x = if i > max then x else round (i + 1) (f ctxt i x)
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   306
  in round 1 end
43041
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict as opposed to the SMT monomorphizer) and with fact annotations controlling in which round certain facts are considered for monomorphization
boehmes
parents:
diff changeset
   307
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
fun monomorph schematic_consts_of rthms ctxt =
43116
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   309
  let
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   310
    val (thm_infos, (known_grounds, subs)) = prepare schematic_consts_of rthms
43116
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   311
  in
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   312
    if Symtab.is_empty known_grounds then
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   313
      (map (single o pair 0 o snd) rthms, ctxt)
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   314
    else
43117
5de84843685f proper nesting of loops in new monomorphizer;
boehmes
parents: 43116
diff changeset
   315
      make_subst_ctxt ctxt thm_infos known_grounds subs
43116
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   316
      |> limit_rounds ctxt (collect_substitutions thm_infos)
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   317
      |> instantiate_all ctxt thm_infos
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   318
  end
e0add071fa10 use new monomorphizer for SMT;
boehmes
parents: 43107
diff changeset
   319
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
   320
218e3943d504 added re-implemented monomorphizer for types with better control for number of generated instances (strict 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
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
   322