src/HOL/Tools/ATP/recon_transfer_proof.ML
author paulson
Fri Oct 07 17:57:21 2005 +0200 (2005-10-07)
changeset 17775 2679ba74411f
parent 17772 818cec5f82a4
child 17997 6c0fe78624d9
permissions -rw-r--r--
minor code tidyig
paulson@15789
     1
(*  ID:         $Id$
wenzelm@16259
     2
    Author:     Claire Quigley
wenzelm@16259
     3
    Copyright   2004  University of Cambridge
paulson@15789
     4
*)
paulson@15789
     5
paulson@15684
     6
structure Recon_Transfer =
paulson@15684
     7
struct
wenzelm@16803
     8
paulson@15684
     9
open Recon_Parse
wenzelm@16803
    10
paulson@15684
    11
infixr 8 ++; infixr 7 >>; infixr 6 ||;
paulson@15684
    12
paulson@17718
    13
val trace_path = Path.basic "transfer_trace";
paulson@17718
    14
paulson@17718
    15
fun trace s = if !Output.show_debug_msgs then File.append (File.tmp_path trace_path) s 
paulson@17718
    16
              else ();
paulson@17718
    17
quigley@15642
    18
quigley@15642
    19
(* Versions that include type information *)
quigley@15642
    20
 
wenzelm@16803
    21
(* FIXME rename to str_of_thm *)
paulson@16157
    22
fun string_of_thm thm =
wenzelm@16803
    23
  setmp show_sorts true (Pretty.str_of o Display.pretty_thm) thm;
quigley@15642
    24
quigley@15642
    25
quigley@15642
    26
(* check separate args in the watcher program for separating strings with a * or ; or something *)
quigley@15642
    27
quigley@15642
    28
fun clause_strs_to_string [] str = str
quigley@15642
    29
|   clause_strs_to_string (x::xs) str = clause_strs_to_string xs (str^x^"%")
quigley@15642
    30
quigley@15642
    31
fun thmvars_to_string [] str = str
quigley@15642
    32
|   thmvars_to_string (x::xs) str = thmvars_to_string xs (str^x^"%")
quigley@15642
    33
quigley@15642
    34
quigley@15642
    35
fun proofstep_to_string Axiom = "Axiom()"
paulson@16091
    36
|   proofstep_to_string  (Binary ((a,b), (c,d)))=
paulson@16091
    37
      "Binary(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"
paulson@16091
    38
|   proofstep_to_string (Factor (a,b,c)) =
paulson@16091
    39
      "Factor("^(string_of_int a)^","^(string_of_int b)^","^(string_of_int c)^")"
paulson@16091
    40
|   proofstep_to_string  (Para ((a,b), (c,d)))= 
paulson@16091
    41
      "Para(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"
paulson@16091
    42
|   proofstep_to_string  (MRR ((a,b), (c,d))) =
paulson@16091
    43
      "MRR(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"
quigley@16548
    44
(*|   proofstep_to_string (Rewrite((a,b),(c,d))) =
quigley@16548
    45
      "Rewrite(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"*)
quigley@15642
    46
quigley@15642
    47
paulson@17484
    48
fun proof_to_string (num,(step,clause_strs, thmvars)) =
paulson@17484
    49
 (string_of_int num)^(proofstep_to_string step)^
paulson@17484
    50
 "["^(clause_strs_to_string clause_strs "")^"]["^(thmvars_to_string thmvars "")^"]"
quigley@15642
    51
 
quigley@15642
    52
quigley@15642
    53
fun proofs_to_string [] str = str
quigley@15642
    54
|   proofs_to_string (x::xs) str = let val newstr = proof_to_string x 
quigley@15642
    55
                                   in
quigley@15642
    56
                                       proofs_to_string xs (str^newstr)
quigley@15642
    57
                                   end
quigley@15642
    58
quigley@15642
    59
quigley@15642
    60
paulson@17484
    61
fun init_proofstep_to_string (num, step, clause_strs) =
paulson@17484
    62
 (string_of_int num)^" "^(proofstep_to_string step)^" "^
paulson@17484
    63
 (clause_strs_to_string clause_strs "")^" "
quigley@15642
    64
quigley@15642
    65
fun init_proofsteps_to_string [] str = str
quigley@15642
    66
|   init_proofsteps_to_string (x::xs) str = let val newstr = init_proofstep_to_string x 
quigley@15642
    67
                                   in
quigley@15642
    68
                                       init_proofsteps_to_string xs (str^newstr)
quigley@15642
    69
                                   end
quigley@15642
    70
  
quigley@15642
    71
quigley@15642
    72
quigley@15642
    73
(*** get a string representing the Isabelle ordered axioms ***)
quigley@15642
    74
paulson@16061
    75
fun origAx_to_string (num,(meta,thmvars)) =
paulson@16061
    76
    let val clause_strs = ReconOrderClauses.get_meta_lits_bracket meta
paulson@16061
    77
    in
paulson@16091
    78
       (string_of_int num)^"OrigAxiom()["^
paulson@16091
    79
       (clause_strs_to_string clause_strs "")^"]["^
paulson@16061
    80
       (thmvars_to_string thmvars "")^"]"
paulson@16061
    81
    end
quigley@15642
    82
quigley@15642
    83
quigley@15642
    84
fun  origAxs_to_string [] str = str
quigley@15642
    85
