src/HOL/Tools/ATP/recon_order_clauses.ML
author wenzelm
Mon Jul 17 18:42:37 2006 +0200 (2006-07-17)
changeset 20138 6dc6fc8b261e
parent 17312 159783c74f75
permissions -rw-r--r--
replaced butlast by Library.split_last;
removed dead code;
     1 (*  ID:         $Id$
     2     Author:     Claire Quigley
     3     Copyright   2004  University of Cambridge
     4 *)
     5 
     6 structure ReconOrderClauses =
     7 struct
     8 
     9 (*----------------------------------------------*)
    10 (* Reorder clauses for use in binary resolution *)
    11 (*----------------------------------------------*)
    12 
    13 fun remove_nth n [] = []
    14 |   remove_nth n xs = (List.take (xs, n-1)) @ (List.drop (xs, n))
    15 
    16 (*Differs from List.nth: it counts from 1 rather than from 0*)
    17 fun get_nth n (x::xs) = hd (Library.drop (n-1, x::xs))
    18 
    19 
    20 exception Not_in_list;  
    21 
    22 
    23 (* code to rearrange clauses so that they're the same as the parsed in SPASS version *)
    24 
    25  fun takeUntil ch [] res  = (res, [])
    26  |   takeUntil ch (x::xs) res = if   x = ch 
    27                                 then
    28                                      (res, xs)
    29                                 else
    30                                      takeUntil ch xs (res@[x]);
    31 
    32 fun contains_eq str = "=" mem str 
    33 
    34 fun eq_not_neq str = let val uptoeq = fst(takeUntil "=" str [])
    35                      in (List.last uptoeq) <> "~" end
    36                    
    37 fun get_eq_strs str =  if eq_not_neq  str   (*not an inequality *)
    38                        then 
    39                            let val (left, right) = takeUntil "=" str []
    40                            in
    41                                (#1 (split_last left), tl right)
    42                            end
    43                        else                  (* is an inequality *)
    44                            let val (left, right) = takeUntil "~" str []
    45                            in 
    46                               (#1 (split_last left), tl (tl right))
    47                            end
    48                 
    49 
    50 
    51 fun switch_equal a x = let val (a_lhs, a_rhs) = get_eq_strs a
    52                            val (x_lhs, x_rhs) = get_eq_strs x
    53                        in
    54                            (a_lhs = x_rhs) andalso (a_rhs = x_lhs)
    55                        end
    56 
    57 fun is_var_pair (a,b) vars = (a mem vars) andalso (b mem vars)
    58 
    59 fun var_equiv vars (a,b)  = a=b orelse (is_var_pair (a,b) vars)
    60 
    61 fun all_true [] = false
    62 |   all_true xs = null (List.filter (equal false ) xs)
    63 
    64 
    65 
    66 fun var_pos_eq vars x y = 
    67     String.size x = String.size y andalso
    68     let val xs = explode x
    69 	val ys = explode y
    70 	val xsys = ListPair.zip (xs,ys)
    71 	val are_var_pairs = map (var_equiv vars) xsys
    72     in
    73 	all_true are_var_pairs 
    74     end;
    75 
    76 fun pos_in_list a [] allvars (pos_num, symlist, nsymlist) =  raise Not_in_list
    77   | pos_in_list a (x::[]) allvars (pos_num , symlist, nsymlist) = 
    78       let val y = explode x 
    79 	  val b = explode a
    80       in
    81 	 if  b = y
    82 	 then 
    83 	      (pos_num, symlist, nsymlist)
    84 	 else 
    85 	      if (var_pos_eq allvars  a x) 
    86 	      then  (* Equal apart from meta-vars having different names *)
    87 		  (pos_num, symlist, nsymlist)
    88 	      else 
    89 		  if (contains_eq b) andalso (contains_eq y)
    90 		  then 
    91 		      if (eq_not_neq b) andalso (eq_not_neq y)  andalso (switch_equal b y )          
    92 		      then (* both are equalities and equal under sym*) 
    93 			  (pos_num, (pos_num::symlist), nsymlist)  (* add pos to symlist *)                                                                    
    94 			  else 
    95 			  if not(eq_not_neq b) andalso not(eq_not_neq y) andalso  (switch_equal b y)
    96 			   then (* if they're equal under not_sym *)
    97 			       (pos_num, (symlist), (pos_num::nsymlist))(* add pos to nsymlist *)
    98 			   else 
    99 				raise Not_in_list
   100 		  else 
   101 		       raise Not_in_list
   102       end   
   103   | pos_in_list a (x::xs) allvars (pos_num, symlist, nsymlist) = 
   104       let val y = explode x 
   105 	  val b = explode a
   106       in
   107 	 if  b = y
   108 	 then 
   109 	      (pos_num, symlist, nsymlist)
   110 	 
   111 	 else
   112 	       if (var_pos_eq allvars  a x) (* Equal apart from meta-vars having different names *)
   113 	       then 
   114 		  (pos_num, symlist, nsymlist)
   115 	       else 
   116 		   if (contains_eq b) andalso (contains_eq y)
   117 		   then 
   118 		       if (eq_not_neq b) andalso (eq_not_neq y)  andalso (switch_equal b y )   (* both are equalities and equal under sym*)        
   119 		       then 
   120 			   (pos_num, (pos_num::symlist), nsymlist)  (* add pos to symlist *)                                                                    else 
   121 			   if not(eq_not_neq b) andalso not(eq_not_neq y) andalso  (switch_equal b y )  (* if they're equal under not_sym *)
   122 			   then 
   123 				(pos_num, (symlist), (pos_num::nsymlist))(* add pos to nsymlist *)
   124 			   else 
   125 				pos_in_list a xs allvars((pos_num + 1), symlist, nsymlist)
   126 		   else 
   127 			 pos_in_list a xs allvars((pos_num + 1), symlist, nsymlist)
   128 		   
   129       end;
   130 
   131 
   132     (* in
   133 	if  b = y
   134 	then 
   135 	     (pos_num, symlist, nsymlist)
   136 	else if (contains_eq b) andalso (contains_eq y)
   137 	     then if (eq_not_neq b) andalso (eq_not_neq y) (* both are equalities*)
   138 		  then if (switch_equal b y )              (* if they're equal under sym *)
   139 		       then 
   140 			   (pos_num, (pos_num::symlist), nsymlist)  (* add pos to symlist *)
   141 		       else 
   142 			     pos_in_list a xs ((pos_num + 1), symlist, nsymlist)
   143 		  else if not(eq_not_neq b) andalso not(eq_not_neq y)  (* both are inequalities *)
   144 		       then if (switch_equal b y )  (* if they're equal under not_sym *)
   145 			    then 
   146 				(pos_num, (symlist), (pos_num::nsymlist))(* add pos to nsymlist *)
   147 			    else 
   148 				     pos_in_list a xs ((pos_num + 1), symlist, nsymlist)
   149 		       else
   150 			      pos_in_list a xs ((pos_num + 1), symlist, nsymlist)
   151 		   else  
   152 			   pos_in_list a xs ((pos_num + 1), symlist, nsymlist)
   153 	      else 
   154 		      pos_in_list a xs ((pos_num + 1), symlist, nsymlist)
   155      end   
   156 
   157     *)
   158 
   159 
   160 (* checkorder Spass Isabelle [] *)
   161 
   162 fun checkorder [] strlist allvars (numlist, symlist, not_symlist) =
   163       (numlist,symlist, not_symlist)
   164 |   checkorder (x::xs) strlist allvars (numlist, symlist, not_symlist) =  
   165          let val (posnum, symlist', not_symlist') = 
   166                pos_in_list x strlist allvars (0, symlist, not_symlist) 
   167          in
   168              checkorder xs  strlist allvars ((numlist@[posnum]), symlist', not_symlist') 
   169          end
   170 
   171 fun is_digit ch =
   172     ( ch >=  "0" andalso ch <=  "9")
   173 
   174 
   175 fun is_alpha ch =
   176     (ch >=  "A" andalso  ch <=  "Z") orelse
   177     (ch >=  "a" andalso ch <=  "z")
   178 
   179 
   180 fun is_alpha_space_or_neg_or_eq ch =
   181     (ch = "~") orelse (is_alpha ch) orelse ( ch = " ")orelse ( ch = "=")
   182 
   183 fun lit_string sg t = 
   184     let val termstr = Sign.string_of_term sg t
   185 	val exp_term = explode termstr
   186     in
   187 	implode(List.filter is_alpha_space_or_neg_or_eq exp_term)
   188     end
   189 
   190 fun get_meta_lits thm = map (lit_string (sign_of_thm thm)) (prems_of thm)
   191 
   192 
   193 fun is_alpha_space_or_neg_or_eq_or_bracket ch =
   194    is_alpha_space_or_neg_or_eq ch orelse (ch= "(") orelse (ch = ")")
   195 
   196 fun lit_string_bracket sg t = 
   197     let val termstr = Sign.string_of_term sg t
   198 	val exp_term = explode termstr
   199     in
   200 	implode(List.filter  is_alpha_space_or_neg_or_eq_or_bracket exp_term)
   201     end;
   202 
   203 fun get_meta_lits_bracket thm = 
   204     map (lit_string_bracket (sign_of_thm thm)) (prems_of thm)
   205 
   206       
   207 fun apply_rule rule [] thm = thm
   208 |   apply_rule rule  (x::xs) thm = let val thm' = rule RSN ((x+1),thm)
   209                                   in
   210                                       apply_rule rule xs thm'
   211                                   end
   212 
   213 
   214 
   215 (* resulting thm, clause-strs in spass order, vars *)
   216 
   217 fun rearrange_clause thm res_strlist allvars = 
   218     let val isa_strlist = get_meta_lits thm 
   219         (* change this to use Jia's code to get same looking thing as isastrlist? *)
   220 	val (posns, symlist, not_symlist) = checkorder res_strlist isa_strlist allvars([],[],[])
   221 	val symmed = apply_rule sym symlist thm
   222 	val not_symmed = apply_rule not_sym not_symlist symmed
   223     in
   224        ((rearrange_prems posns not_symmed), posns, symlist,not_symlist)
   225     end
   226     
   227 end;
   228