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