/-- If a function is `C^n` on a domain with unique derivatives, then its bundled derivative is
`C^m` when `m+1 ≤ n`. In this auxiliary lemma, we prove this fact when the source and target space
are model spaces in models with corners. The general fact is proved in
`ContMDiffOn.contMDiffOn_tangentMapWithin` -/
theorem ContMDiffOn.contMDiffOn_tangentMapWithin_aux {f : H → H'} {s : Set H}
    (hf : ContMDiffOn I I' n f s) (hmn : m + 1 ≤ n) (hs : UniqueMDiffOn I s) :
    ContMDiffOn I.tangent I'.tangent m (tangentMapWithin I I' f s)
      (π E (TangentSpace I) ⁻¹' s) := by
  have m_le_n : m ≤ n := (le_add_right le_rfl).trans hmn
  have one_le_n : 1 ≤ n := (le_add_left le_rfl).trans hmn
  have U' : UniqueDiffOn 𝕜 (range I ∩ I.symm ⁻¹' s) := fun y hy ↦ by
    simpa only [UniqueMDiffOn, UniqueMDiffWithinAt, hy.1, inter_comm, mfld_simps]
      using hs (I.symm y) hy.2
  rw [contMDiffOn_iff]
  refine' ⟨hf.continuousOn_tangentMapWithin_aux one_le_n hs, fun p q => _⟩
  suffices h :
    ContDiffOn 𝕜 m
      (((fun p : H' × E' => (I' p.fst, p.snd)) ∘ TotalSpace.toProd H' E') ∘
        tangentMapWithin I I' f s ∘
          (TotalSpace.toProd H E).symm ∘ fun p : E × E => (I.symm p.fst, p.snd))
      ((range I ∩ I.symm ⁻¹' s) ×ˢ univ)
  · -- Porting note: was `simpa [(· ∘ ·)] using h`
    convert h using 1
    · ext1 ⟨x, y⟩
      simp only [mfld_simps]; rfl
    · simp only [mfld_simps]
      rw [inter_prod, prod_univ, prod_univ]
      rfl
  change
    ContDiffOn 𝕜 m
      (fun p : E × E =>
        ((I' (f (I.symm p.fst)), (mfderivWithin I I' f s (I.symm p.fst) : E → E') p.snd) : E' × E'))
      ((range I ∩ I.symm ⁻¹' s) ×ˢ univ)
  -- check that all bits in this formula are `C^n`
  have hf' := contMDiffOn_iff.1 hf
  have A : ContDiffOn 𝕜 m (I' ∘ f ∘ I.symm) (range I ∩ I.symm ⁻¹' s) := by
    simpa only [mfld_simps] using (hf'.2 (I.symm 0) (I'.symm 0)).of_le m_le_n
  have B : ContDiffOn 𝕜 m
      ((I' ∘ f ∘ I.symm) ∘ Prod.fst) ((range I ∩ I.symm ⁻¹' s) ×ˢ (univ : Set E)) :=
    A.comp contDiff_fst.contDiffOn (prod_subset_preimage_fst _ _)
  suffices C :
    ContDiffOn 𝕜 m
      (fun p : E × E => (fderivWithin 𝕜 (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) p.1 : _) p.2)
      ((range I ∩ I.symm ⁻¹' s) ×ˢ (univ : Set E))
  · refine ContDiffOn.prod B ?_
    

Proof state:
case h
𝕜 : 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
Is : 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'
I's : SmoothManifoldWithCorners I' M'
F : Type u_8
inst✝¹³ : NormedAddCommGroup F
inst✝¹² : NormedSpace 𝕜 F
G : Type u_9
inst✝¹¹ : TopologicalSpace G
J : ModelWithCorners 𝕜 F G
N : Type u_10
inst✝¹⁰ : TopologicalSpace N
inst✝⁹ : ChartedSpace G N
Js : SmoothManifoldWithCorners J N
F' : Type u_11
inst✝⁸ : NormedAddCommGroup F'
inst✝⁷ : NormedSpace 𝕜 F'
G' : Type u_12
inst✝⁶ : TopologicalSpace G'
J' : ModelWithCorners 𝕜 F' G'
N' : Type u_13
inst✝⁵ : TopologicalSpace N'
inst✝⁴ : ChartedSpace G' N'
J's : SmoothManifoldWithCorners J' N'
F₁ : Type u_14
inst✝³ : NormedAddCommGroup F₁
inst✝² : NormedSpace 𝕜 F₁
F₂ : Type u_15
inst✝¹ : NormedAddCommGroup F₂
inst✝ : NormedSpace 𝕜 F₂
f✝ f₁ : M → M'
s✝ s₁ t : Set M
x : M
m n : ℕ∞
f : H → H'
s : Set H
hf : ContMDiffOn I I' n f s
hmn : m + 1 ≤ n
hs : UniqueMDiffOn I s
m_le_n : m ≤ n
one_le_n : 1 ≤ n
U' : UniqueDiffOn 𝕜 (range ↑I ∩ ↑(ModelWithCorners.symm I) ⁻¹' s)
p : TangentBundle I H
q : TangentBundle I' H'
hf' :
  ContinuousOn f s ∧
    ∀ (x : H) (y : H'),
      ContDiffOn 𝕜 n (↑(extChartAt I' y) ∘ f ∘ ↑(PartialEquiv.symm (extChartAt I x)))
        ((extChartAt I x).target ∩ ↑(PartialEquiv.symm (extChartAt I x)) ⁻¹' (s ∩ f ⁻¹' (extChartAt I' y).source))
A : ContDiffOn 𝕜 m (↑I' ∘ f ∘ ↑(ModelWithCorners.symm I)) (range ↑I ∩ ↑(ModelWithCorners.symm I) ⁻¹' s)
B :
  ContDiffOn 𝕜 m ((↑I' ∘ f ∘ ↑(ModelWithCorners.symm I)) ∘ Prod.fst)
    ((range ↑I ∩ ↑(ModelWithCorners.symm I) ⁻¹' s) ×ˢ univ)
C :
  ContDiffOn 𝕜 m
    (fun p =>
      (fderivWithin 𝕜 (↑I' ∘ f ∘ ↑(ModelWithCorners.symm I)) (↑(ModelWithCorners.symm I) ⁻¹' s ∩ range ↑I) p.1) p.2)
    ((range ↑I ∩ ↑(ModelWithCorners.symm I) ⁻¹' s) ×ˢ univ)
⊢ ContDiffOn 𝕜 m (fun p => (mfderivWithin I I' f s (↑(ModelWithCorners.symm I) p.1)) p.2)
    ((range ↑I ∩ ↑(ModelWithCorners.symm I) ⁻¹' s) ×ˢ univ)

Next tactic:
refine C.congr fun p hp => ?_