/-- If a function is `C^n` on a domain with unique derivatives, then its bundled derivative
is `C^m` when `m+1 ≤ n`. -/
theorem ContMDiffOn.contMDiffOn_tangentMapWithin (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
  /- The strategy of the proof is to avoid unfolding the definitions, and reduce by functoriality
    to the case of functions on the model spaces, where we have already proved the result.
    Let `l` and `r` be the charts to the left and to the right, so that we have
    ```
       l^{-1}      f       r
    H --------> M ---> M' ---> H'
    ```
    Then the tangent map `T(r ∘ f ∘ l)` is smooth by a previous result. Consider the composition
    ```
        Tl        T(r ∘ f ∘ l^{-1})         Tr^{-1}
    TM -----> TH -------------------> TH' ---------> TM'
    ```
    where `Tr^{-1}` and `Tl` are the tangent maps of `r^{-1}` and `l`. Writing `Tl` and `Tr^{-1}` as
    composition of charts (called `Dl` and `il` for `l` and `Dr` and `ir` in the proof below), it
    follows that they are smooth. The composition of all these maps is `Tf`, and is therefore smooth
    as a composition of smooth maps.
    -/
  have one_le_n : 1 ≤ n := (le_add_left le_rfl).trans hmn
  -- First step: local reduction on the space, to a set `s'` which is contained in chart domains.
  refine' contMDiffOn_of_locally_contMDiffOn fun p hp => _
  have hf' := contMDiffOn_iff.1 hf
  simp only [mfld_simps] at hp
  let l := chartAt H p.proj
  set Dl := chartAt (ModelProd H E) p with hDl
  let r := chartAt H' (f p.proj)
  let Dr := chartAt (ModelProd H' E') (tangentMapWithin I I' f s p)
  let il := chartAt (ModelProd H E) (tangentMap I I l p)
  let ir := chartAt (ModelProd H' E') (tangentMap I I' (r ∘ f) p)
  let s' := f ⁻¹' r.source ∩ s ∩ l.source
  let s'_lift := π E (TangentSpace I) ⁻¹' s'
  let s'l := l.target ∩ l.symm ⁻¹' s'
  let s'l_lift := π E (TangentSpace I) ⁻¹' s'l
  rcases continuousOn_iff'.1 hf'.1 r.source r.open_source with ⟨o, o_open, ho⟩
  suffices h : ContMDiffOn I.tangent I'.tangent m (tangentMapWithin I I' f s) s'_lift
  · refine' ⟨π E (TangentSpace I) ⁻¹' (o ∩ l.source), _, _, _⟩
    show IsOpen (π E (TangentSpace I) ⁻¹' (o ∩ l.source));
    exact (IsOpen.inter o_open l.open_source).preimage (FiberBundle.continuous_proj E _)
    show p ∈ π E (TangentSpace I) ⁻¹' (o ∩ l.source)
    · simp
      have : p.proj ∈ f ⁻¹' r.source ∩ s := by simp [hp]
      rw [ho] at this
      exact this.1
    · have : π E (TangentSpace I) ⁻¹' s ∩ π E (TangentSpace I) ⁻¹' (o ∩ l.source) = s'_lift := by
        dsimp only; rw [ho]; mfld_set_tac
      rw [this]
      exact h
  /- Second step: check that all functions are smooth, and use the chain rule to write the bundled
    derivative as a composition of a function between model spaces and of charts.
    Convention: statements about the differentiability of `a ∘ b ∘ c` are named `diff_abc`.
    Statements about differentiability in the bundle have a `_lift` suffix. -/
  have U' : UniqueMDiffOn I s' := by
    apply UniqueMDiffOn.inter _ l.open_source
    rw [ho, inter_comm]
    

Proof state:
𝕜 : 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 : ℕ∞
hf : ContMDiffOn I I' n f s
hmn : m + 1 ≤ n
hs : UniqueMDiffOn I s
one_le_n : 1 ≤ n
p : TangentBundle I M
hf' :
  ContinuousOn f s ∧
    ∀ (x : M) (y : M'),
      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))
hp : p.proj ∈ s
l : PartialHomeomorph M H := chartAt H p.proj
Dl : PartialHomeomorph (TangentBundle I M) (ModelProd H E) := chartAt (ModelProd H E) p
hDl : Dl = chartAt (ModelProd H E) p
r : PartialHomeomorph M' H' := chartAt H' (f p.proj)
Dr : PartialHomeomorph (TangentBundle I' M') (ModelProd H' E') :=
  chartAt (ModelProd H' E') (tangentMapWithin I I' f s p)
il : PartialHomeomorph (TangentBundle I H) (ModelProd H E) := chartAt (ModelProd H E) (tangentMap I I (↑l) p)
ir : PartialHomeomorph (TangentBundle I' H') (ModelProd H' E') := chartAt (ModelProd H' E') (tangentMap I I' (↑r ∘ f) p)
s' : Set M := f ⁻¹' r.source ∩ s ∩ l.source
s'_lift : Set (TotalSpace E (TangentSpace I)) := TotalSpace.proj ⁻¹' s'
s'l : Set H := l.target ∩ ↑(PartialHomeomorph.symm l) ⁻¹' s'
s'l_lift : Set (TotalSpace E (TangentSpace I)) := TotalSpace.proj ⁻¹' s'l
o : Set M
o_open : IsOpen o
ho : f ⁻¹' r.source ∩ s = o ∩ s
⊢ UniqueMDiffOn I (s ∩ o)

Next tactic:
exact hs.inter o_open