src/HOL/Tools/ATP/res_clasimpset.ML
author paulson
Fri Jul 22 17:43:49 2005 +0200 (2005-07-22)
changeset 16906 74eddde1de2f
parent 16897 7e5319d0f418
child 16950 e7f0f41d513a
permissions -rw-r--r--
dead code removal
     1 (*  ID:      $Id$
     2     Author:     Claire Quigley
     3     Copyright   2004  University of Cambridge
     4 *)
     5 
     6 
     7 structure ReduceAxiomsN =
     8 (* Author: Jia Meng, Cambridge University Computer Laboratory
     9    Remove irrelevant axioms used for a proof of a goal, with with iteration control*)
    10 
    11 struct
    12 
    13 fun add_term_consts_rm ncs (Const(c, _)) cs = 
    14     if (c mem ncs) then cs else (c ins_string cs)
    15   | add_term_consts_rm ncs (t $ u) cs =
    16       add_term_consts_rm ncs t (add_term_consts_rm ncs u cs)
    17   | add_term_consts_rm ncs (Abs(_,_,t)) cs = add_term_consts_rm ncs t cs
    18   | add_term_consts_rm ncs _ cs = cs;
    19 
    20 
    21 fun term_consts_rm ncs t = add_term_consts_rm ncs t [];
    22 
    23 
    24 fun thm_consts_rm ncs thm = term_consts_rm ncs (prop_of thm);
    25 
    26 
    27 fun consts_of_thm (n,thm) = thm_consts_rm ["Trueprop","==>","all","Ex","op &", "op |", "Not", "All", "op -->", "op =", "==", "True", "False"] thm;
    28 
    29 fun consts_of_term term = term_consts_rm ["Trueprop","==>","all","Ex","op &", "op |", "Not", "All", "op -->", "op =", "==", "True", "False"] term;
    30 
    31 fun make_pairs [] _ = []
    32   | make_pairs (x::xs) y = (x,y)::(make_pairs xs y);
    33 
    34 
    35 
    36 fun const_thm_list_aux [] cthms = cthms
    37   | const_thm_list_aux (thm::thms) cthms =
    38     let val consts = consts_of_thm thm
    39 	val cthms' = make_pairs consts thm 
    40     in
    41 	const_thm_list_aux thms (cthms' @ cthms)
    42     end;
    43 
    44 
    45 fun const_thm_list thms = const_thm_list_aux thms [];
    46 
    47 fun make_thm_table thms  = 
    48     let val consts_thms = const_thm_list thms
    49     in
    50 	Symtab.make_multi consts_thms
    51     end;
    52 
    53 
    54 fun consts_in_goal goal = consts_of_term goal;
    55 
    56 fun axioms_having_consts_aux [] tab thms = thms
    57   | axioms_having_consts_aux (c::cs) tab thms =
    58     let val thms1 = Symtab.lookup(tab,c)
    59 	val thms2 = 
    60 	    case thms1 of (SOME x) => x
    61 			| NONE => []
    62     in
    63 	axioms_having_consts_aux cs tab (thms2 union thms)
    64     end;
    65 
    66 
    67 fun axioms_having_consts cs tab = axioms_having_consts_aux cs tab [];
    68 
    69 
    70 fun relevant_axioms goal thmTab n =  
    71     let val consts = consts_in_goal goal
    72 	fun relevant_axioms_aux1 cs k =
    73 	    let val thms1 = axioms_having_consts cs thmTab
    74 		val cs1 = ResLib.flat_noDup (map consts_of_thm thms1)
    75 	    in
    76 		if ((cs1 subset cs) orelse n <= k) then (k,thms1) 
    77 		else (relevant_axioms_aux1 (cs1 union cs) (k+1))
    78 	    end
    79 
    80     in  relevant_axioms_aux1 consts 1  end;
    81 
    82 
    83 fun relevant_filter n goal thms = 
    84     if n<=0 then thms 
    85     else #2 (relevant_axioms goal (make_thm_table thms) n);
    86 
    87 
    88 (* find the thms from thy that contain relevant constants, n is the iteration number *)
    89 fun find_axioms_n thy goal n =
    90     let val clasetR = ResAxioms.claset_rules_of_thy thy
    91 	val simpsetR = ResAxioms.simpset_rules_of_thy thy	  
    92 	val table = make_thm_table (clasetR @ simpsetR)	
    93     in
    94 	relevant_axioms goal table n
    95     end;
    96 
    97 
    98 fun find_axioms_n_c thy goal n =
    99     let val current_thms = PureThy.thms_of thy
   100 	val table = make_thm_table current_thms
   101     in
   102 	relevant_axioms goal table n
   103     end;
   104 
   105 end;
   106 
   107 
   108 signature RES_CLASIMP = 
   109   sig
   110   val relevant : int ref
   111   val write_out_clasimp : string -> theory -> term -> 
   112                              (ResClause.clause * thm) Array.array * int
   113   end;
   114 
   115 structure ResClasimp : RES_CLASIMP =
   116 struct
   117 
   118 (*Relevance filtering is off by default*)
   119 val relevant = ref 0;  
   120 
   121 fun has_name th = ((Thm.name_of_thm th)  <>  "")
   122 
   123 fun has_name_pair (a,b) = (a <> "");
   124 
   125 
   126 (* changed, now it also finds out the name of the theorem. *)
   127 (* convert a theorem into CNF and then into Clause.clause format. *)
   128 
   129 (* outputs a list of (thm,clause) pairs *)
   130 
   131 
   132 (* for tracing: encloses each string element in brackets. *)
   133 fun concat_with_stop [] = ""
   134   | concat_with_stop [x] =  x
   135   | concat_with_stop (x::xs) = x^ "." ^ concat_with_stop xs;
   136 
   137 fun remove_symb str = 
   138     if String.isPrefix  "List.op @." str
   139     then  ((String.substring(str,0,5)) ^ (String.extract (str, 10, NONE)))
   140     else str;
   141 
   142 fun remove_spaces str = 
   143     let val strlist = String.tokens Char.isSpace str
   144 	val spaceless = concat strlist
   145 	val strlist' = String.tokens Char.isPunct spaceless
   146     in
   147 	concat_with_stop strlist'
   148     end
   149 
   150 fun remove_symb_pair (str, thm) = (remove_symb str, thm);
   151 
   152 fun remove_spaces_pair (str, thm) = (remove_spaces str, thm);
   153 
   154 
   155 (*Insert th into the result provided the name is not "".*)
   156 fun add_nonempty ((name,th), ths) = if name="" then ths else th::ths;
   157 
   158 fun posinlist x [] n = "not in list"
   159 |   posinlist x (y::ys) n = if (x=y) 
   160 			    then
   161  				string_of_int n
   162 			    else posinlist x (ys) (n+1)
   163 
   164 
   165 fun pairup [] [] = []
   166 |   pairup (x::xs) (y::ys) = (x,y)::(pairup xs ys)
   167 
   168 fun multi x 0 xlist = xlist
   169    |multi x n xlist = multi x (n -1 ) (x::xlist);
   170 
   171 fun clause_numbering ((clause, theorem), cls) = 
   172     let val num_of_cls = length cls
   173 	val numbers = 0 upto (num_of_cls -1)
   174 	val multi_name = multi (clause, theorem)  num_of_cls []
   175     in 
   176 	(multi_name)
   177     end;
   178 
   179 (*Write out the claset and simpset rules of the supplied theory.
   180   FIXME: argument "goal" is a hack to allow relevance filtering.
   181   To reduce the number of clauses produced, set ResClasimp.relevant:=1*)
   182 fun write_out_clasimp filename thy goal = 
   183     let val claset_rules = 
   184               ReduceAxiomsN.relevant_filter (!relevant) goal 
   185                 (ResAxioms.claset_rules_of_thy thy);
   186 	val named_claset = List.filter has_name_pair claset_rules;
   187 	val claset_cls_thms = #1 (ResAxioms.clausify_rules_pairs named_claset []);
   188 
   189 	val simpset_rules =
   190 	      ReduceAxiomsN.relevant_filter (!relevant) goal 
   191                 (ResAxioms.simpset_rules_of_thy thy);
   192 	val named_simpset = 
   193 	      map remove_spaces_pair (List.filter has_name_pair simpset_rules)
   194 	val simpset_cls_thms = #1 (ResAxioms.clausify_rules_pairs named_simpset []);
   195 
   196 	val cls_thms = (claset_cls_thms@simpset_cls_thms);
   197 	val cls_thms_list = List.concat cls_thms;
   198         (* length 1429 *)
   199 	(*************************************************)
   200 	(* Write out clauses as tptp strings to filename *)
   201 	(*************************************************)
   202 	val clauses = map #1(cls_thms_list);
   203 	val cls_tptp_strs = map ResClause.tptp_clause clauses;
   204         val alllist = pairup cls_thms_list cls_tptp_strs
   205         val whole_list = List.concat (map clause_numbering alllist);
   206  
   207         (*********************************************************)
   208 	(* create array and put clausename, number pairs into it *)
   209 	(*********************************************************)
   210 	val num_of_clauses =  0;
   211 	val clause_arr = Array.fromList whole_list;
   212 	val num_of_clauses = List.length whole_list;
   213 
   214 	(* list of lists of tptp string clauses *)
   215 	val stick_clasrls =   List.concat cls_tptp_strs;
   216         (* length 1581*)
   217 	val out = TextIO.openOut filename;
   218 	val _=   ResLib.writeln_strs out stick_clasrls;
   219 	val _= TextIO.flushOut out;
   220 	val _= TextIO.closeOut out
   221   in
   222 	(clause_arr, num_of_clauses)
   223   end;
   224 
   225 end;
   226 
   227 
   228