logo

Demonstrations for the theory of proveit.logic.equality

In [1]:
import proveit
%begin demonstrations

Substitution

Equality is a fundamental concept of logic. When two mathematical operations are equal, then one may be substituted for the other. This is rule is defined by the substitution axiom:

In [2]:
from proveit.logic.equality  import substitution
from proveit import a, b, c, d, x, y, z, fx # we'll use these later
substitution
Out[2]:

The English translation of this axiom is: for any function $f$ and any $x, y$ such that $x=y$, $f(x) = f(y)$. In other words, we may substitute $y$ for $x$ in any function whenever $x=y$. The equality of $x$ and $y$ transfers to an equality between $f(x)$ and $f(y)$. This is fundamental to the meaning of equality regardless of what $f$ does (as long as it can act on a single argument). We may instantiate this axiom using any operation for $f$. For example,

In [3]:
from proveit.logic import Not, Equals
substitution.instantiate({fx:Not(x), x:a, y:b}, assumptions=[Equals(a, b)])
Out[3]:

There are more convenient ways to apply this substitution rule than manual instantiation that was demonstrated in the previous input. The Equals class has the substitution, sub_left_side_into and sub_right_side_into methods for conveniently applying substitution and its variants, as we will demonstrate below. Each of these methods takes a lambda_map argument to provide a theory for the substitution -- what is being substituted and where. A lambda_map can be an actual Lambda expression, an InnerExpr object, or if it is neither of these, it can be any other Expression for which the default will be to performa a global replacement.

In [4]:
from proveit import Lambda
from proveit.numbers import Add, frac, Exp
expr = Equals(a, Add(b, frac(c, d), Exp(c, d)))          
Out[4]:
expr:

The global_repl static method of Lambda is useful for creating a global replacement lambda map. Below, we create a map for replacing every occurence of $d$ in expr with anything else:

In [5]:
g_repl = Lambda.global_repl(expr, d)
Out[5]:
g_repl:

We now use this lambda map to replace occurences of $d$ in expr with $y$:

In [6]:
d_eq_y = Equals(d, y)
Out[6]:
d_eq_y:
In [7]:
d_eq_y.substitution(g_repl, assumptions=[d_eq_y])
Out[7]:

Or we can take advantage of that a global replacement is performed by default when a non-Lambda expression is provided as the "lambda_map".

In [8]:
d_eq_y.substitution(expr, assumptions=[d_eq_y])
Out[8]:

Either way, the generated proof is the same:

In [9]:
d_eq_y.substitution(expr, assumptions=[d_eq_y]).proof()
Out[9]:
 step typerequirementsstatement
0instantiation1, 2  ⊢  
  : , : , :
1axiom  ⊢  
 proveit.logic.equality.substitution
2assumption  ⊢  

If we want to perform substitution for a specific inner expression, and not necessarily a global replacement, the proveit._core_.expression.InnerExpr class (aliased as proveit.InnerExpr) is extremely convenient. It uses some Python tricks via implementing the __getitem__ and __getattr__ methods. First, you create the InnerExpr object by calling the inner_expr() method on the top-level expression:

In [10]:
inner_expr = expr.inner_expr()
Out[10]:
inner_expr:

The InnerExpr object displays itself with two important pieces of information: the lambda map that it represents and the inner expression of the top-level expression that would be replaced by this lambda map. The point is that we will be able to "dig" in to inner expressions of the top-level expression via accessing sub-expression attributes. For example, we next will "dig" into the "right hand side" (rhs) of the master expression:

In [11]:
inner_expr = inner_expr.rhs
Out[11]:
inner_expr:

By accessing the rhs attribute, we created a new InnerExpr object that has the same top-level expression as the original but has a new current inner expression. Note that the InnerExpr class does not know anything about the rhs attribute itself; it is relying on the fact that the previous sub-expression has this attribute. The InnerExpr class also has tricks for getting an sub-expression with an index (or key):

In [12]:
inner_expr = inner_expr.operands[1]
Out[12]:
inner_expr:

Now will "dig" down to the denominator of $\frac{c}{d}$ and show how we use the InnerExpr class to replace a particular occurence of $d$ rather than a global replacement:

In [13]:
inner_expr = inner_expr.denominator
Out[13]:
inner_expr:
In [14]:
d_eq_y.substitution(inner_expr, assumptions=[d_eq_y])
Out[14]:

Let us demonstrate this technique again, replacing the other occurrence of $d$. This time we do this more succinctly, without the extra pedogogial steps:

