src/HOL/Tools/ATP/recon_transfer_proof.ML
author quigley
Tue May 31 12:42:36 2005 +0200 (2005-05-31)
changeset 16156 2f6fc19aba1e
parent 16091 3683f0486a11
child 16157 1764cc98bafd
permissions -rw-r--r--
Fixed array containing clasimpset rules. Added flags to turn on and off reconstruction and full spass
ruleset.
quigley@16156
     1
paulson@15789
     2
(*  ID:         $Id$
quigley@16156
     3
 Author:     Claire Quigley
quigley@16156
     4
 Copyright   2004  University of Cambridge
paulson@15789
     5
*)
paulson@15789
     6
quigley@15642
     7
(******************)
quigley@15642
     8
(* complete later *)
quigley@15642
     9
(******************)
quigley@15642
    10
paulson@15684
    11
structure Recon_Transfer =
paulson@15684
    12
struct
paulson@15684
    13
paulson@15684
    14
open Recon_Parse
paulson@15684
    15
infixr 8 ++; infixr 7 >>; infixr 6 ||;
paulson@15684
    16
quigley@15642
    17
fun not_newline ch = not (ch = "\n");
quigley@15642
    18
quigley@15642
    19
quigley@15642
    20
quigley@15919
    21
(*
quigley@15919
    22
fun fill_cls_array array n [] = ()
quigley@15919
    23
|   fill_cls_array array n (x::xs) = let val _ = Array.update (array, n,x)
quigley@15919
    24
                                     in
quigley@15919
    25
                                        fill_cls_array array (n+1) (xs)
quigley@15919
    26
                                     end;
quigley@15919
    27
quigley@15919
    28
quigley@15919
    29
quigley@15919
    30
fun memo_add_clauses ((name:string, (cls:Thm.thm list)), symtable)=
quigley@15919
    31
                        symtable := Symtab.update((name , cls), !symtable);
quigley@15919
    32
      	       
quigley@15919
    33
quigley@15919
    34
fun memo_add_all ([], symtable) = ()
quigley@15919
    35
|   memo_add_all ((x::xs),symtable) = let val _ = memo_add_clauses (x, symtable)
quigley@15919
    36
                           in
quigley@15919
    37
                               memo_add_all (xs, symtable)
quigley@15919
    38
                           end
quigley@15919
    39
quigley@15919
    40
fun memo_find_clause (name, (symtable: Thm.thm list Symtab.table ref)) = case Symtab.lookup (!symtable,name) of
quigley@15919
    41
      	        NONE =>  []
quigley@15919
    42
                |SOME cls  => cls ;
quigley@15919
    43
      	        	
quigley@15919
    44
quigley@15919
    45
fun retrieve_clause array symtable clausenum = let
quigley@15919
    46
                                                  val (name, clnum) = Array.sub(array, clausenum)
quigley@15919
    47
                                                  val clauses = memo_find_clause (name, symtable)
quigley@15919
    48
                                                  val clause = get_nth clnum clauses
quigley@15919
    49
                                               in
quigley@15919
    50
                                                  (name, clause)
quigley@15919
    51
                                               end
quigley@15919
    52
 *)                                             
quigley@15919
    53
quigley@15642
    54
(* Versions that include type information *)
quigley@15642
    55
 
quigley@15642
    56
fun string_of_thm thm = let val _ = set show_sorts
paulson@15787
    57
                                val str = Display.string_of_thm thm
quigley@15642
    58
                                val no_returns =List.filter not_newline (explode str)
quigley@15642
    59
                                val _ = reset show_sorts
quigley@15642
    60
                            in 
quigley@15642
    61
                                implode no_returns
quigley@15642
    62
                            end
quigley@15642
    63
quigley@15642
    64
quigley@15642
    65
(* check separate args in the watcher program for separating strings with a * or ; or something *)
quigley@15642
    66
quigley@15642
    67
fun clause_strs_to_string [] str = str
quigley@15642
    68
|   clause_strs_to_string (x::xs) str = clause_strs_to_string xs (str^x^"%")
quigley@15642
    69
quigley@15642
    70
quigley@15642
    71
quigley@15642
    72
fun thmvars_to_string [] str = str
quigley@15642
    73
|   thmvars_to_string (x::xs) str = thmvars_to_string xs (str^x^"%")
quigley@15642
    74
quigley@15642
    75
quigley@15642
    76
fun proofstep_to_string Axiom = "Axiom()"
paulson@16091
    77
|   proofstep_to_string  (Binary ((a,b), (c,d)))=
paulson@16091
    78
      "Binary(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"
paulson@16091
    79
|   proofstep_to_string (Factor (a,b,c)) =
paulson@16091
    80
      "Factor("^(string_of_int a)^","^(string_of_int b)^","^(string_of_int c)^")"
paulson@16091
    81
|   proofstep_to_string  (Para ((a,b), (c,d)))= 
paulson@16091
    82
      "Para(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"
paulson@16091
    83
|   proofstep_to_string  (MRR ((a,b), (c,d))) =
paulson@16091
    84
      "MRR(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"
paulson@16091
    85
|   proofstep_to_string (Rewrite((a,b),(c,d))) =
paulson@16091
    86
      "Rewrite(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"
quigley@15642
    87
quigley@15642
    88
fun list_to_string [] liststr = liststr
quigley@15642
    89
|   list_to_string (x::[]) liststr = liststr^(string_of_int x)
quigley@15642
    90
|   list_to_string (x::xs) liststr = list_to_string xs (liststr^(string_of_int x)^",")
quigley@15642
    91
quigley@15642
    92
paulson@16091
    93
fun proof_to_string (num,(step,clause_strs, thmvars)) = (string_of_int num)^(proofstep_to_string step)^"["^(clause_strs_to_string clause_strs "")^"]["^(thmvars_to_string thmvars "")^"]"
quigley@15642
    94
 
quigley@15642
    95
quigley@15642
    96
fun proofs_to_string [] str = str
quigley@15642
    97
|   proofs_to_string (x::xs) str = let val newstr = proof_to_string x 
quigley@15642
    98
                                   in
quigley@15642
    99
                                       proofs_to_string xs (str^newstr)
quigley@15642
   100
                                   end
quigley@15642
   101
quigley@15642
   102
quigley@15642
   103
quigley@15642
   104
fun init_proofstep_to_string (num, step, clause_strs) = (string_of_int num)^" "^(proofstep_to_string step)^" "^(clause_strs_to_string clause_strs "")^" "
quigley@15642
   105
quigley@15642
   106
fun init_proofsteps_to_string [] str = str
quigley@15642
   107
|   init_proofsteps_to_string (x::xs) str = let val newstr = init_proofstep_to_string x 
quigley@15642
   108
                                   in
quigley@15642
   109
                                       init_proofsteps_to_string xs (str^newstr)
quigley@15642
   110
                                   end
quigley@15642
   111
  
quigley@15642
   112
quigley@15642
   113
quigley@15642
   114
(*** get a string representing the Isabelle ordered axioms ***)
quigley@15642
   115
paulson@16061
   116
fun origAx_to_string (num,(meta,thmvars)) =
paulson@16061
   117
    let val clause_strs = ReconOrderClauses.get_meta_lits_bracket meta
paulson@16061
   118
    in
paulson@16091
   119
       (string_of_int num)^"OrigAxiom()["^
paulson@16091
   120
       (clause_strs_to_string clause_strs "")^"]["^
paulson@16061
   121
       (thmvars_to_string thmvars "")^"]"
paulson@16061
   122
    end
quigley@15642
   123
quigley@15642
   124
quigley@15642
   125
fun  origAxs_to_string [] str = str
quigley@15642
   126
|   origAxs_to_string (x::xs) str = let val newstr = origAx_to_string x 
quigley@15642
   127
                                   in
quigley@15642
   128
                                       origAxs_to_string xs (str^newstr)
