{
  "status": "proved",
  "decl": "LambekMoser.inverse_iff_partition",
  "lean_source": "import Mathlib.Data.Nat.Count\nimport Mathlib.Data.Nat.Nth\nimport Mathlib.Tactic\n\n/-!\n# Lambek–Moser theorem (Wikidata Q6481192)\n\nFor monotone `f, g : ℕ → ℕ`, `f i ≤ j ↔ i < g j` for all `i, j` if and only if the\nranges of `i ↦ i + f i` and `j ↦ j + g j` are disjoint and have union `ℕ`.\nMoreover, every pair of infinite complementary subsets of `ℕ` is the pair of ranges of\nexactly one such pair of monotone functions.\n\nThe traditional one-based formulation is `f(m) < n ↔ g(n) ≥ m`, with the two shifted\nranges partitioning the positive integers. The change of indexing gives the zero-based\nform above.\n-/\n\nnamespace LambekMoser\n\nopen Nat\n\ndef Partition (s t : Set ℕ) : Prop := Disjoint s t ∧ s ∪ t = Set.univ\n\ndef Inverse (f g : ℕ → ℕ) : Prop := ∀ i j, f i ≤ j ↔ i < g j\n\ndef RangePartition (f g : ℕ → ℕ) : Prop :=\n  Partition (Set.range fun i => i + f i) (Set.range fun j => j + g j)\n\ndef PairRanges (s t : Set ℕ) (f g : ℕ → ℕ) : Prop :=\n  Monotone f ∧ Monotone g ∧ Set.range (fun i => i + f i) = s ∧\n    Set.range (fun j => j + g j) = t\n\ntheorem shifted_strictMono (f : ℕ → ℕ) (hf : Monotone f) :\n    StrictMono (fun i => i + f i) := by\n  intro i j hij\n  have hfj : f i ≤ f j := hf (Nat.le_of_lt hij)\n  change i + f i < j + f j\n  exact (Nat.add_lt_add_right hij (f i)).trans_le (Nat.add_le_add_left hfj j)\n\ntheorem shifted_ge_index (f : ℕ → ℕ) (i : ℕ) : i ≤ i + f i := by omega\n\ntheorem inverse_cutoff (f g : ℕ → ℕ) (h : Inverse f g) (i j : ℕ) :\n    i + f i ≤ i + j ↔ i + j < j + g j := by\n  constructor\n  · intro hi\n    have hf : f i ≤ j := by omega\n    have hg : i < g j := (h i j).mp hf\n    omega\n  · intro hb\n    have hg : i < g j := by omega\n    have hf : f i ≤ j := (h i j).mpr hg\n    omega\n\ntheorem inverse_ranges_disjoint (f g : ℕ → ℕ) (h : Inverse f g) :\n    Disjoint (Set.range fun i => i + f i) (Set.range fun j => j + g j) := by\n  rw [Set.disjoint_left]\n  rintro n ⟨i, hi⟩ ⟨j, hj⟩\n  change i + f i = n at hi\n  change j + g j = n at hj\n  have heq : i + f i = j + g j := hi.trans hj.symm\n  by_cases hfi : f i ≤ j\n  · have hig : i < g j := (h i j).mp hfi\n    omega\n  · have hgi : g j ≤ i := Nat.not_lt.mp (fun hh => hfi ((h i j).mpr hh))\n    omega\n\n/-- Counting a strictly increasing enumeration below a cutoff is the same as counting its range. -/\ntheorem count_range_eq_count_index (a : ℕ → ℕ) (ha : StrictMono a)\n    (hlower : ∀ i, i ≤ a i) (n : ℕ)\n    [DecidablePred (fun x => x ∈ Set.range a)] :\n    Nat.count (fun x => x ∈ Set.range a) n = Nat.count (fun i => a i < n) n := by\n  classical\n  rw [Nat.count_eq_card_filter_range, Nat.count_eq_card_filter_range]\n  symm\n  apply Finset.card_bij (fun i _ => a i)\n  · intro i hi\n    simp only [Finset.mem_filter, Finset.mem_range] at hi ⊢\n    exact ⟨hi.2, ⟨i, rfl⟩⟩\n  · intro i hi j hj heq\n    exact ha.injective heq\n  · intro y hy\n    simp only [Finset.mem_filter, Finset.mem_range, Set.mem_range] at hy\n    obtain ⟨x, rfl⟩ := hy.2\n    refine ⟨x, ?_, rfl⟩\n    simp only [Finset.mem_filter, Finset.mem_range]\n    exact ⟨lt_of_le_of_lt (hlower x) hy.1, hy.1⟩\n\ntheorem count_reverse (p : ℕ → Prop) [DecidablePred p] (n : ℕ) :\n    Nat.count (fun i => p (n - 1 - i)) n = Nat.count p n := by\n  classical\n  rw [Nat.count_eq_card_filter_range, Nat.count_eq_card_filter_range]\n  apply Finset.card_bij (fun i _ => n - 1 - i)\n  · intro i hi\n    simp only [Finset.mem_filter, Finset.mem_range] at hi ⊢\n    rcases hi with ⟨hi, hpi⟩\n    have hpos : 0 < n := by omega\n    have hbound : n - 1 - i ≤ n - 1 := Nat.sub_le _ _\n    exact ⟨by omega, hpi⟩\n  · intro i hi j hj heq\n    simp only [Finset.mem_filter, Finset.mem_range] at hi hj\n    have hi' : i ≤ n - 1 := by omega\n    have hj' : j ≤ n - 1 := by omega\n    have h1 : n - 1 - i + i = n - 1 := Nat.sub_add_cancel hi'\n    have h2 : n - 1 - j + j = n - 1 := Nat.sub_add_cancel hj'\n    omega\n  · intro y hy\n    simp only [Finset.mem_filter, Finset.mem_range] at hy ⊢\n    rcases hy with ⟨hylt, hpy⟩\n    have hyle : y ≤ n - 1 := by omega\n    have hxle : n - 1 - y ≤ n - 1 := Nat.sub_le _ _\n    have hxlt : n - 1 - y < n := by omega\n    have hinv : n - 1 - (n - 1 - y) = y := by omega\n    refine ⟨n - 1 - y, ⟨hxlt, ?_⟩, ?_⟩\n    · simpa [hinv] using hpy\n    · exact hinv\n\ntheorem nth_eq_strict_enumeration (a : ℕ → ℕ) (ha : StrictMono a)\n    (hlower : ∀ i, i ≤ a i) (i : ℕ) :\n    Nat.nth (fun x => x ∈ Set.range a) i = a i := by\n  classical\n  have hmem : a i ∈ Set.range a := ⟨i, rfl⟩\n  have hidx : Nat.count (fun k => a k < a i) (a i) = i := by\n    rw [Nat.count_eq_card_filter_range]\n    have hset : {k ∈ Finset.range (a i) | a k < a i} = Finset.range i := by\n      ext k\n      simp only [Finset.mem_filter, Finset.mem_range]\n      constructor\n      · rintro ⟨_, hki⟩\n        exact (ha.lt_iff_lt).mp hki\n      · intro hki\n        have hli := hlower i\n        exact ⟨by omega, (ha.lt_iff_lt).2 hki⟩\n    rw [hset, Finset.card_range]\n  have hcount := count_range_eq_count_index a ha hlower (a i)\n  rw [hidx] at hcount\n  calc\n    Nat.nth (fun x => x ∈ Set.range a) i =\n        Nat.nth (fun x => x ∈ Set.range a)\n          (Nat.count (fun x => x ∈ Set.range a) (a i)) := by rw [hcount]\n    _ = a i := Nat.nth_count hmem\ntheorem count_compl_of_partition {p q : ℕ → Prop} [DecidablePred p] [DecidablePred q]\n    (h : ∀ n, q n ↔ ¬ p n) :\n    ∀ n, Nat.count p n + Nat.count q n = n := by\n  intro n\n  induction n with\n  | zero => simp\n  | succ n ih =>\n      rw [Nat.count_succ, Nat.count_succ]\n      have hn : (if p n then 1 else 0) + (if q n then 1 else 0) = 1 := by\n        by_cases hp : p n\n        · have hq : ¬ q n := fun hq => (h n).mp hq hp\n          simp [hp, hq]\n        · have hq : q n := (h n).2 hp\n          simp [hp, hq]\n      omega\n\ntheorem count_compl_below {p q : ℕ → Prop} [DecidablePred p] [DecidablePred q]\n    {n : ℕ} (h : ∀ i < n, q i ↔ ¬ p i) :\n    Nat.count p n + Nat.count q n = n := by\n  have aux : ∀ k ≤ n, Nat.count p k + Nat.count q k = k := by\n    intro k\n    induction k with\n    | zero => intro; simp\n    | succ k ih =>\n        intro hk\n        have ih' := ih (by omega)\n        rw [Nat.count_succ, Nat.count_succ]\n        have hkn := h k (by omega)\n        by_cases hp : p k\n        · have hq : ¬ q k := fun hq => (hkn.mp hq) hp\n          simp [hp, hq]\n          omega\n        · have hq : q k := (hkn.mpr hp)\n          simp [hp, hq]\n          omega\n  exact aux n le_rfl\n\ntheorem inverse_range_count (f g : ℕ → ℕ) (hf : Monotone f) (hg : Monotone g)\n    (hinv : Inverse f g) (n : ℕ)\n    [DecidablePred (fun x => x ∈ Set.range fun i => i + f i)]\n    [DecidablePred (fun x => x ∈ Set.range fun j => j + g j)] :\n    Nat.count (fun x => x ∈ Set.range fun i => i + f i) n +\n      Nat.count (fun x => x ∈ Set.range fun j => j + g j) n = n := by\n  classical\n  let a : ℕ → ℕ := fun i => i + f i\n  let b : ℕ → ℕ := fun j => j + g j\n  have ha : StrictMono a := by\n    exact shifted_strictMono f hf\n  have hb : StrictMono b := by\n    exact shifted_strictMono g hg\n  have halower : ∀ i, i ≤ a i := by intro i; exact shifted_ge_index f i\n  have hblower : ∀ j, j ≤ b j := by intro j; exact shifted_ge_index g j\n  have hcomp : ∀ i < n, (b (n - 1 - i) < n) ↔ ¬ a i < n := by\n    intro i hin\n    have hsum : i + (n - 1 - i) = n - 1 := by omega\n    have hcut := inverse_cutoff f g hinv i (n - 1 - i)\n    change a i ≤ i + (n - 1 - i) ↔ i + (n - 1 - i) < b (n - 1 - i) at hcut\n    rw [hsum] at hcut\n    omega\n  have hc := count_compl_below (p := fun i => a i < n)\n    (q := fun i => b (n - 1 - i) < n) hcomp\n  rw [count_reverse (fun i => b i < n) n] at hc\n  have hca := count_range_eq_count_index a ha halower n\n  have hcb := count_range_eq_count_index b hb hblower n\n  calc\n    Nat.count (fun x => x ∈ Set.range fun i => i + f i) n +\n        Nat.count (fun x => x ∈ Set.range fun j => j + g j) n =\n        Nat.count (fun x => x ∈ Set.range a) n + Nat.count (fun x => x ∈ Set.range b) n := by\n          rfl\n    _ = Nat.count (fun i => a i < n) n + Nat.count (fun j => b j < n) n := by\n          rw [hca, hcb]\n    _ = n := hc\n\ntheorem inverse_implies_partition (f g : ℕ → ℕ) (hf : Monotone f) (hg : Monotone g)\n    (hinv : Inverse f g) : RangePartition f g := by\n  classical\n  constructor\n  · exact inverse_ranges_disjoint f g hinv\n  · apply Set.eq_univ_iff_forall.mpr\n    intro x\n    have hcount := inverse_range_count f g hf hg hinv\n    have h0 := hcount x\n    have h1 := hcount (x + 1)\n    rw [Nat.count_succ, Nat.count_succ] at h1\n    have hone :\n        (if x ∈ Set.range (fun i => i + f i) then 1 else 0) +\n          (if x ∈ Set.range (fun j => j + g j) then 1 else 0) = 1 := by\n      omega\n    have hmem : x ∈ Set.range (fun i => i + f i) ∨\n        x ∈ Set.range (fun j => j + g j) := by\n      by_cases hp : x ∈ Set.range (fun i => i + f i)\n      · exact Or.inl hp\n      · right\n        by_contra hq\n        simp [hp, hq] at hone\n    simpa [Partition, Set.mem_union] using hmem\n\ntheorem partition_implies_inverse (f g : ℕ → ℕ) (hf : Monotone f) (hg : Monotone g)\n    (hpart : RangePartition f g) : Inverse f g := by\n  classical\n  let a : ℕ → ℕ := fun i => i + f i\n  let b : ℕ → ℕ := fun j => j + g j\n  let p : ℕ → Prop := fun x => x ∈ Set.range a\n  let q : ℕ → Prop := fun x => x ∈ Set.range b\n  have ha : StrictMono a := shifted_strictMono f hf\n  have hb : StrictMono b := shifted_strictMono g hg\n  have halower : ∀ i, i ≤ a i := by intro i; exact shifted_ge_index f i\n  have hblower : ∀ j, j ≤ b j := by intro j; exact shifted_ge_index g j\n  have hpinf : (Set.ofPred p).Infinite := Set.infinite_range_of_injective ha.injective\n  have hqinf : (Set.ofPred q).Infinite := Set.infinite_range_of_injective hb.injective\n  have hcomp : ∀ x, q x ↔ ¬ p x := by\n    intro x\n    constructor\n    · intro hxq hxp\n      have hdis : Disjoint (Set.range a) (Set.range b) := by\n        simpa [RangePartition, Partition, a, b] using hpart.1\n      exact (Set.disjoint_left.mp hdis) hxp hxq\n    · intro hnp\n      have hunion : Set.range a ∪ Set.range b = Set.univ := by\n        simpa [RangePartition, Partition, a, b] using hpart.2\n      have hcov : x ∈ Set.range a ∪ Set.range b := by rw [hunion]; exact Set.mem_univ x\n      rcases hcov with hxa | hxb\n      · exact (hnp hxa).elim\n      · exact hxb\n  have hcounts : ∀ n, Nat.count p n + Nat.count q n = n :=\n    count_compl_of_partition hcomp\n  intro i j\n  let N := i + j\n  have hna : Nat.nth p i = a i := nth_eq_strict_enumeration a ha halower i\n  have hnb : Nat.nth q j = b j := nth_eq_strict_enumeration b hb hblower j\n  have hleft : f i ≤ j ↔ i < Nat.count p (N + 1) := by\n    have hnth := Nat.lt_nth_iff_count_lt hpinf (a := i) (b := N + 1)\n    rw [hna] at hnth\n    change f i ≤ j ↔ i < Nat.count p (N + 1)\n    rw [hnth]\n    dsimp [N, a]\n    omega\n  have hright : i < g j ↔ Nat.count q (N + 1) ≤ j := by\n    have hnth := Nat.count_le_iff_le_nth hqinf (a := N + 1) (b := j)\n    rw [hnb] at hnth\n    change i < g j ↔ Nat.count q (N + 1) ≤ j\n    rw [hnth]\n    dsimp [N, b]\n    omega\n  have hsum := hcounts (N + 1)\n  dsimp [N] at hsum hleft hright ⊢\n  rw [hleft, hright]\n  omega\n\ntheorem inverse_iff_partition (f g : ℕ → ℕ) (hf : Monotone f) (hg : Monotone g) :\n    Inverse f g ↔ RangePartition f g := by\n  constructor\n  · exact inverse_implies_partition f g hf hg\n  · exact partition_implies_inverse f g hf hg\n\ntheorem existsUnique_monotone_pair {s t : Set ℕ} (hs : s.Infinite) (ht : t.Infinite)\n    (hpart : Partition s t) :\n    ∃! fg : (ℕ → ℕ) × (ℕ → ℕ), PairRanges s t fg.1 fg.2 := by\n  classical\n  let p : ℕ → Prop := fun x => x ∈ s\n  let q : ℕ → Prop := fun x => x ∈ t\n  have hpinf : (Set.ofPred p).Infinite := by simpa [p] using hs\n  have hqinf : (Set.ofPred q).Infinite := by simpa [q] using ht\n  have hcomp : ∀ x, q x ↔ ¬ p x := by\n    intro x\n    constructor\n    · intro hq hp\n      exact (Set.disjoint_left.mp hpart.1) hp hq\n    · intro hp\n      have hx : x ∈ s ∪ t := by rw [hpart.2]; exact Set.mem_univ x\n      rcases hx with hsx | htx\n      · exact (hp hsx).elim\n      · exact htx\n  have hcounts : ∀ n, Nat.count p n + Nat.count q n = n :=\n    count_compl_of_partition hcomp\n  let f : ℕ → ℕ := fun i => Nat.count q (Nat.nth p i)\n  let g : ℕ → ℕ := fun j => Nat.count p (Nat.nth q j)\n  have hshiftf : ∀ i, i + f i = Nat.nth p i := by\n    intro i\n    have hcountp := Nat.count_nth_of_infinite hpinf i\n    have hc := hcounts (Nat.nth p i)\n    dsimp [f]\n    omega\n  have hshiftg : ∀ j, j + g j = Nat.nth q j := by\n    intro j\n    have hcountq := Nat.count_nth_of_infinite hqinf j\n    have hc := hcounts (Nat.nth q j)\n    dsimp [g]\n    omega\n  have hf : Monotone f := by\n    dsimp [f]\n    exact (Nat.count_monotone q).comp (Nat.nth_monotone hpinf)\n  have hg : Monotone g := by\n    dsimp [g]\n    exact (Nat.count_monotone p).comp (Nat.nth_monotone hqinf)\n  have hrangef : Set.range (fun i => i + f i) = s := by\n    rw [show (fun i => i + f i) = Nat.nth p from funext hshiftf]\n    rw [Nat.range_nth_of_infinite hpinf]\n    ext x\n    simp [p]\n  have hrangeg : Set.range (fun j => j + g j) = t := by\n    rw [show (fun j => j + g j) = Nat.nth q from funext hshiftg]\n    rw [Nat.range_nth_of_infinite hqinf]\n    ext x\n    simp [q]\n  refine ⟨(f, g), ⟨hf, hg, hrangef, hrangeg⟩, ?_⟩\n  intro fg hfg\n  rcases hfg with ⟨hmonoF, hmonoG, hrangeF, hrangeG⟩\n  apply Prod.ext\n  · funext i\n    have ha : StrictMono (fun k => k + fg.1 k) := shifted_strictMono fg.1 hmonoF\n    have hlower : ∀ k, k ≤ k + fg.1 k := by intro k; omega\n    have hpred : (fun x => x ∈ Set.range (fun k => k + fg.1 k)) = p := by\n      funext x\n      apply propext\n      change x ∈ Set.range (fun k => k + fg.1 k) ↔ x ∈ s\n      rw [hrangeF]\n    have henum := nth_eq_strict_enumeration (fun k => k + fg.1 k) ha hlower i\n    rw [hpred] at henum\n    have hsame := hshiftf i\n    dsimp [f] at hsame\n    change Nat.nth p i = i + fg.1 i at henum\n    dsimp [f]\n    omega\n  · funext j\n    have hb : StrictMono (fun k => k + fg.2 k) := shifted_strictMono fg.2 hmonoG\n    have hlower : ∀ k, k ≤ k + fg.2 k := by intro k; omega\n    have hpred : (fun x => x ∈ Set.range (fun k => k + fg.2 k)) = q := by\n      funext x\n      apply propext\n      change x ∈ Set.range (fun k => k + fg.2 k) ↔ x ∈ t\n      rw [hrangeG]\n    have henum := nth_eq_strict_enumeration (fun k => k + fg.2 k) hb hlower j\n    rw [hpred] at henum\n    have hsame := hshiftg j\n    dsimp [g] at hsame\n    change Nat.nth q j = j + fg.2 j at henum\n    dsimp [g]\n    omega\n\n#print axioms inverse_ranges_disjoint\n#print axioms inverse_iff_partition\n#print axioms existsUnique_monotone_pair\n\nend LambekMoser\n",
  "build_output": "Command: `lean -DautoImplicit=false test/scratch/LambekMoser.lean` with the supplied Lean 4.36.0 executable and prebuilt Mathlib bundle. Exit status: 0. Compiler output:\n'LambekMoser.inverse_ranges_disjoint' depends on axioms: [propext, Classical.choice, Quot.sound]\n'LambekMoser.inverse_iff_partition' depends on axioms: [propext, Classical.choice, Quot.sound]\n'LambekMoser.existsUnique_monotone_pair' depends on axioms: [propext, Classical.choice, Quot.sound]",
  "explanation": "For a strictly increasing sequence a with a(i) ≥ i, the number of its range elements below n equals the number of indices i<n with a(i)<n. The inverse relation pairs the cutoff tests for the two shifted sequences by reversing the index j=n−1−i. Complementary counts below every cutoff then give disjointness and coverage. Conversely, for complementary ranges, Nat.count and Nat.nth give the inverse inequalities. For arbitrary infinite complementary sets, enumerate each with Nat.nth and define each function by counting how many elements of the other set precede that element. The count identities prove the shifted ranges equal the original sets; strict enumeration by Nat.nth proves uniqueness.",
  "fidelity": {
    "main_statement": "The declaration assumes Monotone f and Monotone g for f,g : ℕ → ℕ and proves the exact iff Inverse f g ↔ RangePartition f g. Inverse is ∀ i j, f i ≤ j ↔ i < g j. RangePartition means the ranges of i ↦ i+f i and j ↦ j+g j are disjoint and their union is Set.univ (all ℕ).",
    "companion": "existsUnique_monotone_pair assumes s and t are both infinite and form a disjoint union equal to ℕ. It proves there exists exactly one ordered pair of monotone functions whose respective shifted ranges are s and t.",
    "indexing": "The module docstring relates the zero-based convention to the one-based statement f(m) < n ↔ g(n) ≥ m and the positive integers. The formal declarations use the vetted zero-based convention.",
    "proof_route": "The proof uses Nat.count identities to count elements below cutoffs and Nat.nth to enumerate arbitrary infinite sets. No sorry, admit, native_decide, decide +native, new axiom, unsafe, or implemented_by is used.",
    "axioms": "The main declaration and companion each print only propext, Classical.choice, and Quot.sound."
  },
  "prior_work": {
    "finding": "The current 1000-missing page lists Q6481192 as Lambek–Moser. Mathlib’s docs/1000.yaml in the pinned bundle also retains the missing entry, and a source search of the pinned Mathlib files found no Lambek–Moser formalization. GitHub’s mathlib4 PR search API query for Lambek Moser returned total_count 0. The requested rjwalters/lean-genius tree search had no Lambek/Moser path; its matching Moser path was the unrelated Proofs/MoserTardos.lean. Exact-name web searches and Lean Zulip searches returned no relevant formalization. These searches cannot establish global absence.",
    "unverified": "UNVERIFIED: a comprehensive search of all GitHub repositories and the complete Lean Zulip archive was not possible; the zero-match search queries do not prove no prior proof exists."
  },
  "database_entry": {
    "wikidata_id": "Q6481192",
    "title": "Lambek–Moser theorem",
    "decl": "LambekMoser.inverse_iff_partition",
    "file": "LambekMoser.lean"
  },
  "stuck": null,
  "sources": [
    {
      "url": "https://leanprover-community.github.io/1000-missing.html",
      "quote": "Q6481192: Lambek–Moser theorem"
    },
    {
      "url": "https://www.cs.utexas.edu/~EWD/transcriptions/EWD07xx/EWD753.html",
      "quote": "The second result attributed to Lambek and Moser is that `D` `f` and `D` `g` form a partitioning of the natural numbers."
    },
    {
      "url": "https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/Nat/Count.lean",
      "quote": "Count the number of naturals `k < n` satisfying `p k`."
    },
    {
      "url": "https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/Nat/Nth.lean",
      "quote": "When `s` is an infinite set, `nth` agrees with `Nat.Subtype.orderIsoOfNat`."
    },
    {
      "url": "https://api.github.com/search/issues?q=repo%3Aleanprover-community%2Fmathlib4+Lambek+Moser+type%3Apr",
      "quote": "\"total_count\": 0,"
    },
    {
      "url": "https://api.github.com/repos/rjwalters/lean-genius/git/trees/main?recursive=1",
      "quote": "\"path\": \"proofs/Proofs/MoserTardos.lean\","
    }
  ]
}
