SQL's three-valued logic #
SQL predicates take three truth values, ordered false < unknown < true. With this order, SQL's
AND is the minimum ⊓, OR is the maximum ⊔, and NOT reverses the order while fixing
unknown: this is Kleene's strong three-valued logic [Kle52]. A WHERE or ON clause keeps a
row exactly when its predicate is true (Truth.isTrue).
The order is transported from ℕ along Truth.rank, so Truth is a LinearOrder and a
BoundedOrder with ⊥ = false and ⊤ = true, hence a distributive lattice, and Mathlib's lattice
lemmas (inf_comm, sup_assoc, inf_sup_left, …) apply to AND and OR unchanged. The minimum
and maximum are given by truth tables, so the kernel evaluates them by decide.
NOT is Truth.not, an order-reversing involution (Truth.notOrderIso : Truth ≃o Truthᵒᵈ). It is
not a lattice complement: unknown ⊔ not unknown = unknown, so excluded middle fails
(Truth.unknown_sup_not_unknown). For this reason NOT is not a Compl instance: Mathlib's
compl lemmas belong to Heyting and Boolean algebras, and the Heyting complement of this chain
sends unknown to false, which is not SQL's NOT. De Morgan's laws (Truth.not_inf,
Truth.not_sup) and Kleene's law a ⊓ not a ≤ b ⊔ not b (Truth.inf_not_le_sup_not) are proved
directly.
It adapts and redesigns the Trilean type of sql_equiv (revision 38030a6b) by Evelyn
Mitchell, whose connectives were separate truth tables; here they are the lattice operations.
Main definitions #
Cslib.SQL.Truth: the truth valuesfalse,unknownandtrue, withfalse < unknown < true.Cslib.SQL.Truth.not: Kleene negation.Cslib.SQL.Truth.ofBool,Cslib.SQL.Truth.isTrue: embedding of two-valued logic, and the test aWHEREclause applies.
Main results #
Cslib.SQL.Truth.not_not,Cslib.SQL.Truth.not_inf,Cslib.SQL.Truth.not_sup:NOTis an involution satisfying De Morgan's laws.Cslib.SQL.Truth.inf_not_le_sup_not: Kleene's law.Cslib.SQL.Truth.unknown_sup_not_unknown: excluded middle fails.Cslib.SQL.Truth.ofBool_and,Cslib.SQL.Truth.ofBool_or,Cslib.SQL.Truth.ofBool_not: on known values the connectives are Boolean.Cslib.SQL.Truth.isTrue_inf,Cslib.SQL.Truth.isTrue_sup: a conjunction holds when both conjuncts hold, a disjunction when one does.
References #
Distinct truth values have distinct ranks.
The test a WHERE or ON clause applies: only true keeps a row.