|   origAxs_to_string (x::xs) str = let val newstr = origAx_to_string x 
quigley@15642
    86
                                   in
quigley@15642
    87
                                       origAxs_to_string xs (str^newstr)
quigley@15642
    88
                                   end
quigley@15642
    89
quigley@15642
    90
quigley@15642
    91
(*** get a string representing the Isabelle ordered axioms not used in the spass proof***)
quigley@15642
    92
paulson@16061
    93
fun extraAx_to_string (num, (meta,thmvars)) =
paulson@16061
    94
   let val clause_strs = ReconOrderClauses.get_meta_lits_bracket meta
paulson@16061
    95
   in
paulson@16091
    96
      (string_of_int num)^"ExtraAxiom()["^
paulson@16061
    97
      (clause_strs_to_string clause_strs "")^"]"^
paulson@16061
    98
      "["^(thmvars_to_string thmvars "")^"]"
paulson@16061
    99
   end;
quigley@15642
   100
paulson@16061
   101
fun extraAxs_to_string [] str = str
paulson@16061
   102
|   extraAxs_to_string (x::xs) str =
paulson@16061
   103
      let val newstr = extraAx_to_string x 
paulson@16061
   104
      in
paulson@16061
   105
	  extraAxs_to_string xs (str^newstr)
paulson@16061
   106
      end;
quigley@15642
   107
paulson@17312
   108
fun is_axiom (_,Axiom,str) = true
paulson@17312
   109
|   is_axiom (_,_,_) = false
quigley@15642
   110
quigley@15642
   111
fun get_step_nums [] nums = nums
quigley@15642
   112
|   get_step_nums (( num:int,Axiom, str)::xs) nums = get_step_nums xs (nums@[num])
quigley@15642
   113
paulson@15774
   114
exception Noassoc;
paulson@15774
   115
paulson@15774
   116
fun assoc_snd a [] = raise Noassoc
quigley@15642
   117
  | assoc_snd a ((x, y)::t) = if a = y then x else assoc_snd a t;
quigley@15642
   118
quigley@15642
   119
(* change to be something using check_order  instead of a = y --> returns true if ASSERTION not raised in checkorder, false otherwise *)
quigley@15642
   120
quigley@15642
   121
(*fun get_assoc_snds [] xs assocs= assocs
quigley@15642
   122
|   get_assoc_snds (x::xs) ys assocs = get_assoc_snds xs ys (assocs@[((assoc_snd x ys))])
quigley@15642
   123
*)
quigley@15642
   124
(*FIX - should this have vars in it? *)
paulson@16061
   125
fun there_out_of_order xs ys = (ReconOrderClauses.checkorder xs ys [] ([],[],[]); true) 
paulson@15774
   126
                               handle _ => false
quigley@15642
   127
paulson@15774
   128
fun assoc_out_of_order a [] = raise Noassoc
quigley@15642
   129
|   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
   130
quigley@15642
   131
fun get_assoc_snds [] xs assocs= assocs
quigley@15642
   132
|   get_assoc_snds (x::xs) ys assocs = get_assoc_snds xs ys (assocs@[((assoc_out_of_order x ys))])
quigley@15642
   133
quigley@15642
   134
fun add_if_not_inlist [] xs newlist = newlist
paulson@16157
   135
|   add_if_not_inlist (y::ys) xs newlist = if (not (y mem xs)) then 
quigley@15642
   136
                                      add_if_not_inlist ys xs (y::newlist)
quigley@15642
   137
                                        else add_if_not_inlist ys xs (newlist)
quigley@15642
   138
paulson@15700
   139
(*Flattens a list of list of strings to one string*)
paulson@15700
   140
fun onestr ls = String.concat (map String.concat ls);
quigley@15642
   141
paulson@17315
   142
fun is_clasimp_ax clasimp_num n = n <= clasimp_num 
quigley@15919
   143
quigley@16156
   144
fun subone x = x - 1
quigley@16156
   145
quigley@16156
   146
fun numstr [] = ""
quigley@16156
   147
|   numstr (x::xs) = (string_of_int x)^"%"^(numstr xs)
quigley@16156
   148
quigley@15919
   149
quigley@15919
   150
(* retrieve the axioms that were obtained from the clasimpset *)
quigley@15919
   151
paulson@17484
   152
fun get_clasimp_cls (clause_arr: (ResClause.clause * thm) array) step_nums = 
paulson@17484
   153
    let val clasimp_nums = List.filter (is_clasimp_ax (Array.length clause_arr - 1)) 
paulson@17484
   154
	                   (map subone step_nums)
paulson@16157
   155
    in
paulson@17315
   156
	map (fn x =>  Array.sub(clause_arr, x)) clasimp_nums
paulson@16157
   157
    end
quigley@15919
   158
paulson@17775
   159
(* get names of clasimp axioms used*)
paulson@17488
   160
 fun get_axiom_names step_nums clause_arr =
