def translate_program(filename, program, translation_type, options): program = cratylus.parse_program(program, filename, modulo=OPTIONS['source_modulo']) # Collect all polynomials all_polys = [] for rule in program.rules: if rule.is_goal(): all_polys.extend(rule.clause) else: all_polys.append(rule.head) all_polys.extend(rule.clause) # Count variables var_count = {} for poly in all_polys: if not poly.is_monomial(): raise SimpCrException('"%s" is not a monomial' % (poly, )) key, coef = poly.coefficients().items()[0] if coef != 1: raise SimpCrException( '"%s" is not in monomial form (coeff should be 1)' % (poly, )) for var, power in key: if var not in options['initial_table']: var_count[var] = var_count.get(var, 0) + 1 # Build translation table num_vars = len(var_count) # number of distinct variables old_vars = sorted(var_count.items(), key=lambda (v, c): -c) old_vars = [v for v, c in old_vars] new_vars = irreducible_elements(num_vars, translation_type) new_vars = [add_prefix(v, options['prefix']) for v in new_vars] table = dict(zip(old_vars, new_vars)) comment = [] for old, new in sorted(table.items() + options['initial_table'].items()): msg = '# %s --> %s' % (old, new.repr_compact(OPTIONS['compact'])) comment.append(msg) # Translate program rules2 = [] for rule in program.rules: if rule.is_goal(): r = cratylus.Goal( [translate_monomial(table, m, options) for m in rule.clause]) else: r = cratylus.Rule( translate_monomial(table, rule.head, options), [translate_monomial(table, m, options) for m in rule.clause]) rules2.append(r) return '\n'.join(comment) + '\n\n' + cratylus.Program(rules2).repr_compact( OPTIONS['compact'])
def cratylus_compile(filename, program): program = cratylus.parse_program(program, filename) all_polys = collect_polys(program) if in_monomial_form(all_polys): return crc(program) elif in_univariate_form(all_polys): return uni_crc(program) else: raise CrcException( 'program is not in monomial or univariate form, cannot compile')
def translate_program(filename, program, translation_type, options): program = cratylus.parse_program(program, filename, modulo=OPTIONS['source_modulo']) # Collect all polynomials all_polys = [] for rule in program.rules: if rule.is_goal(): all_polys.extend(rule.clause) else: all_polys.append(rule.head) all_polys.extend(rule.clause) # Count variables var_count = {} for poly in all_polys: if not poly.is_monomial(): raise SimpCrException('"%s" is not a monomial' % (poly,)) key, coef = poly.coefficients().items()[0] if coef != 1: raise SimpCrException('"%s" is not in monomial form (coeff should be 1)' % (poly,)) for var, power in key: if var not in options['initial_table']: var_count[var] = var_count.get(var, 0) + 1 # Build translation table num_vars = len(var_count) # number of distinct variables old_vars = sorted(var_count.items(), key=lambda (v, c): -c) old_vars = [v for v, c in old_vars] new_vars = irreducible_elements(num_vars, translation_type) new_vars = [add_prefix(v, options['prefix']) for v in new_vars] table = dict(zip(old_vars, new_vars)) comment = [] for old, new in sorted(table.items() + options['initial_table'].items()): msg = '# %s --> %s' % (old, new.repr_compact(OPTIONS['compact'])) comment.append(msg) # Translate program rules2 = [] for rule in program.rules: if rule.is_goal(): r = cratylus.Goal([translate_monomial(table, m, options) for m in rule.clause]) else: r = cratylus.Rule( translate_monomial(table, rule.head, options), [translate_monomial(table, m, options) for m in rule.clause]) rules2.append(r) return '\n'.join(comment) + '\n\n' + cratylus.Program(rules2).repr_compact(OPTIONS['compact'])
def normalize_program(filename, program): return repr(cratylus.parse_program(program, filename, modulo=OPTIONS['source_modulo']))
def normalize_program(filename, program): return repr( cratylus.parse_program(program, filename, modulo=OPTIONS['source_modulo']))