src/HOL/Tools/ATP/res_clasimpset.ML
author paulson
Fri Dec 16 12:15:54 2005 +0100 (2005-12-16)
changeset 18420 9470061ab283
parent 18144 4edcb5fdc3b0
child 18449 e314fb38307d
permissions -rw-r--r--
hashing to eliminate the output of duplicate clauses
paulson@16795
     1
(*  ID:      $Id$
paulson@15789
     2
    Author:     Claire Quigley
paulson@15789
     3
    Copyright   2004  University of Cambridge
paulson@15789
     4
*)
paulson@15789
     5
paulson@16741
     6
paulson@16741
     7
structure ReduceAxiomsN =
paulson@16741
     8
(* Author: Jia Meng, Cambridge University Computer Laboratory
paulson@17234
     9
   Remove irrelevant axioms used for a proof of a goal, with with iteration control
paulson@17234
    10
   
paulson@17234
    11
   Initial version. Needs elaboration. *)
paulson@16741
    12
paulson@16741
    13
struct
paulson@16741
    14
paulson@16741
    15
fun add_term_consts_rm ncs (Const(c, _)) cs = 
paulson@16741
    16
    if (c mem ncs) then cs else (c ins_string cs)
paulson@16795
    17
  | add_term_consts_rm ncs (t $ u) cs =
paulson@16795
    18
      add_term_consts_rm ncs t (add_term_consts_rm ncs u cs)
paulson@16741
    19
  | add_term_consts_rm ncs (Abs(_,_,t)) cs = add_term_consts_rm ncs t cs
paulson@16741
    20
  | add_term_consts_rm ncs _ cs = cs;
paulson@16741
    21
paulson@16741
    22
paulson@16741
    23
fun term_consts_rm ncs t = add_term_consts_rm ncs t [];
paulson@16741
    24
paulson@16741
    25
fun thm_consts_rm ncs thm = term_consts_rm ncs (prop_of thm);
paulson@16741
    26
paulson@16741
    27
fun consts_of_thm (n,thm) = thm_consts_rm ["Trueprop","==>","all","Ex","op &", "op |", "Not", "All", "op -->", "op =", "==", "True", "False"] thm;
paulson@16741
    28
paulson@16741
    29
fun consts_of_term term = term_consts_rm ["Trueprop","==>","all","Ex","op &", "op |", "Not", "All", "op -->", "op =", "==", "True", "False"] term;
paulson@16741
    30
paulson@16741
    31
fun make_pairs [] _ = []
paulson@16741
    32
  | make_pairs (x::xs) y = (x,y)::(make_pairs xs y);
paulson@16741
    33
paulson@16741
    34
fun const_thm_list_aux [] cthms = cthms
paulson@16741
    35
  | const_thm_list_aux (thm::thms) cthms =
paulson@16741
    36
    let val consts = consts_of_thm thm
paulson@16741
    37
	val cthms' = make_pairs consts thm 
paulson@16741
    38
    in