quigley@15642
   129
                                   end
quigley@15642
   130
quigley@15642
   131
quigley@15642
   132
(*** get a string representing the Isabelle ordered axioms not used in the spass proof***)
quigley@15642
   133
paulson@16061
   134
fun extraAx_to_string (num, (meta,thmvars)) =
paulson@16061
   135
   let val clause_strs = ReconOrderClauses.get_meta_lits_bracket meta
paulson@16061
   136
   in
paulson@16091
   137
      (string_of_int num)^"ExtraAxiom()["^
paulson@16061
   138
      (clause_strs_to_string clause_strs "")^"]"^
paulson@16061
   139
      "["^(thmvars_to_string thmvars "")^"]"
paulson@16061
   140
   end;
quigley@15642
   141
paulson@16061
   142
fun extraAxs_to_string [] str = str
paulson@16061
   143
|   extraAxs_to_string (x::xs) str =
paulson@16061
   144
      let val newstr = extraAx_to_string x 
paulson@16061
   145
      in
paulson@16061
   146
	  extraAxs_to_string xs (str^newstr)
paulson@16061
   147
      end;
quigley@15642
   148
quigley@15642
   149
fun is_axiom ( num:int,Axiom, str) = true
quigley@15642
   150
|   is_axiom (num, _,_) = false
quigley@15642
   151
quigley@15642
   152
fun get_init_axioms xs = List.filter (is_axiom) ( xs)
quigley@15642
   153
quigley@15642
   154
fun get_step_nums [] nums = nums
quigley@15642
   155
|   get_step_nums (( num:int,Axiom, str)::xs) nums = get_step_nums xs (nums@[num])
quigley@15642
   156
paulson@15774
   157
exception Noassoc;
paulson@15774
   158
paulson@15774
   159
fun assoc_snd a [] = raise Noassoc
quigley@15642
   160
  | assoc_snd a ((x, y)::t) = if a = y then x else assoc_snd a t;
quigley@15642
   161
quigley@15642
   162
(* change to be something using check_order  instead of a = y --> returns true if ASSERTION not raised in checkorder, false otherwise *)
quigley@15642
   163
quigley@15642
   164
(*fun get_assoc_snds [] xs assocs= assocs
quigley@15642
   165
|   get_assoc_snds (x::xs) ys assocs = get_assoc_snds xs ys (assocs@[((assoc_snd x ys))])
quigley@15642
   166
*)
quigley@15642
   167
(*FIX - should this have vars in it? *)
paulson@16061
   168
fun there_out_of_order xs ys = (ReconOrderClauses.checkorder xs ys [] ([],[],[]); true) 
paulson@15774
   169
                               handle _ => false
quigley@15642
   170
paulson@15774
   171
fun assoc_out_of_order a [] = raise Noassoc
quigley@15642
   172
|   assoc_out_of_order a ((b,c)::t) = if there_out_of_order a c then b else assoc_out_of_order a t;
quigley@15642
   173
quigley@15642
   174
fun get_assoc_snds [] xs assocs= assocs
quigley@15642
   175
|   get_assoc_snds (x::xs) ys assocs = get_assoc_snds xs ys (assocs@[((assoc_out_of_order x ys))])
quigley@15642
   176
quigley@15642
   177
quigley@15642
   178
quigley@15642
   179
quigley@15642
   180
fun add_if_not_inlist [] xs newlist = newlist
quigley@15642
   181
|   add_if_not_inlist (y::ys) xs newlist = if (not (inlist y xs)) then 
quigley@15642
   182
                                      add_if_not_inlist ys xs (y::newlist)
quigley@15642
   183
                                        else add_if_not_inlist ys xs (newlist)
quigley@15642
   184
paulson@15700
   185
(*Flattens a list of list of strings to one string*)
paulson@15700
   186
fun onestr ls = String.concat (map String.concat ls);
quigley@15642
   187
quigley@15642
   188
fun thmstrings [] str = str
quigley@15642
   189
|   thmstrings (x::xs) str = thmstrings xs (str^(string_of_thm x))
quigley@15642
   190
quigley@15919
   191
fun is_clasimp_ax clasimp_num n = n <=clasimp_num 
quigley@15642
   192
quigley@15919
   193
quigley@15919
   194
quigley@15919
   195
fun retrieve_axioms clause_arr  [] = []
quigley@15919
   196
|   retrieve_axioms clause_arr  (x::xs) =  [Array.sub(clause_arr, x)]@
quigley@15919
   197
 						     (retrieve_axioms clause_arr  xs)
quigley@15919
   198
quigley@16156
   199
fun subone x = x - 1
quigley@16156
   200
quigley@16156
   201
fun numstr [] = ""
quigley@16156
   202
|   numstr (x::xs) = (string_of_int x)^"%"^(numstr xs)
quigley@16156
   203
quigley@15919
   204
quigley@15919
   205
(* retrieve the axioms that were obtained from the clasimpset *)
quigley@15919
   206
quigley@15919
   207
fun get_clasimp_cls clause_arr clasimp_num step_nums = 
quigley@16156
   208
                                let 
quigley@16156
   209
                                    val realnums = map subone step_nums
quigley@16156
   210
                                   
quigley@16156
   211
 				    
quigley@16156
   212
                                    val clasimp_nums = List.filter (is_clasimp_ax (clasimp_num - 1)) realnums
quigley@16156
   213
                                   val axnums = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "axnums")))     
quigley@16156
   214
                                   val _ = TextIO.output(axnums,(numstr clasimp_nums))
quigley@16156
   215
                                   val _ = TextIO.closeOut(axnums)
quigley@15919
   216
                                in
quigley@15919
   217
                                    retrieve_axioms clause_arr  clasimp_nums
quigley@15919
   218
                                end
quigley@15919
   219
quigley@16156
   220
fun get_cls []  = []
quigley@16156
   221
|   get_cls (x::xs) = ((#1 x)::(get_cls xs))
quigley@16156
   222
quigley@16156
   223
(* add array and table here, so can retrieve clasimp axioms *)
quigley@16156
   224
quigley@16156
   225
 fun get_axiom_names proof_steps thms (clause_arr:(ResClause.clause * Thm.thm) Array.array) num_of_clauses  =
quigley@16156
   226
                                         let 
quigley@16156
   227
                                           (* not sure why this is necessary again, but seems to be *)
quigley@16156
   228
					    val _=  (print_mode := (Library.gen_rems (op =) (! print_mode, ["xsymbols", "symbols"])))
quigley@16156
   229
                                            val axioms = get_init_axioms proof_steps
quigley@16156
   230
                                            val step_nums = get_step_nums axioms []
quigley@16156
   231
quigley@16156
   232
                                           (***********************************************)
quigley@16156
   233
                                           (* here need to add the clauses from clause_arr*)
quigley@16156
   234
                                           (***********************************************)
quigley@16156
   235
quigley@16156
   236
                                            val (clasimp_names_cls) = get_clasimp_cls clause_arr   num_of_clauses step_nums 
quigley@16156
   237
                                            val clauses=(get_cls clasimp_names_cls) 
quigley@16156
   238
                                            val (names_clsnums) = map ResClause.clause_info clauses
quigley@16156
   239
                                            val clasimp_names = map fst names_clsnums
quigley@16156
   240
quigley@16156
   241
                                            val  outfile = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "clasimp_names")))                                                                   val clasimp_namestr = concat clasimp_names                            
quigley@16156
   242
                                            val _ = TextIO.output (outfile,clasimp_namestr)
quigley@16156
   243
                                            val _ =  TextIO.closeOut outfile
quigley@16156
   244
					    val _= (print_mode := (["xsymbols", "symbols"] @ ! print_mode))
quigley@16156
   245
                                           
quigley@16156
   246
                                         in
quigley@16156
   247
                                            (clasimp_names)
quigley@16156
   248
                                         end
