src/HOL/Tools/SMT/lib/scripts/z3_wrapper
author blanchet
Thu, 16 Dec 2010 21:53:31 +0100
changeset 41218 028449eb1548
parent 41216 5cee84180cd7
child 41219 41f3fdc49ec3
permissions -rwxr-xr-x
better propagation of stdout in case of failure + comply with strict/warnings
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41213
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
     1
#!/usr/bin/env perl
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
     2
#
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
     3
# Author: Jasmin Blanchette, TU Muenchen
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
     4
#
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
     5
# Invoke Z3 with error detection and correction.
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
     6
# To use this wrapper, set
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
     7
#
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
     8
#     Z3_REAL_SOLVER=/path-to-z3/z3
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
     9
#     Z3_SOLVER=$ISABELLE_HOME/src/HOL/Tools/SMT/lib/scripts/z3_wrapper
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    10
#
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    11
# in your "~/.isabelle/etc/settings" file.
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    12
41218
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    13
use strict;
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    14
use warnings;
41216
5cee84180cd7 cleaner handling of temporary files
blanchet
parents: 41213
diff changeset
    15
use POSIX;
5cee84180cd7 cleaner handling of temporary files
blanchet
parents: 41213
diff changeset
    16
41218
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    17
my $in_file = $ARGV[$#ARGV - 1];
41216
5cee84180cd7 cleaner handling of temporary files
blanchet
parents: 41213
diff changeset
    18
my $out_file = tmpnam();
5cee84180cd7 cleaner handling of temporary files
blanchet
parents: 41213
diff changeset
    19
my $err_file = tmpnam();
5cee84180cd7 cleaner handling of temporary files
blanchet
parents: 41213
diff changeset
    20
41213
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    21
$ENV{'Z3_REAL_SOLVER'} || die "Environment variable \"Z3_REAL_SOLVER\" not set";
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    22
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    23
RUN:
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    24
my $code = system $ENV{'Z3_REAL_SOLVER'} . " " . join(" ", @ARGV) . " >$out_file 2>$err_file";
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    25
41218
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    26
open(ERR_FILE, "<$err_file") || die "Cannot open file \"$err_file\"";
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    27
my @err_lines = <ERR_FILE>;
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    28
close(ERR_FILE);
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    29
print STDERR join("", @err_lines);
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    30
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    31
if ($code != 0) {
41213
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    32
  foreach (@err_lines) {
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    33
    if (m/[lL]ine ([0-9]+)/) {
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    34
      open(IN_FILE, "<$in_file") || die "Cannot open file \"$in_file\"";
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    35
      my @in_lines = <IN_FILE>;
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    36
      close(IN_FILE);
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    37
      delete $in_lines[$1 - 1];
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    38
      open(IN_FILE, ">$in_file") || die "Cannot open file \"$in_file\"";
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    39
      print IN_FILE join ("", @in_lines);
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    40
      close(IN_FILE);
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    41
      goto RUN;
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    42
    }
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    43
  }
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    44
}
41216
5cee84180cd7 cleaner handling of temporary files
blanchet
parents: 41213
diff changeset
    45
41218
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    46
open(OUT_FILE, "<$out_file") || die "Cannot open file \"$out_file\"";
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    47
my @out_lines = <OUT_FILE>;
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    48
close(OUT_FILE);
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    49
print join("", @out_lines);
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    50
41216
5cee84180cd7 cleaner handling of temporary files
blanchet
parents: 41213
diff changeset
    51
unlink($out_file);
5cee84180cd7 cleaner handling of temporary files
blanchet
parents: 41213
diff changeset
    52
unlink($err_file);
5cee84180cd7 cleaner handling of temporary files
blanchet
parents: 41213
diff changeset
    53
exit $code;