src/HOL/Tools/monomorph.ML
changeset 73376 96ef620c8b1e
parent 67149 e61557884799
child 74282 c2ee8d993d6a
equal deleted inserted replaced
73375:a80fd78c85bd 73376:96ef620c8b1e
    35   (* configuration options *)
    35   (* configuration options *)
    36   val max_rounds: int Config.T
    36   val max_rounds: int Config.T
    37   val max_new_instances: int Config.T
    37   val max_new_instances: int Config.T
    38   val max_thm_instances: int Config.T
    38   val max_thm_instances: int Config.T
    39   val max_new_const_instances_per_round: int Config.T
    39   val max_new_const_instances_per_round: int Config.T
       
    40   val max_duplicated_instances: int Config.T
    40 
    41 
    41   (* monomorphization *)
    42   (* monomorphization *)
    42   val monomorph: (term -> typ list Symtab.table) -> Proof.context ->
    43   val monomorph: (term -> typ list Symtab.table) -> Proof.context ->
    43     (int * thm) list -> (int * thm) list list
    44     (int * thm) list -> (int * thm) list list
    44 end
    45 end
    71 val max_thm_instances =
    72 val max_thm_instances =
    72   Attrib.setup_config_int \<^binding>\<open>monomorph_max_thm_instances\<close> (K 20)
    73   Attrib.setup_config_int \<^binding>\<open>monomorph_max_thm_instances\<close> (K 20)
    73 
    74 
    74 val max_new_const_instances_per_round =
    75 val max_new_const_instances_per_round =
    75   Attrib.setup_config_int \<^binding>\<open>monomorph_max_new_const_instances_per_round\<close> (K 5)
    76   Attrib.setup_config_int \<^binding>\<open>monomorph_max_new_const_instances_per_round\<close> (K 5)
       
    77 
       
    78 val max_duplicated_instances =
       
    79   Attrib.setup_config_int \<^binding>\<open>monomorph_max_duplicated_instances\<close> (K 16000)
    76 
    80 
    77 fun limit_rounds ctxt f =
    81 fun limit_rounds ctxt f =
    78   let
    82   let
    79     val max = Config.get ctxt max_rounds
    83     val max = Config.get ctxt max_rounds
    80     fun round i x = if i > max then x else round (i + 1) (f ctxt i x)
    84     fun round i x = if i > max then x else round (i + 1) (f ctxt i x)
   171           else if not (Symtab.defined used_grounds n) then I
   175           else if not (Symtab.defined used_grounds n) then I
   172           else Symtab.insert_list (op =) c
   176           else Symtab.insert_list (op =) c
   173       | add _ = I
   177       | add _ = I
   174   in Term.fold_aterms add (Thm.prop_of thm) end
   178   in Term.fold_aterms add (Thm.prop_of thm) end
   175 
   179 
   176 fun add_insts max_instances max_thm_insts ctxt round used_grounds
   180 fun add_insts max_instances max_duplicated_instances max_thm_insts ctxt round used_grounds
   177     new_grounds id thm tvars schematics cx =
   181     new_grounds id thm tvars schematics cx =
   178   let
   182   let
   179     exception ENOUGH of
   183     exception ENOUGH of
   180       typ list Symtab.table * (int * ((int * (sort * typ) Vartab.table) * thm) list Inttab.table)
   184       typ list Symtab.table * (int * int * ((int * (sort * typ) Vartab.table) * thm) list Inttab.table)
   181 
   185 
   182     val thy = Proof_Context.theory_of ctxt
   186     val thy = Proof_Context.theory_of ctxt
   183 
   187 
   184     fun add subst (cx as (next_grounds, (n, insts))) =
   188     fun add subst (cx as (next_grounds, (hits, misses, insts))) =
   185       if n >= max_instances then
   189       if hits >= max_instances orelse misses >= max_duplicated_instances then
   186         raise ENOUGH cx
   190         raise ENOUGH cx
   187       else
   191       else
   188         let
   192         let
   189           val thm' = instantiate ctxt subst thm
   193           val thm' = instantiate ctxt subst thm
   190           val rthm = ((round, subst), thm')
   194           val rthm = ((round, subst), thm')
   191           val rthms = Inttab.lookup_list insts id;
   195           val rthms = Inttab.lookup_list insts id
   192           val n_insts' =
   196           val n_insts' =
   193             if member (eq_snd Thm.eq_thm) rthms rthm then
   197             if member (eq_snd Thm.eq_thm) rthms rthm then
   194               (n, insts)
   198               (hits, misses + 1, insts)
   195             else
   199             else
   196               (if length rthms >= max_thm_insts then n else n + 1,
   200               let
   197                Inttab.cons_list (id, rthm) insts)
   201                 val (hits', misses') =
       
   202                   if length rthms >= max_thm_insts then (hits, misses + 1) else (hits + 1, misses)
       
   203               in
       
   204                 (hits', misses', Inttab.cons_list (id, rthm) insts)
       
   205               end
   198           val next_grounds' =
   206           val next_grounds' =
   199             add_new_grounds used_grounds new_grounds thm' next_grounds
   207             add_new_grounds used_grounds new_grounds thm' next_grounds
   200         in (next_grounds', n_insts') end
   208         in (next_grounds', n_insts') end
   201 
   209 
   202     fun with_grounds (n, T) f subst (n', Us) =
   210     fun with_grounds (n, T) f subst (n', Us) =
   239   end
   247   end
   240 
   248 
   241 fun is_new round initial_round = (round = initial_round)
   249 fun is_new round initial_round = (round = initial_round)
   242 fun is_active round initial_round = (round > initial_round)
   250 fun is_active round initial_round = (round > initial_round)
   243 
   251 
   244 fun find_instances max_instances max_thm_insts max_new_grounds thm_infos ctxt round
   252 fun find_instances max_instances max_duplicated_instances max_thm_insts max_new_grounds thm_infos
   245     (known_grounds, new_grounds0, insts) =
   253     ctxt round (known_grounds, new_grounds0, insts) =
   246   let
   254   let
   247     val new_grounds =
   255     val new_grounds =
   248       Symtab.map (fn _ => fn grounds =>
   256       Symtab.map (fn _ => fn grounds =>
   249         if length grounds <= max_new_grounds then grounds
   257         if length grounds <= max_new_grounds then grounds
   250         else take max_new_grounds (sort Term_Ord.typ_ord grounds)) new_grounds0
   258         else take max_new_grounds (sort Term_Ord.typ_ord grounds)) new_grounds0
   251 
   259 
   252     val add_new = add_insts max_instances max_thm_insts ctxt round
   260     val add_new = add_insts max_instances max_duplicated_instances max_thm_insts ctxt round
   253     fun consider_all pred f (cx as (_, (n, _))) =
   261     fun consider_all pred f (cx as (_, (hits, misses, _))) =
   254       if n >= max_instances then cx else fold_schematics pred f thm_infos cx
   262       if hits >= max_instances orelse misses >= max_duplicated_instances then
       
   263         cx
       
   264       else
       
   265         fold_schematics pred f thm_infos cx
   255 
   266 
   256     val known_grounds' = Symtab.merge_list (op =) (known_grounds, new_grounds)
   267     val known_grounds' = Symtab.merge_list (op =) (known_grounds, new_grounds)
   257     val empty_grounds = clear_grounds known_grounds'
   268     val empty_grounds = clear_grounds known_grounds'
   258 
   269 
   259     val (new_grounds', insts') =
   270     val (new_grounds', insts') =
   272   let
   283   let
   273     val known_grounds = fold_grounds add_ground_types thm_infos consts
   284     val known_grounds = fold_grounds add_ground_types thm_infos consts
   274     val empty_grounds = clear_grounds known_grounds
   285     val empty_grounds = clear_grounds known_grounds
   275     val max_instances = Config.get ctxt max_new_instances
   286     val max_instances = Config.get ctxt max_new_instances
   276       |> fold (fn Schematic _ => Integer.add 1 | _ => I) thm_infos
   287       |> fold (fn Schematic _ => Integer.add 1 | _ => I) thm_infos
       
   288     val max_duplicated_instances = Config.get ctxt max_duplicated_instances
       
   289     val (_, _, (_, _, insts)) =
       
   290       limit_rounds ctxt (find_instances max_instances max_duplicated_instances max_thm_insts
       
   291       max_new_grounds thm_infos) (empty_grounds, known_grounds, (0, 0, Inttab.empty))
   277   in
   292   in
   278     (empty_grounds, known_grounds, (0, Inttab.empty))
   293     insts
   279     |> limit_rounds ctxt (find_instances max_instances max_thm_insts
       
   280       max_new_grounds thm_infos)
       
   281     |> (fn (_, _, (_, insts)) => insts)
       
   282   end
   294   end
   283 
   295 
   284 
   296 
   285 (* monomorphization *)
   297 (* monomorphization *)
   286 
   298