quigley@16156
   249
    
quigley@16156
   250
fun numclstr (vars, []) str = str
quigley@16156
   251
|   numclstr ( vars, ((num, thm)::rest)) str = let val newstr = str^(string_of_int num)^" "^(string_of_thm thm)^" "
quigley@16156
   252
in
quigley@16156
   253
numclstr  (vars,rest) newstr
quigley@16156
   254
end
quigley@15919
   255
quigley@15919
   256
quigley@16156
   257
fun addvars  c (a,b)  = (a,b,c)
quigley@15919
   258
quigley@15919
   259
quigley@15919
   260
 fun get_axioms_used proof_steps thms clause_arr num_of_clauses  =
paulson@16061
   261
     let 
paulson@16061
   262
	 (*val  outfile = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "foo_ax_thmstr")))                                                                      
paulson@16061
   263
	 val _ = TextIO.output (outfile, thmstring)
paulson@16061
   264
	 
paulson@16061
   265
	 val _ =  TextIO.closeOut outfile*)
paulson@16061
   266
	(* not sure why this is necessary again, but seems to be *)
quigley@15782
   267
paulson@16061
   268
	val _=  (print_mode := (Library.gen_rems (op =) (! print_mode, ["xsymbols", "symbols"])))
paulson@16061
   269
	val axioms = get_init_axioms proof_steps
paulson@16061
   270
	val step_nums = get_step_nums axioms []
quigley@15919
   271
paulson@16061
   272
       (***********************************************)
paulson@16061
   273
       (* here need to add the clauses from clause_arr*)
paulson@16061
   274
       (***********************************************)
quigley@15919
   275
paulson@16061
   276
       (* val clasimp_names_cls = get_clasimp_cls clause_arr   num_of_clauses step_nums   
paulson@16061
   277
	val clasimp_names = map #1 clasimp_names_cls
paulson@16061
   278
	val clasimp_cls = map #2 clasimp_names_cls
paulson@16061
   279
	val  outfile = TextIO.openAppend(File.sysify_path(File.tmp_path (Path.basic "clasimp_names")))                                                            val clasimp_namestr = concat clasimp_names                            
paulson@16061
   280
	 val _ = TextIO.output (outfile,clasimp_namestr)
paulson@16061
   281
	 
paulson@16061
   282
	 val _ =  TextIO.closeOut outfile
quigley@15919
   283
*)
quigley@15919
   284
paulson@16061
   285
	val clauses =(*(clasimp_cls)@*)( make_clauses thms)
paulson@16061
   286
	
paulson@16061
   287
	val vars = map thm_vars clauses
paulson@16061
   288
       
paulson@16061
   289
	val distvars = distinct (fold append vars [])
paulson@16061
   290
	val clause_terms = map prop_of clauses  
paulson@16061
   291
	val clause_frees = List.concat (map term_frees clause_terms)
quigley@15642
   292
paulson@16061
   293
	val frees = map lit_string_with_nums clause_frees;
quigley@15642
   294
paulson@16061
   295
	val distfrees = distinct frees
quigley@15642
   296
paulson@16061
   297
	val metas = map Meson.make_meta_clause clauses
paulson@16061
   298
	val ax_strs = map #3 axioms
quigley@15642
   299
paulson@16061
   300
	(* literals of -all- axioms, not just those used by spass *)
paulson@16061
   301
	val meta_strs = map ReconOrderClauses.get_meta_lits metas
paulson@16061
   302
       
paulson@16061
   303
	val metas_and_strs = ListPair.zip (metas,meta_strs)
paulson@16061
   304
	 val  outfile = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "foo_clauses")));                                                                       
paulson@16061
   305
	 val _ = TextIO.output (outfile, onestr ax_strs)
paulson@16061
   306
	 
paulson@16061
   307
	 val _ =  TextIO.closeOut outfile
paulson@16061
   308
	 val  outfile = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "foo_metastrs")));                                                                       
paulson@16061
   309
	 val _ = TextIO.output (outfile, onestr meta_strs)
paulson@16061
   310
	 val _ =  TextIO.closeOut outfile
quigley@15642
   311
paulson@16061
   312
	(* get list of axioms as thms with their variables *)
quigley@15642
   313
paulson@16061
   314
	val ax_metas = get_assoc_snds ax_strs metas_and_strs []
paulson@16061
   315
	val ax_vars = map thm_vars ax_metas
paulson@16061
   316
	val ax_with_vars = ListPair.zip (ax_metas,ax_vars)
quigley@15642
   317
paulson@16061
   318
	(* get list of extra axioms as thms with their variables *)
paulson@16061
   319
	val extra_metas = add_if_not_inlist metas ax_metas []
paulson@16061
   320
	val extra_vars = map thm_vars extra_metas
paulson@16061
   321
	val extra_with_vars = if (not (extra_metas = []) ) 
paulson@16061
   322
			      then
paulson@16061
   323
				     ListPair.zip (extra_metas,extra_vars)
paulson@16061
   324
			      else
paulson@16061
   325
				     []
quigley@15642
   326
paulson@16061
   327
       (* val _=  (print_mode := (Library.gen_rems (op =) (! print_mode, ["xsymbols", "symbols"])))
paulson@16061
   328
       val  outfile = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "foo_metas")))
quigley@15642
   329
paulson@16061
   330
       val _ = TextIO.output (outfile, ((thmstrings ax_metas "")))
paulson@16061
   331
       val _ =  TextIO.closeOut outfile
paulson@16061
   332
      val foo_metas =  File.sysify_path(File.tmp_path (Path.basic "foo_metas"))
paulson@16061
   333
      val foo_metas2 =   File.sysify_path(File.tmp_path (Path.basic "foo_metas2"))
paulson@16061
   334
	val execperl = Unix.execute("/usr/bin/perl", ["remchars.pl", "  <", foo_metas, "  >", foo_metas2])
paulson@16061
   335
     val infile = TextIO.openIn(File.sysify_path(File.tmp_path (Path.basic "foo_metas2")))
paulson@16061
   336
    val ax_metas_str = TextIO.inputLine (infile)
paulson@16061
   337
    val _ = TextIO.closeIn infile
paulson@16061
   338
       val _= (print_mode := (["xsymbols", "symbols"] @ ! print_mode))*)
paulson@16061
   339
       
paulson@16061
   340
     in
paulson@16061
   341
	(distfrees,distvars, extra_with_vars,ax_with_vars, (*clasimp_names*)(ListPair.zip (step_nums,ax_metas)))
paulson@16061
   342
     end
paulson@16061
   343
    
quigley@15642
   344
quigley@16156
   345
                                        
quigley@15642
   346
quigley@15642
   347
(*********************************************************************)
quigley@15642
   348
(* Pass in spass string of proof and string version of isabelle goal *)
quigley@15642
   349
(* Get out reconstruction steps as a string to be sent to Isabelle   *)
quigley@15642
   350
(*********************************************************************)
quigley@15642
   351
quigley@15642
   352
quigley@16156
   353
fun rules_to_string [] str = str
quigley@16156
   354
|   rules_to_string [x] str = str^x
quigley@16156
   355
|   rules_to_string (x::xs) str = rules_to_string xs (str^x^"   ")
quigley@16156
   356
                                  
quigley@15642
   357
quigley@16156
   358
val dummy_tac = PRIMITIVE(fn thm => thm );
quigley@15642
   359
quigley@15642
   360
quigley@16156
   361
fun spassString_to_lemmaString proofstr thmstring goalstring toParent ppid thms clause_arr  num_of_clauses  = 
quigley@16156
   362
           let val  outfile = TextIO.openAppend(File.sysify_path(File.tmp_path (Path.basic "thmstringfile")));     						  val _ = TextIO.output (outfile, (" thmstring is: "^thmstring^"proofstr is: "^proofstr^"goalstr is: "^goalstring^" num of clauses is: "^(string_of_int num_of_clauses)))
