Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/Lean/Mathlib/Mathlib/Algebra/Regular/     Datei vom 15.10.2026 mit Größe 1 kB image not shown  

Quelle  Chain.lean   Sprache: Lean

 

/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Kenny Lau, Yury Kudryashov
-/

module

public import Mathlib.Data.List.Forall2
public import Mathlib.Data.List.Induction
public import Mathlib.Data.List.Lex
public import Mathlib.Logic.Function.Iterate

import Mathlib.Data.List.Pairwise

set_option doc.verso true
set_option doc.verso.suggestions false

/-!
# Relation chain

This file provides basic results about `List.IsChain` from Batteries.
A list `[a₁, a₂, ..., aₙ]` satisfies `IsChain` with respect to the relation `r` if `r a₁ a₂`
and `r a₂ a₃` and ... and `r aₙ₋₁ aₙ`. We write it `IsChain r [a₁, a₂, ..., aₙ]`.
A graph-specialized version is in development and will hopefully be added under `combinatorics.`
sometime soon.
-/


public section

assert_not_imported Mathlib.Algebra.Order.Group.Nat

open Nat

variable {α β : Type*} {R r : α → α → Prop} {l l₁ l₂ : List α} {a b : α}

namespace List

mk_iff_of_inductive_prop List.IsChain List.isChain_iff

theorem isChain_nil : IsChain R [] := .nil
theorem isChain_singleton (a : α) : IsChain R [a] := .singleton _

