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