nubpaws.dev
Back to Articles
Article

Chapter 5: The Math Behind It All

Formal definitions and proofs for propositional logic, DNF/CNF existence, and key logical equivalences.

February 28, 2026 NubPaws
Boolean Logic Math Proofs CNF DNF

Table of Contents

  1. Propositional Logic
  2. Valuations
  3. Disjunctive Normal Form
  4. Conjunctive Normal Form
  5. Implication Equivalence
  6. Biconditional Equivalence
  7. De Morgan’s Laws
  8. Why CNF and DNF Matter

This chapter stands alone as a mathematical reference. If you’re only interested in the code, chapters 1–4 cover the complete implementation. Here we formalize the ideas and prove that they work.

Propositional Logic

Propositional variables are symbols - p,q,r,p, q, r, \ldots - that can be either true (\top) or false (\bot).

A well-formed formula (wff) is built inductively:

  1. Every propositional variable is a wff.
  2. If φ\varphi is a wff, then ¬φ\lnot \varphi is a wff.
  3. If φ\varphi and ψ\psi are wffs, then (φψ)(\varphi \land \psi), (φψ)(\varphi \lor \psi), (φψ)(\varphi \to \psi), and (φψ)(\varphi \leftrightarrow \psi) are wffs.

We write Var(φ)\operatorname{Var}(\varphi) for the set of propositional variables appearing in φ\varphi.

Valuations

A valuation (or assignment) is a function v ⁣:Var(φ){,}v \colon \operatorname{Var}(\varphi) \to \{\top, \bot\}. Given a valuation, the truth value of a formula is determined recursively:

