| author | paulson | 
| Wed, 12 Oct 2005 10:49:07 +0200 | |
| changeset 17841 | b1f10b98430d | 
| parent 17819 | 1241e5d31d5b | 
| child 18680 | 677e2bdd75f0 | 
| permissions | -rw-r--r-- | 
| 15789 
4cb16144c81b
added hearder lines and deleted some redundant material
 paulson parents: 
15787diff
changeset | 1 | (* Title: Watcher.ML | 
| 
4cb16144c81b
added hearder lines and deleted some redundant material
 paulson parents: 
15787diff
changeset | 2 | ID: $Id$ | 
| 
4cb16144c81b
added hearder lines and deleted some redundant material
 paulson parents: 
15787diff
changeset | 3 | Author: Claire Quigley | 
| 
4cb16144c81b
added hearder lines and deleted some redundant material
 paulson parents: 
15787diff
changeset | 4 | Copyright 2004 University of Cambridge | 
| 15642 | 5 | *) | 
| 6 | ||
| 17764 | 7 | (* The watcher process starts a resolution process when it receives a *) | 
| 15642 | 8 | (* message from Isabelle *) | 
| 9 | (* Signals Isabelle, puts output of child into pipe to Isabelle, *) | |
| 10 | (* and removes dead processes. Also possible to kill all the resolution *) | |
| 11 | (* processes currently running. *) | |
| 12 | ||
| 17305 
6cef3aedd661
axioms  now included in tptp files, no /bin/cat and various tidying
 paulson parents: 
17235diff
changeset | 13 | signature WATCHER = | 
| 
6cef3aedd661
axioms  now included in tptp files, no /bin/cat and various tidying
 paulson parents: 
17235diff
changeset | 14 | sig | 
| 
6cef3aedd661
axioms  now included in tptp files, no /bin/cat and various tidying
 paulson parents: 
17235diff
changeset | 15 | |
| 
6cef3aedd661
axioms  now included in tptp files, no /bin/cat and various tidying
 paulson parents: 
