Exemplo n.º 1
0
 def __init__(self, a, b, *, styles=None):
     TransitiveRelation.__init__(self,
                                 QcircuitEquiv._operator_,
                                 a,
                                 b,
                                 styles=styles)
     self.a = a
     self.b = b
Exemplo n.º 2
0
 def __init__(self, a, b):
     TransitiveRelation.__init__(self, Equals._operator_, a, b)
     if self not in Equals.initializing:
         Equals.initializing.add(self)
         try:
             self.deduceInBool()  # proactively prove (a=b) in Booleans.
         except:
             # may fail before the relevent _axioms_ have been generated
             pass  # and that's okay
         Equals.initializing.remove(self)
Exemplo n.º 3
0
 def __init__(self, a, b):
     TransitiveRelation.__init__(self, SetEquiv._operator_, a, b)
     if self not in SetEquiv.initializing:
         SetEquiv.initializing.add(self)
         try:
             # proactively prove (a equiv b) in Boolean.
             self.deduce_in_bool()
         except BaseException:
             # may fail before the relevent _axioms_ have been generated
             pass  # and that's okay
         SetEquiv.initializing.remove(self)
Exemplo n.º 4
0
    def conclude(self, assumptions):
        '''
        Try to automatically conclude this bi-directional implication by
        reducing its operands to true/false.
        '''
        from . import iff_t_t, iff_t_f, iff_f_t, iff_f_f, true_iff_true, false_iff_false
        if self in {true_iff_true, false_iff_false}:
            # should be proven via one of the imported theorems as a simple
            # special case
            try:
                self.evaluation(assumptions)
            except BaseException:
                return self.prove()
        try:
            # try to prove the bi-directional implication via evaluation reduction.
            # if that is possible, it is a relatively straightforward thing to
            # do.
            return Operation.conclude(assumptions)
        except BaseException:
            pass
        try:
            # Use a breadth-first search approach to find the shortest
            # path to get from one end-point to the other.
            return TransitiveRelation.conclude(self, assumptions)
        except BaseException:
            pass

        # the last attempt is to introduce the Iff via implications each way, an
        # essentially direct consequence of the definition.
        return self.conclude_by_definition(assumptions)
Exemplo n.º 5
0
 def side_effects(self, judgment):
     '''
     In addition to the TransitiveRelation side-effects, also
     attempt derive_reversed.
     '''
     for side_effect in TransitiveRelation.side_effects(self, judgment):
         yield side_effect
     yield self.derive_reversed
Exemplo n.º 6
0
    def conclude(self, assumptions):
        '''
        Attempt to conclude the equivalence in various ways:
        simple reflexivity (A equiv A), via an evaluation (if one side
        is an irreducible), or via transitivity.
        IN PROGRESS. NOT YET CLEAR how this applies to the SetEquiv
        '''
        from proveit.logic import TRUE, FALSE, Implies, Iff
        if self.lhs == self.rhs:
            # Trivial A = A
            return self.concludeViaReflexivity(assumptions=assumptions)
    #     if self.lhs or self.rhs in (TRUE, FALSE):
    #         try:
    #             # Try to conclude as TRUE or FALSE.
    #             return self.concludeBooleanEquality(assumptions)
    #         except ProofFailure:
    #             pass
    #     if isIrreducibleValue(self.rhs):
    #         try:
    #             evaluation = self.lhs.evaluation(assumptions)
    #             if evaluation.rhs != self.rhs:
    #                 raise ProofFailure(self, assumptions, "Does not match with evaluation: %s"%str(evaluation))
    #             return evaluation
    #         except SimplificationError as e:
    #             raise ProofFailure(self, assumptions, "Evaluation error: %s"%e.message)
    #     elif isIrreducibleValue(self.lhs):
    #         try:
    #             evaluation = self.rhs.evaluation(assumptions)
    #             if evaluation.rhs != self.lhs:
    #                 raise ProofFailure(self, assumptions, "Does not match with evaluation: %s"%str(evaluation))
    #             return evaluation.deriveReversed()
    #         except SimplificationError as e:
    #             raise ProofFailure(self, assumptions, "Evaluation error: %s"%e.message)
    #     try:
    #         Implies(self.lhs, self.rhs).prove(assumptions, automation=False)
    #         Implies(self.rhs, self.lhs).prove(assumptions, automation=False)
    #         # lhs => rhs and rhs => lhs, so attempt to prove lhs = rhs via lhs <=> rhs
    #         # which works when they can both be proven to be Booleans.
    #         try:
    #             return Iff(self.lhs, self.rhs).deriveEquality(assumptions)
    #         except:
    #             from proveit.logic.boolean.implication._theorems_ import eqFromMutualImpl
    #             return eqFromMutualImpl.specialize({A:self.lhs, B:self.rhs}, assumptions=assumptions)
    #     except ProofFailure:
    #         pass

    #     """
    #     # Use concludeEquality if available
    #     if hasattr(self.lhs, 'concludeEquality'):
    #         return self.lhs.concludeEquality(assumptions)
    #     if hasattr(self.rhs, 'concludeEquality'):
    #         return self.rhs.concludeEquality(assumptions)
    #     """
    # Use a breadth-first search approach to find the shortest
    # path to get from one end-point to the other.
        return TransitiveRelation.conclude(self, assumptions)
