Beispiel #1
0
def search_method():
    """Match for applicable methods and their arguments."""
    data = json.loads(request.get_data().decode("utf-8"))
    if data:
        cell = cells[data['id']]
        thy = cell.thy
        fact_ids = data['fact_ids']
        goal_id = data['goal_id']
        search_res = cell.search_method(goal_id, fact_ids)
        for res in search_res:
            if '_goal' in res:
                res['_goal'] = [
                    printer.print_term(thy, t, unicode=True)
                    for t in res['_goal']
                ]
            if '_fact' in res:
                res['_fact'] = [
                    printer.print_term(thy, t, unicode=True)
                    for t in res['_fact']
                ]

        ctxt = cell.get_ctxt(goal_id)
        print_ctxt = dict((k, printer.print_type(thy, v, highlight=True))
                          for k, v in ctxt.items())
        return jsonify({'search_res': search_res, 'ctxt': print_ctxt})
    return jsonify({})

    return jsonify({})
Beispiel #2
0
    def testParseUnicodeType(self):
        test_data = ["'a ⇒ 'b"]

        for s in test_data:
            T = parser.parse_type(thy, s)
            self.assertIsInstance(T, HOLType)
            self.assertEqual(print_type(thy, T, unicode=True), s)
Beispiel #3
0
    def get_display(self):
        Targs = [TVar(arg) for arg in self.args]
        T = TConst(self.name, *Targs)
        constrs = []
        for constr in self.constrs:
            argsT, _ = constr['type'].strip_type()
            res = pprint.N(constr['name'])
            for i, arg in enumerate(constr['args']):
                res += pprint.N(' (' + arg + ' :: ') + printer.print_type(
                    argsT[i]) + pprint.N(')')
            constrs.append(res)

        return {
            'ty': 'type.ind',
            'type': printer.print_type(T),
            'constrs': constrs if settings.highlight else '\n'.join(constrs)
        }
Beispiel #4
0
def search_method():
    """Match for applicable methods and their arguments.
    
    Input:
    * username: username.
    * theory_name: name of the theory.
    * thm_name: name of the theorem.

    Returns:
    * search_res: list of search results.
    * ctxt: current proof context.

    """
    data = json.loads(request.get_data().decode("utf-8"))

    if data['profile']:
        pr = cProfile.Profile()
        pr.enable()

    if not proof_cache.check_cache(data):
        start_time = time.perf_counter()
        proof_cache.create_cache(data)
        print("Load: %f" % (time.perf_counter() - start_time))

    if data['thm_name'] != '':
        limit = ('thm', data['thm_name'])
    else:
        limit = None
    basic.load_theory(data['theory_name'],
                      limit=limit,
                      username=data['username'])

    start_time = time.perf_counter()
    state = proof_cache.states[data['index']]
    fact_ids = data['step']['fact_ids']
    goal_id = data['step']['goal_id']

    search_res = state.search_method(goal_id, fact_ids)
    with settings.global_setting(unicode=True):
        for res in search_res:
            if '_goal' in res:
                res['_goal'] = [printer.print_term(t) for t in res['_goal']]
            if '_fact' in res:
                res['_fact'] = [printer.print_term(t) for t in res['_fact']]

    vars = state.get_vars(goal_id)
    with settings.global_setting(unicode=True, highlight=True):
        print_vars = dict((k, printer.print_type(v)) for k, v in vars.items())
    print("Response:", time.perf_counter() - start_time)

    if data['profile']:
        p = Stats(pr)
        p.strip_dirs()
        p.sort_stats('cumtime')
        p.print_stats()

    return jsonify({'search_res': search_res, 'ctxt': print_vars})
Beispiel #5
0
    def testParseUnicodeType(self):
        test_data = ["'a ⇒ 'b"]

        basic.load_theory('list')
        for s in test_data:
            T = parser.parse_type(s)
            self.assertIsInstance(T, Type)
            with global_setting(unicode=True):
                self.assertEqual(print_type(T), s)
Beispiel #6
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)
Beispiel #7
0
 def export_json(self):
     with global_setting(unicode=True):
         res = {
             'ty': 'def.ax',
             'name': self.name,
             'type':
             self.type if self.error else printer.print_type(self.type)
         }
     if self.overloaded:
         res['overloaded'] = True
     return res
Beispiel #8
0
 def get_display(self):
     return {
         'ty':
         'def.ax',
         'name':
         self.name,
         'type':
         display_raw(self.type)
         if self.error else printer.print_type(self.type),
         'overloaded':
         self.overloaded
     }
