src/HOL/Tools/ATP/res_clasimpset.ML
author paulson
Fri Sep 02 17:55:24 2005 +0200 (2005-09-02)
changeset 17234 12a9393c5d77
parent 17150 ce2a1aeb42aa
child 17261 193b84a70ca4
permissions -rw-r--r--
further tidying up of Isabelle-ATP link
     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    Initial version. Needs elaboration. *)
    12 
    13 struct
    14 
    15 fun add_term_consts_rm ncs (Const(c, _)) cs = 
    16     if (c mem ncs) then cs else (c ins_string cs)
    17   | add_term_consts_rm ncs (t $ u) cs =
    18       add_term_consts_rm ncs t (add_term_consts_rm ncs u cs)
    19   | add_term_consts_rm ncs (Abs(_,_,t)) cs = add_term_consts_rm ncs t cs
    20   | add_term_consts_rm ncs _ cs = cs;
    21 
    22 
    23 fun term_consts_rm ncs t = add_term_consts_rm ncs t [];
    24 
    25 
    26 fun thm_consts_rm ncs thm = term_consts_rm ncs (prop_of thm);
    27 
    28 
    29 fun consts_of_thm (n,thm) = thm_consts_rm ["Trueprop","==>","all","Ex","op &", "op |", "Not", "All", "op -->", "op =", "==", "True", "False"] thm;
    30 
    31 fun consts_of_term term = term_consts_rm ["Trueprop","==>","all","Ex","op &", "op |", "Not", "All", "op -->", "op =", "==", "True", "False"] term;
    32 
    33 fun make_pairs [] _ = []
    34   | make_pairs (x::xs) y = (x,y)::(make_pairs xs y);
    35 
    36 
    37 
    38 fun const_thm_list_aux [] cthms = cthms
    39   | const_thm_list_aux (thm::thms) cthms =
    40     let val consts = consts_of_thm thm
    41 	val cthms' = make_pairs consts thm 
    42     in
    43 	const_thm_list_aux thms (cthms' @ cthms)
    44     end;
    45 
    46 
    47 fun const_thm_list thms = const_thm_list_aux thms [];
    48 
    49 fun make_thm_table thms  = 
    50     let val consts_thms = const_thm_list thms
    51     in
    52 	Symtab.make_multi consts_thms
    53     end;
    54 
    55 
    56 fun consts_in_goal goal = consts_of_term goal;
    57 
    58 fun axioms_having_consts_aux [] tab thms = thms
    59   | axioms_having_consts_aux (c::cs) tab thms =
    60     let val thms1 = Symtab.lookup(tab,c)
    61 	val thms2 = 
    62 	    case thms1 of (SOME x) => x
    63 			| NONE => []
    64     in
    65 	axioms_having_consts_aux cs tab (thms2 union thms)
    66     end;
    67 
    68 
    69 fun axioms_having_consts cs tab = axioms_having_consts_aux cs tab [];
    70 
    71 
    72 fun relevant_axioms goal thmTab n =  
    73     let val consts = consts_in_goal goal
    74 	fun relevant_axioms_aux1 cs k =
    75 	    let val thms1 = axioms_having_consts cs thmTab
    76 		val cs1 = ResLib.flat_noDup (map consts_of_thm thms1)
    77 	    in
    78 		if ((cs1 subset cs) orelse n <= k) then (k,thms1) 
    79 		else (relevant_axioms_aux1 (cs1 union cs) (k+1))
    80 	    end
    81 
    82     in  relevant_axioms_aux1 consts 1  end;
    83 
    84 
    85 fun relevant_filter n goal thms = 
    86     if n<=0 then thms 
    87     else #2 (relevant_axioms goal (make_thm_table thms) n);
    88 
    89 
    90 (* find the thms from thy that contain relevant constants, n is the iteration number *)
    91 fun find_axioms_n thy goal n =
    92     let val clasetR = ResAxioms.claset_rules_of_thy thy
    93 	val simpsetR = ResAxioms.simpset_rules_of_thy thy	  
    94 	val table = make_thm_table (clasetR @ simpsetR)	
    95     in
    96 	relevant_axioms goal table n
    97     end;
    98 
    99 
   100 fun find_axioms_n_c thy goal n =
   101     let val current_thms = PureThy.thms_of thy
   102 	val table = make_thm_table current_thms
   103     in
   104 	relevant_axioms goal table n
   105     end;
   106 
   107 end;
   108 
   109 
   110 signature RES_CLASIMP = 
   111   sig
   112 
   113   val relevant : int ref
   114   val use_simpset: bool ref
   115   val use_nameless: bool ref
   116   val write_out_clasimp : string -> theory -> term -> 
   117                              (ResClause.clause * thm) Array.array * int * ResClause.clause list 
   118 
   119   end;
   120 
   121 structure ResClasimp : RES_CLASIMP =
   122 struct
   123 val use_simpset = ref true;
   124 val use_nameless = ref false;  (*Because most are useless [iff] rules*)
   125 
   126 val relevant = ref 0;  (*Relevance filtering is off by default*)
   127 
   128 (*The "name" of a theorem is its statement, if nothing else is available.*)
   129 val plain_string_of_thm =
   130     setmp show_question_marks false 
   131       (setmp print_mode [] 
   132 	(Pretty.setmp_margin 999 string_of_thm));
   133 	
   134 fun fake_thm_name th = 
   135     Context.theory_name (theory_of_thm th) ^ "." ^ 
   136     ResLib.trim_ends (plain_string_of_thm th);
   137 
   138 fun put_name_pair ("",th) = if !use_nameless then (fake_thm_name th, th)
   139                             else ("HOL.TrueI",TrueI)
   140   | put_name_pair (a,th)  = (a,th);
   141 
   142 (* changed, now it also finds out the name of the theorem. *)
   143 (* convert a theorem into CNF and then into Clause.clause format. *)
   144 
   145 (* outputs a list of (thm,clause) pairs *)
   146 
   147 
   148 (*Insert th into the result provided the name is not "".*)
   149 fun add_nonempty ((name,th), ths) = if name="" then ths else th::ths;
   150 
   151 fun posinlist x [] n = "not in list"
   152 |   posinlist x (y::ys) n = if (x=y) 
   153 			    then
   154  				string_of_int n
   155 			    else posinlist x (ys) (n+1)
   156 
   157 
   158 fun pairup [] [] = []
   159 |   pairup (x::xs) (y::ys) = (x,y)::(pairup xs ys)
   160 
   161 fun multi x 0 xlist = xlist
   162    |multi x n xlist = multi x (n -1 ) (x::xlist);
   163 
   164 fun clause_numbering ((clause, theorem), cls) = 
   165     let val num_of_cls = length cls
   166 	val numbers = 0 upto (num_of_cls -1)
   167     in 
   168 	multi (clause, theorem)  num_of_cls []
   169     end;
   170     
   171 
   172 (*Write out the claset and simpset rules of the supplied theory.
   173   FIXME: argument "goal" is a hack to allow relevance filtering.
   174   To reduce the number of clauses produced, set ResClasimp.relevant:=1*)
   175 fun write_out_clasimp filename thy goal = 
   176     let val claset_rules = 
   177               map put_name_pair
   178 	        (ReduceAxiomsN.relevant_filter (!relevant) goal 
   179 		  (ResAxioms.claset_rules_of_thy thy));
   180 	val claset_cls_thms = #1 (ResAxioms.clausify_rules_pairs claset_rules []);
   181 
   182 	val simpset_rules =
   183 	      ReduceAxiomsN.relevant_filter (!relevant) goal 
   184                 (ResAxioms.simpset_rules_of_thy thy);
   185 	val named_simpset = map put_name_pair simpset_rules
   186 	val simpset_cls_thms = #1 (ResAxioms.clausify_rules_pairs named_simpset []);
   187 
   188 	val cls_thms = if !use_simpset then (claset_cls_thms@simpset_cls_thms) 
   189 	               else claset_cls_thms;
   190 	val cls_thms_list = List.concat cls_thms;
   191 	(*************************************************)
   192 	(* Write out clauses as tptp strings to filename *)
   193 	(*************************************************)
   194 	val clauses = map #1(cls_thms_list);
   195 	val cls_tptp_strs = map ResClause.tptp_clause clauses;
   196         val alllist = pairup cls_thms_list cls_tptp_strs
   197         val whole_list = List.concat (map clause_numbering alllist);
   198  
   199         (*********************************************************)
   200 	(* create array and put clausename, number pairs into it *)
   201 	(*********************************************************)
   202 	val num_of_clauses =  0;
   203 	val clause_arr = Array.fromList whole_list;
   204 	val num_of_clauses = List.length whole_list;
   205 
   206 	(* list of lists of tptp string clauses *)
   207 	val stick_clasrls =   List.concat cls_tptp_strs;
   208 	val out = TextIO.openOut filename;
   209 	val _=   ResLib.writeln_strs out stick_clasrls;
   210 	val _= TextIO.closeOut out
   211   in
   212 	(clause_arr, num_of_clauses, clauses)
   213   end;
   214 
   215 
   216 end;
   217 
   218 
   219