Exemplo n.º 1
0
def output_hint(state, step):
    method = global_methods[step['method_name']]
    res = method.display_step(state, step)
    if '_goal' in step:
        if step['_goal']:
            goals = [printer.print_term(t) for t in step['_goal']]
            res += pprint.KWRed(" goal ") + printer.commas_join(goals)
        else:
            res += pprint.KWGreen(" (solves)")

    if '_fact' in step and len(step['_fact']) > 0:
        facts = [printer.print_term(t) for t in step['_fact']]
        res += pprint.KWGreen(" fact ") + printer.commas_join(facts)

    return res
Exemplo n.º 2
0
    def testPrintSet(self):
        A = Var("A", set.setT(Ta))
        B = Var("B", set.setT(Ta))
        x = Var("x", Ta)
        test_data = [
            (set.empty_set(Ta), "({}::'a set)", "(∅::'a set)"),
            (set.mk_mem(x, A), "x MEM A", "x ∈ A"),
            (set.mk_subset(A, B), "A SUB B", "A ⊆ B"),
            (set.mk_inter(A, B), "A INTER B", "A ∩ B"),
            (set.mk_union(A, B), "A UNION B", "A ∪ B"),
        ]

        for t, s1, s2 in test_data:
            self.assertEqual(printer.print_term(thy, t), s1)
            self.assertEqual(printer.print_term(thy, t, unicode=True), s2)
Exemplo n.º 3
0
def display_term(t):
    """Display parsed term."""
    res = printer.print_term(t)
    if not settings.highlight and settings.line_length is not None:
        return '\n'.join(res)
    else:
        return res
Exemplo n.º 4
0
    def testParseUnicode(self):
        test_data = [
            ("A ∧ B", "A & B"),
            ("A ∨ B", "A | B"),
            ("A ⟶ B ⟶ C", "A --> B --> C"),
            ("A ∧ B | C", "A & B | C"),
            ("¬A", "~A"),
            ("λx::'a. x", "%x::'a. x"),
            ("∀x::'a. P x", "!x. P x"),
            ("∃x::'a. P x", "?x. P x"),
            ("∀x::'a. P x ∧ Q x", "!x. P x & Q x"),
            ("(∀x::'a. P x) & Q y", "(!x. P x) & Q y"),
        ]

        context.set_context('logic_base',
                            vars={
                                'A': 'bool',
                                'B': 'bool',
                                'C': 'bool',
                                'P': "'a => bool",
                                'Q': "'a => bool"
                            })
        for s, ascii_s in test_data:
            t = parser.parse_term(s)
            self.assertIsInstance(t, Term)
            with global_setting(unicode=False):
                self.assertEqual(print_term(t), ascii_s)
Exemplo n.º 5
0
def export_term(t):
    """Function for printing a term for export to json."""
    with global_setting(unicode=True, line_length=80):
        res = printer.print_term(t)
    if len(res) == 1:
        res = res[0]
    return res
Exemplo n.º 6
0
    def run_test(self, thy_name, *, vars=None, svars=None, s, Ts):
        context.set_context(thy_name, vars=vars, svars=svars)

        t = parser.parse_term(s)
        T = parser.parse_type(Ts)
        self.assertIsInstance(t, Term)
        self.assertEqual(t.checked_get_type(), T)
        # with global_setting(unicode=False):
        self.assertEqual(print_term(t), s)
Exemplo n.º 7
0
    def testPrintWithType(self):
        test_data = [
            (list.nil(Ta), "([]::'a list)"),
            (eq(list.nil(Ta), list.nil(Ta)), "([]::'a list) = []"),
            (all(a, eq(a, a)), "!a::'a. a = a"),
        ]

        for t, s in test_data:
            self.assertEqual(printer.print_term(thy, t), s)
Exemplo n.º 8
0
    def testPrintFunction(self):
        test_data = [
            (function.mk_fun_upd(f, a, b), "(f)(a := b)"),
            (function.mk_fun_upd(f, a, b, b, a), "(f)(a := b, b := a)"),
        ]

        thy = basic.load_theory('function')
        for t, s in test_data:
            self.assertEqual(printer.print_term(thy, t), s)
