{
 "verdict": "pass",
 "decl": "LambekMoser.inverse_iff_partition",
 "author_status": "proved (as reported by author in t04-lambek-moser result; confirmed by this rebuild)",
 "lean_source": "/-\nCopyright (c) 2026 Contributors. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\n-/\nimport Mathlib\n\n/-!\n# The Lambek–Moser theorem\n\nWikidata: Q6481192.\n\nInformal statement (Lambek–Moser, 1954). Let `f` and `g` be nondecreasing functions\nof the positive integers that are *inverse* to each other in the sense\n`f m < n ↔ g n ≥ m` (for `m, n ≥ 1`). Then the sequences `F m = f m + m` and `G n = g n + n`\npartition the positive integers: every positive integer occurs in exactly one of them.\nConversely every partition of the positive integers into two infinite sets arises this way.\n\nWe use the zero-based form on `ℕ`: for `f g : ℕ → ℕ` monotone,\n`(∀ i j, f i ≤ j ↔ i < g j)` iff the ranges of `i ↦ i + f i` and `j ↦ j + g j` are disjoint\nwith union `ℕ`.  (Passing from the one-based form: put `f' i = f (i+1)`, `g' j = g (j+1)`, shifting\n`F, G` by one; `f m < n ↔ m ≤ g n` becomes `f' i < j + 1 ↔ i < g' j`, i.e. `f' i ≤ j ↔ i < g' j`.)\n\n## Main results\n\n* `LambekMoser.inverse_iff_partition`: the theorem.\n* `LambekMoser.existsUnique_of_partition`: the companion; every partition of `ℕ` into two\n  infinite sets `S`, `T` is the pair of ranges for exactly one pair of monotone `f`, `g`.\n-/\n\nopen Set\n\nnamespace LambekMoser\n\n/-- Counting lemma: if `F` and `G` are strictly monotone with disjoint ranges covering `ℕ`, then\n`G j < F i` iff `j < F i - i`. -/\nlemma lt_iff_lt_sub {F G : ℕ → ℕ} (hF : StrictMono F) (hG : StrictMono G)\n    (hd : Disjoint (range F) (range G)) (hu : range F ∪ range G = univ) (i j : ℕ) :\n    G j < F i ↔ j < F i - i := by\n  classical\n  set S : Finset ℕ := (Finset.range (F i)).filter (fun k => G k < F i) with hS\n  have hsplit : Finset.range (F i) = (Finset.range i).image F ∪ S.image G := by\n    ext m\n    simp only [Finset.mem_range, Finset.mem_union, Finset.mem_image, hS, Finset.mem_filter]\n    constructor\n    · intro hm\n      have : m ∈ range F ∪ range G := hu ▸ mem_univ m\n      rcases this with ⟨k, rfl⟩ | ⟨k, rfl⟩\n      · exact Or.inl ⟨k, hF.lt_iff_lt.1 hm, rfl⟩\n      · exact Or.inr ⟨k, ⟨lt_of_le_of_lt (hG.id_le k) hm, hm⟩, rfl⟩\n    · rintro (⟨k, hk, rfl⟩ | ⟨k, ⟨-, hk⟩, rfl⟩)\n      · exact hF hk\n      · exact hk\n  have hdisj : Disjoint ((Finset.range i).image F) (S.image G) := by\n    rw [Finset.disjoint_left]\n    intro m hm hm'\n    simp only [Finset.mem_image] at hm hm'\n    obtain ⟨a, -, rfl⟩ := hm\n    obtain ⟨b, -, hb⟩ := hm'\n    exact Set.disjoint_left.1 hd ⟨a, rfl⟩ ⟨b, hb⟩\n  have hcard : F i = i + S.card := by\n    have := congrArg Finset.card hsplit\n    rw [Finset.card_union_of_disjoint hdisj, Finset.card_image_of_injective _ hF.injective,\n      Finset.card_image_of_injective _ hG.injective] at this\n    simpa using this\n  have hS' : S.card = F i - i := by omega\n  rw [← hS']\n  constructor\n  · intro h\n    have hsub : Finset.range (j + 1) ⊆ S := by\n      intro k hk\n      rw [Finset.mem_range] at hk\n      simp only [hS, Finset.mem_filter, Finset.mem_range]\n      have hkj : G k ≤ G j := hG.monotone (by omega)\n      exact ⟨lt_of_le_of_lt (hG.id_le k) (lt_of_le_of_lt hkj h), lt_of_le_of_lt hkj h⟩\n    simpa using Finset.card_le_card hsub\n  · intro h\n    by_contra hc\n    push Not at hc\n    have hsub : S ⊆ Finset.range j := by\n      intro k hk\n      simp only [hS, Finset.mem_filter, Finset.mem_range] at hk\n      rw [Finset.mem_range]\n      exact hG.lt_iff_lt.1 (lt_of_lt_of_le hk.2 hc)\n    simpa using lt_of_lt_of_le h (Finset.card_le_card hsub)\n\n/-- **Lambek–Moser theorem** (Wikidata Q6481192), zero-based form.\nFor monotone `f g : ℕ → ℕ`, the relation `f i ≤ j ↔ i < g j` (the functions are \"inverse\" to each\nother) holds for all `i j` if and only if the ranges of `i ↦ i + f i` and `j ↦ j + g j` are\ndisjoint and their union is `ℕ`, i.e. they partition `ℕ`. -/\ntheorem inverse_iff_partition {f g : ℕ → ℕ} (hf : Monotone f) (hg : Monotone g) :\n    (∀ i j, f i ≤ j ↔ i < g j) ↔\n      (Disjoint (range fun i => i + f i) (range fun j => j + g j) ∧\n        (range fun i => i + f i) ∪ (range fun j => j + g j) = univ) := by\n  have hF : StrictMono fun i => i + f i := fun a b h => by\n    have := hf h.le; simp only; omega\n  have hG : StrictMono fun j => j + g j := fun a b h => by\n    have := hg h.le; simp only; omega\n  constructor\n  · intro h\n    refine ⟨?_, ?_⟩\n    · rw [Set.disjoint_left]\n      rintro _ ⟨i, rfl⟩ ⟨j, hj⟩\n      by_cases hij : f i ≤ j\n      · have := (h i j).1 hij; simp only at hj; omega\n      · have h2 : ¬ i < g j := fun h' => hij ((h i j).2 h')\n        simp only at hj; omega\n    · refine eq_univ_of_forall fun n => ?_\n      have hex : ∃ i, n ≤ i + f i := ⟨n, by omega⟩\n      set i := Nat.find hex with hi\n      have hni : n ≤ i + f i := Nat.find_spec hex\n      by_cases heq : n = i + f i\n      · exact Or.inl ⟨i, heq.symm⟩\n      · refine Or.inr ⟨n - i, ?_⟩\n        have hlt : n < i + f i := lt_of_le_of_ne hni heq\n        have hin : i ≤ n := by\n          rcases Nat.eq_zero_or_pos i with h0 | hpos\n          · omega\n          · have := Nat.find_min hex (show i - 1 < i by omega)\n            push Not at this\n            omega\n        have hg1 : ¬ i < g (n - i) := fun h' => by\n          have := (h i (n - i)).2 h'; omega\n        have hg2 : i ≤ g (n - i) := by\n          rcases Nat.eq_zero_or_pos i with h0 | hpos\n          · omega\n          · have hm := Nat.find_min hex (show i - 1 < i by omega)\n            push Not at hm\n            have := (h (i - 1) (n - i)).1 (by omega)\n            omega\n        simp only; omega\n  · rintro ⟨hd, hu⟩ i j\n    have h1 := lt_iff_lt_sub hF hG hd hu i j\n    have h2 := lt_iff_lt_sub hG hF hd.symm (by rw [union_comm]; exact hu) j i\n    simp only [Nat.add_sub_cancel_left] at h1 h2\n    have hne : i + f i ≠ j + g j := fun h => Set.disjoint_left.1 hd ⟨i, rfl⟩ ⟨j, h.symm⟩\n    rw [← h2, ← not_lt, ← h1]\n    omega\n\n/-- **Lambek–Moser, companion.** Every partition of `ℕ` into two infinite sets `S`, `T` is the\npair of ranges of `i ↦ i + f i` and `j ↦ j + g j` for exactly one pair `(f, g)` of monotone\nfunctions `ℕ → ℕ`. (By `inverse_iff_partition`, `f` and `g` are then inverse to each other.) -/\ntheorem existsUnique_of_partition {S T : Set ℕ} (hS : S.Infinite) (hT : T.Infinite)\n    (hd : Disjoint S T) (hu : S ∪ T = univ) :\n    ∃! p : (ℕ → ℕ) × (ℕ → ℕ), Monotone p.1 ∧ Monotone p.2 ∧\n      (range fun i => i + p.1 i) = S ∧ (range fun j => j + p.2 j) = T := by\n  classical\n  have key : ∀ U : Set ℕ, U.Infinite →\n      (∃ f : ℕ → ℕ, Monotone f ∧ (range fun i => i + f i) = U) ∧\n      ∀ f f' : ℕ → ℕ, Monotone f → Monotone f' → (range fun i => i + f i) = U →\n        (range fun i => i + f' i) = U → f = f' := by\n    intro U hU\n    have hs : StrictMono (Nat.nth (· ∈ U)) := Nat.nth_strictMono hU\n    have hr : range (Nat.nth (· ∈ U)) = U := by\n      exact Nat.range_nth_of_infinite (p := (· ∈ U)) hU\n    have hsm : ∀ f : ℕ → ℕ, Monotone f → StrictMono fun i => i + f i := fun f hf a b h => by\n      have := hf h.le; simp only; omega\n    have hid : ∀ i, i + (Nat.nth (· ∈ U) i - i) = Nat.nth (· ∈ U) i := fun i => by\n      have : i ≤ Nat.nth (· ∈ U) i := hs.id_le i\n      omega\n    refine ⟨⟨fun i => Nat.nth (· ∈ U) i - i, ?_, ?_⟩, ?_⟩\n    · refine monotone_nat_of_le_succ fun n => ?_\n      have : Nat.nth (· ∈ U) n < Nat.nth (· ∈ U) (n + 1) := hs (Nat.lt_succ_self n)\n      omega\n    · simp only [hid]; exact hr\n    · intro f f' hf hf' h h'\n      have := (hsm f hf).range_inj_of_wellFoundedLT (hsm f' hf') |>.1 (h.trans h'.symm)\n      funext i\n      have := congrFun this i\n      omega\n  obtain ⟨⟨f, hf, hfS⟩, huS⟩ := key S hS\n  obtain ⟨⟨g, hg, hgT⟩, huT⟩ := key T hT\n  refine ⟨(f, g), ⟨hf, hg, hfS, hgT⟩, ?_⟩\n  rintro ⟨f', g'⟩ ⟨hf', hg', hf'S, hg'T⟩\n  exact Prod.ext (huS f' f hf' hf hf'S hfS) (huT g' g hg' hg hg'T hgT)\n\nend LambekMoser\n\n#print axioms LambekMoser.inverse_iff_partition\n",
 "source_sha256": "b5610ca2b1f88cbb68eb30617025992020cd41abc70c9f5d2f23c80420e525e0",
 "build_output": "Lean (version 4.36.0, x86_64-unknown-linux-gnu), Mathlib fc4f59e5 prebuilt; command: lean -DautoImplicit=false LambekMoser.lean\nLambekMoser.lean:143:5: warning: Variable name `hd` is not explicitly referenced.\nLambekMoser.lean:143:25: warning: Variable name `hu` is not explicitly referenced.\n'LambekMoser.inverse_iff_partition' depends on axioms: [propext, Classical.choice, Quot.sound]\nexit 0 (no errors, no sorry warning; only the two unused-variable linter warnings, in the companion theorem existsUnique_of_partition)",
 "fidelity_review": "Rebuilt from the author's lean_source byte-for-byte (the file is saved unchanged; the compile used the prescribed Lean 4.36.0 + prebuilt Mathlib bundles, whose sha256 matched fetch.json). Textual scan: the only hit for sorry/admit/native_decide/axiom/unsafe/implemented_by/extern/opaque is the '#print axioms' line. Clause by clause: (1) hf hg : Monotone f/g with f g : ℕ → ℕ — exactly 'monotone f, g'. (2) LHS ∀ i j, f i ≤ j ↔ i < g j — exactly the vetted inverse relation. (3) RHS Disjoint (range fun i => i + f i) (range fun j => j + g j) ∧ union = univ — exactly 'ranges disjoint with union ℕ'. It is a genuine ↔, so both directions are proved; no extra hypotheses; not vacuous (the LHS is satisfiable: the evens/odds partition gives f i = i, g j = j, and the companion constructs a pair for every partition) and not trivial (needs the counting lemma lt_iff_lt_sub). Local definitions: none beyond lemma lt_iff_lt_sub (statement: for strictly monotone F,G with disjoint ranges covering ℕ, G j < F i ↔ j < F i - i), which is correct. Companion existsUnique_of_partition: for S T infinite, Disjoint S T, S ∪ T = univ, ∃! p : (ℕ→ℕ)×(ℕ→ℕ), Monotone p.1 ∧ Monotone p.2 ∧ range (i+p.1 i)=S ∧ range (j+p.2 j)=T — matches the required companion. Minor notes: hd, hu are unused in the companion (they are implied by the range equalities, so harmless, only linter warnings); the one-based original is not stated separately, only related in the docstring via f'(i)=f(i+1), g'(j)=g(j+1), and that translation is correct (f m < n ↔ m ≤ g n becomes f' i < j+1 ↔ i < g' j, i.e. f' i ≤ j ↔ i < g' j). Companion axioms were not separately printed (the file prints only the main theorem); it contains no forbidden keywords. Verdict: faithful in the vetted generality.",
 "prior_work": "No earlier formalization found. Checked: Mathlib master docs/1000.yaml lists Q6481192 'Lambek–Moser theorem' with only a title (no decl); the 1000-missing page lists it as missing; grep of the Mathlib in the prebuilt bundle finds 'Lambek' only in Mathlib/RingTheory/Flat/Tensor.lean (reference to Lambek's flatness paper, unrelated); GitHub search of mathlib4 PRs for 'Lambek' returns 4 unrelated PRs (Lambek's lemma on algebras #21140, Adamek's initial algebra theorem #36094, Flat doc #20266, UniqueProds #6723); rjwalters/lean-genius git tree has no path matching lambek/moser/beatty (only MoserTardos.lean, unrelated). UNVERIFIED: GitHub code search (needs authentication), the Lean Zulip archive, and open PRs not mentioning the word 'Lambek' in title/body (e.g. a Beatty-sequence-style PR).",
 "explanation": "Statement: take two monotone (non-decreasing) functions f, g from ℕ to ℕ. They are 'inverse' in the sense f(i) ≤ j exactly when i < g(j) for all i, j, if and only if the two sequences i + f(i) and j + g(j) never coincide and together hit every natural number, i.e. they partition ℕ. Proof: F i = i + f i and G j = j + g j are strictly increasing. Forward: if f i ≤ j then i < g j so F i < G j, otherwise G j < F i, so the ranges are disjoint; for covering, pick the least i with n ≤ F i; either n = F i, or F(i-1) < n < F i, and with j = n - i one shows g j = i, so G j = n. Backward: a counting lemma shows that, when the ranges partition ℕ, the number of G-values below F i is F i - i, and the G-values below F i form an initial segment, so G j < F i ↔ j < F i - i = f i; symmetrically F i < G j ↔ i < g j; since F i ≠ G j, f i ≤ j ↔ ¬(G j < F i) ↔ F i < G j ↔ i < g j. Companion: for infinite disjoint S, T covering ℕ, set f i = nth S i - i (monotone), so i + f i = nth S i with range S; uniqueness because a strictly monotone ℕ-sequence is determined by its range.",
 "database_entry": {
  "wikidata_id": "Q6481192",
  "title": "Lambek–Moser theorem",
  "decl": "LambekMoser.inverse_iff_partition",
  "file": "LambekMoser.lean",
  "yaml": "Q6481192:\n  title: Lambek–Moser theorem\n  decl: LambekMoser.inverse_iff_partition\n  author: \"(contributor)\"\n  date: 2026\n  # zero-based form; companion: LambekMoser.existsUnique_of_partition"
 },
 "stuck": null,
 "repairs": "None; source compiled unchanged.",
 "sources": [
  "https://leanprover-community.github.io/1000-missing.html",
  "https://raw.githubusercontent.com/leanprover-community/mathlib4/master/docs/1000.yaml",
  "https://github.com/Identity-md/lean-bundles/releases/download/pequalnp-e125f04d0dc6/fetch.json"
 ]
}