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