/-
Copyright (c) 2026 Anthropic, PBC. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
SPDX-License-Identifier: Apache-2.0
-/
import Minimal.Construction
import Minimal.Limit

/-!
# Theorem 1.1 of the paper

Paper: "**Theorem 1.1.** There is a bounded operator T on ℓ² whose only closed invariant subspaces are
{0} and ℓ²."

Lean: `IspMinimal.exists_no_invariant_subspace` is this statement, with `ℓ² = ℓ²(ℕ, ℂ)`
(`lp (fun _ : ℕ => ℂ) 2`); `IspMinimal.theorem` restates it existentially — there is a separable
infinite-dimensional complex Hilbert space `H` carrying such an operator — the form that answers the
invariant subspace problem as usually posed (`ℓ²` is such a space: `ℓ2_separable`, `ℓ2_not_finiteDimensional`, and Mathlib's
instances for completeness and the inner product).
-/

open Polynomial Module
open scoped InnerProductSpace

noncomputable section

namespace IspMinimal

/-- Theorem 1.1 of the paper, on `ℓ²(ℕ, ℂ)`. -/
theorem exists_no_invariant_subspace :
    ∃ T : ℓ2 →L[ℂ] ℓ2, ∀ M : Submodule ℂ ℓ2,
      IsClosed (M : Set ℓ2) → (∀ x ∈ M, T x ∈ M) → M = ⊥ ∨ M = ⊤ := by
  obtain ⟨J, hJ⟩ := exists_embed tower.K tower.V tower.rank_lt
  obtain ⟨T, hT⟩ := tower.exists_limit J hJ
  exact ⟨T, fun M hM hinv => tower.no_invariant_subspace J hJ T hT M hM hinv⟩

-- The density step `hd` in the following proof of `ℓ2_separable` tracks the proof of
-- `HilbertBasis.dense_span` and the `lp.hasSum_single` and `lp.single_smul` steps in that of
-- `HilbertBasis.hasSum_repr_symm`, and the orthonormality step in `ℓ2_not_finiteDimensional` tracks
-- the proof of `HilbertBasis.orthonormal`, all in Mathlib,
-- Mathlib/Analysis/InnerProductSpace/l2Space.lean:
--   Copyright (c) 2022 Heather Macbeth. All rights reserved.
--   Released under Apache 2.0 license as described in the file LICENSE.
--   Authors: Heather Macbeth
theorem ℓ2_separable : TopologicalSpace.SeparableSpace ℓ2 := by
  have hd : Dense ((Submodule.span ℂ (Set.range fun i : ℕ => (lp.single 2 i (1 : ℂ) : ℓ2))) : Set ℓ2) := by
    intro x
    have hs := (lp.hasSum_single (p := 2) (by simp) x).tendsto_sum_nat
    refine mem_closure_of_tendsto hs (Filter.Eventually.of_forall fun N => ?_)
    refine Submodule.sum_mem _ fun i _ => ?_
    have : (lp.single 2 i (x i) : ℓ2) = (x i) • lp.single 2 i (1 : ℂ) := by
      rw [← lp.single_smul, smul_eq_mul, mul_one]
    rw [this]
    exact Submodule.smul_mem _ _ (Submodule.subset_span ⟨i, rfl⟩)
  have h := (Set.countable_range fun i : ℕ => (lp.single 2 i (1 : ℂ) : ℓ2)).isSeparable.span (R := ℂ)
  rw [← TopologicalSpace.isSeparable_univ_iff, ← hd.closure_eq]
  exact h.closure

theorem ℓ2_not_finiteDimensional : ¬ FiniteDimensional ℂ ℓ2 := by
  intro h
  have ho : Orthonormal ℂ (fun i : ℕ => (lp.single 2 i (1 : ℂ) : ℓ2)) := by
    rw [orthonormal_iff_ite]
    intro i j
    rw [lp.inner_single_left, lp.single_apply, Pi.single_apply]
    split_ifs <;> simp
  exact Module.Finite.not_linearIndependent_of_infinite _ ho.linearIndependent

/-- Theorem 1.1 in existential form: there is a bounded operator `T` on a separable
infinite-dimensional complex Hilbert space `H` whose only closed invariant subspaces are `{0}` and `H`. -/
theorem «theorem» :
    ∃ (H : Type) (_ : NormedAddCommGroup H) (_ : InnerProductSpace ℂ H) (_ : CompleteSpace H),
      TopologicalSpace.SeparableSpace H ∧ ¬ FiniteDimensional ℂ H ∧
      ∃ T : H →L[ℂ] H, ∀ M : Submodule ℂ H,
        IsClosed (M : Set H) → (∀ x ∈ M, T x ∈ M) → M = ⊥ ∨ M = ⊤ :=
  ⟨ℓ2, inferInstance, inferInstance, inferInstance, ℓ2_separable, ℓ2_not_finiteDimensional,
    exists_no_invariant_subspace⟩

end IspMinimal

#print axioms IspMinimal.exists_no_invariant_subspace
#print axioms IspMinimal.theorem
