Cycles of a list #
THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.
Lists have an equivalence relation of whether they are rotational permutations of one another.
This relation is defined as is_rotated.
Based on this, we define the quotient of lists by the rotation relation, called cycle.
We also define a representation of concrete cycles, available when viewing them in a goal state or
via #eval, when over representatble types. For example, the cycle (2 1 4 3) will be shown
as c[2, 1, 4, 3]. Two equal cycles may be printed differently if their internal representation
is different.
Return the z such that x :: z :: _ appears in xs, or default if there is no such z.
next_or does not depend on the default value, if the next value appears.
Given an element x : α of l : list α such that x ∈ l, get the next
element of l. This works from head to tail, (including a check for last element)
so it will match on first hit, ignoring later duplicates.
For example:
- next [1, 2, 3] 2 _ = 3
- next [1, 2, 3] 3 _ = 1
- next [1, 2, 3, 2, 4] 2 _ = 3
- next [1, 2, 3, 2] 2 _ = 3
- next [1, 1, 2, 3, 2] 1 _ = 1
Given an element x : α of l : list α such that x ∈ l, get the previous
element of l. This works from head to tail, (including a check for last element)
so it will match on first hit, ignoring later duplicates.
- prev [1, 2, 3] 2 _ = 1
- prev [1, 2, 3] 1 _ = 3
- prev [1, 2, 3, 2, 4] 2 _ = 1
- prev [1, 2, 3, 4, 2] 2 _ = 1
- prev [1, 1, 2] 1 _ = 2
Equations
- cycle.has_coe = {coe := quot.mk setoid.r}
For consistency with list.has_emptyc.
Equations
- cycle.has_emptyc = {emptyc := cycle.nil α}
Equations
- cycle.inhabited = {default := cycle.nil α}
Equations
- cycle.has_mem = {mem := cycle.mem α}
Equations
- cycle.decidable_eq = λ (s₁ s₂ : cycle α), quotient.rec_on_subsingleton₂' s₁ s₂ (λ (l₁ l₂ : list α), decidable_of_iff' (setoid.r l₁ l₂) _)
Equations
- cycle.has_mem.mem.decidable x s = quotient.rec_on_subsingleton' s (λ (l : list α), list.decidable_mem x l)
The length of the s : cycle α, which is the number of elements, counting duplicates.
Equations
- s.length = quot.lift_on s list.length cycle.length._proof_1
A s : cycle α that is at most one element.
Equations
- s.subsingleton = (s.length ≤ 1)
A s : cycle α that is made up of at least two unique elements.
Instances for cycle.nontrivial
        
        
    The s : cycle α contains no duplicates.
Equations
- s.nodup = quot.lift_on s list.nodup cycle.nodup._proof_1
Instances for cycle.nodup
        
        
    The s : cycle α as a multiset α.
Equations
- s.to_multiset = quotient.lift_on' s coe cycle.to_multiset._proof_1
The multiset of lists that can make the cycle.
Equations
- s.lists = quotient.lift_on' s (λ (l : list α), ↑(l.cyclic_permutations)) cycle.lists._proof_1
Auxiliary decidability algorithm for lists that contain at least two unique elements.
Equations
- cycle.decidable_nontrivial_coe (x :: y :: l) = dite (x = y) (λ (h : x = y), decidable_of_iff' ↑(x :: l).nontrivial _) (λ (h : ¬x = y), decidable.is_true _)
- cycle.decidable_nontrivial_coe [x] = decidable.is_false _
- cycle.decidable_nontrivial_coe list.nil = decidable.is_false cycle.decidable_nontrivial_coe._main._pack._proof_1
Equations
- cycle.fintype_nodup_cycle = fintype.of_surjective (λ (l : {l // l.nodup}), ⟨↑(l.val), _⟩) cycle.fintype_nodup_cycle._proof_2
Equations
- cycle.fintype_nodup_nontrivial_cycle = fintype.subtype (finset.filter cycle.nontrivial (finset.map (function.embedding.subtype (λ (s : cycle α), s.nodup)) finset.univ)) cycle.fintype_nodup_nontrivial_cycle._proof_1
Given a s : cycle α such that nodup s, retrieve the next element after x ∈ s.
Equations
- cycle.next = λ (s : cycle α), quot.hrec_on s (λ (l : list α) (hn : cycle.nodup (quot.mk setoid.r l)) (x : α) (hx : x ∈ quot.mk setoid.r l), l.next x hx) cycle.next._proof_1
Given a s : cycle α such that nodup s, retrieve the previous element before x ∈ s.
Equations
- cycle.prev = λ (s : cycle α), quot.hrec_on s (λ (l : list α) (hn : cycle.nodup (quot.mk setoid.r l)) (x : α) (hx : x ∈ quot.mk setoid.r l), l.prev x hx) cycle.prev._proof_1
chain R s means that R holds between adjacent elements of s.
chain R ([a, b, c] : cycle α) ↔ R a b ∧ R b c ∧ R c a
Equations
- cycle.chain r c = quotient.lift_on' c (λ (l : list α), cycle.chain._match_1 r l) _
- cycle.chain._match_1 r (a :: m) = list.chain r a (m ++ [a])
- cycle.chain._match_1 r list.nil = true
As a function from a relation to a predicate, chain is monotonic.