17235diff
changeset | 16 | (* Send request to Watcher for multiple spasses to be called for filenames in arg *) | 
| 17568 | 17 | (* callResProvers (outstreamtoWatcher, prover name,prover-command, (settings,file) list *) | 
| 17305 
6cef3aedd661
axioms  now included in tptp files, no /bin/cat and various tidying
 paulson parents: 
17235diff
changeset | 18 | |
| 17773 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 19 | val callResProvers : TextIO.outstream * (string*string*string*string) list -> unit | 
| 17305 
6cef3aedd661
axioms  now included in tptp files, no /bin/cat and various tidying
 paulson parents: 
17235diff
changeset | 20 | |
| 17764 | 21 | (* Send message to watcher to kill resolution provers *) | 
| 17305 
6cef3aedd661
axioms  now included in tptp files, no /bin/cat and various tidying
 paulson parents: 
17235diff
changeset | 22 | val callSlayer : TextIO.outstream -> unit | 
| 
6cef3aedd661
axioms  now included in tptp files, no /bin/cat and various tidying
 paulson parents: 
17235diff
changeset | 23 | |
| 
6cef3aedd661
axioms  now included in tptp files, no /bin/cat and various tidying
 paulson parents: 
17235diff
changeset | 24 | (* Start a watcher and set up signal handlers *) | 
| 17484 
f6a225f97f0a
simplification of the Isabelle-ATP code; hooks for batch generation of problems
 paulson parents: 
17435diff
changeset | 25 | val createWatcher : | 
| 
f6a225f97f0a
simplification of the Isabelle-ATP code; hooks for batch generation of problems
 paulson parents: 
17435diff
changeset | 26 | thm * (ResClause.clause * thm) Array.array -> | 
| 
f6a225f97f0a
simplification of the Isabelle-ATP code; hooks for batch generation of problems
 paulson parents: 
17435diff
changeset | 27 | TextIO.instream * TextIO.outstream * Posix.Process.pid | 
| 17305 
6cef3aedd661
axioms  now included in tptp files, no /bin/cat and various tidying
 paulson parents: 
17235diff
changeset | 28 | val killWatcher : Posix.Process.pid -> unit | 
| 17764 | 29 | val setting_sep : char | 
| 17305 
6cef3aedd661
axioms  now included in tptp files, no /bin/cat and various tidying
 paulson parents: 
17235diff
changeset | 30 | end | 
| 
6cef3aedd661
axioms  now included in tptp files, no /bin/cat and various tidying
 paulson parents: 
17235diff
changeset | 31 | |
| 
6cef3aedd661
axioms  now included in tptp files, no /bin/cat and various tidying
 paulson parents: 
17235diff
changeset | 32 | |
| 15642 | 33 | |
| 34 | structure Watcher: WATCHER = | |
| 17484 
f6a225f97f0a
simplification of the Isabelle-ATP code; hooks for batch generation of problems
 paulson parents: 
17435diff
changeset | 35 | struct | 
| 15642 | 36 | |
| 17764 | 37 | (*Field separators, used to pack items onto a text line*) | 
| 38 | val command_sep = #"\t" | |
| 39 | and setting_sep = #"%"; | |
| 40 | ||
| 15642 | 41 | val goals_being_watched = ref 0; | 
| 42 | ||
| 17583 
c272b91b619f
removal of "sleep" to stop looping in Poly/ML, and replacement of funny codes by tracing statements
 paulson parents: 
17568diff
changeset | 43 | val trace_path = Path.basic "watcher_trace"; | 
| 
c272b91b619f
removal of "sleep" to stop looping in Poly/ML, and replacement of funny codes by tracing statements
 paulson parents: 
17568diff
changeset | 44 | |
| 17690 
8ba7c3cd24a8
time limit option; fixed bug concerning first line of ATP output
 paulson parents: 
17583diff
changeset | 45 | fun trace s = if !Output.show_debug_msgs then File.append (File.tmp_path trace_path) s | 
| 
8ba7c3cd24a8
time limit option; fixed bug concerning first line of ATP output
 paulson parents: 
17583diff
changeset | 46 | else (); | 
| 17583 
c272b91b619f
removal of "sleep" to stop looping in Poly/ML, and replacement of funny codes by tracing statements
 paulson parents: 
17568diff
changeset | 47 | |
| 17773 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 48 | (*Representation of a watcher process*) | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 49 | type proc = {pid : Posix.Process.pid,
 | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 50 | instr : TextIO.instream, | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 51 | outstr : TextIO.outstream}; | 
| 16039 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 52 | |
| 17773 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 53 | (*Representation of a child (ATP) process*) | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 54 | type cmdproc = {
 | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 55 | prover: string, (* Name of the resolution prover used, e.g. "spass"*) | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 56 | file: string, (* The file containing the goal for the ATP to prove *) | 
| 16039 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 57 | proc_handle : (TextIO.instream,TextIO.outstream) Unix.proc, | 
| 17773 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 58 | instr : TextIO.instream, (*Output of the child process *) | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 59 | outstr : TextIO.outstream}; (*Input to the child process *) | 
| 16039 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 60 | |
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 61 | |
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 62 | fun fdReader (name : string, fd : Posix.IO.file_desc) = | 
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 63 | 	  Posix.IO.mkTextReader {initBlkMode = true,name = name,fd = fd };
 | 
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 64 | |
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 65 | fun fdWriter (name, fd) = | 
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 66 |           Posix.IO.mkTextWriter {
 | 
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 67 | appendMode = false, | 
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 68 | initBlkMode = true, | 
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 69 | name = name, | 
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 70 | chunkSize=4096, | 
| 17317 
3f12de2e2e6e
Isabelle-ATP link: sortable axiom names; no spaces in switches; general tidying
 paulson parents: 
17315diff
changeset | 71 | fd = fd}; | 
| 16039 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 72 | |
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 73 | fun openOutFD (name, fd) = | 
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 74 | TextIO.mkOutstream ( | 
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 75 | TextIO.StreamIO.mkOutstream ( | 
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 76 | fdWriter (name, fd), IO.BLOCK_BUF)); | 
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 77 | |
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 78 | fun openInFD (name, fd) = | 
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 79 | TextIO.mkInstream ( | 
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 80 | TextIO.StreamIO.mkInstream ( | 
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 81 | fdReader (name, fd), "")); | 
| 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 82 | |
| 15642 | 83 | |
| 17773 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 84 | (* Send request to Watcher for a vampire to be called for filename in arg*) | 
| 17231 | 85 | fun callResProver (toWatcherStr, arg) = | 
| 17317 
3f12de2e2e6e
Isabelle-ATP link: sortable axiom names; no spaces in switches; general tidying
 paulson parents: 
17315diff
changeset | 86 | (TextIO.output (toWatcherStr, arg^"\n"); | 
| 17231 | 87 | TextIO.flushOut toWatcherStr) | 
| 15642 | 88 | |
| 17773 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 89 | (* Send request to Watcher for multiple provers to be called*) | 
| 16475 | 90 | fun callResProvers (toWatcherStr, []) = | 
| 17317 
3f12de2e2e6e
Isabelle-ATP link: sortable axiom names; no spaces in switches; general tidying
 paulson parents: 
17315diff
changeset | 91 | (TextIO.output (toWatcherStr, "End of calls\n"); TextIO.flushOut toWatcherStr) | 
| 17773 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 92 | | callResProvers (toWatcherStr, | 
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 93 | (prover,proverCmd,settings,probfile) :: args) = | 
| 17773 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 94 | (trace (space_implode ", " (["\ncallResProvers:", prover, proverCmd, probfile])); | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 95 | (*Uses a special character to separate items sent to watcher*) | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 96 | TextIO.output (toWatcherStr, | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 97 | space_implode (str command_sep) [prover, proverCmd, settings, probfile, "\n"]); | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 98 | goals_being_watched := (!goals_being_watched) + 1; | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 99 | TextIO.flushOut toWatcherStr; | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 100 | callResProvers (toWatcherStr,args)) | 
| 16357 | 101 | |
| 15642 | 102 | |
| 17764 | 103 | (*Send message to watcher to kill currently running vampires. NOT USED and possibly | 
| 104 | buggy. Note that killWatcher kills the entire process group anyway.*) | |
| 105 | fun callSlayer toWatcherStr = (TextIO.output (toWatcherStr, "Kill children\n"); | |
| 15642 | 106 | TextIO.flushOut toWatcherStr) | 
| 107 | ||
| 17746 | 108 | |
| 17773 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 109 | (* Get commands from Isabelle*) | 
| 17764 | 110 | fun getCmds (toParentStr, fromParentStr, cmdList) = | 
| 16475 | 111 | let val thisLine = TextIO.inputLine fromParentStr | 
| 112 | in | |
| 17764 | 113 |      trace("\nGot command from parent: " ^ thisLine);
 | 
| 17568 | 114 | if thisLine = "End of calls\n" orelse thisLine = "" then cmdList | 
| 17422 | 115 | else if thisLine = "Kill children\n" | 
| 17764 | 116 | then (TextIO.output (toParentStr,thisLine); | 
| 17568 | 117 | TextIO.flushOut toParentStr; | 
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 118 | 	   [("","Kill children",[],"")])
 | 
| 17746 | 119 | else | 
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 120 | let val [prover,proverCmd,settingstr,probfile,_] = | 
| 17764 | 121 | String.tokens (fn c => c = command_sep) thisLine | 
| 122 | val settings = String.tokens (fn c => c = setting_sep) settingstr | |
| 17746 | 123 | in | 
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 124 |            trace ("\nprover: " ^ prover ^ "  prover path: " ^ proverCmd ^
 | 
| 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 125 | "\n problem file: " ^ probfile); | 
| 17746 | 126 | getCmds (toParentStr, fromParentStr, | 
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 127 | (prover, proverCmd, settings, probfile)::cmdList) | 
| 17746 | 128 | end | 
| 17764 | 129 | handle Bind => | 
| 130 | (trace "getCmds: command parsing failed!"; | |
| 131 | getCmds (toParentStr, fromParentStr, cmdList)) | |
| 16475 | 132 | end | 
| 133 | ||
| 16357 | 134 | |
| 17774 | 135 | (*Get Io-descriptor for polling of an input stream*) | 
| 15642 | 136 | fun getInIoDesc someInstr = | 
| 137 | let val (rd, buf) = TextIO.StreamIO.getReader(TextIO.getInstream someInstr) | |
| 15702 
2677db44c795
new signalling primmitives for sml/nj compatibility
 paulson parents: 
15684diff
changeset | 138 | val _ = TextIO.output (TextIO.stdOut, buf) | 
| 15642 | 139 | val ioDesc = | 
| 140 | case rd | |
| 17774 | 141 | 	      of TextPrimIO.RD{ioDesc = SOME iod, ...} => SOME iod
 | 
| 15642 | 142 | | _ => NONE | 
| 143 | in (* since getting the reader will have terminated the stream, we need | |
| 144 | * to build a new stream. *) | |
| 145 | TextIO.setInstream(someInstr, TextIO.StreamIO.mkInstream(rd, buf)); | |
| 146 | ioDesc | |
| 147 | end | |
| 148 | ||
| 17774 | 149 | fun pollChild fromStr = | 
| 150 | case getInIoDesc fromStr of | |
| 151 | SOME iod => | |
| 152 | (case OS.IO.pollDesc iod of | |
| 153 | SOME pd => | |
| 154 | let val pd' = OS.IO.pollIn pd in | |
| 155 | case OS.IO.poll ([pd'], SOME (Time.fromSeconds 2)) of | |
| 156 | [] => false | |
| 157 | | pd''::_ => OS.IO.isIn pd'' | |
| 158 | end | |
| 159 | | NONE => false) | |
| 160 | | NONE => false | |
| 161 | ||
| 15642 | 162 | |
| 163 | (*************************************) | |
| 164 | (* Set up a Watcher Process *) | |
| 165 | (*************************************) | |
| 166 | ||
| 17525 
ae5bb6001afb
tidying, and support for axclass/classrel clauses
 paulson parents: 
17502diff
changeset | 167 | fun killChild proc = (Unix.kill(proc, Posix.Signal.kill); Unix.reap proc); | 
| 
ae5bb6001afb
tidying, and support for axclass/classrel clauses
 paulson parents: 
17502diff
changeset | 168 | |
| 17774 | 169 | val killChildren = List.app (ignore o killChild o #proc_handle) : cmdproc list -> unit; | 
| 170 | ||
| 171 | fun killWatcher (toParentStr, procList) = | |
| 172 | (trace "\nWatcher timeout: Killing proof processes"; | |
| 173 | TextIO.output(toParentStr, "Timeout: Killing proof processes!\n"); | |
| 174 | TextIO.flushOut toParentStr; | |
| 175 | killChildren procList; | |
| 176 | Posix.Process.exit 0w0); | |
| 17502 | 177 | |
| 17774 | 178 | (* take an instream and poll its underlying reader for input *) | 
| 179 | fun pollParentInput (fromParentIOD, fromParentStr, toParentStr) = | |
| 180 | case OS.IO.pollDesc fromParentIOD of | |
| 181 | SOME pd => | |
| 182 | (case OS.IO.poll ([OS.IO.pollIn pd], SOME (Time.fromSeconds 2)) of | |
| 183 | [] => NONE | |
| 184 | | pd''::_ => if OS.IO.isIn pd'' | |
| 185 | then SOME (getCmds (toParentStr, fromParentStr, [])) | |
| 186 | else NONE) | |
| 187 | | NONE => NONE; | |
| 17568 | 188 | |
| 17583 
c272b91b619f
removal of "sleep" to stop looping in Poly/ML, and replacement of funny codes by tracing statements
 paulson parents: 
17568diff
changeset | 189 | (*get the number of the subgoal from the filename: the last digit string*) | 
| 
c272b91b619f
removal of "sleep" to stop looping in Poly/ML, and replacement of funny codes by tracing statements
 paulson parents: 
17568diff
changeset | 190 | fun number_from_filename s = | 
| 
c272b91b619f
removal of "sleep" to stop looping in Poly/ML, and replacement of funny codes by tracing statements
 paulson parents: 
17568diff
changeset | 191 | case String.tokens (not o Char.isDigit) s of | 
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 192 |       [] => (trace ("\nWatcher could not read subgoal nunber! " ^ s); 
 | 
| 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 193 | raise ERROR) | 
| 17583 
c272b91b619f
removal of "sleep" to stop looping in Poly/ML, and replacement of funny codes by tracing statements
 paulson parents: 
17568diff
changeset | 194 | | numbers => valOf (Int.fromString (List.last numbers)); | 
| 
c272b91b619f
removal of "sleep" to stop looping in Poly/ML, and replacement of funny codes by tracing statements
 paulson parents: 
17568diff
changeset | 195 | |
| 17774 | 196 | (* call ATP. Settings should be a list of strings ["-t300", "-m100000"]*) | 
| 197 | fun execCmds [] procList = procList | |
| 198 | | execCmds ((prover,proverCmd,settings,file)::cmds) procList = | |
| 199 |       let val _ = trace ("\nAbout to execute command: " ^ proverCmd ^ " " ^ file)
 | |
| 200 | val childhandle:(TextIO.instream,TextIO.outstream) Unix.proc = | |
| 201 | Unix.execute(proverCmd, settings@[file]) | |
| 202 | val (instr, outstr) = Unix.streamsOf childhandle | |
| 203 | 	  val newProcList = {prover=prover, file=file, proc_handle=childhandle,
 | |
| 204 | instr=instr, outstr=outstr} :: procList | |
| 205 | 	  val _ = trace ("\nFinished at " ^
 | |
| 206 | Date.toString(Date.fromTimeLocal(Time.now()))) | |
| 207 | in execCmds cmds newProcList end | |
| 208 | ||
| 209 | fun checkChildren (th, clause_arr, toParentStr, children) = | |
| 210 | let fun check [] = [] (* no children to check *) | |
| 211 | | check (child::children) = | |
| 212 | 	   let val {prover, file, proc_handle, instr=childIn, ...} : cmdproc =
 | |
| 213 | child | |
| 214 | 	       val _ = trace ("\nprobfile = " ^ file)
 | |
| 215 | val sgno = number_from_filename file | |
| 216 | val ppid = Posix.ProcEnv.getppid() | |
| 217 | in | |
| 218 | if pollChild childIn | |
| 219 | then (* check here for prover label on child*) | |
| 220 | 	       let val _ = trace ("\nInput available from child: " ^ file)
 | |
| 221 | val childDone = (case prover of | |
| 222 | "vampire" => AtpCommunication.checkVampProofFound | |
| 223 | (childIn, toParentStr, ppid, file, clause_arr) | |
| 224 | | "E" => AtpCommunication.checkEProofFound | |
| 225 | (childIn, toParentStr, ppid, file, clause_arr) | |
| 226 | | "spass" => AtpCommunication.checkSpassProofFound | |
| 227 | (childIn, toParentStr, ppid, file, th, sgno,clause_arr) | |
| 228 | 		     | _ => (trace ("\nBad prover! " ^ prover); true) )
 | |
| 229 | in | |
| 230 | if childDone (*child has found a proof and transferred it*) | |
| 231 | then (Unix.reap proc_handle; OS.FileSys.remove file; | |
| 232 | check children) | |
| 233 | else child :: check children | |
| 234 | end | |
| 235 | else (trace "\nNo child output"; child :: check children) | |
| 236 | end | |
| 237 | in | |
| 238 |     trace ("\nIn checkChildren, length of queue: " ^  Int.toString(length children));
 | |
| 239 | check children | |
| 240 | end; | |
| 241 | ||
| 242 | ||
| 243 | fun setupWatcher (th,clause_arr) = | |
| 16061 | 244 | let | 
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 245 | val p1 = Posix.IO.pipe() (*pipes for communication between parent and watcher*) | 
| 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 246 | val p2 = Posix.IO.pipe() | 
| 17764 | 247 | (****** fork a watcher process and get it set up and going ******) | 
| 17317 
3f12de2e2e6e
Isabelle-ATP link: sortable axiom names; no spaces in switches; general tidying
 paulson parents: 
17315diff
changeset | 248 | fun startWatcher procList = | 
| 17774 | 249 | case Posix.Process.fork() of | 
| 250 | SOME pid => pid (* parent - i.e. main Isabelle process *) | |
| 251 | | NONE => | |
| 252 | let (* child - i.e. watcher *) | |
| 253 | val oldchildin = #infd p1 | |
| 254 | val fromParent = Posix.FileSys.wordToFD 0w0 | |
| 255 | val oldchildout = #outfd p2 | |
| 256 | val toParent = Posix.FileSys.wordToFD 0w1 | |
| 257 | val fromParentIOD = Posix.FileSys.fdToIOD fromParent | |
| 258 | 	    val fromParentStr = openInFD ("_exec_in_parent", fromParent)
 | |
| 259 | 	    val toParentStr = openOutFD ("_exec_out_parent", toParent)
 | |
| 260 | val pid = Posix.ProcEnv.getpid() | |
| 261 | 	    val () = Posix.ProcEnv.setpgid {pid = SOME pid, pgid = SOME pid}
 | |
| 262 | (*set process group id: allows killing all children*) | |
| 263 | val () = trace "\nsubgoals forked to startWatcher" | |
| 264 | val limit = ref 200; (*don't let watcher run forever*) | |
| 265 | (*Watcher Loop : Check running ATP processes for output*) | |
| 266 | fun keepWatching procList = | |
| 267 | 	      (trace ("\npollParentInput. Limit = " ^ Int.toString (!limit) ^ 
 | |
| 268 | " length(procList) = " ^ Int.toString(length procList)); | |
| 269 | OS.Process.sleep (Time.fromMilliseconds 100); limit := !limit - 1; | |
| 270 | if !limit < 0 then killWatcher (toParentStr, procList) | |
| 271 | else | |
| 272 | case pollParentInput(fromParentIOD, fromParentStr, toParentStr) of | |
| 273 | SOME [(_,"Kill children",_,_)] => | |
| 274 | (trace "\nReceived Kill command"; | |
| 275 | killChildren procList; keepWatching []) | |
| 276 | | SOME cmds => (* deal with commands from Isabelle process *) | |
| 277 | if length procList < 40 then (* Execute locally *) | |
| 278 | 		      let val _ = trace("\nCommands from parent: " ^ 
 | |
| 279 | Int.toString(length cmds)) | |
| 280 | val newProcList' = checkChildren(th, clause_arr, toParentStr, | |
| 281 | execCmds cmds procList) | |
| 282 | in trace "\nCommands executed"; keepWatching newProcList' end | |
| 283 | else (* Execute remotely [FIXME: NOT REALLY] *) | |
| 284 | let val newProcList' = checkChildren (th, clause_arr, toParentStr, | |
| 285 | execCmds cmds procList) | |
| 286 | in keepWatching newProcList' end | |
| 287 | | NONE => (* No new input from Isabelle *) | |
| 288 | (trace "\nNothing from parent..."; | |
| 289 | keepWatching(checkChildren(th, clause_arr, toParentStr, procList)))) | |
| 290 | handle exn => (*FIXME: exn handler is too general!*) | |
| 291 | 	       (trace ("\nkeepWatching exception handler: " ^ Toplevel.exn_message exn);
 | |
| 292 | keepWatching procList); | |
| 293 | in | |
| 294 | (*** Sort out pipes ********) | |
| 295 | Posix.IO.close (#outfd p1); Posix.IO.close (#infd p2); | |
| 296 | 	    Posix.IO.dup2{old = oldchildin, new = fromParent};
 | |
| 297 | Posix.IO.close oldchildin; | |
| 298 | 	    Posix.IO.dup2{old = oldchildout, new = toParent};
 | |
| 299 | Posix.IO.close oldchildout; | |
| 300 | keepWatching (procList) | |
| 301 | end; | |
| 15642 | 302 | |
| 16061 | 303 | val _ = TextIO.flushOut TextIO.stdOut | 
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 304 | val pid = startWatcher [] | 
| 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 305 | (* communication streams to watcher*) | 
| 16061 | 306 |     val instr = openInFD ("_exec_in", #infd p2)
 | 
| 307 |     val outstr = openOutFD ("_exec_out", #outfd p1)
 | |
| 17568 | 308 | in | 
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 309 | (* close the child-side fds*) | 
| 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 310 | Posix.IO.close (#outfd p2); Posix.IO.close (#infd p1); | 
| 17568 | 311 | (* set the fds close on exec *) | 
| 312 | Posix.IO.setfd (#infd p2, Posix.IO.FD.flags [Posix.IO.FD.cloexec]); | |
| 313 | Posix.IO.setfd (#outfd p1, Posix.IO.FD.flags [Posix.IO.FD.cloexec]); | |
| 17773 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 314 |     {pid = pid, instr = instr, outstr = outstr}
 | 
| 17568 | 315 | end; | 
| 15642 | 316 | |
| 317 | ||
| 318 | ||
| 319 | (**********************************************************) | |
| 320 | (* Start a watcher and set up signal handlers *) | |
| 321 | (**********************************************************) | |
| 16039 
dfe264950511
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
 quigley parents: 
15919diff
changeset | 322 | |
| 17773 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 323 | fun reapAll s = (*Signal handler to tidy away dead processes*) | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 324 | (case Posix.Process.waitpid_nh(Posix.Process.W_ANY_CHILD, []) of | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 325 | SOME _ => reapAll s | NONE => ()) | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 326 | handle OS.SysErr _ => () | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 327 | |
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 328 | (*FIXME: does the main process need something like this? | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 329 | IsaSignal.signal (IsaSignal.chld, IsaSignal.SIG_HANDLE reap)??*) | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 330 | |
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 331 | fun killWatcher pid = | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 332 | (goals_being_watched := 0; | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 333 | Posix.Process.kill(Posix.Process.K_GROUP pid, Posix.Signal.kill); | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 334 | reapAll()); | 
| 17488 
67376a311a2b
further simplification of the Isabelle-ATP linkup
 paulson parents: 
17484diff
changeset | 335 | |
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 336 | fun reapWatcher(pid, instr, outstr) = ignore | 
| 17484 
f6a225f97f0a
simplification of the Isabelle-ATP code; hooks for batch generation of problems
 paulson parents: 
17435diff
changeset | 337 | (TextIO.closeIn instr; TextIO.closeOut outstr; | 
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 338 | Posix.Process.waitpid(Posix.Process.W_CHILD pid, [])) | 
| 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 339 | handle OS.SysErr _ => () | 
| 15642 | 340 | |
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 341 | fun string_of_subgoal th i = | 
| 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 342 | string_of_cterm (List.nth(cprems_of th, i-1)) | 
| 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 343 | handle Subscript => "Subgoal number out of range!" | 
| 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 344 | |
| 17773 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 345 | fun prems_string_of th = space_implode "\n" (map string_of_cterm (cprems_of th)) | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 346 | |
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 347 | fun createWatcher (th, clause_arr) = | 
| 17773 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 348 |  let val {pid=childpid, instr=childin, outstr=childout} = setupWatcher (th,clause_arr)
 | 
| 17435 | 349 | fun decr_watched() = | 
| 17502 | 350 | (goals_being_watched := !goals_being_watched - 1; | 
| 17435 | 351 | if !goals_being_watched = 0 | 
| 352 | then | |
| 17819 | 353 | 	     (debug ("\nReaping a watcher, childpid = " ^ string_of_pid childpid);
 | 
| 17773 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 354 | killWatcher childpid (*???; reapWatcher (childpid, childin, childout)*) ) | 
| 17435 | 355 | else ()) | 
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 356 |      val _ = debug ("subgoals forked to createWatcher: "^ prems_string_of th);
 | 
| 17484 
f6a225f97f0a
simplification of the Isabelle-ATP code; hooks for batch generation of problems
 paulson parents: 
17435diff
changeset | 357 | fun proofHandler n = | 
| 
f6a225f97f0a
simplification of the Isabelle-ATP code; hooks for batch generation of problems
 paulson parents: 
17435diff
changeset | 358 | let val outcome = TextIO.inputLine childin | 
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 359 | val probfile = TextIO.inputLine childin | 
| 17773 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 360 | val sgno = number_from_filename probfile | 
| 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 361 | val text = string_of_subgoal th sgno | 
| 17484 
f6a225f97f0a
simplification of the Isabelle-ATP code; hooks for batch generation of problems
 paulson parents: 
17435diff
changeset | 362 | 	   val _ = debug ("In signal handler. outcome = \"" ^ outcome ^ 
 | 
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 363 | "\"\nprobfile = " ^ probfile ^ | 
| 17773 
a7258e1020b7
more tidying. Fixed process management bugs and race condition
 paulson parents: 
17772diff
changeset | 364 | "\nGoals being watched: "^ Int.toString (!goals_being_watched)) | 
| 17502 | 365 | in | 
| 366 | if String.isPrefix "[" outcome (*indicates a proof reconstruction*) | |
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 367 | then (priority (Recon_Transfer.apply_res_thm outcome); | 
| 17435 | 368 | decr_watched()) | 
| 17484 
f6a225f97f0a
simplification of the Isabelle-ATP code; hooks for batch generation of problems
 paulson parents: 
17435diff
changeset | 369 | else if String.isPrefix "Invalid" outcome | 
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 370 | 	 then (priority ("Subgoal is not provable:\n" ^ text);
 | 
| 17484 
f6a225f97f0a
simplification of the Isabelle-ATP code; hooks for batch generation of problems
 paulson parents: 
17435diff
changeset | 371 | decr_watched()) | 
| 
f6a225f97f0a
simplification of the Isabelle-ATP code; hooks for batch generation of problems
 paulson parents: 
17435diff
changeset | 372 | else if String.isPrefix "Failure" outcome | 
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 373 | 	 then (priority ("Proof attempt failed:\n" ^ text);
 | 
| 17435 | 374 | decr_watched()) | 
| 17216 | 375 | (* print out a list of rules used from clasimpset*) | 
| 17484 
f6a225f97f0a
simplification of the Isabelle-ATP code; hooks for batch generation of problems
 paulson parents: 
17435diff
changeset | 376 | else if String.isPrefix "Success" outcome | 
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 377 | then (priority (outcome ^ text); | 
| 17435 | 378 | decr_watched()) | 
| 17216 | 379 | (* if proof translation failed *) | 
| 17484 
f6a225f97f0a
simplification of the Isabelle-ATP code; hooks for batch generation of problems
 paulson parents: 
17435diff
changeset | 380 | else if String.isPrefix "Translation failed" outcome | 
| 17772 
818cec5f82a4
major simplification: removal of the goalstring argument
 paulson parents: 
17764diff
changeset | 381 | then (priority (outcome ^ text); | 
| 17435 | 382 | decr_watched()) | 
| 17690 
8ba7c3cd24a8
time limit option; fixed bug concerning first line of ATP output
 paulson parents: 
17583diff
changeset | 383 | else (priority "System error in proof handler"; | 
| 17435 | 384 | decr_watched()) | 
| 17484 
f6a225f97f0a
simplification of the Isabelle-ATP code; hooks for batch generation of problems
 paulson parents: 
17435diff
changeset | 385 | end | 
| 
f6a225f97f0a
simplification of the Isabelle-ATP code; hooks for batch generation of problems
 paulson parents: 
17435diff
changeset | 386 | in IsaSignal.signal (IsaSignal.usr2, IsaSignal.SIG_HANDLE proofHandler); | 
| 17216 | 387 | (childin, childout, childpid) | 
| 388 | end | |
| 15642 | 389 | |
| 390 | end (* structure Watcher *) |