quigley@16156
   363
                                                  val _ =  TextIO.closeOut outfile
quigley@16156
   364
quigley@16156
   365
                                              (***********************************)
quigley@16156
   366
                                              (* parse spass proof into datatype *)
quigley@16156
   367
                                              (***********************************)
quigley@16156
   368
         
quigley@16156
   369
                                                  val tokens = #1(lex proofstr)
quigley@16156
   370
                                                  val proof_steps1 = parse tokens
quigley@16156
   371
                                                  val proof_steps = just_change_space proof_steps1
quigley@16156
   372
                                                  val  outfile = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "foo_parse")));                                                     val _ = TextIO.output (outfile, ("Did parsing on "^proofstr))
quigley@16156
   373
                                                  val _ =  TextIO.closeOut outfile
quigley@16156
   374
                                                
quigley@16156
   375
                                                  val  outfile = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "foo_thmstring_at_parse")));                                        val _ = TextIO.output (outfile, ("Parsing for thmstring: "^thmstring))
quigley@16156
   376
                                                  val _ =  TextIO.closeOut outfile
quigley@16156
   377
                                              (************************************)
quigley@16156
   378
                                              (* recreate original subgoal as thm *)
quigley@16156
   379
                                              (************************************)
quigley@16156
   380
                                                
quigley@16156
   381
                                                  (* get axioms as correctly numbered clauses w.r.t. the Spass proof *)
quigley@16156
   382
                                                  (* need to get prems_of thm, then get right one of the prems, relating to whichever*)
quigley@16156
   383
                                                  (* subgoal this is, and turn it into meta_clauses *)
quigley@16156
   384
                                                  (* should prob add array and table here, so that we can get axioms*)
quigley@16156
   385
                                                  (* produced from the clasimpset rather than the problem *)
quigley@16156
   386
quigley@16156
   387
                                                  val (axiom_names) = get_axiom_names proof_steps  thms clause_arr  num_of_clauses
quigley@16156
   388
                                                  val ax_str = "Rules from clasimpset used in proof found by SPASS: "^(rules_to_string axiom_names "")
quigley@16156
   389
                                                  val  outfile = TextIO.openAppend(File.sysify_path(File.tmp_path (Path.basic "reconstrfile")));                                                     val _ = TextIO.output (outfile, ("reconstring is: "^ax_str^"  "^goalstring))
quigley@16156
   390
                                                  val _ =  TextIO.closeOut outfile
quigley@16156
   391
                                                   
quigley@16156
   392
                                              in 
quigley@16156
   393
                                                   TextIO.output (toParent, ax_str^"\n");
quigley@16156
   394
                                                   TextIO.flushOut toParent;
quigley@16156
   395
                                        	   TextIO.output (toParent, "goalstring: "^goalstring^"\n");
quigley@16156
   396
                                         	   TextIO.flushOut toParent;
quigley@16156
   397
                                         	   TextIO.output (toParent, "thmstring: "^thmstring^"\n");
quigley@16156
   398
                                         	   TextIO.flushOut toParent;
quigley@16156
   399
                                          
quigley@16156
   400
                                         	   Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
quigley@16156
   401
                                         	  (* Attempt to prevent several signals from turning up simultaneously *)
quigley@16156
   402
                                         	   Posix.Process.sleep(Time.fromSeconds 1) ; dummy_tac
quigley@16156
   403
                                              end
quigley@16156
   404
                                              handle _ => (let val  outfile = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "foo_handler")));
quigley@16156
   405
quigley@16156
   406
                                                  val _ = TextIO.output (outfile, ("In exception handler"));
quigley@16156
   407
                                                  val _ =  TextIO.closeOut outfile
quigley@16156
   408
                                              in 
quigley@16156
   409
                                                  TextIO.output (toParent,"Proof found but translation failed!" ^"\n");
quigley@16156
   410
                                                  TextIO.flushOut toParent;
quigley@16156
   411
						   TextIO.output (toParent, thmstring^"\n");
quigley@16156
   412
                                         	   TextIO.flushOut toParent;
quigley@16156
   413
                                         	   TextIO.output (toParent, goalstring^"\n");
quigley@16156
   414
                                         	   TextIO.flushOut toParent;
quigley@16156
   415
            					  Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
quigley@16156
   416
                                         	  (* Attempt to prevent several signals from turning up simultaneously *)
quigley@16156
   417
                                         	  Posix.Process.sleep(Time.fromSeconds 1) ;dummy_tac
quigley@16156
   418
                                              end)
quigley@15642
   419
quigley@15642
   420
quigley@15642
   421
quigley@15919
   422
fun spassString_to_reconString proofstr thmstring goalstring toParent ppid thms clause_arr  num_of_clauses  = 
paulson@16061
   423
      let val  outfile = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "thmstringfile")));                                        
paulson@16061
   424
	  (* val sign = sign_of_thm thm
paulson@16061
   425
	 val prems = prems_of thm
paulson@16061
   426
	 val prems_string =  concat_with_and (map (Sign.string_of_term sign) prems) "" 
paulson@16061
   427
	  val _ = warning ("thmstring is: "^thmstring);(*("thm in spassStrtoRec: "^(concat_with_and (map string_of_thm thms) "")^*)*)
paulson@16061
   428
	  val _ = TextIO.output (outfile, (" thmstring is: "^thmstring^"proofstr is: "^proofstr))
paulson@16061
   429
(*val _ = TextIO.output (outfile, (("in spassStrto Reconstr")));*)
paulson@16061
   430
	  val _ =  TextIO.closeOut outfile
quigley@15782
   431
paulson@16061
   432
	  val tokens = #1(lex proofstr)
quigley@15782
   433
paulson@16061
   434
	    
quigley@15919
   435
paulson@16061
   436
      (***********************************)
paulson@16061
   437
      (* parse spass proof into datatype *)
paulson@16061
   438
      (***********************************)
quigley@15642
   439
paulson@16061
   440
	  val proof_steps1 = parse tokens
paulson@16061
   441
	  val proof_steps = just_change_space proof_steps1
quigley@15642
   442
paulson@16061
   443
	  val  outfile = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "foo_parse")));                                                                                          val _ = TextIO.output (outfile, ("Did parsing on "^proofstr))
paulson@16061
   444
	  val _ =  TextIO.closeOut outfile
paulson@16061
   445
	
paulson@16061
   446
	  val  outfile = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "foo_thmstring_at_parse")));                                                                             val _ = TextIO.output (outfile, ("Parsing for thmstring: "^thmstring))
paulson@16061
   447
	  val _ =  TextIO.closeOut outfile
paulson@16061
   448
      (************************************)
paulson@16061
   449
      (* recreate original subgoal as thm *)
paulson@16061
   450
      (************************************)
paulson@16061
   451
	
paulson@16061
   452
	  (* get axioms as correctly numbered clauses w.r.t. the Spass proof *)
paulson@16061
   453
	  (* need to get prems_of thm, then get right one of the prems, relating to whichever*)
paulson@16061
   454
	  (* subgoal this is, and turn it into meta_clauses *)
paulson@16061
   455
	  (* should prob add array and table here, so that we can get axioms*)
paulson@16061
   456
	  (* produced from the clasimpset rather than the problem *)
paulson@16061
   457
	  val (frees,vars,extra_with_vars ,ax_with_vars,numcls) = get_axioms_used proof_steps  thms clause_arr  num_of_clauses
paulson@16061
   458
	  
paulson@16061
   459
	  (*val numcls_string = numclstr ( vars, numcls) ""*)
paulson@16061
   460
	  val  outfile = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "foo_axiom")));                                                                            val _ = TextIO.output (outfile,"got axioms")
paulson@16061
   461
	  val _ =  TextIO.closeOut outfile
paulson@16061
   462
	    
paulson@16061
   463
      (************************************)