Beispiel #9
0
 def export_json(self):
     with global_setting(unicode=True):
         res = {
             'ty': 'def',
             'name': self.name,
             'type':
             self.type if self.error else printer.print_type(self.type),
             'prop': self.prop if self.error else export_term(self.prop)
         }
     if self.attributes:
         res['attributes'] = self.attributes
     return res
Beispiel #10
0
 def export_json(self):
     with global_setting(unicode=True):
         res = {
             'ty': 'thm.ax',
             'name': self.name,
             'vars': self.vars if self.error else \
                 dict((nm, printer.print_type(T)) for nm, T in self.vars.items()),
             'prop': self.prop if self.error else export_term(self.prop)
         }
     if self.attributes:
         res['attributes'] = self.attributes
     return res
Beispiel #11
0
    def json_data(self):
        """Export proof in json format."""
        with global_setting(unicode=True):
            vars = {v.name: printer.print_type(v.T) for v in self.vars}

        with global_setting(unicode=True, highlight=True):
            res = {
                "vars": vars,
                "proof": self.export_proof(),
                "num_gaps": len(self.rpt.gaps),
                "method_sig": method.get_method_sig(),
            }
        return res
Beispiel #12
0
 def export_json(self):
     with global_setting(unicode=True):
         return {
             'ty':
             'def.ind',
             'name':
             self.name,
             'type':
             self.type if self.error else printer.print_type(self.type),
             'rules': [{
                 'prop':
                 rule['prop'] if self.error else export_term(rule['prop'])
             } for rule in self.rules]
         }
Beispiel #13
0
    def get_display(self):
        if self.error:
            disp_type = display_raw(self.type)
            disp_prop = display_raw(self.prop)
        else:
            disp_type = printer.print_type(self.type)
            disp_prop = display_term(self.prop)

        return {
            'ty': 'def',
            'name': self.name,
            'type': disp_type,
            'prop': disp_prop,
            'attributes': self.attributes
        }
Beispiel #14
0
 def export_json(self):
     constrs = []
     for constr in self.constrs:
         constrs.append({
             'name':
             constr['name'],
             'args':
             constr['args'],
             'type':
             constr['type']
             if self.error else printer.print_type(constr['type'])
         })
     return {
         'ty': 'type.ind',
         'name': self.name,
         'args': self.args,
         'constrs': constrs
     }
Beispiel #15
0
    def get_display(self):
        if self.error:
            disp_type = display_raw(self.type)
            disp_rules = [rule['prop'] for rule in self.rules]
        else:
            disp_type = printer.print_type(self.type)
            with global_setting(line_length=None):
                disp_rules = [
                    printer.print_term(rule['prop']) for rule in self.rules
                ]

        return {
            'ty': 'def.ind',
            'name': self.name,
            'type': disp_type,
            'rules':
            disp_rules if settings.highlight else '\n'.join(disp_rules)
        }
Beispiel #16
0
    def get_display(self):
        if self.error:
            disp_vars = [
                pprint.N(nm + ' :: ' + T) for nm, T in self.vars.items()
            ]
            disp_prop = display_raw(self.prop)
        else:
            disp_vars = [
                pprint.N(nm + ' :: ') + printer.print_type(T)
                for nm, T in self.vars.items()
            ]
            disp_prop = display_term(self.prop)

        return {
            'ty': 'thm.ax',
            'name': self.name,
            'vars': disp_vars if settings.highlight else '\n'.join(disp_vars),
            'prop': disp_prop,
            'attributes': self.attributes
        }
