src/HOL/Tools/ATP/watcher.ML
author quigley
Fri Jun 10 16:15:36 2005 +0200 (2005-06-10)
changeset 16357 f1275d2a1dee
parent 16260 4a1f36eafe17
child 16475 8f3ba52a7937
permissions -rw-r--r--
All subgoals sent to the watcher at once now.
Rules added to parser for Spass proofs.
If parsing or translation fails on a proof, the Spass proof is printed out in PG.
paulson@15789
     1
(*  Title:      Watcher.ML
quigley@16156
     2
paulson@15789
     3
    ID:         $Id$
paulson@15789
     4
    Author:     Claire Quigley
paulson@15789
     5
    Copyright   2004  University of Cambridge
quigley@15642
     6
 *)
quigley@15642
     7
quigley@15642
     8
 (***************************************************************************)
quigley@15642
     9
 (*  The watcher process starts a resolution process when it receives a     *)
quigley@15642
    10
(*  message from Isabelle                                                  *)
quigley@15642
    11
(*  Signals Isabelle, puts output of child into pipe to Isabelle,          *)
quigley@15642
    12
(*  and removes dead processes.  Also possible to kill all the resolution  *)
quigley@15642
    13
(*  processes currently running.                                           *)
quigley@15642
    14
(*  Hardwired version of where to pick up the tptp files for the moment    *)
quigley@15642
    15
(***************************************************************************)
quigley@15642
    16
quigley@15642
    17
(*use "Proof_Transfer";
quigley@15642
    18
use "VampireCommunication";
paulson@16089
    19
use "SpassCommunication";*)
quigley@15642
    20
(*use "/homes/clq20/Jia_Code/TransStartIsar";*)
quigley@15642
    21
quigley@15642
    22
quigley@15642
    23
structure Watcher: WATCHER =
quigley@15642
    24
  struct
quigley@15642
    25
quigley@15642
    26
val goals_being_watched = ref 0;
quigley@15642
    27
quigley@16039
    28
(*****************************************)
quigley@16039
    29
(*  The result of calling createWatcher  *)
quigley@16039
    30
(*****************************************)
quigley@15642
    31
quigley@16039
    32
datatype proc = PROC of {
quigley@16039
    33
        pid : Posix.Process.pid,
quigley@16039
    34
        instr : TextIO.instream,
quigley@16039
    35
        outstr : TextIO.outstream
quigley@16039
    36
      };
quigley@16039
    37
quigley@16039
    38
(*****************************************)
quigley@16039
    39
(*  The result of calling executeToList  *)
quigley@16039
    40
(*****************************************)
quigley@16039
    41
quigley@16039
    42
datatype cmdproc = CMDPROC of {
quigley@16039
    43
        prover: string,             (* Name of the resolution prover used, e.g. Vampire, SPASS *)
quigley@16039
    44
        cmd:  string,              (* The file containing the goal for res prover to prove *)     
quigley@16039
    45
        thmstring: string,         (* string representation of subgoal after negation, skolemization*) 
quigley@16039
    46
        goalstring: string,         (* string representation of subgoal*) 
quigley@16039
    47
        proc_handle : (TextIO.instream,TextIO.outstream) Unix.proc,
quigley@16039
    48
        instr : TextIO.instream,   (*  Input stream to child process *)
quigley@16039
    49
        outstr : TextIO.outstream  (*  Output stream from child process *)
quigley@16039
    50
      };
quigley@16039
    51
quigley@16039
    52
type signal = Posix.Signal.signal
quigley@16039
    53
datatype exit_status = datatype Posix.Process.exit_status
quigley@16039
    54
quigley@16039
    55
val fromStatus = Posix.Process.fromStatus
quigley@16039
    56
quigley@16039
    57
quigley@16039
    58
fun reap(pid, instr, outstr) =
quigley@16039
    59
        let
quigley@16039
    60
		val u = TextIO.closeIn instr;
quigley@16039
    61
	        val u = TextIO.closeOut outstr;
quigley@16039
    62
	
quigley@16039
    63
		val (_, status) =
quigley@16039
    64
			Posix.Process.waitpid(Posix.Process.W_CHILD pid, [])
quigley@16039
    65
	in
quigley@16039
    66
		status
quigley@16039
    67
	end
quigley@16039
    68
quigley@16039
    69
fun fdReader (name : string, fd : Posix.IO.file_desc) =
quigley@16039
    70
	  Posix.IO.mkTextReader {initBlkMode = true,name = name,fd = fd };
quigley@16039
    71
quigley@16039
    72
fun fdWriter (name, fd) =
quigley@16039
    73
          Posix.IO.mkTextWriter {
quigley@16039
    74
	      appendMode = false,
quigley@16039
    75
              initBlkMode = true,
quigley@16039
    76
              name = name,  
quigley@16039
    77
              chunkSize=4096,
quigley@16039
    78
              fd = fd
quigley@16039
    79
            };
quigley@16039
    80
quigley@16039
    81
fun openOutFD (name, fd) =
quigley@16039
    82
	  TextIO.mkOutstream (
quigley@16039
    83
	    TextIO.StreamIO.mkOutstream (
quigley@16039
    84
	      fdWriter (name, fd), IO.BLOCK_BUF));
quigley@16039
    85
quigley@16039
    86
fun openInFD (name, fd) =
quigley@16039
    87
	  TextIO.mkInstream (
quigley@16039
    88
	    TextIO.StreamIO.mkInstream (
quigley@16039
    89
	      fdReader (name, fd), ""));
quigley@16039
    90
quigley@16039
    91
quigley@16039
    92
quigley@16039
    93
quigley@16039
    94
quigley@16039
    95
fun killChild child_handle = Unix.reap child_handle 
quigley@16039
    96
quigley@16039
    97
fun childInfo (PROC{pid,instr,outstr }) = (pid,(instr,outstr));
quigley@16039
    98
quigley@16039
    99
fun cmdstreamsOf (CMDPROC{instr,outstr,...}) = (instr, outstr);
quigley@16039
   100
quigley@16039
   101
fun cmdInStream (CMDPROC{instr,outstr,...}) = (instr);
quigley@16039
   102
quigley@16039
   103
fun cmdchildInfo (CMDPROC{prover,cmd,thmstring,proc_handle,goalstring,instr,outstr}) = (prover,(cmd, (instr,outstr)));
quigley@16039
   104
quigley@16039
   105
fun cmdchildHandle (CMDPROC{prover,cmd,thmstring,goalstring,proc_handle,instr,outstr})  = proc_handle;
quigley@16039
   106
quigley@16039
   107
fun cmdProver (CMDPROC{prover,cmd,thmstring,goalstring,proc_handle,instr,outstr})  = (prover);
quigley@16039
   108
quigley@16039
   109
fun cmdThm (CMDPROC{prover,cmd,thmstring,goalstring,proc_handle,instr,outstr})  = (thmstring);
quigley@16039
   110
quigley@16039
   111
fun cmdGoal (CMDPROC{prover,cmd,thmstring,goalstring,proc_handle,instr,outstr})  = (goalstring);
quigley@15642
   112
quigley@15642
   113
fun sendOutput (outstr,str) = (TextIO.outputSubstr (outstr, (Substring.all str));TextIO.flushOut outstr);
quigley@15642
   114
quigley@15642
   115
(********************************************************************************************)
quigley@15642
   116
(*  takes a list of arguments and sends them individually to the watcher process by pipe    *)
quigley@15642
   117
(********************************************************************************************)
quigley@15642
   118
quigley@15642
   119
