Documentation

CslibFrontier.Languages.SQL.Equiv

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 #

Main results #

def Cslib.SQL.Expr.Equiv {n : ℕ} (Γ : Fin n → SqlType) (e₁ e₂ : Expr n) :

Expressions e₁ and e₂ are equivalent in the typing context Γ: on every row conforming to Γ, both fail or both succeed with the same datum.

Instances For
    theorem Cslib.SQL.Expr.Equiv.refl {n : ℕ} {Γ : Fin n → SqlType} (e : Expr n) :
    Equiv Γ e e
    theorem Cslib.SQL.Expr.Equiv.symm {n : ℕ} {Γ : Fin n → SqlType} {e₁ e₂ : Expr n} (h : Equiv Γ e₁ e₂) :
    Equiv Γ e₂ e₁
    theorem Cslib.SQL.Expr.Equiv.trans {n : ℕ} {Γ : Fin n → SqlType} {e₁ e₂ e₃ : Expr n} (h₁₂ : Equiv Γ e₁ e₂) (h₂₃ : Equiv Γ e₂ e₃) :
    Equiv Γ e₁ e₃
    @[instance_reducible]
    instance Cslib.SQL.Expr.Equiv.instTrans {n : ℕ} {Γ : Fin n → SqlType} :
    Trans (Equiv Γ) (Equiv Γ) (Equiv Γ)
    theorem Cslib.SQL.Expr.Equiv.of_eval_eq {n : ℕ} {Γ : Fin n → SqlType} {e₁ e₂ : Expr n} (h : ∀ (r : Row n), Row.Conforms Γ r → eval r e₁ = eval r e₂) :
    Equiv Γ e₁ e₂

    Expressions with the same evaluation on every conforming row are equivalent.

    theorem Cslib.SQL.Expr.Equiv.evalTruth {n : ℕ} {Γ : Fin n → SqlType} {e₁ e₂ : Expr n} (h : Equiv Γ e₁ e₂) {r : Row n} (hr : Row.Conforms Γ r) :

    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 #

    theorem Cslib.SQL.Expr.Equiv.unary {n : ℕ} {Γ : Fin n → SqlType} (op : UnOp) {e e' : Expr n} (h : Equiv Γ e e') :
    Equiv Γ (Expr.unary op e) (Expr.unary op e')
    theorem Cslib.SQL.Expr.Equiv.binary {n : ℕ} {Γ : Fin n → SqlType} (op : BinOp) {e₁ e₁' e₂ e₂' : Expr n} (h₁ : Equiv Γ e₁ e₁') (h₂ : Equiv Γ e₂ e₂') :
    Equiv Γ (Expr.binary op e₁ e₂) (Expr.binary op e₁' e₂')
    theorem Cslib.SQL.Expr.Equiv.inList {n : ℕ} {Γ : Fin n → SqlType} {e e' : Expr n} (h : Equiv Γ e e') (vs : List Datum) :
    Equiv Γ (e.inList vs) (e'.inList vs)
    theorem Cslib.SQL.Expr.Equiv.binary_comm {n : ℕ} {Γ : Fin n → SqlType} {op : BinOp} (hop : ∀ (a b : Datum), (evalBinOp op a b).toOption = (evalBinOp op b a).toOption) (e₁ e₂ : Expr n) :
    Equiv Γ (Expr.binary op e₁ e₂) (Expr.binary op e₂ e₁)

    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.

    theorem Cslib.SQL.Expr.Equiv.caseWhen {n : ℕ} {Γ : Fin n → SqlType} {c c' a a' b b' : Expr n} (hc : Equiv Γ c c') (ha : Equiv Γ a a') (hb : Equiv Γ b b') :
    Equiv Γ (c.caseWhen a b) (c'.caseWhen a' b')

    Renaming and substitution #

    theorem Cslib.SQL.Expr.Equiv.rename {n m : ℕ} {Γ : Fin m → SqlType} {e₁ e₂ : Expr n} (ρ : Fin n → Fin m) (h : Equiv (Γ ∘ ρ) e₁ e₂) :
    Equiv Γ (Expr.rename ρ e₁) (Expr.rename ρ e₂)

    Expressions equivalent over the reindexed context stay equivalent after renaming.

    theorem Cslib.SQL.Expr.Equiv.subst_right {n m : ℕ} {Δ : Fin m → SqlType} {σ σ' : Fin n → Expr m} (h : ∀ (i : Fin n), Equiv Δ (σ i) (σ' i)) (e : Expr n) :
    Equiv Δ (Expr.subst σ e) (Expr.subst σ' e)

    Substituting equivalent expressions gives equivalent results.

    theorem Cslib.SQL.Expr.Equiv.subst {n m : ℕ} {Γ : Fin n → SqlType} {Δ : Fin m → SqlType} {σ : Fin n → Expr m} (hσ : ∀ (i : Fin n), typeOf Δ (σ i) = some (Γ i)) (hσs : ∀ (i : Fin n), (σ i).divSafe = true) {e₁ e₂ : Expr n} (h : Equiv Γ e₁ e₂) :
    Equiv Δ (Expr.subst σ e₁) (Expr.subst σ e₂)

    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.

    theorem Cslib.SQL.Expr.Equiv.of_eval_eq_ok {n : ℕ} {Γ : Fin n → SqlType} {e : Expr n} {d : Datum} (h : ∀ (r : Row n), Row.Conforms Γ r → eval r e = Except.ok d) (τ : SqlType) :
    Equiv Γ e (ofDatum τ d)

    An expression that succeeds with the same datum on every conforming row is that literal.

    theorem Cslib.SQL.Expr.Equiv.not_cmp {n : ℕ} {Γ : Fin n → SqlType} (op : CmpOp) (a b : Expr n) :

    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.

    theorem Cslib.SQL.Expr.Equiv.neg_neg {n : ℕ} {Γ : Fin n → SqlType} {x : Expr n} (hx : typeOf Γ x = some SqlType.int) :

    -(-x) is x for an integer x.

    theorem Cslib.SQL.Expr.Equiv.and_true {n : ℕ} {Γ : Fin n → SqlType} {p : Expr n} (hp : typeOf Γ p = some SqlType.bool) :

    p AND TRUE is p for a boolean p.

    theorem Cslib.SQL.Expr.Equiv.true_and {n : ℕ} {Γ : Fin n → SqlType} {p : Expr n} (hp : typeOf Γ p = some SqlType.bool) :

    TRUE AND p is p for a boolean p.

    theorem Cslib.SQL.Expr.Equiv.or_false {n : ℕ} {Γ : Fin n → SqlType} {p : Expr n} (hp : typeOf Γ p = some SqlType.bool) :

    p OR FALSE is p for a boolean p.

    theorem Cslib.SQL.Expr.Equiv.false_or {n : ℕ} {Γ : Fin n → SqlType} {p : Expr n} (hp : typeOf Γ p = some SqlType.bool) :

    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.

    theorem Cslib.SQL.Expr.Equiv.add_zero {n : ℕ} {Γ : Fin n → SqlType} {x : Expr n} (hx : typeOf Γ x = some SqlType.int) :

    x + 0 is x for an integer x.

    theorem Cslib.SQL.Expr.Equiv.zero_add {n : ℕ} {Γ : Fin n → SqlType} {x : Expr n} (hx : typeOf Γ x = some SqlType.int) :

    0 + x is x for an integer x.

    theorem Cslib.SQL.Expr.Equiv.sub_zero {n : ℕ} {Γ : Fin n → SqlType} {x : Expr n} (hx : typeOf Γ x = some SqlType.int) :

    x - 0 is x for an integer x.

    theorem Cslib.SQL.Expr.Equiv.mul_one {n : ℕ} {Γ : Fin n → SqlType} {x : Expr n} (hx : typeOf Γ x = some SqlType.int) :

    x * 1 is x for an integer x.

    theorem Cslib.SQL.Expr.Equiv.one_mul {n : ℕ} {Γ : Fin n → SqlType} {x : Expr n} (hx : typeOf Γ x = some SqlType.int) :

    1 * x is x for an integer x.

    theorem Cslib.SQL.Expr.Equiv.inList_nil {n : ℕ} {Γ : Fin n → SqlType} {e : Expr n} (he : IsSafe Γ e) :

    e IN () is FALSE for a safe e, also when e is NULL.

    theorem Cslib.SQL.Expr.Equiv.inList_singleton {n : ℕ} {Γ : Fin n → SqlType} (e : Expr n) (v : Datum) (τ : SqlType) :

    e IN (v) is e = v.

    theorem Cslib.SQL.Expr.Equiv.caseWhen_true {n : ℕ} {Γ : Fin n → SqlType} (a b : Expr n) :

    CASE WHEN TRUE THEN a ELSE b END is a.

    theorem Cslib.SQL.Expr.Equiv.caseWhen_false {n : ℕ} {Γ : Fin n → SqlType} (a b : Expr n) :

    CASE WHEN FALSE THEN a ELSE b END is b.

    theorem Cslib.SQL.Expr.Equiv.caseWhen_null {n : ℕ} {Γ : Fin n → SqlType} (τ : SqlType) (a b : Expr n) :
    Equiv Γ ((null τ).caseWhen a b) b

    CASE WHEN NULL THEN a ELSE b END is b.

    def Cslib.SQL.Expr.equivSetoid {n : ℕ} (Γ : Fin n → SqlType) :

    Equivalence in a typing context, as a setoid.

    Instances For