Exemplo n.º 7
0
 def sideEffects(self, knownTruth):
     '''
     Yield the TransitiveRelation side-effects (which also records knownLeftSides
     and knownRightSides).  Also derive the left and right implications,
     derive the reversed version and attempt to derive equality.
     '''
     for sideEffect in TransitiveRelation.sideEffects(self, knownTruth):
         yield sideEffect
     yield self.deriveLeftImplication  # B=>A given A<=>B
     yield self.deriveRightImplication  # A=>B given A<=>B
     yield self.deriveReversed  # B<=>A given A<=>B
     yield self.deriveEquality  # A=B given A<=>B (assuming A and B are in booleans)
Exemplo n.º 8
0
 def sideEffects(self, knownTruth):
     '''
     Yield the TransitiveRelation side-effects (which also records knownLeftSides
     and knownRightSides).  Also derive the consequent as a side-effect.
     As a special case, if the consequent is FALSE, do deriveViaContradiction.
     '''
     from proveit.logic.boolean._common_ import FALSE
     for sideEffect in TransitiveRelation.sideEffects(self, knownTruth):
         yield sideEffect
     yield self.deriveConsequent  # B given A=>B and A
     if self.consequent == FALSE:
         yield self.deriveViaContradiction  # Not(A) given A=>FALSE or A given Not(A)=>FALSE
Exemplo n.º 9
0
 def side_effects(self, judgment):
     '''
     Yield the TransitiveRelation side-effects (which also records known_left_sides
     and known_right_sides).  Also derive the left and right implications,
     derive the reversed version and attempt to derive equality.
     '''
     for side_effect in TransitiveRelation.side_effects(self, judgment):
         yield side_effect
     yield self.derive_left_implication  # B=>A given A<=>B
     yield self.derive_right_implication  # A=>B given A<=>B
     yield self.derive_reversed  # B<=>A given A<=>B
     # A=B given A<=>B (assuming A and B are in booleans)
     yield self.derive_equality
