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