src/HOL/Mirabelle/lib/scripts/mirabelle.pl
author boehmes
Wed, 02 Sep 2009 16:23:53 +0200
changeset 32496 4ab00a2642c3
parent 32472 src/HOL/Tools/Mirabelle/lib/scripts/mirabelle.pl@7b92a8b8daaf
child 32498 1132c7c13f36
permissions -rw-r--r--
moved Mirabelle from HOL/Tools to HOL, added session HOL-Mirabelle
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'};
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    17
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    18
my $mirabelle_thy = $mirabelle_home . "/Mirabelle";
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
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    21
# arguments
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
my $actions = $ARGV[0];
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    24
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    25
my $thy_file = $ARGV[1];
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");
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    34
my $new_thy_name = $thy_name . "_Mirabelle";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    35
my $new_thy_file = $output_path . "/" . $new_thy_name . $ext;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    36
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
# setup
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    39
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    40
my $setup_thy_name = $thy_name . "_Setup";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    41
my $setup_file = $output_path . "/" . $setup_thy_name . ".thy";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    42
my $log_file = $output_path . "/" . $thy_name . ".log";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    43
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    44
my @action_files;
32396
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
    45
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
    46
foreach (split(/:/, $actions)) {
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    47
  if (m/([^[]*)/) {
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    48
    push @action_files, "\"$mirabelle_home/Tools/mirabelle_$1.ML\"";
32396
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
    49
    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
    50
  }
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
my $tools = "";
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    53
if ($#action_files >= 0) {
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    54
  $tools = "uses " . join(" ", @action_files);
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    55
}
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    56
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    57
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
    58
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    59
print SETUP_FILE <<END;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    60
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
    61
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
    62
$tools
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    63
begin
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    64
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    65
setup {* 
32396
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
    66
  Config.put_thy Mirabelle.logfile "$log_file" #>
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    67
  Config.put_thy Mirabelle.timeout $timeout #>
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    68
  Config.put_thy Mirabelle.start_line $start_line #>
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    69
  Config.put_thy Mirabelle.end_line $end_line
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    70
*}
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    71
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    72
END
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    73
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    74
foreach (split(/:/, $actions)) {
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    75
  if (m/([^[]*)(?:\[(.*)\])?/) {
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    76
    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
    77
    $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
    78
    print SETUP_FILE "setup {* Mirabelle_$name.invoke [";
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    79
    my $sep = "";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    80
    foreach (split(/,/, $settings_str)) {
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    81
      if (m/\s*(.*)\s*=\s*(.*)\s*/) {
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    82
        print SETUP_FILE "$sep(\"$1\", \"$2\")";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    83
        $sep = ", ";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    84
      }
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    85
    }
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    86
    print SETUP_FILE "] *}\n";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    87
  }
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    88
}
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    89
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    90
print SETUP_FILE "\nend";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    91
close SETUP_FILE;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    92
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    93
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    94
# modify target theory file
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    95
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    96
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
    97
my @lines = <OLD_FILE>;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    98
close(OLD_FILE);
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    99
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   100
my $thy_text = join("", @lines);
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   101
my $old_len = length($thy_text);
32395
9692b0714295 do not introduce additional newlines
boehmes
parents: 32385
diff changeset
   102
$thy_text =~ s/(theory\s+)\"?$thy_name\"?/$1"$new_thy_name"/g;
9692b0714295 do not introduce additional newlines
boehmes
parents: 32385
diff changeset
   103
$thy_text =~ s/(imports)(\s+)/$1 "$setup_thy_name"$2/g;
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   104
die "No 'imports' found" if length($thy_text) == $old_len;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   105
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   106
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
   107
print NEW_FILE $thy_text;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   108
close(NEW_FILE);
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   109
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   110
my $root_file = "$output_path/ROOT_$thy_name.ML";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   111
open(ROOT_FILE, ">$root_file") || die "Cannot create file '$root_file'";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   112
print ROOT_FILE "use_thy \"$output_path/$new_thy_name\";\n";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   113
close(ROOT_FILE);
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
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   116
# run isabelle
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   117
32396
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   118
open(LOG_FILE, ">$log_file");
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   119
print LOG_FILE "Run of $new_thy_file with:\n";
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   120
foreach $name (@action_names) {
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   121
  print LOG_FILE "  $name\n";
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   122
}
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   123
close(LOG_FILE);
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   124
32434
6b93b73a712b selectable prover for sledgehammer, proper environment lookup
boehmes
parents: 32397
diff changeset
   125
my $r = system "\"$ENV{'ISABELLE_PROCESS'}\" " .
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   126
  "-e 'use \"$root_file\";' -q $mirabelle_logic" . "\n";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   127
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   128
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   129
# cleanup
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   130
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   131
unlink $root_file;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   132
unlink $setup_file;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   133
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   134
exit $r;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   135