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