paulson@17775
   161
   map (ResClause.get_axiomName o #1) (get_clasimp_cls clause_arr step_nums);   
paulson@17315
   162
paulson@17488
   163
fun get_axiom_names_spass proofstr clause_arr =
paulson@17315
   164
  let (* parse spass proof into datatype *)
paulson@17718
   165
      val _ = trace ("\nStarted parsing:\n" ^ proofstr)
paulson@17718
   166
      val proof_steps = parse (#1(lex proofstr))
paulson@17718
   167
      val _ = trace "\nParsing finished!"
paulson@17315
   168
      (* get axioms as correctly numbered clauses w.r.t. the Spass proof *)
paulson@17315
   169
  in
paulson@17488
   170
    get_axiom_names (get_step_nums (List.filter is_axiom proof_steps) []) clause_arr
paulson@17315
   171
  end;
quigley@16357
   172
    
paulson@17569
   173
 (*String contains multiple lines.
paulson@17484
   174
  A list consisting of the first number in each line is returned. *)
paulson@17484
   175
fun get_linenums proofstr = 
paulson@17484
   176
  let val numerics = String.tokens (not o Char.isDigit)
paulson@17484
   177
      fun firstno [] = NONE
paulson@17484
   178
        | firstno (x::xs) = Int.fromString x
paulson@17484
   179
      val lines = String.tokens (fn c => c = #"\n") proofstr
paulson@17484
   180
  in  List.mapPartial (firstno o numerics) lines  end
paulson@17484
   181
paulson@17569
   182
fun get_axiom_names_e proofstr clause_arr  =
paulson@17488
   183
   get_axiom_names (get_linenums proofstr) clause_arr;
paulson@17306
   184
    
paulson@17569
   185
 (*String contains multiple lines. We want those of the form 
paulson@17569
   186
     "*********** [448, input] ***********".
paulson@17569
   187
  A list consisting of the first number in each line is returned. *)
paulson@17569
   188
fun get_vamp_linenums proofstr = 
paulson@17569
   189
  let val toks = String.tokens (not o Char.isAlphaNum)
paulson@17569
   190
      fun inputno [n,"input"] = Int.fromString n
paulson@17569
   191
        | inputno _ = NONE
paulson@17569
   192
      val lines = String.tokens (fn c => c = #"\n") proofstr
paulson@17569
   193
  in  List.mapPartial (inputno o toks) lines  end
paulson@17569
   194
paulson@17569
   195
fun get_axiom_names_vamp proofstr clause_arr  =
paulson@17569
   196
   get_axiom_names (get_vamp_linenums proofstr) clause_arr;
paulson@17569
   197
    
quigley@17235
   198
quigley@16357
   199
(***********************************************)
quigley@16357
   200
(* get axioms for reconstruction               *)
quigley@16357
   201
(***********************************************)
quigley@16156
   202
fun numclstr (vars, []) str = str
paulson@16157
   203
|   numclstr ( vars, ((num, thm)::rest)) str =
paulson@16157
   204
      let val newstr = str^(string_of_int num)^" "^(string_of_thm thm)^" "
paulson@16157
   205
      in
paulson@16157
   206
        numclstr  (vars,rest) newstr
paulson@16157
   207
      end
quigley@15919
   208
paulson@16157
   209
fun addvars c (a,b)  = (a,b,c)
quigley@15919
   210
paulson@17484
   211
fun get_axioms_used proof_steps thms clause_arr  =
paulson@17775
   212
 let val axioms = (List.filter is_axiom) proof_steps
paulson@17315
   213
     val step_nums = get_step_nums axioms []
quigley@15919
   214
paulson@17484
   215
     val clauses = make_clauses thms    (*FIXME: must this be repeated??*)
paulson@17315
   216
     
paulson@17315
   217
     val vars = map thm_vars clauses
paulson@17315
   218
    
paulson@17315
   219
     val distvars = distinct (fold append vars [])
paulson@17315
   220
     val clause_terms = map prop_of clauses  
paulson@17315
   221
     val clause_frees = List.concat (map term_frees clause_terms)
quigley@15642
   222
paulson@17315
   223
     val frees = map lit_string_with_nums clause_frees;
quigley@15642
   224
paulson@17315
   225
     val distfrees = distinct frees
quigley@15642
   226
paulson@17315
   227
     val metas = map Meson.make_meta_clause clauses
paulson@17315
   228
     val ax_strs = map #3 axioms
quigley@15642
   229
paulson@17315
   230
     (* literals of -all- axioms, not just those used by spass *)
paulson@17315
   231
     val meta_strs = map ReconOrderClauses.get_meta_lits metas
paulson@17315
   232
    
paulson@17315
   233
     val metas_and_strs = ListPair.zip (metas,meta_strs)
paulson@17718
   234
     val _ = trace ("\nAxioms: " ^ onestr ax_strs)
paulson@17718
   235
     val _ = trace ("\nMeta_strs: " ^ onestr meta_strs)
quigley@15642
   236
paulson@17315
   237
     (* get list of axioms as thms with their variables *)
quigley@15642
   238
paulson@17315
   239
     val ax_metas = get_assoc_snds ax_strs metas_and_strs []
paulson@17315
   240
     val ax_vars = map thm_vars ax_metas
paulson@17315
   241
     val ax_with_vars = ListPair.zip (ax_metas,ax_vars)
quigley@15642
   242
paulson@17315
   243
     (* get list of extra axioms as thms with their variables *)
paulson@17315
   244
     val extra_metas = add_if_not_inlist metas ax_metas []
paulson@17315
   245
     val extra_vars = map thm_vars extra_metas
paulson@17315
   246
     val extra_with_vars = if (not (extra_metas = []) ) 
paulson@17315
   247
			   then ListPair.zip (extra_metas,extra_vars)
paulson@17315
   248
			   else []
paulson@17775
   249
 in
paulson@17775
   250
    (distfrees,distvars, extra_with_vars,ax_with_vars, ListPair.zip (step_nums,ax_metas))
paulson@17775
   251
 end;
paulson@16905
   252
                                            
quigley@15642
   253
quigley@15642
   254
(*********************************************************************)
quigley@15642
   255
(* Pass in spass string of proof and string version of isabelle goal *)
quigley@15642
   256
(* Get out reconstruction steps as a string to be sent to Isabelle   *)
quigley@15642
   257
(*********************************************************************)
quigley@15642
   258
paulson@17122
   259
fun rules_to_string [] = "NONE"
paulson@17775
   260
  | rules_to_string xs = space_implode "  " xs
quigley@16357
   261
quigley@16478
   262
paulson@17772
   263
(*The signal handler in watcher.ML must be able to read the output of this.*)
paulson@17772
   264
fun prover_lemma_list_aux getax proofstr probfile toParent ppid clause_arr = 
paulson@17718
   265
 let val _ = trace
paulson@17718
   266
               ("\nGetting lemma names. proofstr is " ^ proofstr ^
paulson@17772
   267
                "\nprobfile is " ^ probfile ^
paulson@17772
   268
                "  num of clauses is " ^ string_of_int (Array.length clause_arr))
paulson@17488
   269
     val axiom_names = getax proofstr clause_arr
paulson@17484
   270
     val ax_str = rules_to_string axiom_names
quigley@17235
   271
    in 
paulson@17718
   272
	 trace ("\nDone. Lemma list is " ^ ax_str);
paulson@17484
   273
         TextIO.output (toParent, "Success. Lemmas used in automatic proof: " ^
paulson@17484
   274
                  ax_str ^ "\n");
paulson@17772
   275
	 TextIO.output (toParent, probfile ^ "\n");
quigley@17235
   276
	 TextIO.flushOut toParent;
paulson@17583
   277
	 Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2)
quigley@17235
   278
    end
paulson@17484
   279
    handle exn => (*FIXME: exn handler is too general!*)
paulson@17718
   280
     (trace ("\nprover_lemma_list_aux: In exception handler: " ^ 
paulson@17718
   281
             Toplevel.exn_message exn);
paulson@17484
   282
      TextIO.output (toParent, "Translation failed for the proof: " ^ 
paulson@17746
   283
                     String.toString proofstr ^ "\n");
paulson@17772
   284
      TextIO.output (toParent, probfile);
paulson@17315
   285
      TextIO.flushOut toParent;
paulson@17583
   286
      Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2));
quigley@17235
   287
paulson@17569
   288
val e_lemma_list = prover_lemma_list_aux get_axiom_names_e;
paulson@17569
   289
paulson@17569
   290
val vamp_lemma_list = prover_lemma_list_aux get_axiom_names_vamp;
paulson@17315
   291
paulson@17488
   292
val spass_lemma_list = prover_lemma_list_aux get_axiom_names_spass;
paulson@17315
   293
paulson@17315
   294
paulson@17315
   295
(**** Full proof reconstruction for SPASS (not really working) ****)
quigley@15642
   296
paulson@17772
   297
fun spass_reconstruct proofstr probfile toParent ppid thms clause_arr = 
paulson@17718
   298
  let val _ = trace ("\nspass_reconstruct. Proofstr is "^proofstr)
paulson@16905
   299
      val tokens = #1(lex proofstr)
quigley@15782
   300
paulson@16905
   301
  (* parse spass proof into datatype *)
paulson@16905
   302
  (***********************************)
paulson@17306
   303
      val proof_steps = parse tokens
paulson@17718
   304
      val _ = trace "\nParsing finished"
paulson@16905
   305
    
paulson@16905
   306
  (************************************)
paulson@16905
   307
  (* recreate original subgoal as thm *)
paulson@16905
   308
  (************************************)
paulson@16905
   309
      (* get axioms as correctly numbered clauses w.r.t. the Spass proof *)
paulson@16905
   310
      (* need to get prems_of thm, then get right one of the prems, relating to whichever*)
paulson@16905
   311
      (* subgoal this is, and turn it into meta_clauses *)
paulson@16905
   312
      (* should prob add array and table here, so that we can get axioms*)
paulson@16905
   313
      (* produced from the clasimpset rather than the problem *)
paulson@17484
   314
      val (frees,vars,extra_with_vars ,ax_with_vars,numcls) = get_axioms_used proof_steps  thms clause_arr
paulson@16905
   315
      
paulson@16905
   316
      (*val numcls_string = numclstr ( vars, numcls) ""*)
paulson@17718
   317
      val _ = trace "\ngot axioms"
paulson@16061
   318
	
paulson@16905
   319
  (************************************)
paulson@16905
   320
  (* translate proof                  *)
paulson@16905
   321
  (************************************)
paulson@17718
   322
      val _ = trace ("\nabout to translate proof, steps: "
paulson@17718
   323
                       ^ (init_proofsteps_to_string proof_steps ""))
paulson@16905
   324
      val (newthm,proof) = translate_proof numcls  proof_steps vars
paulson@17718
   325
      val _ = trace ("translated proof, steps: "^(init_proofsteps_to_string proof_steps ""))
paulson@16905
   326
  (***************************************************)
paulson@16905
   327
  (* transfer necessary steps as strings to Isabelle *)
paulson@16905
   328
  (***************************************************)
paulson@16905
   329
      (* turn the proof into a string *)
paulson@16905
   330
      val reconProofStr = proofs_to_string proof ""
paulson@16905
   331
      (* do the bit for the Isabelle ordered axioms at the top *)
paulson@16905
   332
      val ax_nums = map #1 numcls
paulson@16905
   333
      val ax_strs = map ReconOrderClauses.get_meta_lits_bracket (map #2 numcls)
paulson@16905
   334
      val numcls_strs = ListPair.zip (ax_nums,ax_strs)
paulson@16905
   335
      val num_cls_vars =  map (addvars vars) numcls_strs;
paulson@16905
   336
      val reconIsaAxStr = origAxs_to_string (ListPair.zip (ax_nums,ax_with_vars)) ""
paulson@16905
   337
      
paulson@16905
   338
      val extra_nums = if (not (extra_with_vars = [])) then (1 upto (length extra_with_vars))
paulson@16905
   339
                       else []
paulson@16905
   340
      val reconExtraAxStr = extraAxs_to_string ( ListPair.zip (extra_nums,extra_with_vars)) ""
paulson@16905
   341
      val frees_str = "["^(thmvars_to_string frees "")^"]"
paulson@16905
   342
      val reconstr = (frees_str^reconExtraAxStr^reconIsaAxStr^reconProofStr)
paulson@17718
   343
      val _ = trace ("\nReconstruction:\n" ^ reconstr)
paulson@16905
   344
  in 
paulson@16905
   345
       TextIO.output (toParent, reconstr^"\n");
paulson@17772
   346
       TextIO.output (toParent, probfile ^ "\n");
paulson@16905
   347
       TextIO.flushOut toParent;
paulson@16905
   348
       Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
paulson@17583
   349
       all_tac
paulson@16905
   350
  end
paulson@17484
   351
  handle exn => (*FIXME: exn handler is too general!*)
paulson@17718
   352
   (trace ("\nspass_reconstruct. In exception handler: " ^ Toplevel.exn_message exn);
paulson@17772
   353
    TextIO.output (toParent,"Translation failed for SPASS proof:"^
paulson@17746
   354
         String.toString proofstr ^"\n");
paulson@17772
   355
    TextIO.output (toParent, probfile ^ "\n");
paulson@17422
   356
    TextIO.flushOut toParent;
paulson@17583
   357
    Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2); all_tac)
quigley@15642
   358
quigley@15642
   359
(**********************************************************************************)
quigley@15642
   360
(* At other end, want to turn back into datatype so can apply reconstruct_proof.  *)
quigley@15642
   361
(* This will be done by the signal handler                                        *)
quigley@15642
   362
(**********************************************************************************)
quigley@15642
   363
quigley@15642
   364
(* Parse in the string version of the proof steps for reconstruction *)
quigley@15642
   365
(* Isar format: cl1 [BINARY 0 cl2 0];cl1 [PARAMOD 0 cl2 0]; cl1 [DEMOD 0 cl2];cl1 [FACTOR 1 2];*)
quigley@15642
   366
quigley@15642
   367
quigley@15642
   368
 val term_numstep =
quigley@15642
   369
        (number ++ (a (Other ",")) ++ number) >> (fn (a, (_, c)) => (a, c))
quigley@15642
   370
quigley@15642
   371
val extraaxiomstep = (a (Word "ExtraAxiom"))++ (a (Other "(")) ++(a (Other ")"))
quigley@15642
   372
            >> (fn (_) => ExtraAxiom)
quigley@15642
   373
quigley@15642
   374
quigley@15642
   375
quigley@15642
   376
val origaxiomstep = (a (Word "OrigAxiom"))++ (a (Other "(")) ++(a (Other ")"))
quigley@15642
   377
            >> (fn (_) => OrigAxiom)
quigley@15642
   378
quigley@15642
   379
quigley@15642
   380
 val axiomstep = (a (Word "Axiom"))++ (a (Other "(")) ++(a (Other ")"))
quigley@15642
   381
            >> (fn (_) => Axiom)
quigley@15642
   382
     
quigley@15642
   383
quigley@15642
   384
quigley@15642
   385
      
quigley@15642
   386
 val binarystep = (a (Word "Binary")) ++ (a (Other "(")) ++ (a (Other "(")) 
quigley@15642
   387
                   ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
quigley@15642
   388
                   ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
quigley@15642
   389
            >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => Binary (c,e))
quigley@15642
   390
      
quigley@15642
   391
quigley@15642
   392
 val parastep = (a (Word "Para")) ++ (a (Other "(")) ++ (a (Other "(")) 
quigley@15642
   393
                   ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
quigley@15642
   394
                   ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
quigley@15642
   395
            >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => Para(c, e))
