Admin/mira.py
author krauss
Thu, 24 Mar 2011 23:42:06 +0100
changeset 42980 b9ae421fbcc7
parent 42979 e6a1dc0aa058
child 42984 b8f176348f44
permissions -rw-r--r--
added configurations SML_HOL and SML_makeall (even though the latter is practically infeasible)
     1 """
     2     Test configuration descriptions for mira.
     3 """
     4 
     5 import os
     6 from os import path
     7 from glob import glob
     8 import subprocess
     9 import re
    10 
    11 import util
    12 
    13 
    14 # build and evaluation tools
    15 
    16 def prepare_isabelle_repository(loc_isabelle, loc_contrib, loc_dependency_heaps, parallelism = True, more_settings=''):
    17 
    18     loc_contrib = path.expanduser(loc_contrib)
    19     if not path.exists(loc_contrib):
    20         raise IOError('Bad file: %s' % loc_contrib)
    21     subprocess.check_call(['ln', '-s', loc_contrib, '%s/contrib' % loc_isabelle])
    22 
    23     contributed_components = path.join(loc_isabelle, 'Admin', 'contributed_components')
    24     if path.exists(contributed_components):
    25         components = []
    26         for component in util.readfile_lines(contributed_components):
    27             loc_component = path.join(loc_isabelle, component)
    28             if path.exists(loc_component):
    29                 components.append(loc_component)
    30         writer = open(path.join(loc_isabelle, 'etc', 'components'), 'a')
    31         for component in components:
    32             writer.write(component + '\n')
    33         writer.close()
    34 
    35     if loc_dependency_heaps:
    36         isabelle_path = loc_dependency_heaps + '/$ISABELLE_IDENTIFIER:$ISABELLE_OUTPUT'
    37     else:
    38         isabelle_path = '$ISABELLE_OUTPUT'
    39 
    40     if parallelism:
    41         parallelism_options = '-M max'
    42     else:
    43         parallelism_options = '-M 1 -q 0'
    44 
    45     extra_settings = '''
    46 ISABELLE_HOME_USER="$ISABELLE_HOME/home_user"
    47 ISABELLE_OUTPUT="$ISABELLE_HOME/heaps"
    48 ISABELLE_BROWSER_INFO="$ISABELLE_HOME/browser_info"
    49 ISABELLE_PATH="%s"
    50 
    51 ISABELLE_USEDIR_OPTIONS="$ISABELLE_USEDIR_OPTIONS %s -t true -v true -d pdf -g true -i true"
    52 Z3_NON_COMMERCIAL="yes"
    53 %s
    54 ''' % (isabelle_path, parallelism_options, more_settings)
    55 
    56     writer = open(path.join(loc_isabelle, 'etc', 'settings'), 'a')
    57     writer.write(extra_settings)
    58     writer.close()
    59 
    60 
    61 def extract_isabelle_run_timing(logdata):
    62 
    63     def to_secs(h, m, s):
    64         return (int(h) * 60 + int(m)) * 60 + int(s)
    65     pat = r'Finished (\S+) \((\d+):(\d+):(\d+) elapsed time, (\d+):(\d+):(\d+) cpu time'
    66     pat2 = r'Timing (\S+) \((\d+) threads, (\d+\.\d+)s elapsed time, (\d+\.\d+)s cpu time, (\d+\.\d+)s GC time\)'
    67     t = dict((name, {'elapsed': to_secs(eh,em,es), 'cpu': to_secs(ch,cm,cs)})
    68              for name, eh, em, es, ch, cm, cs in re.findall(pat, logdata))
    69     for name, threads, elapsed, cpu, gc in re.findall(pat2, logdata):
    70 
    71         if name not in t:
    72             t[name] = {}
    73 
    74         t[name]['threads'] = int(threads)
    75         t[name]['elapsed_inner'] = elapsed
    76         t[name]['cpu_inner'] = cpu
    77         t[name]['gc'] = gc
    78 
    79     return t
    80 
    81 
    82 def extract_isabelle_run_summary(logdata):
    83 
    84     re_error = re.compile(r'^(?:make: )?\*\*\* (.*)$', re.MULTILINE)
    85     summary = '\n'.join(re_error.findall(logdata))
    86     if summary == '':
    87         summary = 'ok'
    88 
    89     return summary
    90 
    91 
    92 def isabelle_usedir(env, isa_path, isabelle_usedir_opts, base_image, dir_name):
    93 
    94     return env.run_process('%s/bin/isabelle' % isa_path, 'usedir',
    95         isabelle_usedir_opts, base_image, dir_name)
    96 
    97 
    98 def isabelle_dependency_only(env, case, paths, dep_paths, playground):
    99 
   100     isabelle_home = paths[0]
   101     result = path.join(isabelle_home, 'heaps')
   102     os.makedirs(result)
   103     for dep_path in dep_paths:
   104         subprocess.check_call(['cp', '-R'] + glob(dep_path + '/*') + [result])
   105 
   106     return (True, 'ok', {}, {}, result)
   107 
   108 
   109 def build_isabelle_image(subdir, base, img, env, case, paths, dep_paths, playground, more_settings=''):
   110 
   111     isabelle_home = paths[0]
   112     dep_path = dep_paths[0]
   113     prepare_isabelle_repository(p, env.settings.contrib, dep_path, more_settings=more_settings)
   114     os.chdir(path.join(isabelle_home, 'src', subdir))
   115 
   116     (return_code, log) = isabelle_usedir(env, isabelle_home, '-b', base, img)
   117 
   118     result = path.join(isabelle_home, 'heaps')
   119     return (return_code == 0, extract_isabelle_run_summary(log),
   120       {'timing': extract_isabelle_run_timing(log)}, {'log': log}, result)
   121 
   122 
   123 def isabelle_make(subdir, env, case, paths, dep_paths, playground, more_settings='', target='all'):
   124 
   125     isabelle_home = paths[0]
   126     dep_path = dep_paths[0] if dep_paths else None
   127     prepare_isabelle_repository(isabelle_home, env.settings.contrib, dep_path, more_settings=more_settings)
   128     os.chdir(path.join(isabelle_home, subdir))
   129 
   130     (return_code, log) = env.run_process('%s/bin/isabelle' % isabelle_home, 'make', '-k', target)
   131 
   132     return (return_code == 0, extract_isabelle_run_summary(log),
   133       {'timing': extract_isabelle_run_timing(log)}, {'log': log}, None)
   134 
   135 
   136 def isabelle_makeall(env, case, paths, dep_paths, playground, more_settings='', target='all'):
   137 
   138     isabelle_home = paths[0]
   139     dep_path = dep_paths[0] if dep_paths else None
   140     prepare_isabelle_repository(isabelle_home, env.settings.contrib, dep_path, more_settings=more_settings)
   141     os.chdir(isabelle_home)
   142 
   143     (return_code, log) = env.run_process('%s/bin/isabelle' % isabelle_home, 'makeall', '-k', target)
   144 
   145     return (return_code == 0, extract_isabelle_run_summary(log),
   146       {'timing': extract_isabelle_run_timing(log)}, {'log': log}, None)
   147 
   148 
   149 # Isabelle configurations
   150 
   151 @configuration(repos = [Isabelle], deps = [])
   152 def Pure(env, case, paths, dep_paths, playground):
   153     """Pure image"""
   154 
   155     isabelle_home = paths[0]
   156     prepare_isabelle_repository(isabelle_home, env.settings.contrib, '')
   157     os.chdir(path.join(isabelle_home, 'src', 'Pure'))
   158 
   159     (return_code, log) = env.run_process('%s/bin/isabelle' % isabelle_home, 'make', 'Pure')
   160 
   161     result = path.join(isabelle_home, 'heaps')
   162     return (return_code == 0, extract_isabelle_run_summary(log),
   163       {'timing': extract_isabelle_run_timing(log)}, {'log': log}, result)
   164 
   165 @configuration(repos = [Isabelle], deps = [(Pure, [0])])
   166 def FOL(*args):
   167     """FOL image"""
   168     return build_isabelle_image('FOL', 'Pure', 'FOL', *args)
   169 
   170 @configuration(repos = [Isabelle], deps = [(Pure, [0])])
   171 def HOL(*args):
   172     """HOL image"""
   173     return build_isabelle_image('HOL', 'Pure', 'HOL', *args)
   174 
   175 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   176 def HOL_HOLCF(*args):
   177     """HOL-HOLCF image"""
   178     return build_isabelle_image('HOL/HOLCF', 'HOL', 'HOLCF', *args)
   179 
   180 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   181 def HOL_Nominal(*args):
   182     """HOL-Nominal image"""
   183     return build_isabelle_image('HOL/Nominal', 'HOL', 'HOL-Nominal', *args)
   184 
   185 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   186 def HOL_Word(*args):
   187     """HOL-Word image"""
   188     return build_isabelle_image('HOL/Word', 'HOL', 'HOL-Word', *args)
   189 
   190 @configuration(repos = [Isabelle], deps = [
   191     (HOL, [0]),
   192     (HOL_HOLCF, [0]),
   193     (HOL_Nominal, [0]),
   194     (HOL_Word, [0])
   195   ])
   196 def AFP_images(*args):
   197     """Isabelle images needed for the AFP"""
   198     return isabelle_dependency_only(*args)
   199 
   200 @configuration(repos = [Isabelle], deps = [
   201     (AFP_images, [0])
   202   ])
   203 def Isabelle_makeall(*args):
   204     """Isabelle makeall"""
   205     return isabelle_makeall(*args)
   206 
   207 
   208 # Mutabelle configurations
   209 
   210 def invoke_mutabelle(theory, env, case, paths, dep_paths, playground):
   211 
   212     """Mutant testing for counterexample generators in Isabelle"""
   213 
   214     (loc_isabelle,) = paths
   215     (dep_isabelle,) = dep_paths
   216     prepare_isabelle_repository(loc_isabelle, env.settings.contrib, dep_isabelle)
   217     os.chdir(loc_isabelle)
   218     
   219     (return_code, log) = env.run_process('bin/isabelle',
   220       'mutabelle', '-O', playground, theory)
   221     
   222     try:
   223         mutabelle_log = util.readfile(path.join(playground, 'log'))
   224     except IOError:
   225         mutabelle_log = ''
   226 
   227     return (return_code == 0 and mutabelle_log != '', extract_isabelle_run_summary(log),
   228       {'timing': extract_isabelle_run_timing(log)},
   229       {'log': log, 'mutabelle_log': mutabelle_log}, None)
   230 
   231 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   232 def Mutabelle_Relation(*args):
   233     """Mutabelle regression suite on Relation theory"""
   234     return invoke_mutabelle('Relation', *args)
   235 
   236 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   237 def Mutabelle_List(*args):
   238     """Mutabelle regression suite on List theory"""
   239     return invoke_mutabelle('List', *args)
   240 
   241 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   242 def Mutabelle_Set(*args):
   243     """Mutabelle regression suite on Set theory"""
   244     return invoke_mutabelle('Set', *args)
   245 
   246 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   247 def Mutabelle_Map(*args):
   248     """Mutabelle regression suite on Map theory"""
   249     return invoke_mutabelle('Map', *args)
   250 
   251 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   252 def Mutabelle_Divides(*args):
   253     """Mutabelle regression suite on Divides theory"""
   254     return invoke_mutabelle('Divides', *args)
   255 
   256 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   257 def Mutabelle_MacLaurin(*args):
   258     """Mutabelle regression suite on MacLaurin theory"""
   259     return invoke_mutabelle('MacLaurin', *args)
   260 
   261 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   262 def Mutabelle_Fun(*args):
   263     """Mutabelle regression suite on Fun theory"""
   264     return invoke_mutabelle('Fun', *args)
   265 
   266 
   267 # Judgement Day configurations
   268 
   269 judgement_day_provers = ('e', 'spass', 'vampire', 'z3', 'cvc3', 'yices')
   270 
   271 def judgement_day(base_path, theory, opts, env, case, paths, dep_paths, playground):
   272     """Judgement Day regression suite"""
   273 
   274     isa = paths[0]
   275     dep_path = dep_paths[0]
   276 
   277     os.chdir(path.join(playground, '..', base_path)) # Mirabelle requires specific cwd
   278     prepare_isabelle_repository(isa, env.settings.contrib, dep_path)
   279 
   280     output = {}
   281     success_rates = {}
   282     some_success = False
   283 
   284     for atp in judgement_day_provers:
   285 
   286         log_dir = path.join(playground, 'mirabelle_log_' + atp)
   287         os.makedirs(log_dir)
   288 
   289         cmd = ('%s/bin/isabelle mirabelle -q -O %s sledgehammer[prover=%s,%s] %s.thy'
   290                % (isa, log_dir, atp, opts, theory))
   291 
   292         os.system(cmd)
   293         output[atp] = util.readfile(path.join(log_dir, theory + '.log'))
   294 
   295         percentages = list(re.findall(r'Success rate: (\d+)%', output[atp]))
   296         if len(percentages) == 2:
   297             success_rates[atp] = {
   298                 'sledgehammer': int(percentages[0]),
   299                 'metis': int(percentages[1])}
   300             if success_rates[atp]['sledgehammer'] > 0:
   301                 some_success = True
   302         else:
   303             success_rates[atp] = {}
   304 
   305 
   306     data = {'success_rates': success_rates}
   307     raw_attachments = dict((atp + "_output", output[atp]) for atp in judgement_day_provers)
   308     # FIXME: summary?
   309     return (some_success, '', data, raw_attachments, None)
   310 
   311 
   312 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   313 def JD_NS(*args):
   314     """Judgement Day regression suite NS"""
   315     return judgement_day('Isabelle/src/HOL/Auth', 'NS_Shared', 'prover_timeout=10', *args)
   316 
   317 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   318 def JD_FTA(*args):
   319     """Judgement Day regression suite FTA"""
   320     return judgement_day('Isabelle/src/HOL/Library', 'Fundamental_Theorem_Algebra', 'prover_timeout=10', *args)
   321 
   322 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   323 def JD_Hoare(*args):
   324     """Judgement Day regression suite Hoare"""
   325     return judgement_day('Isabelle/src/HOL/IMPP', 'Hoare', 'prover_timeout=10', *args)
   326 
   327 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   328 def JD_SN(*args):
   329     """Judgement Day regression suite SN"""
   330     return judgement_day('Isabelle/src/HOL/Proofs/Lambda', 'StrongNorm', 'prover_timeout=10', *args)
   331 
   332 
   333 # SML/NJ
   334 
   335 smlnj_settings = '''
   336 ML_SYSTEM=smlnj
   337 ML_HOME="/home/smlnj/110.72/bin"
   338 ML_OPTIONS="@SMLdebug=/dev/null @SMLalloc=256"
   339 ML_PLATFORM=$(eval $("$ML_HOME/.arch-n-opsys" 2>/dev/null); echo "$HEAP_SUFFIX")
   340 '''
   341 
   342 @configuration(repos = [Isabelle], deps = [])
   343 def SML_HOL(*args):
   344     """HOL image built with SML/NJ"""
   345     return isabelle_make('src/HOL', *args, more_settings=smlnj_settings, target='HOL')
   346 
   347 @configuration(repos = [Isabelle], deps = [])
   348 def SML_makeall(*args):
   349     """Makeall built with SML/NJ"""
   350     return isabelle_makeall(*args, more_settings=smlnj_settings)