-- Lean 4 proof step from Mathlib.AlgebraicGeometry.Morphisms.UniversallyClosed (goal)
Goal:
X : Scheme
K : Type u
inst : Field K
f : X ⟶ Spec (CommRingCat.of K)
inst_1 : UniversallyClosed f
let let₁ let₂ let₃ let₄ let₅ let₆ let₇ let₈ let₉ : CompactSpace.{u} ↥X
let₁₀ let₁₁ let₁₂ let₁₃ :
  let 𝒰 := X.affineCover;
  let U : (i : 𝒰.I₀) → X.Opens := fun (i : 𝒰.I₀) ↦ Scheme.Hom.opensRange (𝒰.f i);
  let T := Spec (CommRingCat.of (MvPolynomial 𝒰.I₀ K));
  let q : T ⟶ Spec (CommRingCat.of K) := Spec.map (CommRingCat.ofHom C);
  let Ti : (i : 𝒰.I₀) → T.Opens := fun (i : 𝒰.I₀) ↦ basicOpen (MvPolynomial.X i);
  let fT : pullback f q ⟶ T := pullback.snd f q;
  let p : pullback f q ⟶ X := pullback.fst f q;
  let Z := (↑(⨆ (i : 𝒰.I₀), fT ⁻¹ᵁ Ti i ⊓ p ⁻¹ᵁ U i) : Set ↥(pullback f q))ᶜ;
  ∀ (hZ : IsClosed Z), CompactSpace.{u} ↥X
let₁₄ :
  let 𝒰 := X.affineCover;
  let U : (i : 𝒰.I₀) → X.Opens := fun (i : 𝒰.I₀) ↦ Scheme.Hom.opensRange (𝒰.f i);
  let T := Spec (CommRingCat.of (MvPolynomial 𝒰.I₀ K));
  let q : T ⟶ Spec (CommRingCat.of K) := Spec.map (CommRingCat.ofHom C);
  let Ti : (i : 𝒰.I₀) → T.Opens := fun (i : 𝒰.I₀) ↦ basicOpen (MvPolynomial.X i);
  let fT : pullback f q ⟶ T := pullback.snd f q;
  let p : pullback f q ⟶ X := pullback.fst f q;
  let Z := (↑(⨆ (i : 𝒰.I₀), fT ⁻¹ᵁ Ti i ⊓ p ⁻¹ᵁ U i) : Set ↥(pullback f q))ᶜ;
  ∀ (hZ : IsClosed Z),
    let ψ : MvPolynomial 𝒰.I₀ K →ₐ[K] K := aeval fun (x : 𝒰.I₀) ↦ 1;
    let t : ↥T := (Spec.map (CommRingCat.ofHom ψ.toRingHom)) default;
    ∀ (ht : ∀ (i : 𝒰.I₀), t ∈ Ti i), CompactSpace.{u} ↥X
