Admin/mira.py
author wenzelm
Sat May 14 19:49:10 2016 +0200 (2016-05-14 ago)
changeset 63094 056ea294c256
parent 59993 8f6cacc87f42
permissions -rw-r--r--
toplevel theorem statements support 'if'/'for' eigen-context;
     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 from datetime import datetime
    10 import re
    11 
    12 import util
    13 
    14 from mira import schedule, misc
    15 from mira.environment import scheduler
    16 from mira import repositories
    17 
    18 # build and evaluation tools
    19 
    20 DEFAULT_TIMEOUT = 2 * 60 * 60
    21 SMLNJ_TIMEOUT = 72 * 60 * 60
    22 
    23 def prepare_isabelle_repository(loc_isabelle, loc_dependency_heaps, more_settings=''):
    24 
    25     # patch settings
    26     extra_settings = '''
    27 
    28 init_components "/home/isabelle/contrib" "$ISABELLE_HOME/Admin/components/main"
    29 init_components "/home/isabelle/contrib" "$ISABELLE_HOME/Admin/components/optional"
    30 init_components "/home/isabelle/contrib" "$ISABELLE_HOME/Admin/components/nonfree"
    31 
    32 ''' + more_settings
    33 
    34     writer = open(path.join(loc_isabelle, 'etc', 'settings'), 'a')
    35     writer.write(extra_settings)
    36     writer.close()
    37 
    38 
    39 def isabelle_getenv(isabelle_home, var):
    40 
    41     _, out = env.run_process('%s/bin/isabelle' % isabelle_home, 'getenv', var)
    42     return out.split('=', 1)[1].strip()
    43 
    44 
    45 def extract_isabelle_run_timing(logdata):
    46 
    47     def to_secs(h, m, s):
    48         return (int(h) * 60 + int(m)) * 60 + int(s)
    49     pat = r'Finished (\S+) \((\d+):(\d+):(\d+) elapsed time, (\d+):(\d+):(\d+) cpu time'
    50     pat2 = r'Timing (\S+) \((\d+) threads, (\d+\.\d+)s elapsed time, (\d+\.\d+)s cpu time, (\d+\.\d+)s GC time, factor (\d+\.\d+)\)'
    51     t = dict((name, {'elapsed': to_secs(eh,em,es), 'cpu': to_secs(ch,cm,cs)})
    52              for name, eh, em, es, ch, cm, cs in re.findall(pat, logdata))
    53     for name, threads, elapsed, cpu, gc, factor in re.findall(pat2, logdata):
    54 
    55         if name not in t:
    56             t[name] = {}
    57 
    58         t[name]['threads'] = int(threads)
    59         t[name]['elapsed_inner'] = elapsed
    60         t[name]['cpu_inner'] = cpu
    61         t[name]['gc'] = gc
    62         t[name]['factor'] = factor
    63 
    64     return t
    65 
    66 
    67 def extract_isabelle_run_summary(logdata):
    68 
    69     re_error = re.compile(r'^(?:make: )?\*\*\* (.*)$', re.MULTILINE)
    70     summary = '\n'.join(re_error.findall(logdata))
    71     if summary == '':
    72         summary = 'ok'
    73 
    74     return summary
    75 
    76 
    77 def extract_image_size(isabelle_home):
    78     
    79     isabelle_output = isabelle_getenv(isabelle_home, 'ISABELLE_OUTPUT')
    80     if not path.exists(isabelle_output):
    81         return {}
    82 
    83     return dict((p, path.getsize(path.join(isabelle_output, p))) for p in os.listdir(isabelle_output) if p != "log")
    84 
    85 def extract_report_data(isabelle_home, logdata):
    86 
    87     return {
    88         'timing': extract_isabelle_run_timing(logdata),
    89         'image_size': extract_image_size(isabelle_home) }
    90 
    91 
    92 def isabelle_build(env, case, paths, dep_paths, playground, *cmdargs, **kwargs):
    93 
    94     more_settings = kwargs.get('more_settings', '')
    95     keep_results = kwargs.get('keep_results', True)
    96     timeout = kwargs.get('timeout', DEFAULT_TIMEOUT)
    97 
    98     isabelle_home = paths[0]
    99 
   100     home_user_dir = path.join(isabelle_home, 'home_user')
   101     os.makedirs(home_user_dir)
   102 
   103     # copy over build results from dependencies
   104     heap_dir = path.join(isabelle_home, 'heaps')
   105     classes_dir = path.join(heap_dir, 'classes')
   106     os.makedirs(classes_dir)
   107 
   108     for dep_path in dep_paths:
   109         subprocess.check_call(['cp', '-a'] + glob(dep_path + '/*') + [heap_dir])
   110 
   111     subprocess.check_call(['ln', '-s', classes_dir, path.join(isabelle_home, 'lib', 'classes')])
   112     jars = glob(path.join(classes_dir, 'ext', '*.jar'))
   113     if jars:
   114         subprocess.check_call(['touch'] + jars)
   115 
   116     # misc preparations
   117     if 'lxbroy10' in misc.hostnames():  # special settings for lxbroy10
   118         more_settings += '''
   119 ISABELLE_GHC="/usr/bin/ghc"
   120 '''
   121 
   122     prepare_isabelle_repository(isabelle_home, None, more_settings=more_settings)
   123     os.chdir(isabelle_home)
   124 
   125     args = (['-o', 'timeout=%s' % timeout] if timeout is not None else []) + list(cmdargs)
   126 
   127     # invoke build tool
   128     (return_code, log1) = env.run_process('%s/bin/isabelle' % isabelle_home, 'jedit', '-bf',
   129             USER_HOME=home_user_dir)
   130     (return_code, log2) = env.run_process('%s/bin/isabelle' % isabelle_home, 'build', '-s', '-v', *args,
   131             USER_HOME=home_user_dir)
   132     log = log1 + log2
   133 
   134     # collect report
   135     return (return_code == 0, extract_isabelle_run_summary(log),
   136       extract_report_data(isabelle_home, log), {'log': log}, heap_dir if keep_results else None)
   137 
   138 
   139 
   140 # Isabelle configurations
   141 
   142 @configuration(repos = [Isabelle], deps = [])
   143 def Pure(*args):
   144     """Pure Image"""
   145     return isabelle_build(*(args + ("-b", "Pure")))
   146 
   147 @configuration(repos = [Isabelle], deps = [(Pure, [0])])
   148 def HOL(*args):
   149     """HOL Image"""
   150     return isabelle_build(*(args + ("-b", "HOL")))
   151 
   152 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   153 def HOL_Library(*args):
   154     """HOL Library"""
   155     return isabelle_build(*(args + ("-b", "HOL-Library")))
   156 
   157 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   158 def HOLCF(*args):
   159     """HOLCF"""
   160     return isabelle_build(*(args + ("-b", "HOLCF")))
   161 
   162 @configuration(repos = [Isabelle], deps = [(Pure, [0])])
   163 def ZF(*args):
   164     """HOLCF"""
   165     return isabelle_build(*(args + ("-b", "ZF")))
   166 
   167 
   168 settings64='''
   169 # enforce 64 bit, overriding smart detection
   170 ML_PLATFORM="$ISABELLE_PLATFORM64"
   171 ML_HOME="$(dirname $ML_HOME)/$ML_PLATFORM"
   172 '''
   173 
   174 @configuration(repos = [Isabelle], deps = [])
   175 def Isabelle_makeall(*args):
   176     """Build all sessions"""
   177     return isabelle_build(*(args + ("-j", "6", "-o", "threads=4", "-a")), more_settings=settings64, keep_results=False)
   178 
   179 
   180 # Mutabelle configurations
   181 
   182 def invoke_mutabelle(theory, env, case, paths, dep_paths, playground):
   183 
   184     """Mutant testing for counterexample generators in Isabelle"""
   185 
   186     (loc_isabelle,) = paths
   187     (dep_isabelle,) = dep_paths
   188     more_settings = '''
   189 ISABELLE_GHC="/usr/bin/ghc"
   190 '''
   191     prepare_isabelle_repository(loc_isabelle, dep_isabelle, more_settings = more_settings)
   192     os.chdir(loc_isabelle)
   193     
   194     (return_code, log) = env.run_process('bin/isabelle',
   195       'mutabelle', '-O', playground, theory)
   196     
   197     try:
   198         mutabelle_log = util.readfile(path.join(playground, 'log'))
   199     except IOError:
   200         mutabelle_log = ''
   201 
   202     mutabelle_data = dict(
   203         (tool, {'counterexample': c, 'no_counterexample': n, 'timeout': t, 'error': e})
   204         for tool, c, n, t, e in re.findall(r'(\S+)\s+: C: (\d+) N: (\d+) T: (\d+) E: (\d+)', log))
   205 
   206     return (return_code == 0 and mutabelle_log != '', extract_isabelle_run_summary(log),
   207       {'mutabelle_results': {theory: mutabelle_data}},
   208       {'log': log, 'mutabelle_log': mutabelle_log}, None)
   209 
   210 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   211 def Mutabelle_Relation(*args):
   212     """Mutabelle regression suite on Relation theory"""
   213     return invoke_mutabelle('Relation', *args)
   214 
   215 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   216 def Mutabelle_List(*args):
   217     """Mutabelle regression suite on List theory"""
   218     return invoke_mutabelle('List', *args)
   219 
   220 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   221 def Mutabelle_Set(*args):
   222     """Mutabelle regression suite on Set theory"""
   223     return invoke_mutabelle('Set', *args)
   224 
   225 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   226 def Mutabelle_Map(*args):
   227     """Mutabelle regression suite on Map theory"""
   228     return invoke_mutabelle('Map', *args)
   229 
   230 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   231 def Mutabelle_Divides(*args):
   232     """Mutabelle regression suite on Divides theory"""
   233     return invoke_mutabelle('Divides', *args)
   234 
   235 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   236 def Mutabelle_MacLaurin(*args):
   237     """Mutabelle regression suite on MacLaurin theory"""
   238     return invoke_mutabelle('MacLaurin', *args)
   239 
   240 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   241 def Mutabelle_Fun(*args):
   242     """Mutabelle regression suite on Fun theory"""
   243     return invoke_mutabelle('Fun', *args)
   244 
   245 mutabelle_confs = 'Mutabelle_Relation Mutabelle_List Mutabelle_Set Mutabelle_Map Mutabelle_Divides Mutabelle_MacLaurin Mutabelle_Fun'.split(' ')
   246 
   247 @scheduler()
   248 def mutabelle_scheduler(env):
   249     """Scheduler for Mutabelle."""
   250     return schedule.age_scheduler(env, 'Isabelle', mutabelle_confs)
   251 
   252 
   253 # Judgement Day configurations
   254 
   255 judgement_day_provers = ('e', 'spass', 'vampire', 'z3', 'cvc3', 'yices')
   256 
   257 def judgement_day(base_path, theory, opts, env, case, paths, dep_paths, playground):
   258     """Judgement Day regression suite"""
   259 
   260     isa = paths[0]
   261     dep_path = dep_paths[0]
   262 
   263     os.chdir(path.join(playground, '..', base_path)) # Mirabelle requires specific cwd
   264     prepare_isabelle_repository(isa, dep_path)
   265 
   266     output = {}
   267     success_rates = {}
   268     some_success = False
   269 
   270     for atp in judgement_day_provers:
   271 
   272         log_dir = path.join(playground, 'mirabelle_log_' + atp)
   273         os.makedirs(log_dir)
   274 
   275         cmd = ('%s/bin/isabelle mirabelle -q -O %s sledgehammer[prover=%s,%s] %s.thy'
   276                % (isa, log_dir, atp, opts, theory))
   277 
   278         os.system(cmd)
   279         output[atp] = util.readfile(path.join(log_dir, theory + '.log'))
   280 
   281         percentages = list(re.findall(r'Success rate: (\d+)%', output[atp]))
   282         if len(percentages) == 2:
   283             success_rates[atp] = {
   284                 'sledgehammer': int(percentages[0]),
   285                 'metis': int(percentages[1])}
   286             if success_rates[atp]['sledgehammer'] > 0:
   287                 some_success = True
   288         else:
   289             success_rates[atp] = {}
   290 
   291 
   292     data = {'success_rates': success_rates}
   293     raw_attachments = dict((atp + "_output", output[atp]) for atp in judgement_day_provers)
   294     # FIXME: summary?
   295     return (some_success, '', data, raw_attachments, None)
   296 
   297 
   298 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   299 def JD_NS(*args):
   300     """Judgement Day regression suite NS"""
   301     return judgement_day('Isabelle/src/HOL/Auth', 'NS_Shared', 'prover_timeout=10', *args)
   302 
   303 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   304 def JD_FTA(*args):
   305     """Judgement Day regression suite FTA"""
   306     return judgement_day('Isabelle/src/HOL/Library', 'Fundamental_Theorem_Algebra', 'prover_timeout=10', *args)
   307 
   308 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   309 def JD_Hoare(*args):
   310     """Judgement Day regression suite Hoare"""
   311     return judgement_day('Isabelle/src/HOL/IMPP', 'Hoare', 'prover_timeout=10', *args)
   312 
   313 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   314 def JD_SN(*args):
   315     """Judgement Day regression suite SN"""
   316     return judgement_day('Isabelle/src/HOL/Proofs/Lambda', 'StrongNorm', 'prover_timeout=10', *args)
   317 
   318 
   319 JD_confs = 'JD_NS JD_FTA JD_Hoare JD_SN JD_Arrow JD_FFT JD_Jinja JD_QE JD_S2S'.split(' ')
   320 
   321 @scheduler()
   322 def judgement_day_scheduler(env):
   323     """Scheduler for Judgement Day."""
   324     return schedule.age_scheduler(env, 'Isabelle', JD_confs)
   325 
   326 
   327 # SML/NJ
   328 
   329 smlnj_settings = '''
   330 ML_SYSTEM=smlnj
   331 ML_HOME="/home/smlnj/110.76/bin"
   332 ML_OPTIONS="@SMLdebug=/dev/null @SMLalloc=1024"
   333 ML_PLATFORM=$(eval $("$ML_HOME/.arch-n-opsys" 2>/dev/null); echo "$HEAP_SUFFIX")
   334 '''
   335 
   336 @configuration(repos = [Isabelle], deps = [(Pure, [0])])
   337 def SML_HOL(*args):
   338     """HOL image built with SML/NJ"""
   339     return isabelle_build(*(args + ("-b", "HOL")), more_settings=smlnj_settings, timeout=SMLNJ_TIMEOUT)
   340 
   341 @configuration(repos = [Isabelle], deps = [])
   342 def SML_makeall(*args):
   343     """SML/NJ build of all possible sessions"""
   344     return isabelle_build(*(args + ("-j", "3", "-a")), more_settings=smlnj_settings, timeout=SMLNJ_TIMEOUT)
   345 
   346