quigley@15642
   396
      
quigley@15642
   397
 val mrrstep = (a (Word "MRR")) ++ (a (Other "(")) ++ (a (Other "(")) 
quigley@15642
   398
                   ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
quigley@15642
   399
                   ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
quigley@15642
   400
            >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => MRR(c, e))
quigley@15642
   401
      
quigley@15642
   402
quigley@15642
   403
 val factorstep = (a (Word "Factor")) ++ (a (Other "("))
quigley@15642
   404
                    ++ number ++ (a (Other ","))
quigley@15642
   405
                       ++ number ++ (a (Other ","))
quigley@15642
   406
                       ++ number ++  (a (Other ")"))
quigley@15642
   407
                   
quigley@15642
   408
            >> (fn (_, (_, (c, (_, (e,(_,(f,_))))))) =>  Factor (c,e,f))
quigley@15642
   409
quigley@15642
   410
quigley@16548
   411
(*val rewritestep = (a (Word "Rewrite"))  ++ (a (Other "(")) ++ (a (Other "(")) 
quigley@15642
   412
                   ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
quigley@15642
   413
                   ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
quigley@16548
   414
            >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => Rewrite (c,e))*)
quigley@15642
   415
quigley@15642
   416
val obviousstep = (a (Word "Obvious")) ++ (a (Other "(")) 
quigley@15642
   417
                   ++ term_numstep  ++ (a (Other ")")) 
