src/HOL/Tools/ATP/res_clasimpset.ML
author paulson
Wed Jul 13 15:06:04 2005 +0200 (2005-07-13)
changeset 16795 b400b53d8f7d
parent 16741 7a6c17b826c0
child 16897 7e5319d0f418
permissions -rw-r--r--
relevance filtering is now optional
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
paulson@16795
   113
  val write_out_clasimp_small :
paulson@16795
   114
     string -> theory -> (ResClause.clause * thm) Array.array * int
quigley@15919
   115
  end;
quigley@15919
   116
quigley@15919
   117
structure ResClasimp : RES_CLASIMP =
quigley@15919
   118
struct
quigley@15643
   119
paulson@16795
   120
(*Relevance filtering is off by default*)
paulson@16795
   121
val relevant = ref 0;  
paulson@16795
   122
paulson@16061
   123
fun has_name th = ((Thm.name_of_thm th)  <>  "")
paulson@16061
   124
paulson@16061
   125
fun has_name_pair (a,b) = (a <> "");
quigley@16039
   126
quigley@16039
   127
quigley@15919
   128
(* changed, now it also finds out the name of the theorem. *)
quigley@15919
   129
(* convert a theorem into CNF and then into Clause.clause format. *)
quigley@16039
   130
quigley@16039
   131
(* outputs a list of (thm,clause) pairs *)
quigley@15643
   132
quigley@15643
   133
quigley@16039
   134
(* for tracing: encloses each string element in brackets. *)
quigley@16039
   135
fun concat_with_stop [] = ""
quigley@16039
   136
  | concat_with_stop [x] =  x
quigley@16039
   137
  | concat_with_stop (x::xs) = x^ "." ^ concat_with_stop xs;
quigley@16039
   138
paulson@16061
   139
fun remove_symb str = 
paulson@16061
   140
    if String.isPrefix  "List.op @." str
paulson@16061
   141
    then  ((String.substring(str,0,5)) ^ (String.extract (str, 10, NONE)))
paulson@16061
   142
    else str;
quigley@16039
   143
paulson@16061
   144
fun remove_spaces str = 
paulson@16061
   145
    let val strlist = String.tokens Char.isSpace str
paulson@16061
   146
	val spaceless = concat strlist
paulson@16061
   147
	val strlist' = String.tokens Char.isPunct spaceless
paulson@16061
   148
    in
paulson@16061
   149
	concat_with_stop strlist'
paulson@16061
   150
    end
quigley@16039
   151
paulson@16061
   152
fun remove_symb_pair (str, thm) = (remove_symb str, thm);
quigley@16039
   153
paulson@16061
   154
fun remove_spaces_pair (str, thm) = (remove_spaces str, thm);
quigley@15643
   155
paulson@15956
   156
paulson@15956
   157
(*Insert th into the result provided the name is not "".*)
paulson@15956
   158
fun add_nonempty ((name,th), ths) = if name="" then ths else th::ths;
paulson@15956
   159
quigley@16156
   160
fun posinlist x [] n = "not in list"
quigley@16156
   161
|   posinlist x (y::ys) n = if (x=y) 
quigley@16156
   162
			    then
quigley@16156
   163
 				string_of_int n
quigley@16156
   164
			    else posinlist x (ys) (n+1)
quigley@16156
   165
quigley@16156
   166
quigley@16156
   167
fun pairup [] [] = []
quigley@16156
   168
|   pairup (x::xs) (y::ys) = (x,y)::(pairup xs ys)
quigley@16156
   169
quigley@16156
   170
fun multi x 0 xlist = xlist
quigley@16156
   171
   |multi x n xlist = multi x (n -1 ) (x::xlist);
quigley@16156
   172
paulson@16172
   173
fun clause_numbering ((clause, theorem), cls) = 
paulson@16172
   174
    let val num_of_cls = length cls
paulson@16172
   175
	val numbers = 0 upto (num_of_cls -1)
paulson@16172
   176
	val multi_name = multi (clause, theorem)  num_of_cls []
paulson@16172
   177
    in 
paulson@16172
   178
	(multi_name)
paulson@16172
   179
    end;
quigley@16156
   180
paulson@16741
   181
(*Write out the claset and simpset rules of the supplied theory.
paulson@16741
   182
  FIXME: argument "goal" is a hack to allow relevance filtering.*)
paulson@16741
   183
fun write_out_clasimp filename thy goal = 
paulson@16795
   184
    let val claset_rules = 
paulson@16795
   185
              ReduceAxiomsN.relevant_filter (!relevant) goal 
paulson@16795
   186
                (ResAxioms.claset_rules_of_thy thy);
paulson@16061
   187
	val named_claset = List.filter has_name_pair claset_rules;
paulson@16061
   188
	val claset_names = map remove_spaces_pair (named_claset)
paulson@16061
   189
	val claset_cls_thms = #1 (ResAxioms.clausify_rules_pairs named_claset []);
quigley@15643
   190
paulson@16795
   191
	val simpset_rules =
paulson@16795
   192
	      ReduceAxiomsN.relevant_filter (!relevant) goal 
paulson@16795
   193
                (ResAxioms.simpset_rules_of_thy thy);
paulson@16061
   194
	val named_simpset = 