let₁₅ :
  let 𝒰 := X.affineCover;
  let U : (i : 𝒰.I₀) → X.Opens := fun (i : 𝒰.I₀) ↦ Scheme.Hom.opensRange (𝒰.f i);
  let T := Spec (CommRingCat.of (MvPolynomial 𝒰.I₀ K));
  let q : T ⟶ Spec (CommRingCat.of K) := Spec.map (CommRingCat.ofHom C);
  let Ti : (i : 𝒰.I₀) → T.Opens := fun (i : 𝒰.I₀) ↦ basicOpen (MvPolynomial.X i);
  let fT : pullback f q ⟶ T := pullback.snd f q;
  let p : pullback f q ⟶ X := pullback.fst f q;
  let Z := (↑(⨆ (i : 𝒰.I₀), fT ⁻¹ᵁ Ti i ⊓ p ⁻¹ᵁ U i) : Set ↥(pullback f q))ᶜ;
  ∀ (hZ : IsClosed Z),
    let Zc : T.Opens := { carrier := (⇑fT '' Z)ᶜ, is_open' := ⋯ };
    let ψ : MvPolynomial 𝒰.I₀ K →ₐ[K] K := aeval fun (x : 𝒰.I₀) ↦ 1;
    let t : ↥T := (Spec.map (CommRingCat.ofHom ψ.toRingHom)) default;
    ∀ (ht : ∀ (i : 𝒰.I₀), t ∈ Ti i) (htZc : t ∈ Zc), CompactSpace.{u} ↥X
let₁₆ let₁₇ let₁₈ let₁₉ :
  let 𝒰 := X.affineCover;
  let U : (i : 𝒰.I₀) → X.Opens := fun (i : 𝒰.I₀) ↦ Scheme.Hom.opensRange (𝒰.f i);
  let T := Spec (CommRingCat.of (MvPolynomial 𝒰.I₀ K));
  let q : T ⟶ Spec (CommRingCat.of K) := Spec.map (CommRingCat.ofHom C);
  let Ti : (i : 𝒰.I₀) → T.Opens := fun (i : 𝒰.I₀) ↦ basicOpen (MvPolynomial.X i);
  let fT : pullback f q ⟶ T := pullback.snd f q;
  let p : pullback f q ⟶ X := pullback.fst f q;
  let Z := (↑(⨆ (i : 𝒰.I₀), fT ⁻¹ᵁ Ti i ⊓ p ⁻¹ᵁ U i) : Set ↥(pullback f q))ᶜ;
  ∀ (hZ : IsClosed Z),
    let Zc : T.Opens := { carrier := (⇑fT '' Z)ᶜ, is_open' := ⋯ };
    let ψ : MvPolynomial 𝒰.I₀ K →ₐ[K] K := aeval fun (x : 𝒰.I₀) ↦ 1;
    let t : ↥T := (Spec.map (CommRingCat.ofHom ψ.toRingHom)) default;
    ∀ (ht : ∀ (i : 𝒰.I₀), t ∈ Ti i) (htZc : t ∈ Zc) (g : (↑(CommRingCat.of (MvPolynomial 𝒰.I₀ K)) : Type u))
      (htU' :
        Membership.mem (γ := Opens (PrimeSpectrum (↑(CommRingCat.of (MvPolynomial 𝒰.I₀ K)) : Type u))) (basicOpen g) t)
      (hU'le : basicOpen g ≤ Zc), CompactSpace.{u} ↥X
let₂₀ :
  let 𝒰 := X.affineCover;
  let U : (i : 𝒰.I₀) → X.Opens := fun (i : 𝒰.I₀) ↦ Scheme.Hom.opensRange (𝒰.f i);
  let T := Spec (CommRingCat.of (MvPolynomial 𝒰.I₀ K));
  let q : T ⟶ Spec (CommRingCat.of K) := Spec.map (CommRingCat.ofHom C);
  let Ti : (i : 𝒰.I₀) → T.Opens := fun (i : 𝒰.I₀) ↦ basicOpen (MvPolynomial.X i);
  let fT : pullback f q ⟶ T := pullback.snd f q;
  let p : pullback f q ⟶ X := pullback.fst f q;
  let Z := (↑(⨆ (i : 𝒰.I₀), fT ⁻¹ᵁ Ti i ⊓ p ⁻¹ᵁ U i) : Set ↥(pullback f q))ᶜ;
  ∀ (hZ : IsClosed Z),
    let Zc : T.Opens := { carrier := (⇑fT '' Z)ᶜ, is_open' := ⋯ };
    let ψ : MvPolynomial 𝒰.I₀ K →ₐ[K] K := aeval fun (x : 𝒰.I₀) ↦ 1;
    let t : ↥T := (Spec.map (CommRingCat.ofHom ψ.toRingHom)) default;
    ∀ (ht : ∀ (i : 𝒰.I₀), t ∈ Ti i) (htZc : t ∈ Zc) (g : (↑(CommRingCat.of (MvPolynomial 𝒰.I₀ K)) : Type u))
      (htU' :
        Membership.mem (γ := Opens (PrimeSpectrum (↑(CommRingCat.of (MvPolynomial 𝒰.I₀ K)) : Type u))) (basicOpen g) t)
      (hU'le : basicOpen g ≤ Zc),
      let σ := vars g;
      let φ : MvPolynomial 𝒰.I₀ K →+* MvPolynomial 𝒰.I₀ K :=
        AlgHom.toRingHom (aeval fun (i : 𝒰.I₀) ↦ if i ∈ σ then MvPolynomial.X i else 0);
      let t' : ↥T := (Spec.map (CommRingCat.ofHom φ)) t;
      ∀
        (ht'g :
          Membership.mem (γ := Opens (PrimeSpectrum (↑(CommRingCat.of (MvPolynomial 𝒰.I₀ K)) : Type u))) (basicOpen g)
            t'),
        CompactSpace.{u} ↥X
let₂₁ :
  let 𝒰 := X.affineCover;
  let U : (i : 𝒰.I₀) → X.Opens := fun (i : 𝒰.I₀) ↦ Scheme.Hom.opensRange (𝒰.f i);
  let T := Spec (CommRingCat.of (MvPolynomial 𝒰.I₀ K));
  let q : T ⟶ Spec (CommRingCat.of K) := Spec.map (CommRingCat.ofHom C);
  let Ti : (i : 𝒰.I₀) → T.Opens := fun (i : 𝒰.I₀) ↦ basicOpen (MvPolynomial.X i);
  let fT : pullback f q ⟶ T := pullback.snd f q;
  let p : pullback f q ⟶ X := pullback.fst f q;
  let Z := (↑(⨆ (i : 𝒰.I₀), fT ⁻¹ᵁ Ti i ⊓ p ⁻¹ᵁ U i) : Set ↥(pullback f q))ᶜ;
  ∀ (hZ : IsClosed Z),
    let Zc : T.Opens := { carrier := (⇑fT '' Z)ᶜ, is_open' := ⋯ };
    let ψ : MvPolynomial 𝒰.I₀ K →ₐ[K] K := aeval fun (x : 𝒰.I₀) ↦ 1;
    let t : ↥T := (Spec.map (CommRingCat.ofHom ψ.toRingHom)) default;
    ∀ (ht : ∀ (i : 𝒰.I₀), t ∈ Ti i) (htZc : t ∈ Zc) (g : (↑(CommRingCat.of (MvPolynomial 𝒰.I₀ K)) : Type u))
      (htU' :
        Membership.mem (γ := Opens (PrimeSpectrum (↑(CommRingCat.of (MvPolynomial 𝒰.I₀ K)) : Type u))) (basicOpen g) t)
      (hU'le : basicOpen g ≤ Zc),
      let σ := vars g;
      let φ : MvPolynomial 𝒰.I₀ K →+* MvPolynomial 𝒰.I₀ K :=
        AlgHom.toRingHom (aeval fun (i : 𝒰.I₀) ↦ if i ∈ σ then MvPolynomial.X i else 0);
      let t' : ↥T := (Spec.map (CommRingCat.ofHom φ)) t;
      ∀
        (ht'g :
          Membership.mem (γ := Opens (PrimeSpectrum (↑(CommRingCat.of (MvPolynomial 𝒰.I₀ K)) : Type u))) (basicOpen g)
            t')
        (h : t' ∉ ⇑fT '' Z), CompactSpace.{u} ↥X
let₂₂ :
  let 𝒰 := X.affineCover;
  let U : (i : 𝒰.I₀) → X.Opens := fun (i : 𝒰.I₀) ↦ Scheme.Hom.opensRange (𝒰.f i);
  let T := Spec (CommRingCat.of (MvPolynomial 𝒰.I₀ K));
  let q : T ⟶ Spec (CommRingCat.of K) := Spec.map (CommRingCat.ofHom C);
  let Ti : (i : 𝒰.I₀) → T.Opens := fun (i : 𝒰.I₀) ↦ basicOpen (MvPolynomial.X i);
  let fT : pullback f q ⟶ T := pullback.snd f q;
  let p : pullback f q ⟶ X := pullback.fst f q;
  let Z := (↑(⨆ (i : 𝒰.I₀), fT ⁻¹ᵁ Ti i ⊓ p ⁻¹ᵁ U i) : Set ↥(pullback f q))ᶜ;
  ∀ (hZ : IsClosed Z),
    let Zc : T.Opens := { carrier := (⇑fT '' Z)ᶜ, is_open' := ⋯ };
    let ψ : MvPolynomial 𝒰.I₀ K →ₐ[K] K := aeval fun (x : 𝒰.I₀) ↦ 1;
    let t : ↥T := (Spec.map (CommRingCat.ofHom ψ.toRingHom)) default;
    ∀ (ht : ∀ (i : 𝒰.I₀), t ∈ Ti i) (htZc : t ∈ Zc) (g : (↑(CommRingCat.of (MvPolynomial 𝒰.I₀ K)) : Type u))
      (htU' :
        Membership.mem (γ := Opens (PrimeSpectrum (↑(CommRingCat.of (MvPolynomial 𝒰.I₀ K)) : Type u))) (basicOpen g) t)
      (hU'le : basicOpen g ≤ Zc),
      let σ := vars g;
      let φ : MvPolynomial 𝒰.I₀ K →+* MvPolynomial 𝒰.I₀ K :=
        AlgHom.toRingHom (aeval fun (i : 𝒰.I₀) ↦ if i ∈ σ then MvPolynomial.X i else 0);
      let t' : ↥T := (Spec.map (CommRingCat.ofHom φ)) t;
      ∀
        (ht'g :
          Membership.mem (γ := Opens (PrimeSpectrum (↑(CommRingCat.of (MvPolynomial 𝒰.I₀ K)) : Type u))) (basicOpen g)
            t')
        (h : t' ∉ ⇑fT '' Z), Eq.{u + 1} (α := Set ↥X) (⋃ i ∈ σ, (U i).carrier) Set.univ
⊢ CompactSpace.{u} ↥X
have let₂₃ : let 𝒰 := X.affineCover;
let U : (i : 𝒰.I₀) → X.Opens := fun (i : 𝒰.I₀) ↦ Scheme.Hom.opensRange (𝒰.f i);
let T := Spec (CommRingCat.of (MvPolynomial 𝒰.I₀ K));
let q : T ⟶ Spec (CommRingCat.of K) := Spec.map (CommRingCat.ofHom C);
let Ti : (i : 𝒰.I₀) → T.Opens := fun (i : 𝒰.I₀) ↦ basicOpen (MvPolynomial.X i);
let fT : pullback f q ⟶ T := pullback.snd f q;
let p : pullback f q ⟶ X := pullback.fst f q;
let Z := (↑(⨆ (i : 𝒰.I₀), fT ⁻¹ᵁ Ti i ⊓ p ⁻¹ᵁ U i) : Set ↥(pullback f q))ᶜ;
∀ (hZ : IsClosed Z),
  let Zc : T.Opens := { carrier := (⇑fT '' Z)ᶜ, is_open' := sorry };
  let ψ : MvPolynomial 𝒰.I₀ K →ₐ[K] K := aeval fun (x : 𝒰.I₀) ↦ 1;
  let t : ↥T := (Spec.map (CommRingCat.ofHom ψ.toRingHom)) default;
  ∀ (ht : ∀ (i : 𝒰.I₀), t ∈ Ti i) (htZc : t ∈ Zc) (g : (↑(CommRingCat.of (MvPolynomial 𝒰.I₀ K)) : Type u))
    (htU' :
      Membership.mem (γ := Opens (PrimeSpectrum (↑(CommRingCat.of (MvPolynomial 𝒰.I₀ K)) : Type u))) (basicOpen g) t)
    (hU'le : basicOpen g ≤ Zc),
    let σ := vars g;
    let φ : MvPolynomial 𝒰.I₀ K →+* MvPolynomial 𝒰.I₀ K :=
      AlgHom.toRingHom (aeval fun (i : 𝒰.I₀) ↦ if i ∈ σ then MvPolynomial.X i else 0);
    let t' : ↥T := (Spec.map (CommRingCat.ofHom φ)) t;
    ∀
      (ht'g :
        Membership.mem (γ := Opens (PrimeSpectrum (↑(CommRingCat.of (MvPolynomial 𝒰.I₀ K)) : Type u))) (basicOpen g) t')
      (h : t' ∉ ⇑fT '' Z) (a : ↥X), ∃ (i : 𝒰.I₀) (_ : i ∈ σ), a ∈ (U i).carrier
Tactic: classical
  let 𝒰 : X.OpenCover := X.affineCover
  let U (i : 𝒰.I₀) : X.Opens := (𝒰.f i).opensRange
  let T : Scheme := Spec (.of <| MvPolynomial 𝒰.I₀ K)
  let q : T ⟶ Spec (.of K) := Spec.map (CommRingCat.ofHom MvPolynomial.C)
  let Ti (i : 𝒰.I₀) : T.Opens := basicOpen (MvPolynomial.X i)
  let fT : pullback f q ⟶ T := pullback.snd f q
  let p : pullback f q ⟶ X := pullback.fst f q
  let Z : Set (pullback f q :) := (⨆ i, fT ⁻¹ᵁ (Ti i) ⊓ p ⁻¹ᵁ (U i) : (pullback f q).Opens)ᶜ
  have hZ : IsClosed Z := by
    simp only [Z, isClosed_compl_iff, Opens.coe_iSup, Opens.coe_inf, Opens.map_coe]
    exact isOpen_iUnion fun i ↦ (fT.continuous.1 _ (Ti i).2).inter (p.continuous.1 _ (U i).2)
  let Zc : T.Opens := ⟨(fT '' Z)ᶜ, (fT.isClosedMap _ hZ).isOpen_compl⟩
  let ψ : MvPolynomial 𝒰.I₀ K →ₐ[K] K := MvPolynomial.aeval (fun _ ↦ 1)
  let t : T := Spec.map (CommRingCat.ofHom ψ.toRingHom) default
  have ht (i : 𝒰.I₀) : t ∈ Ti i := show ψ (.X i) ≠ 0 by simp [ψ]
  have htZc : t ∈ Zc := by
    intro ⟨z, hz, hzt⟩
    suffices ∃ i, fT z ∈ Ti i ∧ p z ∈ U i from hz (by simpa)
    exact ⟨𝒰.idx (p z), hzt ▸ ht _, by simpa [U] using 𝒰.covers (p z)⟩
  obtain ⟨U', ⟨g, rfl⟩, htU', hU'le⟩ := Opens.isBasis_iff_nbhd.mp isBasis_basic_opens htZc
  let σ : Finset 𝒰.I₀ := MvPolynomial.vars g
  let φ : MvPolynomial 𝒰.I₀ K →+* MvPolynomial 𝒰.I₀ K :=
    (MvPolynomial.aeval fun i : 𝒰.I₀ ↦ if i ∈ σ then MvPolynomial.X i else 0).toRingHom
  let t' : T := Spec.map (CommRingCat.ofHom φ) t
  have ht'g : t' ∈ PrimeSpectrum.basicOpen g :=
    show φ g ∉ t.asIdeal from (show φ g = g from aeval_ite_mem_eq_self g subset_rfl).symm ▸ htU'
  have h : t' ∉ fT '' Z := hU'le ht'g
  suffices ⋃ i ∈ σ, (U i).1 = Set.univ from
    ⟨this ▸ Finset.isCompact_biUnion _ fun i _ ↦ isCompact_range (𝒰.f i).continuous⟩
  rw [Set.iUnion₂_eq_univ_iff]
  contrapose! h
  obtain ⟨x, hx⟩ := h
  obtain ⟨z, rfl, hzr⟩ := exists_preimage_pullback x t' (Subsingleton.elim (f x) (q t'))
  suffices ∀ i, t ∈ (Ti i).comap ⟨_, continuous_comap φ⟩ → p z ∉ U i from
    ⟨z, by simpa [Z, p, fT, hzr], hzr⟩
  intro i hi₁ hi₂
  rw [comap_basicOpen, show φ (.X i) = 0 by simpa [φ] using (hx i · hi₂), basicOpen_zero] at hi₁
  cases hi₁