{"status": "proved", "decl": "LambekMoser.inverse_iff_partition", "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", "build_output": "LambekMoser.lean:143:5: warning: Variable name `hd` is not explicitly referenced.\n\nHint: The binding can be removed (if unused) or named `_` (if used implicitly). Alternatively, prefix the name with `_` to silence this warning:\n  [apply] _hd\n\nNote: This linter can be disabled with `set_option linter.unusedVariables false`\nLambekMoser.lean:143:25: warning: Variable name `hu` is not explicitly referenced.\n\nHint: The binding can be removed (if unused) or named `_` (if used implicitly). Alternatively, prefix the name with `_` to silence this warning:\n  [apply] _hu\n\nNote: This linter can be disabled with `set_option linter.unusedVariables false`\n'LambekMoser.inverse_iff_partition' depends on axioms: [propext, Classical.choice, Quot.sound]\nexit 0\n", "explanation": "Statement: for monotone f,g : ℕ → ℕ, (∀ i j, f i ≤ j ↔ i < g j) iff the ranges of i ↦ i+f i and j ↦ j+g j are disjoint with union ℕ. Proof (direct comparison). Put F i=i+f i, G j=j+g j; both are strictly monotone. (⇒) Disjointness: if f i ≤ j then i<g j so F i<G j; otherwise j<f i and g j ≤ i so G j<F i. Covering: for n take i minimal with n ≤ F i (exists, F i ≥ i). If n=F i done. Otherwise F(i-1)<n<F i (so i ≤ n); set j=n-i. Then ¬ i<g j (since f i > n-i) and i ≤ g j (since f(i-1) ≤ n-i, so i-1<g j), so g j=i and G j=n. (⇐) Counting lemma lt_iff_lt_sub: for strictly monotone F,G with disjoint ranges covering ℕ, {0..F i-1} splits into i values F k (k<i) and the G-values below F i, hence #{j | G j<F i}=F i-i, and G j<F i ↔ j<F i-i (G-values below F i form an initial segment). So G j<F i ↔ j<f i and, symmetrically, F i<G j ↔ i<g j. Since F i≠G j (disjoint), f i ≤ j ↔ ¬ G j<F i ↔ F i<G j ↔ i<g j. Companion existsUnique_of_partition: for infinite disjoint S,T with S∪T=ℕ there is exactly one pair of monotone (f,g) with the two ranges equal to S,T: f i = Nat.nth (·∈S) i − i (monotone because nth is strictly monotone; i+f i = nth i as nth i ≥ i; range = S by Nat.range_nth_of_infinite). Uniqueness: i+f i is strictly monotone with range S, and a strictly monotone map out of ℕ is determined by its range (StrictMono.range_inj_of_wellFoundedLT). Hypotheses hd,hu are unused in the companion (they are redundant for the ranges claim) but kept to match the vetted statement. One-based form relation in docstring: f' i=f(i+1), g' j=g(j+1) turns f m<n ↔ m ≤ g n into f' i ≤ j ↔ i<g' j. The one-based original itself is not stated separately.", "fidelity": "Clause 1 (monotone f,g : ℕ → ℕ): hypotheses hf hg : Monotone f/g, exact. Clause 2 (∀ i j, f i ≤ j ↔ i < g j): left side of the iff, verbatim. Clause 3 (ranges of i↦i+f i and j↦j+g j disjoint with union ℕ): right side is Disjoint (range ..) (range ..) ∧ range ∪ range = univ, verbatim; it is an iff, so both directions are proved. Companion: for S,T infinite, Disjoint, S∪T=univ, ∃! pair (f,g) of monotone functions with the two ranges equal to S and T, stated with ∃! over (ℕ→ℕ)×(ℕ→ℕ); not weaker or trivial. Caveat: the statement is the zero-based version on ℕ, not the one-based original on positive integers; the relation is explained in the docstring but no separate one-based theorem is proved. Fidelity review was by the author only (no independent reviewer): UNVERIFIED by a second party.", "prior_work": [{"address": "https://leanprover-community.github.io/1000-missing.html", "finding": "Lists Q6481192 Lambek–Moser theorem as missing (fetched 2026-11-26)."}, {"address": "Mathlib at fc4f59e577fe (local grep -rli lambek)", "finding": "Only hit is Mathlib/RingTheory/Flat/Tensor.lean (unrelated, Lambek's flatness criterion); no Lambek–Moser."}, {"address": "https://github.com/rjwalters/lean-genius/tree/main/proofs/Proofs", "finding": "UNVERIFIED: unauthenticated GitHub API listing was attempted and showed no lambek-named file, but the listing was not confirmed complete."}, {"address": "Mathlib open PRs, GitHub code search, Lean Zulip archive", "finding": "UNVERIFIED: could not be checked (GitHub code search requires authentication; Zulip/PR queue not searched)."}], "database_entry": {"wikidata_id": "Q6481192", "title": "Lambek–Moser theorem", "decl": "LambekMoser.inverse_iff_partition", "file": "LambekMoser.lean"}, "stuck": null, "sources": ["https://leanprover-community.github.io/1000-missing.html"]}