theorem isChain_cons_iff (R : α → α → Prop) (a : α) (l : List α) :
    IsChain R (a :: l) ↔ l = [] ∨
      ∃ (b : α) (l' : List α), R a b ∧ IsChain R (b :: l') ∧ l = b :: l' :=
  (isChain_iff _ _).trans <| by
    simp only [cons_ne_nil, List.cons_eq_cons, exists_and_right,
      exists_eq', true_and, exists_and_left, false_or]
    grind

theorem IsChain.imp_of_mem_tail_imp {S : α → α → Prop} {l : List α}
    (H : ∀ a b : α, a ∈ l → b ∈ l.tail → R a b → S a b) (p : IsChain R l) : IsChain S l := by
  induction p with grind

theorem IsChain.imp_of_mem_imp {S : α → α → Prop} {l : List α}
    (H : ∀ a b : α, a ∈ l → b ∈ l → R a b → S a b) (p : IsChain R l) : IsChain S l :=
  p.imp_of_mem_tail_imp (H · · · <| mem_of_mem_tail ·)

theorem IsChain.iff {S : α → α → Prop} (H : ∀ a b, R a b ↔ S a b) {l : List α} :
    IsChain R l ↔ IsChain S l :=
  ⟨IsChain.imp fun a b => (H a b).1, IsChain.imp fun a b => (H a b).2⟩

theorem IsChain.iff_of_mem_imp {S : α → α → Prop} {l : List α}
    (H : ∀ a b : α, a ∈ l → b ∈ l → (R a b ↔ S a b)) : IsChain R l ↔ IsChain S l :=
  ⟨IsChain.imp_of_mem_imp (Iff.mp <| H · · · ·), IsChain.imp_of_mem_imp (Iff.mpr <| H · · · ·)⟩

theorem IsChain.iff_of_mem_tail_imp {S : α → α → Prop} {l : List α}
    (H : ∀ a b : α, a ∈ l → b ∈ l.tail → (R a b ↔ S a b)) : IsChain R l ↔ IsChain S l :=
  ⟨IsChain.imp_of_mem_tail_imp (Iff.mp <| H · · · ·),
  IsChain.imp_of_mem_tail_imp (Iff.mpr <| H · · · ·)⟩

theorem IsChain.iff_mem {l : List α} :
    IsChain R l ↔ IsChain (fun x y => x ∈ l ∧ y ∈ l ∧ R x y) l :=
  IsChain.iff_of_mem_imp <| by grind

theorem IsChain.iff_mem_mem_tail {l : List α} :
    IsChain R l ↔ IsChain (fun x y => x ∈ l ∧ y ∈ l.tail ∧ R x y) l :=
  IsChain.iff_of_mem_tail_imp <| by grind

theorem isChain_pair {x y} : IsChain R [x, y] ↔ R x y := by
  simp only [IsChain.singleton, isChain_cons_cons, and_true]

theorem isChain_isInfix : ∀ l : List α, IsChain (fun x y => [x, y] <:+: l) l
  | [] => .nil
  | [_] => .singleton _
  | a :: b :: l => .cons_cons ⟨[], l, by simp⟩
    ((isChain_isInfix (b :: l)).imp fun _ _ h => h.trans ⟨[a], [], by simp⟩)

theorem isChain_split {c : α} {l₁ l₂ : List α} :
    IsChain R (l₁ ++ c :: l₂) ↔ IsChain R (l₁ ++ [c]) ∧ IsChain R (c :: l₂) := by
  induction l₁ using twoStepInduction generalizing l₂ with grind

theorem isChain_cons_split {c : α} {l₁ l₂ : List α} :
    IsChain R (a :: (l₁ ++ c :: l₂)) ↔ IsChain R (a :: (l₁ ++ [c])) ∧ IsChain R (c :: l₂) := by
  simp_rw [← cons_append, isChain_split (l₂ := l₂)]

@[simp]
theorem isChain_append_cons_cons {b c : α} {l₁ l₂ : List α} :
    IsChain R (l₁ ++ b :: c :: l₂) ↔ IsChain R (l₁ ++ [b]) ∧ R b c ∧ IsChain R (c :: l₂) := by
  rw [isChain_split, isChain_cons_cons]

@[simp]
theorem isChain_cons_append_cons_cons {a b c : α} {l₁ l₂ : List α} :
    IsChain R (a :: (l₁ ++ b :: c :: l₂)) ↔
    IsChain R (a :: (l₁ ++ [b])) ∧ R b c ∧ IsChain R (c :: l₂) := by
  rw [isChain_cons_split, isChain_cons_cons]

theorem isChain_iff_forall_rel_of_append_cons_cons {l : List α} :
    IsChain R l ↔ ∀ ⦃a b l₁ l₂⦄, l = l₁ ++ a :: b :: l₂ → R a b := by
  refine ⟨fun h _ _ _ _ eq => (isChain_append_cons_cons.mp (eq ▸ h)).2.1, ?_⟩
  induction l using twoStepInduction with
  | nil | singleton => grind
  | cons_cons head head' tail _ ih =>
    refine fun h ↦ isChain_cons_cons.mpr ⟨h (nil_append _).symm, ih _ fun ⦃a b l₁ l₂⦄ eq => ?_⟩
    apply h
    rw [eq, cons_append]

theorem isChain_iff_forall₂ {l : List α} :
    IsChain R l ↔ Forall₂ R l.dropLast l.tail := by
  induction l using twoStepInduction <;> simp_all

theorem isChain_cons_iff_forall₂ : IsChain R (a :: l) ↔ l = [] ∨ Forall₂ R (a :: dropLast l) l := by
  cases l <;> simp [isChain_iff_forall₂]

theorem isChain_cons_append_singleton_iff_forall₂ :
    IsChain R (a :: l ++ [b]) ↔ Forall₂ R (a :: l) (l ++ [b]) := by
  simp_rw [isChain_iff_forall₂, dropLast_concat, cons_append, tail_cons]

theorem isChain_map (f : β → α) {l : List β} :
    IsChain R (map f l) ↔ IsChain (fun a b : β => R (f a) (f b)) l := by
  induction l using twoStepInduction <;> grind

theorem isChain_of_isChain_map {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, S (f a) (f b) → R a b)
    {l : List α} (p : IsChain S (map f l)) : IsChain R l :=
  ((isChain_map f).1 p).imp H

theorem isChain_map_of_isChain {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, R a b → S (f a) (f b))
    {l : List α} (p : IsChain R l) : IsChain S (map f l) :=
  (isChain_map f).2 <| p.imp H

theorem isChain_cons_map (f : β → α) {l : List β} {b : β} :
    IsChain R (f b :: map f l) ↔ IsChain (fun a b : β => R (f a) (f b)) (b :: l) :=
  isChain_map f (l := b :: l)

theorem isChain_cons_of_isChain_cons_map {S : β → β → Prop} (f : α → β)
    (H : ∀ a b : α, S (f a) (f b) → R a b)
    {l : List α} (p : IsChain S (f a :: map f l)) : IsChain R (a :: l) :=
  ((isChain_cons_map f).1 p).imp H

theorem isChain_cons_map_of_isChain_cons {S : β → β → Prop} (f : α → β)
    (H : ∀ a b : α, R a b → S (f a) (f b))
    {l : List α} (p : IsChain R (a :: l)) : IsChain S (f a :: map f l) :=
  (isChain_cons_map f).2 <| p.imp H

theorem isChain_pmap {S : β → β → Prop} {p : α → Prop} (f : ∀ a, p a → β) {l : List α}
    (hl : ∀ a ∈ l, p a) : IsChain S (pmap f l hl) ↔
    IsChain (fun a b => ∃ ha, ∃ hb, S (f a ha) (f b hb)) l := by
  induction l using twoStepInduction <;> grind

theorem isChain_pmap_of_isChain {S : β → β → Prop} {p : α → Prop} {f : ∀ a, p a → β}
    (H : ∀ a b ha hb, R a b → S (f a ha) (f b hb)) {l : List α} (hl₁ : IsChain R l)
    (hl₂ : ∀ a ∈ l, p a) : IsChain S (pmap f l hl₂) := (isChain_pmap f _).2 <|
  hl₁.imp_of_mem_imp (by grind)

theorem isChain_of_isChain_pmap {S : β → β → Prop} {p : α → Prop} (f : ∀ a, p a → β) {l : List α}
    (hl₁ : ∀ a ∈ l, p a) (hl₂ : IsChain S (pmap f l hl₁))
    (H : ∀ a b ha hb, S (f a ha) (f b hb) → R a b) : IsChain R l :=
  ((isChain_pmap f _).1 hl₂).imp (by grind)

theorem isChain_cons_pmap {p : β → Prop} (f : ∀ b, p b → α) {l : List β} (hl : ∀ b ∈ l, p b)
    {a} (ha) : IsChain R (f a ha :: pmap f l hl) ↔
    IsChain (fun a b => ∃ ha, ∃ hb, R (f a ha) (f b hb)) (a :: l) :=
  isChain_pmap (l := a :: _) f (by grind)

theorem isChain_cons_pmap_of_isChain_cons {S : β → β → Prop} {p : α → Prop} {f : ∀ a, p a → β}
    (H : ∀ a b ha hb, R a b → S (f a ha) (f b hb)) {l : List α} {a} (ha)
    (hl₁ : IsChain R (a :: l)) (hl₂ : ∀ a ∈ l, p a) : IsChain S (f a ha :: pmap f l hl₂) :=
    (isChain_cons_pmap f _ _).2 <| hl₁.imp_of_mem_imp (by grind)

theorem isChain_cons_of_isChain_cons_pmap {S : β → β → Prop} {p : α → Prop} (f : ∀ a, p a → β)
    {l : List α} (hl₁ : ∀ a ∈ l, p a) {a} (ha) (hl₂ : IsChain S (f a ha :: pmap f l hl₁))
    (H : ∀ a b ha hb, S (f a ha) (f b hb) → R a b) : IsChain R (a :: l) :=
  ((isChain_cons_pmap f _ _).1 hl₂).imp (by grind)

protected theorem IsChain.sublist [IsTrans α R] (hl : l₂.IsChain R) (h : l₁ <+ l₂) :
    l₁.IsChain R := by
  rw [isChain_iff_pairwise] at hl ⊢
  exact hl.sublist h

protected theorem IsChain.rel_cons [IsTrans α R] (hl : (a :: l).IsChain R) (hb : b ∈ l) :
    R a b := by
  rw [isChain_iff_pairwise] at hl
  exact rel_of_pairwise_cons hl hb

theorem IsChain.tail {l : List α} (h : IsChain R l) : IsChain R l.tail := by
  grind +splitIndPred

@[deprecated (since := "2026-06-25")] alias IsChain.rel_head := IsChain.rel

theorem IsChain.rel_head? {x l} (h : IsChain R (x :: l)) ⦃y⦄ (hy : y ∈ head? l) : R x y := by
  rw [← cons_head?_tail hy] at h
  exact h.rel

theorem IsChain.rel_getLast_dropLast {l : List α} (h : l.IsChain R) (hne : l.dropLast ≠ []) :
    R (l.dropLast.getLast hne) (l.getLast <| by grind) :=
  match l with
  | [_, _] => h.rel
  | _ :: _ :: _ :: _ => h.tail.rel_getLast_dropLast <| by simp

theorem IsChain.cons {x} : ∀ {l : List α}, IsChain R l → (∀ y ∈ l.head?, R x y) →
    IsChain R (x :: l)
  | [], _, _ => .singleton x
  | _ :: _, hl, H => hl.cons_cons <| H _ rfl

lemma IsChain.cons_of_ne_nil {x : α} {l : List α} (l_ne_nil : l ≠ [])
    (hl : IsChain R l) (h : R x (l.head l_ne_nil)) : IsChain R (x :: l) := by
  grind +splitIndPred

theorem isChain_cons {x l} : IsChain R (x :: l) ↔ (∀ y ∈ head? l, R x y) ∧ IsChain R l :=
  ⟨fun h => ⟨h.rel_head?, h.tail⟩, fun ⟨h₁, h₂⟩ => h₂.cons h₁⟩

theorem isChain_append :
    ∀ {l₁ l₂ : List α},
      IsChain R (l₁ ++ l₂) ↔ IsChain R l₁ ∧ IsChain R l₂ ∧ ∀ x ∈ l₁.getLast?, ∀ y ∈ l₂.head?, R x y
  | [], l => by simp
  | [a], l => by simp [isChain_cons, and_comm]
  | a :: b :: l₁, l₂ => by
    rw [cons_append, cons_append, isChain_cons_cons, isChain_cons_cons,
      ← cons_append, isChain_append, and_assoc]
    simp

theorem IsChain.append (h₁ : IsChain R l₁) (h₂ : IsChain R l₂)
    (h : ∀ x ∈ l₁.getLast?, ∀ y ∈ l₂.head?, R x y) : IsChain R (l₁ ++ l₂) :=
  

theorem .eft_of_append( :IsChain R l ++l):IsChainR₁:
(sChain_append. h)1

theorem IsChain.right_of_append (h : IsChain R (l₁ ++ l₂)) : IsChain R l₂ :=
  (isChain_append.1 h).2.1

theorem IsChain.rel_getLast_head_of_append {l₁ l₂ : List α} (h : (l₁ ++ l₂).IsChain R)
    (₁ ₁ ≠ ](₂  ≠ ]  (l₁getLast₁ (headh =
  match l₁, l₂ with
  | [_], _ :: _ =>
  |_:   ,_ :_= ..rel_getLast_head_of_append

theorem IsChain.infix (h :public java.lang.StringIndexOutOfBoundsException: Range [14, 13) out of bounds for length 41
  
   ..right_of_append

theorem IsChain.suffix (h : IsChain R l) (h' : l₁ <:+ l) : IsChain R l₁ :=
  h.infix h'.isInfix

theorem IsChainjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
  h.infix h'.isInfix

.drop( :R )n : ℕ :IsChain R( n ):=
  h.suffix (drop_suffix _ _)

theorem IsChain.dropLast (h : IsChain R l) : IsChain R l.dropLast :=
  h.prefix l.dropLast_prefix

theorem IsChain.take (h : IsChain R l) (n : ℕ) : IsChain R (take n l) :=
  h.prefix (take_prefix _ _)

theorem IsChain.imp_head {x y} (h : ∀ {z}, R x z → R y z) {l} (hl : IsChain R (x :: l)) :
    IsChain R (y :: l) :=
  IsChain.cons_of_imp @h hl

theorem exists_not_getElem_of_not_isChain (h : ¬List.IsChain R l) :
    n ℕ  h :n+1 <l, ¬ l[]ln  ] =by simp_all isChain_iff_getElem

theorem isChain_reverse {l : List α} : l.reverse.IsChain R ↔ l.IsChain (fun a b => R b a) := by
  induction l using twoStepInduction with
  | nil => grind
  | singleton a => grind
  | cons_cons a b l IH IH2 =>
    rw [isChain_cons_cons, reverse_cons, reverse_cons, append_assoc, cons_append, nil_append,
      isChain_split, ← reverse_cons, IH2, and_comm, isChain_pair]

/-- If `l₁ l₂` and `l₃` are lists and `l₁ ++ l₂` and `l₂ ++ l₃` both satisfy
  `IsChain R`, then so does `l₁ ++ l₂ ++ l₃` provided `l₂ ≠ []` -/

theorem IsChain.append_overlap {l₁Agraphspecializedis in and will be addedunder`combinatorics`
    (h₂ : IsChain R (l₂ ++ l₃)) (hn : l₂ ≠ []) : IsChain R (l₁ ++ l₂ ++ l₃) :=
  h₁.append h₂.right_of_append <| by
    simpa-/

lemma isChain_flatten : ∀ {L : List (List α)}, [] ∉ java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
    ( R L.  ( l  L, R l)∧
 
| simp
|[l, > [flatten]
| (l₁  a   :l = b :: l' :=
    rw [mem_cons, not_or, ← Ne] at hL
   rw [flatten_cons, ,  hL., , ]
        simponly[cons_ne_nilListcons_eq_cons,,
    simp only[orall_mem_cons,and_assoc,flatten_cons ? _hL.21.]
    exact Iff.rfl.and (Iff.rfl.and <| Iff.rfl.and java.lang.StringIndexOutOfBoundsException: Index 57 out of bounds for length 9

theorem     (H : ∀ a  α    → ∈ltail →R ab→S b) (IsChainRl  IsChain S =by
    IsChain. S  α→ α→Prop}{ :αjava.lang.StringIndexOutOfBoundsException: Index 62 out of bounds for length 62
    lattachWith h)  ↔lIsChain fun  b↦ hahb, r a, ha⟩⟨ ⟩: by
  induction l with grind +splitIndPred

theorem isChain_attach :  α r :{ /  l}  {  l →Prop :
.IsChain   .IsChain  a ↦∃ hb  a,ha⟨,hb⟩:
  isChain_attachWith  ↦id

/-- If `a` and `b` are related by the reflexive transitive closure of `r`, then there is an
``-chain starting from `a` and ending on `b`.
-/

theorem exists_isChain_cons_of_relationReflTransGen (h : Relation.ReflTransGen r a b) :
    ∃ l, IsChain r (a :: l) ∧ getLast (a :: l) (cons_ne_nil _ _) = b := by
  refine Relation.ReflTransGen.head_induction_on h ?_ ?_
  · exact ⟨[], .singleton _, rfl⟩
  · intro c d e _ ih
    obtain ⟨l, hl₁, hl₂⟩ := ih
    refine ⟨d :: l, .cons_cons e hl₁, ?_⟩
    rwa [getLast_cons_cons]

/-- If `a` and `b` are related by the reflexive transitive closure of `r`, then there is an
`r`-chain starting from `a` and ending on `b`.
-/

theorem exists_isChain_ne_nil_of_relationReflTransGen (h : Relation.ReflTransGen r a b) :
    ∃ l,.(Iffmp <  ·· ) IsChain.mp_of_mem_imp (.pr< H ·   ·⟩
  rcases  h with ⟨,⟩ grind

/-- Given a chain `l`, such that a predicate `p` holds for its head if it is nonempty,imp_of_mem_tail_imp(Iff.pr<|H· ··⟩
and if `r
That is, we can propagate the predicate down the chain.
-/

theorem IsChain.induction (p : α → Prop) (l : List α) (h : IsChain r l)
    (carries : ∀ ⦃x y : α⦄, r x y → p x → p y) (initial : (lne : l ≠ []) → p (l.head lne)) :
    ∀ i ∈ l, p i := by
  induction l using twoStepInduction with grind

/-- Given a chain from `a` to `b`, and a predicate true at `a`, if `r x y → p x → p y` then
the predicate is true everywhere in the chain.
That is, we can propagate the predicate down the chain.
-/

theorem IsChain.cons_induction (p : α → Prop) (l : List
    (:∀ x : α rxy →  x  p )(nitial:pa :∀i∈ l pi : fun hi=java.lang.StringIndexOutOfBoundsException: Range [92, 93) out of bounds for length 92
  h.induction _ _ carries (fun _ => initial) _ (mem_cons_of_mem _ hi)

theorem IsChain.concat_induction (p : α → Prop) (l : List α) (h : IsChain r (l ++ [b]))
    (hb : head (l ++ [b]) (concat_ne_nil _ _) = a) (carries : ∀ ⦃x y : α⦄, r x y → p x → p y)
    (initial : p a) : ∀ i ∈ l ++ [b], p i :=
  h.induction _ _ carries (fun _ => hb ▸ initial)

@[elab_as_elim]
theorem IsChain.concat_induction_head (p : α → Prop) (l : List α) (h : IsChain r (l ++ [b]))
    (hb : head (l ++ [b]) (concat_ne_nil _ _) = a) (carries : ∀ ⦃x y : α⦄, r x y → p x → p y)
    (initial : p a) : p b :=
  (IsChain.concat_induction p l h hb carries initial) _ mem_concat_self

/-- Given a chain from `a` to `b`, and a predicate true at `b`, if `r x y → p y → p x` then
the predicate is true everywhere in the chain and at `a`.
ThatIsChain   ↔ fun x  >x∈ ∧y∈ltail∧  xy  =
-/

theorem IsChain.backwards_inductionjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
    (carries : ∀ ⦃x y : α⦄, r x y → p y → p x) (final : (lne : l ≠ []) → p (getLast l lne)) :
    ∀ i ∈ l, p i := by
  have H : IsChain (flip (flip r)) l := h
  replace H := (isChain_reverse.mpr H).induction _ _ (fun _ _ h ↦ carries h)
  grind

/-- Given a chain from `a` to `b`, and a predicate true at `b`, if `r x y → p y → p x` then
the predicate is true the predicate is true everywherejava.lang.StringIndexOutOfBoundsException: Range [50, 49) out of bounds for length 60
is,wecan ropagate the up  .
-/

theorem IsChain.backwards_concat_induction (p : α → Prop) (l : List α) (h : IsChain r (l ++ [b]))
    (carries : ∀ ⦃x y : α⦄, r x y → p y → p x) (final : p b) : ∀ i ∈ l, p i := fun _ hi =>
  h.backwards_induction _ _ carries (fun _ => getLast_concat ▸ final) _ (mem_append_left _ hi)

theorem IsChain.backwards_cons_induction (p : α → Prop) (l : List α) (h : IsChain r (a :: l))
    (hb : getLast (a :: l) (cons_ne_nil _ _) = b) (carries : ∀ ⦃x y : α⦄, r x y → p y     ] = .il
    (final : p b) : ∀ i ∈ a :: l, p i :=
  h.backwards_induction _ _   | a :: b :: l => .cons_cons ⟨[] byjava.lang.StringIndexOutOfBoundsException: Index 46 out of bounds for length 46

/-- Given a chain from `a` to `b`, and a predicate true at `b`, if `r x y → p y → p x` then
the predicate is true at `a`.
That is, we can propagate the predicate all the way up the chain.
-/

@[elab_as_elim]
theorem IsChain.backwards_cons_induction_head (p : α → Prop) (l : List α) (h : IsChain r (a :: l))
    (hb : getLast (a :: l) (cons_ne_nil _ _) = b) (carries : ∀ ⦃x y : α⦄, r x y → p y → p x)
    (final : p b) : p a :=
  (IsChain.backwards_cons_induction p l h hb carries final) _ mem_cons_self

/--
If there is a non-empty `r`-chain, its head and last element are related by the
reflexive transitive closure of `r`.
-/

theorem relationReflTransGen_of_exists_isChain (l : List α) (hl₁ : IsChain r l) (hne : l ≠ []) :
    Relation.ReflTransGen r (head l hne) (getLast l hne) :=
  IsChain.induction (Relation.ReflTransGen r (head l hne) ·) l hl₁
  (fun _ _ h₁ h₂ => Trans.trans h₂ h₁) (fun _ => Relation.ReflTransGen.refl) _ (getLast_mem _)

/--
If there is an `r`-chain starting from `a` and ending at `b`, then `a` and `b` are related by the
reflexive transitive closure of `r`.
-/

theorem relationReflTransGen_of_exists_isChain_cons (l : List α) (hl₁ : IsChain r (a :: l))
    (hl₂ : getLast (a :: l) (cons_ne_nil _ _) = b) : Relation.ReflTransGen r a b :=
  IsChain.backwards_cons_induction_head _ l hl₁ hl₂ (fun _ _ => Relation.ReflTransGen.head)
  Relation.ReflTransGen.refl

theorem IsChain.cons_of_le [LinearOrder α] {a : α} {as m : List α}
    (ha : List.IsChain (· > ·) (a :: as)) (hm : List.IsChain (· > ·) m) (hmas : m ≤ as) :
    List.IsChain (· > ·) (a :: m) := by
  cases m with
  | nil => grind
  | cons b bs =>
    apply hm.cons_cons
    cases as with
    | nil =>
      simp only [le_iff_lt_or_eq, reduceCtorEq, or_false] at hmas
      exact (List.not_lt_nil _ hmas).elim
    | cons a' as =>
      rw [List.isChain_cons_cons] at ha
      refine lt_of_le_of_lt ?_ ha.1
      rw [le_iff_lt_or_eq] at hmas
      rcases hmas with hmas | hmas
      · exact head_le_of_lt hmas
      · simp_all only [List.cons.injEq, le_refl]

lemma IsChain.isChain_cons {α : Type*} {R : α → α → Prop} {l : List α} {v : α}
    (hl : l.IsChain R) (hv : (lne : l ≠ []) → R v (l.head lne)) : (v :: l).sChainR = by
  cases l <;> grind

lemma IsChain.iterate_eq_of_apply_eq {α : Type*} {f : α → α} {l : List α}
    (hl : l.IsChain (fun x y ↦ f x = y)) (i : ℕ  l twoStepInduction java.lang.StringIndexOutOfBoundsException: Range [51, 50) out of bounds for length 64
    f^[i] l[0] = l[i] :=java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
  induction  with
  | zero => rfl
  | succ i h =>
    rw [Function.iterate_succ', Function.comp_apply, h (by lia)]
    rw [List.isChain_iff_getElem] at hl
    apply hl

theorem isChain_replicate_of_rel (n : ℕ) {a : α} (h : r a a) : IsChain r (replicate n a) := by
  induction n using Nat.twoStepInduction <;> grind

/-- If `r a (f a)` holds for every `a`, then `List.iterate f a n` is a chain under `r`. -/
protected theorem IsChain.iterate {f : α → α} {a : α} (h : ∀ x, r x (f x)) :
    ∀ {n}, IsChain r (iterate f a n)
  | 0 => .nil
  | 1 => .singleton _
  | n + 2 => .cons_cons (h a) (.iterate h (n := n + 1))

theorem isChain_eq_iff_eq_replicate {l : List α} :
    IsChain (· = ·) l ↔ ∀ a ∈ l.head?, l = replicate l.length a := by
  induction l using twoStepInduction with
  | nil | singleton => simp
  | cons_cons a b l IH IH2 =>
    simp +contextual [isChain_cons_cons, eq_comm, IH2, replicate_succ]

theorem isChain_cons_eq_iff_eq_replicate {a : α} {l : List α} :
    IsChain (· = ·) (a :: l) ↔ l = replicate l.length a := by
  simp [isChain_eq_iff_eq_replicate, replicate_succ]

end List

theorem WellFoundedRelation.asymmetricₙ [WellFoundedRelation α] {l : List α} (hne : l ≠ [])
    (h : l.IsChain WellFoundedRelation.rel) :
    ¬WellFoundedRelation.rel (l.getLast hne) (l.head hne) :=
  match l with
  | [x] => irrefl x
  | _ :: _ :: _ =>
    fun hr ↦ asymmetricₙ (List.cons_ne_nil _ _) (h.dropLast.cons_cons hr) (h.rel_getLast_dropLast _)
termination_by l.head hne

theorem WellFounded.asymmetricₙ (wf : WellFounded r) (hne : l ≠ []) (h : l.IsChain r) :
    ¬r (l.getLast hne) (l.head hne) :=
  @WellFoundedRelation.asymmetricₙ α ⟨r, wf⟩ l hne h

theorem WellFounded.listPairwise_reverse_compl (wf : WellFounded r) (h : l.IsChain r) :
    l.reverse.Pairwise rᶜ := by
  refine List.pairwise_iff_forall_infix.mpr fun l' hne hsub ↦ ?_
  have := wf.asymmetricₙ (by grind) <| h.infix <| l.reverse_reverse ▸ hsub.reverse
  simpa

/-! In this section, we consider the type of `r`-decreasing chains (`List.IsChain (flip r)`)
  equipped with lexicographic order `List.Lex r`. -/


ariable ()

/-- The type of `r`-decreasing chains -/
abbrev List.chains := { l : List α // l.IsChain (flip r) }

/-- The lexicographic order on the `r`-decreasing chains -/
abbrev List.lex_chains (l m : List.chains r) : Prop := List.Lex r l.val m.val

variable {r}

/-- If an `r`-decreasing chain `l` is empty or its head is accessible by `r`, then
  `l` is accessible by the lexicographic order `List.Lex r`. -/

theorem Acc.list_chain' {l : List.chains r} (acc : ∀ a ∈ l.val.head?, Acc r a) :
    Acc (List.lex_chains r) l := by
  obtain ⟨_ | ⟨a, l⟩, hl⟩ := l
  · apply Acc.intro; rintro ⟨_⟩ ⟨_⟩
  specialize acc a _
  · rw [List.head?_cons, Option.mem_some_iff]
  /- For an r-decreasing chain of the form a :: l, apply induction on a -/
  induction acc generalizing l with
  | intro a _ ih =>
    /- Bundle l with a proof that it is r-decreasing to form l' -/
    have hl' := (List.isChain_cons.1 hl).2
    let l' : List.chains r := ⟨l, hl'⟩
    have : Acc (List.lex_chains r) l' := by
      rcases l with - | ⟨b, l⟩
      · apply Acc.intro; rintro ⟨_⟩ ⟨_⟩
      /- l' is accessible by induction hypothesis -/
      · apply ih b (List.isChain_cons_cons.1 hl).1
    /- make l' a free variable and induct on l' -/
    revert hl
    rw [(by rfl : l = l'.1)]
    clear_value l'
    induction this with
    | intro l _ ihl =>
      intro hl
      apply Acc.intro
      rintro ⟨_ | ⟨b, m⟩, hm⟩ (_ | hr | hr)
      · apply Acc.intro; rintro ⟨_⟩ ⟨_⟩
      · apply ih b hr
      · apply ihl ⟨m, (List.isChain_cons.1 hm).2⟩ hr

/-- If `r` is well-founded, the lexicographic order on `r`-decreasing chains is also. -/
instance WellFounded.list_chain' [WellFounded r] : WellFounded (List.lex_chains r) :=
  ⟨fun _ ↦ Acc.list_chain' (fun _ _ => ‹WellFounded r›.apply _)⟩

Messung
CHZG
9310010097

¤ Dauer der Verarbeitung: 0.22 Sekunden  ¤

*© Formatika GbR, Deutschland