src/HOL/Mirabelle/lib/scripts/mirabelle.pl
author krauss
Mon, 21 Mar 2011 14:46:59 +0100
changeset 42038 626fcf4a803e
parent 42037 1571fde21911
child 42070 d3404f32328a
permissions -rw-r--r--
fixed perl error
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
     1
#
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
     2
# Author: Jasmin Blanchette and Sascha Boehme
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
     3
#
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
     4
# Testing tool for automated proof tools.
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
     5
#
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
     6
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
     7
use File::Basename;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
     8
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
     9
# environment
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    10
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    11
my $isabelle_home = $ENV{'ISABELLE_HOME'};
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    12
my $mirabelle_home = $ENV{'MIRABELLE_HOME'};
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    13
my $mirabelle_logic = $ENV{'MIRABELLE_LOGIC'};
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    14
my $mirabelle_theory = $ENV{'MIRABELLE_THEORY'};
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    15
my $output_path = $ENV{'MIRABELLE_OUTPUT_PATH'};
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    16
my $timeout = $ENV{'MIRABELLE_TIMEOUT'};
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
    17
my $be_quiet = $ENV{'MIRABELLE_QUIET'};
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
    18
my $actions = $ENV{'MIRABELLE_ACTIONS'};
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    19
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    20
my $mirabelle_thy = $mirabelle_home . "/Mirabelle";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    21
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    22
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    23
# arguments
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    24
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
    25
my $thy_file = $ARGV[0];
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    26
my $start_line = "0";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    27
my $end_line = "~1";
32454
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32434
diff changeset
    28
if ($thy_file =~ /^(.*)\[([0-9]+)\:(~?[0-9]+)\]$/) {
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32434
diff changeset
    29
  $thy_file = $1;
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32434
diff changeset
    30
  $start_line = $2;
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32434
diff changeset
    31
  $end_line = $3;
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    32
}
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    33
my ($thy_name, $path, $ext) = fileparse($thy_file, ".thy");
42038
626fcf4a803e fixed perl error
krauss
parents: 42037
diff changeset
    34
my $rand_suffix = join('', map { ('a'..'z')[rand(26)] } 1 .. 10);
42033
60350051ef93 mirabelle: create modified theory file in original location, to ensure that its dependencies can be found (cf. aa8dce9ab8a9)
krauss
parents: 41361
diff changeset
    35
my $new_thy_name = $thy_name . "_Mirabelle_" . $rand_suffix;
60350051ef93 mirabelle: create modified theory file in original location, to ensure that its dependencies can be found (cf. aa8dce9ab8a9)
krauss
parents: 41361
diff changeset
    36
my $new_thy_file = $path . "/" . $new_thy_name . $ext;
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    37
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    38
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    39
# setup
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    40
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    41
my $setup_thy_name = $thy_name . "_Setup";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    42
my $setup_file = $output_path . "/" . $setup_thy_name . ".thy";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    43
my $log_file = $output_path . "/" . $thy_name . ".log";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    44
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    45
my @action_files;
32396
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
    46