Exemplo n.º 10
0
 def conclude(self, assumptions):
     '''
     Try to automatically conclude this implication by reducing its operands
     to true/false, or by doing a "transitivity" search amongst known true implications
     whose assumptions are covered by the given assumptions.
     '''
     from ._axioms_ import untrueAntecedentImplication
     from ._theorems_ import trueImpliesTrue, falseImpliesTrue, falseImpliesFalse, falseAntecedentImplication
     from proveit.logic import TRUE, FALSE
     if self.antecedent == self.consequent:
         return self.concludeSelfImplication()
     if self in {trueImpliesTrue, falseImpliesTrue, falseImpliesFalse}:
         # should be proven via one of the imported theorems as a simple special case
         return self.prove()
     try:
         # Try evaluating the consequent.
         evaluation = self.consequent.evaluation(assumptions)
         if evaluation.rhs == TRUE:
             # If the consequent evaluates to true, we can prove trivially via hypothetical reasoning
             return self.consequent.prove(assumptions).asImplication(
                 self.antecedent)
         elif evaluation.rhs == FALSE:
             if self.antecedent.evaluation(assumptions).rhs == FALSE:
                 # Derive A => B given Not(A); it doesn't matter what B is if A is FALSE
                 return falseAntecedentImplication.specialize(
                     {
                         A: self.antecedent,
                         B: self.consequent
                     },
                     assumptions=assumptions)
             else:
                 # Derive A => B given (A != TRUE); it doesn't matter what B is if A is not TRUE
                 return untrueAntecedentImplication.specialize(
                     {
                         A: self.antecedent,
                         B: self.consequent
                     },
                     assumptions=assumptions)
     except:
         pass
     try:
         # Use a breadth-first search approach to find the shortest
         # path to get from one end-point to the other.
         return TransitiveRelation.conclude(self, assumptions)
     except:
         pass
     # try to prove the implication via hypothetical reasoning.
     return self.consequent.prove(assumptions +
                                  (self.antecedent, )).asImplication(
                                      self.antecedent)
Exemplo n.º 11
0
 def evaluation(self, assumptions=USE_DEFAULTS):
     '''
     Given operands that may be evaluated to irreducible values that
     may be compared, or if there is a known evaluation of this
     equality, derive and return this expression equated to
     TRUE or FALSE.
     '''
     if self.lhs == self.rhs:
         # prove equality is true by reflexivity
         return evaluateTruth(self.prove().expr, assumptions=[])
     if isIrreducibleValue(self.lhs) and isIrreducibleValue(self.rhs):
         # Irreducible values must know how to evaluate the equality
         # between each other, where appropriate.
         return self.lhs.evalEquality(self.rhs)
     return TransitiveRelation.evaluation(self, assumptions)
Exemplo n.º 12
0
    def side_effects(self, judgment):
        '''
        In addition to the TransitiveRelation side-effects, also
        attempt (where applicable) eliminate_dividen_exponent,
        eliminate_common_exponent, and eliminate_common_factor.
        '''
        from proveit.numbers import two, Exp, Mult

        for side_effect in TransitiveRelation.side_effects(self, judgment):
            yield side_effect

        # For each of the following, use the default assumptions to
        # verify some conditions before yielding the side effect method
        # (i.e. check using .proven() without arguments)

        # for 2|(b^n), derive 2|b.
        # (can be generalized to any prime number).
        if self.lhs == two and isinstance(self.rhs, Exp):
            try:
                deduce_number_set(self.rhs.base)
                deduce_number_set(self.rhs.exponent)
            except UnsatisfiedPrerequisites:
                pass
            if (InSet(self.rhs.base, Integer).proven()
                    and InSet(self.rhs.exponent, Integer).proven()):
                yield self.eliminate_dividend_exponent

        # for (a^n)|(b^n)
        if (isinstance(self.rhs, Exp) and isinstance(self.lhs, Exp)
                and self.lhs.exponent == self.rhs.exponent):
            try:
                deduce_number_set(self.lhs.base)
                deduce_number_set(self.rhs.base)
                deduce_number_set(self.lhs.exponent)
            except UnsatisfiedPrerequisites:
                pass
            if (InSet(self.lhs.base, Integer).proven()
                    and InSet(self.rhs.base, Integer).proven()
                    and InSet(self.lhs.exponent, NaturalPos).proven()):
                yield self.eliminate_common_exponent

        # for (ka)|(kb)
        if (isinstance(self.lhs, Mult) and isinstance(self.rhs, Mult)):
            operands_intersection = (set(self.lhs.operands).intersection(
                set(self.lhs.operands)))
            if (len(operands_intersection) > 0):
                yield self.eliminate_common_factors
