/-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
module
public import Mathlib.Basic.SetLike.Basic
public import Mathlib.Data.List.Pairwise
public import Mathlib.Data.
Set.Notation
public import Mathlib.Data.
Set.Pairwise.Basic
public import Mathlib.Order.Directed
public import Mathlib.Order.Hom.
Set
/-!
# Chains and flags
This file defines chains for an arbitrary relation and flags for an order.
## Main declarations
* `IsChain s`: A chain `s` is a set of comparable elements.
* `Flag`: The type of flags, aka maximal chains, of an order.
## Notes
Originally ported from Isabelle/HOL. The
[original file](https://isabelle.in.tum.de/dist/library/HOL/HOL/Zorn.html) was written by Jacques D.
Fleuriot, Tobias Nipkow, Christian Sternagel.
-/
@[
expose]
public section
assert_not_exists CompleteLattice
open Set Set.Notation
variable {α β F : Type*}
/-! ### Chains -/
section Chain
variable (r : α → α → Prop)
/-- In this file, we use `≺` as a local notation for any relation `r`. -/
local infixl:
50 " ≺ " => r
/-- A chain is a set `s` satisfying `x ≺ y ∨ x = y ∨ y ≺ x` for all `x y ∈ s`. -/
def IsChain (s :
Set α) : Prop :=
s.Pairwise
fun x y => x ≺ y ∨ y ≺ x
/-- `SuperChain s t` means that `t` is a chain that strictly includes `s`. -/
def SuperChain (s t :
Set α) : Prop :=
IsChain r t ∧ s ⊂ t
/-- A chain `s` is a maximal chain if there does not exists a chain strictly including `s`. -/
def IsMaxChain (s :
Set α) : Prop :=
IsChain r s ∧ ∀ ⦃t⦄, IsChain r t → s ⊆ t → s = t
variable {r} {c s t :
Set α} {a b x y : α}
@[
simp]
lemma IsChain.empty : IsChain r ∅ := pairwise_empty _
@[
simp]
lemma IsChain.singleton : IsChain r {a} := pairwise_singleton ..
theorem Set.Subsingleton.isChain (hs : s.Subsingleton) : IsChain r s :=
hs.pairwise _
theorem IsChain.mono : s ⊆ t → IsChain r t → IsChain r s :=
Set.Pairwise.mono
theorem IsChain.mono_rel {r' : α → α → Prop} (h : IsChain r s) (h_imp : ∀ x y, r x y → r' x y) :
IsChain r' s :=
h.mono'
fun x y => Or.imp (h_imp x y) (h_imp y x)
/-- This can be used to turn `IsChain (≥)` into `IsChain (≤)` and vice-versa. -/
theorem IsChain.symm (h : IsChain r s) : IsChain (flip r) s :=
h.mono'
fun _ _ => Or.symm
theorem isChain_of_trichotomous [Std.Trichotomous r] (s :
Set α) : IsChain r s :=
fun a _ b _ hab => (trichotomous_of r a b).imp_right
fun h => h.resolve_left hab
protected theorem IsChain.insert (hs : IsChain r s) (ha : ∀ b ∈ s, a ≠ b → a ≺ b ∨ b ≺ a) :
IsChain r (insert a s) :=
have : Std.Symm
fun a b ↦ a ≺ b ∨ b ≺ a := { symm _ _ := Or.symm }
hs.insert_of_symm ha
lemma IsChain.
pair (h : r a b) : IsChain r {a, b} :=
IsChain.singleton.insert
fun _ hb _ ↦ .inl <| (eq_of_mem_singleton hb).symm.recOn ‹_›
theorem isChain_univ_iff : IsChain r (univ :
Set α) ↔ Std.Trichotomous r :=
by
refine ⟨
fun h => ⟨
fun a b => ?_⟩,
fun h => @isChain_of_trichotomous _ _ h univ⟩
have : a ≠ b → (r a b ∨ r b a) := h trivial trivial
grind
theorem IsChain.image_of_map_rel (r : α → α → Prop) (s : β → β → Prop) (f : α → β)
(h : ∀ x y, r x y → s (f x) (f y)) {c :
Set α} (hrc : IsChain r c) : IsChain s (f '' c) :=
fun _ ⟨_, ha₁, ha₂⟩ _ ⟨_, hb₁, hb₂⟩ =>
ha₂ ▸ hb₂ ▸
fun hxy => (hrc ha₁ hb₁ <| ne_of_apply_ne f hxy).imp (h _ _) (h _ _)
theorem IsChain.preimage (r : α → α → Prop) (s : β → β → Prop) (f : α → β)
(hf : Function.Injective f) (h : ∀ x y, s (f x) (f y) → r x y) {c :
Set β} (hrc : IsChain s c) :
IsChain r (f ⁻¹' c) :=
by
intro _ ha _ hb hne
have := hrc ha hb (
fun h ↦ hne (hf h))
grind
lemma isChain_union {s t :
Set α} :
IsChain r (s ∪ t) ↔ IsChain r s ∧ IsChain r t ∧ ∀ a ∈ s, ∀ b ∈ t, a ≠ b → r a b ∨ r b a :=
by
have : Std.Symm
fun a b ↦ a ≺ b ∨ b ≺ a := { symm _ _ := Or.symm }
rw [IsChain, IsChain, IsChain, pairwise_union_of_symm]
lemma Monotone.isChain_image [Preorder α] [Preorder β] {s :
Set α} {f : α → β}
(hf : Monotone f) (hs : IsChain (· ≤ ·) s) : IsChain (· ≤ ·) (f '' s) :=
hs.image_of_map_rel _ _ _ (
fun _ _ a ↦ hf a)
theorem Monotone.isChain_range [LinearOrder α] [Preorder β] {f : α → β} (hf : Monotone f) :
IsChain (· ≤ ·) (range f) :=
by
rw [← image_univ]
exact hf.isChain_image (isChain_of_trichotomous _)
lemma Antitone.isChain_image [Preorder α] [Preorder β] {s :
Set α} {f : α → β}
(hf : Antitone f) (hs : IsChain (· ≤ ·) s) : IsChain (· ≤ ·) (f '' s) :=
hf.dual_left.isChain_image hs.symm
theorem Antitone.isChain_range [LinearOrder α] [Preorder β] {f : α → β} (hf : Antitone f) :
IsChain (· ≤ ·) (range f) :=
hf.dual_left.isChain_range
theorem IsChain.lt_of_le [PartialOrder α] {s :
Set α} (h : IsChain (· ≤ ·) s) :
IsChain (· < ·) s :=
fun _a ha _b hb hne ↦
(h ha hb hne).imp hne.lt_of_le hne.lt_of_le'
@[
simp]
protected theorem IsChain.diff {s t :
Set α} (h : IsChain r s) : IsChain r (s \ t) :=
h.mono
Set.sdiff_subset
theorem isChain_preimage_subtypeVal (s t :
Set α) :
@IsChain ↑s (r · ·) (s ↓∩ t) ↔ IsChain r (s ∩ t) :=
by
simp [IsChain,
Set.Pairwise]
theorem isChain_coe_univ_iff {s :
Set α} : @IsChain ↑s (r · ·) univ ↔ IsChain r s :=
by
simpa using isChain_preimage_subtypeVal s univ
section Rel
variable {r : α → α → Prop} {r' : β → β → Prop} {s :
Set α}
theorem IsChain.image [FunLike F α β] [RelHomClass F r r'] (hs : IsChain r s) (φ : F) :
IsChain r' (φ '' s) :=
hs.image_of_map_rel _ _ _ (
fun _ _ h ↦ map_rel φ h)
theorem IsChain.preimage_relEmbedding {t :
Set β} (ht : IsChain r' t) (φ : r ↪r r') :
IsChain r (φ ⁻¹' t) :=
ht.preimage _ _ _ φ.injective (
fun _ _ h ↦ φ.map_rel_iff.mp h)
theorem IsChain.preimage_relIso {t :
Set β} (hs : IsChain r' t) (φ : r ≃r r') :
IsChain r (φ ⁻¹' t) :=
hs.preimage_relEmbedding φ.toRelEmbedding
theorem IsChain.image_relEmbedding_iff {φ : r ↪r r'} : IsChain r' (φ '' s) ↔ IsChain r s :=
⟨
fun h => (φ.injective.preimage_image s).subst (h.preimage_relEmbedding φ),
fun h => h.image φ⟩
theorem IsChain.image_relIso_iff {φ : r ≃r r'} : IsChain r' (φ '' s) ↔ IsChain r s :=
@image_relEmbedding_iff _ _ _ _ _ (φ : r ↪r r')
theorem IsChain.preimage_embedding [LE α] [LE β] {t :
Set β} (ht : IsChain (· ≤ ·) t) (φ : α ↪o β) :
IsChain (· ≤ ·) (φ ⁻¹' t) :=
preimage_relEmbedding ht _
theorem IsChain.image_embedding_iff [LE α] [LE β] {φ : α ↪o β} :
IsChain (· ≤ ·) (φ '' s) ↔ IsChain (· ≤ ·) s :=
image_relEmbedding_iff
theorem IsChain.image_iso_iff [LE α] [LE β] {φ : α ≃o β} :
IsChain (· ≤ ·) (φ '' s) ↔ IsChain (· ≤ ·) s :=
image_relEmbedding_iff
theorem IsChain.preimage_iso [LE α] [LE β] {t :
Set β} (ht : IsChain (· ≤ ·) t) (φ : α ≃o β) :
IsChain (· ≤ ·) (φ ⁻¹' t) :=
preimage_relEmbedding ht _
theorem IsChain.preimage_iso_iff [LE α] [LE β] {t :
Set β} {φ : α ≃o β} :
IsChain (· ≤ ·) (φ ⁻¹' t) ↔ IsChain (· ≤ ·) t :=
⟨
fun h => (φ.image_preimage t).subst (h.image φ),
fun h => h.preimage_iso _⟩
end Rel
section Total
variable [Std.Refl r]
theorem IsChain.total (h : IsChain r s) (hx : x ∈ s) (hy : y ∈ s) : x ≺ y ∨ y ≺ x :=
(eq_or_ne x y).elim (
fun e => Or.inl <| e ▸ refl _) (h hx hy)
theorem IsChain.directedOn (H : IsChain r s) : DirectedOn r s :=
fun x hx y hy =>
((H.total hx hy).elim
fun h => ⟨y, hy, h, refl _⟩)
fun h => ⟨x, hx, refl _, h⟩
protected theorem IsChain.directed {f : β → α} {c :
Set β} (h : IsChain (f ⁻¹'o r) c) :
Directed r
fun x : { a : β // a ∈ c } => f x :=
fun ⟨a, ha⟩ ⟨b, hb⟩ =>
(by_cases
fun hab : a = b =>
by
simp only [hab, exists_prop, and_self_iff, Subtype.exists]
exact ⟨b, hb, refl _⟩)
fun hab => ((h ha hb hab).elim
fun h => ⟨⟨b, hb⟩, h, refl _⟩)
fun h => ⟨⟨a, ha⟩, refl _, h⟩
theorem IsChain.exists3 (hchain : IsChain r s) [IsTrans α r] {a b c} (mem1 : a ∈ s) (mem2 : b ∈ s)
(mem3 : c ∈ s) : ∃ (z : _) (_ : z ∈ s), r a z ∧ r b z ∧ r c z :=
by
rcases directedOn_iff_directed.mpr (IsChain.directed hchain) a mem1 b mem2 with ⟨z, mem4, H1
, H2⟩
rcases directedOn_iff_directed.mpr (IsChain.directed hchain) z mem4 c mem3 with
⟨z', mem5, H3, H4⟩
exact ⟨z', mem5, _root_.trans H1 H3, _root_.trans H2 H3, H4⟩
end Total
/-- A chain in a preorder is a linear order. -/
@[implicit_reducible]
def IsChain.linearOrder [Preorder α] [DecidableLE α] {s : Set α} (hs : IsChain (· < ·) s) :
LinearOrder s where
le_antisymm :=
fun ⟨a, ha⟩ ⟨b, hb⟩ hab hba ↦
Subtype.ext <| not_not.mp (hs ha hb · |>.elim hba.not_gt hab.not_gt)
le_total :=
fun ⟨a, ha⟩ ⟨b, hb⟩ ↦ eq_or_ne a b |>.elim (by simp [·]) (hs ha hb · |>.imp (·.le) (·.le))
toDecidableLE x y := inferInstanceAs (Decidable (x.1 ≤ y.1))
lemma IsChain.le_of_not_gt [Preorder α] (hs : IsChain (· ≤ ·) s)
{x y : α} (hx : x ∈ s) (hy : y ∈ s) (h : ¬ x < y) : y ≤ x := by
cases hs.total hx hy with
| inr h' => exact h'
| inl h' => simpa [lt_iff_le_not_ge, h'] using h
lemma IsChain.not_lt [Preorder α] (hs : IsChain (· ≤ ·) s)
{x y : α} (hx : x ∈ s) (hy : y ∈ s) : ¬ x < y ↔ y ≤ x :=
⟨(hs.le_of_not_gt hx hy ·), fun h h' ↦ h'.not_ge h⟩
lemma IsChain.lt_of_not_ge [Preorder α] (hs : IsChain (· ≤ ·) s)
{x y : α} (hx : x ∈ s) (hy : y ∈ s) (h : ¬ x ≤ y) : y < x :=
(hs.total hx hy).elim (h · |>.elim) (lt_of_le_not_ge · h)
lemma IsChain.not_le [Preorder α] (hs : IsChain (· ≤ ·) s)
{x y : α} (hx : x ∈ s) (hy : y ∈ s) : ¬ x ≤ y ↔ y < x :=
⟨(hs.lt_of_not_ge hx hy ·), fun h h' ↦ h'.not_gt h⟩
theorem IsMaxChain.isChain (h : IsMaxChain r s) : IsChain r s :=
h.1
theorem IsMaxChain.not_superChain (h : IsMaxChain r s) : ¬SuperChain r s t := fun ht =>
ht.2.ne <| h.2 ht.1 ht.2.1
theorem maximal_isChain_iff : Maximal (IsChain r) s ↔ IsMaxChain r s where
mp h := ⟨h.prop, fun _ ht hst ↦ hst.antisymm <| h.le_of_ge ht hst⟩
mpr h := ⟨h.isChain, fun _ ht hst ↦ h.right ht hst |>.ge⟩
alias ⟨_, IsMaxChain.maximal_isChain⟩ := maximal_isChain_iff
theorem IsMaxChain.bot_mem [LE α] [OrderBot α] (h : IsMaxChain (· ≤ ·) s) : ⊥ ∈ s :=
(h.2 (h.1.insert fun _ _ _ => Or.inl bot_le) <| subset_insert _ _).symm ▸ mem_insert _ _
theorem IsMaxChain.top_mem [LE α] [OrderTop α] (h : IsMaxChain (· ≤ ·) s) : ⊤ ∈ s :=
(h.2 (h.1.insert fun _ _ _ => Or.inr le_top) <| subset_insert _ _).symm ▸ mem_insert _ _
lemma IsMaxChain.image {s : β → β → Prop} (e : r ≃r s) {c : Set α} (hc : IsMaxChain r c) :
IsMaxChain s (e '' c) where
left := hc.isChain.image e
right t ht hf := by
rw [← e.coe_fn_toEquiv, ← e.toEquiv.eq_preimage_iff_image_eq, ← Equiv.image_symm_eq_preimage]
exact hc.2 (ht.image e.symm) ((e.toEquiv.subset_symm_image _ _).2 hf)
protected theorem IsMaxChain.isEmpty_iff (h : IsMaxChain r s) : IsEmpty α ↔ s = ∅ := by
refine ⟨fun _ ↦ s.eq_empty_of_isEmpty, fun h' ↦ ?_⟩
constructor
intro x
simp only [IsMaxChain, h', IsChain.empty, empty_subset, forall_const, true_and] at h
exact singleton_ne_empty x (h IsChain.singleton).symm
protected theorem IsMaxChain.nonempty_iff (h : IsMaxChain r s) : Nonempty α ↔ s.Nonempty :=
not_iff_not.mp <| by simpa [Set.not_nonempty_iff_eq_empty] using h.isEmpty_iff
theorem IsMaxChain.symm (h : IsMaxChain r s) : IsMaxChain (flip r) s :=
⟨h.isChain.symm, fun _ ht₁ ht₂ ↦ h.2 ht₁.symm ht₂⟩
open scoped Classical in
/-- Given a set `s`, if there exists a chain `t` strictly including `s`, then `SuccChain s`
is one of these chains. Otherwise it is `s`. -/
-- Note: `Set` has no computational content, but Lean still attempts to compile it.
-- See https://github.com/leanprover/lean4/issues/14084.
noncomputable def SuccChain (r : α → α → Prop) (s : Set α) : Set α :=
if h : ∃ t, IsChain r s ∧ SuperChain r s t then h.choose else s
theorem succChain_spec (h : ∃ t, IsChain r s ∧ SuperChain r s t) :
SuperChain r s (SuccChain r s) := by
have : IsChain r s ∧ SuperChain r s h.choose := h.choose_spec
simpa [SuccChain, dite_eq_left, exists_and_left.mp h] using this.2
theorem IsChain.succ (hs : IsChain r s) : IsChain r (SuccChain r s) := by
if h : ∃ t, IsChain r s ∧ SuperChain r s t then exact (succChain_spec h).1
else
rw [exists_and_left] at h
simpa [SuccChain, dite_eq_right, h] using hs
theorem IsChain.superChain_succChain (hs₁ : IsChain r s) (hs₂ : ¬IsMaxChain r s) :
SuperChain r s (SuccChain r s) := by
simp only [IsMaxChain, _root_.not_and, not_forall, exists_prop] at hs₂
obtain ⟨t, ht, hst⟩ := hs₂ hs₁
exact succChain_spec ⟨t, hs₁, ht, ssubset_iff_subset_ne.2 hst⟩
theorem subset_succChain : s ⊆ SuccChain r s := by
if h : ∃ t, IsChain r s ∧ SuperChain r s t then exact (succChain_spec h).2.1
else
simp [SuccChain, h]
theorem List.IsChain.isChain_setOfPred_mem [IsTrans α r] {l : List α} (h : l.IsChain r) :
_root_.IsChain r {a | a ∈ l} :=
h.pairwise.imp Relation.SymmGen.of_rel |>.set_pairwise
end Chain
/-! ### Flags -/
/-- The type of flags, aka maximal chains, of an order. -/
structure Flag (α : Type*) [LE α] where
/-- The `carrier` of a flag is the underlying set. -/
carrier : Set α
/-- By definition, a flag is a chain -/
Chain' : IsChain (· ≤ ·) carrier
/-- By definition, a flag is a maximal chain -/
max_chain' : ∀ ⦃s⦄, IsChain (· ≤ ·) s → carrier ⊆ s → carrier = s
namespace Flag
section LE
variable [LE α] {s t : Flag α} {a : α}
instance : SetLike (Flag α) α where
coe := carrier
coe_injective s t h := by
cases s
cases t
congr
instance : PartialOrder (Flag α) := .ofSetLike (Flag α)
@[ext]
theorem ext : (s : Set α) = t → s = t :=
SetLike.ext'
theorem mem_coe_iff : a ∈ (s : Set α) ↔ a ∈ s :=
Iff.rfl
@[simp]
theorem coe_mk (s : Set α) (h₁ h₂) : (mk s h₁ h₂ : Set α) = s :=
rfl
@[simp]
theorem mk_coe (s : Flag α) : mk (s : Set α) s.Chain' s.max_chain' = s :=
ext rfl
theorem chain_le (s : Flag α) : IsChain (· ≤ ·) (s : Set α) :=
s.Chain'
protected theorem maxChain (s : Flag α) : IsMaxChain (· ≤ ·) (s : Set α) :=
⟨s.chain_le, s.max_chain'⟩
theorem top_mem [OrderTop α] (s : Flag α) : (⊤ : α) ∈ s :=
s.maxChain.top_mem
theorem bot_mem [OrderBot α] (s : Flag α) : (⊥ : α) ∈ s :=
s.maxChain.bot_mem
/-- Reinterpret a maximal chain as a flag. -/
def ofIsMaxChain (c : Set α) (hc : IsMaxChain (· ≤ ·) c) : Flag α := ⟨c, hc.isChain, hc.2⟩
@[simp, norm_cast]
lemma coe_ofIsMaxChain (c : Set α) (hc) : ofIsMaxChain c hc = c := rfl
end LE
section Preorder
variable [Preorder α] [Preorder β] {a b : α} {s : Flag α}
protected theorem le_or_le (s : Flag α) (ha : a ∈ s) (hb : b ∈ s) : a ≤ b ∨ b ≤ a :=
s.chain_le.total ha hb
instance [OrderTop α] (s : Flag α) : OrderTop s :=
Subtype.orderTop s.top_mem
instance [OrderBot α] (s : Flag α) : OrderBot s :=
Subtype.orderBot s.bot_mem
instance [BoundedOrder α] (s : Flag α) : BoundedOrder s :=
Subtype.boundedOrder s.bot_mem s.top_mem
lemma mem_iff_forall_le_or_ge : a ∈ s ↔ ∀ ⦃b⦄, b ∈ s → a ≤ b ∨ b ≤ a :=
⟨fun ha b => s.le_or_le ha, fun hb =>
of_not_not fun ha =>
Set.ne_insert_of_notMem _ ‹_› <|
s.maxChain.2 (s.chain_le.insert fun c hc _ => hb hc) <| Set.subset_insert _ _⟩
/-- Flags are preserved under order isomorphisms. -/
def map (e : α ≃o β) : Flag α ≃ Flag β where
toFun s := ofIsMaxChain _ (s.maxChain.image e)
invFun s := ofIsMaxChain _ (s.maxChain.image e.symm)
left_inv s := ext <| e.symm_image_image s
right_inv s := ext <| e.image_symm_image s
@[simp, norm_cast] lemma coe_map (e : α ≃o β) (s : Flag α) : ↑(map e s) = e '' s := rfl
@[simp] lemma symm_map (e : α ≃o β) : (map e).symm = map e.symm := rfl
end Preorder
section PartialOrder
variable [PartialOrder α]
theorem chain_lt (s : Flag α) : IsChain (· < ·) (s : Set α) := s.chain_le.lt_of_le
instance [DecidableLE α] [DecidableLT α] [DecidableEq α] (s : Flag α) : LinearOrder s :=
{ Subtype.partialOrder _ with
le_total := fun a b => s.le_or_le a.2 b.2
toDecidableLE := Subtype.decidableLE
toDecidableLT := Subtype.decidableLT
toDecidableEq := Subtype.instDecidableEq }
end PartialOrder
instance [LinearOrder α] : Unique (Flag α) where
default := ⟨univ, isChain_of_trichotomous _, fun s _ => s.subset_univ.antisymm'⟩
uniq s := SetLike.coe_injective <| s.3 (isChain_of_trichotomous _) <| subset_univ _
end Flag