src/HOL/Mirabelle/lib/scripts/mirabelle.pl
author blanchet
Tue, 21 Dec 2010 13:57:35 +0100
changeset 41361 d1e4a20911cb
parent 38898 a243f8883e8e
child 42033 60350051ef93
permissions -rw-r--r--
better parsing of options, in case the value has '='
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");
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) {
38895
ec417f748064 deal with duplicates
blanchet
parents: 37378
diff changeset
    54
  # uniquify
ec417f748064 deal with duplicates
blanchet
parents: 37378
diff changeset
    55
  my $s = join ("\n", @action_files);
ec417f748064 deal with duplicates
blanchet
parents: 37378
diff changeset
    56
  my @action_files = split(/\n/, $s . "\n" . $s);
ec417f748064 deal with duplicates
blanchet
parents: 37378
diff changeset
    57
  %action_files = sort(@action_files);
ec417f748064 deal with duplicates
blanchet
parents: 37378
diff changeset
    58
  $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
    59
}
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    60
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    61
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
    62
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    63
print SETUP_FILE <<END;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    64
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
    65
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
    66
$tools
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    67
begin
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    68
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    69
setup {* 
37378
5449c9aafdca Adapted Mirabelle script (cf. f60e4dd6d76f)
krauss
parents: 32522
diff changeset
    70
  Config.put_global Mirabelle.logfile "$log_file" #>
5449c9aafdca Adapted Mirabelle script (cf. f60e4dd6d76f)
krauss
parents: 32522
diff changeset
    71
  Config.put_global Mirabelle.timeout $timeout #>
5449c9aafdca Adapted Mirabelle script (cf. f60e4dd6d76f)
krauss
parents: 32522
diff changeset
    72
  Config.put_global Mirabelle.start_line $start_line #>
5449c9aafdca Adapted Mirabelle script (cf. f60e4dd6d76f)
krauss
parents: 32522
diff changeset
    73
  Config.put_global Mirabelle.end_line $end_line
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    74
*}
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
END
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    77
38898
a243f8883e8e execute actions in same order as specified on command line
blanchet
parents: 38895
diff changeset
    78
foreach (reverse(split(/:/, $actions))) {
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    79
  if (m/([^[]*)(?:\[(.*)\])?/) {
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    80
    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
    81
    $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
    82
    print SETUP_FILE "setup {* Mirabelle_$name.invoke [";
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    83
    my $sep = "";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    84
    foreach (split(/,/, $settings_str)) {
41361
d1e4a20911cb better parsing of options, in case the value has '='
blanchet
parents: 38898
diff changeset
    85
      if (m/\s*([^=]*)\s*=\s*(.*)\s*/) {
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    86
        print SETUP_FILE "$sep(\"$1\", \"$2\")";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    87
        $sep = ", ";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    88
      }
32522
1b70db55c811 Mirabelle: command-line action options may either be key=value or just key
boehmes
parents: 32521
diff changeset
    89
      elsif (m/\s*(.*)\s*/) {
1b70db55c811 Mirabelle: command-line action options may either be key=value or just key
boehmes
parents: 32521
diff changeset
    90
	print SETUP_FILE "$sep(\"$1\", \"\")";
1b70db55c811 Mirabelle: command-line action options may either be key=value or just key
boehmes
parents: 32521
diff changeset
    91
	$sep = ", ";
1b70db55c811 Mirabelle: command-line action options may either be key=value or just key
boehmes
parents: 32521
diff changeset
    92
      }
32382
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
    print SETUP_FILE "] *}\n";
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
}
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
print SETUP_FILE "\nend";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
    99
close SETUP_FILE;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   100
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
# modify target theory file
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   103
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   104
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
   105
my @lines = <OLD_FILE>;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   106
close(OLD_FILE);
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   107
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   108
my $thy_text = join("", @lines);
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   109
my $old_len = length($thy_text);
32395
9692b0714295 do not introduce additional newlines
boehmes
parents: 32385
diff changeset
   110
$thy_text =~ s/(theory\s+)\"?$thy_name\"?/$1"$new_thy_name"/g;
9692b0714295 do not introduce additional newlines
boehmes
parents: 32385
diff changeset
   111
$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
   112
die "No 'imports' found" if length($thy_text) == $old_len;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   113
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   114
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
   115
print NEW_FILE $thy_text;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   116
close(NEW_FILE);
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   117
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   118
my $root_file = "$output_path/ROOT_$thy_name.ML";
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   119
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
   120
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
   121
close(ROOT_FILE);
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   122
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   123
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   124
# run isabelle
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   125
32396
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   126
open(LOG_FILE, ">$log_file");
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   127
print LOG_FILE "Run of $new_thy_file with:\n";
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   128
foreach $name (@action_names) {
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   129
  print LOG_FILE "  $name\n";
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   130
}
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   131
close(LOG_FILE);
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32395
diff changeset
   132
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
   133
my $quiet = "";
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
   134
if (defined $be_quiet and $be_quiet ne "") {
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
   135
  $quiet = " > /dev/null 2>&1";
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
   136
}
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   137
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
   138
print "Mirabelle: $thy_file\n" if ($quiet ne "");
32498
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32496
diff changeset
   139
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
   140
my $result = system "\"$ENV{'ISABELLE_PROCESS'}\" " .
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
   141
  "-e 'use \"$root_file\";' -q $mirabelle_logic" . $quiet;
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
   142
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32498
diff changeset
   143
print "Finished:  $thy_file\n" if ($quiet ne "");
32498
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32496
diff changeset
   144
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32496
diff changeset
   145
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   146
# cleanup
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   147
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   148
unlink $root_file;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   149
unlink $setup_file;
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
diff changeset
   150
32498
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32496
diff changeset
   151
exit $result;