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