Exemplo n.º 13
0
 def conclude(self, assumptions):
     '''
     Attempt to conclude the equality various ways:
     simple reflexivity (x=x), via an evaluation (if one side is an irreducible),
     or via transitivity.
     '''
     from proveit.logic import TRUE, FALSE, Implies, Iff
     if self.lhs == self.rhs:
         # Trivial x=x
         return self.concludeViaReflexivity()
     if self.lhs or self.rhs in (TRUE, FALSE):
         try:
             # Try to conclude as TRUE or FALSE.
             return self.concludeBooleanEquality(assumptions)
         except ProofFailure:
             pass
     if isIrreducibleValue(self.rhs):
         return self.lhs.evaluation()
     elif isIrreducibleValue(self.lhs):
         return self.rhs.evaluation().deriveReversed()
     try:
         Implies(self.lhs, self.rhs).prove(assumptions, automation=False)
         Implies(self.rhs, self.lhs).prove(assumptions, automation=False)
         # lhs => rhs and rhs => lhs, so attempt to prove lhs = rhs via lhs <=> rhs
         # which works when they can both be proven to be Booleans.
         try:
             return Iff(self.lhs, self.rhs).deriveEquality(assumptions)
         except:
             from proveit.logic.boolean.implication._theorems_ import eqFromMutualImpl
             return eqFromMutualImpl.specialize({
                 A: self.lhs,
                 B: self.rhs
             },
                                                assumptions=assumptions)
     except ProofFailure:
         pass
     """
     # Use concludeEquality if available
     if hasattr(self.lhs, 'concludeEquality'):
         return self.lhs.concludeEquality(assumptions)
     if hasattr(self.rhs, 'concludeEquality'):
         return self.rhs.concludeEquality(assumptions)
     """
     # Use a breadth-first search approach to find the shortest
     # path to get from one end-point to the other.
     return TransitiveRelation.conclude(self, assumptions)
Exemplo n.º 14
0
 def side_effects(self, judgment):
     '''
     Yield the TransitiveRelation side-effects (which also records
     known_left_sides nd known_right_sides).  Also derive the consequent
     as a side-effect if the antecedent is known to be true
     (under the "side-effect" assumptions).
     As a special case, if the consequent is FALSE, do
     derive_via_contradiction.
     '''
     from proveit.logic.booleans import FALSE
     for side_effect in TransitiveRelation.side_effects(self, judgment):
         yield side_effect
     if self.antecedent.proven():
         yield self.derive_consequent  # B given A=>B and A
     if self.consequent == FALSE:
         # Not(A) given A=>FALSE or A given Not(A)=>FALSE
         yield self.derive_via_contradiction
Exemplo n.º 15
0
 def apply_transitivity(self, other, **defaults_config):
     '''
     Apply a transitivity rule to derive from this x<y expression
     and something of the form y<z, y<=z, z>y, z>=y, or y=z to
     obtain x<z.
     '''
     from proveit.logic import Equals
     from . import equiv_transitivity
     other = as_expression(other)
     if isinstance(other, Equals):
         return TransitiveRelation.apply_transitivity(
             self, other)  # handles this special case
     elif self.rhs == other.lhs:
         return equiv_transitivity.instantiate(
             {
                 A: self.lhs,
                 B: self.rhs,
                 C: other.rhs
             }, preserve_all=True)
     elif self.rhs == other.rhs:
         return equiv_transitivity.instantiate(
             {
                 A: self.lhs,
                 B: self.rhs,
                 C: other.lhs
             }, preserve_all=True)
     elif self.lhs == other.lhs:
         return equiv_transitivity.instantiate(
             {
                 A: self.rhs,
                 B: self.lhs,
                 C: other.rhs
             }, preserve_all=True)
     elif self.lhs == other.rhs:
         return equiv_transitivity.instantiate(
             {
                 A: self.rhs,
                 B: self.lhs,
                 C: other.lhs
             }, preserve_all=True)
     else:
         raise TransitivityException(
             None, defaults.assumptions,
             'Transitivity cannot be applied unless there is something '
             'in common in the equalities: %s vs %s' %
             (str(self), str(other)))
Exemplo n.º 16
0
 def __init__(self, A, B, *, styles=None):
     TransitiveRelation.__init__(self, Iff._operator_, A, B, styles=styles)
     self.A = A
     self.B = B