Exemplo n.º 9
0
    def testPrintString(self):
        test_data = [
            (string.mk_char('c'), "'c'"),
            (string.mk_string("ab"), '"ab"'),
        ]

        basic.load_theory('string')
        for t, s in test_data:
            self.assertEqual(printer.print_term(t), s)
Exemplo n.º 10
0
    def testInferType2(self):
        thy = basic.load_theory('function')
        test_data = [("(%x. 0)(1 := 7)", "nat => nat")]

        for s, Ts in test_data:
            t = parser.parse_term(thy, ctxt, s)
            T = parser.parse_type(thy, Ts)
            self.assertIsInstance(t, Term)
            self.assertEqual(t.checked_get_type(), T)
            self.assertEqual(print_term(thy, t), s)
Exemplo n.º 11
0
    def testBinary(self):
        test_data = [
            (nat.one, "1"),
            (nat.bit0(nat.one), "2"),
            (nat.bit1(nat.one), "3"),
            (nat.Suc(nat.one), "Suc 1"),
        ]

        for t, s in test_data:
            self.assertEqual(printer.print_term(thy, t), s)
Exemplo n.º 12
0
    def testPrintWithType(self):
        test_data = [
            (list.nil(Ta), "([]::'a list)"),
            (Eq(list.nil(Ta), list.nil(Ta)), "([]::'a list) = []"),
            (Forall(a, Eq(a, a)), "!a::'a. a = a"),
        ]

        with global_setting(unicode=False):
            for t, s in test_data:
                self.assertEqual(printer.print_term(t), s)
Exemplo n.º 13
0
def print_extension(thy, ext):
    """Print given extension."""
    if isinstance(ext, AxType):
        return "Type " + ext.name
    elif isinstance(ext, AxConstant):
        return "Constant " + ext.name + " :: " + printer.print_type(
            thy, ext.T, unicode=True)
    elif isinstance(ext, Theorem):
        return "Theorem " + ext.name + ": " + printer.print_term(
            thy, ext.th.prop, unicode=True)
Exemplo n.º 14
0
    def __str__(self):
        res = "Variables: " + ", ".join(v.name for v in self.vars) + "\n"

        res += "States: " + ", ".join(v.name for v in self.states) + "\n"

        res += "Number of rules: %d\n" % len(self.rules)
        for i, rule in enumerate(self.rules):
            _, guard, assigns = rule
            assigns_str = ", ".join("%s := %s" % (str(k), str(v))
                                    for k, v in assigns.items())
            res += "%d: [%s] %s" % (i, printer.print_term(
                self.thy, guard), assigns_str) + "\n"

        res += "Number of invariants: %d\n" % len(self.invs)
        for i, inv in enumerate(self.invs):
            _, inv_term = inv
            res += "%d: %s" % (i, printer.print_term(self.thy,
                                                     inv_term)) + "\n"

        return res
Exemplo n.º 15
0
    def testPrintInterval(self):
        m = Var("m", NatType)
        n = Var("n", NatType)
        test_data = [
            (interval.mk_interval(m, n), "{m..n}"),
            (interval.mk_interval(nat.one, m), "{1..m}"),
        ]

        basic.load_theory('iterate')
        for t, s in test_data:
            self.assertEqual(printer.print_term(t), s)
Exemplo n.º 16
0
    def testPrintBinary(self):
        m = Var("m", NatType)
        test_data = [
            (nat.one, "(1::nat)"),
            (Nat(2), "(2::nat)"),
            (Nat(3), "(3::nat)"),
            (m + 1, "m + 1"),
        ]

        for t, s in test_data:
            self.assertEqual(printer.print_term(t), s)
Exemplo n.º 17
0
    def testPrintFunction(self):
        test_data = [
            (P(a), "P a"),
            (P(f(a)), "P (f a)"),
            (R(a,a), "R a a"),
            (nn(conj(A,B)), "n (A & B)"),
            (conj(nn(A), B), "n A & B"),
        ]

        for t, s in test_data:
            self.assertEqual(printer.print_term(thy, t), s)
