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