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