paulson@16741
    39
	const_thm_list_aux thms (cthms' @ cthms)
paulson@16741
    40
    end;
paulson@16741
    41
paulson@16741
    42
paulson@16741
    43
fun const_thm_list thms = const_thm_list_aux thms [];
paulson@16741
    44
paulson@16741
    45
fun make_thm_table thms  = 
paulson@16741
    46
    let val consts_thms = const_thm_list thms
paulson@16741
    47
    in
paulson@16741
    48
	Symtab.make_multi consts_thms
paulson@16741
    49
    end;
paulson@16741
    50
paulson@16741
    51
paulson@16741
    52
fun consts_in_goal goal = consts_of_term goal;
paulson@16741
    53
paulson@16741
    54
fun axioms_having_consts_aux [] tab thms = thms
paulson@16741
    55
  | axioms_having_consts_aux (c::cs) tab thms =
wenzelm@17412
    56
    let val thms1 = Symtab.lookup tab c
wenzelm@17261
    57
      val thms2 = 
wenzelm@17261
    58
          case thms1 of (SOME x) => x
wenzelm@17261
    59
                      | NONE => []
paulson@16741
    60
    in
wenzelm@17261
    61
      axioms_having_consts_aux cs tab (thms2 union thms)
paulson@16741
    62
    end;
paulson@16741
    63
paulson@16741
    64
fun axioms_having_consts cs tab = axioms_having_consts_aux cs tab [];
paulson@16741
    65
paulson@16741
    66
paulson@16741
    67
fun relevant_axioms goal thmTab n =  
paulson@16741
    68
    let val consts = consts_in_goal goal
paulson@16741
    69
	fun relevant_axioms_aux1 cs k =
paulson@16741
    70
	    let val thms1 = axioms_having_consts cs thmTab
paulson@17764
    71
		val cs1 = foldl (op union_string) [] (map consts_of_thm thms1)
paulson@16741
    72
	    in
paulson@16795
    73
		if ((cs1 subset cs) orelse n <= k) then (k,thms1) 
paulson@16741
    74
		else (relevant_axioms_aux1 (cs1 union cs) (k+1))
paulson@16741
    75
	    end
paulson@16741
    76
paulson@16795
    77
    in  relevant_axioms_aux1 consts 1  end;
paulson@16741
    78
paulson@16795
    79
fun relevant_filter n goal thms = 
paulson@16795
    80
    if n<=0 then thms 
paulson@16795
    81
    else #2 (relevant_axioms goal (make_thm_table thms) n);
paulson@16741
    82
paulson@16741
    83
(* find the thms from thy that contain relevant constants, n is the iteration number *)
paulson@16741
    84
fun find_axioms_n thy goal n =
paulson@16741
    85
    let val clasetR = ResAxioms.claset_rules_of_thy thy
paulson@16741
    86
	val simpsetR = ResAxioms.simpset_rules_of_thy thy	  
paulson@16741
    87
	val table = make_thm_table (clasetR @ simpsetR)	
paulson@16741
    88
    in
paulson@16741
    89
	relevant_axioms goal table n
paulson@16741
    90
    end;
paulson@16741
    91
paulson@16741
    92
fun find_axioms_n_c thy goal n =
paulson@16741
    93
    let val current_thms = PureThy.thms_of thy
paulson@16741
    94
	val table = make_thm_table current_thms
paulson@16741
    95
    in
paulson@16741
    96
	relevant_axioms goal table n
paulson@16741
    97
    end;
paulson@16741
    98
paulson@16741
    99
end;
paulson@16741
   100
paulson@16741
   101
quigley@16156
   102
signature RES_CLASIMP = 
quigley@15919
   103
  sig
paulson@16795
   104
  val relevant : int ref
quigley@16950
   105
  val use_simpset: bool ref
paulson@17305
   106
  val get_clasimp_lemmas : 
paulson@17484
   107
         Proof.context -> term -> 
paulson@17484
   108
         (ResClause.clause * thm) Array.array * ResClause.clause list 
quigley@15919
   109
  end;
quigley@15919
   110
quigley@15919
   111
structure ResClasimp : RES_CLASIMP =
quigley@15919
   112
struct
paulson@17305
   113
val use_simpset = ref false;   (*Performance is much better without simprules*)
paulson@16957
   114
paulson@16957
   115
val relevant = ref 0;  (*Relevance filtering is off by default*)
paulson@16795
   116
paulson@16956
   117
(*The "name" of a theorem is its statement, if nothing else is available.*)
paulson@16956
   118
val plain_string_of_thm =
paulson@16956
   119
    setmp show_question_marks false 
paulson@16956
   120
      (setmp print_mode [] 
paulson@16956
   121
	(Pretty.setmp_margin 999 string_of_thm));
paulson@16957
   122
	
paulson@17828
   123
(*Returns the first substring enclosed in quotation marks, typically omitting 
paulson@17828
   124
  the [.] of meta-level assumptions.*)
paulson@17828
   125
val firstquoted = hd o (String.tokens (fn c => c = #"\""))
paulson@17828
   126
	
paulson@16957
   127
fun fake_thm_name th = 
paulson@17828
   128
    Context.theory_name (theory_of_thm th) ^ "." ^ firstquoted (plain_string_of_thm th);
paulson@16061
   129
paulson@17828
   130
fun put_name_pair ("",th) = (fake_thm_name th, th)
paulson@16956
   131
  | put_name_pair (a,th)  = (a,th);
quigley@16039
   132
quigley@16039
   133
(* outputs a list of (thm,clause) pairs *)
quigley@15643
   134
paulson@17305
   135
fun multi x 0 xlist = xlist
paulson@17305
   136
   |multi x n xlist = multi x (n-1) (x::xlist);
quigley@16156
   137
paulson@17305
   138
fun clause_numbering ((clause, theorem), num_of_cls) = 
paulson@17305
   139
    let val numbers = 0 upto (num_of_cls - 1)
paulson@16172
   140
    in 
paulson@17305
   141
	multi (clause, theorem) num_of_cls []
paulson@16172
   142
    end;
paulson@17234
   143
    
paulson@18420
   144
(*Hashing to detect duplicate and variant clauses, e.g. from the [iff] attribute
paulson@18420
   145
Some primes from http://primes.utm.edu/:
paulson@18420
   146
   1823   1831   1847   1861   1867   1871   1873   1877   1879   1889 
paulson@18420
   147
   1901   1907   1913   1931   1933   1949   1951   1973   1979   1987 
paulson@18420
   148
   1993   1997   1999   2003   2011   2017   2027   2029   2039   2053 
paulson@18420
   149
   2063   2069   2081   2083   2087   2089   2099   2111   2113   2129 
paulson@18420
   150
*)
paulson@18420
   151
paulson@18420
   152
exception HASH_CLAUSE;
paulson@18420
   153
paulson@18420
   154
(*Create a hash table for clauses, of the given size*)
paulson@18420
   155
fun mk_clause_table size =
paulson@18420
   156
      Hashtable.create{hash = ResClause.hash1_clause, 
paulson@18420
   157
		       exn = HASH_CLAUSE,
paulson@18420
   158
		       == = ResClause.clause_eq, 
paulson@18420
   159
		       size = size};
paulson@18420
   160
paulson@18420
   161
(*Insert x only if fresh*)
paulson@18420
   162
fun insert_new ht (x,y) = ignore (Hashtable.lookup ht x)
paulson@18420
   163
            handle HASH_CLAUSE => Hashtable.insert ht (x,y); 
paulson@18420
   164
paulson@18420
   165
(*Use a hash table to eliminate duplicates from xs*)
paulson@18420
   166
fun make_unique ht xs = (app (insert_new ht) xs;  Hashtable.map Library.I ht);
quigley@16950
   167
paulson@16741
   168
(*Write out the claset and simpset rules of the supplied theory.
paulson@16906
   169
  FIXME: argument "goal" is a hack to allow relevance filtering.
paulson@16906
   170
  To reduce the number of clauses produced, set ResClasimp.relevant:=1*)
paulson@17484
   171
fun get_clasimp_lemmas ctxt goal = 
paulson@18420
   172
  let val claset_thms =
paulson@18420
   173
	    map put_name_pair
paulson@18420
   174
	      (ReduceAxiomsN.relevant_filter (!relevant) goal 
paulson@18420
   175
		(ResAxioms.claset_rules_of_ctxt ctxt))
paulson@18420
   176
      val claset_cls_thms = ResAxioms.clausify_rules_pairs claset_thms
paulson@18420
   177
      val simpset_cls_thms = 
paulson@18420
   178
	    if !use_simpset then 
paulson@18420
   179
	       ResAxioms.clausify_rules_pairs 
paulson@18420
   180
		  (map put_name_pair 
paulson@18420
   181
		    (ReduceAxiomsN.relevant_filter (!relevant) goal
paulson@18420
   182
		      (ResAxioms.simpset_rules_of_ctxt ctxt)))
paulson@18420
   183
	    else []
paulson@18420
   184
      val cls_thms_list = make_unique (mk_clause_table 2129) 
paulson@18420
   185
                                      (List.concat (simpset_cls_thms@claset_cls_thms))
paulson@18420
   186
      (* Identify the set of clauses to be written out *)
paulson@18420
   187
      val clauses = map #1(cls_thms_list);
paulson@18420
   188
      val cls_nums = map ResClause.num_of_clauses clauses;
paulson@18420
   189
      (*Note: in every case, cls_num = 1.  I think that only conjecture clauses
paulson@18420
   190
	can have any other value.*)
paulson@18420
   191
      val whole_list = List.concat 
paulson@18420
   192
	    (map clause_numbering (ListPair.zip (cls_thms_list, cls_nums)));
paulson@18420
   193
      
paulson@17828
   194
  in  (* create array of put clausename, number pairs into it *)
paulson@17828
   195
      (Array.fromList whole_list, clauses)
paulson@16061
   196
  end;
quigley@15643
   197
quigley@16950
   198
quigley@15919
   199
end;
quigley@16039
   200
quigley@16156
   201
quigley@16156
   202