def trace(*part_names, **kwargs): """Trace an approximation to A. Possible names: all base copy div bot swap preconj postconj compose Kwargs: steps = 10 type = '(FUN r (FUN s (FUN f (FUN x (r (f (s x)))))))' inhab = '(FUN x x)' fmt = 'sexpr' (one of: 'sexpr', 'tiny') """ print_ = PRINTERS[kwargs.get('fmt', 'sexpr')] # Construct an approximation of A with only a few parts. if 'all' in part_names: part_names = PARTS.keys() for name in part_names: print('{} = {}'.format(name, print_(simplify(PARTS[name])))) assert name in PARTS, name A = simplify(build_A(*part_names)) print('A = {}'.format(print_(A))) # Cast a candidate inhabitant via the defined type. type_ = sexpr_simplify(kwargs.get('type', default_type)) inhab = sexpr_simplify(kwargs.get('inhab', default_inhab)) term = simplify(app(A, type_, inhab)) print('0\t{}'.format(print_(term))) # Print a reduction sequence. steps = int(kwargs.get('steps', 10)) for step in xrange(steps): term = try_compute_step(term) if term is None: print '--- Normalized ---' return print('{}\t{}'.format(1 + step, print_(term))) print('... Not normalized ...')
#!/usr/bin/env python import os os.environ['POMAGMA_LOG_LEVEL'] = '3' from pomagma.compiler.util import temp_memoize # isort:skip from pomagma.reducer import bohm # isort:skip print('Example 1.') with temp_memoize(): bohm.sexpr_simplify('(ABS (ABS (1 0 (1 0))) (ABS (ABS (1 (0 0)))))') print('Example 2.') with temp_memoize(): bohm.sexpr_simplify('(ABS (ABS (0 1 1)) (ABS (ABS (1 (0 0)))))')
def test_try_compute_step_terminates(term): term = sexpr_simplify(term) bohm.try_compute_step(term)
def test_print_tiny(sexpr, expected): term = sexpr_simplify(sexpr) assert print_tiny(term) == expected
def test_sexpr_print_simplify(term): sexpr = sexpr_print(term) assert sexpr_print(sexpr_simplify(sexpr)) == sexpr
def test_sexpr_simplify(sexpr, expected): assert sexpr_print(sexpr_simplify(sexpr)) == expected
def test_print_tiny(sexpr, expected): code = sexpr_simplify(sexpr) assert print_tiny(code) == expected
def test_sexpr_print_simplify(code): sexpr = sexpr_print(code) assert sexpr_simplify(sexpr) is code
def test_sexpr_simplify(sexpr, expected): assert sexpr_simplify(sexpr) is sexpr_parse(expected)