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