my @action_names;
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    47
foreach (split(/:/, $actions)) {
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    48
  if (m/([^[]*)/) {
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    49
    push @action_files, "\"$mirabelle_home/Tools/mirabelle_$1.ML\"";
32396
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
    50
    push @action_names, $1;
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    51
  }
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    52
}
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    53
my $tools = "";
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    54
if ($#action_files >= 0) {
38895
ec417f748064 deal with duplicates
blanchet
parents: 37378
diff changeset
    55
  # uniquify
ec417f748064 deal with duplicates
blanchet
parents: 37378
diff changeset
    56
  my $s = join ("\n", @action_files);
ec417f748064 deal with duplicates
blanchet
parents: 37378
diff changeset
    57
  my @action_files = split(/\n/, $s . "\n" . $s);
ec417f748064 deal with duplicates
blanchet
parents: 37378
diff changeset
    58
  %action_files = sort(@action_files);
ec417f748064 deal with duplicates
blanchet
parents: 37378
diff changeset
    59
  $tools = "uses " . join(" ", sort(keys(%action_files)));
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    60
}
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    61
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    62
open(SETUP_FILE, ">$setup_file") || die "Could not create file '$setup_file'";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    63
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    64
print SETUP_FILE <<END;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    65
theory "$setup_thy_name"
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    66
imports "$mirabelle_thy" "$mirabelle_theory"
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    67
$tools
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    68
begin
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    69
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    70
setup {* 
37378
5449c9aafdca Adapted Mirabelle script (cf. f60e4dd6d76f)
krauss
parents: 32522
diff changeset
    71
  Config.put_global Mirabelle.logfile "$log_file" #>
5449c9aafdca Adapted Mirabelle script (cf. f60e4dd6d76f)
krauss
parents: 32522
diff changeset
    72
  Config.put_global Mirabelle.timeout $timeout #>
5449c9aafdca Adapted Mirabelle script (cf. f60e4dd6d76f)
krauss
parents: 32522
diff changeset
    73
  Config.put_global Mirabelle.start_line $start_line #>
5449c9aafdca Adapted Mirabelle script (cf. f60e4dd6d76f)
krauss
parents: 32522
diff changeset
    74
  Config.put_global Mirabelle.end_line $end_line
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    75
*}
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    76
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    77
END
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    78
38898
a243f8883e8e execute actions in same order as specified on command line
blanchet
parents: 38895
diff changeset
    79
foreach (reverse(split(/:/, $actions))) {
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    80
  if (m/([^[]*)(?:\[(.*)\])?/) {
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    81
    my ($name, $settings_str) = ($1, $2 || "");
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    82
    $name =~ s/^([a-z])/\U$1/;
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    83
    print SETUP_FILE "setup {* Mirabelle_$name.invoke [";
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    84
    my $sep = "";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    85
    foreach (split(/,/, $settings_str)) {
41361
d1e4a20911cb better parsing of options, in case the value has '='
blanchet
parents: 38898
diff changeset
    86
      if (m/\s*([^=]*)\s*=\s*(.*)\s*/) {
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    87
        print SETUP_FILE "$sep(\"$1\", \"$2\")";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    88
        $sep = ", ";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    89
      }
32522
1b70db55c811 Mirabelle: command-line action options may either be key=value or just key
boehmes
parents: 32521
diff changeset
    90
      elsif (m/\s*(.*)\s*/) {
1b70db55c811 Mirabelle: command-line action options may either be key=value or just key
boehmes
parents: 32521
diff changeset
    91
	print SETUP_FILE "$sep(\"$1\", \"\")";
1b70db55c811 Mirabelle: command-line action options may either be key=value or just key
boehmes
parents: 32521
diff changeset
    92
	$sep = ", ";
1b70db55c811 Mirabelle: command-line action options may either be key=value or just key
boehmes
parents: 32521
diff changeset
    93
      }
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    94
    }
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    95
    print SETUP_FILE "] *}\n";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    96
  }
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    97
}
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    98
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    99
print SETUP_FILE "\nend";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   100
close SETUP_FILE;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   101
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   102
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   103
# modify target theory file
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   104
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   105
open(OLD_FILE, "<$thy_file") || die "Cannot open file '$thy_file'";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   106
my @lines = <OLD_FILE>;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   107
close(OLD_FILE);
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   108
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   109
my $thy_text = join("", @lines);
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   110
my $old_len = length($thy_text);
32395
9692b0714295 do not introduce additional newlines
boehmes
parents: 32385
diff changeset
   111
$thy_text =~ s/(theory\s+)\"?$thy_name\"?/$1"$new_thy_name"/g;
42033
60350051ef93 mirabelle: create modified theory file in original location, to ensure that its dependencies can be found (cf. aa8dce9ab8a9)
krauss
parents: 41361
diff changeset
   112
$thy_text =~ s/(imports)(\s+)/$1 "$output_path\/$setup_thy_name"$2/g;
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   113
die "No 'imports' found" if length($thy_text) == $old_len;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   114
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   115
open(NEW_FILE, ">$new_thy_file") || die "Cannot create file '$new_thy_file'";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   116
print NEW_FILE $thy_text;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   117
close(NEW_FILE);
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   118
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   119
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   120
# run isabelle
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   121
32396
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   122
open(LOG_FILE, ">$log_file");
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   123
print LOG_FILE "Run of $new_thy_file with:\n";
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   124
foreach $name (@action_names) {
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   125
  print LOG_FILE "  $name\n";
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   126
}
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   127
close(LOG_FILE);
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   128
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
   129
my $quiet = "";
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
   130
if (defined $be_quiet and $be_quiet ne "") {
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
   131
  $quiet = " > /dev/null 2>&1";
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
   132
}
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   133
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
   134
print "Mirabelle: $thy_file\n" if ($quiet ne "");
32498
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32496
diff changeset
   135
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
   136
my $result = system "\"$ENV{'ISABELLE_PROCESS'}\" " .
42037
1571fde21911 eliminated unnecessary generated ROOT.ML
krauss
parents: 42034
diff changeset
   137
  "-e 'use_thy \"$path/$new_thy_name\" handle _ => exit 1;\n' -q $mirabelle_logic" . $quiet;
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
   138
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
   139
print "Finished:  $thy_file\n" if ($quiet ne "");
32498
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32496
diff changeset
   140
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32496
diff changeset
   141
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   142
# cleanup
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   143
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   144
unlink $setup_file;
42033
60350051ef93 mirabelle: create modified theory file in original location, to ensure that its dependencies can be found (cf. aa8dce9ab8a9)
krauss
parents: 41361
diff changeset
   145
unlink $new_thy_file;
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   146
42034
a77df5241959 propagate mirabelle failures properly;
krauss
parents: 42033
diff changeset
   147
exit ($result ? 1 : 0);