src/HOL/Tools/ATP/res_clasimpset.ML
author paulson
Mon Sep 19 15:12:13 2005 +0200 (2005-09-19)
changeset 17484 f6a225f97f0a
parent 17412 e26cb20ef0cc
child 17525 ae5bb6001afb
permissions -rw-r--r--
simplification of the Isabelle-ATP code; hooks for batch generation of problems
     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 fun axioms_having_consts cs tab = axioms_having_consts_aux cs tab [];
    69 
    70 
    71 fun relevant_axioms goal thmTab n =  
    72     let val consts = consts_in_goal goal
    73 	fun relevant_axioms_aux1 cs k =
    74 	    let val thms1 = axioms_having_consts cs thmTab
    75 		val cs1 = ResLib.flat_noDup (map consts_of_thm thms1)
    76 	    in
    77 		if ((cs1 subset cs) orelse n <= k) then (k,thms1) 
    78 		else (relevant_axioms_aux1 (cs1 union cs) (k+1))
    79 	    end
    80 
    81     in  relevant_axioms_aux1 consts 1  end;
    82 
    83 
    84 fun relevant_filter n goal thms = 
    85     if n<=0 then thms 
    86     else #2 (relevant_axioms goal (make_thm_table thms) n);
    87 
    88 
    89 (* find the thms from thy that contain relevant constants, n is the iteration number *)
    90 fun find_axioms_n thy goal n =
    91     let val clasetR = ResAxioms.claset_rules_of_thy thy
    92 	val simpsetR = ResAxioms.simpset_rules_of_thy thy	  
    93 	val table = make_thm_table (clasetR @ simpsetR)	
    94     in
    95 	relevant_axioms goal table n
    96     end;
    97 
    98 
    99 fun find_axioms_n_c thy goal n =
   100     let val current_thms = PureThy.thms_of thy
   101 	val table = make_thm_table current_thms
   102     in
   103 	relevant_axioms goal table n
   104     end;
   105 
   106 end;
   107 
   108 
   109 signature RES_CLASIMP = 
   110   sig
   111   val relevant : int ref
   112   val use_simpset: bool ref
   113   val use_nameless: bool ref
   114   val get_clasimp_lemmas : 
   115          Proof.context -> term -> 
   116          (ResClause.clause * thm) Array.array * ResClause.clause list 
   117   end;
   118 
   119 structure ResClasimp : RES_CLASIMP =
   120 struct
   121 val use_simpset = ref false;   (*Performance is much better without simprules*)
   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 fun multi x 0 xlist = xlist
   146    |multi x n xlist = multi x (n-1) (x::xlist);
   147 
   148 fun clause_numbering ((clause, theorem), num_of_cls) = 
   149     let val numbers = 0 upto (num_of_cls - 1)
   150     in 
   151 	multi (clause, theorem) num_of_cls []
   152     end;
   153     
   154 
   155 (*Write out the claset and simpset rules of the supplied theory.
   156   FIXME: argument "goal" is a hack to allow relevance filtering.
   157   To reduce the number of clauses produced, set ResClasimp.relevant:=1*)
   158 fun get_clasimp_lemmas ctxt goal = 
   159     let val claset_rules = 
   160               map put_name_pair
   161 	        (ReduceAxiomsN.relevant_filter (!relevant) goal 
   162 		  (ResAxioms.claset_rules_of_ctxt ctxt));
   163 	val claset_cls_thms = #1 (ResAxioms.clausify_rules_pairs claset_rules []);
   164 
   165 	val simpset_rules =
   166 	      ReduceAxiomsN.relevant_filter (!relevant) goal 
   167                 (ResAxioms.simpset_rules_of_ctxt ctxt);
   168 	val named_simpset = map put_name_pair simpset_rules
   169 	val simpset_cls_thms = #1 (ResAxioms.clausify_rules_pairs named_simpset []);
   170 
   171 	val cls_thms = if !use_simpset then (claset_cls_thms@simpset_cls_thms) 
   172 	               else claset_cls_thms;
   173 	val cls_thms_list = List.concat cls_thms;
   174 	(*************************************************)
   175 	(* Identify the set of clauses to be written out *)
   176 	(*************************************************)
   177 	val clauses = map #1(cls_thms_list);
   178 	val cls_nums = map ResClause.num_of_clauses clauses;
   179         val whole_list = List.concat 
   180               (map clause_numbering (ListPair.zip (cls_thms_list, cls_nums)));
   181  
   182         (*********************************************************)
   183 	(* create array and put clausename, number pairs into it *)
   184 	(*********************************************************)
   185 	val clause_arr = Array.fromList whole_list;
   186   in
   187 	(clause_arr, clauses)
   188   end;
   189 
   190 
   191 end;
   192 
   193 
   194