Exemplo n.º 1
0
    def testDatatypeList(self):
        basic.load_theory('logic_base')
        list_item = items.parse_item({
            "args": ["a"],
            "constrs": [{
                "args": [],
                "name": "nil",
                "type": "'a list"
            }, {
                "args": ["x", "xs"],
                "name": "cons",
                "type": "'a => 'a list => 'a list"
            }],
            "name":
            "list",
            "ty":
            "type.ind"
        })
        self.assertIsNone(list_item.error)
        ext = list_item.get_extension()
        theory.thy.unchecked_extend(ext)
        ext_output = [
            "Type list 1", "Constant nil :: 'a list",
            "Constant cons :: 'a => 'a list => 'a list",
            "Theorem list_nil_cons_neq: ~([] = x # xs)",
            "Theorem list_cons_inject: x # xs = x1 # xs1 --> x = x1 & xs = xs1",
            "Theorem list_induct: P [] --> (!x1. !xs. P xs --> P (x1 # xs)) --> P x",
            "Attribute list_induct [var_induct]"
        ]

        with global_setting(unicode=False):
            self.assertEqual(printer.print_extensions(ext),
                             '\n'.join(ext_output))
Exemplo n.º 2
0
    def testDatatypeProd(self):
        basic.load_theory('logic_base')
        prod_item = items.parse_item({
            "args": ["a", "b"],
            "constrs": [{
                "args": ["a", "b"],
                "name": "Pair",
                "type": "'a => 'b => ('a, 'b) prod"
            }],
            "name":
            "prod",
            "ty":
            "type.ind"
        })
        self.assertIsNone(prod_item.error)
        ext = prod_item.get_extension()
        theory.thy.unchecked_extend(ext)
        ext_output = [
            "Type prod 2", "Constant Pair :: 'a => 'b => ('a, 'b) prod",
            "Theorem prod_Pair_inject: Pair a b = Pair a1 b1 --> a = a1 & b = b1",
            "Theorem prod_induct: (!a. !b. P (Pair a b)) --> P x",
            "Attribute prod_induct [var_induct]"
        ]

        with global_setting(unicode=False):
            self.assertEqual(printer.print_extensions(ext),
                             '\n'.join(ext_output))
Exemplo n.º 3
0
    def testPredEven(self):
        basic.load_theory('nat', limit=('def', 'one'))
        even_item = items.parse_item({
            "name":
            "even",
            "rules": [{
                "name": "even_zero",
                "prop": "even 0"
            }, {
                "name": "even_Suc",
                "prop": "even n --> even (Suc (Suc n))"
            }],
            "ty":
            "def.pred",
            "type":
            "nat => bool"
        })
        self.assertIsNone(even_item.error)
        ext = even_item.get_extension()
        theory.thy.unchecked_extend(ext)
        ext_output = [
            "Constant even :: nat => bool", "Theorem even_zero: even 0",
            "Attribute even_zero [hint_backward]",
            "Theorem even_Suc: even n --> even (Suc (Suc n))",
            "Attribute even_Suc [hint_backward]",
            "Theorem even_cases: even _a1 --> (_a1 = 0 --> P) --> (!n. _a1 = Suc (Suc n) --> even n --> P) --> P"
        ]

        with global_setting(unicode=False):
            self.assertEqual(printer.print_extensions(ext),
                             '\n'.join(ext_output))
Exemplo n.º 4
0
    def testDatatypeNat(self):
        basic.load_theory('logic_base')
        nat_item = items.parse_item({
            "args": [],
            "constrs": [{
                "args": [],
                "name": "zero",
                "type": "nat"
            }, {
                "args": ["n"],
                "name": "Suc",
                "type": "nat => nat"
            }],
            "name":
            "nat",
            "ty":
            "type.ind"
        })
        self.assertIsNone(nat_item.error)
        ext = nat_item.get_extension()
        theory.thy.unchecked_extend(ext)
        ext_output = [
            "Type nat 0", "Constant zero :: nat", "Constant Suc :: nat => nat",
            "Theorem nat_zero_Suc_neq: ~(0 = Suc n)",
            "Theorem nat_Suc_inject: Suc n = Suc n1 --> n = n1",
            "Theorem nat_induct: P 0 --> (!n. P n --> P (Suc n)) --> P x",
            "Attribute nat_induct [var_induct]"
        ]

        with global_setting(unicode=False):
            self.assertEqual(printer.print_extensions(ext),
                             '\n'.join(ext_output))
