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