In [15]:
d_eq_y.substitution(expr.inner_expr().rhs.operands[2].exponent, assumptions=[d_eq_y])
Out[15]:

The substition method, that we demonstrated above, is a direct application of the substitution axiom. It proves the equality between some $f(x)$ and some $f(y)$. We often will want to take a shortcut to perform a statement substitution in which we prove some $P(y)$ is true assuming that $P(x)$ is true and $P(x) = P(y)$. For this, we have the sub_right_side_into and sub_left_side_into methods.

If the expression that we want to substitute in is on the right hand side of the Equals object playing the role of $x=y$, then we use sub_right_side_into:

In [16]:
d_eq_y.sub_right_side_into(g_repl, assumptions=[d_eq_y,expr])
Out[16]:

We can also take advantage of the global replacement default and provide a non-Lambda Expression.

In [17]:
d_eq_y.sub_right_side_into(expr, assumptions=[d_eq_y,expr])
Out[17]:

If the expression that we want to substitute in is on the left hand side of the Equals object playing the role of $x=y$, then we use sub_left_side_into:

In [18]:
y_eq_d = Equals(y, d)
Out[18]:
y_eq_d:
In [19]:
y_eq_d.sub_left_side_into(g_repl, assumptions=[y_eq_d,expr])
Out[19]:

Again, we can provide a non-Lambda Expression to do a simple global replacement.

In [20]:
y_eq_d.sub_left_side_into(expr, assumptions=[y_eq_d,expr])
Out[20]:

The proof uses a theorem the relies upon the substitution axiom, rather than using the substition axiom directly:

In [21]:
y_eq_d.sub_left_side_into(expr, assumptions=[y_eq_d,expr]).proof()
Out[21]:
 step typerequirementsstatement
0instantiation1, 2, 3,  ⊢  
  : , : , :
1theorem  ⊢  
 proveit.logic.equality.sub_left_side_into
2assumption  ⊢  
3assumption  ⊢  

Reflexivity, symmetry, and transitivity

Reflexivity, symmetry, and transitivity are also fundamental properties of equality, in addition to the ability to perform substitution. Reflexivity is the fact that any mathematical object is equal to itself. Symmetry is the fact that $x = y$ and $y = x$ are equivalent (either both of these are true or both of these false). Transitivity is the ability to derive $x=z$ from $x=y$ and $y=z$. These are all axioms.

In [22]:
from proveit.logic.equality  import equals_reflexivity, equals_symmetry, equals_transitivity
In [23]:
equals_reflexivity
Out[23]:
In [24]:
equals_symmetry
Out[24]:
In [25]:
equals_transitivity
Out[25]:

equals_reversal is a useful theorem for applying the symmetry property of equality:

In [26]:
from proveit.logic.equality import equals_reversal
equals_reversal # y=x derives from x=y
Out[26]:

These three properties are applied automatically for Equals objects

Reflexivity is concluded automatically:

In [27]:
Equals(a, a).prove()
Out[27]:
In [28]:
Equals(a, a).prove().proof()
Out[28]:
 step typerequirementsstatement
0instantiation1  ⊢  
  :
1axiom  ⊢  
 proveit.logic.equality.equals_reflexivity

Symmetric statements are derived as side-effects. Note that the Judgment.derive_side_effects method employs a mechanism to prevent infinite recursion or this would not be possible (it would continually go back and forth, proving $y=x$ from $x=y$ then $x=y$ from $y=x$, ad infinitum)

In [29]:
a_eq_b = Equals(a, b)
Out[29]:
a_eq_b:
In [30]:
Equals(b, a).prove([a_eq_b])
Out[30]:

The reversed form may also be derived explicitly via derive_reversed. The proof is the same.

In [31]:
a_eq_b.prove([a_eq_b]).derive_reversed()
Out[31]:

Transitivity derivations are attempted with automation via the conclude_via_transitivity method from the TransitiveRelation superclass of Equals. This performs a breadth-first, bidirectional search (meeting in the middle from both ends) over the space of Judgment objects representing equality and using appropriate assumptions. This is therefore reasonably efficient. Efficiency should not really be an issue, anyways, as long as proofs for each theorem are relatively small. A long proof should be broken up into several smaller proofs for lemma-like theorems. In that case (in the setting of small proofs), the space of Judgments will be small and this search algorithm will have ample efficiency.

