Equivalence of SQL expressions #
Two expressions are equivalent in a typing context Γ (Expr.Equiv Γ e₁ e₂) when, on every row
conforming to Γ, they both fail or both succeed with the same datum. Equivalence observes
Except.toOption: whether evaluation succeeds and with what, not which error it raises, since
strict evaluation fixes one order of evaluation among those SQL permits and the error an engine
reports may differ.
Equivalence is an equivalence relation (Expr.equivSetoid) and a congruence for every expression
constructor. It is compatible with renaming (Expr.Equiv.rename), with replacing the substituted
expressions by equivalent ones (Expr.Equiv.subst_right), and with substituting into equivalent
expressions, provided the substituted expressions are safe and have the column types
(Expr.Equiv.subst). The side condition is needed: TRUE and x IS NULL OR x IS NOT NULL are
equivalent, but substituting 1 / 0 for x makes only the second fail.
The laws that the expression simplifier (Cslib.SQL.Expr.simplify) applies are proved here, each
with the side condition strict evaluation needs: NOT over a comparison or a null test, double
negation of a boolean or an integer, TRUE and FALSE as operands of AND and OR, the
identities of +, - and *, IN over an empty or one-element list, and CASE with a literal
condition. A law that inspects an operand's value needs the operand's type (p AND TRUE is p
only for a boolean p), and a law that drops an operand needs it to be safe (p AND FALSE is
FALSE only when p cannot fail).
It adapts and redesigns the expression equivalence of sql_equiv (revision 38030a6b) by Evelyn
Mitchell, which quantified over all rows and compared results including errors; here equivalence
quantifies over the rows of a typing context and does not distinguish errors.
Main definitions #
Cslib.SQL.Expr.Equiv: equivalence of expressions over the rows conforming to a typing context.Cslib.SQL.Expr.equivSetoid: the corresponding setoid.
Main results #
Cslib.SQL.Expr.Equiv.unary,Cslib.SQL.Expr.Equiv.binary,Cslib.SQL.Expr.Equiv.inList,Cslib.SQL.Expr.Equiv.caseWhen: congruence.Cslib.SQL.Expr.Equiv.binary_comm: commutative operators commute on expressions.Cslib.SQL.Expr.Equiv.rename,Cslib.SQL.Expr.Equiv.subst,Cslib.SQL.Expr.Equiv.subst_right: compatibility with renaming and substitution.Cslib.SQL.Expr.Equiv.evalTruth: equivalent predicates select the same rows.Cslib.SQL.Expr.Equiv.of_eval_eq_ok: an expression with the same value on every row is that literal (constant folding).Cslib.SQL.Expr.Equiv.not_cmp,Cslib.SQL.Expr.Equiv.not_not,Cslib.SQL.Expr.Equiv.and_true,Cslib.SQL.Expr.Equiv.and_false,Cslib.SQL.Expr.Equiv.add_zero,Cslib.SQL.Expr.Equiv.inList_nil,Cslib.SQL.Expr.Equiv.inList_singleton,Cslib.SQL.Expr.Equiv.caseWhen_trueand their siblings: the simplifier's laws.
Equivalent predicates have the same truth value, or both fail, on every conforming row; so a
WHERE or ON clause keeps the same rows with either.
Congruence #
A binary operator whose observations do not depend on the order of its operands is
commutative on expressions. It applies to AND and OR (evalBinOp_and_comm, …), whose two
sides may raise different first errors, so Equiv.of_eval_eq cannot prove them.
Renaming and substitution #
Expressions equivalent over the reindexed context stay equivalent after renaming.
Substituting equivalent expressions gives equivalent results.
Substituting into equivalent expressions gives equivalent results, when the substituted
expressions are divSafe and have the column types. Without this side condition the substituted
expressions may fail where an equivalent expression does not evaluate them.
Laws #
The rewrites of the expression simplifier (Cslib.SQL.Expr.simplify). A law that inspects an
operand's value needs the operand's type; a law that drops an operand needs it to be safe, since
strict evaluation fails when the dropped operand fails.
NOT (a op b) is a (negate op) b; for instance NOT (a < b) is a >= b.
NOT (e IS NULL) is e IS NOT NULL.
NOT (e IS NOT NULL) is e IS NULL.
NOT (NOT p) is p for a boolean p.
-(-x) is x for an integer x.
p AND TRUE is p for a boolean p.
TRUE AND p is p for a boolean p.
p OR FALSE is p for a boolean p.
FALSE OR p is p for a boolean p.
p AND FALSE is FALSE for a safe predicate p. Safety is needed: (1 / 0 = 1) AND FALSE
fails.
FALSE AND p is FALSE for a safe predicate p.
p OR TRUE is TRUE for a safe predicate p.
TRUE OR p is TRUE for a safe predicate p.
x + 0 is x for an integer x.
0 + x is x for an integer x.
x - 0 is x for an integer x.
x * 1 is x for an integer x.
1 * x is x for an integer x.