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