import ConNF.External.Basic

/-!
# New file

In this file...

## Main declarations

* `ConNF.foo`: Something new.
-/

noncomputable section
universe u

open Cardinal Ordinal ConNF.TSet

namespace ConNF

variable [Params.{u}] {α β γ δ ε ζ : Λ} (hβ : (β : TypeIndex) < α) (hγ : (γ : TypeIndex) < β)
  (hδ : (δ : TypeIndex) < γ) (hε : (ε : TypeIndex) < δ) (hζ : (ζ : TypeIndex) < ε)

/-- A set in our model that is a well-order. Internal well-orders are exactly external well-orders,
so we externalise the definition for convenience. -/
def InternalWellOrder (r : TSet α) : Prop :=
  IsWellOrder (ExternalRel hβ hγ hδ r).field
    (InvImage (ExternalRel hβ hγ hδ r) Subtype.val)

def InternallyWellOrdered (x : TSet γ) : Prop :=
  {y : TSet δ | y ∈' x}.Subsingleton ∨ (∃ r, InternalWellOrder hβ hγ hδ r ∧ x = field hβ hγ hδ r)

@[simp]
theorem externalRel_smul (r : TSet α) (ρ : AllPerm α) :
    ExternalRel hβ hγ hδ (ρ • r) =
      InvImage (ExternalRel hβ hγ hδ r) ((ρ ↘ hβ ↘ hγ ↘ hδ)⁻¹ • ·) := by
  ext a b
  simp only [ExternalRel, mem_smul_iff', allPerm_inv_sderiv', smul_op, InvImage]

omit [Params] in
/-- Well-orders are rigid. -/
theorem apply_eq_of_isWellOrder {X : Type _} {r : Rel X X} {f : X → X}
    (hr : IsWellOrder X r) (hf : Function.Bijective f) (hf' : ∀ x y, r x y ↔ r (f x) (f y)) :
    ∀ x, f x = x := by
  let emb : r ≼i r := ⟨⟨⟨f, hf.injective⟩, λ {a b} ↦ (hf' a b).symm⟩, ?_⟩
  · have : emb = InitialSeg.refl r := Subsingleton.elim _ _
    intro x
    exact congr_arg (λ f ↦ f x) this
  · intro a b h
    exact hf.surjective _

omit [Params] in
theorem apply_eq_of_isWellOrder' {X : Type _} {r : Rel X X} {f : X → X}
    (hr : IsWellOrder r.field (InvImage r Subtype.val)) (hf : Function.Bijective f)
    (hf' : ∀ x y, r x y ↔ r (f x) (f y)) :
    ∀ x ∈ r.field, f x = x := by
  have : ∀ x ∈ r.field, f x ∈ r.field := by
    rintro x (⟨y, h⟩ | ⟨y, h⟩)
    · exact Or.inl ⟨f y, (hf' x y).mp h⟩
    · exact Or.inr ⟨f y, (hf' y x).mp h⟩
  have := apply_eq_of_isWellOrder (f := λ x ↦ ⟨f x.val, this x.val x.prop⟩) hr ⟨?_, ?_⟩ ?_
  · intro x hx
    exact congr_arg Subtype.val (this ⟨x, hx⟩)
  · intro x y h
    rw [Subtype.mk.injEq] at h
    exact Subtype.val_injective (hf.injective h)
  · intro x
    obtain ⟨y, hy⟩ := hf.surjective x.val
    refine ⟨⟨y, ?_⟩, ?_⟩
    · obtain (⟨z, h⟩ | ⟨z, h⟩) := x.prop <;>
          rw [← hy] at h <;>
          obtain ⟨z, rfl⟩ := hf.surjective z
      · exact Or.inl ⟨z, (hf' y z).mpr h⟩
      · exact Or.inr ⟨z, (hf' z y).mpr h⟩
    · simp only [hy]
  · intros
    apply hf'

theorem exists_common_support_of_internallyWellOrdered' {x : TSet δ}
    (h : InternallyWellOrdered hγ hδ hε x) :
    ∃ S : Support β, ∀ y, y ∈' x → S.Supports { { {y}' }' }[hγ] := by
  obtain (h | ⟨r, h, rfl⟩) := h
  · obtain (h | ⟨y, hy⟩) := h.eq_empty_or_singleton
    · use ⟨Enumeration.empty, Enumeration.empty⟩
      intro y hy
      rw [Set.eq_empty_iff_forall_not_mem] at h
      cases h y hy
    · obtain ⟨S, hS⟩ := TSet.exists_support y
      use S ↗ hε ↗ hδ ↗ hγ
      intro z hz
      rw [Set.eq_singleton_iff_unique_mem] at hy
      cases hy.2 z hz
      refine ⟨?_, λ h ↦ by cases h⟩
      intro ρ hρ
      simp only [Support.smul_scoderiv, ← allPermSderiv_forget', Support.scoderiv_inj] at hρ
      simp only [smul_singleton, singleton_inj]
      exact hS _ hρ
  obtain ⟨S, hS⟩ := TSet.exists_support r
  use S
  intro a ha
  refine ⟨?_, λ h ↦ by cases h⟩
  intro ρ hρ
  have := hS ρ hρ
  simp only [smul_singleton, singleton_inj]
  apply apply_eq_of_isWellOrder' (r := ExternalRel hγ hδ hε r)
  · exact h
  · exact MulAction.bijective (ρ ↘ hγ ↘ hδ ↘ hε)
  · intro x y
    conv_rhs => rw [← this]
    simp only [externalRel_smul, InvImage, inv_smul_smul]
  · rwa [mem_field_iff] at ha

include hγ in
theorem Support.Supports.ofSingleton {S : Support α} {x : TSet β}
    (h : S.Supports {x}') :
    letI : Level := ⟨α⟩
    letI : LeLevel α := ⟨le_rfl⟩
    (S.strong ↘ hβ).Supports x := by
  refine ⟨?_, λ h ↦ by cases h⟩
  intro ρ hρ
  open scoped Pointwise in
  have := sUnion_singleton_symmetric_aux hγ hβ {y | y ∈' x} S ?_ ρ hρ
  · apply ConNF.ext hγ
    intro z
    simp only [Set.ext_iff, Set.mem_setOf_eq, Set.mem_smul_set_iff_inv_smul_mem] at this
    rw [mem_smul_iff', allPerm_inv_sderiv', this]
  · intro ρ hρ
    ext z
    simp only [Set.mem_smul_set_iff_inv_smul_mem, Set.mem_image, Set.mem_setOf_eq]
    have := h.supports ρ hρ
    simp only [smul_singleton, singleton_inj] at this
    constructor
    · rintro ⟨y, h₁, h₂⟩
      rw [← smul_eq_iff_eq_inv_smul, smul_singleton] at h₂
      refine ⟨_, ?_, h₂⟩
      rw [← this]
      simp only [mem_smul_iff', allPerm_inv_sderiv', inv_smul_smul]
      exact h₁
    · rintro ⟨y, h, rfl⟩
      refine ⟨(ρ ↘ hβ ↘ hγ)⁻¹ • y, ?_, ?_⟩
      · rwa [← allPerm_inv_sderiv', ← mem_smul_iff', this]
      · simp only [smul_singleton, allPerm_inv_sderiv']

include hγ in
theorem supports_of_supports_singletons {S : Support α} {s : Set (TSet β)}
    (h : ∀ x ∈ s, S.Supports {x}') :
    ∃ S : Support β, ∀ x ∈ s, S.Supports x :=
  ⟨_, λ x hx ↦ (h x hx).ofSingleton hβ hγ⟩

theorem exists_common_support_of_internallyWellOrdered {x : TSet δ}
    (h : InternallyWellOrdered hγ hδ hε x) :
    ∃ S : Support δ, ∀ y, y ∈' x → S.Supports {y}' := by
  obtain ⟨S, hS⟩ := exists_common_support_of_internallyWellOrdered' hγ hδ hε h
  have := supports_of_supports_singletons (S := S)
      (s := singleton hδ '' (singleton hε '' {y | y ∈' x})) hγ hδ ?_
  swap
  · simp only [Set.mem_image, Set.mem_setOf_eq, exists_exists_and_eq_and, forall_exists_index,
      and_imp, forall_apply_eq_imp_iff₂]
    exact hS
  obtain ⟨T, hT⟩ := this
  have := supports_of_supports_singletons (S := T)
      (s := singleton hε '' {y | y ∈' x}) hδ hε ?_
  swap
  · simp only [Set.mem_image, Set.mem_setOf_eq, forall_exists_index, and_imp,
      forall_apply_eq_imp_iff₂] at hT ⊢
    exact hT
  simp only [Set.mem_image, Set.mem_setOf_eq, forall_exists_index, and_imp,
    forall_apply_eq_imp_iff₂] at this
  exact this

theorem internallyWellOrdered_of_common_support_of_nontrivial {x : TSet γ}
    (hx : {y : TSet δ | y ∈' x}.Nontrivial)
    (S : Support δ) (hS : ∀ y : TSet δ, y ∈' x → S.Supports y) :
    InternallyWellOrdered hβ hγ hδ x := by
  have := exists_of_symmetric
      {p : TSet β | ∃ a b : TSet δ, p = ⟨a, b⟩' ∧ a ∈' x ∧ b ∈' x ∧ WellOrderingRel a b} hβ ?_
  swap
  · use S ↗ hδ ↗ hγ ↗ hβ
    intro ρ hρ
    ext z
    simp only [Support.smul_scoderiv, ← allPermSderiv_forget', Support.scoderiv_inj] at hρ
    simp only [Set.mem_smul_set_iff_inv_smul_mem, Set.mem_setOf_eq]
    constructor
    · rintro ⟨a, b, h₁, h₂, h₃, h₄⟩
      refine ⟨a, b, ?_, h₂, h₃, h₄⟩
      rw [inv_smul_eq_iff] at h₁
      rw [h₁, smul_op, op_inj]
      exact ⟨(hS a h₂).supports _ hρ, (hS b h₃).supports _ hρ⟩
    · rintro ⟨a, b, h₁, h₂, h₃, h₄⟩
      refine ⟨a, b, ?_, h₂, h₃, h₄⟩
      rw [h₁, smul_op, op_inj]
      simp only [allPerm_inv_sderiv', inv_smul_eq_iff]
      rw [(hS a h₂).supports _ hρ, (hS b h₃).supports _ hρ]
      exact ⟨rfl, rfl⟩
  obtain ⟨r, hr⟩ := this
  right
  use r
  have hr' : ∀ a b, ExternalRel hβ hγ hδ r a b ↔ a ∈' x ∧ b ∈' x ∧ WellOrderingRel a b := by
    intro a b
    rw [ExternalRel, hr]
    simp only [Set.mem_setOf_eq, op_inj]
    constructor
    · rintro ⟨a, b, ⟨rfl, rfl⟩, h⟩
      exact h
    · intro h
      exact ⟨a, b, ⟨rfl, rfl⟩, h⟩
  have hrx : ∀ a, a ∈ (ExternalRel hβ hγ hδ r).field ↔ a ∈' x := by
    intro a
    constructor
    · rintro (⟨b, h⟩ | ⟨b, h⟩)
      · rw [hr'] at h
        exact h.1
      · rw [hr'] at h
        exact h.2.1
    · intro h
      obtain ⟨b, h₁, h₂⟩ := hx.exists_ne a
      obtain (h₃ | h₃ | h₃) := WellOrderingRel.isWellOrder.trichotomous a b
      · refine Or.inl ⟨b, ?_⟩
        rw [hr']
        exact ⟨h, h₁, h₃⟩
      · cases h₂ h₃.symm
      · refine Or.inr ⟨b, ?_⟩
        rw [hr']
        exact ⟨h₁, h, h₃⟩
  refine ⟨?_, ?_⟩
  swap
  · apply ext hδ
    intro z
    rw [mem_field_iff, hrx]
  refine @IsWellOrder.mk _ _ ?_ ?_ ?_
  · constructor
    intro a b
    obtain (h | h | h) := WellOrderingRel.isWellOrder.trichotomous a.val b.val
    · apply Or.inl
      rw [InvImage, hr']
      exact ⟨(hrx a).mp a.prop, (hrx b).mp b.prop, h⟩
    · exact Or.inr (Or.inl (Subtype.val_injective h))
    · apply Or.inr ∘ Or.inr
      rw [InvImage, hr']
      exact ⟨(hrx b).mp b.prop, (hrx a).mp a.prop, h⟩
  · constructor
    intro a b c h₁ h₂
    rw [InvImage, hr'] at h₁ h₂ ⊢
    exact ⟨h₁.1, h₂.2.1, WellOrderingRel.isWellOrder.trans _ _ _ h₁.2.2 h₂.2.2⟩
  · constructor
    apply InvImage.wf
    refine Subrelation.wf ?_ WellOrderingRel.isWellOrder.wf
    intro a b h
    rw [hr'] at h
    exact h.2.2

theorem internallyWellOrdered_of_common_support {x : TSet γ}
    (S : Support δ) (hS : ∀ y : TSet δ, y ∈' x → S.Supports y) :
    InternallyWellOrdered hβ hγ hδ x := by
  obtain (hx | hx) := Set.subsingleton_or_nontrivial {y : TSet δ | y ∈' x}
  · exact Or.inl hx
  · exact internallyWellOrdered_of_common_support_of_nontrivial hβ hγ hδ hx S hS

theorem powerset_internallyWellOrdered {x : TSet δ} (h : InternallyWellOrdered hγ hδ hε x) :
    InternallyWellOrdered hβ hγ hδ (powerset hδ hε x)

:= by
  obtain ⟨S, hS⟩ := exists_common_support_of_internallyWellOrdered hγ hδ hε h
  apply internallyWellOrdered_of_common_support hβ hγ hδ S
  intro y hy
  simp only [mem_powerset_iff] at hy
  refine ⟨?_, λ h ↦ by cases h⟩
  intro ρ hρ
  apply ext hε
  intro z
  constructor
  · intro hz
    simp only [mem_smul_iff', allPerm_inv_sderiv'] at hz
    have := (hS _ (hy _ hz)).supports ρ hρ
    simp only [smul_singleton, smul_inv_smul, singleton_inj] at this
    rwa [this]
  · intro hz
    have := (hS z (hy _ hz)).supports ρ hρ
    simp only [smul_singleton, singleton_inj] at this
    rw [← this]
    simp only [mem_smul_iff', allPerm_inv_sderiv', inv_smul_smul]
    exact hz


import ConNF.Model.Result

/-!
# New file

In this file...

## Main declarations

* `ConNF.foo`: Something new.
-/

noncomputable section
universe u

open Cardinal Ordinal ConNF.TSet

namespace ConNF

variable [Params.{u}] {α β γ δ ε ζ : Λ} (hβ : (β : TypeIndex) < α) (hγ : (γ : TypeIndex) < β)
  (hδ : (δ : TypeIndex) < γ) (hε : (ε : TypeIndex) < δ) (hζ : (ζ : TypeIndex) < ε)

def union (x y : TSet α) : TSet α :=
  (xᶜ' ⊓' yᶜ')ᶜ'

notation:68 x:68 " ⊔[" h "] " y:68 => _root_.ConNF.union h x y
notation:68 x:68 " ⊔' " y:68 => x ⊔[by assumption] y

@[simp]
theorem mem_union_iff (x y : TSet α) :
    ∀ z : TSet β, z ∈' x ⊔' y ↔ z ∈' x ∨ z ∈' y := by
  rw [union]
  intro z
  rw [mem_compl_iff, mem_inter_iff, mem_compl_iff, mem_compl_iff, or_iff_not_and_not]

def higherIndex (α : Λ) : Λ :=
  (exists_gt α).choose

theorem lt_higherIndex {α : Λ} :
    (α : TypeIndex) < higherIndex α :=
  WithBot.coe_lt_coe.mpr (exists_gt α).choose_spec

theorem tSet_nonempty (h : ∃ β : Λ, (β : TypeIndex) < α) : Nonempty (TSet α) := by
  obtain ⟨α', hα⟩ := h
  constructor
  apply typeLower lt_higherIndex lt_higherIndex lt_higherIndex hα
  apply cardinalOne lt_higherIndex lt_higherIndex

def empty : TSet α :=
  (tSet_nonempty ⟨β, hβ⟩).some ⊓' (tSet_nonempty ⟨β, hβ⟩).someᶜ'

@[simp]
theorem mem_empty_iff :
    ∀ x : TSet β, ¬x ∈' empty hβ := by
  intro x
  rw [empty, mem_inter_iff, mem_compl_iff]
  exact and_not_self

def univ : TSet α :=
  (empty hβ)ᶜ'

@[simp]
theorem mem_univ_iff :
    ∀ x : TSet β, x ∈' univ hβ := by
  intro x
  simp only [univ, mem_compl_iff, mem_empty_iff, not_false_eq_true]

/-- The set of all ordered pairs. -/
def orderedPairs : TSet α :=
  vCross hβ hγ hδ (univ hδ)

@[simp]
theorem mem_orderedPairs_iff (x : TSet β) :
    x ∈' orderedPairs hβ hγ hδ ↔ ∃ a b, x = ⟨a, b⟩' := by
  simp only [orderedPairs, vCross_spec, mem_univ_iff, and_true]

def converse (x : TSet α) : TSet α :=
  converse' hβ hγ hδ x ⊓' orderedPairs hβ hγ hδ

@[simp]
theorem op_mem_converse_iff (x : TSet α) :
    ∀ a b, ⟨a, b⟩' ∈' converse hβ hγ hδ x ↔ ⟨b, a⟩' ∈' x := by
  intro a b
  simp only [converse, mem_inter_iff, converse'_spec, mem_orderedPairs_iff, op_inj, exists_and_left,
    exists_eq', and_true]

def cross (x y : TSet γ) : TSet α :=
  converse hβ hγ hδ (vCross hβ hγ hδ x) ⊓' vCross hβ hγ hδ y

@[simp]
theorem mem_cross_iff (x y : TSet γ) :
    ∀ a, a ∈' cross hβ hγ hδ x y ↔ ∃ b c, a = ⟨b, c⟩' ∧ b ∈' x ∧ c ∈' y

:= by
  intro a
  rw [cross, mem_inter_iff, vCross_spec]
  constructor
  · rintro ⟨h₁, b, c, rfl, h₂⟩
    simp only [op_mem_converse_iff, vCross_spec, op_inj] at h₁
    obtain ⟨b', c', ⟨rfl, rfl⟩, h₁⟩ := h₁
    exact ⟨b, c, rfl, h₁, h₂⟩
  · rintro ⟨b, c, rfl, h₁, h₂⟩
    simp only [op_mem_converse_iff, vCross_spec, op_inj]
    exact ⟨⟨c, b, ⟨rfl, rfl⟩, h₁⟩, ⟨b, c, ⟨rfl, rfl⟩, h₂⟩⟩


import ConNF.Model.RaiseStrong

/-!
# New file

In this file...

## Main declarations

* `ConNF.foo`: Something new.
-/

noncomputable section
universe u

open Cardinal Ordinal

open scoped Pointwise

namespace ConNF

variable [Params.{u}]

/-- A redefinition of the derivative of allowable permutations that is invariant of level,
but still has nice definitional properties. -/
@[default_instance 200]
instance {β γ : TypeIndex} : Derivative (AllPerm β) (AllPerm γ) β γ where
  deriv ρ A :=
    A.recSderiv
    (motive := λ (δ : TypeIndex) (A : β ↝ δ) ↦
      letI : Level := ⟨δ.recBotCoe (Nonempty.some inferInstance) id⟩
      letI : LeLevel δ := ⟨δ.recBotCoe (λ _ ↦ bot_le) (λ _ h ↦ WithBot.coe_le_coe.mpr h.le)
        (show δ.recBotCoe (Nonempty.some inferInstance) id = Level.α from rfl)⟩
      AllPerm δ)
    ρ (λ δ ε A h ρ ↦
      letI : Level := ⟨δ.recBotCoe (Nonempty.some inferInstance) id⟩
      letI : LeLevel δ := ⟨δ.recBotCoe (λ _ ↦ bot_le) (λ _ h ↦ WithBot.coe_le_coe.mpr h.le)
        (show δ.recBotCoe (Nonempty.some inferInstance) id = Level.α from rfl)⟩
      letI : LeLevel ε := ⟨h.le.trans LeLevel.elim⟩
      PreCoherentData.allPermSderiv h ρ)

@[simp]
theorem allPerm_deriv_nil' {β : TypeIndex}
    (ρ : AllPerm β) :
    ρ ⇘ (.nil : β ↝ β) = ρ :=
  rfl

@[simp]
theorem allPerm_deriv_sderiv' {β γ δ : TypeIndex}
    (ρ : AllPerm β) (A : β ↝ γ) (h : δ < γ) :
    ρ ⇘ (A ↘ h) = ρ ⇘ A ↘ h :=
  rfl

@[simp]
theorem allPermSderiv_forget' {β γ : TypeIndex} (h : γ < β) (ρ : AllPerm β) :
    (ρ ↘ h)ᵁ = ρᵁ ↘ h :=
  letI : Level := ⟨β.recBotCoe (Nonempty.some inferInstance) id⟩
  letI : LeLevel β := ⟨β.recBotCoe (λ _ ↦ bot_le) (λ _ h ↦ WithBot.coe_le_coe.mpr h.le)
    (show β.recBotCoe (Nonempty.some inferInstance) id = Level.α from rfl)⟩
  letI : LeLevel γ := ⟨h.le.trans LeLevel.elim⟩
  allPermSderiv_forget h ρ

@[simp]
theorem allPerm_inv_sderiv' {β γ : TypeIndex} (h : γ < β) (ρ : AllPerm β) :
    ρ⁻¹ ↘ h = (ρ ↘ h)⁻¹ := by
  apply allPermForget_injective
  rw [allPermSderiv_forget', allPermForget_inv, Tree.inv_sderiv, allPermForget_inv,
    allPermSderiv_forget']

def Symmetric {α β : Λ} (s : Set (TSet β)) (hβ : (β : TypeIndex) < α) : Prop :=
  ∃ S : Support α, ∀ ρ : AllPerm α, ρᵁ • S = S → ρ ↘ hβ • s = s

def newSetEquiv {α : Λ} :
    letI : Level := ⟨α⟩
    @TSet _ α newModelData.toPreModelData ≃ TSet α :=
  letI : Level := ⟨α⟩
  castTSet (D₁ := newModelData) (D₂ := globalModelData) rfl
    (by rw [globalModelData, motive_eq, constructMotive, globalLtData_eq])

@[simp]
theorem newSetEquiv_forget {α : Λ}
    (x : letI : Level := ⟨α⟩; @TSet _ α newModelData.toPreModelData) :
    (newSetEquiv x)ᵁ = xᵁ :=
  letI : Level := ⟨α⟩
  castTSet_forget (D₁ := newModelData) (D₂ := globalModelData) _ x

def allPermEquiv {α : Λ} :
    letI : Level := ⟨α⟩
    NewPerm ≃ AllPerm α :=
  letI : Level := ⟨α⟩
  castAllPerm (D₁ := newModelData) (D₂ := globalModelData) rfl
    (by rw [globalModelData, motive_eq, constructMotive, globalLtData_eq])

@[simp]
theorem allPermEquiv_forget {α : Λ} (ρ : letI : Level := ⟨α⟩; NewPerm) :
    (allPermEquiv ρ)ᵁ = ρᵁ

:=
  letI : Level := ⟨α⟩
  castAllPerm_forget (D₁ := newModelData) (D₂ := globalModelData) _ ρ


import ConNF.ModelData.PathEnumeration

/-!
# Supports

In this file, we define the notion of a support.

## Main declarations

* `ConNF.BaseSupport`: The type of supports of atoms.
* `ConNF.Support`: The type of supports of objects of arbitrary type indices.
-/

universe u

open Cardinal

namespace ConNF

variable [Params.{u}]

/-!
## Base supports
-/

structure BaseSupport where
  atoms : Enumeration Atom
  nearLitters : Enumeration NearLitter

namespace BaseSupport

instance : SuperA BaseSupport (Enumeration Atom) where
  superA := atoms

instance : SuperN BaseSupport (Enumeration NearLitter) where
  superN := nearLitters

@[simp]
theorem mk_atoms {a : Enumeration Atom} {N : Enumeration NearLitter} :
    (BaseSupport.mk a N)ᴬ = a :=
  rfl

@[simp]
theorem mk_nearLitters {a : Enumeration Atom} {N : Enumeration NearLitter} :
    (BaseSupport.mk a N)ᴺ = N :=
  rfl

theorem atoms_congr {S T : BaseSupport} (h : S = T) :
    Sᴬ = Tᴬ :=
  h ▸ rfl

theorem nearLitters_congr {S T : BaseSupport} (h : S = T) :
    Sᴺ = Tᴺ :=
  h ▸ rfl

@[ext]
theorem ext {S T : BaseSupport} (h₁ : Sᴬ = Tᴬ) (h₂ : Sᴺ = Tᴺ) : S = T := by
  obtain ⟨SA, SN⟩ := S
  obtain ⟨TA, TN⟩ := T
  cases h₁
  cases h₂
  rfl

instance : SMul BasePerm BaseSupport where
  smul π S := ⟨π • Sᴬ, π • Sᴺ⟩

@[simp]
theorem smul_atoms (π : BasePerm) (S : BaseSupport) :
    (π • S)ᴬ = π • Sᴬ :=
  rfl

@[simp]
theorem smul_nearLitters (π : BasePerm) (S : BaseSupport) :
    (π • S)ᴺ = π • Sᴺ :=
  rfl

@[simp]
theorem smul_atoms_eq_of_smul_eq {π : BasePerm} {S : BaseSupport}
    (h : π • S = S) :
    π • Sᴬ = Sᴬ := by
  rw [← smul_atoms, h]

@[simp]
theorem smul_nearLitters_eq_of_smul_eq {π : BasePerm} {S : BaseSupport}
    (h : π • S = S) :
    π • Sᴺ = Sᴺ := by
  rw [← smul_nearLitters, h]

instance : MulAction BasePerm BaseSupport where
  one_smul S := by
    apply ext
    · rw [smul_atoms, one_smul]
    · rw [smul_nearLitters, one_smul]
  mul_smul π₁ π₂ S := by
    apply ext
    · rw [smul_atoms, smul_atoms, smul_atoms, mul_smul]
    · rw [smul_nearLitters, smul_nearLitters, smul_nearLitters, mul_smul]

theorem smul_eq_smul_iff (π₁ π₂ : BasePerm) (S : BaseSupport) :
    π₁ • S = π₂ • S ↔ (∀ a ∈ Sᴬ, π₁ • a = π₂ • a) ∧ (∀ N ∈ Sᴺ, π₁ • N = π₂ • N) := by
  constructor
  · intro h
    constructor
    · rintro a ⟨i, ha⟩
      have := congr_arg (·ᴬ.rel i (π₁ • a)) h
      simp only [smul_atoms, Enumeration.smul_rel, inv_smul_smul, eq_iff_iff] at this
      have := Sᴬ.rel_coinjective.coinjective ha (this.mp ha)
      rw [eq_inv_smul_iff] at this
      rw [this]
    · rintro N ⟨i, hN⟩
      have := congr_arg (·ᴺ.rel i (π₁ • N)) h
      simp only [smul_nearLitters, Enumeration.smul_rel, inv_smul_smul, eq_iff_iff] at this
      have := Sᴺ.rel_coinjective.coinjective hN (this.mp hN)
      rw [eq_inv_smul_iff] at this
      rw [this]
  · intro h
    ext : 2
    · rfl
    · ext i a : 3
      rw [smul_atoms, smul_atoms, Enumeration.smul_rel, Enumeration.smul_rel]
      constructor
      · intro ha
        have := h.1 _ ⟨i, ha⟩
        rw [smul_inv_smul, ← inv_smul_eq_iff] at this
        rwa [this]
      · intro ha
        have := h.1 _ ⟨i, ha⟩
        rw [smul_inv_smul, smul_eq_iff_eq_inv_smul] at this
        rwa [← this]
    · rfl
    · ext i a : 3
      rw [smul_nearLitters, smul_nearLitters, Enumeration.smul_rel, Enumeration.smul_rel]
      constructor
      · intro hN
        have := h.2 _ ⟨i, hN⟩
        rw [smul_inv_smul, ← inv_smul_eq_iff] at this
        rwa [this]
      · intro hN
        have := h.2 _ ⟨i, hN⟩
        rw [smul_inv_smul, smul_eq_iff_eq_inv_smul] at this
        rwa [← this]

theorem smul_eq_iff (π : BasePerm) (S : BaseSupport) :
    π • S = S ↔ (∀ a ∈ Sᴬ, π • a = a) ∧ (∀ N ∈ Sᴺ, π • N = N) := by
  have := smul_eq_smul_iff π 1 S
  simp only [one_smul] at this
  exact this

noncomputable instance : Add BaseSupport where
  add S T := ⟨Sᴬ + Tᴬ, Sᴺ + Tᴺ⟩

@[simp]
theorem add_atoms (S T : BaseSupport) :
    (S + T)ᴬ = Sᴬ + Tᴬ :=
  rfl

@[simp]
theorem add_nearLitters (S T : BaseSupport) :
    (S + T)ᴺ = Sᴺ + Tᴺ :=
  rfl

end BaseSupport

def baseSupportEquiv : BaseSupport ≃ Enumeration Atom × Enumeration NearLitter where
  toFun S := (Sᴬ, Sᴺ)
  invFun S := ⟨S.1, S.2⟩
  left_inv _ := rfl
  right_inv _ := rfl

theorem card_baseSupport : #BaseSupport = #μ := by
  rw [Cardinal.eq.mpr ⟨baseSupportEquiv⟩, mk_prod, lift_id, lift_id,
    card_enumeration_eq card_atom, card_enumeration_eq card_nearLitter, mul_eq_self aleph0_lt_μ.le]

/-!
## Structural supports
-/

structure Support (α : TypeIndex) where
  atoms : Enumeration (α ↝ ⊥ × Atom)
  nearLitters : Enumeration (α ↝ ⊥ × NearLitter)

namespace Support

variable {α β : TypeIndex}

instance : SuperA (Support α) (Enumeration (α ↝ ⊥ × Atom)) where
  superA := atoms

instance : SuperN (Support α) (Enumeration (α ↝ ⊥ × NearLitter)) where
  superN := nearLitters

@[simp]
theorem mk_atoms (E : Enumeration (α ↝ ⊥ × Atom)) (F : Enumeration (α ↝ ⊥ × NearLitter)) :
    (⟨E, F⟩ : Support α)ᴬ = E :=
  rfl

@[simp]
theorem mk_nearLitters (E : Enumeration (α ↝ ⊥ × Atom)) (F : Enumeration (α ↝ ⊥ × NearLitter)) :
    (⟨E, F⟩ : Support α)ᴺ = F :=
  rfl

instance : Derivative (Support α) (Support β) α β where
  deriv S A := ⟨Sᴬ ⇘ A, Sᴺ ⇘ A⟩

instance : Coderivative (Support β) (Support α) α β where
  coderiv S A := ⟨Sᴬ ⇗ A, Sᴺ ⇗ A⟩

instance : BotDerivative (Support α) BaseSupport α where
  botDeriv S A := ⟨Sᴬ ⇘. A, Sᴺ ⇘. A⟩
  botSderiv S := ⟨Sᴬ ↘., Sᴺ ↘.⟩
  botDeriv_single S h := by dsimp only; rw [botDeriv_single, botDeriv_single]

@[simp]
theorem deriv_atoms {α β : TypeIndex} (S : Support α) (A : α ↝ β) :
    Sᴬ ⇘ A = (S ⇘ A)ᴬ :=
  rfl

@[simp]
theorem deriv_nearLitters {α β : TypeIndex} (S : Support α) (A : α ↝ β) :
    Sᴺ ⇘ A = (S ⇘ A)ᴺ :=
  rfl

@[simp]
theorem sderiv_atoms {α β : TypeIndex} (S : Support α) (h : β < α) :
    Sᴬ ↘ h = (S ↘ h)ᴬ :=
  rfl

@[simp]
theorem sderiv_nearLitters {α β : TypeIndex} (S : Support α) (h : β < α) :
    Sᴺ ↘ h = (S ↘ h)ᴺ


import ConNF.Background.ReflTransGen
import ConNF.FOA.Inflexible

/-!
# Strong supports

In this file, we define strong supports.

## Main declarations

* `ConNF.Support.Strong`: The property that a support is strong.
-/

noncomputable section
universe u

open Cardinal Ordinal
open scoped symmDiff

namespace ConNF

variable [Params.{u}] {β : TypeIndex}

structure BaseSupport.Closed (S : BaseSupport) : Prop where
  interference_subset {N₁ N₂ : NearLitter} :
    N₁ ∈ Sᴺ → N₂ ∈ Sᴺ → ∀ a ∈ interference N₁ N₂, a ∈ Sᴬ

namespace Support

variable [Level] [CoherentData] [LeLevel β]

structure PreStrong (S : Support β) : Prop where
  support_le {A : β ↝ ⊥} {N : NearLitter} (h : N ∈ (S ⇘. A)ᴺ)
    (P : InflexiblePath β) (t : Tangle P.δ)
    (hA : A = P.A ↘ P.hε ↘.) (ht : Nᴸ = fuzz P.hδε t) :
    t.support ≤ S ⇘ (P.A ↘ P.hδ)

structure Closed (S : Support β) : Prop where
  closed : ∀ A, (S ⇘. A).Closed

structure Strong (S : Support β) extends S.PreStrong, S.Closed : Prop

theorem PreStrong.smul {S : Support β} (hS : S.PreStrong) (ρ : AllPerm β) : (ρᵁ • S).PreStrong := by
  constructor
  intro A N hN P t hA ht
  rw [smul_derivBot, BaseSupport.smul_nearLitters, Enumeration.mem_smul] at hN
  have := hS.support_le hN P (ρ⁻¹ ⇘ P.A ↘ P.hδ • t) hA ?_
  · convert smul_le_smul this (ρᵁ ⇘ P.A ↘ P.hδ) using 1
    · rw [Tangle.smul_support, smul_smul,
        allPermSderiv_forget, allPermDeriv_forget, allPermForget_inv,
        Tree.inv_deriv, Tree.inv_sderiv, mul_inv_cancel, one_smul]
    · ext B : 1
      rw [smul_derivBot, Tree.sderiv_apply, Tree.deriv_apply, Path.deriv_scoderiv]
      rfl
  · rw [← smul_fuzz P.hδ P.hε P.hδε, allPermDeriv_forget, allPermForget_inv,
      BasePerm.smul_nearLitter_litter, ← Tree.inv_apply, hA, ht]
    rfl

theorem Closed.smul {S : Support β} (hS : S.Closed) (ρ : AllPerm β) : (ρᵁ • S).Closed := by
  constructor
  intro A
  constructor
  intro N₁ N₂ h₁ h₂
  simp only [smul_derivBot, BaseSupport.smul_nearLitters, BaseSupport.smul_atoms,
    Enumeration.mem_smul] at h₁ h₂ ⊢
  intro a ha
  apply (hS.closed A).interference_subset h₁ h₂
  rwa [← BasePerm.smul_interference, Set.smul_mem_smul_set_iff]

theorem Strong.smul {S : Support β} (hS : S.Strong) (ρ : AllPerm β) : (ρᵁ • S).Strong :=
  ⟨hS.toPreStrong.smul ρ, hS.toClosed.smul ρ⟩

theorem PreStrong.add {S T : Support β} (hS : S.PreStrong) (hT : T.PreStrong) :
    (S + T).PreStrong

:= by
  constructor
  intro A N hN P t hA ht
  simp only [add_derivBot, BaseSupport.add_nearLitters, Enumeration.mem_add_iff] at hN
  obtain hN | hN := hN
  · intro B
    simp only [deriv_derivBot, add_derivBot]
    exact (hS.support_le hN P t hA ht B).trans (BaseSupport.le_add_right)
  · intro B
    simp only [deriv_derivBot, add_derivBot]
    exact (hT.support_le hN P t hA ht B).trans (BaseSupport.le_add_left)


import ConNF.Model.TTT

/-!
# New file

In this file...

## Main declarations

* `ConNF.foo`: Something new.
-/

noncomputable section
universe u

open Cardinal Ordinal

namespace ConNF

variable [Params.{u}] {α β γ δ ε ζ : Λ} (hβ : (β : TypeIndex) < α) (hγ : (γ : TypeIndex) < β)
  (hδ : (δ : TypeIndex) < γ) (hε : (ε : TypeIndex) < δ) (hζ : (ζ : TypeIndex) < ε)

namespace TSet

theorem exists_inter (x y : TSet α) :
    ∃ w : TSet α, ∀ z : TSet β, z ∈[hβ] w ↔ z ∈[hβ] x ∧ z ∈[hβ] y := by
  refine exists_of_symmetric {z | z ∈[hβ] x ∧ z ∈[hβ] y} hβ ?_
  obtain ⟨S, hS⟩ := symmetric x hβ
  obtain ⟨T, hT⟩ := symmetric y hβ
  use S + T
  intro ρ hρ
  specialize hS ρ (smul_eq_of_le Support.le_add_right hρ)
  specialize hT ρ (smul_eq_of_le Support.le_add_left hρ)
  simp [Set.ext_iff, Set.mem_smul_set_iff_inv_smul_mem] at hS hT ⊢
  aesop

theorem exists_compl (x : TSet α) :
    ∃ y : TSet α, ∀ z : TSet β, z ∈[hβ] y ↔ ¬z ∈[hβ] x := by
  refine exists_of_symmetric {z | ¬z ∈[hβ] x} hβ ?_
  obtain ⟨S, hS⟩ := symmetric x hβ
  use S
  intro ρ hρ
  specialize hS ρ hρ
  simp [Set.ext_iff, Set.mem_smul_set_iff_inv_smul_mem] at hS ⊢
  aesop

theorem exists_up (x y : TSet β) :
    ∃ w : TSet α, ∀ z : TSet β, z ∈[hβ] w ↔ z = x ∨ z = y := by
  refine exists_of_symmetric {x, y} hβ ?_
  obtain ⟨S, hS⟩ := exists_support x
  obtain ⟨T, hT⟩ := exists_support y
  use (S + T) ↗ hβ
  intro ρ hρ
  rw [Support.smul_scoderiv, Support.scoderiv_inj, ← allPermSderiv_forget'] at hρ
  specialize hS (ρ ↘ hβ) (smul_eq_of_le Support.le_add_right hρ)
  specialize hT (ρ ↘ hβ) (smul_eq_of_le Support.le_add_left hρ)
  simp only [Set.smul_set_def, Set.image, Set.mem_insert_iff, Set.mem_singleton_iff,
    exists_eq_or_imp, hS, exists_eq_left, hT]
  ext z
  rw [Set.mem_insert_iff, Set.mem_singleton_iff, Set.mem_setOf_eq]
  aesop

/-- The unordered pair. -/
def up (x y : TSet β) : TSet α :=
  (exists_up hβ x y).choose

@[simp]
theorem mem_up_iff (x y z : TSet β) :
    z ∈[hβ] up hβ x y ↔ z = x ∨ z = y :=
  (exists_up hβ x y).choose_spec z

/-- The Kuratowski ordered pair. -/
def op (x y : TSet γ) : TSet α :=
  up hβ (singleton hγ x) (up hγ x y)

theorem up_injective {x y z w : TSet β} (h : up hβ x y = up hβ z w) :
    (x = z ∧ y = w) ∨ (x = w ∧ y = z) := by
  have h₁ := mem_up_iff hβ x y z
  have h₂ := mem_up_iff hβ x y w
  have h₃ := mem_up_iff hβ z w x
  have h₄ := mem_up_iff hβ z w y
  rw [h, mem_up_iff] at h₁ h₂
  rw [← h, mem_up_iff] at h₃ h₄
  aesop

@[simp]
theorem up_inj (x y z w : TSet β) :
    up hβ x y = up hβ z w ↔ (x = z ∧ y = w) ∨ (x = w ∧ y = z) := by
  constructor
  · exact up_injective hβ
  · rintro (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩)
    · rfl
    · apply tSet_ext' hβ
      aesop

@[simp]
theorem up_self {x : TSet β} :
    up hβ x x = singleton hβ x := by
  apply tSet_ext' hβ
  aesop

@[simp]
theorem up_eq_singleton_iff (x y z : TSet β) :
    up hβ x y = singleton hβ z ↔ x = z ∧ y = z := by
  constructor
  · intro h
    have h₁ := typedMem_singleton_iff' hβ z x
    have h₂ := typedMem_singleton_iff' hβ z y
    rw [← h, mem_up_iff] at h₁ h₂
    aesop
  · rintro ⟨rfl, rfl⟩
    rw [up_self]

@[simp]
theorem singleton_eq_up_iff (x y z : TSet β) :
    singleton hβ z = up hβ x y ↔ x = z ∧ y = z := by
  rw [← up_eq_singleton_iff hβ x y z, eq_comm]

theorem op_injective {x y z w : TSet γ} (h : op hβ hγ x y = op hβ hγ z w) :
    x = z ∧ y = w := by
  rw [op, op] at h
  simp only [up_inj, singleton_inj, singleton_eq_up_iff, up_eq_singleton_iff] at h
  obtain (⟨rfl, ⟨h, rfl⟩ | ⟨rfl, rfl⟩⟩ | ⟨⟨rfl, rfl⟩, ⟨h, rfl⟩⟩) := h <;> simp only [and_self]

@[simp]
theorem op_inj (x y z w : TSet γ) :
    op hβ hγ x y = op hβ hγ z w ↔ x = z ∧ y = w := by
  constructor
  · exact op_injective hβ hγ
  · rintro ⟨rfl, rfl⟩
    rfl

@[simp]
theorem op_eq_singleton_iff (x y : TSet γ) (z : TSet β) :
    op hβ hγ x y = singleton hβ z ↔ singleton hγ x = z ∧ singleton hγ y = z := by
  rw [op, up_eq_singleton_iff, and_congr_right_iff]
  rintro rfl
  simp only [up_eq_singleton_iff, true_and, singleton_inj]

@[simp]
theorem smul_up (x y : TSet β) (ρ : AllPerm α) :
    ρ • up hβ x y = up hβ (ρ ↘ hβ • x) (ρ ↘ hβ • y) := by
  apply tSet_ext' hβ
  aesop

@[simp]
theorem smul_op (x y : TSet γ) (ρ : AllPerm α) :
    ρ • op hβ hγ x y = op hβ hγ (ρ ↘ hβ ↘ hγ • x) (ρ ↘ hβ ↘ hγ • y) := by
  apply tSet_ext' hβ
  simp only [op, smul_up, smul_singleton, mem_up_iff, implies_true]

theorem exists_singletonImage (x : TSet β) :
    ∃ y : TSet α, ∀ z w,
    op hγ hδ (singleton hε z) (singleton hε w) ∈[hβ] y ↔ op hδ hε z w ∈[hγ] x := by
  have := exists_of_symmetric {u | ∃ z w : TSet ε, op hδ hε z w ∈[hγ] x ∧
      u = op hγ hδ (singleton hε z) (singleton hε w)} hβ ?_
  · obtain ⟨y, hy⟩ := this
    use y
    intro z w
    rw [hy]
    simp only [Set.mem_setOf_eq, op_inj, singleton_inj, exists_eq_right_right', exists_eq_right']
  · obtain ⟨S, hS⟩ := exists_support x
    use S ↗ hβ
    intro ρ hρ
    rw [Support.smul_scoderiv, Support.scoderiv_inj, ← allPermSderiv_forget'] at hρ
    specialize hS (ρ ↘ hβ) hρ
    ext z
    constructor
    · rintro ⟨_, ⟨z, w, hab, rfl⟩, rfl⟩
      refine ⟨ρ ↘ hβ ↘ hγ ↘ hδ ↘ hε • z, ρ ↘ hβ ↘ hγ ↘ hδ ↘ hε • w, ?_, ?_⟩
      · rwa [← hS, mem_smul_iff', smul_op, allPerm_inv_sderiv', allPerm_inv_sderiv',
          allPerm_inv_sderiv', inv_smul_smul, inv_smul_smul]
      · simp only [smul_op, smul_singleton]
    · rintro ⟨z, w, hab, rfl⟩
      refine ⟨ρ⁻¹ ↘ hβ • op hγ hδ (singleton hε z) (singleton hε w), ?_, ?_⟩
      · simp only [allPerm_inv_sderiv', smul_op, smul_singleton, Set.mem_setOf_eq, op_inj,
          singleton_inj, exists_eq_right_right', exists_eq_right']
        rw [smul_eq_iff_eq_inv_smul] at hS
        rw [hS]
        simp only [mem_smul_iff', inv_inv, smul_op, smul_inv_smul]
        exact hab
      · simp only [allPerm_inv_sderiv', smul_inv_smul]

theorem exists_insertion2 (x : TSet γ) :
    ∃ y : TSet α, ∀ a b c, op hγ hδ (singleton hε (singleton hζ a)) (op hε hζ b c) ∈[hβ] y ↔
    op hε hζ a c ∈[hδ] x := by
  have := exists_of_symmetric {u | ∃ a b c : TSet ζ, op hε hζ a c ∈[hδ] x ∧
      u = op hγ hδ (singleton hε (singleton hζ a)) (op hε hζ b c)} hβ ?_
  · obtain ⟨y, hy⟩ := this
    use y
    intro a b c
    rw [hy]
    constructor
    · rintro ⟨a', b', c', h₁, h₂⟩
      simp only [op_inj, singleton_inj] at h₂
      obtain ⟨rfl, rfl, rfl⟩ := h₂
      exact h₁
    · intro h
      exact ⟨a, b, c, h, rfl⟩
  · obtain ⟨S, hS⟩ := exists_support x
    use S ↗ hγ ↗ hβ
    intro ρ hρ
    simp only [Support.smul_scoderiv, Support.scoderiv_inj, ← allPermSderiv_forget'] at hρ
    specialize hS (ρ ↘ hβ ↘ hγ) hρ
    ext z
    constructor
    · rintro ⟨_, ⟨a, b, c, hx, rfl⟩, rfl⟩
      refine ⟨ρ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • a, ρ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • b,
        ρ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • c, ?_, ?_⟩
      · rw [← hS]
        simp only [mem_smul_iff', allPerm_inv_sderiv', smul_op, inv_smul_smul]
        exact hx
      · simp only [smul_op, smul_singleton]
    · rintro ⟨a, b, c, hx, rfl⟩
      rw [Set.mem_smul_set_iff_inv_smul_mem]
      refine ⟨ρ⁻¹ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • a, ρ⁻¹ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • b,
        ρ⁻¹ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • c, ?_, ?_⟩
      · rw [smul_eq_iff_eq_inv_smul] at hS
        rw [hS, mem_smul_iff']
        simp only [inv_inv, allPerm_inv_sderiv', smul_op, smul_inv_smul]
        exact hx
      · simp only [smul_op, allPerm_inv_sderiv', smul_singleton]

theorem exists_insertion3 (x : TSet γ) :
    ∃ y : TSet α, ∀ a b c, op hγ hδ (singleton hε (singleton hζ a)) (op hε hζ b c) ∈[hβ] y ↔
    op hε hζ a b ∈[hδ] x := by
  have := exists_of_symmetric {u | ∃ a b c : TSet ζ, op hε hζ a b ∈[hδ] x ∧
      u = op hγ hδ (singleton hε (singleton hζ a)) (op hε hζ b c)} hβ ?_
  · obtain ⟨y, hy⟩ := this
    use y
    intro a b c
    rw [hy]
    constructor
    · rintro ⟨a', b', c', h₁, h₂⟩
      simp only [op_inj, singleton_inj] at h₂
      obtain ⟨rfl, rfl, rfl⟩ := h₂
      exact h₁
    · intro h
      exact ⟨a, b, c, h, rfl⟩
  · obtain ⟨S, hS⟩ := exists_support x
    use S ↗ hγ ↗ hβ
    intro ρ hρ
    simp only [Support.smul_scoderiv, Support.scoderiv_inj, ← allPermSderiv_forget'] at hρ
    specialize hS (ρ ↘ hβ ↘ hγ) hρ
    ext z
    constructor
    · rintro ⟨_, ⟨a, b, c, hx, rfl⟩, rfl⟩
      refine ⟨ρ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • a, ρ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • b,
        ρ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • c, ?_, ?_⟩
      · rw [← hS]
        simp only [mem_smul_iff', allPerm_inv_sderiv', smul_op, inv_smul_smul]
        exact hx
      · simp only [smul_op, smul_singleton]
    · rintro ⟨a, b, c, hx, rfl⟩
      rw [Set.mem_smul_set_iff_inv_smul_mem]
      refine ⟨ρ⁻¹ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • a, ρ⁻¹ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • b,
        ρ⁻¹ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • c, ?_, ?_⟩
      · rw [smul_eq_iff_eq_inv_smul] at hS
        rw [hS, mem_smul_iff']
        simp only [inv_inv, allPerm_inv_sderiv', smul_op, smul_inv_smul]
        exact hx
      · simp only [smul_op, allPerm_inv_sderiv', smul_singleton]

theorem exists_cross (x : TSet γ) :
    ∃ y : TSet α, ∀ a, a ∈[hβ] y ↔ ∃ b c, a = op hγ hδ b c ∧ c ∈[hδ] x

:= by
  have := exists_of_symmetric {a | ∃ b c, a = op hγ hδ b c ∧ c ∈[hδ] x} hβ ?_
  · obtain ⟨y, hy⟩ := this
    use y
    intro a
    rw [hy]
    rfl
  · obtain ⟨S, hS⟩ := exists_support x
    use S ↗ hγ ↗ hβ
    intro ρ hρ
    simp only [Support.smul_scoderiv, Support.scoderiv_inj, ← allPermSderiv_forget'] at hρ
    specialize hS (ρ ↘ hβ ↘ hγ) hρ
    ext z
    constructor
    · rintro ⟨_, ⟨a, b, rfl, hab⟩, rfl⟩
      refine ⟨ρ ↘ hβ ↘ hγ ↘ hδ • a, ρ ↘ hβ ↘ hγ ↘ hδ • b, ?_, ?_⟩
      · simp only [smul_op]
      · rw [← hS]
        simp only [mem_smul_iff', allPerm_inv_sderiv', inv_smul_smul]
        exact hab
    · rintro ⟨a, b, rfl, hab⟩
      rw [Set.mem_smul_set_iff_inv_smul_mem]
      refine ⟨ρ⁻¹ ↘ hβ ↘ hγ ↘ hδ • a, ρ⁻¹ ↘ hβ ↘ hγ ↘ hδ • b, ?_, ?_⟩
      · simp only [smul_op, allPerm_inv_sderiv']
      · rw [smul_eq_iff_eq_inv_smul] at hS
        rw [hS]
        simp only [allPerm_inv_sderiv', mem_smul_iff', inv_inv, smul_inv_smul]
        exact hab


import ConNF.Model.RaiseStrong

/-!
# New file

In this file...

## Main declarations

* `ConNF.foo`: Something new.
-/

noncomputable section
universe u

open Cardinal Ordinal

open scoped Pointwise

namespace ConNF

variable [Params.{u}]

/-- A redefinition of the derivative of allowable permutations that is invariant of level,
but still has nice definitional properties. -/
@[default_instance 200]
instance {β γ : TypeIndex} : Derivative (AllPerm β) (AllPerm γ) β γ where
  deriv ρ A :=
    A.recSderiv
    (motive := λ (δ : TypeIndex) (A : β ↝ δ) ↦
      letI : Level := ⟨δ.recBotCoe (Nonempty.some inferInstance) id⟩
      letI : LeLevel δ := ⟨δ.recBotCoe (λ _ ↦ bot_le) (λ _ h ↦ WithBot.coe_le_coe.mpr h.le)
        (show δ.recBotCoe (Nonempty.some inferInstance) id = Level.α from rfl)⟩
      AllPerm δ)
    ρ (λ δ ε A h ρ ↦
      letI : Level := ⟨δ.recBotCoe (Nonempty.some inferInstance) id⟩
      letI : LeLevel δ := ⟨δ.recBotCoe (λ _ ↦ bot_le) (λ _ h ↦ WithBot.coe_le_coe.mpr h.le)
        (show δ.recBotCoe (Nonempty.some inferInstance) id = Level.α from rfl)⟩
      letI : LeLevel ε := ⟨h.le.trans LeLevel.elim⟩
      PreCoherentData.allPermSderiv h ρ)

@[simp]
theorem allPerm_deriv_nil' {β : TypeIndex}
    (ρ : AllPerm β) :
    ρ ⇘ (.nil : β ↝ β) = ρ


import ConNF.Model.Result

/-!
# New file

In this file...

## Main declarations

* `ConNF.foo`: Something new.
-/

noncomputable section
universe u

open Cardinal Ordinal ConNF.TSet

namespace ConNF

variable [Params.{u}] {α β γ δ ε ζ : Λ} (hβ : (β : TypeIndex) < α) (hγ : (γ : TypeIndex) < β)
  (hδ : (δ : TypeIndex) < γ) (hε : (ε : TypeIndex) < δ) (hζ : (ζ : TypeIndex) < ε)

def union (x y : TSet α) : TSet α :=
  (xᶜ' ⊓' yᶜ')ᶜ'

notation:68 x:68 " ⊔[" h "] " y:68 => _root_.ConNF.union h x y
notation:68 x:68 " ⊔' " y:68 => x ⊔[by assumption] y

@[simp]
theorem mem_union_iff (x y : TSet α) :
    ∀ z : TSet β, z ∈' x ⊔' y ↔ z ∈' x ∨ z ∈' y := by
  rw [union]
  intro z
  rw [mem_compl_iff, mem_inter_iff, mem_compl_iff, mem_compl_iff, or_iff_not_and_not]

def higherIndex (α : Λ) : Λ :=
  (exists_gt α).choose

theorem lt_higherIndex {α : Λ} :
    (α : TypeIndex) < higherIndex α :=
  WithBot.coe_lt_coe.mpr (exists_gt α).choose_spec

theorem tSet_nonempty (h : ∃ β : Λ, (β : TypeIndex) < α) : Nonempty (TSet α) := by
  obtain ⟨α', hα⟩ := h
  constructor
  apply typeLower lt_higherIndex lt_higherIndex lt_higherIndex hα
  apply cardinalOne lt_higherIndex lt_higherIndex

def empty : TSet α :=
  (tSet_nonempty ⟨β, hβ⟩).some ⊓' (tSet_nonempty ⟨β, hβ⟩).someᶜ'

@[simp]
theorem mem_empty_iff :
    ∀ x : TSet β, ¬x ∈' empty hβ := by
  intro x
  rw [empty, mem_inter_iff, mem_compl_iff]
  exact and_not_self

def univ : TSet α :=
  (empty hβ)ᶜ'

@[simp]
theorem mem_univ_iff :
    ∀ x : TSet β, x ∈' univ hβ := by
  intro x
  simp only [univ, mem_compl_iff, mem_empty_iff, not_false_eq_true]

/-- The set of all ordered pairs. -/
def orderedPairs : TSet α :=
  vCross hβ hγ hδ (univ hδ)

@[simp]
theorem mem_orderedPairs_iff (x : TSet β) :
    x ∈' orderedPairs hβ hγ hδ ↔ ∃ a b, x = ⟨a, b⟩'

:= by
  simp only [orderedPairs, vCross_spec, mem_univ_iff, and_true]


import ConNF.ModelData.PathEnumeration

/-!
# Supports

In this file, we define the notion of a support.

## Main declarations

* `ConNF.BaseSupport`: The type of supports of atoms.
* `ConNF.Support`: The type of supports of objects of arbitrary type indices.
-/

universe u

open Cardinal

namespace ConNF

variable [Params.{u}]

/-!
## Base supports
-/

structure BaseSupport where
  atoms : Enumeration Atom
  nearLitters : Enumeration NearLitter

namespace BaseSupport

instance : SuperA BaseSupport (Enumeration Atom) where
  superA := atoms

instance : SuperN BaseSupport (Enumeration NearLitter) where
  superN := nearLitters

@[simp]
theorem mk_atoms {a : Enumeration Atom} {N : Enumeration NearLitter} :
    (BaseSupport.mk a N)ᴬ = a :=
  rfl

@[simp]
theorem mk_nearLitters {a : Enumeration Atom} {N : Enumeration NearLitter} :
    (BaseSupport.mk a N)ᴺ = N :=
  rfl

theorem atoms_congr {S T : BaseSupport} (h : S = T) :
    Sᴬ = Tᴬ :=
  h ▸ rfl

theorem nearLitters_congr {S T : BaseSupport} (h : S = T) :
    Sᴺ = Tᴺ :=
  h ▸ rfl

@[ext]
theorem ext {S T : BaseSupport} (h₁ : Sᴬ = Tᴬ) (h₂ : Sᴺ = Tᴺ) : S = T := by
  obtain ⟨SA, SN⟩ := S
  obtain ⟨TA, TN⟩ := T
  cases h₁
  cases h₂
  rfl

instance : SMul BasePerm BaseSupport where
  smul π S := ⟨π • Sᴬ, π • Sᴺ⟩

@[simp]
theorem smul_atoms (π : BasePerm) (S : BaseSupport) :
    (π • S)ᴬ = π • Sᴬ :=
  rfl

@[simp]
theorem smul_nearLitters (π : BasePerm) (S : BaseSupport) :
    (π • S)ᴺ = π • Sᴺ :=
  rfl

@[simp]
theorem smul_atoms_eq_of_smul_eq {π : BasePerm} {S : BaseSupport}
    (h : π • S = S) :
    π • Sᴬ = Sᴬ := by
  rw [← smul_atoms, h]

@[simp]
theorem smul_nearLitters_eq_of_smul_eq {π : BasePerm} {S : BaseSupport}
    (h : π • S = S) :
    π • Sᴺ = Sᴺ := by
  rw [← smul_nearLitters, h]

instance : MulAction BasePerm BaseSupport where
  one_smul S := by
    apply ext
    · rw [smul_atoms, one_smul]
    · rw [smul_nearLitters, one_smul]
  mul_smul π₁ π₂ S := by
    apply ext
    · rw [smul_atoms, smul_atoms, smul_atoms, mul_smul]
    · rw [smul_nearLitters, smul_nearLitters, smul_nearLitters, mul_smul]

theorem smul_eq_smul_iff (π₁ π₂ : BasePerm) (S : BaseSupport) :
    π₁ • S = π₂ • S ↔ (∀ a ∈ Sᴬ, π₁ • a = π₂ • a) ∧ (∀ N ∈ Sᴺ, π₁ • N = π₂ • N) := by
  constructor
  · intro h
    constructor
    · rintro a ⟨i, ha⟩
      have := congr_arg (·ᴬ.rel i (π₁ • a)) h
      simp only [smul_atoms, Enumeration.smul_rel, inv_smul_smul, eq_iff_iff] at this
      have := Sᴬ.rel_coinjective.coinjective ha (this.mp ha)
      rw [eq_inv_smul_iff] at this
      rw [this]
    · rintro N ⟨i, hN⟩
      have := congr_arg (·ᴺ.rel i (π₁ • N)) h
      simp only [smul_nearLitters, Enumeration.smul_rel, inv_smul_smul, eq_iff_iff] at this
      have := Sᴺ.rel_coinjective.coinjective hN (this.mp hN)
      rw [eq_inv_smul_iff] at this
      rw [this]
  · intro h
    ext : 2
    · rfl
    · ext i a : 3
      rw [smul_atoms, smul_atoms, Enumeration.smul_rel, Enumeration.smul_rel]
      constructor
      · intro ha
        have := h.1 _ ⟨i, ha⟩
        rw [smul_inv_smul, ← inv_smul_eq_iff] at this
        rwa [this]
      · intro ha
        have := h.1 _ ⟨i, ha⟩
        rw [smul_inv_smul, smul_eq_iff_eq_inv_smul] at this
        rwa [← this]
    · rfl
    · ext i a : 3
      rw [smul_nearLitters, smul_nearLitters, Enumeration.smul_rel, Enumeration.smul_rel]
      constructor
      · intro hN
        have := h.2 _ ⟨i, hN⟩
        rw [smul_inv_smul, ← inv_smul_eq_iff] at this
        rwa [this]
      · intro hN
        have := h.2 _ ⟨i, hN⟩
        rw [smul_inv_smul, smul_eq_iff_eq_inv_smul] at this
        rwa [← this]

theorem smul_eq_iff (π : BasePerm) (S : BaseSupport) :
    π • S = S ↔ (∀ a ∈ Sᴬ, π • a = a) ∧ (∀ N ∈ Sᴺ, π • N = N) := by
  have := smul_eq_smul_iff π 1 S
  simp only [one_smul] at this
  exact this

noncomputable instance : Add BaseSupport where
  add S T := ⟨Sᴬ + Tᴬ, Sᴺ + Tᴺ⟩

@[simp]
theorem add_atoms (S T : BaseSupport) :
    (S + T)ᴬ = Sᴬ + Tᴬ :=
  rfl

@[simp]
theorem add_nearLitters (S T : BaseSupport) :
    (S + T)ᴺ = Sᴺ + Tᴺ :=
  rfl

end BaseSupport

def baseSupportEquiv : BaseSupport ≃ Enumeration Atom × Enumeration NearLitter where
  toFun S := (Sᴬ, Sᴺ)
  invFun S := ⟨S.1, S.2⟩
  left_inv _ := rfl
  right_inv _ := rfl

theorem card_baseSupport : #BaseSupport = #μ := by
  rw [Cardinal.eq.mpr ⟨baseSupportEquiv⟩, mk_prod, lift_id, lift_id,
    card_enumeration_eq card_atom, card_enumeration_eq card_nearLitter, mul_eq_self aleph0_lt_μ.le]

/-!
## Structural supports
-/

structure Support (α : TypeIndex) where
  atoms : Enumeration (α ↝ ⊥ × Atom)
  nearLitters : Enumeration (α ↝ ⊥ × NearLitter)

namespace Support

variable {α β : TypeIndex}

instance : SuperA (Support α) (Enumeration (α ↝ ⊥ × Atom)) where
  superA := atoms

instance : SuperN (Support α) (Enumeration (α ↝ ⊥ × NearLitter)) where
  superN := nearLitters

@[simp]
theorem mk_atoms (E : Enumeration (α ↝ ⊥ × Atom)) (F : Enumeration (α ↝ ⊥ × NearLitter)) :
    (⟨E, F⟩ : Support α)ᴬ = E :=
  rfl

@[simp]
theorem mk_nearLitters (E : Enumeration (α ↝ ⊥ × Atom)) (F : Enumeration (α ↝ ⊥ × NearLitter)) :
    (⟨E, F⟩ : Support α)ᴺ = F :=
  rfl

instance : Derivative (Support α) (Support β) α β where
  deriv S A := ⟨Sᴬ ⇘ A, Sᴺ ⇘ A⟩

instance : Coderivative (Support β) (Support α) α β where
  coderiv S A := ⟨Sᴬ ⇗ A, Sᴺ ⇗ A⟩

instance : BotDerivative (Support α) BaseSupport α where
  botDeriv S A := ⟨Sᴬ ⇘. A, Sᴺ ⇘. A⟩
  botSderiv S := ⟨Sᴬ ↘., Sᴺ ↘.⟩
  botDeriv_single S h := by dsimp only; rw [botDeriv_single, botDeriv_single]

@[simp]
theorem deriv_atoms {α β : TypeIndex} (S : Support α) (A : α ↝ β) :
    Sᴬ ⇘ A = (S ⇘ A)ᴬ :=
  rfl

@[simp]
theorem deriv_nearLitters {α β : TypeIndex} (S : Support α) (A : α ↝ β) :
    Sᴺ ⇘ A = (S ⇘ A)ᴺ :=
  rfl

@[simp]
theorem sderiv_atoms {α β : TypeIndex} (S : Support α) (h : β < α) :
    Sᴬ ↘ h = (S ↘ h)ᴬ :=
  rfl

@[simp]
theorem sderiv_nearLitters {α β : TypeIndex} (S : Support α) (h : β < α) :
    Sᴺ ↘ h = (S ↘ h)ᴺ :=
  rfl

@[simp]
theorem coderiv_atoms {α β : TypeIndex} (S : Support β) (A : α ↝ β) :
    Sᴬ ⇗ A = (S ⇗ A)ᴬ :=
  rfl

@[simp]
theorem coderiv_nearLitters {α β : TypeIndex} (S : Support β) (A : α ↝ β) :
    Sᴺ ⇗ A = (S ⇗ A)ᴺ :=
  rfl

@[simp]
theorem scoderiv_atoms {α β : TypeIndex} (S : Support β) (h : β < α) :
    Sᴬ ↗ h = (S ↗ h)ᴬ :=
  rfl

@[simp]
theorem scoderiv_nearLitters {α β : TypeIndex} (S : Support β) (h : β < α) :
    Sᴺ ↗ h = (S ↗ h)ᴺ :=
  rfl

@[simp]
theorem derivBot_atoms {α : TypeIndex} (S : Support α) (A : α ↝ ⊥) :
    Sᴬ ⇘. A = (S ⇘. A)ᴬ :=
  rfl

@[simp]
theorem derivBot_nearLitters {α : TypeIndex} (S : Support α) (A : α ↝ ⊥) :
    Sᴺ ⇘. A = (S ⇘. A)ᴺ :=
  rfl

theorem ext' {S T : Support α} (h₁ : Sᴬ = Tᴬ) (h₂ : Sᴺ = Tᴺ) : S = T := by
  obtain ⟨SA, SN⟩ := S
  obtain ⟨TA, TN⟩ := T
  cases h₁
  cases h₂
  rfl

@[ext]
theorem ext {S T : Support α} (h : ∀ A, S ⇘. A = T ⇘. A) : S = T := by
  obtain ⟨SA, SN⟩ := S
  obtain ⟨TA, TN⟩ := T
  rw [mk.injEq]
  constructor
  · apply Enumeration.ext_path
    intro A
    exact BaseSupport.atoms_congr (h A)
  · apply Enumeration.ext_path
    intro A
    exact BaseSupport.nearLitters_congr (h A)

@[simp]
theorem deriv_derivBot {α : TypeIndex} (S : Support α)
    (A : α ↝ β) (B : β ↝ ⊥) :
    S ⇘ A ⇘. B = S ⇘. (A ⇘ B) :=
  rfl

@[simp]
theorem coderiv_deriv_eq {α β : TypeIndex} (S : Support β) (A : α ↝ β) :
    S ⇗ A ⇘ A = S :=
  ext' (Sᴬ.coderiv_deriv_eq A) (Sᴺ.coderiv_deriv_eq A)

theorem eq_of_atom_mem_scoderiv_botDeriv {α β : TypeIndex} {S : Support β} {A : α ↝ ⊥}
    {h : β < α} {a : Atom} (ha : a ∈ (S ↗ h ⇘. A)ᴬ) :
    ∃ B : β ↝ ⊥, A = B ↗ h :=
  Enumeration.eq_of_mem_scoderiv_botDeriv ha

theorem eq_of_nearLitter_mem_scoderiv_botDeriv {α β : TypeIndex} {S : Support β} {A : α ↝ ⊥}
    {h : β < α} {N : NearLitter} (hN : N ∈ (S ↗ h ⇘. A)ᴺ) :
    ∃ B : β ↝ ⊥, A = B ↗ h :=
  Enumeration.eq_of_mem_scoderiv_botDeriv hN

@[simp]
theorem scoderiv_botDeriv_eq {α β : TypeIndex} (S : Support β) (A : β ↝ ⊥) (h : β < α) :
    S ↗ h ⇘. (A ↗ h) = S ⇘. A

:=
  BaseSupport.ext (Enumeration.scoderiv_botDeriv_eq _ _ _) (Enumeration.scoderiv_botDeriv_eq _ _ _)


import ConNF.Model.Externalise

/-!
# New file

In this file...

## Main declarations

* `ConNF.foo`: Something new.
-/

noncomputable section
universe u

open Cardinal Ordinal

namespace ConNF

variable [Params.{u}] {β γ : Λ} {hγ : (γ : TypeIndex) < β}

namespace Support

theorem not_mem_scoderiv_botDeriv (S : Support γ) (N : NearLitter) :
    N ∉ (S ↗ hγ ⇘. (Path.nil ↘.))ᴺ := by
  rintro ⟨i, ⟨A, N'⟩, h₁, h₂⟩
  simp only [Prod.mk.injEq] at h₂
  cases A
  case sderiv δ A hδ _ =>
    simp only [Path.deriv_sderiv] at h₂
    cases A
    case nil => cases h₂.1
    case sderiv ζ A hζ _ =>
      simp only [Path.deriv_sderiv] at h₂
      cases h₂.1

variable [Level] [LtLevel β]

theorem not_mem_strong_botDeriv (S : Support γ) (N : NearLitter) :
    N ∉ ((S ↗ hγ).strong ⇘. (Path.nil ↘.))ᴺ := by
  rintro h
  rw [strong, close_nearLitters, preStrong_nearLitters, Enumeration.mem_add_iff] at h
  obtain h | h := h
  · exact not_mem_scoderiv_botDeriv S N h
  · rw [mem_constrainsNearLitters_nearLitters] at h
    obtain ⟨B, N', hN', h⟩ := h
    cases h using Relation.ReflTransGen.head_induction_on
    case refl => exact not_mem_scoderiv_botDeriv S N hN'
    case head x hx₁ hx₂ _ =>
      obtain ⟨⟨γ, δ, ε, hδ, hε, hδε, A⟩, t, B, hB, hN, ht⟩ := hx₂
      simp only at hB
      cases B
      case nil =>
        cases hB
        obtain ⟨C, N''⟩ := x
        simp only at ht
        cases ht.1
        change _ ∈ t.supportᴺ at hN
        rw [t.support_supports.2 rfl] at hN
        obtain ⟨i, hN⟩ := hN
        cases hN
      case sderiv δ B hδ _ _ =>
        cases B
        case nil => cases hB
        case sderiv ζ B hζ _ _ => cases hB

theorem raise_preStrong' (S : Support α) (hS : S.Strong) (T : Support γ) (ρ : AllPerm β)
    (hγ : (γ : TypeIndex) < β) :
    (S + (ρᵁ • ((T ↗ hγ).strong +
      (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗ LtLevel.elim).PreStrong := by
  apply hS.toPreStrong.add
  constructor
  intro A N hN P t hA ht
  obtain ⟨A, rfl⟩ := eq_of_nearLitter_mem_scoderiv_botDeriv hN
  simp only [scoderiv_botDeriv_eq, add_derivBot, smul_derivBot,
    BaseSupport.add_nearLitters, BaseSupport.smul_nearLitters, interferenceSupport_nearLitters,
    Enumeration.mem_add_iff, Enumeration.mem_smul, Enumeration.not_mem_empty, or_false] at hN
  obtain ⟨δ, ε, ζ, hε, hζ, hεζ, B⟩ := P
  dsimp only at *
  cases A
  case sderiv ζ' A hζ' _ =>
    rw [← Path.coderiv_deriv] at hA
    cases Path.sderiv_index_injective hA
    apply Path.sderiv_left_inj.mp at hA
    cases A
    case nil =>
      cases hA
      cases not_mem_strong_botDeriv T _ hN
    case sderiv ι A hι _ _ =>
      rw [← Path.coderiv_deriv] at hA
      cases Path.sderiv_index_injective hA
      cases hA
      haveI : LtLevel δ := ⟨A.le.trans_lt LtLevel.elim⟩
      haveI : LtLevel ε := ⟨hε.trans LtLevel.elim⟩
      haveI : LtLevel ζ := ⟨hζ.trans LtLevel.elim⟩
      have := (T ↗ hγ).strong_strong.support_le hN ⟨δ, ε, ζ, hε, hζ, hεζ, A⟩
          (ρ⁻¹ ⇘ A ↘ hε • t) rfl ?_
      · simp only [Tangle.smul_support, allPermSderiv_forget, allPermDeriv_forget,
          allPermForget_inv, Tree.inv_deriv, Tree.inv_sderiv] at this
        have := smul_le_smul this (ρᵁ ⇘ A ↘ hε)
        simp only [smul_inv_smul] at this
        apply le_trans this
        intro B
        constructor
        · intro a ha
          simp only [smul_derivBot, Tree.sderiv_apply, Tree.deriv_apply, Path.deriv_scoderiv,
            deriv_derivBot, Enumeration.mem_smul] at ha
          rw [deriv_derivBot, ← Path.deriv_scoderiv, Path.coderiv_deriv', scoderiv_botDeriv_eq,]
          simp only [Path.deriv_scoderiv, add_derivBot, smul_derivBot,
            BaseSupport.add_atoms, BaseSupport.smul_atoms, Enumeration.mem_add_iff,
            Enumeration.mem_smul]
          exact Or.inl ha
        · intro N hN
          simp only [smul_derivBot, Tree.sderiv_apply, Tree.deriv_apply, Path.deriv_scoderiv,
            deriv_derivBot, Enumeration.mem_smul] at hN
          rw [deriv_derivBot, ← Path.deriv_scoderiv, Path.coderiv_deriv', scoderiv_botDeriv_eq,]
          simp only [Path.deriv_scoderiv, add_derivBot, smul_derivBot,
            BaseSupport.add_nearLitters, BaseSupport.smul_nearLitters, Enumeration.mem_add_iff,
            Enumeration.mem_smul]
          exact Or.inl hN
      · rw [← smul_fuzz hε hζ hεζ, ← ht]
        simp only [Path.botSderiv_coe_eq, BasePerm.smul_nearLitter_litter, allPermDeriv_forget,
          allPermForget_inv, Tree.inv_deriv, Tree.inv_sderiv, Tree.inv_sderivBot]
        rfl

theorem raise_closed' (S : Support α) (hS : S.Strong) (T : Support γ) (ρ : AllPerm β)
    (hγ : (γ : TypeIndex) < β)
    (hρ : ρᵁ • (S ↘ LtLevel.elim : Support β) = S ↘ LtLevel.elim) :
    (S + (ρᵁ • ((T ↗ hγ).strong +
      (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗ LtLevel.elim).Closed := by
  constructor
  intro A
  constructor
  intro N₁ N₂ hN₁ hN₂ a ha
  simp only [add_derivBot, BaseSupport.add_nearLitters, Enumeration.mem_add_iff,
    BaseSupport.add_atoms] at hN₁ hN₂ ⊢
  obtain hN₁ | hN₁ := hN₁
  · obtain hN₂ | hN₂ := hN₂
    · exact Or.inl ((hS.closed A).interference_subset hN₁ hN₂ a ha)
    · obtain ⟨B, rfl⟩ := eq_of_nearLitter_mem_scoderiv_botDeriv hN₂
      simp only [smul_add, scoderiv_botDeriv_eq, add_derivBot, smul_derivBot,
        BaseSupport.add_nearLitters, BaseSupport.smul_nearLitters, Enumeration.mem_add_iff,
        Enumeration.mem_smul, BaseSupport.add_atoms, BaseSupport.smul_atoms] at hN₁ hN₂ ⊢
      refine Or.inr (Or.inr ?_)
      rw [mem_interferenceSupport_atoms]
      refine ⟨(ρᵁ B)⁻¹ • N₁, ?_, (ρᵁ B)⁻¹ • N₂, ?_, ?_⟩
      · simp only [add_derivBot, BaseSupport.add_nearLitters, Enumeration.mem_add_iff]
        rw [← Enumeration.mem_smul, ← BaseSupport.smul_nearLitters, ← smul_derivBot, hρ]
        exact Or.inl hN₁
      · simp only [add_derivBot, BaseSupport.add_nearLitters, Enumeration.mem_add_iff]
        simp only [interferenceSupport_nearLitters, Enumeration.not_mem_empty, or_false] at hN₂
        exact Or.inr hN₂
      · rw [← BasePerm.smul_interference]
        exact Set.smul_mem_smul_set ha
  · obtain ⟨B, rfl⟩ := eq_of_nearLitter_mem_scoderiv_botDeriv hN₁
    simp only [smul_add, scoderiv_botDeriv_eq, add_derivBot, smul_derivBot,
      BaseSupport.add_nearLitters, BaseSupport.smul_nearLitters, Enumeration.mem_add_iff,
      Enumeration.mem_smul, BaseSupport.add_atoms, BaseSupport.smul_atoms] at hN₁ hN₂ ⊢
    refine Or.inr (Or.inr ?_)
    rw [mem_interferenceSupport_atoms]
    refine ⟨(ρᵁ B)⁻¹ • N₁, ?_, (ρᵁ B)⁻¹ • N₂, ?_, ?_⟩
    · simp only [add_derivBot, BaseSupport.add_nearLitters, Enumeration.mem_add_iff]
      simp only [interferenceSupport_nearLitters, Enumeration.not_mem_empty, or_false] at hN₁
      exact Or.inr hN₁
    · simp only [add_derivBot, BaseSupport.add_nearLitters, Enumeration.mem_add_iff]
      simp only [interferenceSupport_nearLitters, Enumeration.not_mem_empty, or_false] at hN₂
      obtain hN₂ | hN₂ := hN₂
      · rw [← Enumeration.mem_smul, ← BaseSupport.smul_nearLitters, ← smul_derivBot, hρ]
        exact Or.inl hN₂
      · exact Or.inr hN₂
    · rw [← BasePerm.smul_interference]
      exact Set.smul_mem_smul_set ha

theorem raise_strong' (S : Support α) (hS : S.Strong) (T : Support γ) (ρ : AllPerm β)
    (hγ : (γ : TypeIndex) < β)
    (hρ : ρᵁ • (S ↘ LtLevel.elim : Support β) = S ↘ LtLevel.elim) :
    (S + (ρᵁ • ((T ↗ hγ).strong +
      (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗ LtLevel.elim).Strong :=
  ⟨raise_preStrong' S hS T ρ hγ, raise_closed' S hS T ρ hγ hρ⟩

theorem convAtoms_injective_of_fixes {S : Support α} {T : Support γ}
    {ρ₁ ρ₂ : AllPerm β} {hγ : (γ : TypeIndex) < β}
    (hρ₁ : ρ₁ᵁ • (S ↘ LtLevel.elim : Support β) = S ↘ LtLevel.elim)
    (hρ₂ : ρ₂ᵁ • (S ↘ LtLevel.elim : Support β) = S ↘ LtLevel.elim)
    (A : ↑α ↝ ⊥) :
    (convAtoms
      (S + (ρ₁ᵁ • ((T ↗ hγ).strong + (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗
        LtLevel.elim)
      (S + (ρ₂ᵁ • ((T ↗ hγ).strong + (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗
        LtLevel.elim) A).Injective := by
  rw [Support.smul_eq_iff] at hρ₁ hρ₂
  constructor
  rintro a₁ a₂ a₃ ⟨i, hi₁, hi₂⟩ ⟨j, hj₁, hj₂⟩
  simp only [add_derivBot, BaseSupport.add_atoms, Rel.inv_apply,
    Enumeration.rel_add_iff] at hi₁ hi₂ hj₁ hj₂
  obtain hi₁ | ⟨i, rfl, hi₁⟩ := hi₁
  · obtain hi₂ | ⟨i', rfl, _⟩ := hi₂
    swap
    · have := Enumeration.lt_bound _ _ ⟨_, hi₁⟩
      simp only [add_lt_iff_neg_left] at this
      cases (κ_zero_le i').not_lt this
    cases (Enumeration.rel_coinjective _).coinjective hi₁ hi₂
    obtain hj₁ | ⟨j, rfl, hj₁⟩ := hj₁
    · obtain hj₂ | ⟨j', rfl, _⟩ := hj₂
      · exact (Enumeration.rel_coinjective _).coinjective hj₂ hj₁
      · have := Enumeration.lt_bound _ _ ⟨_, hj₁⟩
        simp only [add_lt_iff_neg_left] at this
        cases (κ_zero_le j').not_lt this
    · obtain hj₂ | hj₂ := hj₂
      · have := Enumeration.lt_bound _ _ ⟨_, hj₂⟩
        simp only [add_lt_iff_neg_left] at this
        cases (κ_zero_le j).not_lt this
      · simp only [add_right_inj, exists_eq_left] at hj₂
        obtain ⟨B, rfl⟩ := eq_of_atom_mem_scoderiv_botDeriv ⟨j, hj₁⟩
        simp only [scoderiv_botDeriv_eq, smul_derivBot, add_derivBot, BaseSupport.smul_atoms,
          BaseSupport.add_atoms, Enumeration.smul_rel, add_right_inj, exists_eq_left] at hj₁ hj₂
        have := (Enumeration.rel_coinjective _).coinjective hj₁ hj₂
        rw [← (hρ₂ B).1 a₁ ⟨_, hi₁⟩, inv_smul_smul, inv_smul_eq_iff, (hρ₁ B).1 a₁ ⟨_, hi₁⟩] at this
        exact this.symm
  · obtain ⟨B, rfl⟩ := eq_of_atom_mem_scoderiv_botDeriv ⟨i, hi₁⟩
    simp only [scoderiv_botDeriv_eq, smul_derivBot, add_derivBot, BaseSupport.smul_atoms,
      BaseSupport.add_atoms, Enumeration.smul_rel, add_right_inj, exists_eq_left] at hi₁ hi₂ hj₁ hj₂
    obtain hi₂ | hi₂ := hi₂
    · have := Enumeration.lt_bound _ _ ⟨_, hi₂⟩
      simp only [add_lt_iff_neg_left] at this
      cases (κ_zero_le i).not_lt this
    have hi := (Enumeration.rel_coinjective _).coinjective hi₁ hi₂
    suffices hj : (ρ₁ᵁ B)⁻¹ • a₂ = (ρ₂ᵁ B)⁻¹ • a₃ by
      rwa [← hj, smul_left_cancel_iff] at hi
    obtain hj₁ | ⟨j, rfl, hj₁⟩ := hj₁
    · obtain hj₂ | ⟨j', rfl, _⟩ := hj₂
      · rw [← (hρ₁ B).1 a₂ ⟨_, hj₁⟩, ← (hρ₂ B).1 a₃ ⟨_, hj₂⟩, inv_smul_smul, inv_smul_smul]
        exact (Enumeration.rel_coinjective _).coinjective hj₁ hj₂
      · have := Enumeration.lt_bound _ _ ⟨_, hj₁⟩
        simp only [add_lt_iff_neg_left] at this
        cases (κ_zero_le j').not_lt this
    · obtain hj₂ | hj₂ := hj₂
      · have := Enumeration.lt_bound _ _ ⟨_, hj₂⟩
        simp only [add_lt_iff_neg_left] at this
        cases (κ_zero_le j).not_lt this
      · simp only [add_right_inj, exists_eq_left] at hj₂
        exact (Enumeration.rel_coinjective _).coinjective hj₁ hj₂

theorem atomMemRel_le_of_fixes {S : Support α} {T : Support γ}
    {ρ₁ ρ₂ : AllPerm β} {hγ : (γ : TypeIndex) < β}
    (hρ₁ : ρ₁ᵁ • (S ↘ LtLevel.elim : Support β) = S ↘ LtLevel.elim)
    (hρ₂ : ρ₂ᵁ • (S ↘ LtLevel.elim : Support β) = S ↘ LtLevel.elim)
    (A : ↑α ↝ ⊥) :
    atomMemRel (S + (ρ₁ᵁ • ((T ↗ hγ).strong +
      (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗ LtLevel.elim) A ≤
    atomMemRel (S + (ρ₂ᵁ • ((T ↗ hγ).strong +
      (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗ LtLevel.elim) A := by
  rw [Support.smul_eq_iff] at hρ₁ hρ₂
  rintro i j ⟨N, hN, a, haN, ha⟩
  simp only [add_derivBot, BaseSupport.add_atoms, Rel.inv_apply, Enumeration.rel_add_iff,
    BaseSupport.add_nearLitters] at ha hN
  obtain hN | ⟨i, rfl, hi⟩ := hN
  · obtain ha | ⟨j, rfl, hj⟩ := ha
    · exact ⟨N, Or.inl hN, a, haN, Or.inl ha⟩
    · obtain ⟨B, rfl⟩ := eq_of_atom_mem_scoderiv_botDeriv ⟨j, hj⟩
      simp only [scoderiv_botDeriv_eq, smul_derivBot, add_derivBot, BaseSupport.smul_atoms,
        BaseSupport.add_atoms, Enumeration.smul_rel] at hj hN
      refine ⟨N, Or.inl hN, ρ₂ᵁ B • (ρ₁ᵁ B)⁻¹ • a, ?_, ?_⟩
      · dsimp only
        rw [← (hρ₂ B).2 N ⟨_, hN⟩, BasePerm.smul_nearLitter_atoms, Set.smul_mem_smul_set_iff]
        have := (hρ₁ B).2 N ⟨_, hN⟩
        rw [smul_eq_iff_eq_inv_smul] at this
        rwa [this, BasePerm.smul_nearLitter_atoms, Set.smul_mem_smul_set_iff]
      · rw [Rel.inv_apply, add_derivBot, BaseSupport.add_atoms, Enumeration.rel_add_iff]
        simp only [add_right_inj, scoderiv_botDeriv_eq, smul_derivBot, add_derivBot,
          BaseSupport.smul_atoms, BaseSupport.add_atoms, Enumeration.smul_rel, inv_smul_smul,
          exists_eq_left]
        exact Or.inr hj
  · obtain ⟨B, rfl⟩ := eq_of_nearLitter_mem_scoderiv_botDeriv ⟨i, hi⟩
    simp only [scoderiv_botDeriv_eq, smul_derivBot, add_derivBot, BaseSupport.smul_atoms,
      BaseSupport.add_atoms, Enumeration.smul_rel] at hi ha
    obtain ha | ⟨j, rfl, hj⟩ := ha
    · refine ⟨ρ₂ᵁ B • (ρ₁ᵁ B)⁻¹ • N, ?_, a, ?_, Or.inl ha⟩
      · rw [add_derivBot, BaseSupport.add_nearLitters, Enumeration.rel_add_iff]
        simp only [add_right_inj, scoderiv_botDeriv_eq, smul_derivBot, add_derivBot,
          BaseSupport.smul_nearLitters, BaseSupport.add_nearLitters, Enumeration.smul_rel,
          inv_smul_smul, exists_eq_left]
        exact Or.inr hi
      · dsimp only
        rw [← (hρ₂ B).1 a ⟨_, ha⟩, BasePerm.smul_nearLitter_atoms, Set.smul_mem_smul_set_iff]
        have := (hρ₁ B).1 a ⟨_, ha⟩
        rw [smul_eq_iff_eq_inv_smul] at this
        rwa [this, BasePerm.smul_nearLitter_atoms, Set.smul_mem_smul_set_iff]
    · refine ⟨ρ₂ᵁ B • (ρ₁ᵁ B)⁻¹ • N, ?_, ρ₂ᵁ B • (ρ₁ᵁ B)⁻¹ • a, ?_, ?_⟩
      · rw [add_derivBot, BaseSupport.add_nearLitters, Enumeration.rel_add_iff]
        simp only [add_right_inj, scoderiv_botDeriv_eq, smul_derivBot, add_derivBot,
          BaseSupport.smul_nearLitters, BaseSupport.add_nearLitters, Enumeration.smul_rel,
          inv_smul_smul, exists_eq_left]
        exact Or.inr hi
      · simp only [BasePerm.smul_nearLitter_atoms, Set.smul_mem_smul_set_iff]
        exact haN
      · rw [Rel.inv_apply, add_derivBot, BaseSupport.add_atoms, Enumeration.rel_add_iff]
        simp only [add_right_inj, scoderiv_botDeriv_eq, smul_derivBot, add_derivBot,
          BaseSupport.smul_atoms, BaseSupport.add_atoms, Enumeration.smul_rel, inv_smul_smul,
          exists_eq_left]
        exact Or.inr hj

theorem convNearLitters_cases {S : Support α} {T : Support γ}
    {ρ₁ ρ₂ : AllPerm β} {hγ : (γ : TypeIndex) < β}
    {A : α ↝ ⊥} {N₁ N₂ : NearLitter} :
  convNearLitters
    (S + (ρ₁ᵁ • ((T ↗ hγ).strong +
      (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗ LtLevel.elim)
    (S + (ρ₂ᵁ • ((T ↗ hγ).strong +
      (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗ LtLevel.elim) A N₁ N₂ →
    N₁ = N₂ ∧ N₁ ∈ (S ⇘. A)ᴺ ∨
    ∃ B : β ↝ ⊥, A = B ↗ LtLevel.elim ∧ (ρ₁ᵁ B)⁻¹ • N₁ = (ρ₂ᵁ B)⁻¹ • N₂ ∧
      (ρ₁ᵁ B)⁻¹ • N₁ ∈ (((T ↗ hγ).strong +
        (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport) ⇘. B)ᴺ := by
  rintro ⟨i, hN₁, hN₂⟩
  simp only [add_derivBot, BaseSupport.add_nearLitters, Rel.inv_apply,
    Enumeration.rel_add_iff] at hN₁ hN₂
  obtain hN₁ | ⟨i, rfl, hN₁⟩ := hN₁
  · obtain hN₂ | ⟨i, rfl, hN₂⟩ := hN₂
    swap
    · have := Enumeration.lt_bound _ _ ⟨_, hN₁⟩
      simp only [add_lt_iff_neg_left] at this
      cases (κ_zero_le i).not_lt this
    exact Or.inl ⟨(Enumeration.rel_coinjective _).coinjective hN₁ hN₂, _, hN₁⟩
  · obtain ⟨B, rfl⟩ := eq_of_nearLitter_mem_scoderiv_botDeriv ⟨i, hN₁⟩
    simp only [scoderiv_botDeriv_eq, smul_derivBot, add_derivBot, BaseSupport.smul_nearLitters,
      BaseSupport.add_nearLitters, Enumeration.smul_rel, add_right_inj, exists_eq_left] at hN₁ hN₂
    obtain hN₂ | hN₂ := hN₂
    · have := Enumeration.lt_bound _ _ ⟨_, hN₂⟩
      simp only [add_lt_iff_neg_left] at this
      cases (κ_zero_le i).not_lt this
    exact Or.inr ⟨B, rfl, (Enumeration.rel_coinjective _).coinjective hN₁ hN₂, _, hN₁⟩

theorem inflexible_of_inflexible_of_fixes {S : Support α} {T : Support γ}
    {ρ₁ ρ₂ : AllPerm β} {hγ : (γ : TypeIndex) < β}
    (hρ₁ : ρ₁ᵁ • (S ↘ LtLevel.elim : Support β) = S ↘ LtLevel.elim)
    (hρ₂ : ρ₂ᵁ • (S ↘ LtLevel.elim : Support β) = S ↘ LtLevel.elim)
    {A : α ↝ ⊥} {N₁ N₂ : NearLitter} :
    convNearLitters
      (S + (ρ₁ᵁ • ((T ↗ hγ).strong +
        (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗ LtLevel.elim)
      (S + (ρ₂ᵁ • ((T ↗ hγ).strong +
        (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗ LtLevel.elim) A N₁ N₂ →
      ∀ (P : InflexiblePath ↑α) (t : Tangle P.δ), A = P.A ↘ P.hε ↘. → N₁ᴸ = fuzz P.hδε t →
      ∃ ρ : AllPerm P.δ, N₂ᴸ = fuzz P.hδε (ρ • t) := by
  rintro hN ⟨γ, δ, ε, hδ, hε, hδε, A⟩ t hA ht
  haveI : LeLevel γ := ⟨A.le⟩
  haveI : LtLevel δ := ⟨hδ.trans_le LeLevel.elim⟩
  haveI : LtLevel ε := ⟨hε.trans_le LeLevel.elim⟩
  obtain ⟨rfl, _⟩ | ⟨B, rfl, hN'⟩ := convNearLitters_cases hN
  · use 1
    rw [one_smul, ht]
  · clear hN
    cases B
    case sderiv ε B hε' _ =>
      rw [← Path.coderiv_deriv] at hA
      cases Path.sderiv_index_injective hA
      apply Path.sderiv_path_injective at hA
      cases B
      case nil =>
        simp only [Path.botSderiv_coe_eq, add_derivBot, BaseSupport.add_nearLitters,
          interferenceSupport_nearLitters, Enumeration.add_empty] at hN'
        cases not_mem_strong_botDeriv _ _ hN'.2
      case sderiv ζ B hζ _ _ =>
        rw [← Path.coderiv_deriv] at hA
        cases Path.sderiv_index_injective hA
        apply Path.sderiv_path_injective at hA
        dsimp only at hA hζ hε' B t
        cases hA
        use (ρ₂ * ρ₁⁻¹) ⇘ B ↘ hδ
        rw [inv_smul_eq_iff] at hN'
        rw [← smul_fuzz hδ hε hδε, ← ht, hN'.1]
        simp only [allPermDeriv_forget, allPermForget_mul, allPermForget_inv, Tree.mul_deriv,
          Tree.inv_deriv, Tree.mul_sderiv, Tree.inv_sderiv, Tree.mul_sderivBot, Tree.inv_sderivBot,
          Path.botSderiv_coe_eq, BasePerm.smul_nearLitter_litter, mul_smul]
        erw [inv_smul_smul, smul_inv_smul]

theorem atoms_of_inflexible_of_fixes {S : Support α} (hS : S.Strong) {T : Support γ}
    {ρ₁ ρ₂ : AllPerm β} {hγ : (γ : TypeIndex) < β}
    (hρ₁ : ρ₁ᵁ • (S ↘ LtLevel.elim : Support β) = S ↘ LtLevel.elim)
    (hρ₂ : ρ₂ᵁ • (S ↘ LtLevel.elim : Support β) = S ↘ LtLevel.elim)
    (A : α ↝ ⊥) (N₁ N₂ : NearLitter) (P : InflexiblePath ↑α) (t : Tangle P.δ) (ρ : AllPerm P.δ) :
    A = P.A ↘ P.hε ↘. → N₁ᴸ = fuzz P.hδε t → N₂ᴸ = fuzz P.hδε (ρ • t) →
    convNearLitters
      (S + (ρ₁ᵁ • ((T ↗ hγ).strong +
        (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗ LtLevel.elim)
      (S + (ρ₂ᵁ • ((T ↗ hγ).strong +
        (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗ LtLevel.elim) A N₁ N₂ →
    ∀ (B : P.δ ↝ ⊥), ∀ a ∈ (t.support ⇘. B)ᴬ, ∀ (i : κ),
      ((S + (ρ₁ᵁ • ((T ↗ hγ).strong + (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗
        LtLevel.elim) ⇘. (P.A ↘ P.hδ ⇘ B))ᴬ.rel i a →
      ((S + (ρ₂ᵁ • ((T ↗ hγ).strong + (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗
        LtLevel.elim) ⇘. (P.A ↘ P.hδ ⇘ B))ᴬ.rel i (ρᵁ B • a) := by
  rw [Support.smul_eq_iff] at hρ₁ hρ₂
  obtain ⟨γ, δ, ε, hδ, hε, hδε, B⟩ := P
  haveI : LeLevel γ := ⟨B.le⟩
  haveI : LtLevel δ := ⟨hδ.trans_le LeLevel.elim⟩
  haveI : LtLevel ε := ⟨hε.trans_le LeLevel.elim⟩
  dsimp only at t ρ ⊢
  intro hA hN₁ hN₂ hN C a ha i hi
  obtain ⟨rfl, hN'⟩ | ⟨A, rfl, hN₁', hN₂'⟩ := convNearLitters_cases hN
  · have haS := (hS.support_le hN' ⟨γ, δ, ε, hδ, hε, hδε, _⟩ t hA hN₁ _).1 a ha
    rw [hN₂] at hN₁
    have hρt := congr_arg Tangle.support (fuzz_injective hN₁)
    rw [Tangle.smul_support, Support.smul_eq_iff] at hρt
    simp only [add_derivBot, BaseSupport.add_atoms, Enumeration.rel_add_iff] at hi ⊢
    rw [(hρt C).1 a ha]
    obtain hi | ⟨i, rfl, hi⟩ := hi
    · exact Or.inl hi
    · simp only [add_right_inj, exists_eq_left]
      obtain ⟨D, hD⟩ := eq_of_atom_mem_scoderiv_botDeriv ⟨i, hi⟩
      cases B using Path.recScoderiv
      case nil =>
        cases Path.scoderiv_index_injective hD
        cases Path.scoderiv_left_inj.mp hD
        simp only [hD, Path.coderiv_deriv, Path.coderiv_deriv', scoderiv_botDeriv_eq, smul_derivBot,
          add_derivBot, BaseSupport.smul_atoms, BaseSupport.add_atoms, Enumeration.smul_rel] at hi ⊢
        rw [deriv_derivBot, hD] at haS
        rw [← (hρ₂ _).1 a haS, inv_smul_smul]
        rw [← (hρ₁ _).1 a haS, inv_smul_smul] at hi
        exact Or.inr hi
      case scoderiv ζ B hζ' _ =>
        rw [Path.coderiv_deriv, Path.coderiv_deriv'] at hD
        cases Path.scoderiv_index_injective hD
        rw [Path.scoderiv_left_inj] at hD
        cases hD
        simp only [Path.coderiv_deriv, Path.coderiv_deriv', scoderiv_botDeriv_eq, smul_derivBot,
          add_derivBot, BaseSupport.smul_atoms, BaseSupport.add_atoms, Enumeration.smul_rel] at hi ⊢
        rw [deriv_derivBot, Path.coderiv_deriv, Path.coderiv_deriv'] at haS
        rw [← (hρ₂ _).1 a haS, inv_smul_smul]
        rw [← (hρ₁ _).1 a haS, inv_smul_smul] at hi
        exact Or.inr hi
  · simp only [add_derivBot, BaseSupport.add_nearLitters, interferenceSupport_nearLitters,
      Enumeration.add_empty] at hN₂'
    cases A
    case sderiv ζ A hζ' _ =>
      rw [← Path.coderiv_deriv] at hA
      cases Path.sderiv_index_injective hA
      apply Path.sderiv_path_injective at hA
      cases A
      case nil =>
        cases hA
        cases not_mem_strong_botDeriv _ _ hN₂'
      case sderiv ζ A hζ _ _ =>
        rw [← Path.coderiv_deriv] at hA
        cases Path.sderiv_index_injective hA
        apply Path.sderiv_path_injective at hA
        cases hA
        simp only [Path.coderiv_deriv, Path.coderiv_deriv', add_derivBot, scoderiv_botDeriv_eq,
          smul_derivBot, BaseSupport.add_atoms, BaseSupport.smul_atoms] at hi ⊢
        have : N₂ᴸ = (ρ₂ ⇘ A)ᵁ ↘ hζ ↘. • (ρ₁⁻¹ ⇘ A)ᵁ ↘ hζ ↘. • fuzz hδε t := by
          rw [inv_smul_eq_iff] at hN₁'
          rw [hN₁', Path.botSderiv_coe_eq, BasePerm.smul_nearLitter_litter,
            BasePerm.smul_nearLitter_litter, smul_smul, smul_eq_iff_eq_inv_smul,
            mul_inv_rev, inv_inv, mul_smul, ← Tree.inv_apply, ← allPermForget_inv] at hN₁
          rw [hN₁]
          simp only [allPermForget_inv, Tree.inv_apply, allPermDeriv_forget, Tree.inv_deriv,
            Tree.inv_sderiv, Tree.inv_sderivBot]
          rfl
        rw [smul_fuzz hδ hε hδε, smul_fuzz hδ hε hδε] at this
        have := fuzz_injective (hN₂.symm.trans this)
        rw [smul_smul] at this
        rw [t.smul_atom_eq_of_mem_support this ha]
        rw [Enumeration.rel_add_iff] at hi ⊢
        obtain hi | ⟨i, rfl, hi⟩ := hi
        · left
          simp only [allPermForget_mul, allPermSderiv_forget, allPermDeriv_forget,
            allPermForget_inv, Tree.inv_deriv, Tree.inv_sderiv, Tree.mul_apply, Tree.sderiv_apply,
            Tree.deriv_apply, Path.deriv_scoderiv, Tree.inv_apply, mul_smul]
          rwa [← (hρ₁ _).1 a ⟨i, hi⟩, inv_smul_smul, (hρ₂ _).1 a ⟨i, hi⟩]
        · refine Or.inr ⟨i, rfl, ?_⟩
          simp only [allPermForget_mul, allPermSderiv_forget, allPermDeriv_forget,
            allPermForget_inv, Tree.inv_deriv, Tree.inv_sderiv, Tree.mul_apply, Tree.sderiv_apply,
            Tree.deriv_apply, Path.deriv_scoderiv, Tree.inv_apply, mul_smul, Enumeration.smul_rel,
            inv_smul_smul]
          exact hi

theorem nearLitters_of_inflexible_of_fixes {S : Support α} (hS : S.Strong) {T : Support γ}
    {ρ₁ ρ₂ : AllPerm β} {hγ : (γ : TypeIndex) < β}
    (hρ₁ : ρ₁ᵁ • (S ↘ LtLevel.elim : Support β) = S ↘ LtLevel.elim)
    (hρ₂ : ρ₂ᵁ • (S ↘ LtLevel.elim : Support β) = S ↘ LtLevel.elim)
    (A : α ↝ ⊥) (N₁ N₂ : NearLitter) (P : InflexiblePath ↑α) (t : Tangle P.δ) (ρ : AllPerm P.δ) :
    A = P.A ↘ P.hε ↘. → N₁ᴸ = fuzz P.hδε t → N₂ᴸ = fuzz P.hδε (ρ • t) →
    convNearLitters
      (S + (ρ₁ᵁ • ((T ↗ hγ).strong +
        (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗ LtLevel.elim)
      (S + (ρ₂ᵁ • ((T ↗ hγ).strong +
        (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗ LtLevel.elim) A N₁ N₂ →
    ∀ (B : P.δ ↝ ⊥), ∀ N ∈ (t.support ⇘. B)ᴺ, ∀ (i : κ),
      ((S + (ρ₁ᵁ • ((T ↗ hγ).strong + (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗
        LtLevel.elim) ⇘. (P.A ↘ P.hδ ⇘ B))ᴺ.rel i N →
      ((S + (ρ₂ᵁ • ((T ↗ hγ).strong + (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗
        LtLevel.elim) ⇘. (P.A ↘ P.hδ ⇘ B))ᴺ.rel i (ρᵁ B • N) := by
  rw [Support.smul_eq_iff] at hρ₁ hρ₂
  obtain ⟨γ, δ, ε, hδ, hε, hδε, B⟩ := P
  haveI : LeLevel γ := ⟨B.le⟩
  haveI : LtLevel δ := ⟨hδ.trans_le LeLevel.elim⟩
  haveI : LtLevel ε := ⟨hε.trans_le LeLevel.elim⟩
  dsimp only at t ρ ⊢
  intro hA hN₁ hN₂ hN C N₀ hN₀ i hi
  obtain ⟨rfl, hN'⟩ | ⟨A, rfl, hN₁', hN₂'⟩ := convNearLitters_cases hN
  · have haS := (hS.support_le hN' ⟨γ, δ, ε, hδ, hε, hδε, _⟩ t hA hN₁ _).2 N₀ hN₀
    rw [hN₂] at hN₁
    have hρt := congr_arg Tangle.support (fuzz_injective hN₁)
    rw [Tangle.smul_support, Support.smul_eq_iff] at hρt
    simp only [add_derivBot, BaseSupport.add_nearLitters, Enumeration.rel_add_iff] at hi ⊢
    rw [(hρt C).2 N₀ hN₀]
    obtain hi | ⟨i, rfl, hi⟩ := hi
    · exact Or.inl hi
    · simp only [add_right_inj, exists_eq_left]
      obtain ⟨D, hD⟩ := eq_of_nearLitter_mem_scoderiv_botDeriv ⟨i, hi⟩
      cases B using Path.recScoderiv
      case nil =>
        cases Path.scoderiv_index_injective hD
        cases Path.scoderiv_left_inj.mp hD
        simp only [hD, Path.coderiv_deriv, Path.coderiv_deriv', scoderiv_botDeriv_eq, smul_derivBot,
          add_derivBot, BaseSupport.smul_nearLitters, BaseSupport.add_nearLitters, Enumeration.smul_rel] at hi ⊢
        rw [deriv_derivBot, hD] at haS
        rw [← (hρ₂ _).2 N₀ haS, inv_smul_smul]
        rw [← (hρ₁ _).2 N₀ haS, inv_smul_smul] at hi
        exact Or.inr hi
      case scoderiv ζ B hζ' _ =>
        rw [Path.coderiv_deriv, Path.coderiv_deriv'] at hD
        cases Path.scoderiv_index_injective hD
        rw [Path.scoderiv_left_inj] at hD
        cases hD
        simp only [Path.coderiv_deriv, Path.coderiv_deriv', scoderiv_botDeriv_eq, smul_derivBot,
          add_derivBot, BaseSupport.smul_nearLitters, BaseSupport.add_nearLitters, Enumeration.smul_rel] at hi ⊢
        rw [deriv_derivBot, Path.coderiv_deriv, Path.coderiv_deriv'] at haS
        rw [← (hρ₂ _).2 N₀ haS, inv_smul_smul]
        rw [← (hρ₁ _).2 N₀ haS, inv_smul_smul] at hi
        exact Or.inr hi
  · simp only [add_derivBot, BaseSupport.add_nearLitters, interferenceSupport_nearLitters,
      Enumeration.add_empty] at hN₂'
    cases A
    case sderiv ζ A hζ' _ =>
      rw [← Path.coderiv_deriv] at hA
      cases Path.sderiv_index_injective hA
      apply Path.sderiv_path_injective at hA
      cases A
      case nil =>
        cases hA
        cases not_mem_strong_botDeriv _ _ hN₂'
      case sderiv ζ A hζ _ _ =>
        rw [← Path.coderiv_deriv] at hA
        cases Path.sderiv_index_injective hA
        apply Path.sderiv_path_injective at hA
        cases hA
        simp only [Path.coderiv_deriv, Path.coderiv_deriv', add_derivBot, scoderiv_botDeriv_eq,
          smul_derivBot, BaseSupport.add_nearLitters, BaseSupport.smul_nearLitters] at hi ⊢
        have : N₂ᴸ = (ρ₂ ⇘ A)ᵁ ↘ hζ ↘. • (ρ₁⁻¹ ⇘ A)ᵁ ↘ hζ ↘. • fuzz hδε t := by
          rw [inv_smul_eq_iff] at hN₁'
          rw [hN₁', Path.botSderiv_coe_eq, BasePerm.smul_nearLitter_litter,
            BasePerm.smul_nearLitter_litter, smul_smul, smul_eq_iff_eq_inv_smul,
            mul_inv_rev, inv_inv, mul_smul, ← Tree.inv_apply, ← allPermForget_inv] at hN₁
          rw [hN₁]
          simp only [allPermForget_inv, Tree.inv_apply, allPermDeriv_forget, Tree.inv_deriv,
            Tree.inv_sderiv, Tree.inv_sderivBot]
          rfl
        rw [smul_fuzz hδ hε hδε, smul_fuzz hδ hε hδε] at this
        have := fuzz_injective (hN₂.symm.trans this)
        rw [smul_smul] at this
        rw [t.smul_nearLitter_eq_of_mem_support this hN₀]
        rw [Enumeration.rel_add_iff] at hi ⊢
        obtain hi | ⟨i, rfl, hi⟩ := hi
        · left
          simp only [allPermForget_mul, allPermSderiv_forget, allPermDeriv_forget,
            allPermForget_inv, Tree.inv_deriv, Tree.inv_sderiv, Tree.mul_apply, Tree.sderiv_apply,
            Tree.deriv_apply, Path.deriv_scoderiv, Tree.inv_apply, mul_smul]
          rwa [← (hρ₁ _).2 N₀ ⟨i, hi⟩, inv_smul_smul, (hρ₂ _).2 N₀ ⟨i, hi⟩]
        · refine Or.inr ⟨i, rfl, ?_⟩
          simp only [allPermForget_mul, allPermSderiv_forget, allPermDeriv_forget,
            allPermForget_inv, Tree.inv_deriv, Tree.inv_sderiv, Tree.mul_apply, Tree.sderiv_apply,
            Tree.deriv_apply, Path.deriv_scoderiv, Tree.inv_apply, mul_smul, Enumeration.smul_rel,
            inv_smul_smul]
          exact hi

theorem litter_eq_of_flexible_of_fixes {S : Support α} {T : Support γ}
    {ρ₁ ρ₂ : AllPerm β} {hγ : (γ : TypeIndex) < β}
    (hρ₁ : ρ₁ᵁ • (S ↘ LtLevel.elim : Support β) = S ↘ LtLevel.elim)
    (hρ₂ : ρ₂ᵁ • (S ↘ LtLevel.elim : Support β) = S ↘ LtLevel.elim)
    {A : ↑α ↝ ⊥} {N₁ N₂ N₃ N₄ : NearLitter} :
    convNearLitters
      (S + (ρ₁ᵁ • ((T ↗ hγ).strong +
        (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗ LtLevel.elim)
      (S + (ρ₂ᵁ • ((T ↗ hγ).strong +
        (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗ LtLevel.elim) A N₁ N₂ →
    convNearLitters
      (S + (ρ₁ᵁ • ((T ↗ hγ).strong +
        (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗ LtLevel.elim)
      (S + (ρ₂ᵁ • ((T ↗ hγ).strong +
        (S ↘ LtLevel.elim + (T ↗ hγ).strong).interferenceSupport)) ↗ LtLevel.elim) A N₃ N₄ →
      ¬Inflexible A N₁ᴸ → ¬Inflexible A N₂ᴸ → ¬Inflexible A N₃ᴸ → ¬Inflexible A N₄ᴸ →
      N₁ᴸ = N₃ᴸ → N₂ᴸ = N₄ᴸ

:= by
  rw [Support.smul_eq_iff] at hρ₁ hρ₂
  rintro ⟨i, hi₁, hi₂⟩ ⟨j, hj₁, hj₂⟩ hN₁ hN₂ hN₃ hN₄ hN₁₃
  simp only [add_derivBot, BaseSupport.add_nearLitters, Rel.inv_apply,
    Enumeration.rel_add_iff] at hi₁ hi₂ hj₁ hj₂
  obtain hi₁ | ⟨i, rfl, hi₁⟩ := hi₁
  · obtain hi₂ | ⟨i, rfl, hi₂⟩ := hi₂
    swap
    · have := Enumeration.lt_bound _ _ ⟨_, hi₁⟩
      simp only [add_lt_iff_neg_left] at this
      cases (κ_zero_le i).not_lt this
    cases (Enumeration.rel_coinjective _).coinjective hi₁ hi₂
    obtain hj₁ | ⟨j, rfl, hj₁⟩ := hj₁
    · obtain hj₂ | ⟨j, rfl, hj₂⟩ := hj₂
      swap
      · have := Enumeration.lt_bound _ _ ⟨_, hj₁⟩
        simp only [add_lt_iff_neg_left] at this
        cases (κ_zero_le j).not_lt this
      cases (Enumeration.rel_coinjective _).coinjective hj₁ hj₂
      exact hN₁₃
    · simp only [add_right_inj, exists_eq_left] at hj₂
      obtain hj₂ | hj₂ := hj₂
      · have := Enumeration.lt_bound _ _ ⟨_, hj₂⟩
        simp only [add_lt_iff_neg_left] at this
        cases (κ_zero_le j).not_lt this
      obtain ⟨A, rfl⟩ := eq_of_nearLitter_mem_scoderiv_botDeriv ⟨j, hj₁⟩
      simp only [scoderiv_botDeriv_eq, smul_derivBot, add_derivBot, BaseSupport.smul_nearLitters,
        BaseSupport.add_nearLitters, Enumeration.smul_rel] at hj₁ hj₂
      have := congr_arg (·ᴸ) ((Enumeration.rel_coinjective _).coinjective hj₁ hj₂)
      simp only [BasePerm.smul_nearLitter_litter] at this
      rw [← hN₁₃, ← (hρ₁ A).2 N₁ ⟨i, hi₁⟩, BasePerm.smul_nearLitter_litter, inv_smul_smul] at this
      have hN₁' := (hρ₂ A).2 N₁ ⟨i, hi₁⟩
      rw [smul_eq_iff_eq_inv_smul] at hN₁'
      rwa [hN₁', BasePerm.smul_nearLitter_litter, smul_left_cancel_iff] at this
  · obtain hi₂ | hi₂ := hi₂
    · have := Enumeration.lt_bound _ _ ⟨_, hi₂⟩
      simp only [add_lt_iff_neg_left] at this
      cases (κ_zero_le i).not_lt this
    simp only [add_right_inj, exists_eq_left] at hi₂
    obtain ⟨A, rfl⟩ := eq_of_nearLitter_mem_scoderiv_botDeriv ⟨i, hi₁⟩
    simp only [scoderiv_botDeriv_eq, smul_derivBot, add_derivBot, BaseSupport.smul_nearLitters,
      BaseSupport.add_nearLitters, Enumeration.smul_rel] at hi₁ hi₂ hj₁ hj₂
    have hN₁₂ := congr_arg (·ᴸ) ((Enumeration.rel_coinjective _).coinjective hi₁ hi₂)
    obtain hj₁ | ⟨j, rfl, hj₁⟩ := hj₁
    · obtain hj₂ | ⟨j, rfl, hj₂⟩ := hj₂
      swap
      · have := Enumeration.lt_bound _ _ ⟨_, hj₁⟩
        simp only [add_lt_iff_neg_left] at this
        cases (κ_zero_le j).not_lt this
      cases (Enumeration.rel_coinjective _).coinjective hj₁ hj₂
      simp only [BasePerm.smul_nearLitter_litter] at hN₁₂
      rw [hN₁₃, ← (hρ₁ A).2 N₃ ⟨j, hj₁⟩, BasePerm.smul_nearLitter_litter, inv_smul_smul,
        eq_inv_smul_iff, ← BasePerm.smul_nearLitter_litter, (hρ₂ A).2 N₃ ⟨j, hj₁⟩] at hN₁₂
      rw [hN₁₂]
    · simp only [add_right_inj, exists_eq_left] at hj₂
      obtain hj₂ | hj₂ := hj₂
      · have := Enumeration.lt_bound _ _ ⟨_, hj₂⟩
        simp only [add_lt_iff_neg_left] at this
        cases (κ_zero_le j).not_lt this
      have hN₃₄ := congr_arg (·ᴸ) ((Enumeration.rel_coinjective _).coinjective hj₁ hj₂)
      simp only [BasePerm.smul_nearLitter_litter] at hN₁₂ hN₃₄
      rw [hN₁₃] at hN₁₂
      rwa [hN₁₂, smul_left_cancel_iff] at hN₃₄


import ConNF.ModelData.PathEnumeration

/-!
# Supports

In this file, we define the notion of a support.

## Main declarations

* `ConNF.BaseSupport`: The type of supports of atoms.
* `ConNF.Support`: The type of supports of objects of arbitrary type indices.
-/

universe u

open Cardinal

namespace ConNF

variable [Params.{u}]

/-!
## Base supports
-/

structure BaseSupport where
  atoms : Enumeration Atom
  nearLitters : Enumeration NearLitter

namespace BaseSupport

instance : SuperA BaseSupport (Enumeration Atom) where
  superA := atoms

instance : SuperN BaseSupport (Enumeration NearLitter) where
  superN := nearLitters

@[simp]
theorem mk_atoms {a : Enumeration Atom} {N : Enumeration NearLitter} :
    (BaseSupport.mk a N)ᴬ = a :=
  rfl

@[simp]
theorem mk_nearLitters {a : Enumeration Atom} {N : Enumeration NearLitter} :
    (BaseSupport.mk a N)ᴺ = N :=
  rfl

theorem atoms_congr {S T : BaseSupport} (h : S = T) :
    Sᴬ = Tᴬ :=
  h ▸ rfl

theorem nearLitters_congr {S T : BaseSupport} (h : S = T) :
    Sᴺ = Tᴺ :=
  h ▸ rfl

@[ext]
theorem ext {S T : BaseSupport} (h₁ : Sᴬ = Tᴬ) (h₂ : Sᴺ = Tᴺ) : S = T := by
  obtain ⟨SA, SN⟩ := S
  obtain ⟨TA, TN⟩ := T
  cases h₁
  cases h₂
  rfl

instance : SMul BasePerm BaseSupport where
  smul π S := ⟨π • Sᴬ, π • Sᴺ⟩

@[simp]
theorem smul_atoms (π : BasePerm) (S : BaseSupport) :
    (π • S)ᴬ = π • Sᴬ :=
  rfl

@[simp]
theorem smul_nearLitters (π : BasePerm) (S : BaseSupport) :
    (π • S)ᴺ = π • Sᴺ :=
  rfl

@[simp]
theorem smul_atoms_eq_of_smul_eq {π : BasePerm} {S : BaseSupport}
    (h : π • S = S) :
    π • Sᴬ = Sᴬ := by
  rw [← smul_atoms, h]

@[simp]
theorem smul_nearLitters_eq_of_smul_eq {π : BasePerm} {S : BaseSupport}
    (h : π • S = S) :
    π • Sᴺ = Sᴺ := by
  rw [← smul_nearLitters, h]

instance : MulAction BasePerm BaseSupport where
  one_smul S := by
    apply ext
    · rw [smul_atoms, one_smul]
    · rw [smul_nearLitters, one_smul]
  mul_smul π₁ π₂ S := by
    apply ext
    · rw [smul_atoms, smul_atoms, smul_atoms, mul_smul]
    · rw [smul_nearLitters, smul_nearLitters, smul_nearLitters, mul_smul]

theorem smul_eq_smul_iff (π₁ π₂ : BasePerm) (S : BaseSupport) :
    π₁ • S = π₂ • S ↔ (∀ a ∈ Sᴬ, π₁ • a = π₂ • a) ∧ (∀ N ∈ Sᴺ, π₁ • N = π₂ • N) := by
  constructor
  · intro h
    constructor
    · rintro a ⟨i, ha⟩
      have := congr_arg (·ᴬ.rel i (π₁ • a)) h
      simp only [smul_atoms, Enumeration.smul_rel, inv_smul_smul, eq_iff_iff] at this
      have := Sᴬ.rel_coinjective.coinjective ha (this.mp ha)
      rw [eq_inv_smul_iff] at this
      rw [this]
    · rintro N ⟨i, hN⟩
      have := congr_arg (·ᴺ.rel i (π₁ • N)) h
      simp only [smul_nearLitters, Enumeration.smul_rel, inv_smul_smul, eq_iff_iff] at this
      have := Sᴺ.rel_coinjective.coinjective hN (this.mp hN)
      rw [eq_inv_smul_iff] at this
      rw [this]
  · intro h
    ext : 2
    · rfl
    · ext i a : 3
      rw [smul_atoms, smul_atoms, Enumeration.smul_rel, Enumeration.smul_rel]
      constructor
      · intro ha
        have := h.1 _ ⟨i, ha⟩
        rw [smul_inv_smul, ← inv_smul_eq_iff] at this
        rwa [this]
      · intro ha
        have := h.1 _ ⟨i, ha⟩
        rw [smul_inv_smul, smul_eq_iff_eq_inv_smul] at this
        rwa [← this]
    · rfl
    · ext i a : 3
      rw [smul_nearLitters, smul_nearLitters, Enumeration.smul_rel, Enumeration.smul_rel]
      constructor
      · intro hN
        have := h.2 _ ⟨i, hN⟩
        rw [smul_inv_smul, ← inv_smul_eq_iff] at this
        rwa [this]
      · intro hN
        have := h.2 _ ⟨i, hN⟩
        rw [smul_inv_smul, smul_eq_iff_eq_inv_smul] at this
        rwa [← this]

theorem smul_eq_iff (π : BasePerm) (S : BaseSupport) :
    π • S = S ↔ (∀ a ∈ Sᴬ, π • a = a) ∧ (∀ N ∈ Sᴺ, π • N = N) := by
  have := smul_eq_smul_iff π 1 S
  simp only [one_smul] at this
  exact this

noncomputable instance : Add BaseSupport where
  add S T := ⟨Sᴬ + Tᴬ, Sᴺ + Tᴺ⟩

@[simp]
theorem add_atoms (S T : BaseSupport) :
    (S + T)ᴬ = Sᴬ + Tᴬ :=
  rfl

@[simp]
theorem add_nearLitters (S T : BaseSupport) :
    (S + T)ᴺ = Sᴺ + Tᴺ :=
  rfl

end BaseSupport

def baseSupportEquiv : BaseSupport ≃ Enumeration Atom × Enumeration NearLitter where
  toFun S := (Sᴬ, Sᴺ)
  invFun S := ⟨S.1, S.2⟩
  left_inv _ := rfl
  right_inv _ := rfl

theorem card_baseSupport : #BaseSupport = #μ := by
  rw [Cardinal.eq.mpr ⟨baseSupportEquiv⟩, mk_prod, lift_id, lift_id,
    card_enumeration_eq card_atom, card_enumeration_eq card_nearLitter, mul_eq_self aleph0_lt_μ.le]

/-!
## Structural supports
-/

structure Support (α : TypeIndex) where
  atoms : Enumeration (α ↝ ⊥ × Atom)
  nearLitters : Enumeration (α ↝ ⊥ × NearLitter)

namespace Support

variable {α β : TypeIndex}

instance : SuperA (Support α) (Enumeration (α ↝ ⊥ × Atom)) where
  superA := atoms

instance : SuperN (Support α) (Enumeration (α ↝ ⊥ × NearLitter)) where
  superN := nearLitters

@[simp]
theorem mk_atoms (E : Enumeration (α ↝ ⊥ × Atom)) (F : Enumeration (α ↝ ⊥ × NearLitter)) :
    (⟨E, F⟩ : Support α)ᴬ = E :=
  rfl

@[simp]
theorem mk_nearLitters (E : Enumeration (α ↝ ⊥ × Atom)) (F : Enumeration (α ↝ ⊥ × NearLitter)) :
    (⟨E, F⟩ : Support α)ᴺ = F :=
  rfl

instance : Derivative (Support α) (Support β) α β where
  deriv S A := ⟨Sᴬ ⇘ A, Sᴺ ⇘ A⟩

instance : Coderivative (Support β) (Support α) α β where
  coderiv S A := ⟨Sᴬ ⇗ A, Sᴺ ⇗ A⟩

instance : BotDerivative (Support α) BaseSupport α where
  botDeriv S A := ⟨Sᴬ ⇘. A, Sᴺ ⇘. A⟩
  botSderiv S := ⟨Sᴬ ↘., Sᴺ ↘.⟩
  botDeriv_single S h := by dsimp only; rw [botDeriv_single, botDeriv_single]

@[simp]
theorem deriv_atoms {α β : TypeIndex} (S : Support α) (A : α ↝ β) :
    Sᴬ ⇘ A = (S ⇘ A)ᴬ :=
  rfl

@[simp]
theorem deriv_nearLitters {α β : TypeIndex} (S : Support α) (A : α ↝ β) :
    Sᴺ ⇘ A = (S ⇘ A)ᴺ :=
  rfl

@[simp]
theorem sderiv_atoms {α β : TypeIndex} (S : Support α) (h : β < α) :
    Sᴬ ↘ h = (S ↘ h)ᴬ


import ConNF.Model.TTT

/-!
# New file

In this file...

## Main declarations

* `ConNF.foo`: Something new.
-/

noncomputable section
universe u

open Cardinal Ordinal

namespace ConNF

variable [Params.{u}] {α β γ δ ε ζ : Λ} (hβ : (β : TypeIndex) < α) (hγ : (γ : TypeIndex) < β)
  (hδ : (δ : TypeIndex) < γ) (hε : (ε : TypeIndex) < δ) (hζ : (ζ : TypeIndex) < ε)

namespace TSet

theorem exists_inter (x y : TSet α) :
    ∃ w : TSet α, ∀ z : TSet β, z ∈[hβ] w ↔ z ∈[hβ] x ∧ z ∈[hβ] y := by
  refine exists_of_symmetric {z | z ∈[hβ] x ∧ z ∈[hβ] y} hβ ?_
  obtain ⟨S, hS⟩ := symmetric x hβ
  obtain ⟨T, hT⟩ := symmetric y hβ
  use S + T
  intro ρ hρ
  specialize hS ρ (smul_eq_of_le Support.le_add_right hρ)
  specialize hT ρ (smul_eq_of_le Support.le_add_left hρ)
  simp [Set.ext_iff, Set.mem_smul_set_iff_inv_smul_mem] at hS hT ⊢
  aesop

theorem exists_compl (x : TSet α) :
    ∃ y : TSet α, ∀ z : TSet β, z ∈[hβ] y ↔ ¬z ∈[hβ] x := by
  refine exists_of_symmetric {z | ¬z ∈[hβ] x} hβ ?_
  obtain ⟨S, hS⟩ := symmetric x hβ
  use S
  intro ρ hρ
  specialize hS ρ hρ
  simp [Set.ext_iff, Set.mem_smul_set_iff_inv_smul_mem] at hS ⊢
  aesop

theorem exists_up (x y : TSet β) :
    ∃ w : TSet α, ∀ z : TSet β, z ∈[hβ] w ↔ z = x ∨ z = y := by
  refine exists_of_symmetric {x, y} hβ ?_
  obtain ⟨S, hS⟩ := exists_support x
  obtain ⟨T, hT⟩ := exists_support y
  use (S + T) ↗ hβ
  intro ρ hρ
  rw [Support.smul_scoderiv, Support.scoderiv_inj, ← allPermSderiv_forget'] at hρ
  specialize hS (ρ ↘ hβ) (smul_eq_of_le Support.le_add_right hρ)
  specialize hT (ρ ↘ hβ) (smul_eq_of_le Support.le_add_left hρ)
  simp only [Set.smul_set_def, Set.image, Set.mem_insert_iff, Set.mem_singleton_iff,
    exists_eq_or_imp, hS, exists_eq_left, hT]
  ext z
  rw [Set.mem_insert_iff, Set.mem_singleton_iff, Set.mem_setOf_eq]
  aesop

/-- The unordered pair. -/
def up (x y : TSet β) : TSet α :=
  (exists_up hβ x y).choose

@[simp]
theorem mem_up_iff (x y z : TSet β) :
    z ∈[hβ] up hβ x y ↔ z = x ∨ z = y :=
  (exists_up hβ x y).choose_spec z

/-- The Kuratowski ordered pair. -/
def op (x y : TSet γ) : TSet α :=
  up hβ (singleton hγ x) (up hγ x y)

theorem up_injective {x y z w : TSet β} (h : up hβ x y = up hβ z w) :
    (x = z ∧ y = w) ∨ (x = w ∧ y = z) := by
  have h₁ := mem_up_iff hβ x y z
  have h₂ := mem_up_iff hβ x y w
  have h₃ := mem_up_iff hβ z w x
  have h₄ := mem_up_iff hβ z w y
  rw [h, mem_up_iff] at h₁ h₂
  rw [← h, mem_up_iff] at h₃ h₄
  aesop

@[simp]
theorem up_inj (x y z w : TSet β) :
    up hβ x y = up hβ z w ↔ (x = z ∧ y = w) ∨ (x = w ∧ y = z) := by
  constructor
  · exact up_injective hβ
  · rintro (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩)
    · rfl
    · apply tSet_ext' hβ
      aesop

@[simp]
theorem up_self {x : TSet β} :
    up hβ x x = singleton hβ x := by
  apply tSet_ext' hβ
  aesop

@[simp]
theorem up_eq_singleton_iff (x y z : TSet β) :
    up hβ x y = singleton hβ z ↔ x = z ∧ y = z := by
  constructor
  · intro h
    have h₁ := typedMem_singleton_iff' hβ z x
    have h₂ := typedMem_singleton_iff' hβ z y
    rw [← h, mem_up_iff] at h₁ h₂
    aesop
  · rintro ⟨rfl, rfl⟩
    rw [up_self]

@[simp]
theorem singleton_eq_up_iff (x y z : TSet β) :
    singleton hβ z = up hβ x y ↔ x = z ∧ y = z := by
  rw [← up_eq_singleton_iff hβ x y z, eq_comm]

theorem op_injective {x y z w : TSet γ} (h : op hβ hγ x y = op hβ hγ z w) :
    x = z ∧ y = w := by
  rw [op, op] at h
  simp only [up_inj, singleton_inj, singleton_eq_up_iff, up_eq_singleton_iff] at h
  obtain (⟨rfl, ⟨h, rfl⟩ | ⟨rfl, rfl⟩⟩ | ⟨⟨rfl, rfl⟩, ⟨h, rfl⟩⟩) := h <;> simp only [and_self]

@[simp]
theorem op_inj (x y z w : TSet γ) :
    op hβ hγ x y = op hβ hγ z w ↔ x = z ∧ y = w := by
  constructor
  · exact op_injective hβ hγ
  · rintro ⟨rfl, rfl⟩
    rfl

@[simp]
theorem op_eq_singleton_iff (x y : TSet γ) (z : TSet β) :
    op hβ hγ x y = singleton hβ z ↔ singleton hγ x = z ∧ singleton hγ y = z := by
  rw [op, up_eq_singleton_iff, and_congr_right_iff]
  rintro rfl
  simp only [up_eq_singleton_iff, true_and, singleton_inj]

@[simp]
theorem smul_up (x y : TSet β) (ρ : AllPerm α) :
    ρ • up hβ x y = up hβ (ρ ↘ hβ • x) (ρ ↘ hβ • y) := by
  apply tSet_ext' hβ
  aesop

@[simp]
theorem smul_op (x y : TSet γ) (ρ : AllPerm α) :
    ρ • op hβ hγ x y = op hβ hγ (ρ ↘ hβ ↘ hγ • x) (ρ ↘ hβ ↘ hγ • y) := by
  apply tSet_ext' hβ
  simp only [op, smul_up, smul_singleton, mem_up_iff, implies_true]

theorem exists_singletonImage (x : TSet β) :
    ∃ y : TSet α, ∀ z w,
    op hγ hδ (singleton hε z) (singleton hε w) ∈[hβ] y ↔ op hδ hε z w ∈[hγ] x := by
  have := exists_of_symmetric {u | ∃ z w : TSet ε, op hδ hε z w ∈[hγ] x ∧
      u = op hγ hδ (singleton hε z) (singleton hε w)} hβ ?_
  · obtain ⟨y, hy⟩ := this
    use y
    intro z w
    rw [hy]
    simp only [Set.mem_setOf_eq, op_inj, singleton_inj, exists_eq_right_right', exists_eq_right']
  · obtain ⟨S, hS⟩ := exists_support x
    use S ↗ hβ
    intro ρ hρ
    rw [Support.smul_scoderiv, Support.scoderiv_inj, ← allPermSderiv_forget'] at hρ
    specialize hS (ρ ↘ hβ) hρ
    ext z
    constructor
    · rintro ⟨_, ⟨z, w, hab, rfl⟩, rfl⟩
      refine ⟨ρ ↘ hβ ↘ hγ ↘ hδ ↘ hε • z, ρ ↘ hβ ↘ hγ ↘ hδ ↘ hε • w, ?_, ?_⟩
      · rwa [← hS, mem_smul_iff', smul_op, allPerm_inv_sderiv', allPerm_inv_sderiv',
          allPerm_inv_sderiv', inv_smul_smul, inv_smul_smul]
      · simp only [smul_op, smul_singleton]
    · rintro ⟨z, w, hab, rfl⟩
      refine ⟨ρ⁻¹ ↘ hβ • op hγ hδ (singleton hε z) (singleton hε w), ?_, ?_⟩
      · simp only [allPerm_inv_sderiv', smul_op, smul_singleton, Set.mem_setOf_eq, op_inj,
          singleton_inj, exists_eq_right_right', exists_eq_right']
        rw [smul_eq_iff_eq_inv_smul] at hS
        rw [hS]
        simp only [mem_smul_iff', inv_inv, smul_op, smul_inv_smul]
        exact hab
      · simp only [allPerm_inv_sderiv', smul_inv_smul]

theorem exists_insertion2 (x : TSet γ) :
    ∃ y : TSet α, ∀ a b c, op hγ hδ (singleton hε (singleton hζ a)) (op hε hζ b c) ∈[hβ] y ↔
    op hε hζ a c ∈[hδ] x := by
  have := exists_of_symmetric {u | ∃ a b c : TSet ζ, op hε hζ a c ∈[hδ] x ∧
      u = op hγ hδ (singleton hε (singleton hζ a)) (op hε hζ b c)} hβ ?_
  · obtain ⟨y, hy⟩ := this
    use y
    intro a b c
    rw [hy]
    constructor
    · rintro ⟨a', b', c', h₁, h₂⟩
      simp only [op_inj, singleton_inj] at h₂
      obtain ⟨rfl, rfl, rfl⟩ := h₂
      exact h₁
    · intro h
      exact ⟨a, b, c, h, rfl⟩
  · obtain ⟨S, hS⟩ := exists_support x
    use S ↗ hγ ↗ hβ
    intro ρ hρ
    simp only [Support.smul_scoderiv, Support.scoderiv_inj, ← allPermSderiv_forget'] at hρ
    specialize hS (ρ ↘ hβ ↘ hγ) hρ
    ext z
    constructor
    · rintro ⟨_, ⟨a, b, c, hx, rfl⟩, rfl⟩
      refine ⟨ρ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • a, ρ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • b,
        ρ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • c, ?_, ?_⟩
      · rw [← hS]
        simp only [mem_smul_iff', allPerm_inv_sderiv', smul_op, inv_smul_smul]
        exact hx
      · simp only [smul_op, smul_singleton]
    · rintro ⟨a, b, c, hx, rfl⟩
      rw [Set.mem_smul_set_iff_inv_smul_mem]
      refine ⟨ρ⁻¹ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • a, ρ⁻¹ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • b,
        ρ⁻¹ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • c, ?_, ?_⟩
      · rw [smul_eq_iff_eq_inv_smul] at hS
        rw [hS, mem_smul_iff']
        simp only [inv_inv, allPerm_inv_sderiv', smul_op, smul_inv_smul]
        exact hx
      · simp only [smul_op, allPerm_inv_sderiv', smul_singleton]

theorem exists_insertion3 (x : TSet γ) :
    ∃ y : TSet α, ∀ a b c, op hγ hδ (singleton hε (singleton hζ a)) (op hε hζ b c) ∈[hβ] y ↔
    op hε hζ a b ∈[hδ] x := by
  have := exists_of_symmetric {u | ∃ a b c : TSet ζ, op hε hζ a b ∈[hδ] x ∧
      u = op hγ hδ (singleton hε (singleton hζ a)) (op hε hζ b c)} hβ ?_
  · obtain ⟨y, hy⟩ := this
    use y
    intro a b c
    rw [hy]
    constructor
    · rintro ⟨a', b', c', h₁, h₂⟩
      simp only [op_inj, singleton_inj] at h₂
      obtain ⟨rfl, rfl, rfl⟩ := h₂
      exact h₁
    · intro h
      exact ⟨a, b, c, h, rfl⟩
  · obtain ⟨S, hS⟩ := exists_support x
    use S ↗ hγ ↗ hβ
    intro ρ hρ
    simp only [Support.smul_scoderiv, Support.scoderiv_inj, ← allPermSderiv_forget'] at hρ
    specialize hS (ρ ↘ hβ ↘ hγ) hρ
    ext z
    constructor
    · rintro ⟨_, ⟨a, b, c, hx, rfl⟩, rfl⟩
      refine ⟨ρ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • a, ρ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • b,
        ρ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • c, ?_, ?_⟩
      · rw [← hS]
        simp only [mem_smul_iff', allPerm_inv_sderiv', smul_op, inv_smul_smul]
        exact hx
      · simp only [smul_op, smul_singleton]
    · rintro ⟨a, b, c, hx, rfl⟩
      rw [Set.mem_smul_set_iff_inv_smul_mem]
      refine ⟨ρ⁻¹ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • a, ρ⁻¹ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • b,
        ρ⁻¹ ↘ hβ ↘ hγ ↘ hδ ↘ hε ↘ hζ • c, ?_, ?_⟩
      · rw [smul_eq_iff_eq_inv_smul] at hS
        rw [hS, mem_smul_iff']
        simp only [inv_inv, allPerm_inv_sderiv', smul_op, smul_inv_smul]
        exact hx
      · simp only [smul_op, allPerm_inv_sderiv', smul_singleton]

theorem exists_cross (x : TSet γ) :
    ∃ y : TSet α, ∀ a, a ∈[hβ] y ↔ ∃ b c, a = op hγ hδ b c ∧ c ∈[hδ] x := by
  have := exists_of_symmetric {a | ∃ b c, a = op hγ hδ b c ∧ c ∈[hδ] x} hβ ?_
  · obtain ⟨y, hy⟩ := this
    use y
    intro a
    rw [hy]
    rfl
  · obtain ⟨S, hS⟩ := exists_support x
    use S ↗ hγ ↗ hβ
    intro ρ hρ
    simp only [Support.smul_scoderiv, Support.scoderiv_inj, ← allPermSderiv_forget'] at hρ
    specialize hS (ρ ↘ hβ ↘ hγ) hρ
    ext z
    constructor
    · rintro ⟨_, ⟨a, b, rfl, hab⟩, rfl⟩
      refine ⟨ρ ↘ hβ ↘ hγ ↘ hδ • a, ρ ↘ hβ ↘ hγ ↘ hδ • b, ?_, ?_⟩
      · simp only [smul_op]
      · rw [← hS]
        simp only [mem_smul_iff', allPerm_inv_sderiv', inv_smul_smul]
        exact hab
    · rintro ⟨a, b, rfl, hab⟩
      rw [Set.mem_smul_set_iff_inv_smul_mem]
      refine ⟨ρ⁻¹ ↘ hβ ↘ hγ ↘ hδ • a, ρ⁻¹ ↘ hβ ↘ hγ ↘ hδ • b, ?_, ?_⟩
      · simp only [smul_op, allPerm_inv_sderiv']
      · rw [smul_eq_iff_eq_inv_smul] at hS
        rw [hS]
        simp only [allPerm_inv_sderiv', mem_smul_iff', inv_inv, smul_inv_smul]
        exact hab

theorem exists_typeLower (x : TSet α) :
    ∃ y : TSet δ, ∀ a, a ∈[hε] y ↔ ∀ b, op hγ hδ b (singleton hε a) ∈[hβ] x

:= by
  have := exists_of_symmetric {a | ∀ b, op hγ hδ b (singleton hε a) ∈[hβ] x} hε ?_
  · obtain ⟨y, hy⟩ := this
    use y
    intro a
    rw [hy]
    rfl
  · apply sUnion_singleton_symmetric hε hδ
    apply sUnion_singleton_symmetric hδ hγ
    apply sUnion_singleton_symmetric hγ hβ
    obtain ⟨S, hS⟩ := exists_support x
    use S
    intro ρ hρ
    specialize hS ρ hρ
    ext z
    constructor
    · rintro ⟨_, ⟨_, ⟨a, ⟨b, hb, rfl⟩, rfl⟩, rfl⟩, rfl⟩
      simp only [smul_singleton, Set.mem_image, Set.mem_setOf_eq, exists_exists_and_eq_and,
        singleton_inj, exists_eq_right]
      intro c
      have := hb (ρ⁻¹ ↘ hβ ↘ hγ ↘ hδ • c)
      rw [smul_eq_iff_eq_inv_smul] at hS
      rw [hS] at this
      simp only [allPerm_inv_sderiv', mem_smul_iff', inv_inv, smul_op, smul_inv_smul,
        smul_singleton] at this
      exact this
    · rintro ⟨_, ⟨a, ⟨b, hb, rfl⟩, rfl⟩, rfl⟩
      rw [Set.mem_smul_set_iff_inv_smul_mem]
      simp only [smul_singleton, allPerm_inv_sderiv', Set.mem_image, Set.mem_setOf_eq,
        exists_exists_and_eq_and, singleton_inj, exists_eq_right]
      intro c
      have := hb (ρ ↘ hβ ↘ hγ ↘ hδ • c)
      rw [← hS] at this
      simp only [mem_smul_iff', allPerm_inv_sderiv', smul_op, inv_smul_smul, smul_singleton] at this
      exact this


import ConNF.Model.RaiseStrong

/-!
# New file

In this file...

## Main declarations

* `ConNF.foo`: Something new.
-/

noncomputable section
universe u

open Cardinal Ordinal

open scoped Pointwise

namespace ConNF

variable [Params.{u}]

/-- A redefinition of the derivative of allowable permutations that is invariant of level,
but still has nice definitional properties. -/
@[default_instance 200]
instance {β γ : TypeIndex} : Derivative (AllPerm β) (AllPerm γ) β γ where
  deriv ρ A :=
    A.recSderiv
    (motive := λ (δ : TypeIndex) (A : β ↝ δ) ↦
      letI : Level := ⟨δ.recBotCoe (Nonempty.some inferInstance) id⟩
      letI : LeLevel δ := ⟨δ.recBotCoe (λ _ ↦ bot_le) (λ _ h ↦ WithBot.coe_le_coe.mpr h.le)
        (show δ.recBotCoe (Nonempty.some inferInstance) id = Level.α from rfl)⟩
      AllPerm δ)
    ρ (λ δ ε A h ρ ↦
      letI : Level := ⟨δ.recBotCoe (Nonempty.some inferInstance) id⟩
      letI : LeLevel δ := ⟨δ.recBotCoe (λ _ ↦ bot_le) (λ _ h ↦ WithBot.coe_le_coe.mpr h.le)
        (show δ.recBotCoe (Nonempty.some inferInstance) id = Level.α from rfl)⟩
      letI : LeLevel ε := ⟨h.le.trans LeLevel.elim⟩
      PreCoherentData.allPermSderiv h ρ)

@[simp]
theorem allPerm_deriv_nil' {β : TypeIndex}
    (ρ : AllPerm β) :
    ρ ⇘ (.nil : β ↝ β) = ρ :=
  rfl

@[simp]
theorem allPerm_deriv_sderiv' {β γ δ : TypeIndex}
    (ρ : AllPerm β) (A : β ↝ γ) (h : δ < γ) :
    ρ ⇘ (A ↘ h) = ρ ⇘ A ↘ h