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