src/HOL/Tools/ATP/res_clasimpset.ML
author wenzelm
Mon Sep 05 17:38:18 2005 +0200 (2005-09-05)
changeset 17261 193b84a70ca4
parent 17234 12a9393c5d77
child 17305 6cef3aedd661
permissions -rw-r--r--
curried_lookup/update;
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@17234
     9
   Remove irrelevant axioms used for a proof of a goal, with with iteration control
paulson@17234
    10
   
paulson@17234
    11
   Initial version. Needs elaboration. *)
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@16795
    17
  | add_term_consts_rm ncs (t $ u) cs =
paulson@16795
    18
      add_term_consts_rm ncs t (add_term_consts_rm ncs u cs)
paulson@16741
    19
  | add_term_consts_rm ncs (Abs(_,_,t)) cs = add_term_consts_rm ncs t cs
paulson@16741
    20
  | add_term_consts_rm ncs _ cs = cs;
paulson@16741
    21
paulson@16741
    22
paulson@16741
    23
fun term_consts_rm ncs t = add_term_consts_rm ncs t [];
paulson@16741
    24
paulson@16741
    25
paulson@16741
    26
fun thm_consts_rm ncs thm = term_consts_rm ncs (prop_of thm);
paulson@16741
    27
paulson@16741
    28
paulson@16741
    29
fun consts_of_thm (n,thm) = thm_consts_rm ["Trueprop","==>","all","Ex","op &", "op |", "Not", "All", "op -->", "op =", "==", "True", "False"] thm;
paulson@16741
    30
paulson@16741
    31
fun consts_of_term term = term_consts_rm ["Trueprop","==>","all","Ex","op &", "op |", "Not", "All", "op -->", "op =", "==", "True", "False"] term;
paulson@16741
    32
paulson@16741
    33
fun make_pairs [] _ = []
paulson@16741
    34
  | make_pairs (x::xs) y = (x,y)::(make_pairs xs y);
paulson@16741
    35
paulson@16741
    36
paulson@16741
    37
paulson@16741
    38
fun const_thm_list_aux [] cthms = cthms
paulson@16741
    39
  | const_thm_list_aux (thm::thms) cthms =
paulson@16741
    40
    let val consts = consts_of_thm thm
paulson@16741
    41
	val cthms' = make_pairs consts thm 
paulson@16741
    42
    in