Exemplo n.º 5
0
    def testFunPlus(self):
        basic.load_theory('nat', limit=('def', 'one'))
        plus_item = items.parse_item({
            "name":
            "plus",
            "rules": [{
                "prop": "0 + n = n"
            }, {
                "prop": "Suc m + n = Suc (m + n)"
            }],
            "ty":
            "def.ind",
            "type":
            "nat ⇒ nat ⇒ nat"
        })
        self.assertIsNone(plus_item.error)
        ext = plus_item.get_extension()
        theory.thy.unchecked_extend(ext)
        ext_output = [
            "Constant plus :: nat => nat => nat",
            "Theorem plus_def_1: 0 + n = n",
            "Attribute plus_def_1 [hint_rewrite]",
            "Theorem plus_def_2: Suc m + n = Suc (m + n)",
            "Attribute plus_def_2 [hint_rewrite]"
        ]

        with global_setting(unicode=False):
            self.assertEqual(printer.print_extensions(ext),
                             '\n'.join(ext_output))
Exemplo n.º 6
0
Arquivo: basic.py Projeto: bzhan/holpy
def load_theory_cache(filename, username="******"):
    """Load the content of the given theory into cache.
    
    Return the theory cache as a dictionary.

    """
    if username not in theory_cache:
        load_metadata(username)

    cache = theory_cache[username][filename]
    timestamp = os.path.getmtime(user_file(filename, username))

    if 'timestamp' in cache and timestamp == cache['timestamp']:
        # No need to update cache
        return cache

    # Load all required macros and methods for this file.
    # Make table for this later.
    if filename == 'logic':
        from prover import z3wrapper
    if filename == 'expr':
        from data import expr
    if filename == 'real':
        from data import real
    if filename == 'hoare':
        from imperative import imp

    # Load all imported theories
    depend_list = get_import_order(cache['imports'], username)

    with theory.fresh_theory():
        for prev_name in depend_list:
            prev_cache = load_theory_cache(prev_name, username)
            for item in prev_cache['content']:
                if item.error is None:
                    theory.thy.unchecked_extend(item.get_extension())

        # Use this theory to parse the content of current theory
        cache['timestamp'] = timestamp
        data = load_json_data(filename, username)
        cache['content'] = []
        for index, item in enumerate(data['content']):
            item = items.parse_item(item)
            cache['content'].append(item)
            if item.error is None:
                exts = item.get_extension()
                theory.thy.unchecked_extend(exts)
                for ext in exts:
                    if ext.is_constant():
                        name = ext.ref_name
                    else:
                        name = ext.name
                    item_index[username][(ext.ty, name)] = (filename,
                                                            timestamp, index)

    return cache
Exemplo n.º 7
0
def check_theory(filename, username='******', rewrite=False):
    """Check the theory with the given name."""
    start_time = time.perf_counter()

    data = basic.load_json_data(filename, username)
    basic.load_theory(filename, limit='start', username=username)

    res = []
    stat = {'OK': 0, 'NoSteps': 0, 'Failed': 0, 'Partial': 0,
            'ProofOK': 0, 'ProofFail': 0, 'ParseOK': 0, 'ParseFail': 0, 'EditFail': 0}

    content = []

    for raw_item in data['content']:
        item = items.parse_item(raw_item)
        if item.error:
            e = item.error
            item_res = {
                'ty': item.ty,
                'name': item.name,
                'status': 'ParseFail',
                'err_type': e.__class__.__name__,
                'err_str': str(e),
                'trace': item.trace
            }
        else:
            exts = item.get_extension()
            old_thy = copy.copy(theory.thy)
            theory.thy.unchecked_extend(exts)
            new_thy = theory.thy

            # Check consistency with edit_item
            with global_setting(unicode=True, highlight=False):
                edit_item = item.get_display()
            theory.thy = old_thy
            item2 = items.parse_edit(edit_item)
            if item.ty == 'thm':
                item2.proof = item.proof
                item2.steps = item.steps
                item2.num_gaps = item.num_gaps
            if item2.error or item != item2:
                item_res = {
                    'ty': item.ty,
                    'name': item.name,
                    'status': 'EditFail'
                }
            elif item.ty == 'thm':
                item_res = check_proof(item, rewrite=rewrite)
                item_res['ty'] = 'thm'
                item_res['name'] = item.name
            else:
                item_res = {
                    'ty': item.ty,
                    'name': item.name,
                    'status': 'ParseOK'
                }
            theory.thy = new_thy

        stat[item_res['status']] += 1
        res.append(item_res)

        if rewrite:
            content.append(item.export_json())

    if rewrite:
        data['content'] = content
        with open(basic.user_file(filename, username), 'w+', encoding='utf-8') as f:
            json.dump(data, f, indent=4, ensure_ascii=False, sort_keys=True)

    stat['exec_time'] = time.perf_counter() - start_time

    return {
        'data': res,
        'stat': stat
    }