Ejemplo n.º 1
0
 def ApplyTactic(self, request):  # pylint: disable=invalid-name
     response = proof_assistant_pb2.ApplyTacticResponse()
     tactic = str(request.tactic)
     head = tactic[0]
     tail = tactic[1:]
     if head == 'a':  # Append the rest of the characters to goal
         response.goals.goals.add(goal=str(request.goal.goal) + tail)
     elif head == 'b':  # Branch: append each character in separation.
         for c in tail:
             response.goals.goals.add(goal=str(request.goal.goal) + c)
     elif head == 'c':  # Close the goal.
         response.goals.goals.extend([])
     elif head == 'r':  # Replace: replace the goal with the string in tail.
         response.goals.goals.add(goal=tail)
     elif head == 'e':  # Error: produce the error specified in tail.
         response.error = tail
     return response
Ejemplo n.º 2
0
    def prune_tactic_application(self, goal: proof_assistant_pb2.Theorem,
                                 tapp: deephol_pb2.TacticApplication):
        """Parameter pruning for a single tactic application.

    Args:
      goal: Goal of the ProofNode to which the tactic application belongs.
      tapp: The tactic application to be pruned.
    """
        if self.communication_failed:
            tf.logging.error(
                'Communication with prover failed. Not pruning...')
            return
        tactic = tapp.tactic
        parameters = tapp.parameters
        if not parameters:
            return
        assert len(parameters) == 1
        param = parameters[0]
        if param.parameter_type != deephol_pb2.Tactic.THEOREM_LIST:
            return
        thms = list(param.theorems)
        if not thms:
            return
        index = len(thms) - 1
        tactic = tapp.tactic
        time_spent = tapp.time_spent
        false_positives = []
        other_negatives = []
        found_true_positive = False
        while index >= 0:
            thm = thms.pop(index)
            request = _create_request(goal, str(tactic), thms)
            start_time = time.time()
            response = proof_assistant_pb2.ApplyTacticResponse()
            try:
                response = self.hol_wrapper.ApplyTactic(request)
                elapsed_msecs = int((time.time() - start_time) * 1000.0 + 0.5)
                time_spent = elapsed_msecs
            except error.StatusNotOk as exception:
                tf.logging.error(exception)
                elapsed_msecs = int((time.time() - start_time) * 1000.0 + 0.5)
                if exception.message.startswith(
                        'Communication') and exception.message.endswith(
                            'failed.'):
                    tf.logging.error(
                        'Communication with prover failed. Not pruning...')
                    self.communication_failed = True
                    return
            if response.HasField('error'):
                thms.insert(index, thm)
                found_true_positive = True
                index -= 1
                continue
            assert response.HasField('goals'), 'response: %s' % response
            new_subgoals = list(response.goals.goals)
            no_match = False
            if len(new_subgoals) == len(tapp.subgoals):
                for i, sg in enumerate(new_subgoals):
                    if not _matches_subgoal(sg, tapp.subgoals[i]):
                        no_match = True
                        break
            else:
                no_match = True
            if no_match:
                thms.insert(index, thm)
                found_true_positive = True
            else:
                if found_true_positive:
                    false_positives.append(thm)
                else:
                    other_negatives.append(thm)
                time_spent = elapsed_msecs
            index -= 1
        del tapp.parameters[0].theorems[:]
        tapp.parameters[0].theorems.extend(thms)
        tapp.parameters[0].hard_negative_theorems.extend(
            false_positives[:MAX_HARD_NEGATIVES])
        if len(false_positives) < MIN_HARD_NEGATIVES:
            other_negatives.reverse()
            tapp.parameters[0].hard_negative_theorems.extend(
                other_negatives[:(MIN_HARD_NEGATIVES - len(false_positives))])
        tapp.time_spent = time_spent