quigley@15642
   418
            >> (fn (_, (_, (c,_))) => Obvious (c))
quigley@15642
   419
quigley@16548
   420
 val methodstep = extraaxiomstep || origaxiomstep || axiomstep ||binarystep || factorstep|| parastep || mrrstep || (*rewritestep ||*) obviousstep
quigley@15642
   421
quigley@15642
   422
quigley@15642
   423
 val number_list_step =
paulson@15739
   424
        ( number ++ many ((a (Other ",") ++ number)>> #2))
quigley@15642
   425
        >> (fn (a,b) => (a::b))
quigley@15642
   426
        
quigley@15642
   427
 val numberlist_step = a (Other "[")  ++ a (Other "]")
quigley@15642
   428
                        >>(fn (_,_) => ([]:int list))
quigley@15642
   429
                       || a (Other "[") ++ number_list_step ++ a (Other "]")
quigley@15642
   430
                        >>(fn (_,(a,_)) => a)
quigley@15642
   431
                    
quigley@15642
   432
quigley@15642
   433
quigley@15642
   434
(** change this to allow P (x U) *)
paulson@17306
   435
 fun arglist_step input = 
paulson@17306
   436
   ( word ++ many word >> (fn (a, b) => (a^" "^(space_implode " " b)))
paulson@17306
   437
    ||word >> (fn (a) => (a)))input
quigley@15642
   438
                
quigley@15642
   439
quigley@15642
   440
fun literal_step input = (word ++ a (Other "(") ++ arglist_step ++  a (Other ")")
quigley@15642
   441
                                          >>(fn (a, (b, (c,d))) => (a^" ("^(c)^")"))
quigley@15642
   442
                        || arglist_step >> (fn (a) => (a)))input
quigley@15642
   443
                           
quigley@15642
   444
quigley@15642
   445
quigley@15642
   446
(* fun term_step input = (a (Other "~") ++ arglist_step ++ a (Other "%")>> (fn (a,(b,c)) => ("~ "^b))
quigley@15642
   447
                     ||  arglist_step ++ a (Other "%")>> (fn (a,b) => a ))input
quigley@15642
   448
*)
quigley@15642
   449
quigley@15642
   450
quigley@15642
   451
 fun term_step input = (a (Other "~") ++ literal_step ++ a (Other "%")>> (fn (a,(b,c)) => ("~ "^b))
quigley@15642
   452
                     ||  literal_step ++ a (Other "%")>> (fn (a,b) => a ))input
quigley@15642
   453
quigley@15642
   454
quigley@15642
   455
         
quigley@15642
   456
quigley@15642
   457
 val term_list_step =
quigley@15642
   458
        (  term_step ++ many ( term_step))
quigley@15642
   459
        >> (fn (a,b) => (a::b))
quigley@15642
   460
        
quigley@15642
   461
 
quigley@15642
   462
val term_lists_step = a (Other "[")  ++ a (Other "]")
quigley@15642
   463
                        >>(fn (_,_) => ([]:string list))
quigley@15642
   464
                       || a (Other "[") ++ term_list_step ++ a (Other "]")
quigley@15642
   465
                        >>(fn (_,(a,_)) => a)
quigley@15642
   466
                     
quigley@15642
   467
quigley@15642
   468
 val linestep = number ++ methodstep ++ term_lists_step ++ term_lists_step
quigley@15642
   469
                >> (fn (a, (b, (c,d))) => (a,(b,c,d)))
quigley@15642
   470
    
quigley@15642
   471
 val lines_step = many linestep
quigley@15642
   472
paulson@15739
   473
 val alllines_step = (term_lists_step ++ lines_step ) ++ finished >> #1
quigley@15642
   474
    
paulson@15739
   475
 val parse_step = #1 o alllines_step
quigley@15642
   476
quigley@15642
   477
quigley@15642
   478
 (*
quigley@15642
   479
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
   480
*)
quigley@15642
   481
quigley@15642
   482
(************************************************************)
quigley@15642
   483
(* Construct an Isar style proof from a list of proof steps *)
quigley@15642
   484
(************************************************************)
quigley@15642
   485
(* want to assume all axioms, then do haves for the other clauses*)
quigley@15642
   486
(* then show for the last step *)
quigley@15642
   487
quigley@15642
   488
(* replace ~ by not here *)
paulson@17317
   489
val change_nots = String.translate (fn c => if c = #"~" then "\\<not>" else str c);
quigley@15642
   490
paulson@17317
   491
fun clstrs_to_string xs = space_implode "; " (map change_nots xs);
quigley@15642
   492
quigley@15642
   493
fun thmvars_to_quantstring [] str = str
quigley@15642
   494
|   thmvars_to_quantstring (x::[]) str =str^x^". "
quigley@15642
   495
|   thmvars_to_quantstring (x::xs) str = thmvars_to_quantstring xs (str^(x^" "))
quigley@15642
   496
quigley@15642
   497
paulson@17317
   498
fun clause_strs_to_isar clstrs [] =
paulson@17317
   499
      "\"\\<lbrakk>"^(clstrs_to_string clstrs)^"\\<rbrakk> \\<Longrightarrow> False\""
paulson@17317
   500
|   clause_strs_to_isar clstrs thmvars =
paulson@17317
   501
      "\"\\<And>"^(thmvars_to_quantstring thmvars "")^
paulson@17317
   502
      "\\<lbrakk>"^(clstrs_to_string clstrs)^"\\<rbrakk> \\<Longrightarrow> False\""
quigley@15642
   503
paulson@17317
   504
fun frees_to_isar_str clstrs = space_implode " " (map change_nots clstrs)
quigley@15642
   505
quigley@15642
   506
quigley@15642
   507
(***********************************************************************)
quigley@15642
   508
(* functions for producing assumptions for the Isabelle ordered axioms *)
quigley@15642
   509
(***********************************************************************)
quigley@15642
   510
(*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
   511
num, rule, clausestrs, vars*)
quigley@15642
   512
quigley@15642
   513
quigley@15642
   514
(* assume the extra clauses - not used in Spass proof *)
quigley@15642
   515
quigley@15642
   516
fun is_extraaxiom_step ( num:int,(ExtraAxiom, str, tstr)) = true
quigley@15642
   517
|   is_extraaxiom_step (num, _) = false
quigley@15642
   518
quigley@15642
   519
fun get_extraaxioms xs = List.filter (is_extraaxiom_step) ( xs)
quigley@15642
   520
quigley@15642
   521
fun assume_isar_extraaxiom [] str  = str
quigley@15642
   522
|   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
   523
quigley@15642
   524
quigley@15642
   525
quigley@15642
   526
fun assume_isar_extraaxioms  [] = ""
quigley@15642
   527
|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
   528
                                         in
quigley@15642
   529
                                             assume_isar_extraaxiom xs str
quigley@15642
   530
                                         end
quigley@15642
   531
quigley@15642
   532
(* assume the Isabelle ordered clauses *)
quigley@15642
   533
quigley@15642
   534
fun is_origaxiom_step ( num:int,(OrigAxiom, str, tstr)) = true
quigley@15642
   535
|   is_origaxiom_step (num, _) = false
quigley@15642
   536
quigley@15642
   537
fun get_origaxioms xs = List.filter (is_origaxiom_step) ( xs)
quigley@15642
   538
quigley@15642
   539
fun assume_isar_origaxiom [] str  = str
quigley@15642
   540
|   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
   541
quigley@15642
   542
quigley@15642
   543
quigley@15642
   544
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
   545
                                         in
quigley@15642
   546
                                             assume_isar_origaxiom xs str
quigley@15642
   547
                                         end
quigley@15642
   548
quigley@15642
   549
quigley@15642
   550
quigley@15642
   551
fun is_axiom_step ( num:int,(Axiom, str, tstr)) = true
quigley@15642
   552
|   is_axiom_step (num, _) = false
quigley@15642
   553
quigley@15642
   554
fun get_axioms xs = List.filter  (is_axiom_step) ( xs)
quigley@15642
   555
quigley@15642
   556
fun have_isar_axiomline (numb,(step, clstrs, thmstrs))="have cl"^(string_of_int numb)^": "^(clause_strs_to_isar clstrs thmstrs)^"\n"
quigley@15642
   557
quigley@15642
   558
fun  by_isar_axiomline (numb,(step, clstrs, thmstrs))="by (rule cl"^ (string_of_int numb)^"') \n"
quigley@15642
   559
quigley@15642
   560
quigley@15642
   561
fun isar_axiomline (numb, (step, clstrs, thmstrs))  = (have_isar_axiomline (numb,(step,clstrs, thmstrs )))^( by_isar_axiomline(numb,(step,clstrs, thmstrs )) )
quigley@15642
   562
quigley@15642
   563
quigley@15642
   564
fun isar_axiomlines [] str = str
quigley@15642
   565
|   isar_axiomlines (x::xs) str = isar_axiomlines xs (str^(isar_axiomline x))
quigley@15642
   566
quigley@15642
   567
quigley@15642
   568
fun have_isar_line (numb,(step, clstrs, thmstrs))="have cl"^(string_of_int numb)^": "^(clause_strs_to_isar clstrs thmstrs)^"\n"
quigley@16357
   569
(*FIX: ask Larry to add and mrr attribute *)
quigley@15642
   570
paulson@16091
   571
fun by_isar_line ((Binary ((a,b), (c,d)))) = 
paulson@16091
   572
    "by(rule cl"^
paulson@16091
   573
		(string_of_int a)^" [binary "^(string_of_int b)^" cl"^
paulson@16091
   574
		(string_of_int c)^" "^(string_of_int d)^"])\n"
quigley@16357
   575
|by_isar_line ((MRR ((a,b), (c,d)))) = 
quigley@16357
   576
    "by(rule cl"^
quigley@16357
   577
		(string_of_int a)^" [binary "^(string_of_int b)^" cl"^
quigley@16357
   578
		(string_of_int c)^" "^(string_of_int d)^"])\n"
paulson@16091
   579
|   by_isar_line ( (Para ((a,b), (c,d)))) =
paulson@16091
   580
    "by (rule cl"^
paulson@16091
   581
		(string_of_int a)^" [paramod "^(string_of_int b)^" cl"^
paulson@16091
   582
		(string_of_int c)^" "^(string_of_int d)^"])\n"
paulson@16091
   583
|   by_isar_line ((Factor ((a,b,c)))) = 
paulson@16091
   584
    "by (rule cl"^(string_of_int a)^" [factor "^(string_of_int b)^" "^
paulson@16091
   585
		(string_of_int c)^" ])\n"
quigley@16548
   586
(*|   by_isar_line ( (Rewrite ((a,b),(c,d)))) =
paulson@16091
   587
    "by (rule cl"^(string_of_int a)^" [demod "^(string_of_int b)^" "^
quigley@16548
   588
		(string_of_int c)^" "^(string_of_int d)^" ])\n"*)
paulson@16091
   589
|   by_isar_line ( (Obvious ((a,b)))) =
paulson@16091
   590
    "by (rule cl"^(string_of_int a)^" [obvious "^(string_of_int b)^" ])\n"
quigley@15642
   591
quigley@15642
   592
fun isar_line (numb, (step, clstrs, thmstrs))  = (have_isar_line (numb,(step,clstrs, thmstrs )))^( by_isar_line step)
quigley@15642
   593
quigley@15642
   594
quigley@15642
   595
fun isar_lines [] str = str
quigley@15642
   596
|   isar_lines (x::xs) str = isar_lines xs (str^(isar_line x))
quigley@15642
   597
quigley@15642
   598
fun last_isar_line (numb,( step, clstrs,thmstrs)) = "show \"False\"\n"^(by_isar_line step)
quigley@15642
   599
quigley@15642
   600
paulson@17772
   601
fun to_isar_proof (frees, xs) =
paulson@16091
   602
    let val extraaxioms = get_extraaxioms xs
paulson@16091
   603
	val extraax_num = length extraaxioms
paulson@16091
   604
	val origaxioms_and_steps = Library.drop (extraax_num, xs)  
paulson@16091
   605
	
paulson@16091
   606
	val origaxioms = get_origaxioms origaxioms_and_steps
paulson@16091
   607
	val origax_num = length origaxioms
paulson@16091
   608
	val axioms_and_steps = Library.drop (origax_num + extraax_num, xs)  
paulson@16091
   609
	val axioms = get_axioms axioms_and_steps
paulson@16091
   610
	
paulson@16091
   611
	val steps = Library.drop (origax_num, axioms_and_steps)
paulson@16091
   612
	val firststeps = ReconOrderClauses.butlast steps
paulson@17312
   613
	val laststep = List.last steps
paulson@16091
   614
	
paulson@16091
   615
	val isar_proof = 
paulson@17772
   616
		("show \"[your goal]\"\n")^
paulson@16091
   617
		("proof (rule ccontr,skolemize, make_clauses) \n")^
paulson@16091
   618
		("fix "^(frees_to_isar_str frees)^"\n")^
paulson@16091
   619
		(assume_isar_extraaxioms extraaxioms)^
paulson@16091
   620
		(assume_isar_origaxioms origaxioms)^
paulson@16091
   621
		(isar_axiomlines axioms "")^
paulson@16091
   622
		(isar_lines firststeps "")^
paulson@16091
   623
		(last_isar_line laststep)^
paulson@16091
   624
		("qed")
paulson@17718
   625
	val _ = trace ("\nto_isar_proof returns " ^ isar_proof)
paulson@16091
   626
    in
paulson@16091
   627
	isar_proof
paulson@16091
   628
    end;
quigley@15642
   629
quigley@15642
   630
(* get fix vars from axioms - all Frees *)
quigley@15642
   631
(* check each clause for meta-vars and /\ over them at each step*)
quigley@15642
   632
quigley@15642
   633
(*******************************************************)
quigley@15642
   634
(* This assumes the thm list  "numcls" is still there  *)
quigley@15642
   635
(* In reality, should probably label it with an        *)
quigley@15642
   636
(* ID number identifying the subgoal.  This could      *)
quigley@15642
   637
(* be passed over to the watcher, e.g.  numcls25       *)
quigley@15642
   638
(*******************************************************)
quigley@15642
   639
paulson@17772
   640
fun apply_res_thm str  = 
paulson@17315
   641
  let val tokens = #1 (lex str);
paulson@17772
   642
      val _ = trace ("\napply_res_thm. str is: "^str^"\n")	
paulson@17315
   643
      val (frees,recon_steps) = parse_step tokens 
paulson@17315
   644
  in 
paulson@17772
   645
      to_isar_proof (frees, recon_steps)
paulson@17315
   646
  end 
quigley@15642
   647
paulson@15684
   648
end;