paulson@16061
   195
	      map remove_spaces_pair (List.filter has_name_pair simpset_rules)
paulson@16061
   196
	val simpset_cls_thms = #1 (ResAxioms.clausify_rules_pairs named_simpset []);
quigley@15643
   197
paulson@16061
   198
	val cls_thms = (claset_cls_thms@simpset_cls_thms);
paulson@16061
   199
	val cls_thms_list = List.concat cls_thms;
quigley@16156
   200
        (* length 1429 *)
paulson@16061
   201
	(*************************************************)
paulson@16061
   202
	(* Write out clauses as tptp strings to filename *)
paulson@16061
   203
	(*************************************************)
paulson@16061
   204
	val clauses = map #1(cls_thms_list);
paulson@16061
   205
	val cls_tptp_strs = map ResClause.tptp_clause clauses;
quigley@16156
   206
        val alllist = pairup cls_thms_list cls_tptp_strs
quigley@16156
   207
        val whole_list = List.concat (map clause_numbering alllist);
quigley@16156
   208
 
quigley@16156
   209
        (*********************************************************)
quigley@16156
   210
	(* create array and put clausename, number pairs into it *)
quigley@16156
   211
	(*********************************************************)
quigley@16156
   212
	val num_of_clauses =  0;
quigley@16156
   213
	val clause_arr = Array.fromList whole_list;
quigley@16156
   214
	val num_of_clauses = List.length whole_list;
quigley@16156
   215
paulson@16061
   216
	(* list of lists of tptp string clauses *)
paulson@16061
   217
	val stick_clasrls =   List.concat cls_tptp_strs;
quigley@16156
   218
        (* length 1581*)
paulson@16061
   219
	val out = TextIO.openOut filename;
paulson@16061
   220
	val _=   ResLib.writeln_strs out stick_clasrls;
paulson@16061
   221
	val _= TextIO.flushOut out;
paulson@16061
   222
	val _= TextIO.closeOut out
paulson@16061
   223
  in
paulson@16061
   224
	(clause_arr, num_of_clauses)
paulson@16061
   225
  end;
quigley@15643
   226
quigley@16357
   227
quigley@16357
   228
quigley@16357
   229
quigley@16357
   230
quigley@16357
   231
(*Write out the claset and simpset rules of the supplied theory. Only include the first 50 rules*)
quigley@16357
   232
fun write_out_clasimp_small filename thy = 
quigley@16357
   233
    let val claset_rules = ResAxioms.claset_rules_of_thy thy;
quigley@16357
   234
	val named_claset = List.filter has_name_pair claset_rules;
quigley@16357
   235
	val claset_names = map remove_spaces_pair (named_claset)
quigley@16357
   236
	val claset_cls_thms = #1 (ResAxioms.clausify_rules_pairs named_claset []);
quigley@16357
   237
quigley@16357
   238
	val simpset_rules = ResAxioms.simpset_rules_of_thy thy;
quigley@16357
   239
	val named_simpset = 
quigley@16357
   240
	      map remove_spaces_pair (List.filter has_name_pair simpset_rules)
quigley@16357
   241
	val simpset_cls_thms = #1 (ResAxioms.clausify_rules_pairs named_simpset []);
quigley@16357
   242
quigley@16357
   243
	val cls_thms = (claset_cls_thms@simpset_cls_thms);
quigley@16357
   244
	val cls_thms_list = List.concat cls_thms;
quigley@16357
   245
        (* length 1429 *)
quigley@16357
   246
	(*************************************************)
quigley@16357
   247
	(* Write out clauses as tptp strings to filename *)
quigley@16357
   248
	(*************************************************)
quigley@16357
   249
	val clauses = map #1(cls_thms_list);
quigley@16357
   250
	val cls_tptp_strs = map ResClause.tptp_clause clauses;
quigley@16357
   251
        val alllist = pairup cls_thms_list cls_tptp_strs
quigley@16357
   252
        val whole_list = List.concat (map clause_numbering alllist);
quigley@16357
   253
        val mini_list = List.take ( whole_list,50)
quigley@16357
   254
        (*********************************************************)
quigley@16357
   255
	(* create array and put clausename, number pairs into it *)
quigley@16357
   256
	(*********************************************************)
quigley@16357
   257
	val num_of_clauses =  0;
quigley@16357
   258
	val clause_arr = Array.fromList mini_list;
quigley@16357
   259
	val num_of_clauses = List.length mini_list;
quigley@16357
   260
quigley@16357
   261
	(* list of lists of tptp string clauses *)
quigley@16357
   262
	val stick_clasrls =   List.concat cls_tptp_strs;
quigley@16357
   263
        (* length 1581*)
quigley@16357
   264
	val out = TextIO.openOut filename;
quigley@16357
   265
	val _=   ResLib.writeln_strs out (List.take (stick_clasrls,50));
quigley@16357
   266
	val _= TextIO.flushOut out;
quigley@16357
   267
	val _= TextIO.closeOut out
quigley@16357
   268
  in
quigley@16357
   269
	(clause_arr, num_of_clauses)
quigley@16357
   270
  end;
quigley@16357
   271
quigley@16357
   272
quigley@16357
   273
quigley@15919
   274
end;
quigley@16039
   275
quigley@16156
   276
quigley@16156
   277