src/HOL/Tools/ATP/reduce_axiomsN.ML
author paulson
Wed Mar 22 12:30:29 2006 +0100 (2006-03-22)
changeset 19315 b218cc3d1bb4
parent 19231 c8879dd3a953
child 19321 30b5bb35dd33
permissions -rw-r--r--
Removal of obsolete strategies. Initial support for locales: Frees and Consts
treated similarly.
     1 (* Authors: Jia Meng, NICTA and Lawrence C Paulson, Cambridge University Computer Laboratory
     2    ID: $Id$
     3    Filtering strategies *)
     4 
     5 structure ReduceAxiomsN =
     6 struct
     7 
     8 val pass_mark = ref 0.6;
     9 val reduction_factor = ref 1.0;
    10 
    11 (*Whether all "simple" unit clauses should be included*)
    12 val add_unit = ref false;
    13 val unit_pass_mark = ref 0.0;
    14 
    15 
    16 (*Including equality in this list might be expected to stop rules like subset_antisym from
    17   being chosen, but for some reason filtering works better with them listed.*)
    18 val standard_consts =
    19   ["Trueprop","==>","all","Ex","op &","op |","Not","All","op -->",
    20    "op =","==","True","False"];
    21 
    22 
    23 (*** unit clauses ***)
    24 datatype clause_kind = Unit_neq | Unit_geq | Other
    25 
    26 
    27 fun literals_of_term args (Const ("Trueprop",_) $ P) = literals_of_term args P
    28   | literals_of_term args (Const ("op |",_) $ P $ Q) = 
    29     literals_of_term (literals_of_term args P) Q
    30   | literals_of_term args P = P::args;
    31 
    32 fun is_ground t = (term_vars t = []) andalso (term_frees t = []);
    33 
    34 fun eq_clause_type (P,Q) = 
    35     if ((is_ground P) orelse (is_ground Q)) then Unit_geq else Other;
    36 
    37 fun unit_clause_type (Const ("op =",_) $ P $ Q) = eq_clause_type (P,Q)
    38   | unit_clause_type _ = Unit_neq;
    39 
    40 fun clause_kind tm = 
    41     case literals_of_term [] tm of
    42         [lit] => unit_clause_type lit
    43       | _ => Other;
    44 
    45 (*** constants with types ***)
    46 
    47 (*An abstraction of Isabelle types*)
    48 datatype const_typ =  CTVar | CType of string * const_typ list
    49 
    50 fun uni_type (CType(con1,args1)) (CType(con2,args2)) = con1=con2 andalso uni_types args1 args2
    51   | uni_type (CType _) CTVar = true
    52   | uni_type CTVar CTVar = true
    53   | uni_type CTVar _ = false
    54 and uni_types [] [] = true
    55   | uni_types (a1::as1) (a2::as2) = uni_type a1 a2 andalso uni_types as1 as2;
    56 
    57 
    58 fun uni_constants (c1,ctp1) (c2,ctp2) = (c1=c2) andalso uni_types ctp1 ctp2;
    59 
    60 fun uni_mem _ [] = false
    61   | uni_mem (c,c_typ) ((c1,c_typ1)::ctyps) =
    62       uni_constants (c1,c_typ1) (c,c_typ) orelse uni_mem (c,c_typ) ctyps;
    63 
    64 fun const_typ_of (Type (c,typs)) = CType (c, map const_typ_of typs) 
    65   | const_typ_of (TFree _) = CTVar
    66   | const_typ_of (TVar _) = CTVar
    67 
    68 
    69 fun const_with_typ thy (c,typ) = 
    70     let val tvars = Sign.const_typargs thy (c,typ)
    71     in (c, map const_typ_of tvars) end
    72     handle TYPE _ => (c,[]);   (*Variable (locale constant): monomorphic*)   
    73 
    74 (*Free variables are counted, as well as constants, to handle locales*)
    75 fun add_term_consts_typs_rm thy (Const(c, typ)) cs =
    76       if (c mem standard_consts) then cs 
    77       else const_with_typ thy (c,typ) ins cs
    78   | add_term_consts_typs_rm thy (Free(c, typ)) cs =
    79       const_with_typ thy (c,typ) ins cs
    80   | add_term_consts_typs_rm thy (t $ u) cs =
    81       add_term_consts_typs_rm thy t (add_term_consts_typs_rm thy u cs)
    82   | add_term_consts_typs_rm thy (Abs(_,_,t)) cs = add_term_consts_typs_rm thy t cs
    83   | add_term_consts_typs_rm thy _ cs = cs;
    84 
    85 fun consts_typs_of_term thy t = add_term_consts_typs_rm thy t [];
    86 
    87 fun get_goal_consts_typs thy cs = foldl (op union) [] (map (consts_typs_of_term thy) cs)
    88 
    89 
    90 (**** Constant / Type Frequencies ****)
    91 
    92 local
    93 
    94 fun cons_nr CTVar = 0
    95   | cons_nr (CType _) = 1;
    96 
    97 in
    98 
    99 fun const_typ_ord TU =
   100   case TU of
   101     (CType (a, Ts), CType (b, Us)) =>
   102       (case fast_string_ord(a,b) of EQUAL => dict_ord const_typ_ord (Ts,Us) | ord => ord)
   103   | (T, U) => int_ord (cons_nr T, cons_nr U);
   104 
   105 end;
   106 
   107 structure CTtab = TableFun(type key = const_typ list val ord = dict_ord const_typ_ord);
   108 
   109 fun count_axiom_consts thy ((t,_), tab) = 
   110   let fun count_const (a, T, tab) =
   111 	let val (c, cts) = const_with_typ thy (a,T)
   112 	    val cttab = Option.getOpt (Symtab.lookup tab c, CTtab.empty)
   113 	    val n = Option.getOpt (CTtab.lookup cttab cts, 0)
   114 	in 
   115 	    Symtab.update (c, CTtab.update (cts, n+1) cttab) tab
   116 	end
   117       fun count_term_consts (Const(a,T), tab) = count_const(a,T,tab)
   118 	| count_term_consts (Free(a,T), tab) = count_const(a,T,tab)
   119 	| count_term_consts (t $ u, tab) =
   120 	    count_term_consts (t, count_term_consts (u, tab))
   121 	| count_term_consts (Abs(_,_,t), tab) = count_term_consts (t, tab)
   122 	| count_term_consts (_, tab) = tab
   123   in  count_term_consts (t, tab)  end;
   124 
   125 
   126 (******** filter clauses ********)
   127 
   128 (*The default ignores the constant-count and gives the old Strategy 3*)
   129 val weight_fn = ref (fn x : real => 1.0);
   130 
   131 fun const_weight ctab (c, cts) =
   132   let val pairs = CTtab.dest (Option.valOf (Symtab.lookup ctab c))
   133       fun add ((cts',m), n) = if uni_types cts cts' then m+n else n
   134   in  List.foldl add 0 pairs  end;
   135 
   136 fun add_ct_weight ctab ((c,T), w) =
   137   w + !weight_fn (real (const_weight ctab (c,T)));
   138 
   139 fun consts_typs_weight ctab =
   140     List.foldl (add_ct_weight ctab) 0.0;
   141 
   142 (*Relevant constants are weighted according to frequency, 
   143   but irrelevant constants are simply counted. Otherwise, Skolem functions,
   144   which are rare, would harm a clause's chances of being picked.*)
   145 fun clause_weight ctab gctyps consts_typs =
   146     let val rel = filter (fn s => uni_mem s gctyps) consts_typs
   147         val rel_weight = consts_typs_weight ctab rel
   148     in
   149 	rel_weight / (rel_weight + real (length consts_typs - length rel))
   150     end;
   151     
   152 fun relevant_clauses ctab rel_axs [] (addc,tmpc) keep =
   153       if null addc orelse null tmpc 
   154       then (addc @ rel_axs @ keep, tmpc)   (*termination!*)
   155       else relevant_clauses ctab addc tmpc ([],[]) (rel_axs @ keep)
   156   | relevant_clauses ctab rel_axs ((clstm,(consts_typs,w))::e_axs) (addc,tmpc) keep =
   157       let fun clause_weight_ax (_,(refconsts_typs,wa)) =
   158               wa * clause_weight ctab refconsts_typs consts_typs;
   159           val weight' = List.foldl Real.max w (map clause_weight_ax rel_axs)
   160 	  val e_ax' = (clstm, (consts_typs,weight'))
   161       in
   162 	if !pass_mark <= weight' 
   163 	then relevant_clauses ctab rel_axs e_axs (e_ax'::addc, tmpc) keep
   164 	else relevant_clauses ctab rel_axs e_axs (addc, e_ax'::tmpc) keep
   165       end;
   166 
   167 fun pair_consts_typs_axiom thy (tm,name) =
   168     ((tm,name), (consts_typs_of_term thy tm));
   169 
   170 (*Unit clauses other than non-trivial equations can be included subject to
   171   a separate (presumably lower) mark. *)
   172 fun good_unit_clause ((t,_), (_,w)) = 
   173      !unit_pass_mark <= w andalso
   174      (case clause_kind t of
   175 	  Unit_neq => true
   176 	| Unit_geq => true
   177 	| Other => false);
   178 	
   179 fun axiom_ord ((_,(_,w1)), (_,(_,w2))) = Real.compare (w2,w1);
   180 
   181 fun showconst (c,cttab) = 
   182       List.app (fn n => Output.debug (Int.toString n ^ " occurrences of " ^ c))
   183 	        (map #2 (CTtab.dest cttab))
   184 
   185 fun show_cname (name,k) = name ^ "__" ^ Int.toString k;
   186 
   187 fun showax ((_,cname), (_,w)) = 
   188     Output.debug ("Axiom " ^ show_cname cname ^ " has weight " ^ Real.toString w)
   189 	      
   190 fun relevance_filter_aux thy axioms goals = 
   191   let val const_tab = List.foldl (count_axiom_consts thy) Symtab.empty axioms
   192       val goals_consts_typs = get_goal_consts_typs thy goals
   193       fun relevant [] (ax,r) = (ax,r)
   194 	| relevant ((clstm,consts_typs)::y) (ax,r) =
   195 	    let val weight = clause_weight const_tab goals_consts_typs consts_typs
   196 		val ccc = (clstm, (consts_typs,weight))
   197 	    in
   198 	      if !pass_mark <= weight 
   199 	      then relevant y (ccc::ax, r)
   200 	      else relevant y (ax, ccc::r)
   201 	    end
   202       val (rel_clauses,nrel_clauses) =
   203 	  relevant (map (pair_consts_typs_axiom thy) axioms) ([],[]) 
   204       val (ax,r) = relevant_clauses const_tab rel_clauses nrel_clauses ([],[]) []
   205       val max_filtered = floor (!reduction_factor * real (length ax))
   206       val ax' = Library.take(max_filtered, Library.sort axiom_ord ax)
   207   in
   208       if !Output.show_debug_msgs then
   209 	   (List.app showconst (Symtab.dest const_tab);
   210 	    List.app showax ax)
   211       else ();
   212       if !add_unit then (filter good_unit_clause r) @ ax'
   213       else ax'
   214   end;
   215 
   216 fun relevance_filter thy axioms goals =
   217   map #1 (relevance_filter_aux thy axioms goals);
   218     
   219 
   220 end;