v(p)=v(p)(variable)v(¬φ)={if v(φ)=if v(φ)=v(φψ)=    v(φ)= and v(ψ)=v(φψ)=    v(φ)= or v(ψ)=v(φψ)=    v(φ)= or v(ψ)=v(φψ)=    v(φ)=v(ψ)\begin{aligned} v(p) &= v(p) && \text{(variable)} \\ v(\lnot \varphi) &= \begin{cases} \top & \text{if } v(\varphi) = \bot \\ \bot & \text{if } v(\varphi) = \top \end{cases} \\ v(\varphi \land \psi) &= \top \iff v(\varphi) = \top \text{ and } v(\psi) = \top \\ v(\varphi \lor \psi) &= \top \iff v(\varphi) = \top \text{ or } v(\psi) = \top \\ v(\varphi \to \psi) &= \top \iff v(\varphi) = \bot \text{ or } v(\psi) = \top \\ v(\varphi \leftrightarrow \psi) &= \top \iff v(\varphi) = v(\psi) \end{aligned}

A formula φ\varphi is a tautology if v(φ)=v(\varphi) = \top for every valuation vv. Two formulas φ\varphi and ψ\psi are logically equivalent, written φψ\varphi \equiv \psi, if v(φ)=v(ψ)v(\varphi) = v(\psi) for every valuation vv.

Disjunctive Normal Form

A literal is a variable pp or its negation ¬p\lnot p. A minterm over variables p1,,pnp_1, \ldots, p_n is a conjunction of nn literals, one per variable:

12n\ell_1 \land \ell_2 \land \cdots \land \ell_n

where each i\ell_i is either pip_i or ¬pi\lnot p_i.

A formula is in Disjunctive Normal Form (DNF) if it is a disjunction of minterms:

m1m2mkm_1 \lor m_2 \lor \cdots \lor m_k

Theorem. Every formula φ\varphi has an equivalent DNF.

Proof. Let Var(φ)={p1,,pn}\operatorname{Var}(\varphi) = \{p_1, \ldots, p_n\}. Consider all 2n2^n valuations. For each valuation vv where v(φ)=v(\varphi) = \top, define a minterm:

mv=1v2vnvm_v = \ell_1^v \land \ell_2^v \land \cdots \land \ell_n^v

where iv=pi\ell_i^v = p_i if v(pi)=v(p_i) = \top, and iv=¬pi\ell_i^v = \lnot p_i if v(pi)=v(p_i) = \bot.

Define φDNF=v:v(φ)=mv\varphi_{\text{DNF}} = \bigvee_{v : v(\varphi) = \top} m_v. If no valuation satisfies φ\varphi, set φDNF=\varphi_{\text{DNF}} = \bot (which is equivalent since φ\varphi is unsatisfiable).

We claim φDNFφ\varphi_{\text{DNF}} \equiv \varphi. Let ww be any valuation:

  • If w(φ)=w(\varphi) = \top: then mwm_w is one of the disjuncts, and w(mw)=w(m_w) = \top since each literal in mwm_w matches ww. So w(φDNF)=w(\varphi_{\text{DNF}}) = \top.

  • If w(φ)=w(\varphi) = \bot: then mwm_w is not a disjunct. For any other minterm mvm_v in φDNF\varphi_{\text{DNF}}, since vwv \neq w on at least one variable, at least one literal in mvm_v evaluates to \bot under ww, making w(mv)=w(m_v) = \bot. Since all disjuncts are \bot, w(φDNF)=w(\varphi_{\text{DNF}}) = \bot.

Therefore φDNFφ\varphi_{\text{DNF}} \equiv \varphi. \blacksquare

Connection to code:

This is exactly what our code does in Chapter 4: for each TRUE row in the truth table, we build a minterm from the variable values in that row, then OR all the minterms together.

Conjunctive Normal Form

A maxterm over variables p1,,pnp_1, \ldots, p_n is a disjunction of nn literals:

12n\ell_1 \lor \ell_2 \lor \cdots \lor \ell_n

A formula is in Conjunctive Normal Form (CNF) if it is a conjunction of maxterms:

M1M2MkM_1 \land M_2 \land \cdots \land M_k

Theorem. Every formula φ\varphi has an equivalent CNF.

Proof. For each valuation vv where v(φ)=v(\varphi) = \bot, define a maxterm:

Mv=1v2vnvM_v = \ell_1^v \lor \ell_2^v \lor \cdots \lor \ell_n^v

where iv=¬pi\ell_i^v = \lnot p_i if v(pi)=v(p_i) = \top, and iv=pi\ell_i^v = p_i if v(pi)=v(p_i) = \bot.

Note the flipped literal construction compared to DNF.

Define φCNF=v:v(φ)=Mv\varphi_{\text{CNF}} = \bigwedge_{v : v(\varphi) = \bot} M_v. If no valuation falsifies φ\varphi, set φCNF=\varphi_{\text{CNF}} = \top.

We claim φCNFφ\varphi_{\text{CNF}} \equiv \varphi. Let ww be any valuation:

  • If w(φ)=w(\varphi) = \top: for every maxterm MvM_v in φCNF\varphi_{\text{CNF}}, we have vwv \neq w on at least one variable pip_i. If v(pi)=v(p_i) = \top then iv=¬pi\ell_i^v = \lnot p_i and w(pi)=w(p_i) = \bot… wait, that doesn’t help directly. Instead: since v(pi)w(pi)v(p_i) \neq w(p_i) for some ii, the literal iv\ell_i^v evaluates to \top under ww. To see why: if v(pi)=v(p_i) = \top, then iv=¬pi\ell_i^v = \lnot p_i and w(pi)=w(p_i) = \bot (since they differ), so w(¬pi)=w(\lnot p_i) = \top. If v(pi)=v(p_i) = \bot, then iv=pi\ell_i^v = p_i and w(pi)=w(p_i) = \top, so w(pi)=w(p_i) = \top. Either way, at least one literal in MvM_v is \top, so w(Mv)=w(M_v) = \top. Since all maxterms are \top, w(φCNF)=w(\varphi_{\text{CNF}}) = \top.

  • If w(φ)=w(\varphi) = \bot: then MwM_w is one of the conjuncts. Every literal in MwM_w evaluates to \bot under ww: if w(pi)=w(p_i) = \top, then iw=¬pi\ell_i^w = \lnot p_i and w(¬pi)=w(\lnot p_i) = \bot; if w(pi)=w(p_i) = \bot, then iw=pi\ell_i^w = p_i and w(pi)=w(p_i) = \bot. So w(Mw)=w(M_w) = \bot, which makes w(φCNF)=w(\varphi_{\text{CNF}}) = \bot.

Therefore φCNFφ\varphi_{\text{CNF}} \equiv \varphi. \blacksquare

Connection to code:

This matches our code: for each FALSE row, we build a maxterm with flipped literals (negate the trues, keep the falses), then AND all maxterms together.

Implication Equivalence

Our evaluator implements implication as !left || right. Here’s the formal justification.

Theorem. AB¬ABA \to B \equiv \lnot A \lor B.

Proof. We verify by truth table:

AABBABA \to B¬AB\lnot A \lor B
\top\top\top\top
\top\bot\bot\bot
\bot\top\top\top
\bot\bot\top\top

The columns match on all rows, so AB¬ABA \to B \equiv \lnot A \lor B. \blacksquare

Biconditional Equivalence

Theorem. AB(AB)(BA)A \leftrightarrow B \equiv (A \to B) \land (B \to A).

Proof. Again by truth table:

AABBABA \leftrightarrow B(AB)(BA)(A \to B) \land (B \to A)
\top\top\top\top
\top\bot\bot\bot
\bot\top\bot\bot
\bot\bot\top\top

The columns match. \blacksquare

Using the implication equivalence, we can also write:

AB(¬AB)(¬BA)A \leftrightarrow B \equiv (\lnot A \lor B) \land (\lnot B \lor A)

Our code uses === on booleans for equiv, which is equivalent to checking both implications simultaneously.

De Morgan’s Laws

Theorem (De Morgan). For any formulas AA and BB:

¬(AB)¬A¬B\lnot(A \land B) \equiv \lnot A \lor \lnot B ¬(AB)¬A¬B\lnot(A \lor B) \equiv \lnot A \land \lnot B

Proof. Truth tables for the first law:

AABBABA \land B¬(AB)\lnot(A \land B)¬A¬B\lnot A \lor \lnot B
\top\top\top\bot\bot
\top\bot\bot\top\top
\bot\top\bot\top\top
\bot\bot\bot\top\top

The columns ¬(AB)\lnot(A \land B) and ¬A¬B\lnot A \lor \lnot B match.

The second law follows by similar verification (or by applying the first law to ¬A\lnot A and ¬B\lnot B and simplifying). \blacksquare

De Morgan’s laws are fundamental in logic - they let us push negation through conjunctions and disjunctions. While our calculator doesn’t need them for the basic algorithm (we build normal forms directly from the truth table), they are essential if you ever want to simplify or manipulate CNF/DNF expressions.

Why CNF and DNF Matter

Normal forms aren’t just a theoretical curiosity. They have practical applications across computer science:

SAT solvers. Most modern SAT solvers (used in hardware verification, planning, cryptanalysis, and more) expect their input in CNF. The DPLL and CDCL algorithms that power these solvers operate directly on conjunctions of clauses.

Circuit design. In digital electronics, DNF corresponds directly to a sum-of-products implementation (OR gates combining AND gates), while CNF corresponds to a product-of-sums (AND gates combining OR gates). Converting to normal form is a standard step in circuit synthesis.

Logic simplification. Normal forms provide a starting point for minimization algorithms like Quine–McCluskey or Espresso, which reduce the number of terms while preserving logical equivalence.

Databases. Query optimization sometimes involves converting conditions to CNF for efficient index utilization.

The truth table method we implemented gives us a canonical normal form (one unique representation for each truth function), which is correct but can be exponentially large. In practice, specialized algorithms exist for more compact representations. But the truth table approach is the clearest way to understand what CNF and DNF are, which is why we started here.


Previous: Chapter 4: Truth Tables and Normal Forms

Series: Back to Truth Table, CNF, and DNF Generator