src/HOL/Tools/ATP/res_clasimpset.ML
author paulson
Fri Jul 22 17:43:49 2005 +0200 (2005-07-22)
changeset 16906 74eddde1de2f
parent 16897 7e5319d0f418
child 16950 e7f0f41d513a
permissions -rw-r--r--
dead code removal
paulson@16795
     1
(*  ID:      $Id$
paulson@15789
     2
    Author:     Claire Quigley
paulson@15789
     3
    Copyright   2004  University of Cambridge
paulson@15789
     4
*)
paulson@15789
     5
paulson@16741
     6
paulson@16741
     7
structure ReduceAxiomsN =
paulson@16741
     8
(* Author: Jia Meng, Cambridge University Computer Laboratory
paulson@16741
     9
   Remove irrelevant axioms used for a proof of a goal, with with iteration control*)
paulson@16741
    10
paulson@16741
    11
struct
paulson@16741
    12
paulson@16741
    13
fun add_term_consts_rm ncs (Const(c, _)) cs = 
paulson@16741
    14
    if (c mem ncs) then cs else (c ins_string cs)
paulson@16795
    15
  | add_term_consts_rm ncs (t $ u) cs =
paulson@16795
    16
      add_term_consts_rm ncs t (add_term_consts_rm ncs u cs)
paulson@16741
    17
  | add_term_consts_rm ncs (Abs(_,_,t)) cs = add_term_consts_rm ncs t cs
paulson@16741
    18
  | add_term_consts_rm ncs _ cs = cs;
paulson@16741
    19
paulson@16741
    20
paulson@16741
    21
fun term_consts_rm ncs t = add_term_consts_rm ncs t [];
paulson@16741
    22
paulson@16741
    23
paulson@16741
    24
fun thm_consts_rm ncs thm = term_consts_rm ncs (prop_of thm);
paulson@16741
    25
paulson@16741
    26
paulson@16741
    27
fun consts_of_thm (n,thm) = thm_consts_rm ["Trueprop","==>","all","Ex","op &", "op |", "Not", "All", "op -->", "op =", "==", "True", "False"] thm;
paulson@16741
    28
paulson@16741
    29
fun consts_of_term term = term_consts_rm ["Trueprop","==>","all","Ex","op &", "op |", "Not", "All", "op -->", "op =", "==", "True", "False"] term;
paulson@16741
    30
paulson@16741
    31
fun make_pairs [] _ = []
paulson@16741
    32
  | make_pairs (x::xs) y = (x,y)::(make_pairs xs y);
paulson@16741
    33
paulson@16741
    34
paulson@16741
    35
paulson@16741
    36
fun const_thm_list_aux [] cthms = cthms
paulson@16741
    37
  | const_thm_list_aux (thm::thms) cthms =
paulson@16741
    38
    let val consts = consts_of_thm thm
paulson@16741
    39
	val cthms' = make_pairs consts thm 
paulson@16741
    40
    in
