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