paulson@16741
    43
	const_thm_list_aux thms (cthms' @ cthms)
paulson@16741
    44
    end;
paulson@16741
    45
paulson@16741
    46
paulson@16741
    47
fun const_thm_list thms = const_thm_list_aux thms [];
paulson@16741
    48
paulson@16741
    49
fun make_thm_table thms  = 
paulson@16741
    50
    let val consts_thms = const_thm_list thms
paulson@16741
    51
    in
paulson@16741
    52
	Symtab.make_multi consts_thms
paulson@16741
    53
    end;
paulson@16741
    54
paulson@16741
    55
paulson@16741
    56
fun consts_in_goal goal = consts_of_term goal;
paulson@16741
    57
paulson@16741
    58
fun axioms_having_consts_aux [] tab thms = thms
paulson@16741
    59
  | axioms_having_consts_aux (c::cs) tab thms =
wenzelm@17261
    60
    let val thms1 = Symtab.curried_lookup tab c
wenzelm@17261
    61
      val thms2 = 
wenzelm@17261
    62
          case thms1 of (SOME x) => x
wenzelm@17261
    63
                      | NONE => []
paulson@16741
    64
    in
wenzelm@17261
    65
      axioms_having_consts_aux cs tab (thms2 union thms)
paulson@16741
    66
    end;
paulson@16741
    67
paulson@16741
    68
fun axioms_having_consts cs tab = axioms_having_consts_aux cs tab [];
paulson@16741
    69
paulson@16741
    70
paulson@16741
    71
fun relevant_axioms goal thmTab n =  
paulson@16741
    72
    let val consts = consts_in_goal goal
paulson@16741
    73
	fun relevant_axioms_aux1 cs k =
paulson@16741
    74
	    let val thms1 = axioms_having_consts cs thmTab
paulson@16741
    75
		val cs1 = ResLib.flat_noDup (map consts_of_thm thms1)
paulson@16741
    76
	    in
paulson@16795
    77
		if ((cs1 subset cs) orelse n <= k) then (k,thms1) 
paulson@16741
    78
		else (relevant_axioms_aux1 (cs1 union cs) (k+1))
paulson@16741
    79
	    end
paulson@16741
    80
paulson@16795
    81
    in  relevant_axioms_aux1 consts 1  end;
paulson@16741
    82
paulson@16741
    83
paulson@16795
    84
fun relevant_filter n goal thms = 
paulson@16795
    85
    if n<=0 then thms 
paulson@16795
    86
    else #2 (relevant_axioms goal (make_thm_table thms) n);
paulson@16741
    87
paulson@16741
    88
paulson@16741
    89
(* find the thms from thy that contain relevant constants, n is the iteration number *)
paulson@16741
    90
fun find_axioms_n thy goal n =
paulson@16741
    91
    let val clasetR = ResAxioms.claset_rules_of_thy thy
paulson@16741
    92
	val simpsetR = ResAxioms.simpset_rules_of_thy thy	  
paulson@16741
    93
	val table = make_thm_table (clasetR @ simpsetR)	
paulson@16741
    94
    in
paulson@16741
    95
	relevant_axioms goal table n
paulson@16741
    96
    end;
paulson@16741
    97
paulson@16741
    98
paulson@16741
    99
fun find_axioms_n_c thy goal n =
paulson@16741
   100
    let val current_thms = PureThy.thms_of thy
paulson@16741
   101
	val table = make_thm_table current_thms
paulson@16741
   102
    in
paulson@16741
   103
	relevant_axioms goal table n
paulson@16741
   104
    end;
paulson@16741
   105
paulson@16741
   106
end;
paulson@16741
   107
paulson@16741
   108
quigley@16156
   109
signature RES_CLASIMP = 
quigley@15919
   110
  sig
quigley@16950
   111
paulson@16795
   112
  val relevant : int ref
quigley@16950
   113
  val use_simpset: bool ref
paulson@16957
   114
  val use_nameless: bool ref
paulson@16795
   115
  val write_out_clasimp : string -> theory -> term -> 
quigley@17150
   116
                             (ResClause.clause * thm) Array.array * int * ResClause.clause list 
quigley@16950
   117
quigley@15919
   118
  end;
quigley@15919
   119
quigley@15919
   120
structure ResClasimp : RES_CLASIMP =
quigley@15919
   121
struct
quigley@16950
   122
val use_simpset = ref true;
paulson@16957
   123
val use_nameless = ref false;  (*Because most are useless [iff] rules*)
paulson@16957
   124
paulson@16957
   125
val relevant = ref 0;  (*Relevance filtering is off by default*)
paulson@16795
   126
paulson@16956
   127
(*The "name" of a theorem is its statement, if nothing else is available.*)
paulson@16956
   128
val plain_string_of_thm =
paulson@16956
   129
    setmp show_question_marks false 
paulson@16956
   130
      (setmp print_mode [] 
paulson@16956
   131
	(Pretty.setmp_margin 999 string_of_thm));
paulson@16957
   132
	
paulson@16957
   133
fun fake_thm_name th = 
paulson@16957
   134
    Context.theory_name (theory_of_thm th) ^ "." ^ 
paulson@16957
   135
    ResLib.trim_ends (plain_string_of_thm th);
paulson@16061
   136
paulson@16957
   137
fun put_name_pair ("",th) = if !use_nameless then (fake_thm_name th, th)
paulson@16957
   138
                            else ("HOL.TrueI",TrueI)
paulson@16956
   139
  | put_name_pair (a,th)  = (a,th);
quigley@16039
   140
quigley@15919
   141
(* changed, now it also finds out the name of the theorem. *)
quigley@15919
   142
(* convert a theorem into CNF and then into Clause.clause format. *)
quigley@16039
   143
quigley@16039
   144
(* outputs a list of (thm,clause) pairs *)
quigley@15643
   145
quigley@15643
   146
paulson@15956
   147
(*Insert th into the result provided the name is not "".*)
paulson@15956
   148
fun add_nonempty ((name,th), ths) = if name="" then ths else th::ths;
paulson@15956
   149
quigley@16156
   150
fun posinlist x [] n = "not in list"
quigley@16156
   151
|   posinlist x (y::ys) n = if (x=y) 
quigley@16156
   152
			    then
quigley@16156
   153
 				string_of_int n
quigley@16156
   154
			    else posinlist x (ys) (n+1)
quigley@16156
   155
quigley@16156
   156
quigley@16156
   157
fun pairup [] [] = []
quigley@16156
   158
|   pairup (x::xs) (y::ys) = (x,y)::(pairup xs ys)
quigley@16156
   159
quigley@16156
   160
fun multi x 0 xlist = xlist
quigley@16156
   161
   |multi x n xlist = multi x (n -1 ) (x::xlist);
quigley@16156
   162
paulson@16172
   163
fun clause_numbering ((clause, theorem), cls) = 
paulson@16172
   164
    let val num_of_cls = length cls
paulson@16172
   165
	val numbers = 0 upto (num_of_cls -1)
paulson@16172
   166
    in 
paulson@17234
   167
	multi (clause, theorem)  num_of_cls []
paulson@16172
   168
    end;
paulson@17234
   169
    
quigley@16950
   170
paulson@16741
   171
(*Write out the claset and simpset rules of the supplied theory.
paulson@16906
   172
  FIXME: argument "goal" is a hack to allow relevance filtering.
paulson@16906
   173
  To reduce the number of clauses produced, set ResClasimp.relevant:=1*)
paulson@16741
   174
fun write_out_clasimp filename thy goal = 
paulson@16795
   175
    let val claset_rules = 
paulson@16956
   176
              map put_name_pair
paulson@16956
   177
	        (ReduceAxiomsN.relevant_filter (!relevant) goal 
paulson@16956
   178
		  (ResAxioms.claset_rules_of_thy thy));
paulson@16956
   179
	val claset_cls_thms = #1 (ResAxioms.clausify_rules_pairs claset_rules []);
quigley@15643
   180
paulson@16795
   181
	val simpset_rules =
paulson@16795
   182
	      ReduceAxiomsN.relevant_filter (!relevant) goal 
paulson@16795
   183
                (ResAxioms.simpset_rules_of_thy thy);
paulson@17234
   184
	val named_simpset = map put_name_pair simpset_rules
paulson@16061
   185
	val simpset_cls_thms = #1 (ResAxioms.clausify_rules_pairs named_simpset []);
quigley@15643
   186
paulson@17234
   187
	val cls_thms = if !use_simpset then (claset_cls_thms@simpset_cls_thms) 
paulson@17234
   188
	               else claset_cls_thms;
paulson@16061
   189
	val cls_thms_list = List.concat cls_thms;
paulson@16061
   190
	(*************************************************)
paulson@16061
   191
	(* Write out clauses as tptp strings to filename *)
paulson@16061
   192
	(*************************************************)
paulson@16061
   193
	val clauses = map #1(cls_thms_list);
paulson@16061
   194
	val cls_tptp_strs = map ResClause.tptp_clause clauses;
quigley@16156
   195
        val alllist = pairup cls_thms_list cls_tptp_strs
quigley@16156
   196
        val whole_list = List.concat (map clause_numbering alllist);
quigley@16156
   197
 
quigley@16156
   198
        (*********************************************************)
quigley@16156
   199
	(* create array and put clausename, number pairs into it *)
quigley@16156
   200
	(*********************************************************)
quigley@16156
   201
	val num_of_clauses =  0;
quigley@16156
   202
	val clause_arr = Array.fromList whole_list;
quigley@16156
   203
	val num_of_clauses = List.length whole_list;
quigley@16156
   204
paulson@16061
   205
	(* list of lists of tptp string clauses *)
paulson@16061
   206
	val stick_clasrls =   List.concat cls_tptp_strs;
paulson@16061
   207
	val out = TextIO.openOut filename;
paulson@16061
   208
	val _=   ResLib.writeln_strs out stick_clasrls;
paulson@16061
   209
	val _= TextIO.closeOut out
paulson@16061
   210
  in
quigley@17150
   211
	(clause_arr, num_of_clauses, clauses)
paulson@16061
   212
  end;
quigley@15643
   213
quigley@16950
   214
quigley@15919
   215
end;
quigley@16039
   216
quigley@16156
   217
quigley@16156
   218