In [32]:
# We'll make this interesting by reversing some of the equations in the chain.
c_eq_b = Equals(c, b) 
Out[32]:
c_eq_b:
In [33]:
d_eq_c = Equals(d, c)
Out[33]:
d_eq_c:
In [34]:
d_eq_z = Equals(d, z)
Out[34]:
d_eq_z:
In [35]:
a_eq_z = Equals(a, z).conclude_via_transitivity(assumptions=[a_eq_b, c_eq_b, d_eq_c, d_eq_z])
Out[35]:
a_eq_z: , , ,  ⊢  

The apply_transivity method applies the transitivity relation explicity.

In [36]:
a_eq_b.prove([a_eq_b]).apply_transitivity(c_eq_b, assumptions=[c_eq_b])
Out[36]:

Evaluation

An evaluation is an equality in which the right hand side is an irreducible value (specifically, an instance of proveit.logic.irreducible_value.IrreducibleValue, aliased as proveit.logic.IrreducibleValue). An irreducible value represents a mathematical object in its simplest form. It cannot be reduced. The evaluation of an IrreducibleValue is itself. When an evaluation is proven, the associated Judgment is stored for future reference in Equals.evaluations for making other evaluations.

TRUE ($\top$) and FALSE ($\bot$) are both IrreducibleValues:

In [37]:
from proveit.logic import TRUE, FALSE, IrreducibleValue
In [38]:
isinstance(TRUE, IrreducibleValue)
Out[38]:
True
In [39]:
isinstance(FALSE, IrreducibleValue)
Out[39]:
True
In [40]:
TRUE.evaluation()
Out[40]:

An IrreducibleValue should implement the eval_equality and not_equal methods to prove equality relations with other IrreducibleValues as appropriate.

In [41]:
Equals(Equals(FALSE, TRUE), FALSE).derive_contradiction([Equals(FALSE, TRUE)])
Out[41]:
In [42]:
TRUE.eval_equality(TRUE)
Out[42]:
In [43]:
TRUE.eval_equality(FALSE)
Out[43]:
In [44]:
TRUE.not_equal(FALSE)
Out[44]:

A proven expression will evaluate to TRUE.

In [45]:
a_eq_T = a.evaluation([a])
Out[45]:
a_eq_T:  ⊢  

A disproven expression will evaluate to FALSE.

In [46]:
a_eq_F = a.evaluation([Not(a)])
Out[46]:
a_eq_F:  ⊢  

If the expression to be evaluated is known to be equal to an expression that has already been evaluated, transitivity will automatically be applied.

In [47]:
b_eq_F = b.evaluation([Equals(b, a), Not(a)])
Out[47]:
b_eq_F: ,  ⊢  
In [48]:
b_eq_F.proof()
Out[48]:
 step typerequirementsstatement
0instantiation1, 2, 3,  ⊢  
  : , : , :
1axiom  ⊢  
 proveit.logic.equality.equals_transitivity
2assumption  ⊢  
3instantiation4, 5  ⊢  
  :
4axiom  ⊢  
 proveit.logic.booleans.negation.negation_elim
5assumption  ⊢  

When evaluating an expression, an evaluation of the operands will be attempted. The operation class is responsible for overridding the evaluation method, or do_reduced_evaluation (called by the default Expression.evaluation method that first reduces the operation by evaluating its operands) in order to properly treat the operation applied to irreducible values or generate a more efficient proof as appropriate.

In [49]:
from proveit.logic import Or, Not, in_bool
In [50]:
nested_eval = Or(a, Not(a)).evaluation([a])
Out[50]:
nested_eval:  ⊢  

Boolean equality

Equality with TRUE ($\top$) or FALSE ($\bot$) has special logical consequences. The Equals object has automation capabilities to treat these special kinds of equations.

In [51]:
TRUE
Out[51]:
In [52]:
FALSE
Out[52]:

Proofs via boolean equality are automatic via Equals.deduce_side_effects:

In [53]:
a.prove([Equals(a, TRUE)])
Out[53]:
In [54]:
Not(b).prove([Equals(b, FALSE)])
Out[54]:

Going the other direction, boolean equalities are proven automatically via Equals.conclude:

In [55]:
Equals(x, TRUE).prove([x])
Out[55]:
In [56]:
Equals(TRUE, y).prove([y])
Out[56]:
In [57]:
Equals(c, FALSE).prove([Not(c)])
Out[57]:
In [58]:
Equals(FALSE, c).prove([Not(c)])
Out[58]:

When something is equal to FALSE and known to be true, there is a contradiction. That is, FALSE is a consequence. The derive_contradiction method of Equals can be used to prove such a contradiction:

