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