fun outputArgs (toWatcherStr, []) = ()
quigley@15642
   120
|   outputArgs (toWatcherStr, (x::xs)) = (TextIO.output (toWatcherStr, x); 
quigley@15642
   121
                                          TextIO.flushOut toWatcherStr;
quigley@15642
   122
                                         outputArgs (toWatcherStr, xs))
quigley@15642
   123
quigley@15642
   124
(********************************************************************************)
quigley@15642
   125
(*    gets individual args from instream and concatenates them into a list      *)
quigley@15642
   126
(********************************************************************************)
quigley@15642
   127
quigley@15642
   128
fun getArgs (fromParentStr, toParentStr,ys) =  let 
quigley@15642
   129
                                       val thisLine = TextIO.input fromParentStr
quigley@15642
   130
                                    in
quigley@15642
   131
                                        ((ys@[thisLine]))
quigley@15642
   132
                                    end
quigley@15642
   133
quigley@15642
   134
(********************************************************************************)
quigley@15642
   135
(*  Remove the \n character from the end of each filename                       *)
quigley@15642
   136
(********************************************************************************)
quigley@15642
   137
quigley@15642
   138
fun getCmd cmdStr = let val backList = ((rev(explode cmdStr))) 
quigley@15642
   139
                    in
quigley@15642
   140
                        if (String.isPrefix "\n"  (implode backList )) 
paulson@16089
   141
                        then (implode (rev ((tl backList))))
paulson@16089
   142
                        else cmdStr
quigley@15642
   143
                    end
quigley@15642
   144
                            
quigley@15642
   145
(********************************************************************************)
quigley@15642
   146
(*  Send request to Watcher for a vampire to be called for filename in arg      *)
quigley@15642
   147
(********************************************************************************)
quigley@15642
   148
                    
quigley@15642
   149
fun callResProver (toWatcherStr,  arg) = (sendOutput (toWatcherStr, arg^"\n"); 
paulson@16089
   150
                                     TextIO.flushOut toWatcherStr)
quigley@15642
   151
quigley@15642
   152
(*****************************************************************************************)
quigley@16357
   153
(*  Send request to Watcher for multiple provers to be called for filenames in arg       *)
quigley@16357
   154
(*  need to do the dfg stuff in the watcher, not here! send over the clasimp and stuff files too*)
quigley@15642
   155
(*****************************************************************************************)
quigley@15642
   156
quigley@16357
   157
    
quigley@15642
   158
(* need to modify to send over hyps file too *)
quigley@16357
   159
fun callResProvers (toWatcherStr,  []) = (sendOutput (toWatcherStr, "End of calls\n"); 
quigley@16357
   160
                                     TextIO.flushOut toWatcherStr)
quigley@15782
   161
|   callResProvers (toWatcherStr,(prover,thmstring,goalstring, proverCmd,settings,clasimpfile, axfile, hypsfile,probfile)::args) =
quigley@16357
   162
                                          let  val  outfile = TextIO.openOut(File.platform_path(File.tmp_path (Path.basic "tog_comms")));                                                                                    
quigley@16357
   163
                              val _ = TextIO.output (outfile,(prover^thmstring^goalstring^proverCmd^settings^clasimpfile^hypsfile^probfile) )
quigley@16357
   164
                              val _ =  TextIO.closeOut outfile
quigley@16357
   165
                                        in   (sendOutput (toWatcherStr, (prover^"*"^thmstring^"*"^goalstring^"*"^proverCmd^"*"^
quigley@16357
   166
							             settings^"*"^clasimpfile^"*"^hypsfile^"*"^probfile^"\n"));
quigley@16357
   167
                                          goals_being_watched := (!goals_being_watched) + 1;
quigley@16357
   168
                                          TextIO.flushOut toWatcherStr;
quigley@16357
   169
					  
quigley@16357
   170
                                          callResProvers (toWatcherStr,args))
quigley@16357
   171
                                        end   
quigley@16357
   172
                                                
quigley@16156
   173
quigley@15782
   174