In [59]:
contradiction = Equals(a, FALSE).derive_contradiction([a, Equals(a, FALSE)])
Out[59]:
contradiction: ,  ⊢  
In [60]:
contradiction.proof()
Out[60]:
 step typerequirementsstatement
0instantiation1, 2, 3,  ⊢  
  :
1theorem  ⊢  
 proveit.logic.equality.contradiction_via_falsification
2assumption  ⊢  
3assumption  ⊢  

Contradictions are useful in making contradiction proofs (reductio ad absurdum). The affirm_via_contradiction and deny_via_contradiction methods are useful in making such a proof. They both use derive_contradiction. For example:

In [61]:
from proveit.logic import Implies, in_bool
not_a__truth = Equals(a, FALSE).affirm_via_contradiction(Not(a), [Implies(a, Equals(a, FALSE)), in_bool(a)])
Out[61]:
not_a__truth: ,  ⊢  
In [62]:
not_a__truth.proof().num_steps()
Out[62]:
13

It is more efficient to use deny_via_contradiction when proving the negation of something. Here we prove the same as above but in fewer steps.

In [63]:
not_b__truth = Equals(b, FALSE).deny_via_contradiction(b, [Implies(b, Equals(b, FALSE)), in_bool(b)])
Out[63]:
not_b__truth: ,  ⊢  
In [64]:
not_b__truth.proof().num_steps()
Out[64]:
9
In [65]:
not_b__truth.proof()
Out[65]:
 step typerequirementsstatement
0instantiation1, 2, 3,  ⊢  
  :
1axiom  ⊢  
 proveit.logic.booleans.implication.denial_via_contradiction
2assumption  ⊢  
3deduction4  ⊢  
4instantiation5, 8, 6,  ⊢  
  :
5theorem  ⊢  
 proveit.logic.equality.contradiction_via_falsification
6modus ponens7, 8,  ⊢  
7assumption  ⊢  
8assumption  ⊢  

Equality and sets

All equality expressions are in the boolean set by the equality_in_bool axiom. That is, given any two mathematical objects, they are either equal or not (even nonsense is either the same nonsense or different nonsense).

In [66]:
eq_in_bool = Equals(a, b).deduce_in_bool()
Out[66]:
eq_in_bool:  ⊢  

A singleton is a set with one element. If $x=c$, then $x$ is in the singleton set of $\{c\}$:

In [67]:
in_singleton_truth = Equals(x, c).derive_is_in_singleton([Equals(x, c)])
Out[67]:
in_singleton_truth:  ⊢  

Not equals

The NotEquals operation is a more way of expressing that two mathematical objects are not equal to each other.

In [68]:
from proveit.logic import NotEquals
In [69]:
a_neq_b = NotEquals(a, b)
Out[69]:
a_neq_b:
In [70]:
a_neq_b.definition()
Out[70]:

From $\lnot (a = b)$ one can derive $a \neq b$ and vice-versa, folding and unfolding the NotEquals.

In [71]:
a_neq_b.prove([Not(a_eq_b)])
Out[71]:
In [72]:
a_neq_b.prove([Not(a_eq_b)]).proof()
Out[72]:
 step typerequirementsstatement
0instantiation1, 2  ⊢  
  : , :
1theorem  ⊢  
 proveit.logic.equality.fold_not_equals
2assumption  ⊢  
In [73]:
Not(a_eq_b).prove([a_neq_b])
Out[73]:
In [74]:
Not(a_eq_b).prove([a_neq_b]).proof()
Out[74]:
 step typerequirementsstatement
0instantiation1, 2  ⊢  
  : , :
1theorem  ⊢  
 proveit.logic.equality.unfold_not_equals
2assumption  ⊢  

NotEquals also has a symmetry property which can be applied directly. It can be proven through automation.

In [75]:
NotEquals(b, a).prove([a_neq_b])
Out[75]:

Or explicitly via derive_reversed

In [76]:
a_neq_b.prove([a_neq_b]).derive_reversed()
Out[76]:

If two objects are both equal and not equal, there is a contradiction.

In [77]:
neq_contradiction = a_neq_b.derive_contradiction([a_neq_b, a_eq_b])
Out[77]:
neq_contradiction: ,  ⊢  
In [78]:
a_neq_a = NotEquals(a, a)
Out[78]:
a_neq_a:
In [79]:
b_from_contradiction = a_neq_a.affirm_via_contradiction(b, [Implies(Not(b), a_neq_a), in_bool(b)])
Out[79]:
b_from_contradiction: ,  ⊢  
In [80]:
%end demonstrations