src/HOL/Tools/ATP/res_clasimpset.ML
author paulson
Wed Jul 20 17:00:28 2005 +0200 (2005-07-20)
changeset 16897 7e5319d0f418
parent 16795 b400b53d8f7d
child 16906 74eddde1de2f
permissions -rw-r--r--
code streamlining
     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   val write_out_clasimp_small :
   114      string -> theory -> (ResClause.clause * thm) Array.array * int
   115   end;
   116 
   117 structure ResClasimp : RES_CLASIMP =
   118 struct
   119 
   120 (*Relevance filtering is off by default*)
   121 val relevant = ref 0;  
   122 
   123 fun has_name th = ((Thm.name_of_thm th)  <>  "")
   124 
   125 fun has_name_pair (a,b) = (a <> "");
   126 
   127 
   128 (* changed, now it also finds out the name of the theorem. *)
   129 (* convert a theorem into CNF and then into Clause.clause format. *)
   130 
   131 (* outputs a list of (thm,clause) pairs *)
   132 
   133 
   134 (* for tracing: encloses each string element in brackets. *)
   135 fun concat_with_stop [] = ""
   136   | concat_with_stop [x] =  x
   137   | concat_with_stop (x::xs) = x^ "." ^ concat_with_stop xs;
   138 
   139 fun remove_symb str = 
   140     if String.isPrefix  "List.op @." str
   141     then  ((String.substring(str,0,5)) ^ (String.extract (str, 10, NONE)))
   142     else str;
   143 
   144 fun remove_spaces str = 
   145     let val strlist = String.tokens Char.isSpace str
   146 	val spaceless = concat strlist
   147 	val strlist' = String.tokens Char.isPunct spaceless
   148     in
   149 	concat_with_stop strlist'
   150     end
   151 
   152 fun remove_symb_pair (str, thm) = (remove_symb str, thm);
   153 
   154 fun remove_spaces_pair (str, thm) = (remove_spaces str, thm);
   155 
   156 
   157 (*Insert th into the result provided the name is not "".*)
   158 fun add_nonempty ((name,th), ths) = if name="" then ths else th::ths;
   159 
   160 fun posinlist x [] n = "not in list"
   161 |   posinlist x (y::ys) n = if (x=y) 
   162 			    then
   163  				string_of_int n
   164 			    else posinlist x (ys) (n+1)
   165 
   166 
   167 fun pairup [] [] = []
   168 |   pairup (x::xs) (y::ys) = (x,y)::(pairup xs ys)
   169 
   170 fun multi x 0 xlist = xlist
   171    |multi x n xlist = multi x (n -1 ) (x::xlist);
   172 
   173 fun clause_numbering ((clause, theorem), cls) = 
   174     let val num_of_cls = length cls
   175 	val numbers = 0 upto (num_of_cls -1)
   176 	val multi_name = multi (clause, theorem)  num_of_cls []
   177     in 
   178 	(multi_name)
   179     end;
   180 
   181 (*Write out the claset and simpset rules of the supplied theory.
   182   FIXME: argument "goal" is a hack to allow relevance filtering.*)
   183 fun write_out_clasimp filename thy goal = 
   184     let val claset_rules = 
   185               ReduceAxiomsN.relevant_filter (!relevant) goal 
   186                 (ResAxioms.claset_rules_of_thy thy);
   187 	val named_claset = List.filter has_name_pair claset_rules;
   188 	val claset_cls_thms = #1 (ResAxioms.clausify_rules_pairs named_claset []);
   189 
   190 	val simpset_rules =
   191 	      ReduceAxiomsN.relevant_filter (!relevant) goal 
   192                 (ResAxioms.simpset_rules_of_thy thy);
   193 	val named_simpset = 
   194 	      map remove_spaces_pair (List.filter has_name_pair simpset_rules)
   195 	val simpset_cls_thms = #1 (ResAxioms.clausify_rules_pairs named_simpset []);
   196 
   197 	val cls_thms = (claset_cls_thms@simpset_cls_thms);
   198 	val cls_thms_list = List.concat cls_thms;
   199         (* length 1429 *)
   200 	(*************************************************)
   201 	(* Write out clauses as tptp strings to filename *)
   202 	(*************************************************)
   203 	val clauses = map #1(cls_thms_list);
   204 	val cls_tptp_strs = map ResClause.tptp_clause clauses;
   205         val alllist = pairup cls_thms_list cls_tptp_strs
   206         val whole_list = List.concat (map clause_numbering alllist);
   207  
   208         (*********************************************************)
   209 	(* create array and put clausename, number pairs into it *)
   210 	(*********************************************************)
   211 	val num_of_clauses =  0;
   212 	val clause_arr = Array.fromList whole_list;
   213 	val num_of_clauses = List.length whole_list;
   214 
   215 	(* list of lists of tptp string clauses *)
   216 	val stick_clasrls =   List.concat cls_tptp_strs;
   217         (* length 1581*)
   218 	val out = TextIO.openOut filename;
   219 	val _=   ResLib.writeln_strs out stick_clasrls;
   220 	val _= TextIO.flushOut out;
   221 	val _= TextIO.closeOut out
   222   in
   223 	(clause_arr, num_of_clauses)
   224   end;
   225 
   226 
   227 
   228 
   229 
   230 (*Write out the claset and simpset rules of the supplied theory. Only include the first 50 rules*)
   231 fun write_out_clasimp_small filename thy = 
   232     let val claset_rules = ResAxioms.claset_rules_of_thy thy;
   233 	val named_claset = List.filter has_name_pair claset_rules;
   234 	val claset_names = map remove_spaces_pair (named_claset)
   235 	val claset_cls_thms = #1 (ResAxioms.clausify_rules_pairs named_claset []);
   236 
   237 	val simpset_rules = ResAxioms.simpset_rules_of_thy thy;
   238 	val named_simpset = 
   239 	      map remove_spaces_pair (List.filter has_name_pair simpset_rules)
   240 	val simpset_cls_thms = #1 (ResAxioms.clausify_rules_pairs named_simpset []);
   241 
   242 	val cls_thms = (claset_cls_thms@simpset_cls_thms);
   243 	val cls_thms_list = List.concat cls_thms;
   244         (* length 1429 *)
   245 	(*************************************************)
   246 	(* Write out clauses as tptp strings to filename *)
   247 	(*************************************************)
   248 	val clauses = map #1(cls_thms_list);
   249 	val cls_tptp_strs = map ResClause.tptp_clause clauses;
   250         val alllist = pairup cls_thms_list cls_tptp_strs
   251         val whole_list = List.concat (map clause_numbering alllist);
   252         val mini_list = List.take ( whole_list,50)
   253         (*********************************************************)
   254 	(* create array and put clausename, number pairs into it *)
   255 	(*********************************************************)
   256 	val num_of_clauses =  0;
   257 	val clause_arr = Array.fromList mini_list;
   258 	val num_of_clauses = List.length mini_list;
   259 
   260 	(* list of lists of tptp string clauses *)
   261 	val stick_clasrls =   List.concat cls_tptp_strs;
   262         (* length 1581*)
   263 	val out = TextIO.openOut filename;
   264 	val _=   ResLib.writeln_strs out (List.take (stick_clasrls,50));
   265 	val _= TextIO.flushOut out;
   266 	val _= TextIO.closeOut out
   267   in
   268 	(clause_arr, num_of_clauses)
   269   end;
   270 
   271 
   272 
   273 end;
   274 
   275 
   276