src/HOL/Tools/SMT/lib/scripts/z3_wrapper
author blanchet
Fri, 17 Dec 2010 00:11:06 +0100
changeset 41221 da6907b67fac
parent 41219 41f3fdc49ec3
child 41237 8b6f3917bc76
permissions -rwxr-xr-x
fixed off-by-one and return proper error code -- never underestimate the number of oddities in Perl
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
41221
da6907b67fac fixed off-by-one and return proper error code -- never underestimate the number of oddities in Perl
blanchet
parents: 41219
diff changeset
    17
my $in_file = $ARGV[$#ARGV];
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
41221
da6907b67fac fixed off-by-one and return proper error code -- never underestimate the number of oddities in Perl
blanchet
parents: 41219
diff changeset
    21
$ENV{'Z3_REAL_SOLVER'} || exit 10;
41213
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
41221
da6907b67fac fixed off-by-one and return proper error code -- never underestimate the number of oddities in Perl
blanchet
parents: 41219
diff changeset
    26
open(ERR_FILE, "<$err_file") || exit 11;
41218
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) {
41219
41f3fdc49ec3 keep track of errors in Z3 input file for debugging purposes
blanchet
parents: 41218
diff changeset
    32
  foreach my $err_line (@err_lines) {
41f3fdc49ec3 keep track of errors in Z3 input file for debugging purposes
blanchet
parents: 41218
diff changeset
    33
    if ($err_line =~ /[lL]ine ([0-9]+)/) {
41f3fdc49ec3 keep track of errors in Z3 input file for debugging purposes
blanchet
parents: 41218
diff changeset
    34
      my $line_num = $1;
41f3fdc49ec3 keep track of errors in Z3 input file for debugging purposes
blanchet
parents: 41218
diff changeset
    35
41221
da6907b67fac fixed off-by-one and return proper error code -- never underestimate the number of oddities in Perl
blanchet
parents: 41219
diff changeset
    36
      open(IN_FILE, "<$in_file") || exit 12;
41213
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    37
      my @in_lines = <IN_FILE>;
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    38
      close(IN_FILE);
41219
41f3fdc49ec3 keep track of errors in Z3 input file for debugging purposes
blanchet
parents: 41218
diff changeset
    39
41f3fdc49ec3 keep track of errors in Z3 input file for debugging purposes
blanchet
parents: 41218
diff changeset
    40
      $err_line =~ s/[\n\r]//g;
41f3fdc49ec3 keep track of errors in Z3 input file for debugging purposes
blanchet
parents: 41218
diff changeset
    41
      $in_lines[$line_num - 1] = ";;; " . $err_line . " -- "
41f3fdc49ec3 keep track of errors in Z3 input file for debugging purposes
blanchet
parents: 41218
diff changeset
    42
                                 . $in_lines[$line_num - 1];
41f3fdc49ec3 keep track of errors in Z3 input file for debugging purposes
blanchet
parents: 41218
diff changeset
    43
41221
da6907b67fac fixed off-by-one and return proper error code -- never underestimate the number of oddities in Perl
blanchet
parents: 41219
diff changeset
    44
      open(IN_FILE, ">$in_file") || exit 13;
41213
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    45
      print IN_FILE join ("", @in_lines);
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    46
      close(IN_FILE);
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    47
      goto RUN;
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    48
    }
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    49
  }
ee24717e3fe3 added self-correcting wrapper for Z3 -- see comment in the file for details
blanchet
parents:
diff changeset
    50
}
41216
5cee84180cd7 cleaner handling of temporary files
blanchet
parents: 41213
diff changeset
    51
41221
da6907b67fac fixed off-by-one and return proper error code -- never underestimate the number of oddities in Perl
blanchet
parents: 41219
diff changeset
    52
open(OUT_FILE, "<$out_file") || exit 14;
41218
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    53
my @out_lines = <OUT_FILE>;
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    54
close(OUT_FILE);
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    55
print join("", @out_lines);
028449eb1548 better propagation of stdout in case of failure + comply with strict/warnings
blanchet
parents: 41216
diff changeset
    56
41216
5cee84180cd7 cleaner handling of temporary files
blanchet
parents: 41213
diff changeset
    57
unlink($out_file);
5cee84180cd7 cleaner handling of temporary files
blanchet
parents: 41213
diff changeset
    58
unlink($err_file);
41221
da6907b67fac fixed off-by-one and return proper error code -- never underestimate the number of oddities in Perl
blanchet
parents: 41219
diff changeset
    59
exit(($code >> 8 | $code) & 0xff);