Пример #1
0
 def empty_range(_i, _j, _f, assumptions):
     # If the start and end are literal ints and form an
     # empty range, then it should be straightforward to
     # prove that the range is empty.
     from proveit.numbers import is_literal_int, Add
     from proveit.logic import Equals
     from proveit import m
     _m = entries[0].start_index
     _n = entries[0].end_index
     empty_req = Equals(Add(_n, one), _m)
     if is_literal_int(_m) and is_literal_int(_n):
         if _n.as_int() + 1 == _m.as_int():
             empty_req.prove()
     if empty_req.proven(assumptions):
         _f = Lambda(
             (entries[0].parameter, entries[0].body.parameter),
             entries[0].body.body)
         _i = entry_map(_i)
         _j = entry_map(_j)
         return len_of_empty_range_of_ranges.instantiate(
             {
                 m: _m,
                 n: _n,
                 f: _f,
                 i: _i,
                 j: _j
             },
             assumptions=assumptions)
Пример #2
0
    def conclude(self, **defaults_config):
        '''
        Attempt to conclude that the element is in the domain.  First, 
        see if it is known to be contained in a known subset of the
        domain.  Next, check if the element has a known simplification; 
        if so, try to derive membership via this simplification.
        If there isn't a known simplification, next try to call
        the 'self.domain.membership_object.conclude(..)' method to prove
        the membership.  If that fails, try simplifying the element
        again, this time using automation to push the simplification 
        through if possible.
        '''
        from proveit.logic import Equals, SubsetEq, evaluation_or_simplification

        # See if the element, or something known to be equal to
        # the element, is known to be a member of the domain or a subset
        # of the domain.
        for elem_sub in Equals.yield_known_equal_expressions(self.element):
            same_membership = None  # membership in self.domain
            eq_membership = None  # membership in an equal domain
            subset_membership = None  # membership in a subset
            for known_membership in InClass.yield_known_memberships(elem_sub):
                eq_rel = Equals(known_membership.domain, self.domain)
                sub_rel = SubsetEq(known_membership.domain, self.domain)
                if known_membership.domain == self.domain:
                    same_membership = known_membership
                    break  # this is the best to use; we are done
                elif eq_rel.proven():
                    eq_membership = known_membership
                elif sub_rel.proven():
                    subset_membership = known_membership
            elem_sub_in_domain = None
            if same_membership is not None:
                elem_sub_in_domain = same_membership
            elif eq_membership is not None:
                # domains are equal -- just substitute to domain.
                eq_rel = Equals(eq_membership.domain, self.domain)
                elem_sub_in_domain = eq_rel.sub_right_side_into(
                    eq_membership.inner_expr().domain)
            elif subset_membership is not None:
                # S is a superset of R, so now we can prove x in S.
                sub_rel = SubsetEq(subset_membership.domain, self.domain)
                elem_sub_in_domain = sub_rel.derive_superset_membership(
                    elem_sub)
            if elem_sub_in_domain is not None:
                # We found what we are looking for.
                if elem_sub == self.element:
                    return elem_sub_in_domain  # done
                # Just need to sub in the element for _elem_sub.
                Equals(elem_sub, self.element).conclude_via_transitivity()
                return elem_sub_in_domain.inner_expr().element.substitute(
                    self.element)

        # Try the standard Relation strategies -- evaluate or
        # simplify both sides.
        try:
            return Relation.conclude(self)
        except ProofFailure:
            # Both sides are already irreducible or simplified
            # or we were unable to simplify either side.
            pass

        # Unable to simplify the element.  Try to conclude via
        # the 'membership_object' if there is one.
        if hasattr(self, 'membership_object'):
            return self.membership_object.conclude()

        raise ProofFailure(
            self, defaults.assumptions, "Unable to conclude automatically; "
            "the domain, %s, has no 'membership_object' "
            "method with a strategy for proving "
            "membership." % self.domain)
Пример #3
0
    def conclude(self, assumptions):
        '''
        Attempt to conclude that the element is in the domain.
        First, see if it is known to be contained in a known subset of the
        domain.  Next, check if the element has a known simplification; if so,
        try to derive membership via this simplification.
        If there isn't a known simplification, next try to call
        the 'self.domain.membershipObject.conclude(..)' method to prove
        the membership.  If that fails, try simplifying the element
        again, this time using automation to push the simplification through
        if possible.
        '''
        from proveit.logic import Equals, SubsetEq
        from proveit import ProofFailure
        from proveit.logic import SimplificationError

        # See if the membership is already known.
        if self.element in InSet.knownMemberships:
            for knownMembership in InSet.knownMemberships[self.element]:
                if knownMembership.isSufficient(assumptions):
                    # x in R is a known truth; if we know that R subseteq S,
                    # or R = S we are done.
                    eqRel = Equals(knownMembership.domain, self.domain)
                    if eqRel.proven(assumptions):
                        return eqRel.subRightSideInto(
                            knownMembership.innerExpr().domain)
                    subRel = SubsetEq(knownMembership.domain, self.domain)
                    if subRel.proven(assumptions):
                        # S is a superset of R, so now we can prove x in S.
                        return subRel.deriveSupersetMembership(
                            self.element, assumptions)

        # No known membership works.  Let's see if there is a known
        # simplification of the element before trying anything else.
        try:
            elem_simplification = self.element.simplification(assumptions,
                                                              automation=False)
            if elem_simplification.lhs == elem_simplification.rhs:
                elem_simplification = None  # reflection doesn't count
        except SimplificationError:
            elem_simplification = None

        if elem_simplification is None:
            # Let's try harder to simplify the element.
            try:
                elem_simplification = self.element.simplification(assumptions)
                if elem_simplification.lhs == elem_simplification.rhs:
                    elem_simplification = None  # reflection doesn't count
            except SimplificationError:
                pass

        # If the element simplification succeeded, prove the membership
        # via the simplified form of the element.
        if elem_simplification is not None:
            simple_elem = elem_simplification.rhs
            simple_membership = InSet(simple_elem,
                                      self.domain).prove(assumptions)
            inner_expr = simple_membership.innerExpr().element
            return elem_simplification.subLeftSideInto(inner_expr, assumptions)
        else:
            # Unable to simplify the element.  Try to conclude via
            # the 'membershipObject' if there is one.
            if hasattr(self, 'membershipObject'):
                return self.membershipObject.conclude(assumptions)

            raise ProofFailure(
                self, assumptions, "Unable to conclude automatically; "
                "the domain, %s, has no 'membershipObject' "
                "method with a strategy for proving "
                "membership." % self.domain)