Exemplo n.º 18
0
    def testPrintComb(self):
        f = Var("f", TFun(Ta, Ta))
        test_data = [
            (P(a), "P a"),
            (P(f(a)), "P (f a)"),
            (R(a, a), "R a a"),
            (nn(And(A, B)), "n (A & B)"),
            (And(nn(A), B), "n A & B"),
        ]

        with global_setting(unicode=False):
            for t, s in test_data:
                self.assertEqual(printer.print_term(t), s)
Exemplo n.º 19
0
    def testParseFunction(self):
        thy = basic.load_theory('function')
        test_data = [
            ("(f)(a := b)", "'a => 'a"),
            ("(f)(a := b, b := a)", "'a => 'a"),
        ]

        for s, Ts in test_data:
            t = parser.parse_term(thy, ctxt, s)
            T = parser.parse_type(thy, Ts)
            self.assertIsInstance(t, Term)
            self.assertEqual(t.checked_get_type(), T)
            self.assertEqual(print_term(thy, t), s)
Exemplo n.º 20
0
    def testParseTypedTerm(self):
        test_data = [
            ("([]::'a list)", "'a list"),
            ("([]::nat list)", "nat list"),
            ("([]::'a list) = []", "bool"),
            ("!a::'a. a = a", "bool"),
        ]

        for s, Ts in test_data:
            t = parser.parse_term(thy, ctxt, s)
            T = parser.parse_type(thy, Ts)
            self.assertIsInstance(t, Term)
            self.assertEqual(t.checked_get_type(), T)
            self.assertEqual(print_term(thy, t), s)
Exemplo n.º 21
0
    def testPrintRename(self):
        test_data = [
            (Const("exists",
                   TFun(TFun(NatType, BoolType),
                        BoolType))(Abs("x", NatType,
                                       nat.less(Bound(0), Var("x", NatType)))),
             "?x1. x1 < x"),
            (Abs("x", NatType, nat.less(Bound(0),
                                        Var("x", NatType))), "%x1. x1 < x"),
        ]

        with global_setting(unicode=False):
            for t, s in test_data:
                self.assertEqual(printer.print_term(t), s)
Exemplo n.º 22
0
    def add_theorem(self, name, th, prf):
        """Add a theorem with proof to the file."""

        assert len(th.hyps) == 0, "add_theorem"
        vars = dict((v.name, str(v.T)) for v in get_vars(th.prop))
        data = {
            "name": name,
            "ty": "thm",
            "prop": printer.print_term(self.thy, th.prop),
            "vars": vars,
            "num_gaps": 0,
            "proof": sum([self.export_proof_json(item) for item in prf.items], []),
        }
        self.content.append(data)
Exemplo n.º 23
0
    def testPrintReal(self):
        basic.load_theory('real')

        m = Var("m", RealType)
        test_data = [
            (real.zero, "(0::real)"),
            (real.one, "(1::real)"),
            (Real(2), "(2::real)"),
            (Real(3), "(3::real)"),
            (real.plus(m, real.one), "m + 1"),
            (real.plus(real.one, Real(2)), "(1::real) + 2"),
        ]

        for t, s in test_data:
            self.assertEqual(printer.print_term(t), s)
Exemplo n.º 24
0
    def testPrintInt(self):
        basic.load_theory('int')

        m = Var("m", IntType)
        test_data = [
            (Int(0), "(0::int)"),
            (Int(1), "(1::int)"),
            (Int(2), "(2::int)"),
            (Int(3), "(3::int)"),
            (m + 1, "m + 1"),
            (Int(1) + 2, "(1::int) + 2"),
        ]

        for t, s in test_data:
            self.assertEqual(printer.print_term(t), s)
Exemplo n.º 25
0
    def testPrintUnicode(self):
        test_data = [
            (conj(A, B), "A ∧ B"),
            (disj(A, B), "A ∨ B"),
            (imp(A, B), "A ⟶ B"),
            (abs(a, P(a)), "λa. P a"),
            (all(a, P(a)), "∀a. P a"),
            (exists(a, P(a)), "∃a. P a"),
            (neg(A), "¬A"),
            (nat.plus(m, n), "m + n"),
            (nat.times(m, n), "m * n"),
        ]

        for t, s in test_data:
            self.assertEqual(printer.print_term(thy, t, unicode=True), s)
