/-- Let `M` and `M'` be smooth manifolds with the same model-with-corners, `I`.  Then `f : M → M'`
is a local structomorphism for `I`, if and only if it is manifold-smooth on the domain of definition
in both directions. -/
theorem isLocalStructomorphOn_contDiffGroupoid_iff (f : PartialHomeomorph M M') :
    LiftPropOn (contDiffGroupoid ⊤ I).IsLocalStructomorphWithinAt f f.source ↔
      SmoothOn I I f f.source ∧ SmoothOn I I f.symm f.target := by
  constructor
  · intro h
    refine' ⟨isLocalStructomorphOn_contDiffGroupoid_iff_aux h,
      isLocalStructomorphOn_contDiffGroupoid_iff_aux _⟩
    -- todo: we can generalize this part of the proof to a lemma
    intro X hX
    let x := f.symm X
    have hx : x ∈ f.source := f.symm.mapsTo hX
    let c := chartAt H x
    let c' := chartAt H X
    obtain ⟨-, hxf⟩ := h x hx
    refine' ⟨(f.symm.continuousAt hX).continuousWithinAt, fun h2x => _⟩
    obtain ⟨e, he, h2e, hef, hex⟩ :
      ∃ e : PartialHomeomorph H H,
        e ∈ contDiffGroupoid ⊤ I ∧
          e.source ⊆ (c.symm ≫ₕ f ≫ₕ c').source ∧
            EqOn (c' ∘ f ∘ c.symm) e e.source ∧ c x ∈ e.source := by
      have h1 : c' = chartAt H (f x) := by simp only [f.right_inv hX]
      have h2 : c' ∘ f ∘ c.symm = ⇑(c.symm ≫ₕ f ≫ₕ c') := rfl
      have hcx : c x ∈ c.symm ⁻¹' f.source := by simp only [hx, mfld_simps]
      rw [h2]
      rw [← h1, h2, PartialHomeomorph.isLocalStructomorphWithinAt_iff'] at hxf
      · exact hxf hcx
      · mfld_set_tac
      · apply Or.inl
        simp only [hx, h1, mfld_simps]
    have h2X : c' X = e (c (f.symm X)) := by
      rw [← hef hex]
      dsimp only [Function.comp_def]
      have hfX : f.symm X ∈ c.source := by simp only [hX, mfld_simps]
      rw [c.left_inv hfX, f.right_inv hX]
    have h3e : EqOn (c ∘ f.symm ∘ c'.symm) e.symm (c'.symm ⁻¹' f.target ∩ e.target) := by
      have h1 : EqOn (c.symm ≫ₕ f ≫ₕ c').symm e.symm (e.target ∩ e.target) := by
        apply EqOn.symm
        refine' e.isImage_source_target.symm_eqOn_of_inter_eq_of_eqOn _ _
        · rw [inter_self, inter_eq_right.mpr h2e]
        · rw [inter_self]; exact hef.symm
      have h2 : e.target ⊆ (c.symm ≫ₕ f ≫ₕ c').target := by
        intro x hx; rw [← e.right_inv hx, ← hef (e.symm.mapsTo hx)]
        exact PartialHomeomorph.mapsTo _ (h2e <| e.symm.mapsTo hx)
      rw [inter_self] at h1
      rwa [inter_eq_right.mpr]
      refine' h2.trans _
      mfld_set_tac
    refine' ⟨e.symm, StructureGroupoid.symm _ he, h3e, _⟩
    rw [h2X]; exact e.mapsTo hex
  · -- We now show the converse: a local homeomorphism `f : M → M'` which is smooth in both
    -- directions is a local structomorphism.  We do this by proposing
    -- `((chart_at H x).symm.trans f).trans (chart_at H (f x))` as a candidate for a structomorphism
    -- of `H`.
    rintro ⟨h₁, h₂⟩ x hx
    refine' ⟨(h₁ x hx).continuousWithinAt, _⟩
    let c := chartAt H x
    let c' := chartAt H (f x)
    rintro (hx' : c x ∈ c.symm ⁻¹' f.source)
    -- propose `(c.symm.trans f).trans c'` as a candidate for a local structomorphism of `H`
    refine' ⟨(c.symm.trans f).trans c', ⟨_, _⟩, (_ : EqOn (c' ∘ f ∘ c.symm) _ _), _⟩
    · -- smoothness of the candidate local structomorphism in the forward direction
      intro y hy
      simp only [mfld_simps] at hy
      have H : ContMDiffWithinAt I I ⊤ f (f ≫ₕ c').source ((extChartAt I x).symm y) := by
        refine' (h₁ ((extChartAt I x).symm y) _).mono _
        · simp only [hy, mfld_simps]
        · mfld_set_tac
      have hy' : (extChartAt I x).symm y ∈ c.source := by simp only [hy, mfld_simps]
      have hy'' : f ((extChartAt I x).symm y) ∈ c'.source := by simp only [hy, mfld_simps]
      rw [contMDiffWithinAt_iff_of_mem_source hy' hy''] at H
      

Proof state:
case mpr.intro.refine'_1
𝕜 : Type u_1
inst✝¹³ : NontriviallyNormedField 𝕜
E : Type u_2
inst✝¹² : NormedAddCommGroup E
inst✝¹¹ : NormedSpace 𝕜 E
H✝ : Type u_3
inst✝¹⁰ : TopologicalSpace H✝
I : ModelWithCorners 𝕜 E H✝
M : Type u_4
inst✝⁹ : TopologicalSpace M
inst✝⁸ : ChartedSpace H✝ M
inst✝⁷ : SmoothManifoldWithCorners I M
E' : Type u_5
inst✝⁶ : NormedAddCommGroup E'
inst✝⁵ : NormedSpace 𝕜 E'
H' : Type u_6
inst✝⁴ : TopologicalSpace H'
I' : ModelWithCorners 𝕜 E' H'
M' : Type u_7
inst✝³ : TopologicalSpace M'
inst✝² : ChartedSpace H' M'
inst✝¹ : SmoothManifoldWithCorners I' M'
e : PartialHomeomorph M H✝
x✝ : M
m n : ℕ∞
inst✝ : ChartedSpace H✝ M'
IsM' : SmoothManifoldWithCorners I M'
f : PartialHomeomorph M M'
h₁ : SmoothOn I I (↑f) f.source
h₂ : SmoothOn I I (↑(PartialHomeomorph.symm f)) f.target
x : M
hx : x ∈ f.source
c : PartialHomeomorph M H✝ := chartAt H✝ x
c' : PartialHomeomorph M' H✝ := chartAt H✝ (↑f x)
hx' : ↑c x ∈ ↑(PartialHomeomorph.symm c) ⁻¹' f.source
y : E
hy :
  ((↑(ModelWithCorners.symm I) y ∈ (chartAt H✝ x).toPartialEquiv.target ∧
        ↑(PartialHomeomorph.symm (chartAt H✝ x)) (↑(ModelWithCorners.symm I) y) ∈ f.source) ∧
      ↑f (↑(PartialHomeomorph.symm (chartAt H✝ x)) (↑(ModelWithCorners.symm I) y)) ∈
        (chartAt H✝ (↑f x)).toPartialEquiv.source) ∧
    y ∈ range ↑I
H :
  ContinuousWithinAt (↑f) (f ≫ₕ c').toPartialEquiv.source (↑(PartialEquiv.symm (extChartAt I x)) y) ∧
    ContDiffWithinAt 𝕜 ⊤ (↑(extChartAt I (↑f x)) ∘ ↑f ∘ ↑(PartialEquiv.symm (extChartAt I x)))
      (↑(PartialEquiv.symm (extChartAt I x)) ⁻¹' (f ≫ₕ c').toPartialEquiv.source ∩ range ↑I)
      (↑(extChartAt I x) (↑(PartialEquiv.symm (extChartAt I x)) y))
hy' : ↑(PartialEquiv.symm (extChartAt I x)) y ∈ c.source
hy'' : ↑f (↑(PartialEquiv.symm (extChartAt I x)) y) ∈ c'.source
⊢ ContDiffWithinAt 𝕜 ⊤ (↑I ∘ ↑((PartialHomeomorph.symm c ≫ₕ f) ≫ₕ c') ∘ ↑(ModelWithCorners.symm I))
    (↑(ModelWithCorners.symm I) ⁻¹' ((PartialHomeomorph.symm c ≫ₕ f) ≫ₕ c').toPartialEquiv.source ∩ range ↑I) y

Next tactic:
convert H.2.mono _