paulson@16741
    41
	const_thm_list_aux thms (cthms' @ cthms)
paulson@16741
    42
    end;
paulson@16741
    43
paulson@16741
    44
paulson@16741
    45
fun const_thm_list thms = const_thm_list_aux thms [];
paulson@16741
    46
paulson@16741
    47
fun make_thm_table thms  = 
paulson@16741
    48
    let val consts_thms = const_thm_list thms
paulson@16741
    49
    in
paulson@16741
    50
	Symtab.make_multi consts_thms
paulson@16741
    51
    end;
paulson@16741
    52
paulson@16741
    53
paulson@16741
    54
fun consts_in_goal goal = consts_of_term goal;
paulson@16741
    55
paulson@16741
    56
fun axioms_having_consts_aux [] tab thms = thms
paulson@16741
    57
  | axioms_having_consts_aux (c::cs) tab thms =
paulson@16741
    58
    let val thms1 = Symtab.lookup(tab,c)
paulson@16741
    59
	val thms2 = 
paulson@16741
    60
	    case thms1 of (SOME x) => x
paulson@16741
    61
			| NONE => []
paulson@16741
    62
    in
paulson@16741
    63
	axioms_having_consts_aux cs tab (thms2 union thms)
paulson@16741
    64
    end;
paulson@16741
    65
paulson@16741
    66
paulson@16741
    67
fun axioms_having_consts cs tab = axioms_having_consts_aux cs tab [];
paulson@16741
    68
paulson@16741
    69
paulson@16741
    70
fun relevant_axioms goal thmTab n =  
paulson@16741
    71
    let val consts = consts_in_goal goal
paulson@16741
    72
	fun relevant_axioms_aux1 cs k =
paulson@16741
    73
	    let val thms1 = axioms_having_consts cs thmTab
paulson@16741
    74
		val cs1 = ResLib.flat_noDup (map consts_of_thm thms1)
paulson@16741
    75
	    in
paulson@16795
    76
		if ((cs1 subset cs) orelse n <= k) then (k,thms1) 
paulson@16741
    77
		else (relevant_axioms_aux1 (cs1 union cs) (k+1))
paulson@16741
    78
	    end
paulson@16741
    79
paulson@16795
    80
    in  relevant_axioms_aux1 consts 1  end;
paulson@16741
    81
paulson@16741
    82
paulson@16795
    83
fun relevant_filter n goal thms = 
paulson@16795
    84
    if n<=0 then thms 
paulson@16795
    85
    else #2 (relevant_axioms goal (make_thm_table thms) n);
paulson@16741
    86
paulson@16741
    87
paulson@16741
    88
(* find the thms from thy that contain relevant constants, n is the iteration number *)
paulson@16741
    89
fun find_axioms_n thy goal n =
paulson@16741
    90
    let val clasetR = ResAxioms.claset_rules_of_thy thy
paulson@16741
    91
	val simpsetR = ResAxioms.simpset_rules_of_thy thy	  
paulson@16741
    92
	val table = make_thm_table (clasetR @ simpsetR)	
paulson@16741
    93
    in
paulson@16741
    94
	relevant_axioms goal table n
paulson@16741
    95
    end;
paulson@16741
    96
paulson@16741
    97
paulson@16741
    98
fun find_axioms_n_c thy goal n =
paulson@16741
    99
    let val current_thms = PureThy.thms_of thy
paulson@16741
   100
	val table = make_thm_table current_thms
paulson@16741
   101
    in
paulson@16741
   102
	relevant_axioms goal table n
paulson@16741
   103
    end;
paulson@16741
   104
paulson@16741
   105
end;
paulson@16741
   106
paulson@16741
   107
quigley@16156
   108
signature RES_CLASIMP = 
quigley@15919
   109
  sig
paulson@16795
   110
  val relevant : int ref
paulson@16795
   111
  val write_out_clasimp : string -> theory -> term -> 
paulson@16741
   112
                             (ResClause.clause * thm) Array.array * int
quigley@15919
   113
  end;
quigley@15919
   114
quigley@15919
   115
structure ResClasimp : RES_CLASIMP =
quigley@15919
   116
struct
quigley@15643
   117
paulson@16795
   118
(*Relevance filtering is off by default*)
paulson@16795
   119
val relevant = ref 0;  
paulson@16795
   120
paulson@16061
   121
fun has_name th = ((Thm.name_of_thm th)  <>  "")
paulson@16061
   122
paulson@16061
   123
fun has_name_pair (a,b) = (a <> "");
quigley@16039
   124
quigley@16039
   125
quigley@15919
   126
(* changed, now it also finds out the name of the theorem. *)
quigley@15919
   127
(* convert a theorem into CNF and then into Clause.clause format. *)
quigley@16039
   128
quigley@16039
   129
(* outputs a list of (thm,clause) pairs *)
quigley@15643
   130
quigley@15643
   131
quigley@16039
   132
(* for tracing: encloses each string element in brackets. *)
quigley@16039
   133
fun concat_with_stop [] = ""
quigley@16039
   134
  | concat_with_stop [x] =  x
quigley@16039
   135
  | concat_with_stop (x::xs) = x^ "." ^ concat_with_stop xs;
quigley@16039
   136
paulson@16061
   137
fun remove_symb str = 
paulson@16061
   138
    if String.isPrefix  "List.op @." str
paulson@16061
   139
    then  ((String.substring(str,0,5)) ^ (String.extract (str, 10, NONE)))
paulson@16061
   140
    else str;
quigley@16039
   141
paulson@16061
   142
fun remove_spaces str = 
paulson@16061
   143
    let val strlist = String.tokens Char.isSpace str
paulson@16061
   144
	val spaceless = concat strlist
paulson@16061
   145
	val strlist' = String.tokens Char.isPunct spaceless
paulson@16061
   146
    in
paulson@16061
   147
	concat_with_stop strlist'
paulson@16061
   148
    end
quigley@16039
   149
paulson@16061
   150
fun remove_symb_pair (str, thm) = (remove_symb str, thm);
quigley@16039
   151
paulson@16061
   152
fun remove_spaces_pair (str, thm) = (remove_spaces str, thm);
quigley@15643
   153
paulson@15956
   154
paulson@15956
   155
(*Insert th into the result provided the name is not "".*)
paulson@15956
   156
fun add_nonempty ((name,th), ths) = if name="" then ths else th::ths;
paulson@15956
   157
quigley@16156
   158
fun posinlist x [] n = "not in list"
quigley@16156
   159
|   posinlist x (y::ys) n = if (x=y) 
quigley@16156
   160
			    then
quigley@16156
   161
 				string_of_int n
quigley@16156
   162
			    else posinlist x (ys) (n+1)
quigley@16156
   163
quigley@16156
   164
quigley@16156
   165
fun pairup [] [] = []
quigley@16156
   166
|   pairup (x::xs) (y::ys) = (x,y)::(pairup xs ys)
quigley@16156
   167
quigley@16156
   168
fun multi x 0 xlist = xlist
quigley@16156
   169
   |multi x n xlist = multi x (n -1 ) (x::xlist);
quigley@16156
   170
paulson@16172
   171
fun clause_numbering ((clause, theorem), cls) = 
paulson@16172
   172
    let val num_of_cls = length cls
paulson@16172
   173
	val numbers = 0 upto (num_of_cls -1)
paulson@16172
   174
	val multi_name = multi (clause, theorem)  num_of_cls []
paulson@16172
   175
    in 
paulson@16172
   176
	(multi_name)
paulson@16172
   177
    end;
quigley@16156
   178
paulson@16741
   179
(*Write out the claset and simpset rules of the supplied theory.
paulson@16906
   180
  FIXME: argument "goal" is a hack to allow relevance filtering.
paulson@16906
   181
  To reduce the number of clauses produced, set ResClasimp.relevant:=1*)
paulson@16741
   182
fun write_out_clasimp filename thy goal = 
paulson@16795
   183
    let val claset_rules = 
paulson@16795
   184
              ReduceAxiomsN.relevant_filter (!relevant) goal 
paulson@16795
   185
                (ResAxioms.claset_rules_of_thy thy);
paulson@16061
   186
	val named_claset = List.filter has_name_pair claset_rules;
paulson@16061
   187
	val claset_cls_thms = #1 (ResAxioms.clausify_rules_pairs named_claset []);
quigley@15643
   188
paulson@16795
   189
	val simpset_rules =
paulson@16795
   190
	      ReduceAxiomsN.relevant_filter (!relevant) goal 
paulson@16795
   191
                (ResAxioms.simpset_rules_of_thy thy);
paulson@16061
   192
	val named_simpset = 
paulson@16061
   193
	      map remove_spaces_pair (List.filter has_name_pair simpset_rules)
paulson@16061
   194
	val simpset_cls_thms = #1 (ResAxioms.clausify_rules_pairs named_simpset []);
quigley@15643
   195
paulson@16061
   196
	val cls_thms = (claset_cls_thms@simpset_cls_thms);
paulson@16061
   197
	val cls_thms_list = List.concat cls_thms;
quigley@16156
   198
        (* length 1429 *)
paulson@16061
   199
	(*************************************************)
paulson@16061
   200
	(* Write out clauses as tptp strings to filename *)
paulson@16061
   201
	(*************************************************)
paulson@16061
   202
	val clauses = map #1(cls_thms_list);
paulson@16061
   203
	val cls_tptp_strs = map ResClause.tptp_clause clauses;
quigley@16156
   204
        val alllist = pairup cls_thms_list cls_tptp_strs
quigley@16156
   205
        val whole_list = List.concat (map clause_numbering alllist);
quigley@16156
   206
 
quigley@16156
   207
        (*********************************************************)
quigley@16156
   208
	(* create array and put clausename, number pairs into it *)
quigley@16156
   209
	(*********************************************************)
quigley@16156
   210
	val num_of_clauses =  0;
quigley@16156
   211
	val clause_arr = Array.fromList whole_list;
quigley@16156
   212
	val num_of_clauses = List.length whole_list;
quigley@16156
   213
paulson@16061
   214
	(* list of lists of tptp string clauses *)
paulson@16061
   215
	val stick_clasrls =   List.concat cls_tptp_strs;
quigley@16156
   216
        (* length 1581*)
paulson@16061
   217
	val out = TextIO.openOut filename;
paulson@16061
   218
	val _=   ResLib.writeln_strs out stick_clasrls;
paulson@16061
   219
	val _= TextIO.flushOut out;
paulson@16061
   220
	val _= TextIO.closeOut out
paulson@16061
   221
  in
paulson@16061
   222
	(clause_arr, num_of_clauses)
paulson@16061
   223
  end;
quigley@15643
   224
quigley@15919
   225
end;
quigley@16039
   226
quigley@16156
   227
quigley@16156
   228