Cycles of a list #
Lists have an equivalence relation of whether they are rotational permutations of one another.
This relation is defined as IsRotated
.
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 representable 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
.
Equations
- List.nextOr [] x x = x
- List.nextOr [head] x x = x
- List.nextOr (y :: z :: xs) x x = if x = y then z else List.nextOr (z :: xs) x x
Instances For
nextOr
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
Equations
- List.next l x h = List.nextOr l x (List.get l { val := 0, isLt := (_ : 0 < List.length l) })
Instances For
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
Instances For
For consistency with EmptyCollection (List α)
.
Equations
- Cycle.instEmptyCollectionCycle = { emptyCollection := Cycle.nil }
An induction principle for Cycle
. Use as induction s using Cycle.induction_on
.
Equations
- Cycle.instMembershipCycle = { mem := Cycle.Mem }
Equations
- Cycle.instDecidableEqCycle s₁ s₂ = Quotient.recOnSubsingleton₂' s₁ s₂ fun x x_1 => decidable_of_iff' (Setoid.r x x_1) (_ : Quotient.mk'' x = Quotient.mk'' x_1 ↔ Setoid.r x x_1)
Equations
- Cycle.instDecidableMemCycleInstMembershipCycle x s = Quotient.recOnSubsingleton' s fun l => let_fun this := inferInstance; this
Reverse a s : Cycle α
by reversing the underlying List
.
Equations
- Cycle.reverse s = Quot.map List.reverse (_ : ∀ (x x_1 : List α), x ~r x_1 → List.reverse x ~r List.reverse x_1) s
Instances For
The length of the s : Cycle α
, which is the number of elements, counting duplicates.
Equations
- Cycle.length s = Quot.liftOn s List.length (_ : ∀ (x x_1 : List α), Setoid.r x x_1 → List.length x = List.length x_1)
Instances For
A s : Cycle α
that is at most one element.
Equations
- Cycle.Subsingleton s = (Cycle.length s ≤ 1)
Instances For
The s : Cycle α
contains no duplicates.
Equations
- Cycle.Nodup s = Quot.liftOn s List.Nodup (_ : ∀ (_l₁ _l₂ : List α), Setoid.r _l₁ _l₂ → List.Nodup _l₁ = List.Nodup _l₂)
Instances For
The s : Cycle α
as a Multiset α
.
Equations
- Cycle.toMultiset s = Quotient.liftOn' s Multiset.ofList (_ : ∀ (x x_1 : List α), Setoid.r x x_1 → ↑x = ↑x_1)
Instances For
Auxiliary decidability algorithm for lists that contain at least two unique elements.
Equations
Instances For
Equations
- Cycle.instDecidableNontrivial = Quot.recOnSubsingleton' s Cycle.decidableNontrivialCoe
Equations
- Cycle.instDecidableNodup = Quot.recOnSubsingleton' s List.nodupDecidable
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
Given a s : Cycle α
such that Nodup s
, retrieve the next element after x ∈ s
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Given a s : Cycle α
such that Nodup s
, retrieve the previous element before x ∈ s
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
We define a representation of concrete cycles, available when viewing them in a goal state or
via #eval
, when over representable 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.
Equations
- One or more equations did not get rendered due to their size.
As a function from a relation to a predicate, chain
is monotonic.