src/HOL/Tools/ATP/watcher.ML
author paulson
Mon Sep 19 15:12:13 2005 +0200 (2005-09-19)
changeset 17484 f6a225f97f0a
parent 17435 0eed5a1c00c1
child 17488 67376a311a2b
permissions -rw-r--r--
simplification of the Isabelle-ATP code; hooks for batch generation of problems
     1 (*  Title:      Watcher.ML
     2     ID:         $Id$
     3     Author:     Claire Quigley
     4     Copyright   2004  University of Cambridge
     5  *)
     6 
     7  (***************************************************************************)
     8  (*  The watcher process starts a resolution process when it receives a     *)
     9 (*  message from Isabelle                                                  *)
    10 (*  Signals Isabelle, puts output of child into pipe to Isabelle,          *)
    11 (*  and removes dead processes.  Also possible to kill all the resolution  *)
    12 (*  processes currently running.                                           *)
    13 (*  Hardwired version of where to pick up the tptp files for the moment    *)
    14 (***************************************************************************)
    15 
    16 signature WATCHER =
    17 sig
    18 
    19 (*****************************************************************************************)
    20 (*  Send request to Watcher for multiple spasses to be called for filenames in arg       *)
    21 (*  callResProvers (outstreamtoWatcher, prover name,prover-command, (settings,file) list             *)
    22 (*****************************************************************************************)
    23 
    24 val callResProvers :
    25     TextIO.outstream * (string*string*string*string*string) list 
    26     -> unit
    27 
    28 (************************************************************************)
    29 (* Send message to watcher to kill currently running resolution provers *)
    30 (************************************************************************)
    31 
    32 val callSlayer : TextIO.outstream -> unit
    33 
    34 (**********************************************************)
    35 (* Start a watcher and set up signal handlers             *)
    36 (**********************************************************)
    37 
    38 val createWatcher : 
    39     thm * (ResClause.clause * thm) Array.array -> 
    40     TextIO.instream * TextIO.outstream * Posix.Process.pid
    41 
    42 (**********************************************************)
    43 (* Kill watcher process                                   *)
    44 (**********************************************************)
    45 
    46 val killWatcher : Posix.Process.pid -> unit
    47 
    48 end
    49 
    50 
    51 
    52 structure Watcher: WATCHER =
    53 struct
    54 
    55 open ReconPrelim Recon_Transfer
    56 
    57 val goals_being_watched = ref 0;
    58 
    59 (*****************************************)
    60 (*  The result of calling createWatcher  *)
    61 (*****************************************)
    62 
    63 datatype proc = PROC of {
    64         pid : Posix.Process.pid,
    65         instr : TextIO.instream,
    66         outstr : TextIO.outstream
    67       };
    68 
    69 (*****************************************)
    70 (*  The result of calling executeToList  *)
    71 (*****************************************)
    72 
    73 datatype cmdproc = CMDPROC of {
    74         prover: string,             (* Name of the resolution prover used, e.g. Vampire, SPASS *)
    75         cmd:  string,              (* The file containing the goal for res prover to prove *)     
    76         goalstring: string,         (* string representation of subgoal*) 
    77         proc_handle : (TextIO.instream,TextIO.outstream) Unix.proc,
    78         instr : TextIO.instream,   (*  Input stream to child process *)
    79         outstr : TextIO.outstream  (*  Output stream from child process *)
    80       };
    81 
    82 type signal = Posix.Signal.signal
    83 datatype exit_status = datatype Posix.Process.exit_status
    84 
    85 val fromStatus = Posix.Process.fromStatus
    86 
    87 
    88 fun reap(pid, instr, outstr) =
    89   let val u = TextIO.closeIn instr;
    90       val u = TextIO.closeOut outstr;
    91       val (_, status) = Posix.Process.waitpid(Posix.Process.W_CHILD pid, [])
    92   in
    93 	  status
    94   end
    95 
    96 fun fdReader (name : string, fd : Posix.IO.file_desc) =
    97 	  Posix.IO.mkTextReader {initBlkMode = true,name = name,fd = fd };
    98 
    99 fun fdWriter (name, fd) =
   100           Posix.IO.mkTextWriter {
   101 	      appendMode = false,
   102               initBlkMode = true,
   103               name = name,  
   104               chunkSize=4096,
   105               fd = fd};
   106 
   107 fun openOutFD (name, fd) =
   108 	  TextIO.mkOutstream (
   109 	    TextIO.StreamIO.mkOutstream (
   110 	      fdWriter (name, fd), IO.BLOCK_BUF));
   111 
   112 fun openInFD (name, fd) =
   113 	  TextIO.mkInstream (
   114 	    TextIO.StreamIO.mkInstream (
   115 	      fdReader (name, fd), ""));
   116 
   117 fun killChild child_handle = Unix.reap child_handle 
   118 
   119 fun childInfo (PROC{pid,instr,outstr }) = (pid,(instr,outstr));
   120 
   121 fun cmdstreamsOf (CMDPROC{instr,outstr,...}) = (instr, outstr);
   122 
   123 fun cmdInStream (CMDPROC{instr,outstr,...}) = instr;
   124 
   125 fun cmdchildInfo (CMDPROC{prover,cmd,proc_handle,goalstring,instr,outstr}) = 
   126   (prover,(cmd, (instr,outstr)));
   127 
   128 fun cmdchildHandle (CMDPROC{prover,cmd,goalstring,proc_handle,instr,outstr})  = 
   129   proc_handle;
   130 
   131 fun cmdProver (CMDPROC{prover,cmd,goalstring,proc_handle,instr,outstr})  =
   132   prover;
   133 
   134 fun cmdGoal (CMDPROC{prover,cmd,goalstring,proc_handle,instr,outstr})  =
   135   goalstring;
   136 
   137 
   138 (********************************************************************************)
   139 (*    gets individual args from instream and concatenates them into a list      *)
   140 (********************************************************************************)
   141 
   142 fun getArgs (fromParentStr, toParentStr, ys) =  
   143   let val thisLine = TextIO.input fromParentStr
   144   in ys@[thisLine] end
   145 
   146                             
   147 (********************************************************************************)
   148 (*  Send request to Watcher for a vampire to be called for filename in arg      *)
   149 (********************************************************************************)
   150                     
   151 fun callResProver (toWatcherStr,  arg) = 
   152       (TextIO.output (toWatcherStr, arg^"\n"); 
   153        TextIO.flushOut toWatcherStr)
   154 
   155 (*****************************************************************************************)
   156 (*  Send request to Watcher for multiple provers to be called for filenames in arg       *)
   157 (*  need to do the dfg stuff in the watcher, not here! send over the clasimp and stuff files too*)
   158 (*****************************************************************************************)
   159 
   160     
   161 (*Uses the $-character to separate items sent to watcher*)
   162 fun callResProvers (toWatcherStr,  []) = 
   163       (TextIO.output (toWatcherStr, "End of calls\n");  TextIO.flushOut toWatcherStr)
   164 |   callResProvers (toWatcherStr,
   165                     (prover,goalstring, proverCmd,settings, 
   166                      probfile)  ::  args) =
   167       let val _ = File.write (File.tmp_path (Path.basic "tog_comms"))
   168                              (prover^goalstring^proverCmd^settings^
   169                               probfile)
   170       in TextIO.output (toWatcherStr,    
   171             (prover^"$"^goalstring^"$"^proverCmd^"$"^
   172              settings^"$"^probfile^"\n"));
   173          goals_being_watched := (!goals_being_watched) + 1;
   174 	 TextIO.flushOut toWatcherStr;
   175 	 callResProvers (toWatcherStr,args)
   176       end   
   177                                                 
   178                                     
   179  
   180 (**************************************************************)
   181 (* Send message to watcher to kill currently running vampires *)
   182 (**************************************************************)
   183 
   184 fun callSlayer toWatcherStr = (TextIO.output (toWatcherStr, "Kill vampires\n"); 
   185                             TextIO.flushOut toWatcherStr)
   186 
   187 
   188 
   189 (**************************************************************)
   190 (* Remove \n token from a vampire goal filename and extract   *)
   191 (* prover, proverCmd, settings and file from input string     *)
   192 (**************************************************************)
   193 
   194 fun separateFields str =
   195   let val _ = File.append (File.tmp_path (Path.basic "sep_field"))
   196                           ("In separateFields, str is: " ^ str ^ "\n\n") 
   197       val [prover,goalstring,proverCmd,settingstr,probfile] = 
   198           String.tokens (fn c => c = #"$") str
   199       val settings = String.tokens (fn c => c = #"%") settingstr
   200       val _ = File.append (File.tmp_path (Path.basic "sep_comms"))                                                                                
   201                   ("Sep comms are: "^ str ^"**"^
   202                    prover^" goalstr:  "^goalstring^
   203                    "\n provercmd: "^proverCmd^
   204                    "\n prob  "^probfile^"\n\n")
   205   in
   206      (prover,goalstring, proverCmd, settings, probfile)
   207   end
   208 
   209 val remove_newlines = String.translate (fn c => if c = #"\n" then "" else str c);
   210 
   211 fun getCmd cmdStr = 
   212   let val cmdStr' = remove_newlines cmdStr
   213   in
   214       File.write (File.tmp_path (Path.basic"sepfields_call")) 
   215 		 ("about to call separateFields with " ^ cmdStr');
   216       separateFields cmdStr'
   217   end;
   218                       
   219 (**************************************************************)
   220 (* Get commands from Isabelle                                 *)
   221 (**************************************************************)
   222 
   223 fun getCmds (toParentStr,fromParentStr, cmdList) = 
   224   let val thisLine = TextIO.inputLine fromParentStr 
   225       val _ = File.append (File.tmp_path (Path.basic "parent_comms")) thisLine
   226   in
   227      if thisLine = "End of calls\n" then cmdList
   228      else if thisLine = "Kill children\n"
   229      then 
   230 	 (   TextIO.output (toParentStr,thisLine ); 
   231 	     TextIO.flushOut toParentStr;
   232 	   (("","","Kill children",[],"")::cmdList)
   233 	  )
   234      else let val thisCmd = getCmd thisLine 
   235 	       (*********************************************************)
   236 	       (* thisCmd = (prover,proverCmd, settings, file)*)
   237 	       (* i.e. put back into tuple format                       *)
   238 	       (*********************************************************)
   239 	   in
   240 	     (*TextIO.output (toParentStr, thisCmd); 
   241 	       TextIO.flushOut toParentStr;*)
   242 	       getCmds (toParentStr, fromParentStr, thisCmd::cmdList)
   243 	   end
   244   end
   245 	    
   246                                                                   
   247 (**************************************************************)
   248 (*  Get Io-descriptor for polling of an input stream          *)
   249 (**************************************************************)
   250 
   251 fun getInIoDesc someInstr = 
   252     let val (rd, buf) = TextIO.StreamIO.getReader(TextIO.getInstream someInstr)
   253         val _ = TextIO.output (TextIO.stdOut, buf)
   254         val ioDesc = 
   255 	    case rd
   256 	      of TextPrimIO.RD{ioDesc = SOME iod, ...} =>SOME iod
   257 	       | _ => NONE
   258      in (* since getting the reader will have terminated the stream, we need
   259 	 * to build a new stream. *)
   260 	TextIO.setInstream(someInstr, TextIO.StreamIO.mkInstream(rd, buf));
   261 	ioDesc
   262     end
   263 
   264 
   265 (*************************************)
   266 (*  Set up a Watcher Process         *)
   267 (*************************************)
   268 
   269 fun getProofCmd (a,c,d,e,f) = d
   270 
   271 fun prems_string_of th =
   272   Meson.concat_with_and (map (Sign.string_of_term (sign_of_thm th)) (prems_of th))
   273 
   274 fun setupWatcher (thm,clause_arr) = 
   275   let
   276     (** pipes for communication between parent and watcher **)
   277     val p1 = Posix.IO.pipe ()
   278     val p2 = Posix.IO.pipe ()
   279     fun closep () = 
   280 	 (Posix.IO.close (#outfd p1); 
   281 	  Posix.IO.close (#infd p1);
   282 	  Posix.IO.close (#outfd p2); 
   283 	  Posix.IO.close (#infd p2))
   284     (***********************************************************)
   285     (****** fork a watcher process and get it set up and going *)
   286     (***********************************************************)
   287     fun startWatcher procList =
   288      (case  Posix.Process.fork() (***************************************)
   289       of SOME pid =>  pid           (* parent - i.e. main Isabelle process *)
   290 				    (***************************************)
   291 
   292 				      (*************************)
   293        | NONE => let                  (* child - i.e. watcher  *)
   294 	   val oldchildin = #infd p1  (*************************)
   295 	   val fromParent = Posix.FileSys.wordToFD 0w0
   296 	   val oldchildout = #outfd p2
   297 	   val toParent = Posix.FileSys.wordToFD 0w1
   298 	   val fromParentIOD = Posix.FileSys.fdToIOD fromParent
   299 	   val fromParentStr = openInFD ("_exec_in_parent", fromParent)
   300 	   val toParentStr = openOutFD ("_exec_out_parent", toParent)
   301 	   val _ = debug ("subgoals forked to startWatcher: "^ prems_string_of thm);
   302 	   fun killChildren [] = ()
   303 	|      killChildren  (child_handle::children) =
   304 	         (killChild child_handle; killChildren children)
   305 
   306 	 (*************************************************************)
   307 	 (* take an instream and poll its underlying reader for input *)
   308 	 (*************************************************************)
   309 
   310 	 fun pollParentInput () = 
   311 	   let val pd = OS.IO.pollDesc fromParentIOD
   312 	   in 
   313 	     if isSome pd then 
   314 		 let val pd' = OS.IO.pollIn (valOf pd)
   315 		     val pdl = OS.IO.poll ([pd'], SOME (Time.fromMilliseconds 2000)) 
   316 		     val _ = File.append (File.tmp_path (Path.basic "parent_poll")) 
   317 		       ("In parent_poll\n")	
   318 		 in
   319 		    if null pdl 
   320 		    then
   321 		       NONE
   322 		    else if (OS.IO.isIn (hd pdl)) 
   323 			 then SOME (getCmds (toParentStr, fromParentStr, []))
   324 			 else NONE
   325 		 end
   326 	       else NONE
   327 	   end
   328 		 
   329 	  fun pollChildInput fromStr = 
   330 	   let val _ = File.append (File.tmp_path (Path.basic "child_poll")) 
   331 			 ("In child_poll\n")
   332 	       val iod = getInIoDesc fromStr
   333 	   in 
   334 	     if isSome iod 
   335 	     then 
   336 	       let val pd = OS.IO.pollDesc (valOf iod)
   337 	       in 
   338 	       if isSome pd then 
   339 		   let val pd' = OS.IO.pollIn (valOf pd)
   340 		       val pdl = OS.IO.poll ([pd'], SOME (Time.fromMilliseconds 2000)) 
   341 		   in
   342 		      if null pdl 
   343 		      then
   344 			(File.append (File.tmp_path (Path.basic"child_poll_res")) "Null pdl\n"; 
   345 			 NONE)
   346 		      else if OS.IO.isIn (hd pdl)
   347 		      then
   348 			   let val inval =  (TextIO.inputLine fromStr)
   349 			       val _ = File.append (File.tmp_path (Path.basic "child_poll_res")) (inval^"\n")
   350 			   in SOME inval end
   351 		      else
   352                         (File.append (File.tmp_path (Path.basic"child_poll_res")) "Null pdl \n";
   353                          NONE)
   354 		   end
   355 		 else NONE
   356 		 end
   357 	     else NONE
   358 	end
   359 
   360 
   361 	(****************************************************************************)
   362 	(* Check all vampire processes currently running for output                 *)
   363 	(****************************************************************************) 
   364 						   (*********************************)
   365 	 fun checkChildren ([], toParentStr) = []  (*** no children to check ********)
   366 						   (*********************************)
   367 	 |   checkChildren ((childProc::otherChildren), toParentStr) = 
   368 	       let val _ = File.append (File.tmp_path (Path.basic "child_check")) 
   369 			      ("In check child, length of queue:"^
   370 			       string_of_int (length (childProc::otherChildren)))
   371 		   val (childInput,childOutput) =  cmdstreamsOf childProc
   372 		   val child_handle= cmdchildHandle childProc
   373 		   (* childCmd is the .dfg file that the problem is in *)
   374 		   val childCmd = fst(snd (cmdchildInfo childProc))
   375 		   val _ = File.append (File.tmp_path (Path.basic "child_check")) 
   376 			      ("\nchildCmd = " ^ childCmd)
   377 		   (* now get the number of the subgoal from the filename *)
   378 		   val path_parts = String.tokens(fn c => c = #"/") childCmd
   379 		   val digits = String.translate 
   380 		                  (fn c => if Char.isDigit c then str c else "")
   381 		                  (List.last path_parts);
   382 		   val sg_num =
   383 		     (case Int.fromString digits of SOME n => n
   384                         | NONE => (File.append (File.tmp_path (Path.basic "child_check")) 
   385                                    "\nWatcher could not read subgoal nunber!!";
   386                                    raise ERROR));
   387 		   val childIncoming = pollChildInput childInput
   388 		   val _ = File.append (File.tmp_path (Path.basic "child_check")) 
   389 			         "\nfinished polling child"
   390 		   val parentID = Posix.ProcEnv.getppid()
   391 		   val prover = cmdProver childProc
   392 		   val prems_string = prems_string_of thm
   393 		   val goalstring = cmdGoal childProc							
   394 		   val _ =  File.append (File.tmp_path (Path.basic "child_check")) 
   395 		             ("\nsubgoals forked to checkChildren: " ^ 
   396 		              space_implode "\n" [prems_string,prover,goalstring])
   397 	       in 
   398 		 if isSome childIncoming
   399 		 then 
   400 		   (* check here for prover label on child*)
   401 		   let val _ = File.append (File.tmp_path (Path.basic "child_check")) 
   402 			         "\nInput available from childIncoming" 
   403 		       val childDone = (case prover of
   404 			   "vampire" => AtpCommunication.checkVampProofFound(childInput, toParentStr, parentID,goalstring, childCmd, thm, sg_num,clause_arr)  
   405 			    | "E" => AtpCommunication.checkEProofFound(childInput, toParentStr, parentID,goalstring, childCmd, thm, sg_num,clause_arr)             
   406 			  |"spass" => AtpCommunication.checkSpassProofFound(childInput, toParentStr, parentID,goalstring, childCmd, thm, sg_num,clause_arr)  )
   407 		    in
   408 		     if childDone
   409 		     then (* child has found a proof and transferred it *)
   410 			(* Remove this child and go on to check others*)
   411 			(**********************************************)
   412 			(Unix.reap child_handle;
   413 			 checkChildren(otherChildren, toParentStr))
   414 		     else 
   415 			(**********************************************)
   416 			(* Keep this child and go on to check others  *)
   417 			(**********************************************)
   418 		       (childProc::(checkChildren (otherChildren, toParentStr)))
   419 		  end
   420 		else
   421 		  (File.append (File.tmp_path (Path.basic "child_check"))
   422 		               "No child output";
   423 		   childProc::(checkChildren (otherChildren, toParentStr)))
   424 	       end
   425 
   426 	
   427      (********************************************************************)                  
   428      (* call resolution processes                                        *)
   429      (* settings should be a list of strings                             *)
   430      (* e.g. ["-t 300", "-m 100000"]         (TextIO.input instr)^                            *)
   431      (* takes list of (string, string, string list, string)list proclist *)
   432      (********************************************************************)
   433 
   434 
   435 (*** add subgoal id num to this *)
   436 	fun execCmds  [] procList = procList
   437 	|   execCmds  ((prover, goalstring,proverCmd,settings,file)::cmds) procList  =
   438 	      let val _ = File.write (File.tmp_path (Path.basic "exec_child"))  
   439 	                    (space_implode "\n" 
   440 	                      (["About to execute command for goal:",
   441 	                        goalstring, proverCmd] @ settings @
   442 	                       [file, Date.toString(Date.fromTimeLocal(Time.now()))]))
   443 	          val childhandle:(TextIO.instream,TextIO.outstream) Unix.proc  = 
   444 		       (Unix.execute(proverCmd, (settings@[file])))
   445 		  val (instr, outstr) = Unix.streamsOf childhandle
   446 		  
   447 		  val newProcList = (CMDPROC{
   448 				       prover = prover,
   449 				       cmd = file,
   450 				       goalstring = goalstring,
   451 				       proc_handle = childhandle,
   452 				       instr = instr,
   453 				       outstr = outstr }) :: procList
   454      
   455 		  val _ = File.append (File.tmp_path (Path.basic "exec_child")) 
   456 			    ("\nFinished at " ^
   457 			     Date.toString(Date.fromTimeLocal(Time.now())))
   458 	      in
   459 		Posix.Process.sleep (Time.fromSeconds 1); 
   460 		execCmds cmds newProcList
   461 	      end
   462 
   463 
   464      (****************************************)                  
   465      (* call resolution processes remotely   *)
   466      (* settings should be a list of strings *)
   467      (* e.g. ["-t300", "-m100000"]         *)
   468      (****************************************)
   469 
   470       (*  fun execRemoteCmds  [] procList = procList
   471 	|   execRemoteCmds ((prover, goalstring,proverCmd ,settings,file)::cmds) procList  =  
   472 	      let val  newProcList =  mySshExecuteToList ("/usr/bin/ssh",([prover,goalstring,"-t", "shep"]@[proverCmd]@settings@[file]), procList)
   473 		  in
   474 		       execRemoteCmds  cmds newProcList
   475 		  end
   476 *)
   477 
   478      (**********************************************)                  
   479      (* Watcher Loop                               *)
   480      (**********************************************)
   481          val iterations_left = ref 1000;  (*200 seconds, 5 iterations per sec*)
   482 
   483 	 fun keepWatching (toParentStr, fromParentStr,procList) = 
   484 	   let fun loop procList =  
   485 		let val _ = Posix.Process.sleep (Time.fromMilliseconds 200)
   486 		    val cmdsFromIsa = pollParentInput ()
   487 		    fun killchildHandler ()  = 
   488 			  (TextIO.output(toParentStr, "Timeout: Killing proof processes!\n");
   489 			   TextIO.flushOut toParentStr;
   490 			   killChildren (map cmdchildHandle procList);
   491 			   ())
   492 		in 
   493 		   (*Signal.signal (Posix.Signal.usr2, Signal.SIG_HANDLE killchildHandler);*)
   494 		   iterations_left := !iterations_left - 1;
   495 		   if !iterations_left = 0 then killchildHandler ()
   496 		   else if isSome cmdsFromIsa
   497 		   then (*  deal with input from Isabelle *)
   498 		     if getProofCmd(hd(valOf cmdsFromIsa)) = "Kill children" 
   499 		     then 
   500 		       let val child_handles = map cmdchildHandle procList 
   501 		       in
   502 			  killChildren child_handles;
   503 			  (*Posix.Process.kill(Posix.Process.K_PROC (Posix.ProcEnv.getppid()), Posix.Signal.usr2);*)    
   504 			  loop []
   505 		       end
   506 		     else
   507 		       if length procList < 5     (********************)
   508 		       then                        (* Execute locally  *)
   509 			 let 
   510 			   val newProcList = execCmds (valOf cmdsFromIsa) procList
   511 			   val parentID = Posix.ProcEnv.getppid()
   512 			      val _ = File.append (File.tmp_path (Path.basic "prekeep_watch"))
   513 			      "Just execed a child\n"
   514 			   val newProcList' = checkChildren (newProcList, toParentStr) 
   515 			 in
   516 			   File.append (File.tmp_path (Path.basic "keep_watch")) 
   517 			       "Off to keep watching...\n";
   518 			   loop newProcList'
   519 			 end
   520 		       else  (* Execute remotely              *)
   521 			     (* (actually not remote for now )*)
   522 			 let 
   523 			   val newProcList = execCmds (valOf cmdsFromIsa) procList
   524 			   val parentID = Posix.ProcEnv.getppid()
   525 			   val newProcList' =checkChildren (newProcList, toParentStr) 
   526 			 in
   527 			   loop newProcList'
   528 			 end
   529 		   else (* No new input from Isabelle *)
   530 		     let val newProcList = checkChildren (procList, toParentStr)
   531 		     in
   532 		      (File.append (File.tmp_path (Path.basic "keep_watch")) "Off to keep watching...2\n ");
   533 		       loop newProcList
   534 		     end
   535 		 end
   536 	   in  
   537 	       loop procList
   538 	   end
   539 	   
   540 
   541 	   in
   542 	    (***************************)
   543 	    (*** Sort out pipes ********)
   544 	    (***************************)
   545 
   546 	     Posix.IO.close (#outfd p1);
   547 	     Posix.IO.close (#infd p2);
   548 	     Posix.IO.dup2{old = oldchildin, new = fromParent};
   549 	     Posix.IO.close oldchildin;
   550 	     Posix.IO.dup2{old = oldchildout, new = toParent};
   551 	     Posix.IO.close oldchildout;
   552 
   553 	     (***************************)
   554 	     (* start the watcher loop  *)
   555 	     (***************************)
   556 	     keepWatching (toParentStr, fromParentStr, procList);
   557 	     (****************************************************************************)
   558 (* fake return value - actually want the watcher to loop until killed *)
   559 	     (****************************************************************************)
   560 	     Posix.Process.wordToPid 0w0
   561 	   end);
   562      (* end case *)
   563 
   564 
   565     val _ = TextIO.flushOut TextIO.stdOut
   566 
   567     (*******************************)
   568     (***  set watcher going ********)
   569     (*******************************)
   570 
   571     val procList = []
   572     val pid = startWatcher procList
   573     (**************************************************)
   574     (* communication streams to watcher               *)
   575     (**************************************************)
   576 
   577     val instr = openInFD ("_exec_in", #infd p2)
   578     val outstr = openOutFD ("_exec_out", #outfd p1)
   579     
   580     in
   581      (*******************************)
   582      (* close the child-side fds    *)
   583      (*******************************)
   584       Posix.IO.close (#outfd p2);
   585       Posix.IO.close (#infd p1);
   586       (* set the fds close on exec *)
   587       Posix.IO.setfd (#infd p2, Posix.IO.FD.flags [Posix.IO.FD.cloexec]);
   588       Posix.IO.setfd (#outfd p1, Posix.IO.FD.flags [Posix.IO.FD.cloexec]);
   589        
   590      (*******************************)
   591      (* return value                *)
   592      (*******************************)
   593       PROC{pid = pid,
   594 	instr = instr,
   595 	outstr = outstr
   596       }
   597    end;
   598 
   599 
   600 
   601 (**********************************************************)
   602 (* Start a watcher and set up signal handlers             *)
   603 (**********************************************************)
   604 
   605 fun killWatcher pid = Posix.Process.kill(Posix.Process.K_PROC pid, Posix.Signal.kill);
   606 
   607 fun reapWatcher(pid, instr, outstr) =
   608   (TextIO.closeIn instr; TextIO.closeOut outstr;
   609    Posix.Process.waitpid(Posix.Process.W_CHILD pid, []); ())
   610 
   611 fun createWatcher (thm, clause_arr) =
   612  let val (childpid,(childin,childout)) = childInfo (setupWatcher(thm,clause_arr))
   613      fun decr_watched() =
   614 	  (goals_being_watched := (!goals_being_watched) - 1;
   615 	   if !goals_being_watched = 0
   616 	   then 
   617 	     (File.append (File.tmp_path (Path.basic "reap_found"))
   618 	       ("Reaping a watcher, childpid = "^
   619 		LargeWord.toString (Posix.Process.pidToWord childpid)^"\n");
   620 	      killWatcher childpid; reapWatcher (childpid,childin, childout))
   621 	    else ())
   622      val _ = debug ("subgoals forked to createWatcher: "^ prems_string_of thm);
   623      fun proofHandler n = 
   624        let val outcome = TextIO.inputLine childin
   625 	   val goalstring = TextIO.inputLine childin
   626 	   val _ = debug ("In signal handler. outcome = \"" ^ outcome ^ 
   627 		        "\"\ngoalstring = " ^ goalstring ^
   628 		        "\ngoals_being_watched: "^ string_of_int (!goals_being_watched))
   629        in (* if a proof has been found and sent back as a reconstruction proof *)
   630 	 if String.isPrefix "[" outcome
   631 	 then (Pretty.writeln(Pretty.str ( (concat[(oct_char "360"), (oct_char "377")])));
   632 	       Recon_Transfer.apply_res_thm outcome goalstring;
   633 	       Pretty.writeln(Pretty.str  (oct_char "361"));
   634 	       decr_watched())
   635 	 (* if there's no proof, but a message from the signalling process*)
   636 	 else if String.isPrefix "Invalid" outcome
   637 	 then (Pretty.writeln(Pretty.str ( (concat[(oct_char "360"), (oct_char "377")])));
   638 	       Pretty.writeln(Pretty.str ((Recon_Transfer.restore_linebreaks goalstring) 
   639 	       ^ "is not provable"));
   640 	       Pretty.writeln(Pretty.str  (oct_char "361"));
   641 	       decr_watched())
   642 	 else if String.isPrefix "Failure" outcome
   643 	 then (Pretty.writeln(Pretty.str ( (concat[(oct_char "360"), (oct_char "377")])));
   644 	       Pretty.writeln(Pretty.str ((Recon_Transfer.restore_linebreaks goalstring) 
   645 	       ^ "proof attempt failed"));
   646 	       Pretty.writeln(Pretty.str  (oct_char "361"));
   647 	       decr_watched()) 
   648 	(* print out a list of rules used from clasimpset*)
   649 	 else if String.isPrefix "Success" outcome
   650 	 then (Pretty.writeln(Pretty.str ( (concat[(oct_char "360"), (oct_char "377")])));
   651 	       Pretty.writeln(Pretty.str (goalstring^outcome));
   652 	       Pretty.writeln(Pretty.str  (oct_char "361"));
   653 	       decr_watched())
   654 	  (* if proof translation failed *)
   655 	 else if String.isPrefix "Translation failed" outcome
   656 	 then (Pretty.writeln(Pretty.str ( (concat[(oct_char "360"), (oct_char "377")])));
   657 	       Pretty.writeln(Pretty.str (goalstring^(Recon_Transfer.restore_linebreaks outcome)));
   658 	       Pretty.writeln(Pretty.str  (oct_char "361"));
   659 	       decr_watched())
   660 	 else  
   661 	      (Pretty.writeln(Pretty.str ( (concat[(oct_char "360"), (oct_char "377")])));
   662 	       Pretty.writeln(Pretty.str ("System error in proof handler"));
   663 	       Pretty.writeln(Pretty.str  (oct_char "361"));
   664 	       decr_watched())
   665        end
   666  in IsaSignal.signal (IsaSignal.usr2, IsaSignal.SIG_HANDLE proofHandler);
   667     (childin, childout, childpid)
   668   end
   669 
   670 end (* structure Watcher *)