theorem affineLocally_iff_affineOpens_le (hP : RingHom.RespectsIso @P) {X Y : Scheme} (f : X ⟶ Y) :
    affineLocally (@P) f ↔
    ∀ (U : Y.affineOpens) (V : X.affineOpens) (e : V.1 ≤ (Opens.map f.1.base).obj U.1),
      P (Scheme.Hom.appLe f e) := by
  apply forall_congr'
  intro U
  delta sourceAffineLocally
  simp_rw [op_comp, Scheme.Γ.map_comp, Γ_map_morphismRestrict, Category.assoc, Scheme.Γ_map_op,
    hP.cancel_left_isIso]
  constructor
  · intro H V e
    let U' := (Opens.map f.val.base).obj U.1
    have e' : U'.openEmbedding.isOpenMap.functor.obj ((Opens.map U'.inclusion).obj V.1) = V.1 := by
      ext1; refine' Set.image_preimage_eq_inter_range.trans (Set.inter_eq_left.mpr _)
      erw [Subtype.range_val]
      convert e
    have := H ⟨(Opens.map (X.ofRestrict U'.openEmbedding).1.base).obj V.1, ?h'⟩
    erw [← X.presheaf.map_comp] at this
    rw [← hP.cancel_right_isIso _ (X.presheaf.map (eqToHom _)), Category.assoc,
      ← X.presheaf.map_comp]
    convert this using 1
    dsimp only [Functor.op, unop_op]
    rw [Opens.openEmbedding_obj_top]
    congr 1
    exact e'.symm
    

Proof state:
case h'
P : {R S : Type u} → [inst : CommRing R] → [inst_1 : CommRing S] → (R →+* S) → Prop
hP : RingHom.RespectsIso P
X Y : Scheme
f : X ⟶ Y
U : ↑(Scheme.affineOpens Y)
H :
  ∀ (U_1 : ↑(Scheme.affineOpens (Scheme.restrict X (_ : OpenEmbedding ⇑(Opens.inclusion (f⁻¹ᵁ ↑U)))))),
    P
      (f.val.c.app (op ↑U) ≫
        X.presheaf.map
            (eqToHom (_ : (IsOpenMap.functor (_ : IsOpenMap ⇑(Opens.inclusion (f⁻¹ᵁ ↑U)))).obj ⊤ = f⁻¹ᵁ ↑U)).op ≫
          (Scheme.ofRestrict (Scheme.restrict X (_ : OpenEmbedding ⇑(Opens.inclusion (f⁻¹ᵁ ↑U))))
                    (_ : OpenEmbedding ⇑(Opens.inclusion ↑U_1))).val.c.app
            (op ⊤))
V : ↑(Scheme.affineOpens X)
e : ↑V ≤ f⁻¹ᵁ ↑U
U' : Opens ↑↑X.toPresheafedSpace := f⁻¹ᵁ ↑U
e' : (IsOpenMap.functor (_ : IsOpenMap ⇑(Opens.inclusion U'))).obj ((Opens.map (Opens.inclusion U')).obj ↑V) = ↑V
⊢ Scheme.ofRestrict X (_ : OpenEmbedding ⇑(Opens.inclusion U'))⁻¹ᵁ ↑V ∈
    Scheme.affineOpens (Scheme.restrict X (_ : OpenEmbedding ⇑(Opens.inclusion (f⁻¹ᵁ ↑U))))
case h'
P : {R S : Type u} → [inst : CommRing R] → [inst_1 : CommRing S] → (R →+* S) → Prop
hP : RingHom.RespectsIso P
X Y : Scheme
f : X ⟶ Y
U : ↑(Scheme.affineOpens Y)
H :
  ∀ (U_1 : ↑(Scheme.affineOpens (Scheme.restrict X (_ : OpenEmbedding ⇑(Opens.inclusion (f⁻¹ᵁ ↑U)))))),
    P
      (f.val.c.app (op ↑U) ≫
        X.presheaf.map
            (eqToHom (_ : (IsOpenMap.functor (_ : IsOpenMap ⇑(Opens.inclusion (f⁻¹ᵁ ↑U)))).obj ⊤ = f⁻¹ᵁ ↑U)).op ≫
          (Scheme.ofRestrict (Scheme.restrict X (_ : OpenEmbedding ⇑(Opens.inclusion (f⁻¹ᵁ ↑U))))
                    (_ : OpenEmbedding ⇑(Opens.inclusion ↑U_1))).val.c.app
            (op ⊤))
V : ↑(Scheme.affineOpens X)
e : ↑V ≤ f⁻¹ᵁ ↑U
U' : Opens ↑↑X.toPresheafedSpace := f⁻¹ᵁ ↑U
e' : (IsOpenMap.functor (_ : IsOpenMap ⇑(Opens.inclusion U'))).obj ((Opens.map (Opens.inclusion U')).obj ↑V) = ↑V
⊢ Scheme.ofRestrict X (_ : OpenEmbedding ⇑(Opens.inclusion U'))⁻¹ᵁ ↑V ∈
    Scheme.affineOpens (Scheme.restrict X (_ : OpenEmbedding ⇑(Opens.inclusion (f⁻¹ᵁ ↑U))))
case h'
P : {R S : Type u} → [inst : CommRing R] → [inst_1 : CommRing S] → (R →+* S) → Prop
hP : RingHom.RespectsIso P
X Y : Scheme
f : X ⟶ Y
U : ↑(Scheme.affineOpens Y)
H :
  ∀ (U_1 : ↑(Scheme.affineOpens (Scheme.restrict X (_ : OpenEmbedding ⇑(Opens.inclusion (f⁻¹ᵁ ↑U)))))),
    P
      (f.val.c.app (op ↑U) ≫
        X.presheaf.map
            (eqToHom (_ : (IsOpenMap.functor (_ : IsOpenMap ⇑(Opens.inclusion (f⁻¹ᵁ ↑U)))).obj ⊤ = f⁻¹ᵁ ↑U)).op ≫
          (Scheme.ofRestrict (Scheme.restrict X (_ : OpenEmbedding ⇑(Opens.inclusion (f⁻¹ᵁ ↑U))))
                    (_ : OpenEmbedding ⇑(Opens.inclusion ↑U_1))).val.c.app
            (op ⊤))
V : ↑(Scheme.affineOpens X)
e : ↑V ≤ f⁻¹ᵁ ↑U
U' : Opens ↑↑X.toPresheafedSpace := f⁻¹ᵁ ↑U
e' : (IsOpenMap.functor (_ : IsOpenMap ⇑(Opens.inclusion U'))).obj ((Opens.map (Opens.inclusion U')).obj ↑V) = ↑V
⊢ Scheme.ofRestrict X (_ : OpenEmbedding ⇑(Opens.inclusion U'))⁻¹ᵁ ↑V ∈
    Scheme.affineOpens (Scheme.restrict X (_ : OpenEmbedding ⇑(Opens.inclusion (f⁻¹ᵁ ↑U))))

Next tactic:
case h' =>
      apply (X.ofRestrict U'.openEmbedding).isAffineOpen_iff_of_isOpenImmersion.mp
      -- Porting note: was convert V.2
      erw [e']
      apply V.2