paulson@16061
   464
      (* translate proof                  *)
paulson@16061
   465
      (************************************)
paulson@16061
   466
	  val  outfile = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "foo_steps")));                                                          val _ = TextIO.output (outfile, ("about to translate proof, steps: "^(init_proofsteps_to_string proof_steps "")))
paulson@16061
   467
	  val _ =  TextIO.closeOut outfile
paulson@16061
   468
	  val (newthm,proof) = translate_proof numcls  proof_steps vars
paulson@16061
   469
	  val  outfile = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "foo_steps2")));                                                                         val _ = TextIO.output (outfile, ("translated proof, steps: "^(init_proofsteps_to_string proof_steps "")))
paulson@16061
   470
	  val _ =  TextIO.closeOut outfile
paulson@16061
   471
      (***************************************************)
paulson@16061
   472
      (* transfer necessary steps as strings to Isabelle *)
paulson@16061
   473
      (***************************************************)
paulson@16061
   474
	  (* turn the proof into a string *)
paulson@16061
   475
	  val reconProofStr = proofs_to_string proof ""
paulson@16061
   476
	  (* do the bit for the Isabelle ordered axioms at the top *)
paulson@16061
   477
	  val ax_nums = map #1 numcls
paulson@16061
   478
	  val ax_strs = map ReconOrderClauses.get_meta_lits_bracket (map #2 numcls)
paulson@16061
   479
	  val numcls_strs = ListPair.zip (ax_nums,ax_strs)
paulson@16061
   480
	  val num_cls_vars =  map (addvars vars) numcls_strs;
paulson@16061
   481
	  val reconIsaAxStr = origAxs_to_string (ListPair.zip (ax_nums,ax_with_vars)) ""
paulson@16061
   482
	  
paulson@16061
   483
	  val extra_nums = if (not (extra_with_vars = [])) then (1 upto (length extra_with_vars)) else []
paulson@16061
   484
	  val reconExtraAxStr = extraAxs_to_string ( ListPair.zip (extra_nums,extra_with_vars)) ""
paulson@16061
   485
	  val frees_str = "["^(thmvars_to_string frees "")^"]"
paulson@16061
   486
	  val  outfile = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "reconstringfile")));
quigley@15642
   487
paulson@16061
   488
	  val _ = TextIO.output (outfile, (frees_str^reconExtraAxStr^reconIsaAxStr^reconProofStr))
paulson@16061
   489
	  val _ =  TextIO.closeOut outfile
paulson@16061
   490
	  val reconstr = (frees_str^reconExtraAxStr^reconIsaAxStr^reconProofStr)
paulson@16061
   491
      in 
paulson@16061
   492
	   TextIO.output (toParent, reconstr^"\n");
paulson@16061
   493
	   TextIO.flushOut toParent;
paulson@16061
   494
	   TextIO.output (toParent, thmstring^"\n");
paulson@16061
   495
	   TextIO.flushOut toParent;
paulson@16061
   496
	   TextIO.output (toParent, goalstring^"\n");
paulson@16061
   497
	   TextIO.flushOut toParent;
paulson@16061
   498
  
paulson@16061
   499
	   Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
paulson@16061
   500
	  (* Attempt to prevent several signals from turning up simultaneously *)
paulson@16061
   501
	   Posix.Process.sleep(Time.fromSeconds 1) ; dummy_tac
paulson@16061
   502
      end
paulson@16061
   503
      handle _ => (let val  outfile = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "foo_handler")));
quigley@15642
   504
paulson@16061
   505
	  val _ = TextIO.output (outfile, ("In exception handler"));
paulson@16061
   506
	  val _ =  TextIO.closeOut outfile
paulson@16061
   507
      in 
paulson@16061
   508
	  TextIO.output (toParent,"Proof found but translation failed!" ^"\n");
paulson@16061
   509
	  TextIO.flushOut toParent;
paulson@16061
   510
	TextIO.output (toParent, thmstring^"\n");
paulson@16061
   511
	   TextIO.flushOut toParent;
paulson@16061
   512
	   TextIO.output (toParent, goalstring^"\n");
paulson@16061
   513
	   TextIO.flushOut toParent;
paulson@16061
   514
	  Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
paulson@16061
   515
	  (* Attempt to prevent several signals from turning up simultaneously *)
paulson@16061
   516
	  Posix.Process.sleep(Time.fromSeconds 1) ;dummy_tac
paulson@16061
   517
      end)
quigley@15642
   518
quigley@15642
   519
quigley@15642
   520
quigley@15642
   521
quigley@15642
   522
quigley@15642
   523
(**********************************************************************************)
quigley@15642
   524
(* At other end, want to turn back into datatype so can apply reconstruct_proof.  *)
quigley@15642
   525
(* This will be done by the signal handler                                        *)
quigley@15642
   526
(**********************************************************************************)
quigley@15642
   527
quigley@15642
   528
(* Parse in the string version of the proof steps for reconstruction *)
quigley@15642
   529
(* Isar format: cl1 [BINARY 0 cl2 0];cl1 [PARAMOD 0 cl2 0]; cl1 [DEMOD 0 cl2];cl1 [FACTOR 1 2];*)
quigley@15642
   530
quigley@15642
   531
quigley@15642
   532
 val term_numstep =
quigley@15642
   533
        (number ++ (a (Other ",")) ++ number) >> (fn (a, (_, c)) => (a, c))
quigley@15642
   534
quigley@15642
   535
val extraaxiomstep = (a (Word "ExtraAxiom"))++ (a (Other "(")) ++(a (Other ")"))
quigley@15642
   536
            >> (fn (_) => ExtraAxiom)
quigley@15642
   537
quigley@15642
   538
quigley@15642
   539
quigley@15642
   540
val origaxiomstep = (a (Word "OrigAxiom"))++ (a (Other "(")) ++(a (Other ")"))
quigley@15642
   541
            >> (fn (_) => OrigAxiom)
quigley@15642
   542
quigley@15642
   543
quigley@15642
   544
 val axiomstep = (a (Word "Axiom"))++ (a (Other "(")) ++(a (Other ")"))
quigley@15642
   545
            >> (fn (_) => Axiom)
quigley@15642
   546
     
quigley@15642
   547
quigley@15642
   548
quigley@15642
   549
      
quigley@15642
   550
 val binarystep = (a (Word "Binary")) ++ (a (Other "(")) ++ (a (Other "(")) 
quigley@15642
   551
                   ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
quigley@15642
   552
                   ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
quigley@15642
   553
            >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => Binary (c,e))
quigley@15642
   554
      
quigley@15642
   555
quigley@15642
   556
 val parastep = (a (Word "Para")) ++ (a (Other "(")) ++ (a (Other "(")) 
quigley@15642
   557
                   ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
quigley@15642
   558
                   ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
quigley@15642
   559
            >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => Para(c, e))
quigley@15642
   560
      
quigley@15642
   561
 val mrrstep = (a (Word "MRR")) ++ (a (Other "(")) ++ (a (Other "(")) 
quigley@15642
   562
                   ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
quigley@15642
   563
                   ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
quigley@15642
   564
            >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => MRR(c, e))
quigley@15642
   565
      
quigley@15642
   566
quigley@15642
   567
 val factorstep = (a (Word "Factor")) ++ (a (Other "("))
quigley@15642
   568
                    ++ number ++ (a (Other ","))
quigley@15642
   569
                       ++ number ++ (a (Other ","))
quigley@15642
   570
                       ++ number ++  (a (Other ")"))
quigley@15642
   571
                   
quigley@15642
   572
            >> (fn (_, (_, (c, (_, (e,(_,(f,_))))))) =>  Factor (c,e,f))
quigley@15642
   573
quigley@15642
   574
quigley@15642
   575
val rewritestep = (a (Word "Rewrite"))  ++ (a (Other "(")) ++ (a (Other "(")) 
quigley@15642
   576
                   ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
