Documentation

CslibFrontier.Languages.SQL.Truth

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 #

Main results #

References #

SQL's truth values. unknown is the truth value of a comparison involving NULL.

  • false : Truth

    Definitely false.

  • unknown : Truth

    Unknown.

  • true : Truth

    Definitely true.

Instances For
    @[instance_reducible]
    @[instance_reducible]

    The position of a truth value in the chain false < unknown < true.

    Instances For

      Distinct truth values have distinct ranks.

      @[instance_reducible]

      Kleene conjunction, SQL's AND: the minimum in false < unknown < true.

      @[instance_reducible]

      Kleene disjunction, SQL's OR: the maximum in false < unknown < true.

      @[instance_reducible]

      The order false < unknown < true, transported along rank; its minimum and maximum are the truth tables of AND and OR.

      @[simp]
      @[simp]
      @[simp]
      @[simp]

      A disjunction is true exactly when a disjunct is.

      A disjunction is false exactly when both disjuncts are.

      A conjunction is true exactly when both conjuncts are.

      A conjunction is false exactly when a conjunct is.

      Kleene negation, SQL's NOT: it exchanges false and true and fixes unknown.

      Instances For
        @[simp]

        NOT is an involution.

        @[simp]
        theorem Cslib.SQL.Truth.not_le_not {a b : Truth} :
        a.not ≤ b.not ↔ b ≤ a

        NOT reverses the order.

        NOT is an order isomorphism onto the order dual.

        Instances For
          @[simp]
          theorem Cslib.SQL.Truth.not_inf (a b : Truth) :
          (min a b).not = max a.not b.not

          De Morgan: NOT (a AND b) = NOT a OR NOT b.

          @[simp]
          theorem Cslib.SQL.Truth.not_sup (a b : Truth) :
          (max a b).not = min a.not b.not

          De Morgan: NOT (a OR b) = NOT a AND NOT b.

          Kleene's law: a contradiction is below every excluded-middle instance. Together with not_not, not_inf and not_sup it makes Truth a Kleene algebra in the lattice sense.

          The truth value of a known Boolean.

          Instances For
            @[simp]
            @[simp]
            theorem Cslib.SQL.Truth.ofBool_and (a b : Bool) :
            ofBool (a && b) = min (ofBool a) (ofBool b)

            On known values, AND is Boolean conjunction.

            @[simp]
            theorem Cslib.SQL.Truth.ofBool_or (a b : Bool) :
            ofBool (a || b) = max (ofBool a) (ofBool b)

            On known values, OR is Boolean disjunction.

            @[simp]

            On known values, NOT is Boolean negation.

            The test a WHERE or ON clause applies: only true keeps a row.

            Instances For
              @[simp]

              A conjunction holds exactly when both conjuncts hold.

              @[simp]

              A disjunction holds exactly when one disjunct holds.

              @[simp]

              NOT a is true exactly when a is false; so an unknown predicate and its negation both drop the row in a WHERE clause.

              @[simp]

              NOT a is false exactly when a is true.