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