quigley@15642
   577
                   ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
quigley@15642
   578
            >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => Rewrite (c,e))
quigley@15642
   579
quigley@15642
   580
val obviousstep = (a (Word "Obvious")) ++ (a (Other "(")) 
quigley@15642
   581
                   ++ term_numstep  ++ (a (Other ")")) 
quigley@15642
   582
            >> (fn (_, (_, (c,_))) => Obvious (c))
quigley@15642
   583
quigley@15642
   584
 val methodstep = extraaxiomstep || origaxiomstep || axiomstep ||binarystep || factorstep|| parastep || mrrstep || rewritestep || obviousstep
quigley@15642
   585
quigley@15642
   586
quigley@15642
   587
 val number_list_step =
paulson@15739
   588
        ( number ++ many ((a (Other ",") ++ number)>> #2))
quigley@15642
   589
        >> (fn (a,b) => (a::b))
quigley@15642
   590
        
quigley@15642
   591
 val numberlist_step = a (Other "[")  ++ a (Other "]")
quigley@15642
   592
                        >>(fn (_,_) => ([]:int list))
quigley@15642
   593
                       || a (Other "[") ++ number_list_step ++ a (Other "]")
quigley@15642
   594
                        >>(fn (_,(a,_)) => a)
quigley@15642
   595
                    
quigley@15642
   596
quigley@15642
   597
quigley@15642
   598
(** change this to allow P (x U) *)
quigley@15642
   599
 fun arglist_step input = ( word ++ many  word >> (fn (a, b) => (a^" "^(implode_with_space b)))
quigley@15642
   600
                           ||word >> (fn (a) => (a)))input
quigley@15642
   601
                
quigley@15642
   602
quigley@15642
   603
fun literal_step input = (word ++ a (Other "(") ++ arglist_step ++  a (Other ")")
quigley@15642
   604
                                          >>(fn (a, (b, (c,d))) => (a^" ("^(c)^")"))
quigley@15642
   605
                        || arglist_step >> (fn (a) => (a)))input
quigley@15642
   606
                           
quigley@15642
   607
quigley@15642
   608
quigley@15642
   609
(* fun term_step input = (a (Other "~") ++ arglist_step ++ a (Other "%")>> (fn (a,(b,c)) => ("~ "^b))
quigley@15642
   610
                     ||  arglist_step ++ a (Other "%")>> (fn (a,b) => a ))input
quigley@15642
   611
*)
quigley@15642
   612
quigley@15642
   613
quigley@15642
   614
 fun term_step input = (a (Other "~") ++ literal_step ++ a (Other "%")>> (fn (a,(b,c)) => ("~ "^b))
quigley@15642
   615
                     ||  literal_step ++ a (Other "%")>> (fn (a,b) => a ))input
quigley@15642
   616
quigley@15642
   617
quigley@15642
   618
         
quigley@15642
   619
quigley@15642
   620
 val term_list_step =
quigley@15642
   621
        (  term_step ++ many ( term_step))
quigley@15642
   622
        >> (fn (a,b) => (a::b))
quigley@15642
   623
        
quigley@15642
   624
 
quigley@15642
   625
val term_lists_step = a (Other "[")  ++ a (Other "]")
quigley@15642
   626
                        >>(fn (_,_) => ([]:string list))
quigley@15642
   627
                       || a (Other "[") ++ term_list_step ++ a (Other "]")
quigley@15642
   628
                        >>(fn (_,(a,_)) => a)
quigley@15642
   629
                     
quigley@15642
   630
quigley@15642
   631
quigley@15642
   632
quigley@15642
   633
fun anytoken_step input  = (word>> (fn (a) => a)  ) input
quigley@15642
   634
                       handle NOPARSE_WORD => (number>> (fn (a) => string_of_int a)  ) input
quigley@15642
   635
                      handle NOPARSE_NUMBER => (other_char >> (fn(a) => a)) input
quigley@15642
   636
quigley@15642
   637
quigley@15642
   638
quigley@15642
   639
fun goalstring_step input= (anytoken_step ++ many (anytoken_step )
quigley@15642
   640
                  >> (fn (a,b) =>  (a^" "^(implode b)))) input
quigley@15642
   641
quigley@15642
   642
quigley@15642
   643
quigley@15642
   644
 val linestep = number ++ methodstep ++ term_lists_step ++ term_lists_step
quigley@15642
   645
                >> (fn (a, (b, (c,d))) => (a,(b,c,d)))
quigley@15642
   646
    
quigley@15642
   647
 val lines_step = many linestep
quigley@15642
   648
paulson@15739
   649
 val alllines_step = (term_lists_step ++ lines_step ) ++ finished >> #1
quigley@15642
   650
    
paulson@15739
   651
 val parse_step = #1 o alllines_step
quigley@15642
   652
quigley@15642
   653
quigley@15642
   654
 (*
quigley@15642
   655
val reconstr ="[P%x%xa%xb%]1OrigAxiom()[P x%~ P U%][U%]3OrigAxiom()[P U%~ P x%][U%]5OrigAxiom()[~ P xa%~ P U%][U%]7OrigAxiom()[P U%P xb%][U%]1Axiom()[P x%~ P U%][U%]3Axiom()[P U%~ P x%][U%]5Axiom()[~ P U%~ P xa%][U%]7Axiom()[P U%P xb%][U%]9Factor(5,0,1)[~ P xa%][]10Binary((9,0),(3,0))[~ P x%][]11Binary((10,0),(1,0))[~ P U%][U%]12Factor(7,0,1)[P xb%][]14Binary((11,0),(12,0))[][]%(EX x::'a::type. ALL y::'a::type. (P::'a::type => bool) x = P y) -->(EX x::'a::type. P x) = (ALL y::'a::type. P y)"
quigley@15642
   656
*)
quigley@15642
   657
quigley@15642
   658
(************************************************************)
quigley@15642
   659
(* Construct an Isar style proof from a list of proof steps *)
quigley@15642
   660
(************************************************************)
quigley@15642
   661
(* want to assume all axioms, then do haves for the other clauses*)
quigley@15642
   662
(* then show for the last step *)
quigley@15642
   663
quigley@15642
   664
(* replace ~ by not here *)
quigley@15642
   665
fun change_nots [] = []
quigley@15642
   666
|   change_nots (x::xs) = if x = "~" 
quigley@15642
   667
                          then 
quigley@15642
   668
                             ["\\", "<", "n", "o", "t", ">"]@(change_nots xs)
quigley@15642
   669
                          else (x::(change_nots xs))
quigley@15642
   670
quigley@15642
   671
(*
quigley@15642
   672
fun clstrs_to_string [] str = str
quigley@15642
   673
|   clstrs_to_string (x::[]) str = str^x
quigley@15642
   674
|   clstrs_to_string (x::xs) str = clstrs_to_string xs (str^(x^"; "))
quigley@15642
   675
*)
quigley@15642
   676
fun clstrs_to_string [] str = implode (change_nots (explode str))
quigley@15642
   677
|   clstrs_to_string (x::[]) str = implode (change_nots (explode (str^x)))
quigley@15642
   678
|   clstrs_to_string (x::xs) str = implode (change_nots (explode (clstrs_to_string xs (str^(x^"; ")))))
quigley@15642
   679
quigley@15642
   680
quigley@15642
   681
quigley@15642
   682
fun thmvars_to_quantstring [] str = str
quigley@15642
   683
|   thmvars_to_quantstring (x::[]) str =str^x^". "
quigley@15642
   684
|   thmvars_to_quantstring (x::xs) str = thmvars_to_quantstring xs (str^(x^" "))
quigley@15642
   685
quigley@15642
   686
paulson@16091
   687
fun clause_strs_to_isar clstrs [] =  "\"\\<lbrakk>"^(clstrs_to_string clstrs "")^"\\<rbrakk> \\<Longrightarrow> False\""
paulson@16091
   688
|   clause_strs_to_isar clstrs thmvars = "\"\\<And>"^(thmvars_to_quantstring thmvars "")^"\\<lbrakk>"^(clstrs_to_string clstrs "")^"\\<rbrakk> \\<Longrightarrow> False\""
quigley@15642
   689
quigley@15642
   690
fun frees_to_string [] str = implode (change_nots (explode str))
quigley@15642
   691
|   frees_to_string (x::[]) str = implode (change_nots (explode (str^x)))
quigley@15642
   692
|   frees_to_string  (x::xs) str = implode (change_nots (explode (frees_to_string xs (str^(x^" ")))))
quigley@15642
   693
quigley@15642
   694
fun frees_to_isar_str [] =  ""
quigley@15642
   695
|   frees_to_isar_str  clstrs = (frees_to_string clstrs "")
quigley@15642
   696
quigley@15642
   697
quigley@15642
   698
(***********************************************************************)
quigley@15642
   699
(* functions for producing assumptions for the Isabelle ordered axioms *)
quigley@15642
   700
(***********************************************************************)
quigley@15642
   701
(*val str = "[P%x%xa%xb%]1OrigAxiom()[P x%~ P U%][U%]3OrigAxiom()[P U%~ P x%][U%]5OrigAxiom()[~ P xa%~ P U%][U%]7OrigAxiom()[P U%P xb%][U%]1Axiom()[P x%~ P U%][U%]3Axiom()[P U%~ P x%][U%]5Axiom()[~ P U%~ P xa%][U%]7Axiom()[P U%P xb%][U%]9Factor(5,0,1)[~ P xa%][]10Binary((9,0),(3,0))[~ P x%][]11Binary((10,0),(1,0))[~ P U%][U%]12Factor(7,0,1)[P xb%][]14Binary((11,0),(12,0))[][]";       
quigley@15642
   702
num, rule, clausestrs, vars*)
quigley@15642
   703
quigley@15642
   704
quigley@15642
   705
(* assume the extra clauses - not used in Spass proof *)
quigley@15642
   706
quigley@15642
   707
fun is_extraaxiom_step ( num:int,(ExtraAxiom, str, tstr)) = true
quigley@15642
   708
|   is_extraaxiom_step (num, _) = false
quigley@15642
   709
quigley@15642
   710
fun get_extraaxioms xs = List.filter (is_extraaxiom_step) ( xs)
quigley@15642
   711
quigley@15642
   712
fun assume_isar_extraaxiom [] str  = str
quigley@15642
   713
|   assume_isar_extraaxiom ((numb,(step, clstr, thmvars))::xs) str  = assume_isar_extraaxiom xs (str^"and cl"^(string_of_int numb)^"': "^(clause_strs_to_isar clstr thmvars)^"\n " )
quigley@15642
   714
quigley@15642
   715
quigley@15642
   716
quigley@15642
   717
fun assume_isar_extraaxioms  [] = ""
quigley@15642
   718
|assume_isar_extraaxioms ((numb,(step, clstrs, thmstrs))::xs) = let val str = "assume cl"^(string_of_int numb)^"': "^(clause_strs_to_isar clstrs thmstrs)^"\n" 
quigley@15642
   719
                                         in
quigley@15642
   720
                                             assume_isar_extraaxiom xs str
quigley@15642
   721
                                         end
quigley@15642
   722
quigley@15642
   723
(* assume the Isabelle ordered clauses *)
quigley@15642
   724
quigley@15642
   725
fun is_origaxiom_step ( num:int,(OrigAxiom, str, tstr)) = true
quigley@15642
   726
|   is_origaxiom_step (num, _) = false
quigley@15642
   727
quigley@15642
   728
fun get_origaxioms xs = List.filter (is_origaxiom_step) ( xs)
quigley@15642
   729
quigley@15642
   730
fun assume_isar_origaxiom [] str  = str
quigley@15642
   731
|   assume_isar_origaxiom ((numb,(step, clstr, thmvars))::xs) str  = assume_isar_origaxiom xs (str^"and cl"^(string_of_int numb)^"': "^(clause_strs_to_isar clstr thmvars)^"\n " )
quigley@15642
   732
quigley@15642
   733
quigley@15642
   734
quigley@15642
   735
fun assume_isar_origaxioms ((numb,(step, clstrs, thmstrs))::xs) = let val str = "assume cl"^(string_of_int numb)^"': "^(clause_strs_to_isar clstrs thmstrs)^"\n" 
quigley@15642
   736
                                         in
quigley@15642
   737
                                             assume_isar_origaxiom xs str
quigley@15642
   738
                                         end
quigley@15642
   739
quigley@15642
   740
quigley@15642
   741
quigley@15642
   742
fun is_axiom_step ( num:int,(Axiom, str, tstr)) = true
quigley@15642
   743
|   is_axiom_step (num, _) = false
quigley@15642
   744
quigley@15642
   745
fun get_axioms xs = List.filter  (is_axiom_step) ( xs)
quigley@15642
   746
quigley@15642
   747
fun have_isar_axiomline (numb,(step, clstrs, thmstrs))="have cl"^(string_of_int numb)^": "^(clause_strs_to_isar clstrs thmstrs)^"\n"
quigley@15642
   748
quigley@15642
   749
fun  by_isar_axiomline (numb,(step, clstrs, thmstrs))="by (rule cl"^ (string_of_int numb)^"') \n"
quigley@15642
   750
quigley@15642
   751
quigley@15642
   752
fun isar_axiomline (numb, (step, clstrs, thmstrs))  = (have_isar_axiomline (numb,(step,clstrs, thmstrs )))^( by_isar_axiomline(numb,(step,clstrs, thmstrs )) )
quigley@15642
   753
quigley@15642
   754
quigley@15642
   755
fun isar_axiomlines [] str = str
quigley@15642
   756
|   isar_axiomlines (x::xs) str = isar_axiomlines xs (str^(isar_axiomline x))
quigley@15642
   757
quigley@15642
   758
quigley@15642
   759
fun have_isar_line (numb,(step, clstrs, thmstrs))="have cl"^(string_of_int numb)^": "^(clause_strs_to_isar clstrs thmstrs)^"\n"
quigley@15642
   760
quigley@15642
   761
paulson@16091
   762
fun by_isar_line ((Binary ((a,b), (c,d)))) = 
paulson@16091
   763
    "by(rule cl"^
paulson@16091
   764
		(string_of_int a)^" [binary "^(string_of_int b)^" cl"^
paulson@16091
   765
		(string_of_int c)^" "^(string_of_int d)^"])\n"
paulson@16091
   766
|   by_isar_line ( (Para ((a,b), (c,d)))) =
paulson@16091
   767
    "by (rule cl"^
paulson@16091
   768
		(string_of_int a)^" [paramod "^(string_of_int b)^" cl"^
paulson@16091
   769
		(string_of_int c)^" "^(string_of_int d)^"])\n"
paulson@16091
   770
|   by_isar_line ((Factor ((a,b,c)))) = 
paulson@16091
   771
    "by (rule cl"^(string_of_int a)^" [factor "^(string_of_int b)^" "^
paulson@16091
   772
		(string_of_int c)^" ])\n"
paulson@16091
   773
|   by_isar_line ( (Rewrite ((a,b),(c,d)))) =
paulson@16091
   774
    "by (rule cl"^(string_of_int a)^" [demod "^(string_of_int b)^" "^
paulson@16091
   775
		(string_of_int c)^" "^(string_of_int d)^" ])\n"
paulson@16091
   776
|   by_isar_line ( (Obvious ((a,b)))) =
paulson@16091
   777
    "by (rule cl"^(string_of_int a)^" [obvious "^(string_of_int b)^" ])\n"
quigley@15642
   778
quigley@15642
   779
fun isar_line (numb, (step, clstrs, thmstrs))  = (have_isar_line (numb,(step,clstrs, thmstrs )))^( by_isar_line step)
quigley@15642
   780
quigley@15642
   781
quigley@15642
   782
fun isar_lines [] str = str
quigley@15642
   783
|   isar_lines (x::xs) str = isar_lines xs (str^(isar_line x))
quigley@15642
   784
quigley@15642
   785
fun last_isar_line (numb,( step, clstrs,thmstrs)) = "show \"False\"\n"^(by_isar_line step)
quigley@15642
   786
quigley@15642
   787
paulson@16091
   788
fun to_isar_proof (frees, xs, goalstring) =
paulson@16091
   789
    let val extraaxioms = get_extraaxioms xs
paulson@16091
   790
	val extraax_num = length extraaxioms
paulson@16091
   791
	val origaxioms_and_steps = Library.drop (extraax_num, xs)  
paulson@16091
   792
	
paulson@16091
   793
	val origaxioms = get_origaxioms origaxioms_and_steps
paulson@16091
   794
	val origax_num = length origaxioms
paulson@16091
   795
	val axioms_and_steps = Library.drop (origax_num + extraax_num, xs)  
paulson@16091
   796
	val axioms = get_axioms axioms_and_steps
paulson@16091
   797
	
paulson@16091
   798
	val steps = Library.drop (origax_num, axioms_and_steps)
paulson@16091
   799
	val firststeps = ReconOrderClauses.butlast steps
paulson@16091
   800
	val laststep = ReconOrderClauses.last steps
paulson@16091
   801
	val goalstring = implode(ReconOrderClauses.butlast(explode goalstring))
paulson@16091
   802
	
paulson@16091
   803
	val isar_proof = 
paulson@16091
   804
		("show \""^goalstring^"\"\n")^
paulson@16091
   805
		("proof (rule ccontr,skolemize, make_clauses) \n")^
paulson@16091
   806
		("fix "^(frees_to_isar_str frees)^"\n")^
paulson@16091
   807
		(assume_isar_extraaxioms extraaxioms)^
paulson@16091
   808
		(assume_isar_origaxioms origaxioms)^
paulson@16091
   809
		(isar_axiomlines axioms "")^
paulson@16091
   810
		(isar_lines firststeps "")^
paulson@16091
   811
		(last_isar_line laststep)^
paulson@16091
   812
		("qed")
paulson@16091
   813
	val  outfile = TextIO.openOut(File.sysify_path(File.tmp_path (Path.basic "isar_proof_file")));
paulson@16091
   814
	
paulson@16091
   815
	val _ = TextIO.output (outfile, isar_proof)
paulson@16091
   816
	val _ =  TextIO.closeOut outfile
paulson@16091
   817
    in
paulson@16091
   818
	isar_proof
paulson@16091
   819
    end;
quigley@15642
   820
quigley@15642
   821
(* get fix vars from axioms - all Frees *)
quigley@15642
   822
(* check each clause for meta-vars and /\ over them at each step*)
quigley@15642
   823
quigley@15642
   824
(*******************************************************)
quigley@15642
   825
(* This assumes the thm list  "numcls" is still there  *)
quigley@15642
   826
(* In reality, should probably label it with an        *)
quigley@15642
   827
(* ID number identifying the subgoal.  This could      *)
quigley@15642
   828
(* be passed over to the watcher, e.g.  numcls25       *)
quigley@15642
   829
(*******************************************************)
quigley@15642
   830
quigley@15642
   831
(* val str = "[S%x%P%R%Q%]1ExtraAxiom()[~ Q U%~ R U%][U%]2ExtraAxiom()[~ Q U%~ P U%][U%]3ExtraAxiom()[Q U%R U%][U%]1OrigAxiom()[S x%][]2OrigAxiom()[P U%R U%][U%]6OrigAxiom()[~ S U%~ P U%][U%]7OrigAxiom()[~ S U%~ R U%][U%]1Axiom()[S x%][]2Axiom()[R U%P U%][U%]6Axiom()[~ P U%~ S U%][U%]7Axiom()[~ R U%~ S U%][U%]8Binary((6,1),(1,0))[~ P x%][]9Binary((7,1),(1,0))[~ R x%][]19Binary((9,0),(2,0))[P x%][]25Binary((8,0),(19,0))[][]";
quigley@15642
   832
quigley@15642
   833
val str = "[P%x%xa%xb%]1OrigAxiom()[P x%~ P U%][U%]3OrigAxiom()[P U%~ P x%][U%]5OrigAxiom()[~ P xa%~ P U%][U%]7OrigAxiom()[P U%P xb%][U%]1Axiom()[P x%~ P U%][U%]3Axiom()[P U%~ P x%][U%]5Axiom()[~ P U%~ P xa%][U%]7Axiom()[P U%P xb%][U%]9Factor(5,0,1)[~ P xa%][]10Binary((9,0),(3,0))[~ P x%][]11Binary((10,0),(1,0))[~ P U%][U%]12Factor(7,0,1)[P xb%][]14Binary((11,0),(12,0))[][]";
quigley@15642
   834
quigley@15642
   835
val reconstr = "[P%Q%x%xa%]1OrigAxiom()[~ P U%][U%]3OrigAxiom()[Q U%][U%]5OrigAxiom()[P (x U)%~ Q (xa U)%][U%]9Binary((7,0),(3,0))[][]7Binary((1,0),(5,0))[~ Q (xa U)%][U%]5Axiom()[P (x U)%~ Q (xa U)%][U%]3Axiom()[Q U%][U%]1Axiom()[~ P U%][U%](ALL xb::'a::type.   (~ (P::'a::type => bool) ((x::'a::type => 'a::type) xb) |    (Q::'a::type => bool) ((xa::'a::type => 'a::type) xb)) &   P xb & ~ Q xb)";
quigley@15642
   836
quigley@15642
   837
quigley@15642
   838
val reconstr = "[P%x%xa%xb%]1OrigAxiom()[P x%~ P U%][U%]3OrigAxiom()[P U%~ P x%][U%]5OrigAxiom()[~ P xa%~ P U%][U%]7OrigAxiom()[P U%P xb%][U%]1Axiom()[P x%~ P U%][U%]3Axiom()[P U%~ P x%][U%]5Axiom()[~ P U%~ P xa%][U%]7Axiom()[P U%P xb%][U%]9Factor(5,0,1)[~ P xa%][]10Binary((9,0),(3,0))[~ P x%][]11Binary((10,0),(1,0))[~ P U%][U%]12Factor(7,0,1)[P xb%][]14Binary((11,0),(12,0))[][]";
quigley@15642
   839
quigley@15642
   840
val thmstring = " (ALL xa::'a::type.   (~ (P::'a::type => bool) (x::'a::type) | P xa) & (~ P xa | P x)) & (((P::'a::type => bool) (xa::'a::type) | (ALL x::'a::type. P x)) &((ALL x::'a::type. ~ P x) | ~ P (xb::'a::type)))";
quigley@15642
   841
*)
quigley@15642
   842
paulson@15739
   843
fun apply_res_thm str goalstring  = let val tokens = #1 (lex str);
quigley@15642
   844
quigley@15642
   845
                                   val (frees,recon_steps) = parse_step tokens 
quigley@15642
   846
                                   val isar_str = to_isar_proof (frees, recon_steps, goalstring)
quigley@15658
   847
                                   val foo =   TextIO.openOut (File.sysify_path(File.tmp_path (Path.basic "foobar")));
quigley@15642
   848
                               in 
quigley@15642
   849
                                  TextIO.output(foo,(isar_str));TextIO.closeOut foo;Pretty.writeln(Pretty.str  isar_str); () 
quigley@15642
   850
                               end 
quigley@15642
   851
quigley@15642
   852
quigley@16156
   853
quigley@16156
   854
paulson@15684
   855
end;