def compile(*infiles, **kwargs): ''' Compile rules -> C++. Optional keyword arguments: cpp_out=$POMAGMA_ROOT/src/surveyor/<STEM>.theory.cpp theory_out=$POMAGMA_ROOT/src/theory/<STEM>.compiled theory_out=FILENAME extensional=true ''' stem = infiles[-1].split('.')[0] cpp_out = kwargs.get( 'cpp_out', os.path.join(POMAGMA_SRC, 'surveyor', '{0}.theory.cpp'.format(stem))) theory_out = kwargs.get( 'theory_out', os.path.join(POMAGMA_SRC, 'theory', '{0}.compiled'.format(stem))) parse_bool = lambda s: {'true': True, 'false': False}[s.lower()] extensional = parse_bool(kwargs.get('extensional', 'true')) print '# writing', cpp_out argstring = ' '.join( list(infiles) + ['{0}={1}'.format(key, val) for key, val in kwargs.iteritems()]) rules = [] facts = [] for infile in infiles: suffix = infile.split('.')[-1] if suffix == 'rules': rules += parser.parse_rules(infile) elif suffix == 'facts': facts += parser.parse_facts(infile) else: raise TypeError('unknown file type: %s' % infile) if extensional: for rule in rules: facts += derive_facts(rule) code = cpp.Code() code(''' // This file was auto generated by pomagma using: // python run.py compile $argstring ''', argstring=argstring, ).newline() cpp.write_theory(code, rules, facts) with open(cpp_out, 'w') as f: f.write(str(code)) with open(theory_out, 'w') as f: thisfile = os.path.abspath(__file__) f.write('# this file was generated by {0}'.format(thisfile)) for fact in facts: assert fact.is_rel(), 'bad fact: %s' % fact f.write('\n') f.write(fact.polish)
def test_close_rules(infile, is_extensional=True): ''' Compile extensionally some.theory -> some.derived.facts. ''' assert infile.endswith('.theory') rules = load_theory(infile)['rules'] for rule in rules: print print '#', rule for fact in extensional.derive_facts(rule): print fact if is_extensional: extensional.validate(fact)
def test_close_rules(infile, is_extensional=True): ''' Compile extensionally some.rules -> some.derived.facts ''' assert infile.endswith('.rules') rules = parser.parse_rules(infile) for rule in rules: print print '#', rule for fact in derive_facts(rule): print fact if is_extensional: validate(fact)
def test_close_rules(infile, is_extensional=True): """ Compile extensionally some.theory -> some.derived.facts. """ assert infile.endswith(".theory") rules = load_theory(infile)["rules"] for rule in rules: print print "#", rule for fact in extensional.derive_facts(rule): print fact if is_extensional: extensional.validate(fact)
def extract_tasks(infile, outfile=None): """Extract tasks from facts and rules, but do not compile to programs.""" theory = load_theory(infile) with writer(outfile) as write: facts = theory['facts'] for sequent in theory['rules']: facts += extensional.derive_facts(sequent) facts.sort() write('\n'.join(map(str, facts))) for sequent in theory['rules']: write(sequent.ascii()) for normal in sorted(compiler.normalize(sequent)): write(normal.ascii(indent=4)) for event in sorted(compiler.get_events(sequent)): write(' Given: {}'.format(event)) for normal in sorted(compiler.normalize_given(sequent, event)): write(normal.ascii(indent=8))
def compile(*infiles, **kwargs): ''' Compile rules -> programs for the virtual machine. Optional keyword arguments: symbols_out=$POMAGMA_ROOT/src/theory/<STEM>.symbols facts_out=$POMAGMA_ROOT/src/theory/<STEM>.facts programs_out=$POMAGMA_ROOT/src/theory/<STEM>.programs optimized_out=$POMAGMA_ROOT/src/theory/<STEM>.optimized.programs extensional=true ''' stem = infiles[-1].split('.')[0] symbols_out = kwargs.get( 'symbols_out', os.path.join(SRC, 'theory', '{0}.symbols'.format(stem))) facts_out = kwargs.get( 'facts_out', os.path.join(SRC, 'theory', '{0}.facts'.format(stem))) programs_out = kwargs.get( 'programs_out', os.path.join(SRC, 'theory', '{0}.programs'.format(stem))) optimized_out = kwargs.get( 'optimized_out', os.path.join(SRC, 'theory', '{0}.optimized.programs'.format(stem))) is_extensional = parse_bool(kwargs.get('extensional', 'true')) argstring = ' '.join( [relpath(path) for path in infiles] + [ '{0}={1}'.format(key, relpath(path)) for key, path in kwargs.iteritems() ]) header = ( '# This file was auto generated by pomagma using:\n' '# python -m pomagma.compiler compile {0}'.format(argstring) ) rules = [] facts = [] for infile in infiles: theory = load_theory(infile) rules += theory['rules'] facts += theory['facts'] if is_extensional: for rule in rules: facts += extensional.derive_facts(rule) rules.sort() facts.sort() symbols = frontend.write_symbols(rules, facts) with open(symbols_out, 'w') as f: print '# writing', symbols_out f.write(header) for arity, name in symbols: f.write('\n{} {}'.format(arity, name)) with open(facts_out, 'w') as f: print '# writing', facts_out f.write(header) for fact in facts: assert fact.is_rel(), 'bad fact: %s' % fact f.write('\n') f.write(fact.polish) programs = frontend.write_programs(rules) with open(programs_out, 'w') as f: print '# writing', programs_out f.write(header) for line in programs: f.write('\n') f.write(line) lines = sequencer.load_lines(programs_out) optimized = sequencer.optimize(lines) with open(optimized_out, 'w') as f: print '# writing', optimized_out f.write(header) for line in optimized: f.write('\n') f.write(line)
def compile(*infiles, **kwargs): """ Compile rules -> programs for the virtual machine. Optional keyword arguments: symbols_out=$POMAGMA_ROOT/src/theory/<STEM>.symbols facts_out=$POMAGMA_ROOT/src/theory/<STEM>.facts programs_out=$POMAGMA_ROOT/src/theory/<STEM>.programs optimized_out=$POMAGMA_ROOT/src/theory/<STEM>.optimized.programs extensional=true """ stem = infiles[-1].split(".")[0] symbols_out = kwargs.get("symbols_out", os.path.join(SRC, "theory", "{0}.symbols".format(stem))) facts_out = kwargs.get("facts_out", os.path.join(SRC, "theory", "{0}.facts".format(stem))) programs_out = kwargs.get("programs_out", os.path.join(SRC, "theory", "{0}.programs".format(stem))) optimized_out = kwargs.get("optimized_out", os.path.join(SRC, "theory", "{0}.optimized.programs".format(stem))) is_extensional = parse_bool(kwargs.get("extensional", "true")) argstring = " ".join( [relpath(path) for path in infiles] + ["{0}={1}".format(key, relpath(path)) for key, path in kwargs.iteritems()] ) header = "# This file was auto generated by pomagma using:\n" "# python -m pomagma.compiler compile {0}".format( argstring ) rules = [] facts = [] for infile in infiles: theory = load_theory(infile) rules += theory["rules"] facts += theory["facts"] if is_extensional: for rule in rules: facts += extensional.derive_facts(rule) rules.sort() facts.sort() symbols = frontend.write_symbols(rules, facts) with open(symbols_out, "w") as f: print "# writing", symbols_out f.write(header) for arity, name in symbols: f.write("\n{} {}".format(arity, name)) with open(facts_out, "w") as f: print "# writing", facts_out f.write(header) for fact in facts: assert fact.is_rel(), "bad fact: %s" % fact f.write("\n") f.write(fact.polish) programs = frontend.write_programs(rules) with open(programs_out, "w") as f: print "# writing", programs_out f.write(header) for line in programs: f.write("\n") f.write(line) lines = sequencer.load_lines(programs_out) optimized = sequencer.optimize(lines) with open(optimized_out, "w") as f: print "# writing", optimized_out f.write(header) for line in optimized: f.write("\n") f.write(line)