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