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