Exemplo n.º 17
0
 def __init__(self, A, B):
     TransitiveRelation.__init__(self, Iff._operator_, A, B)
     self.A = A
     self.B = B
Exemplo n.º 18
0
 def __init__(self, a, b):
     TransitiveRelation.__init__(self, Equals._operator_, a, b)
     '''
Exemplo n.º 19
0
 def __init__(self, antecedent, consequent):
     TransitiveRelation.__init__(self, Implies._operator_, antecedent,
                                 consequent)
     self.antecedent = antecedent
     self.consequent = consequent
Exemplo n.º 20
0
 def __init__(self, antecedent, consequent, *, styles=None):
     TransitiveRelation.__init__(
         self, Implies._operator_, antecedent, consequent,
         styles=styles)
     self.antecedent = antecedent
     self.consequent = consequent
Exemplo n.º 21
0
    def conclude(self, **defaults_config):
        '''
        Attempt to conclude the equivalence in various ways:
        simple reflexivity (A equiv A), via an evaluation (if one side
        is an irreducible), or via transitivity.
        IN PROGRESS
        '''
        if self.lhs == self.rhs:
            try:
                # Trivial A = A
                return self.conclude_via_reflexivity()
            except BaseException:
                pass  # e.g., reflexivity theorem may not be usable
        try:
            return self.conclude_as_folded()
        except ProofFailure:
            raise ProofFailure(
                self, defaults.assumptions,
                "Unable to automatically conclude by "
                "standard means.  To try to prove this via "
                "transitive implication relations, try "
                "'conclude_via_transitivity'.")

    #     if self.lhs or self.rhs in (TRUE, FALSE):
    #         try:
    #             # Try to conclude as TRUE or FALSE.
    #             return self.conclude_boolean_equality(assumptions)
    #         except ProofFailure:
    #             pass
    #     if is_irreducible_value(self.rhs):
    #         try:
    #             evaluation = self.lhs.evaluation(assumptions)
    #             if evaluation.rhs != self.rhs:
    #                 raise ProofFailure(self, assumptions, "Does not match with evaluation: %s"%str(evaluation))
    #             return evaluation
    #         except SimplificationError as e:
    #             raise ProofFailure(self, assumptions, "Evaluation error: %s"%e.message)
    #     elif is_irreducible_value(self.lhs):
    #         try:
    #             evaluation = self.rhs.evaluation(assumptions)
    #             if evaluation.rhs != self.lhs:
    #                 raise ProofFailure(self, assumptions, "Does not match with evaluation: %s"%str(evaluation))
    #             return evaluation.derive_reversed()
    #         except SimplificationError as e:
    #             raise ProofFailure(self, assumptions, "Evaluation error: %s"%e.message)
    #     try:
    #         Implies(self.lhs, self.rhs).prove(assumptions, automation=False)
    #         Implies(self.rhs, self.lhs).prove(assumptions, automation=False)
    #         # lhs => rhs and rhs => lhs, so attempt to prove lhs = rhs via lhs <=> rhs
    #         # which works when they can both be proven to be Boolean.
    #         try:
    #             return Iff(self.lhs, self.rhs).derive_equality(assumptions)
    #         except:
    #             from proveit.logic.booleans.implication import eq_from_mutual_impl
    #             return eq_from_mutual_impl.instantiate({A:self.lhs, B:self.rhs}, assumptions=assumptions)
    #     except ProofFailure:
    #         pass

    #     """
    #     # Use conclude_equality if available
    #     if hasattr(self.lhs, 'conclude_equality'):
    #         return self.lhs.conclude_equality(assumptions)
    #     if hasattr(self.rhs, 'conclude_equality'):
    #         return self.rhs.conclude_equality(assumptions)
    #     """
    # Use a breadth-first search approach to find the shortest
    # path to get from one end-point to the other.
        return TransitiveRelation.conclude(self)
Exemplo n.º 22
0
 def __init__(self, operator, lhs, rhs, *, styles):
     TransitiveRelation.__init__(self, operator, lhs, rhs, styles=styles)
     self.divisor = self.lhs
     self.dividend = self.rhs