src/HOL/Tools/ATP/res_clasimpset.ML
author paulson
Thu Jul 28 17:56:27 2005 +0200 (2005-07-28)
changeset 16956 5b413c866593
parent 16950 e7f0f41d513a
child 16957 8dcd14e8db7a
permissions -rw-r--r--
invents theorem names; also patches write_out_clasimp
     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 
   111   val relevant : int ref
   112   val use_simpset: bool ref
   113   val write_out_clasimp : string -> theory -> term -> 
   114                              (ResClause.clause * thm) Array.array * int (* * ResClause.clause list *)
   115 
   116   end;
   117 
   118 structure ResClasimp : RES_CLASIMP =
   119 struct
   120 val use_simpset = ref true;
   121 (*Relevance filtering is off by default*)
   122 val relevant = ref 0;  
   123 
   124 (*The "name" of a theorem is its statement, if nothing else is available.*)
   125 val plain_string_of_thm =
   126     setmp show_question_marks false 
   127       (setmp print_mode [] 
   128 	(Pretty.setmp_margin 999 string_of_thm));
   129 
   130 fun put_name_pair ("",th) = (ResLib.trim_ends (plain_string_of_thm th), th)
   131   | put_name_pair (a,th)  = (a,th);
   132 
   133 (* changed, now it also finds out the name of the theorem. *)
   134 (* convert a theorem into CNF and then into Clause.clause format. *)
   135 
   136 (* outputs a list of (thm,clause) pairs *)
   137 
   138 
   139 (* for tracing: encloses each string element in brackets. *)
   140 fun concat_with_stop [] = ""
   141   | concat_with_stop [x] =  x
   142   | concat_with_stop (x::xs) = x^ "." ^ concat_with_stop xs;
   143 
   144 fun remove_symb str = 
   145     if String.isPrefix  "List.op @." str
   146     then  ((String.substring(str,0,5)) ^ (String.extract (str, 10, NONE)))
   147     else str;
   148 
   149 (*FIXME: this logic (especially concat_with_stop) needs to be replaced by code
   150 to invert the function ascii_of.*)
   151 fun remove_spaces str = 
   152     let val strlist = String.tokens Char.isSpace str
   153 	val spaceless = concat strlist
   154 	val strlist' = String.tokens Char.isPunct spaceless
   155     in
   156 	concat_with_stop strlist'
   157     end
   158 
   159 fun remove_symb_pair (str, thm) = (remove_symb str, thm);
   160 
   161 fun remove_spaces_pair (str, thm) = (remove_spaces str, thm);
   162 
   163 
   164 (*Insert th into the result provided the name is not "".*)
   165 fun add_nonempty ((name,th), ths) = if name="" then ths else th::ths;
   166 
   167 fun posinlist x [] n = "not in list"
   168 |   posinlist x (y::ys) n = if (x=y) 
   169 			    then
   170  				string_of_int n
   171 			    else posinlist x (ys) (n+1)
   172 
   173 
   174 fun pairup [] [] = []
   175 |   pairup (x::xs) (y::ys) = (x,y)::(pairup xs ys)
   176 
   177 fun multi x 0 xlist = xlist
   178    |multi x n xlist = multi x (n -1 ) (x::xlist);
   179 
   180 fun clause_numbering ((clause, theorem), cls) = 
   181     let val num_of_cls = length cls
   182 	val numbers = 0 upto (num_of_cls -1)
   183 	val multi_name = multi (clause, theorem)  num_of_cls []
   184     in 
   185 	(multi_name)
   186     end;
   187 
   188 
   189 fun concat_with sep []  = ""
   190   | concat_with sep [x] = "(" ^ x ^ ")"
   191   | concat_with sep (x::xs) = "(" ^ x ^ ")" ^  sep ^ (concat_with sep xs);
   192 
   193 
   194 (*Write out the claset and simpset rules of the supplied theory.
   195   FIXME: argument "goal" is a hack to allow relevance filtering.
   196   To reduce the number of clauses produced, set ResClasimp.relevant:=1*)
   197 fun write_out_clasimp filename thy goal = 
   198     let val claset_rules = 
   199               map put_name_pair
   200 	        (ReduceAxiomsN.relevant_filter (!relevant) goal 
   201 		  (ResAxioms.claset_rules_of_thy thy));
   202 	val claset_cls_thms = #1 (ResAxioms.clausify_rules_pairs claset_rules []);
   203 
   204 	val simpset_rules =
   205 	      ReduceAxiomsN.relevant_filter (!relevant) goal 
   206                 (ResAxioms.simpset_rules_of_thy thy);
   207 	val named_simpset = 
   208 	      map remove_spaces_pair (map put_name_pair simpset_rules)
   209         val justnames = map #1 named_simpset
   210         val namestring = concat_with "\n" justnames
   211         val _ = File.append (File.tmp_path (Path.basic "clasimp_names"))
   212 			  (namestring)
   213 	val simpset_cls_thms = #1 (ResAxioms.clausify_rules_pairs named_simpset []);
   214 
   215 	val cls_thms = if !use_simpset then (claset_cls_thms@simpset_cls_thms) else claset_cls_thms;
   216 	val cls_thms_list = List.concat cls_thms;
   217         (* length 1429 *)
   218 	(*************************************************)
   219 	(* Write out clauses as tptp strings to filename *)
   220 	(*************************************************)
   221 	val clauses = map #1(cls_thms_list);
   222 	val cls_tptp_strs = map ResClause.tptp_clause clauses;
   223         val alllist = pairup cls_thms_list cls_tptp_strs
   224         val whole_list = List.concat (map clause_numbering alllist);
   225  
   226         (*********************************************************)
   227 	(* create array and put clausename, number pairs into it *)
   228 	(*********************************************************)
   229 	val num_of_clauses =  0;
   230 	val clause_arr = Array.fromList whole_list;
   231 	val num_of_clauses = List.length whole_list;
   232 
   233 	(* list of lists of tptp string clauses *)
   234 	val stick_clasrls =   List.concat cls_tptp_strs;
   235         (* length 1581*)
   236 	val out = TextIO.openOut filename;
   237 	val _=   ResLib.writeln_strs out stick_clasrls;
   238 	val _= TextIO.flushOut out;
   239 	val _= TextIO.closeOut out
   240   in
   241 	(clause_arr, num_of_clauses)
   242   end;
   243 
   244 
   245 end;
   246 
   247 
   248