U.S. Code 121
Up until now, I had never read a legal statute in my life and I wouldn't be able to tell you a single thing about tax law. Today, I just spent 5 hours learning everything about the exclusion of gain from sale of principal residence.
That's because I wanted to understand the default logic behind law and this statute shows it very clearly.
Note: left out paragraphs (b)(4) about surviving spouses and (b)(5) about non-qualified use of property
26 U.S.C. §121 - Exclusion of gain from sale of principal residence
(a) Exclusion. Gross income shall not include gain from the sale or exchange of property if, during the 5-year period ending on the date of the sale or exchange, such property has been owned and used by the taxpayer as the taxpayer's principal residence for periods aggregating 2 years or more.
(b) Limitations.
(1) In general. The amount of gain excluded from gross income under subsection (a) with respect to any sale or exchange shall not exceed $250,000.
(2) Special rules for joint returns. In the case of a husband and wife who make a joint return for the taxable year of the sale or exchange of the property—
(A) $500,000 limitation for certain joint returns. Paragraph (1) shall be applied by substituting "$500,000" for "$250,000" if— (i) either spouse meets the ownership requirements of subsection (a) with respect to such property; (ii) both spouses meet the use requirements of subsection (a) with respect to such property; and (iii) neither spouse is ineligible for the benefits of subsection (a) with respect to such property by reason of paragraph (3).
(B) Other joint returns. If such spouses do not meet the requirements of subparagraph (A), the limitation under paragraph (1) shall be the sum of the limitations under paragraph (1) to which each spouse would be entitled if such spouses had not been married. For purposes of the preceding sentence, each spouse shall be treated as owning the property during the period that either spouse owned the property.
(3) Application to only 1 sale or exchange every 2 years. Subsection (a) shall not apply to any sale or exchange by the taxpayer if, during the 2-year period ending on the date of such sale or exchange, there was any other sale or exchange by the taxpayer to which subsection (a) applied.
(c) Exclusion for taxpayers failing to meet certain requirements.
(1) In general. In the case of a sale or exchange to which this subsection applies, the ownership and use requirements of subsection (a), and subsection (b)(3), shall not apply; but the dollar limitation under paragraph (1) or (2) of subsection (b), whichever is applicable, shall be equal to—
(A) the amount which bears the same ratio to such limitation (determined without regard to this paragraph) as
(B)(i) the shorter of—
(I) the aggregate periods, during the 5-year period ending on the date of such sale or exchange, such property has been owned and used by the taxpayer as the taxpayer's principal residence; or
(II) the period after the date of the most recent prior sale or exchange by the taxpayer to which subsection (a) applied and before the date of such sale or exchange,
bears to
(ii) 2 years.
(2) Sales and exchanges to which subsection applies. This subsection shall apply to any sale or exchange if—
(A) subsection (a) would not (but for this subsection) apply to such sale or exchange by reason of—
(i) a failure to meet the ownership and use requirements of subsection (a), or
(ii) subsection (b)(3), and
(B) such sale or exchange is by reason of a change in place of employment, health, or, to the extent provided in regulations, unforeseen circumstances.
Law isn't naturally formalised in standard logic. It contains rules along with many exceptions to those rules.
For example, in the statute above we have "(a) gross income shall not include gain from the sale or exchange of property... unless (b)(3) there was another property sale within 2 years of the recent sale... unless (c) the sale was due to employment, health or unforeseen circumstances in which case, we calculate a prorated cap."
This makes law's logic non-monotonic (adding new information can remove earlier conclusions) while in standard logic, the set of conclusions can only get bigger as new premises are added. This is an example of default logic.
So, when we work with many legal statutes, there are many (possibly infinitely many) exceptions to rules that just aren't written down. This is when default logic must be used.
But since, for us, we're working with just one statute, we can actually stick to using classical logic in Lean.
To do this, we first define our types: person, property, date. This statute specifically aggregates the duration of time in which the seller has owned / lived in the house, so we also have to make this aggregate function explicit.
Extract in English:
"Gross income shall not include gain from the sale or exchange of property if, during the 5-year period ending on the date of the sale or exchange, such property has been owned and used by the taxpayer as the taxpayer's principal residence for periods aggregating 2 years or more."
Extract In Lean:
def ownershipDuration (p : Person) (property : Property) (d : Date) : Duration :=
aggregate (owns p property) (d - fiveYears) d
def useDuration (p : Person) (property : Property) (d : Date) : Duration :=
aggregate (usesAsResidence p property) (d - fiveYears) d
-- Taxpayer has owned the property for periods aggregating 2 years or more
def ownershipRequirement (p : Person) (property : Property) (d : Date) : Prop :=
twoYears ≤ ownershipDuration p property d
-- Taxpayer has used the property for periods aggregating 2 years or more
def useRequirement (p : Person) (property : Property) (d : Date) : Prop :=
twoYears ≤ useDuration p property d
-- Does the sale meet the requirement?
def meetsRequirements (p : Person) (property : Property) (d : Date) : Prop :=
ownershipRequirement p property d ∧ useRequirement p property d
Once we've formalised the domain (U.S.C IRC121), we can now verify any conclusions we make about how the sale of a person's property contributes to their gross income. Hooray.
Given a fixed set of facts about the person, the formalised statute becomes a closed, static program. Any legal argument (proof) made about U.S.C. IRC121 is also a closed, static program. The proof doesn't require any input from an external environment and the internal rules of the law never change.
But there's a caveat.
Of course, in reality, the law does change. We're assuming the proof we make is about a specific instance of the law at a certain point in time. We're also assuming that this proof doesn't at any point need a decision from an outside authority e.g. a tax court.
In practice, the above assumptions are often wrong. This is what makes formalising the law more nuanced.
For example: Section (2)(B) defines "unforseen circumstances" as a term that depends on the "extent provided in regulations". Already we can see there is a dependence on this thing called "regulations" that exists outside of the statute.
So, even here, in the most rule-based domain there is, proofs only hold given some assumptions about the external world. Formalising the domain of law is not just writing down its rules. The hard part is actually about deciding what to assume about everyone who interprets those rules and making those assumptions explicit.