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