def test_compile_term(debug):
    from logic_utils import fresh_variable_name_generator
    from predicates.functions import _compile_term
    fresh_variable_name_generator._reset_for_test()

    for s,expected in [
            ['f(x,g(0))', ['z1=g(0)', 'z2=f(x,z1)']],
            ['f(g(x,h(0)),f(f(0,g(y)),h(h(x))))',
             ['z3=h(0)', 'z4=g(x,z3)', 'z5=g(y)', 'z6=f(0,z5)', 'z7=h(x)',
              'z8=h(z7)', 'z9=f(z6,z8)', 'z10=f(z4,z9)']],
            ['f(x,g(0))', ['z11=g(0)', 'z12=f(x,z11)']]]:
        term = Term.parse(s)
        if debug:
            print('Compiling', term, '...')
        steps = _compile_term(term)
        if debug:
            print('... got', steps)
        assert steps == [Formula.parse(e) for e in expected]
def test_propositional_skeleton(debug=False):
    from logic_utils import fresh_variable_name_generator
    fresh_variable_name_generator._reset_for_test()

    for s,expected,expected_map in [
            ['x=y', 'z1', {'z1': Formula.parse('x=y')}],
            ['R(x,c)', 'z2', {'z2': Formula.parse('R(x,c)')}],
            ['Ax[(R(x)|R(y))]', 'z3', {'z3': Formula.parse('Ax[(R(x)|R(y))]')}],
            ['~1=1', '~z4', {'z4': Formula.parse('1=1')}],
            ['(Ax[P(x)]&Ax[P(x)])', '(z5&z5)',
             {'z5': Formula.parse('Ax[P(x)]')}],
            ['(0=0&1=1)', '(z6&z7)',
             {'z6': Formula.parse('0=0'), 'z7': Formula.parse('1=1')}],
            ['((R(0)|R(1))&~R(0))', '((z8|z9)&~z8)',
             {'z8': Formula.parse('R(0)'), 'z9': Formula.parse('R(1)')}]]:
        skeleton, substitution_map = \
            Formula.parse(s).propositional_skeleton()
        if debug:
            print('The skeleton of', s, 'is', skeleton,
                  'with map', substitution_map)
        assert (str(skeleton), substitution_map) == (expected, expected_map)