Beispiel #17
0
def file_data_to_output(thy, data):
    """Convert items in the theory from json format for the file to
    json format for the web client. Modifies data in-place.
    Also modifies argument thy in parsing the item.

    """
    parser.parse_extension(thy, data)
    if data['ty'] == 'def.ax':
        T = parser.parse_type(thy, data['type'])
        data['type_hl'] = printer.print_type(thy,
                                             T,
                                             unicode=True,
                                             highlight=True)

    elif data['ty'] == 'thm' or data['ty'] == 'thm.ax':
        temp_list = []
        for k, v in data['vars'].items():
            temp_list.append(k + ' :: ' + v)
        ctxt = parser.parse_vars(thy, data['vars'])
        prop = parser.parse_term(thy, ctxt, data['prop'])
        data['prop_hl'] = printer.print_term(thy,
                                             prop,
                                             unicode=True,
                                             highlight=True)
        data['vars_lines'] = temp_list

    elif data['ty'] == 'type.ind':
        constrs = []
        data_content = ''
        for constr in data['constrs']:
            T = parser.parse_type(thy, constr['type'])
            constrs.append((constr['name'], T, constr['args']))
        exts = induct.add_induct_type(data['name'], data['args'], constrs)

        # Obtain items added by the extension
        ext_output = []
        for ext in exts.data:
            s = print_extension(thy, ext)
            if s:
                ext_output.append(s)
        data['ext'] = ext_output

        # Obtain type to be defined
        T = Type(data['name'], *(TVar(nm) for nm in data['args']))
        data['type_hl'] = printer.print_type(thy,
                                             T,
                                             unicode=True,
                                             highlight=True)

        # Obtain types of arguments for each constructor
        data['argsT'] = dict()
        for i, constr in enumerate(data['constrs']):
            str_temp_var = ''
            T = parser.parse_type(thy, constr['type'])
            argsT, _ = HOLType.strip_type(T)
            argsT = [
                printer.print_type(thy, argT, unicode=True, highlight=True)
                for argT in argsT
            ]
            data['argsT'][str(i)] = argsT
            for j, a in enumerate(constr['args']):
                str_temp_term = ''
                for m, t in enumerate(data['argsT'][str(i)][j]):
                    str_temp_term += t[0]
                str_temp_var += ' (' + a + ' :: ' + str_temp_term + ')'
            data_content += '\n' + constr['name'] + str_temp_var
        data['type_content'] = data_content

    elif data['ty'] == 'def.ind' or data['ty'] == 'def.pred':
        container = [[], [], [], '', '', '']
        data_content_list, data_rule_names, data_vars_list, data_new_content, data_rule_name, data_vars_str = container
        T = parser.parse_type(thy, data['type'])
        data['type_hl'] = printer.print_type(thy,
                                             T,
                                             unicode=True,
                                             highlight=True)
        rules = []
        for rule in data['rules']:
            ctxt = parser.parse_vars(thy, rule['vars'])
            prop = parser.parse_term(thy, ctxt, rule['prop'])
            rule['prop_hl'] = printer.print_term(thy,
                                                 prop,
                                                 unicode=True,
                                                 highlight=True)
            rules.append(prop)
        exts = induct.add_induct_def(data['name'], T, rules)

        # Obtain items added by the extension
        ext_output = []
        for ext in exts.data:
            s = print_extension(thy, ext)
            if s:
                ext_output.append(s)
        data['ext'] = ext_output

        if data['ty'] == 'def.ind':
            type_name = 'fun'
        if data['ty'] == 'def.pred':
            type_name = 'inductive'
        data['ext_output'] = '\n'.join(ext_output)
        data['type_name'] = type_name

        for k, r in enumerate(data['rules']):
            vars_str = ''
            data_con = ''
            for m, v in enumerate(r['vars']):
                vars_str += str(m) + '::' + v + '   '
            data_vars_list.append(vars_str)
            for p in r['prop_hl']:
                data_con += p[0]
            data_content_list.append(data_con)
            if 'name' in r:
                data_rule_names.append(r['name'])
        for n, dv in enumerate(data_vars_list):
            data_vars_str += str(n) + ': ' + dv + '\n'
        for j, dc in enumerate(data_content_list):
            data_new_content += str(j) + ': ' + dc + '\n'
            data_rule_name += str(j) + ': ' + dc + '\n'
        data['data_new_content'] = data_new_content
        data['data_rule_name'] = data_rule_name
        data['data_vars_str'] = data_vars_str

    elif data['ty'] == 'def':
        i = 0
        vars = ''
        data_new_content = ''
        data_content_list = []
        data_content_list.append(data['prop'])
        for j, dc in enumerate(data_content_list):
            data_new_content += str(j) + ': ' + dc + '\n'
        for j, v in enumerate(data['vars']):
            vars += str(i) + ': ' + str(j) + '::' + v + '\n'
            i += 1
        data['item_vars'] = vars
        T = parser.parse_type(thy, data['type'])
        data['type_name'] = 'definition'
        data['data_new_content'] = data_new_content
        data['type_hl'] = printer.print_type(thy,
                                             T,
                                             unicode=True,
                                             highlight=True)

        ctxt = parser.parse_vars(thy, data['vars'])
        prop = parser.parse_term(thy, ctxt, data['prop'])
        data['prop_hl'] = printer.print_term(thy,
                                             prop,
                                             unicode=True,
                                             highlight=True)

    # Ignore other types of information.
    else:
        pass
Beispiel #18
0
 def display_step(self, state, data):
     T = parser.parse_type(data['type'])
     return pprint.N("Variable " + data['name'] +
                     " :: ") + printer.print_type(T)
Beispiel #19
0
 def get_display(self):
     Targs = [TVar(arg) for arg in self.args]
     T = TConst(self.name, *Targs)
     return {'ty': 'type.ax', 'type': printer.print_type(T)}