(*
quigley@15642
   175
fun callResProversStr (toWatcherStr,  []) =  "End of calls\n" 
quigley@15642
   176
                                     
quigley@15782
   177
|   callResProversStr (toWatcherStr,(prover,thmstring,goalstring, proverCmd,settings,clasimpfile, axfile, hypsfile, probfile)::args) =
quigley@15782
   178
                                            ((prover^"*"^thmstring^"*"^goalstring^"*"^proverCmd^"*"^settings^"*"^clasimpfile^"*"^axfile^"*"^hypsfile^"*"^probfile^"\n")
quigley@15642
   179
                                            
quigley@15782
   180
     *)                                      
quigley@15642
   181
 
quigley@15642
   182
(**************************************************************)
quigley@15642
   183
(* Send message to watcher to kill currently running vampires *)
quigley@15642
   184
(**************************************************************)
quigley@15642
   185
quigley@15642
   186
fun callSlayer (toWatcherStr) = (sendOutput (toWatcherStr, "Kill vampires\n"); 
quigley@15642
   187
                            TextIO.flushOut toWatcherStr)
quigley@15642
   188
quigley@15642
   189
quigley@15642
   190
quigley@15642
   191
(**************************************************************)
quigley@15642
   192
(* Remove \n token from a vampire goal filename and extract   *)
quigley@15642
   193
(* prover, proverCmd, settings and file from input string     *)
quigley@15642
   194
(**************************************************************)
quigley@15642
   195
quigley@15642
   196
quigley@15642
   197
 fun takeUntil ch [] res  = (res, [])
quigley@15642
   198
 |   takeUntil ch (x::xs) res = if   x = ch 
quigley@15642
   199
                                then
quigley@15642
   200
                                     (res, xs)
quigley@15642
   201
                                else
quigley@15642
   202
                                     takeUntil ch xs (res@[x])
quigley@15642
   203
quigley@15642
   204
 fun getSettings [] settings = settings
quigley@15642
   205
|    getSettings (xs) settings = let val (set, rest ) = takeUntil "%" xs []
quigley@15642
   206
                                 in
quigley@15642
   207
                                     getSettings rest (settings@[(implode set)])
quigley@15642
   208
                                 end
quigley@15642
   209
quigley@16357
   210
fun separateFields str = let val  outfile = TextIO.openAppend(*("sep_field")*)(File.platform_path(File.tmp_path (Path.basic "sep_field")))                                                                         
quigley@16357
   211
                              val _ = TextIO.output (outfile,("In separateFields, str is: "^(implode str)^"\n\n") )
quigley@16357
   212
                              val _ =  TextIO.closeOut outfile
quigley@16357
   213
                              val (prover, rest) = takeUntil "*" str []
quigley@16357
   214
                              val prover = implode prover
quigley@16357
   215
quigley@16357
   216
                              val (thmstring, rest) =  takeUntil "*" rest []
quigley@16357
   217
                              val thmstring = implode thmstring
quigley@16357
   218
quigley@16357
   219
                              val (goalstring, rest) =  takeUntil "*" rest []
quigley@16357
   220
                              val goalstring = implode goalstring
quigley@16357
   221
quigley@16357
   222
                              val (proverCmd, rest ) =  takeUntil "*" rest []
quigley@16357
   223
                              val proverCmd = implode proverCmd
quigley@16357
   224
                              
quigley@16357
   225
                              val (settings, rest) =  takeUntil "*" rest []
quigley@16357
   226
                              val settings = getSettings settings []
quigley@16357
   227
quigley@16357
   228
			      val (clasimpfile, rest ) =  takeUntil "*" rest []
quigley@16357
   229
                              val clasimpfile = implode clasimpfile
quigley@16357
   230
                              
quigley@16357
   231
  			      val (hypsfile, rest ) =  takeUntil "*" rest []
quigley@16357
   232
                              val hypsfile = implode hypsfile
quigley@16357
   233
quigley@16357
   234
                              val (file, rest) =  takeUntil "*" rest []
quigley@16357
   235
                              val file = implode file
quigley@16357
   236
quigley@16357
   237
                              val  outfile = TextIO.openAppend(File.platform_path(File.tmp_path (Path.basic "sep_comms")));                                                                                    
quigley@16357
   238
                              val _ = TextIO.output (outfile,("Sep comms are: "^(implode str)^"**"^prover^thmstring^goalstring^proverCmd^clasimpfile^hypsfile^file^"\n\n") )
quigley@16357
   239
                              val _ =  TextIO.closeOut outfile
quigley@16357
   240
                              
quigley@16357
   241
                          in
quigley@16357
   242
                             (prover,thmstring,goalstring, proverCmd, settings, clasimpfile, hypsfile, file)
quigley@16357
   243
                          end
quigley@15642
   244
quigley@15642
   245
 
quigley@16357
   246
                      
quigley@16357
   247
(***********************************************************************************)
quigley@16357
   248
(* do tptp2x and cat to turn clasimpfile, hypsfile and probfile into one .dfg file *)
quigley@16357
   249
(***********************************************************************************)
quigley@16357
   250
quigley@16357
   251
fun formatCmd (prover,thmstring,goalstring, proverCmd, settings, clasimpfile, hypsfile ,probfile) = 
quigley@16357
   252
 let
quigley@16357
   253
		val dfg_dir = File.tmp_path (Path.basic "dfg"); 
quigley@16357
   254
quigley@16357
   255
  		(*** need to check here if the directory exists and, if not, create it***)
quigley@16357
   256
  		
quigley@16357
   257
quigley@16357
   258
 		(*** want to cat clasimp ax hyps prob, then send the resulting file to tptp2x ***)
quigley@16357
   259
		val probID = List.last(explode probfile)
quigley@16357
   260
    		val wholefile = File.tmp_path (Path.basic ("ax_prob_"^probID))
quigley@16357
   261
quigley@16357
   262
    		(*** only include problem and clasimp for the moment, not sure how to refer to ***)
quigley@16357
   263
   		(*** hyps/local axioms just now                                                ***)
quigley@16357
   264
   		val whole_prob_file = Unix.execute("/bin/cat", [clasimpfile,(*axfile, hypsfile,*)probfile,  ">", (File.platform_path wholefile)])
quigley@16357
   265
    		val dfg_create =
quigley@16357
   266
 		if File.exists dfg_dir 
quigley@16357
   267
     	        then
quigley@16357
   268
     	            ((warning("dfg dir exists"));())
quigley@16357
   269
 		else
quigley@16357
   270
 		     File.mkdir dfg_dir; 
quigley@16357
   271
   		val dfg_path = File.platform_path dfg_dir;
quigley@16357
   272
   		
quigley@16357
   273
   		val bar = system ("/usr/groups/theory/tptp/TPTP-v3.0.1/TPTP2X/tptp2X -fdfg "^
quigley@16357
   274
                                 (File.platform_path wholefile)^" -d "^dfg_path)
quigley@16357
   275
    		val newfile = dfg_path^"/ax_prob"^"_"^(probID)^".dfg"
quigley@16357
   276
                val  outfile = TextIO.openAppend(File.platform_path(File.tmp_path (Path.basic"thmstring_in_watcher")));   
quigley@16357
   277
   		val _ = TextIO.output (outfile, (thmstring^"\n goals_watched"^(string_of_int(!goals_being_watched))^newfile^"\n"))
quigley@16357
   278
 		val _ =  TextIO.closeOut outfile
quigley@16357
   279
quigley@16357
   280
 	     in
quigley@16357
   281
 		(prover,thmstring,goalstring, proverCmd, settings,newfile)	
quigley@16357
   282
	     end;
quigley@16357
   283
quigley@16357
   284
quigley@16357
   285
(* remove \n from end of command and put back into tuple format *)
quigley@16357
   286
             
quigley@16357
   287
quigley@16357
   288
(*  val cmdStr = "spass*((ALL xa. (~ P x | P xa) & (~ P xa | P x)) & (P xa | (ALL x. P x)) & ((ALL x. ~ P x) | ~ P xb) [.])*(EX x. ALL y. P x = P y) --> (EX x. P x) = (ALL y. P y)*/homes/clq20/bin/SPASS*-DocProof*/local/scratch/clq20/27023/clasimp*/local/scratch/clq20/27023/hyps*/local/scratch/clq20/27023/prob_1\n" *)
quigley@16357
   289
quigley@16357
   290
(*FIX:  are the two getCmd procs getting confused?  Why do I have two anyway? *)
quigley@15642
   291
quigley@15642
   292
 fun getCmd cmdStr = let val backList = ((rev(explode cmdStr))) 
quigley@16357
   293
                         val  outfile = TextIO.openAppend(File.platform_path(File.tmp_path (Path.basic"cmdStr")));   
quigley@16357
   294
   		val _ = TextIO.output (outfile, (cmdStr))
quigley@16357
   295
 		val _ =  TextIO.closeOut outfile
quigley@15642
   296
                     in
quigley@15642
   297
quigley@15642
   298
                         if (String.isPrefix "\n"  (implode backList )) 
quigley@15642
   299
                         then 
quigley@16357
   300
                            ( let 
quigley@16357
   301
              val newCmdStr = (rev(tl backList))
quigley@16357
   302
              val  outfile = TextIO.openOut(File.platform_path(File.tmp_path (Path.basic"backlist")));   
quigley@16357
   303
   		val _ = TextIO.output (outfile, ("about to call sepfields with "^(implode newCmdStr)))
quigley@16357
   304
 		val _ =  TextIO.closeOut outfile 
quigley@16357
   305
				 val cmdtuple = separateFields newCmdStr
quigley@16357
   306
                             in 
quigley@16357
   307
 				 formatCmd cmdtuple
quigley@16357
   308
			     end)
quigley@15642
   309
                         else
quigley@16357
   310
                          ( let 
quigley@16357
   311
			      val cmdtuple =(separateFields (explode cmdStr))
quigley@16357
   312
			   in
quigley@16357
   313
 				formatCmd cmdtuple
quigley@16357
   314
			   end)
quigley@16357
   315
			   
quigley@15642
   316
                     end
quigley@15642
   317
                      
quigley@15642
   318
quigley@15642
   319
fun getProofCmd (a,b,c,d,e,f) = d
quigley@15642
   320
quigley@16357
   321
                        
quigley@15642
   322
(**************************************************************)
quigley@15642
   323
(* Get commands from Isabelle                                 *)
quigley@15642
   324
(**************************************************************)
quigley@16357
   325
(* FIX: or perhaps do the tptp2x stuff here - so it's get commands and then format them, before passing them to spass *)
quigley@15642
   326
quigley@15642
   327
fun getCmds (toParentStr,fromParentStr, cmdList) = 
quigley@16357
   328
                                       let val thisLine = TextIO.inputLine fromParentStr 
quigley@16357
   329
                                           val  outfile = TextIO.openAppend(File.platform_path(File.tmp_path (Path.basic "parent_comms")));                                                                                    
quigley@16357
   330
                              val _ = TextIO.output (outfile,(thisLine) )
quigley@16357
   331
                              val _ =  TextIO.closeOut outfile
quigley@16357
   332
                                       in
quigley@16357
   333
                                          (if (thisLine = "End of calls\n") 
quigley@16357
   334
                                           then 
quigley@16357
   335
 					      
quigley@16357
   336
                                              (cmdList)
quigley@16357
   337
 					      
quigley@16357
   338
                                           else if (thisLine = "Kill children\n") 
quigley@16357
   339
                                                then 
quigley@16357
   340
                                                    (   TextIO.output (toParentStr,thisLine ); 
quigley@16357
   341
                                                        TextIO.flushOut toParentStr;
quigley@16357
   342
                                                      (("","","","Kill children",[],"")::cmdList)
quigley@16357
   343
                                                     )
quigley@16357
   344
                                              else (let val thisCmd = getCmd (thisLine) 
quigley@16357
   345
							(*********************************************************)
quigley@16357
   346
                                                        (* thisCmd = (prover,thmstring,proverCmd, settings, file)*)
quigley@16357
   347
						        (* i.e. put back into tuple format                       *)
quigley@16357
   348
							(*********************************************************)
quigley@16357
   349
                                                    in
quigley@16357
   350
                                                      (*TextIO.output (toParentStr, thisCmd); 
quigley@16357
   351
                                                        TextIO.flushOut toParentStr;*)
quigley@16357
   352
                                                        getCmds (toParentStr,fromParentStr, (thisCmd::cmdList))
quigley@16357
   353
                                                    end
quigley@16357
   354
                                                    )
quigley@16357
   355
                                            )
quigley@16357
   356
                                        end
quigley@15642
   357
                                    
quigley@16357
   358
                                                                  
quigley@15642
   359
(**************************************************************)
quigley@15642
   360
(*  Get Io-descriptor for polling of an input stream          *)
quigley@15642
   361
(**************************************************************)
quigley@15642
   362
quigley@15642
   363
quigley@15642
   364
fun getInIoDesc someInstr = 
quigley@15642
   365
    let val (rd, buf) = TextIO.StreamIO.getReader(TextIO.getInstream someInstr)
paulson@15702
   366
        val _ = TextIO.output (TextIO.stdOut, buf)
quigley@15642
   367
        val ioDesc = 
quigley@15642
   368
	    case rd
quigley@15642
   369
	      of TextPrimIO.RD{ioDesc = SOME iod, ...} =>SOME iod
quigley@15642
   370
	       | _ => NONE
quigley@15642
   371
     in (* since getting the reader will have terminated the stream, we need
quigley@15642
   372
	 * to build a new stream. *)
quigley@15642
   373
	TextIO.setInstream(someInstr, TextIO.StreamIO.mkInstream(rd, buf));
quigley@15642
   374
	ioDesc
quigley@15642
   375
    end
quigley@15642
   376
quigley@15642
   377
quigley@15642
   378
(*************************************)
quigley@15642
   379
(*  Set up a Watcher Process         *)
quigley@15642
   380
(*************************************)
quigley@15642
   381
quigley@15642
   382
quigley@15642
   383
paulson@16061
   384
fun setupWatcher (thm,clause_arr, num_of_clauses) = 
paulson@16061
   385
  let
paulson@16061
   386
    (** pipes for communication between parent and watcher **)
paulson@16061
   387
    val p1 = Posix.IO.pipe ()
paulson@16061
   388
    val p2 = Posix.IO.pipe ()
paulson@16061
   389
    fun closep () = (
paulson@16061
   390
	  Posix.IO.close (#outfd p1); 
paulson@16061
   391
	  Posix.IO.close (#infd p1);
paulson@16061
   392
	  Posix.IO.close (#outfd p2); 
paulson@16061
   393
	  Posix.IO.close (#infd p2)
paulson@16061
   394
	)
paulson@16061
   395
    (***********************************************************)
paulson@16061
   396
    (****** fork a watcher process and get it set up and going *)
paulson@16061
   397
    (***********************************************************)
paulson@16061
   398
    fun startWatcher (procList) =
paulson@16061
   399
	     (case  Posix.Process.fork() (***************************************)
paulson@16061
   400
	   of SOME pid =>  pid           (* parent - i.e. main Isabelle process *)
paulson@16061
   401
					 (***************************************)
paulson@16061
   402
paulson@16061
   403
					   (*************************)
paulson@16061
   404
	    | NONE => let                  (* child - i.e. watcher  *)
paulson@16061
   405
		val oldchildin = #infd p1  (*************************)
paulson@16061
   406
		val fromParent = Posix.FileSys.wordToFD 0w0
paulson@16061
   407
		val oldchildout = #outfd p2
paulson@16061
   408
		val toParent = Posix.FileSys.wordToFD 0w1
paulson@16061
   409
		val fromParentIOD = Posix.FileSys.fdToIOD fromParent
paulson@16061
   410
		val fromParentStr = openInFD ("_exec_in_parent", fromParent)
paulson@16061
   411
		val toParentStr = openOutFD ("_exec_out_parent", toParent)
paulson@16061
   412
		val sign = sign_of_thm thm
paulson@16061
   413
		val prems = prems_of thm
paulson@16061
   414
		val prems_string =  Meson.concat_with_and (map (Sign.string_of_term sign) prems) 
paulson@16061
   415
		val _ = (warning ("subgoals forked to startWatcher: "^prems_string));
paulson@16061
   416
	       (* tracing *)
paulson@16061
   417
	      (*val tenth_ax = fst( Array.sub(clause_arr, 1))  
paulson@16061
   418
		val tenth_ax_thms = memo_find_clause (tenth_ax, clause_tab)
paulson@16061
   419
		val clause_str = Meson.concat_with_and (map string_of_thm tenth_ax_thms)
paulson@16061
   420
		val _ = (warning ("tenth axiom in array in watcher is: "^tenth_ax))         
paulson@16061
   421
		val _ = (warning ("tenth axiom in table in watcher is: "^clause_str))         
paulson@16061
   422
		val _ = (warning ("num_of_clauses in watcher is: "^(string_of_int (num_of_clauses))) )       
paulson@16061
   423
			 *)
paulson@16061
   424
		(*val goalstr = string_of_thm (the_goal)
wenzelm@16260
   425
		 val  outfile = TextIO.openOut(File.platform_path(File.tmp_path (Path.basic "goal_in_watcher")));  
paulson@16061
   426
		val _ = TextIO.output (outfile,goalstr )
paulson@16061
   427
		val _ =  TextIO.closeOut outfile*)
paulson@16061
   428
		fun killChildren [] = ()
paulson@16061
   429
	     |      killChildren  (child_handle::children) = (killChild child_handle; killChildren children)
quigley@15642
   430
paulson@16061
   431
	       
paulson@16061
   432
     
paulson@16061
   433
	      (*************************************************************)
paulson@16061
   434
	      (* take an instream and poll its underlying reader for input *)
paulson@16061
   435
	      (*************************************************************)
quigley@15642
   436
paulson@16061
   437
	      fun pollParentInput () = 
paulson@16061
   438
		     
paulson@16061
   439
			 let val pd = OS.IO.pollDesc (fromParentIOD)
paulson@16061
   440
			 in 
paulson@16061
   441
			 if (isSome pd ) then 
paulson@16061
   442
			     let val pd' = OS.IO.pollIn (valOf pd)
quigley@16156
   443
				 val pdl = OS.IO.poll ([pd'], SOME (Time.fromMilliseconds 2000)) 
quigley@16357
   444
				 val _ = File.append (File.tmp_path (Path.basic "parent_poll")) 
quigley@16357
   445
			           ("In parent_poll\n")	
paulson@16061
   446
			     in
paulson@16061
   447
				if null pdl 
paulson@16061
   448
				then
paulson@16061
   449
				   NONE
paulson@16061
   450
				else if (OS.IO.isIn (hd pdl)) 
paulson@16061
   451
				     then
paulson@16061
   452
					(SOME ( getCmds (toParentStr, fromParentStr, [])))
paulson@16061
   453
				     else
paulson@16061
   454
					 NONE
paulson@16061
   455
			     end
paulson@16061
   456
			   else
paulson@16061
   457
			       NONE
paulson@16061
   458
			   end
paulson@16061
   459
		      
paulson@16061
   460
	     
quigley@15642
   461
paulson@16061
   462
	       fun pollChildInput (fromStr) = 
paulson@16061
   463
		     let val iod = getInIoDesc fromStr
paulson@16061
   464
		     in 
paulson@16061
   465
		     if isSome iod 
paulson@16061
   466
		     then 
paulson@16061
   467
			 let val pd = OS.IO.pollDesc (valOf iod)
paulson@16061
   468
			 in 
paulson@16061
   469
			 if (isSome pd ) then 
paulson@16061
   470
			     let val pd' = OS.IO.pollIn (valOf pd)
quigley@16156
   471
				 val pdl = OS.IO.poll ([pd'], SOME (Time.fromMilliseconds 2000)) 
quigley@16357
   472
                                 val _ = File.append (File.tmp_path (Path.basic "child_poll")) 
quigley@16357
   473
			           ("In child_poll\n")
paulson@16061
   474
			     in
paulson@16061
   475
				if null pdl 
paulson@16061
   476
				then
paulson@16061
   477
				   NONE
paulson@16061
   478
				else if (OS.IO.isIn (hd pdl)) 
paulson@16061
   479
				     then
quigley@16357
   480
					 SOME ((*getCmd*) (TextIO.inputLine fromStr))
paulson@16061
   481
				     else
paulson@16061
   482
					 NONE
paulson@16061
   483
			     end
paulson@16061
   484
			   else
paulson@16061
   485
			       NONE
paulson@16061
   486
			   end
paulson@16061
   487
		       else 
paulson@16061
   488
			   NONE
paulson@16061
   489
		      end
quigley@15642
   490
quigley@15642
   491
paulson@16061
   492
	     (****************************************************************************)
paulson@16061
   493
	     (* Check all vampire processes currently running for output                 *)
paulson@16061
   494
	     (****************************************************************************) 
paulson@16061
   495
							(*********************************)
paulson@16061
   496
	      fun checkChildren ([], toParentStr) = []  (*** no children to check ********)
paulson@16061
   497
							(*********************************)
paulson@16061
   498
	      |   checkChildren ((childProc::otherChildren), toParentStr) = 
quigley@16357
   499
		    let val _ = File.append (File.tmp_path (Path.basic "child_check")) 
quigley@16357
   500
			           ("In check child, length of queue:"^(string_of_int (length (childProc::otherChildren)))^"\n")
quigley@16357
   501
                        val (childInput,childOutput) =  cmdstreamsOf childProc
paulson@16061
   502
			val child_handle= cmdchildHandle childProc
paulson@16061
   503
			(* childCmd is the .dfg file that the problem is in *)
paulson@16061
   504
			val childCmd = fst(snd (cmdchildInfo childProc))
quigley@16357
   505
                        val _ = File.append (File.tmp_path (Path.basic "child_command")) 
quigley@16357
   506
			           (childCmd^"\n")
paulson@16061
   507
			(* now get the number of the subgoal from the filename *)
paulson@16061
   508
			val sg_num = int_of_string(ReconOrderClauses.get_nth 5 (rev(explode childCmd)))
paulson@16061
   509
			val childIncoming = pollChildInput childInput
quigley@16357
   510
 			val _ = File.append (File.tmp_path (Path.basic "child_check_polled")) 
quigley@16357
   511
			           ("finished polling child \n")
paulson@16061
   512
			val parentID = Posix.ProcEnv.getppid()
paulson@16061
   513
			val prover = cmdProver childProc
paulson@16061
   514
			val thmstring = cmdThm childProc
paulson@16061
   515
			val sign = sign_of_thm thm
paulson@16061
   516
			val prems = prems_of thm
paulson@16061
   517
			val prems_string =  Meson.concat_with_and (map (Sign.string_of_term sign) prems) 
paulson@16100
   518
			val _ = warning("subgoals forked to checkChildren: "^prems_string)
paulson@16100
   519
			val goalstring = cmdGoal childProc							
quigley@16357
   520
			val _ = File.append (File.tmp_path (Path.basic "child_comms")) 
quigley@16357
   521
			           (prover^thmstring^goalstring^childCmd^"\n")
paulson@16061
   522
		    in 
paulson@16061
   523
		      if (isSome childIncoming) 
paulson@16061
   524
		      then 
paulson@16061
   525
			  (* check here for prover label on child*)
paulson@16100
   526
			  let val _ = File.write (File.tmp_path (Path.basic "child_incoming"))  ("subgoals forked to checkChildren: "^prems_string^prover^thmstring^goalstring^childCmd) 
paulson@16061
   527
		      val childDone = (case prover of
quigley@16156
   528
	    (* "vampire" => startVampireTransfer(childInput, toParentStr, parentID, childCmd)                                               |*)"spass" => (SpassComm.checkSpassProofFound(childInput, toParentStr, parentID,thmstring,goalstring, childCmd, thm, sg_num,clause_arr, num_of_clauses)     ) )
paulson@16061
   529
			   in
paulson@16061
   530
			    if childDone      (**********************************************)
paulson@16061
   531
			    then              (* child has found a proof and transferred it *)
paulson@16061
   532
					      (**********************************************)
quigley@15642
   533
paulson@16061
   534
			       (**********************************************)
paulson@16061
   535
			       (* Remove this child and go on to check others*)
paulson@16061
   536
			       (**********************************************)
paulson@16061
   537
			       ( Unix.reap child_handle;
paulson@16061
   538
				 checkChildren(otherChildren, toParentStr))
paulson@16061
   539
			    else 
paulson@16061
   540
			       (**********************************************)
paulson@16061
   541
			       (* Keep this child and go on to check others  *)
paulson@16061
   542
			       (**********************************************)
quigley@15642
   543
paulson@16061
   544
				 (childProc::(checkChildren (otherChildren, toParentStr)))
paulson@16061
   545
			 end
paulson@16061
   546
		       else
paulson@16100
   547
			 (File.append (File.tmp_path (Path.basic "child_incoming")) "No child output ";
paulson@16100
   548
			  childProc::(checkChildren (otherChildren, toParentStr)))
paulson@16061
   549
		    end
quigley@15642
   550
paulson@16061
   551
	     
paulson@16061
   552
	  (********************************************************************)                  
paulson@16061
   553
	  (* call resolution processes                                        *)
paulson@16061
   554
	  (* settings should be a list of strings                             *)
paulson@16061
   555
	  (* e.g. ["-t 300", "-m 100000"]                                     *)
paulson@16061
   556
	  (* takes list of (string, string, string list, string)list proclist *)
paulson@16061
   557
	  (********************************************************************)
quigley@15642
   558
quigley@16039
   559
paulson@16061
   560
(*** add subgoal id num to this *)
paulson@16061
   561
	     fun execCmds  [] procList = procList
quigley@16357
   562
	     |   execCmds  ((prover, thmstring,goalstring,proverCmd,settings,file)::cmds) procList  =             let val _ = File.append (File.tmp_path (Path.basic "pre_exec_child"))  ("About to execute command for goal:\n"^goalstring^proverCmd^(concat settings)^file^" at "^(Date.toString(Date.fromTimeLocal(Time.now()))))
quigley@16357
   563
	       in 
quigley@16357
   564
		 if (prover = "spass") 
paulson@16100
   565
		   then 
paulson@16100
   566
		       let val childhandle:(TextIO.instream,TextIO.outstream) Unix.proc  = (Unix.execute(proverCmd, (["-FullRed=0"]@settings@[file])))
paulson@16100
   567
			   val (instr, outstr)=Unix.streamsOf childhandle
paulson@16100
   568
			   val newProcList =    (((CMDPROC{
paulson@16100
   569
						prover = prover,
paulson@16100
   570
						cmd = file,
paulson@16100
   571
						thmstring = thmstring,
paulson@16100
   572
						goalstring = goalstring,
paulson@16100
   573
						proc_handle = childhandle,
paulson@16100
   574
						instr = instr,
paulson@16100
   575
						outstr = outstr })::procList))
quigley@16357
   576
			    val _ = File.append (File.tmp_path (Path.basic "exec_child"))  ("executing command for goal:\n"^goalstring^proverCmd^(concat settings)^file^" at "^(Date.toString(Date.fromTimeLocal(Time.now()))))
paulson@16100
   577
		       in
quigley@16357
   578
			  Posix.Process.sleep (Time.fromSeconds 1);execCmds cmds newProcList
paulson@16100
   579
		       end
paulson@16100
   580
		   else 
quigley@16357
   581
		       let val childhandle:(TextIO.instream,TextIO.outstream) Unix.proc  = (Unix.execute(proverCmd, (settings@[file])))
paulson@16100
   582
			   val (instr, outstr)=Unix.streamsOf childhandle
paulson@16100
   583
			   val newProcList =    (((CMDPROC{
paulson@16100
   584
						prover = prover,
paulson@16100
   585
						cmd = file,
paulson@16100
   586
						thmstring = thmstring,
paulson@16100
   587
						goalstring = goalstring,
paulson@16100
   588
						proc_handle = childhandle,
paulson@16100
   589
						instr = instr,
paulson@16100
   590
						outstr = outstr })::procList))
paulson@16100
   591
		       in
quigley@16357
   592
			 Posix.Process.sleep (Time.fromSeconds 1); execCmds cmds newProcList
paulson@16100
   593
		       end
quigley@16357
   594
		end
quigley@15642
   595
quigley@16039
   596
paulson@16061
   597
	  (****************************************)                  
paulson@16061
   598
	  (* call resolution processes remotely   *)
paulson@16061
   599
	  (* settings should be a list of strings *)
paulson@16061
   600
	  (* e.g. ["-t 300", "-m 100000"]         *)
paulson@16061
   601
	  (****************************************)
paulson@16061
   602
paulson@16061
   603
	   (*  fun execRemoteCmds  [] procList = procList
paulson@16061
   604
	     |   execRemoteCmds ((prover, thmstring,goalstring,proverCmd ,settings,file)::cmds) procList  =  
paulson@16061
   605
				       let val  newProcList =  mySshExecuteToList ("/usr/bin/ssh",([prover,thmstring,goalstring,"-t", "shep"]@[proverCmd]@settings@[file]), procList)
paulson@16061
   606
					   in
paulson@16061
   607
						execRemoteCmds  cmds newProcList
paulson@16061
   608
					   end
quigley@16039
   609
*)
quigley@15642
   610
paulson@16061
   611
	     fun printList (outStr, []) = ()
paulson@16061
   612
	     |   printList (outStr, (x::xs)) = (TextIO.output (outStr, x);TextIO.flushOut outStr; printList (outStr,xs))                  
quigley@15642
   613
quigley@15642
   614
paulson@16061
   615
	  (**********************************************)                  
paulson@16061
   616
	  (* Watcher Loop                               *)
paulson@16061
   617
	  (**********************************************)
paulson@16061
   618
quigley@15642
   619
quigley@15642
   620
quigley@15642
   621
paulson@16061
   622
	      fun keepWatching (toParentStr, fromParentStr,procList) = 
paulson@16061
   623
		    let    fun loop (procList) =  
paulson@16061
   624
			   (
paulson@16061
   625
			   let val cmdsFromIsa = pollParentInput ()
paulson@16061
   626
			       fun killchildHandler (n:int)  = (TextIO.output(toParentStr, "Killing child proof processes!\n");
paulson@16061
   627
					    TextIO.flushOut toParentStr;
paulson@16061
   628
					     killChildren (map (cmdchildHandle) procList);
paulson@16061
   629
					      ())
paulson@16061
   630
			       
paulson@16061
   631
			   in 
paulson@16061
   632
			      (*Signal.signal (Posix.Signal.usr2, Signal.SIG_HANDLE killchildHandler);*)
paulson@16061
   633
										 (**********************************)
paulson@16061
   634
			      if (isSome cmdsFromIsa) then                       (*  deal with input from Isabelle *)
paulson@16061
   635
				   (                                             (**********************************)                             
paulson@16061
   636
				    if (getProofCmd(hd(valOf cmdsFromIsa)) = "Kill children" )
paulson@16061
   637
				    then 
paulson@16061
   638
				      (
paulson@16061
   639
					let val child_handles = map cmdchildHandle procList 
paulson@16061
   640
					in
paulson@16061
   641
					   killChildren child_handles;
paulson@16061
   642
					   (*Posix.Process.kill(Posix.Process.K_PROC (Posix.ProcEnv.getppid()), Posix.Signal.usr2);*)                                                              loop ([])
paulson@16061
   643
					end
paulson@16061
   644
					   
paulson@16061
   645
				       )
paulson@16061
   646
				    else
paulson@16061
   647
				      ( 
paulson@16061
   648
					if ((length procList)<10)    (********************)
paulson@16061
   649
					then                        (* Execute locally  *)
paulson@16061
   650
					   (                        (********************)
paulson@16061
   651
					    let 
paulson@16061
   652
					      val newProcList = execCmds (valOf cmdsFromIsa) procList
paulson@16061
   653
					      val parentID = Posix.ProcEnv.getppid()
quigley@16357
   654
          					 val _ = (File.append (File.tmp_path (Path.basic "prekeep_watch")) "Just execed a child\n ")
paulson@16061
   655
					      val newProcList' =checkChildren (newProcList, toParentStr) 
quigley@16357
   656
quigley@16357
   657
					      val _ = warning("off to keep watching...")
quigley@16357
   658
					     val _ = (File.append (File.tmp_path (Path.basic "keep_watch")) "Off to keep watching...\n ")
paulson@16061
   659
					    in
paulson@16061
   660
					      (*Posix.Process.sleep (Time.fromSeconds 1);*)
paulson@16061
   661
					      loop (newProcList') 
paulson@16061
   662
					    end
paulson@16061
   663
					    )
paulson@16061
   664
					else                         (*********************************)
paulson@16061
   665
								     (* Execute remotely              *)
paulson@16061
   666
								     (* (actually not remote for now )*)
paulson@16061
   667
					    (                        (*********************************)
paulson@16061
   668
					    let 
paulson@16061
   669
					      val newProcList = execCmds (valOf cmdsFromIsa) procList
paulson@16061
   670
					      val parentID = Posix.ProcEnv.getppid()
paulson@16061
   671
					      val newProcList' =checkChildren (newProcList, toParentStr) 
paulson@16061
   672
					    in
paulson@16061
   673
					      (*Posix.Process.sleep (Time.fromSeconds 1);*)
paulson@16061
   674
					      loop (newProcList') 
paulson@16061
   675
					    end
paulson@16061
   676
					    )
quigley@15642
   677
quigley@15642
   678
quigley@15642
   679
paulson@16061
   680
					)
paulson@16061
   681
				     )                                              (******************************)
paulson@16061
   682
			      else                                                  (* No new input from Isabelle *)
paulson@16061
   683
										    (******************************)
paulson@16061
   684
				  (    let val newProcList = checkChildren ((procList), toParentStr)
paulson@16061
   685
				       in
quigley@16357
   686
					 (*Posix.Process.sleep (Time.fromSeconds 1);*)
quigley@16357
   687
					(File.append (File.tmp_path (Path.basic "keep_watch")) "Off to keep watching.2..\n ");
paulson@16061
   688
					 loop (newProcList)
paulson@16061
   689
				       end
paulson@16061
   690
				   
paulson@16061
   691
				   )
paulson@16061
   692
			    end)
paulson@16061
   693
		    in  
paulson@16061
   694
			loop (procList)
paulson@16061
   695
		    end
paulson@16061
   696
		
paulson@16061
   697
    
paulson@16061
   698
		in
paulson@16061
   699
		 (***************************)
paulson@16061
   700
		 (*** Sort out pipes ********)
paulson@16061
   701
		 (***************************)
quigley@15642
   702
paulson@16061
   703
		  Posix.IO.close (#outfd p1);
paulson@16061
   704
		  Posix.IO.close (#infd p2);
paulson@16061
   705
		  Posix.IO.dup2{old = oldchildin, new = fromParent};
paulson@16061
   706
		  Posix.IO.close oldchildin;
paulson@16061
   707
		  Posix.IO.dup2{old = oldchildout, new = toParent};
paulson@16061
   708
		  Posix.IO.close oldchildout;
quigley@15642
   709
paulson@16061
   710
		  (***************************)
paulson@16061
   711
		  (* start the watcher loop  *)
paulson@16061
   712
		  (***************************)
paulson@16061
   713
		  keepWatching (toParentStr, fromParentStr, procList);
quigley@15642
   714
quigley@15642
   715
paulson@16061
   716
		  (****************************************************************************)
paulson@16061
   717
		  (* fake return value - actually want the watcher to loop until killed       *)
paulson@16061
   718
		  (****************************************************************************)
paulson@16061
   719
		  Posix.Process.wordToPid 0w0
paulson@16061
   720
		  
paulson@16061
   721
		end);
paulson@16061
   722
	  (* end case *)
quigley@15642
   723
quigley@15642
   724
paulson@16061
   725
    val _ = TextIO.flushOut TextIO.stdOut
quigley@15642
   726
paulson@16061
   727
    (*******************************)
paulson@16061
   728
    (***  set watcher going ********)
paulson@16061
   729
    (*******************************)
quigley@15642
   730
paulson@16061
   731
    val procList = []
paulson@16061
   732
    val pid = startWatcher (procList)
paulson@16061
   733
    (**************************************************)
paulson@16061
   734
    (* communication streams to watcher               *)
paulson@16061
   735
    (**************************************************)
quigley@15642
   736
paulson@16061
   737
    val instr = openInFD ("_exec_in", #infd p2)
paulson@16061
   738
    val outstr = openOutFD ("_exec_out", #outfd p1)
paulson@16061
   739
    
paulson@16061
   740
    in
paulson@16061
   741
     (*******************************)
paulson@16061
   742
     (* close the child-side fds    *)
paulson@16061
   743
     (*******************************)
paulson@16061
   744
      Posix.IO.close (#outfd p2);
paulson@16061
   745
      Posix.IO.close (#infd p1);
paulson@16061
   746
      (* set the fds close on exec *)
paulson@16061
   747
      Posix.IO.setfd (#infd p2, Posix.IO.FD.flags [Posix.IO.FD.cloexec]);
paulson@16061
   748
      Posix.IO.setfd (#outfd p1, Posix.IO.FD.flags [Posix.IO.FD.cloexec]);
paulson@16061
   749
       
paulson@16061
   750
     (*******************************)
paulson@16061
   751
     (* return value                *)
paulson@16061
   752
     (*******************************)
paulson@16061
   753
      PROC{pid = pid,
paulson@16061
   754
	instr = instr,
paulson@16061
   755
	outstr = outstr
paulson@16061
   756
      }
paulson@16061
   757
   end;
quigley@15642
   758
quigley@15642
   759
quigley@15642
   760
quigley@15642
   761
(**********************************************************)
quigley@15642
   762
(* Start a watcher and set up signal handlers             *)
quigley@15642
   763
(**********************************************************)
quigley@16039
   764
quigley@16039
   765
fun killWatcher pid= Posix.Process.kill(Posix.Process.K_PROC pid, Posix.Signal.kill);
quigley@16039
   766
quigley@16039
   767
fun reapWatcher(pid, instr, outstr) =
quigley@16039
   768
        let
quigley@16039
   769
		val u = TextIO.closeIn instr;
quigley@16039
   770
	        val u = TextIO.closeOut outstr;
quigley@16039
   771
	
quigley@16039
   772
		val (_, status) =
quigley@16039
   773
			Posix.Process.waitpid(Posix.Process.W_CHILD pid, [])
quigley@16039
   774
	in
quigley@16039
   775
		status
quigley@16039
   776
	end
quigley@15642
   777
quigley@16156
   778
quigley@16156
   779
fun createWatcher (thm,clause_arr, ( num_of_clauses:int)) = let val mychild = childInfo (setupWatcher(thm,clause_arr,  num_of_clauses))
quigley@16156
   780
			   val streams =snd mychild
quigley@16156
   781
                           val childin = fst streams
quigley@16156
   782
                           val childout = snd streams
quigley@16156
   783
                           val childpid = fst mychild
quigley@16156
   784
                           val sign = sign_of_thm thm
quigley@16156
   785
                           val prems = prems_of thm
quigley@16156
   786
                           val prems_string =  Meson.concat_with_and (map (Sign.string_of_term sign) prems) 
quigley@16156
   787
                           val _ = (warning ("subgoals forked to createWatcher: "^prems_string));
quigley@16156
   788
                           fun vampire_proofHandler (n) =
quigley@16156
   789
                           (Pretty.writeln(Pretty.str ( (concat[(oct_char "360"), (oct_char "377")])));
quigley@16156
   790
                           getVampInput childin; Pretty.writeln(Pretty.str  (oct_char "361"));() )               
quigley@16156
   791
                          
quigley@16156
   792
quigley@16156
   793
fun spass_proofHandler (n) = (
wenzelm@16260
   794
                                 let  val  outfile = TextIO.openOut(File.platform_path(File.tmp_path (Path.basic "foo_signal1")));
quigley@16156
   795
                                      val _ = TextIO.output (outfile, ("In signal handler now\n"))
quigley@16156
   796
                                      val _ =  TextIO.closeOut outfile
quigley@16156
   797
                                      val (reconstr, thmstring, goalstring) = SpassComm.getSpassInput childin
wenzelm@16260
   798
                                      val  outfile = TextIO.openAppend(File.platform_path(File.tmp_path (Path.basic "foo_signal")));
quigley@16156
   799
quigley@16156
   800
                                      val _ = TextIO.output (outfile, ("In signal handler  "^reconstr^thmstring^goalstring^"goals_being_watched is: "^(string_of_int (!goals_being_watched))))
quigley@16156
   801
                                      val _ =  TextIO.closeOut outfile
quigley@16156
   802
                                      in          (* if a proof has been found and sent back as a reconstruction proof *)
quigley@16156
   803
                                                  if ( (substring (reconstr, 0,1))= "[")
quigley@16156
   804
                                                  then 
quigley@15642
   805
quigley@16156
   806
                                                            (
quigley@16156
   807
                                                                 Pretty.writeln(Pretty.str ( (concat[(oct_char "360"), (oct_char "377")])));
quigley@16156
   808
                                                                 Recon_Transfer.apply_res_thm reconstr goalstring;
quigley@16156
   809
                                                                 Pretty.writeln(Pretty.str  (oct_char "361"));
quigley@16156
   810
                                                                 
quigley@16156
   811
                                                                 goals_being_watched := ((!goals_being_watched) - 1);
quigley@16156
   812
                                                         
quigley@16156
   813
                                                                 if ((!goals_being_watched) = 0)
quigley@16156
   814
                                                                 then 
wenzelm@16260
   815
                                                                    (let val  outfile = TextIO.openOut(File.platform_path(File.tmp_path (Path.basic "foo_reap_found")));
quigley@16156
   816
                                                                         val _ = TextIO.output (outfile, ("Reaping a watcher, goals watched is: "^(string_of_int (!goals_being_watched))^"\n"))
quigley@16156
   817
                                                                         val _ =  TextIO.closeOut outfile
quigley@16156
   818
                                                                     in
quigley@16156
   819
                                                                         killWatcher (childpid); reapWatcher (childpid,childin, childout); ()
quigley@16156
   820
                                                                     end)
quigley@16156
   821
                                                                 else
quigley@16156
   822
                                                                    ()
quigley@16156
   823
                                                            )
quigley@16156
   824
                                                    (* if there's no proof, but a message from Spass *)
quigley@16156
   825
                                                    else if ((substring (reconstr, 0,5))= "SPASS")
quigley@16156
   826
                                                         then
quigley@16156
   827
                                                                 (
quigley@16156
   828
                                                                     goals_being_watched := (!goals_being_watched) -1;
quigley@16156
   829
                                                                     Pretty.writeln(Pretty.str ( (concat[(oct_char "360"), (oct_char "377")])));
quigley@16156
   830
                                                                      Pretty.writeln(Pretty.str (goalstring^reconstr));
quigley@16156
   831
                                                                      Pretty.writeln(Pretty.str  (oct_char "361"));
quigley@16156
   832
                                                                      if (!goals_being_watched = 0)
quigley@16156
   833
                                                                      then 
wenzelm@16260
   834
                                                                          (let val  outfile = TextIO.openOut(File.platform_path(File.tmp_path (Path.basic "foo_reap_comp")));
quigley@16156
   835
                                                                               val _ = TextIO.output (outfile, ("Reaping a watcher, goals watched is: "^(string_of_int (!goals_being_watched))^"\n"))
quigley@16156
   836
                                                                               val _ =  TextIO.closeOut outfile
quigley@16156
   837
                                                                           in
quigley@16156
   838
                                                                              killWatcher (childpid); reapWatcher (childpid,childin, childout); ()
quigley@16156
   839
                                                                           end )
quigley@16156
   840
                                                                      else
quigley@16156
   841
                                                                         ()
quigley@16156
   842
                                                                ) 
quigley@16156
   843
						   (* print out a list of rules used from clasimpset*)
quigley@16156
   844
                                                    else if ((substring (reconstr, 0,5))= "Rules")
quigley@16156
   845
                                                         then
quigley@16156
   846
                                                                 (
quigley@16156
   847
                                                                     goals_being_watched := (!goals_being_watched) -1;
quigley@16156
   848
                                                                     Pretty.writeln(Pretty.str ( (concat[(oct_char "360"), (oct_char "377")])));
quigley@16156
   849
                                                                      Pretty.writeln(Pretty.str (goalstring^reconstr));
quigley@16156
   850
                                                                      Pretty.writeln(Pretty.str  (oct_char "361"));
quigley@16156
   851
                                                                      if (!goals_being_watched = 0)
quigley@16156
   852
                                                                      then 
wenzelm@16260
   853
                                                                          (let val  outfile = TextIO.openOut(File.platform_path(File.tmp_path (Path.basic "foo_reap_comp")));
quigley@16156
   854
                                                                               val _ = TextIO.output (outfile, ("Reaping a watcher, goals watched is: "^(string_of_int (!goals_being_watched))^"\n"))
quigley@16156
   855
                                                                               val _ =  TextIO.closeOut outfile
quigley@16156
   856
                                                                           in
quigley@16156
   857
                                                                              killWatcher (childpid);  reapWatcher (childpid,childin, childout);()
quigley@16156
   858
                                                                           end )
quigley@16156
   859
                                                                      else
quigley@16156
   860
                                                                         ()
quigley@16156
   861
                                                                )
quigley@16156
   862
							
quigley@16156
   863
                                                          (* if proof translation failed *)
quigley@16156
   864
                                                          else if ((substring (reconstr, 0,5)) = "Proof")
quigley@16156
   865
                                                               then 
quigley@16156
   866
                                                                   (
quigley@16156
   867
                                                                     goals_being_watched := (!goals_being_watched) -1;
quigley@16156
   868
                                                                     Pretty.writeln(Pretty.str ( (concat[(oct_char "360"), (oct_char "377")])));
quigley@16357
   869
                                                                      Pretty.writeln(Pretty.str (goalstring^(Recon_Transfer.restore_linebreaks reconstr)));
quigley@16156
   870
                                                                      Pretty.writeln(Pretty.str  (oct_char "361"));
quigley@16156
   871
                                                                      if (!goals_being_watched = 0)
quigley@16156
   872
                                                                      then 
quigley@16357
   873
                                                                          (let val  outfile = TextIO.openOut(File.platform_path (File.tmp_path (Path.basic "foo_reap_comp")));
quigley@16156
   874
                                                                               val _ = TextIO.output (outfile, ("Reaping a watcher, goals watched is: "^(string_of_int (!goals_being_watched))^"\n"))
quigley@16156
   875
                                                                               val _ =  TextIO.closeOut outfile
quigley@16156
   876
                                                                           in
quigley@16156
   877
                                                                              killWatcher (childpid); reapWatcher (childpid,childin, childout); ()
quigley@16156
   878
                                                                           end )
quigley@16156
   879
                                                                      else
quigley@16156
   880
                                                                         ()
quigley@16156
   881
                                                                )
quigley@15642
   882
quigley@15642
   883
quigley@16156
   884
                                                                else  (* add something here ?*)
quigley@16156
   885
                                                                   ()
quigley@16156
   886
                                                             
quigley@16156
   887
                                       end)
quigley@16156
   888
quigley@16156
   889
quigley@16156
   890
                        
quigley@16156
   891
                       in IsaSignal.signal (IsaSignal.usr1, IsaSignal.SIG_HANDLE vampire_proofHandler);
quigley@16156
   892
                          IsaSignal.signal (IsaSignal.usr2, IsaSignal.SIG_HANDLE spass_proofHandler);
quigley@16156
   893
                          (childin, childout, childpid)
quigley@16156
   894
quigley@15642
   895
paulson@16100
   896
  end
quigley@15642
   897
quigley@15642
   898
quigley@15642
   899
quigley@15642
   900
quigley@15642
   901
quigley@15642
   902
end (* structure Watcher *)