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