Exemplo n.º 26
0
    def testGerman(self):
        sys = load_system("german")
        log(sys)

        subgoals = load_hints("german_hints")

        failed = 0
        for inv_id, rule_id, case_id, hint in subgoals:
            goal, ans = sys.verify_subgoal(inv_id, rule_id, case_id, hint)
            log(printer.print_term(sys.thy, goal), " --- ",
                "OK" if ans else "FAIL")
            if not ans:
                failed += 1

        log("Number failed: " + str(failed))
        self.assertEqual(failed, 0)
Exemplo n.º 27
0
    def testPrintUnicode(self):
        test_data = [
            (And(A, B), "A ∧ B"),
            (Or(A, B), "A ∨ B"),
            (Implies(A, B), "A ⟶ B"),
            (Lambda(a, P(a)), "λa. P a"),
            (Forall(a, P(a)), "∀a. P a"),
            (Exists(a, P(a)), "∃a. P a"),
            (Not(A), "¬A"),
            (Lambda(m, m + 2), "λm::nat. m + 2"),
            (Lambda(m, m + n), "λm. m + n"),
        ]

        with global_setting(unicode=True):
            for t, s in test_data:
                self.assertEqual(printer.print_term(t), s)
Exemplo n.º 28
0
    def testPrintHighlight(self):
        """Test highlight"""
        # 0, 1, 2, 3 = NORMAL, BOUND, VAR, TVAR
        test_data = [
            (abs(a,P(a)), [('%',0),('a',1),('. ',0),('P ',2),('a',1)]),
            (all(a,P(a)), [('!',0),('a',1),('. ',0),('P ',2),("a",1)]),
            (all(a,all(b,conj(P(a),P(b)))), [('!',0),('a',1),('. !',0),('b',1),('. ',0),('P ',2),('a',1),(' & ',0),('P ',2),('b',1)]),
            (exists(a,all(b,R(a,b))), [('?',0),("a",1),('. !',0),('b',1),('. ',0),('R ',2),('a b',1)]),
            (exists(a,P(a)), [('?',0),('a',1),('. ',0),('P ',2),('a',1)]),
            (disj(disj(A,B),C), [('(',0),('A',2),(' | ',0),('B',2),(') | ',0),('C',2)]),
            (imp(imp(A,B),C), [('(',0),('A',2),(' --> ',0),('B',2),(') --> ',0),('C',2)]),
            (abs(a,a), [('%',0),('a',1),('::',0),("'a",3),('. ',0),('a',1)]),
        ]

        for t, s in test_data:
            self.assertEqual(printer.print_term(thy, t, highlight=True), s)
Exemplo n.º 29
0
    def testPrintArithmetic(self):
        test_data = [
            (nat.plus(m, n), "m + n"),
            (nat.plus(nat.plus(m, n), p), "m + n + p"),
            (nat.plus(m, nat.plus(n, p)), "m + (n + p)"),
            (nat.times(m, n), "m * n"),
            (nat.times(nat.times(m, n), p), "m * n * p"),
            (nat.times(m, nat.times(n, p)), "m * (n * p)"),
            (nat.plus(m, nat.times(n, p)), "m + n * p"),
            (nat.times(m, nat.plus(n, p)), "m * (n + p)"),
            (nat.zero, "0"),
            (nat.plus(nat.zero, nat.zero), "0 + 0"),
            (nat.times(m, nat.zero), "m * 0"),
        ]

        for t, s in test_data:
            self.assertEqual(printer.print_term(thy, t), s)
Exemplo n.º 30
0
    def testPrintFunction(self):
        f = Var("f", TFun(Ta, Ta))
        Tb = TVar('b')
        Tc = TVar('c')
        g = Var('g', TFun(Tb, Tc))
        h = Var('h', TFun(Ta, Tb))
        test_data = [
            (function.mk_fun_upd(f, a, b), "(f)(a := b)"),
            (function.mk_fun_upd(f, a, b, b, a), "(f)(a := b, b := a)"),
            (function.mk_comp(g, h), "g O h"),
            (function.mk_comp(g, h)(a), "(g O h) a"),
            (function.mk_const_fun(NatType, nat.zero), "%x::nat. (0::nat)"),
        ]

        basic.load_theory('function')
        with global_setting(unicode=False):
            for t, s in test_data:
                self.assertEqual(printer.print_term(t), s)