Physlib

Physlib.Mathematics.List

58 declarations · 2 submodules

Declarations

theorem

takeWhile\text{takeWhile} of eraseIdx\text{eraseIdx} equals eraseIdx\text{eraseIdx} of takeWhile\text{takeWhile}

#takeWile_eraseIdx

Let II be a type and PP be a decidable predicate on II. Let ll be a list of elements of type II. Suppose that the predicate PP is downward-closed with respect to the indices of ll; that is, for any indices 0j<k<length(l)0 \le j < k < \text{length}(l), if the element at index kk satisfies PP, then the element at index jj must also satisfy PP. Then, for any index iNi \in \mathbb{N}, it holds that takeWhile(P,eraseIdx(l,i))=eraseIdx(takeWhile(P,l),i)\text{takeWhile}(P, \text{eraseIdx}(l, i)) = \text{eraseIdx}(\text{takeWhile}(P, l), i) where eraseIdx(l,i)\text{eraseIdx}(l, i) denotes the list ll with the element at index ii removed, and takeWhile(P,l)\text{takeWhile}(P, l) is the longest prefix of ll consisting only of elements that satisfy PP.

theorem

dropWhile\text{dropWhile} of a list with an erased element for prefix-closed predicates

#dropWile_eraseIdx

Let II be a type and PP be a decidable predicate on II. Let ll be a list of elements of type II and ii be a natural number representing an index. Suppose that for any indices jj and kk of the list ll, if j<kj < k and the element at index kk satisfies PP, then the element at index jj also satisfies PP. Then, the result of removing the prefix of elements satisfying PP from the list ll after erasing the element at index ii is given by: dropWhile P(eraseIdx li)={eraseIdx (dropWhile Pl)(ilength(takeWhile Pl))if length(takeWhile Pl)idropWhile Plotherwise \text{dropWhile } P (\text{eraseIdx } l \, i) = \begin{cases} \text{eraseIdx } (\text{dropWhile } P \, l) (i - \text{length}(\text{takeWhile } P \, l)) & \text{if } \text{length}(\text{takeWhile } P \, l) \leq i \\ \text{dropWhile } P \, l & \text{otherwise} \end{cases} where takeWhile Pl\text{takeWhile } P \, l is the prefix of ll consisting of elements satisfying PP, dropWhile Pl\text{dropWhile } P \, l is the remainder of the list, and eraseIdx li\text{eraseIdx } l \, i is the list ll with the element at index ii removed.

theorem

insertionSort(L)=L|\text{insertionSort}(L)| = |L|

#insertionSort_length

For any type II equipped with a decidable binary relation \le, and for any list LL of elements in II, the length of the list after applying the insertion sort algorithm using \le is equal to the length of the original list LL. That is, insertionSort(,L)=L|\text{insertionSort}(\le, L)| = |L|.

definition

Position of r0r_0 in `List.orderedInsert`

#orderedInsertPos

Given a type II with a decidable binary relation \le (denoted as `le1`), a list rr, and an element r0r_0, this function returns the index at which r0r_0 is placed when inserted into rr via the `List.orderedInsert` operation. The index is defined as the length of the prefix of rr consisting of elements bb such that the condition r0br_0 \le b is false. The returned value is an index of type Fin(n)\text{Fin}(n), where nn is the length of the list after insertion.

theorem

orderedInsertPos(,r,r0)<r0::r\text{orderedInsertPos}(\le, r, r_0) < |r_0 :: r|

#orderedInsertPos_lt_length

Given a type II with a decidable binary relation \le (denoted as `le1`), for any list rr in II and any element r0Ir_0 \in I, the index orderedInsertPos(,r,r0)\text{orderedInsertPos}(\le, r, r_0) at which r0r_0 is inserted into rr is strictly less than the length of the list formed by prepending r0r_0 to rr, denoted by r0::r|r_0 :: r|.

theorem

(orderedInsert r0r)[orderedInsertPos rr0]=r0(\text{orderedInsert } \le r_0 r)[\text{orderedInsertPos } \le r r_0] = r_0

#orderedInsert_get_orderedInsertPos

Let II be a type with a decidable binary relation \le (denoted as `le1`). For any list rr of elements in II and any element r0r_0 in II, the element at index i=orderedInsertPos(,r,r0)i = \text{orderedInsertPos}(\le, r, r_0) in the list resulting from the ordered insertion orderedInsert(,r0,r)\text{orderedInsert}(\le, r_0, r) is r0r_0. That is, (orderedInsert r0r)[i]=r0(\text{orderedInsert } \le r_0 r)[i] = r_0, where ii is the position at which r0r_0 is placed during the insertion.

theorem

Removing the element at the `orderedInsert` position results in the original list

#orderedInsert_eraseIdx_orderedInsertPos

Let II be a type with a decidable binary relation 1\le_1. For any list rr of elements in II and any element r0Ir_0 \in I, let ii be the index where r0r_0 is placed when inserted into rr via the ordered insertion function `List.orderedInsert` (this index is given by `orderedInsertPos`). If the element at index ii is removed from the resulting list using `eraseIdx`, the remaining list is equal to the original list rr.

theorem

Recursive formula for the insertion position of r0r_0 into r1::rr_1 :: r

#orderedInsertPos_cons

Let II be a type with a decidable binary relation \le. For a list rr and elements r0,r1Ir_0, r_1 \in I, let P(r,r0)P(r, r_0) denote the index at which r0r_0 is placed when inserted into rr via an ordered insertion. Then the index P(r1::r,r0)P(r_1 :: r, r_0) is given by: P(r1::r,r0)={0if r0r1P(r,r0)+1otherwiseP(r_1 :: r, r_0) = \begin{cases} 0 & \text{if } r_0 \le r_1 \\ P(r, r_0) + 1 & \text{otherwise} \end{cases}

theorem

The insertion position of k,a\langle k, a \rangle into a list of sigma types is determined by the insertion position of kk into the list of projected indices.

#orderedInsertPos_sigma

Let II be a type with a decidable binary relation \le. Let f:ITypef : I \to \text{Type} be a type family. Consider a list ll of dependent pairs i,x\langle i, x \rangle (where iIi \in I and xf(i)x \in f(i)), and let pr1\text{pr}_1 denote the projection map that sends i,x\langle i, x \rangle to ii. For any kIk \in I and af(k)a \in f(k), the index at which the pair k,a\langle k, a \rangle is inserted into ll (using the relation defined by \le on the first components) is equal to the index at which kk is inserted into the list map(pr1,l)\text{map}(\text{pr}_1, l) using the relation \le.

theorem

Elements at Indices i<pos(r0)i < \text{pos}(r_0) are Unchanged by `List.orderedInsert`

#orderedInsert_get_lt

Let II be a type equipped with a decidable binary relation \le. For a list rr of elements in II and an element r0Ir_0 \in I, let pp be the index where r0r_0 is inserted into rr using the ordered insertion operation `List.orderedInsert`. For any index i<pi < p, the element at index ii in the list resulting from the insertion is equal to the element at index ii in the original list rr.

theorem

The prefix of `orderedInsert` before the insertion point equals the `takeWhile` prefix of the original list.

#orderedInsertPos_take_orderedInsert

Let II be a type with a decidable binary relation \le. For any list rr of elements in II and an element r0Ir_0 \in I, let pp be the index at which r0r_0 is inserted into rr via the ordered insertion operation (denoted by orderedInsertPos(,r,r0)\text{orderedInsertPos}(\le, r, r_0)). Then, the prefix of the resulting list orderedInsert(,r0,r)\text{orderedInsert}(\le, r_0, r) consisting of the first pp elements is equal to the prefix of the original list rr obtained by taking elements bb such that the condition r0br_0 \le b is false.

theorem

List.take(p,r)=List.take(p,orderedInsert(,r0,r))\text{List.take}(p, r) = \text{List.take}(p, \text{orderedInsert}(\le, r_0, r)) where pp is the insertion position

#orderedInsertPos_take_eq_orderedInsert

Let II be a type equipped with a decidable binary relation \le. For a list rr of elements in II and an element r0Ir_0 \in I, let p=orderedInsertPos(,r,r0)p = \text{orderedInsertPos}(\le, r, r_0) be the index where r0r_0 is inserted into rr via the ordered insertion operation. Then the first pp elements of the original list rr are identical to the first pp elements of the list resulting from the insertion. That is: \[ \text{List.take}(p, r) = \text{List.take}(p, \text{List.orderedInsert}(\le, r_0, r)) \]

theorem

List.drop(i,r)=List.drop(i+1,orderedInsert(,r0,r))\text{List.drop}(i, r) = \text{List.drop}(i + 1, \text{orderedInsert}(\le, r_0, r)) where ii is the insertion position of r0r_0

#orderedInsertPos_drop_eq_orderedInsert

For a type II equipped with a decidable binary relation \le, a list rr, and an element r0r_0, let ii be the position where r0r_0 is inserted into rr (formally defined as i=orderedInsertPos(,r,r0)i = \text{orderedInsertPos}(\le, r, r_0)). Then the suffix of the original list rr starting at index ii is equal to the suffix of the list resulting from the ordered insertion starting at index i+1i+1. That is: \[ \text{List.drop}(i, r) = \text{List.drop}(i + 1, \text{List.orderedInsert}(\le, r_0, r)) \]

theorem

List.take(i,r)=List.takeWhile(λb. ¬(r0b),r)\text{List.take}(i, r) = \text{List.takeWhile}(\lambda b.\ \neg(r_0 \le b), r) where ii is the insertion position of r0r_0

#orderedInsertPos_take

For a type II equipped with a decidable binary relation \le, a list rr, and an element r0Ir_0 \in I, let ii be the index at which r0r_0 would be placed when inserted into rr via an ordered insertion operation (defined as i=orderedInsertPos(,r,r0)i = \text{orderedInsertPos}(\le, r, r_0)). The theorem states that the prefix of the original list rr of length ii is equal to the prefix of rr obtained by taking elements bb as long as the condition r0br_0 \le b is false. That is: \[ \text{List.take}(i, r) = \text{List.takeWhile}(\lambda b.\ \neg(r_0 \le b), r) \]

theorem

List.drop(i,r)=List.dropWhile(λb. ¬(r0b),r)\text{List.drop}(i, r) = \text{List.dropWhile}(\lambda b.\ \neg(r_0 \le b), r) where ii is the insertion position of r0r_0

#orderedInsertPos_drop

For a type II equipped with a decidable binary relation \le, a list rr, and an element r0r_0, let ii be the index at which r0r_0 is placed when inserted into rr via the ordered insertion operation (defined as i=orderedInsertPos(,r,r0)i = \text{orderedInsertPos}(\le, r, r_0)). The theorem states that dropping the first ii elements of the original list rr is equivalent to dropping elements from the start of rr as long as the condition r0br_0 \le b is false. That is: \[ \text{List.drop}(i, r) = \text{List.dropWhile}(\lambda b.\ \neg(r_0 \le b), r) \]

theorem

The prefix of `orderedInsert` up to r0r_0's position is the `takeWhile` of ¬(r0b)\neg(r_0 \le b) followed by r0r_0

#orderedInsertPos_succ_take_orderedInsert

Let II be a type equipped with a decidable binary relation \le. For a list rr of elements in II and an element r0Ir_0 \in I, let ii be the index at which r0r_0 is placed when inserted into rr via the ordered insertion operation (`orderedInsert`). Then, the prefix of the resulting list (after insertion) of length i+1i+1 is equal to the prefix of the original list rr consisting of elements bb for which the condition r0br_0 \le b is false, followed by the element r0r_0.

theorem

¬(r0rn)\neg(r_0 \le r_n) for indices n<orderedInsertPos(,r,r0)n < \text{orderedInsertPos}(\le, r, r_0)

#lt_orderedInsertPos_rel

Let II be a type equipped with a decidable binary relation \le. For a list rr of elements in II and an element r0Ir_0 \in I, let ii be the index at which r0r_0 is placed when inserted into rr via the ordered insertion operation (defined as i=orderedInsertPos(,r,r0)i = \text{orderedInsertPos}(\le, r, r_0)). For any index nn of the list rr, if n<in < i, then the element at that index rnr_n satisfies ¬(r0rn)\neg(r_0 \le r_n).

theorem

¬(r0xn)\neg(r_0 \le x_n) for indices n<orderedInsertPosn < \text{orderedInsertPos} in the list after insertion

#lt_orderedInsertPos_rel_fin

Let II be a type equipped with a decidable binary relation \le. Let rr be a list of elements in II and r0Ir_0 \in I be an element. Let p=orderedInsertPos(,r,r0)p = \text{orderedInsertPos}(\le, r, r_0) be the index at which r0r_0 is inserted into rr via the ordered insertion operation. For any index nn in the list resulting from this insertion, if n<pn < p, then the element xnx_n at that index satisfies ¬(r0xn)\neg(r_0 \le x_n).

theorem

r0rnr_0 \le r_n for indices norderedInsertPos(,r,r0)n \ge \text{orderedInsertPos}(\le, r, r_0)

#gt_orderedInsertPos_rel

Let II be a type equipped with a decidable, total, and transitive binary relation \le. Let rr be a list of elements in II that is sorted with respect to \le (i.e., `List.Pairwise le1 r`). Let r0Ir_0 \in I be an element, and let pp be the position where r0r_0 is placed when inserted into rr via the ordered insertion operation (the index of the first element bb in rr such that r0br_0 \le b is true). For any index nn in the list rr, if npn \ge p, then r0rnr_0 \le r_n.

theorem

eraseIdx\text{eraseIdx} and orderedInsert\text{orderedInsert} commute for indices i<orderedInsertPosi < \text{orderedInsertPos}

#orderedInsert_eraseIdx_lt_orderedInsertPos

Let II be a type with a decidable binary relation \le. Let rr be a list of elements in II, r0r_0 be an element in II, and pp be the position at which r0r_0 is inserted into rr via the `orderedInsert` operation (denoted by `orderedInsertPos le1 r r0`). Suppose that the condition ¬(r0)\neg(r_0 \le \cdot) is prefix-closed in rr; that is, for any indices j<kj < k, if ¬(r0rk)\neg(r_0 \le r_k), then ¬(r0rj)\neg(r_0 \le r_j). Then, for any index i<pi < p, the result of erasing the element at index ii from the list after r0r_0 has been inserted is the same as inserting r0r_0 into the list after the element at index ii has been erased from rr: eraseIdx(orderedInsert(,r0,r),i)=orderedInsert(,r0,eraseIdx(r,i))\text{eraseIdx}(\text{orderedInsert}(\le, r_0, r), i) = \text{orderedInsert}(\le, r_0, \text{eraseIdx}(r, i)) where eraseIdx(l,i)\text{eraseIdx}(l, i) denotes the removal of the element at index ii from list ll.

theorem

eraseIdx\text{eraseIdx} of orderedInsert\text{orderedInsert} at index i+1i+1 equals orderedInsert\text{orderedInsert} into eraseIdx\text{eraseIdx} at index ii for iorderedInsertPosi \ge \text{orderedInsertPos}

#orderedInsert_eraseIdx_orderedInsertPos_le

Let II be a type equipped with a decidable binary relation \le. Let rr be a list of elements of type II and r0r_0 be an element of II. Let p=orderedInsertPos(,r,r0)p = \text{orderedInsertPos}(\le, r, r_0) be the index where r0r_0 is placed when inserted into rr via the `orderedInsert` operation (specifically, the length of the prefix of rr where the condition r0br_0 \le b is false). Suppose that for any indices jj and kk of the list rr, if j<kj < k and the condition r0rkr_0 \le r_k is false, then r0rjr_0 \le r_j is also false. Then for any natural number ii such that pip \le i, it holds that: eraseIdx(orderedInsert(,r0,r),i+1)=orderedInsert(,r0,eraseIdx(r,i)) \text{eraseIdx}(\text{orderedInsert}(\le, r_0, r), i + 1) = \text{orderedInsert}(\le, r_0, \text{eraseIdx}(r, i)) where orderedInsert(,r0,r)\text{orderedInsert}(\le, r_0, r) is the list rr with r0r_0 inserted at index pp, and eraseIdx(l,n)\text{eraseIdx}(l, n) is the list ll with the element at index nn removed.

definition

Equivalence of indices between r0::rr_0 :: r and orderedInsert(,r0,r)\text{orderedInsert}(\le, r_0, r)

#orderedInsertEquiv

Let II be a type with a decidable binary relation \le. Given a list rr and an element r0Ir_0 \in I, let Lins=orderedInsert(,r0,r)L_{ins} = \text{orderedInsert}(\le, r_0, r) be the list obtained by inserting r0r_0 into rr at the position p=orderedInsertPos(,r,r0)p = \text{orderedInsertPos}(\le, r, r_0). This definition provides an equivalence (bijection) between the set of indices of the prepended list r0::rr_0 :: r, denoted by Fin(r+1)\text{Fin}(|r| + 1), and the set of indices of the list LinsL_{ins}, denoted by Fin(Lins)\text{Fin}(|L_{ins}|). Under this mapping, the index 00 is sent to pp, and the indices {1,,r}\{1, \dots, |r|\} are mapped to the remaining positions in LinsL_{ins} such that the relative order of the original elements in rr is preserved.

theorem

orderedInsertEquiv(,r,r0)(0)=orderedInsertPos(,r,r0)\text{orderedInsertEquiv}(\le, r, r_0)(0) = \text{orderedInsertPos}(\le, r, r_0)

#orderedInsertEquiv_zero

Let II be a type with a decidable binary relation \le. For any list rr of elements in II and any element r0Ir_0 \in I, let ϕ=orderedInsertEquiv(,r,r0)\phi = \text{orderedInsertEquiv}(\le, r, r_0) be the equivalence (bijection) that maps the indices of the prepended list r0::rr_0 :: r to the indices of the list resulting from inserting r0r_0 into rr via the `orderedInsert` operation. Let p=orderedInsertPos(,r,r0)p = \text{orderedInsertPos}(\le, r, r_0) be the index at which r0r_0 is placed in the resulting list. Then the equivalence ϕ\phi maps the first index 00 to pp, i.e., ϕ(0)=p\phi(0) = p.

theorem

Value of orderedInsertEquiv\text{orderedInsertEquiv} for successor indices n+1n+1 via succAbovep(n)\text{succAbove}_p(n)

#orderedInsertEquiv_succ

Let II be a type with a decidable binary relation \le. For any list rr of elements in II and any element r0Ir_0 \in I, let p=orderedInsertPos(,r,r0)p = \text{orderedInsertPos}(\le, r, r_0) be the position at which r0r_0 is inserted into rr by the orderedInsert\text{orderedInsert} operation. Let ϕ\phi be the index equivalence orderedInsertEquiv(,r,r0)\text{orderedInsertEquiv}(\le, r, r_0) which maps indices of the prepended list r0::rr_0 :: r to indices of the list resulting from the ordered insertion. Then for any natural number nn such that n+1n+1 is a valid index of r0::rr_0 :: r, the equivalence satisfies: ϕ(n+1)=succAbovep(n)\phi(n+1) = \text{succAbove}_p(n) where succAbovep(n)\text{succAbove}_p(n) is defined as nn if n<pn < p and n+1n+1 if npn \ge p. This formula describes how the original indices of rr (shifted by 1 in r0::rr_0 :: r) are mapped to their new positions in the list after r0r_0 is inserted at position pp.

theorem

orderedInsertEquiv(,r,r0)(n+1)=succAbovep(n)\text{orderedInsertEquiv}(\le, r, r_0)(n+1) = \text{succAbove}_p(n)

#orderedInsertEquiv_fin_succ

Let II be a type with a decidable binary relation \le. For any list rr of elements in II and any element r0Ir_0 \in I, let p=orderedInsertPos(,r,r0)p = \text{orderedInsertPos}(\le, r, r_0) be the position at which r0r_0 is inserted into rr via the ordered insertion operation. Let ϕ\phi denote the index equivalence orderedInsertEquiv(,r,r0)\text{orderedInsertEquiv}(\le, r, r_0) which maps the set of indices of the prepended list r0::rr_0 :: r to the set of indices of the list resulting from the insertion. For any index nFin(r)n \in \text{Fin}(|r|), let n+1n+1 be its successor in Fin(r+1)\text{Fin}(|r|+1). Then it holds that: ϕ(n+1)=succAbovep(n)\phi(n+1) = \text{succAbove}_p(n) where succAbovep(n)\text{succAbove}_p(n) is the function that maps nn to nn if n<pn < p and to n+1n+1 if npn \ge p.

theorem

ϕ(n+1)<ϕ(m+1)    n<m\phi(n+1) < \phi(m+1) \implies n < m for ϕ=orderedInsertEquiv\phi = \text{orderedInsertEquiv}

#orderedInsertEquiv_monotone_fin_succ

Let II be a type with a decidable binary relation \le. For any list rr of elements in II and any element r0Ir_0 \in I, let ϕ\phi denote the index equivalence orderedInsertEquiv(,r,r0)\text{orderedInsertEquiv}(\le, r, r_0) which maps the set of indices of the prepended list r0::rr_0 :: r to the set of indices of the list resulting from the ordered insertion of r0r_0 into rr. For any two indices n,mFin(r)n, m \in \text{Fin}(|r|), if ϕ(n+1)<ϕ(m+1)\phi(n+1) < \phi(m+1), then it holds that n<mn < m.

theorem

l=ll = l' implies congruence of orderedInsertEquiv\text{orderedInsertEquiv}

#orderedInsertEquiv_congr

Let α\alpha be a type with a decidable binary relation \le. For any element aαa \in \alpha and any two lists l,ll, l' of elements in α\alpha, if l=ll = l', then the equivalence of indices orderedInsertEquiv(,l,a)\text{orderedInsertEquiv}(\le, l, a) (which maps indices of a::la :: l to indices of orderedInsert(,a,l)\text{orderedInsert}(\le, a, l)) is equal to the composition: orderedInsertEquiv(,l,a)=cast2orderedInsertEquiv(,l,a)cast1\text{orderedInsertEquiv}(\le, l, a) = \text{cast}_2 \circ \text{orderedInsertEquiv}(\le, l', a) \circ \text{cast}_1 where cast1\text{cast}_1 and cast2\text{cast}_2 are the canonical isomorphisms between the respective index sets Fin(l+1)Fin(l+1)\text{Fin}(|l|+1) \cong \text{Fin}(|l'|+1) and Fin(orderedInsert(,a,l))Fin(orderedInsert(,a,l))\text{Fin}(|\text{orderedInsert}(\le, a, l')|) \cong \text{Fin}(|\text{orderedInsert}(\le, a, l)|) induced by the equality of the lists.

theorem

(r0::r).get=(orderedInsert r0r).getorderedInsertEquiv(r_0 :: r).\text{get} = (\text{orderedInsert } \le r_0 r).\text{get} \circ \text{orderedInsertEquiv}

#get_eq_orderedInsertEquiv

Let II be a type with a decidable binary relation \le. For any list rr of elements in II and any element r0Ir_0 \in I, let Lins=orderedInsert(,r0,r)L_{ins} = \text{orderedInsert}(\le, r_0, r) be the list resulting from the ordered insertion of r0r_0 into rr. Let ϕ:Fin(r+1)Fin(Lins)\phi: \text{Fin}(|r| + 1) \cong \text{Fin}(|L_{ins}|) be the index equivalence orderedInsertEquiv(,r,r0)\text{orderedInsertEquiv}(\le, r, r_0), which maps the indices of the prepended list r0::rr_0 :: r to the corresponding indices in LinsL_{ins}. Then, the element at index ii in the list r0::rr_0 :: r is equal to the element at index ϕ(i)\phi(i) in LinsL_{ins}. That is, the element retrieval functions satisfy: (r0::r).get=Lins.getϕ(r_0 :: r).\text{get} = L_{ins}.\text{get} \circ \phi

theorem

(r0::r).getϕ1=orderedInsert(,r0,r).get(r_0 :: r).\text{get} \circ \phi^{-1} = \text{orderedInsert}(\le, r_0, r).\text{get}

#orderedInsertEquiv_get

Let II be a type with a decidable binary relation \le. For any list rr of elements in II and any element r0Ir_0 \in I, let Lins=orderedInsert(,r0,r)L_{ins} = \text{orderedInsert}(\le, r_0, r) be the list resulting from the ordered insertion of r0r_0 into rr. Let ϕ:Fin(r+1)Fin(Lins)\phi: \text{Fin}(|r| + 1) \cong \text{Fin}(|L_{ins}|) be the index equivalence orderedInsertEquiv(,r,r0)\text{orderedInsertEquiv}(\le, r, r_0), which maps the indices of the prepended list r0::rr_0 :: r to the corresponding indices in LinsL_{ins}, and let ϕ1\phi^{-1} be its inverse. Then, the element retrieval functions (the `get` operations) satisfy the identity: (r0::r).getϕ1=Lins.get(r_0 :: r).\text{get} \circ \phi^{-1} = L_{ins}.\text{get}

theorem

eraseIdx(ϕ(0),orderedInsert(,r0,r))=r\text{eraseIdx}(\phi(0), \text{orderedInsert}(\le, r_0, r)) = r

#orderedInsert_eraseIdx_orderedInsertEquiv_zero

Let II be a type with a decidable binary relation \le. For any list rr of elements in II and any element r0Ir_0 \in I, let Lins=orderedInsert(,r0,r)L_{ins} = \text{orderedInsert}(\le, r_0, r) be the list resulting from the ordered insertion of r0r_0 into rr. Let ϕ:Fin(r+1)Fin(Lins)\phi: \text{Fin}(|r| + 1) \cong \text{Fin}(|L_{ins}|) be the index equivalence orderedInsertEquiv(,r,r0)\text{orderedInsertEquiv}(\le, r, r_0), which maps indices of the prepended list r0::rr_0 :: r to the corresponding indices in LinsL_{ins}. Then, removing the element at the index ϕ(0)\phi(0) from LinsL_{ins} using the `eraseIdx` function results in the original list rr: (Lins).eraseIdx(ϕ(0))=r(L_{ins}).\text{eraseIdx}(\phi(0)) = r

theorem

eraseIdx(orderedInsert(,r0,r),ϕ(n+1))=orderedInsert(,r0,eraseIdx(r,n))\text{eraseIdx}(\text{orderedInsert}(\le, r_0, r), \phi(n+1)) = \text{orderedInsert}(\le, r_0, \text{eraseIdx}(r, n))

#orderedInsert_eraseIdx_orderedInsertEquiv_succ

Let II be a type with a decidable binary relation \le. Let rr be a list of elements in II and r0r_0 be an element in II. Let ϕ\phi be the index equivalence orderedInsertEquiv(,r,r0)\text{orderedInsertEquiv}(\le, r, r_0), which maps the indices of the prepended list r0::rr_0 :: r to the indices of the list resulting from inserting r0r_0 into rr (denoted by orderedInsert(,r0,r)\text{orderedInsert}(\le, r_0, r)). Suppose that for any indices i<ji < j of the list rr, the condition ¬(r0)\neg(r_0 \le \cdot) is prefix-closed, meaning ¬(r0rj)    ¬(r0ri)\neg(r_0 \le r_j) \implies \neg(r_0 \le r_i). Then, for any natural number nn such that n+1n+1 is a valid index of r0::rr_0 :: r, it holds that: eraseIdx(orderedInsert(,r0,r),ϕ(n+1))=orderedInsert(,r0,eraseIdx(r,n))\text{eraseIdx}(\text{orderedInsert}(\le, r_0, r), \phi(n+1)) = \text{orderedInsert}(\le, r_0, \text{eraseIdx}(r, n)) where eraseIdx(l,k)\text{eraseIdx}(l, k) denotes the removal of the element at index kk from list ll.

theorem

eraseIdx(orderedInsert(,r0,r),ϕ(n+1))=orderedInsert(,r0,eraseIdx(r,n))\text{eraseIdx}(\text{orderedInsert}(\le, r_0, r), \phi(n+1)) = \text{orderedInsert}(\le, r_0, \text{eraseIdx}(r, n))

#orderedInsert_eraseIdx_orderedInsertEquiv_fin_succ

Let II be a type with a decidable binary relation \le. Let rr be a list of elements in II and r0r_0 be an element in II. Let ϕ\phi be the index equivalence orderedInsertEquiv(,r,r0)\text{orderedInsertEquiv}(\le, r, r_0), which maps the indices of the prepended list r0::rr_0 :: r to the indices of the list resulting from inserting r0r_0 into rr (denoted by orderedInsert(,r0,r)\text{orderedInsert}(\le, r_0, r)). Suppose that for any indices i<ji < j of the list rr, the condition ¬(r0)\neg(r_0 \le \cdot) is prefix-closed, meaning ¬(r0rj)    ¬(r0ri)\neg(r_0 \le r_j) \implies \neg(r_0 \le r_i). Then, for any index nFin(r)n \in \text{Fin}(|r|), it holds that: eraseIdx(orderedInsert(,r0,r),ϕ(n+1))=orderedInsert(,r0,eraseIdx(r,n))\text{eraseIdx}(\text{orderedInsert}(\le, r_0, r), \phi(n+1)) = \text{orderedInsert}(\le, r_0, \text{eraseIdx}(r, n)) where n+1n+1 is the successor index in Fin(r+1)\text{Fin}(|r|+1) and eraseIdx(l,k)\text{eraseIdx}(l, k) denotes the removal of the element at index kk from list ll.

theorem

`orderedInsertEquiv` for a list of sigma types is equivalent to the `orderedInsertEquiv` of its projection

#orderedInsertEquiv_sigma

Let II be a type with a decidable binary relation \le, and f:ITypef : I \to \text{Type} be a type family. Let ll be a list of dependent pairs j,x\langle j, x \rangle where jIj \in I and xf(j)x \in f(j), and let pr1\text{pr}_1 denote the projection map j,xj\langle j, x \rangle \mapsto j. For any iIi \in I and af(i)a \in f(i), let Σ\le_\Sigma be the relation on the sigma type defined by the first components, i.e., j1,x1Σj2,x2\langle j_1, x_1 \rangle \le_\Sigma \langle j_2, x_2 \rangle if and only if j1j2j_1 \le j_2. The index equivalence ϕΣ=orderedInsertEquiv(Σ,l,i,a)\phi_\Sigma = \text{orderedInsertEquiv}(\le_\Sigma, l, \langle i, a \rangle), which maps indices of the prepended list i,a::l\langle i, a \rangle :: l to the indices of the list resulting from the ordered insertion of i,a\langle i, a \rangle into ll, is equal to the index equivalence ϕ=orderedInsertEquiv(,map(pr1,l),i)\phi = \text{orderedInsertEquiv}(\le, \text{map}(\text{pr}_1, l), i) of the projected list. This equality holds identifying the respective index sets through their canonical isomorphisms.

theorem

orderedInsert\text{orderedInsert} equals insertIdx\text{insertIdx} at the position orderedInsertPos\text{orderedInsertPos}

#orderedInsert_eq_insertIdx_orderedInsertPos

Let II be a type with a decidable binary relation \le. For any list rr of elements in II and any element r0Ir_0 \in I, the list resulting from the ordered insertion of r0r_0 into rr is identical to the list obtained by inserting r0r_0 at the specific index p=orderedInsertPos(,r,r0)p = \text{orderedInsertPos}(\le, r, r_0) using the standard index-based insertion operation. Mathematically, this is expressed as: orderedInsert(,r0,r)=insertIdx(r,p,r0)\text{orderedInsert}(\le, r_0, r) = \text{insertIdx}(r, p, r_0) where pp is the index where r0r_0 is placed according to the ordering rule.

definition

Equivalence of indices between ll and insertionSort(r,l)\text{insertionSort}(r, l)

#insertionSortEquiv

Let α\alpha be a type equipped with a decidable binary relation rr. For any list ll of elements in α\alpha, the function constructs a bijection (equivalence) between the set of indices of the original list ll, denoted by Fin(l)\text{Fin}(|l|), and the set of indices of the sorted list insertionSort(r,l)\text{insertionSort}(r, l). The mapping is defined recursively: 1. For the empty list l=[]l = [], the bijection is the identity map. 2. For a list a::la :: l, the bijection is formed by first applying the recursive equivalence for ll, extended to include the new head element aa, and then composing it with the bijection `orderedInsertEquiv`, which maps the indices of the prepended list a::insertionSort(r,l)a :: \text{insertionSort}(r, l) to the indices of the list after aa has been inserted into its correct sorted position.

theorem

l.getϕ1=insertionSort(r,l).getl.\text{get} \circ \phi^{-1} = \text{insertionSort}(r, l).\text{get}

#insertionSortEquiv_get

Let α\alpha be a type equipped with a decidable binary relation rr. For any list ll of elements in α\alpha, let lsorted=insertionSort(r,l)l_{\text{sorted}} = \text{insertionSort}(r, l) be the list resulting from the insertion sort of ll according to rr. Let ϕ:Fin(l)Fin(lsorted)\phi: \text{Fin}(|l|) \cong \text{Fin}(|l_{\text{sorted}}|) be the index equivalence insertionSortEquiv(r,l)\text{insertionSortEquiv}(r, l), which maps the indices of the original list to the corresponding indices in the sorted list, and let ϕ1\phi^{-1} be its inverse. Then, the element retrieval functions (the `get` operations) satisfy the identity: l.getϕ1=lsorted.getl.\text{get} \circ \phi^{-1} = l_{\text{sorted}}.\text{get}

theorem

Congruence of insertionSortEquiv\text{insertionSortEquiv} for l=ll = l'

#insertionSortEquiv_congr

Let α\alpha be a type and rr be a decidable binary relation on α\alpha. For any two lists l,lList(α)l, l' \in \text{List}(\alpha) such that l=ll = l', the bijection insertionSortEquiv(r,l)\text{insertionSortEquiv}(r, l) between the indices of ll and the indices of its sorted version insertionSort(r,l)\text{insertionSort}(r, l) is equal to the composition of the following equivalences: 1. The natural cast between the indices of ll and ll', 2. The bijection insertionSortEquiv(r,l)\text{insertionSortEquiv}(r, l'), 3. The natural cast between the indices of insertionSort(r,l)\text{insertionSort}(r, l') and insertionSort(r,l)\text{insertionSort}(r, l).

theorem

insertionSortEquiv(r,l)(i)=insertionSortEquiv(r,l)(i)\text{insertionSortEquiv}(r, l)(i) = \text{insertionSortEquiv}(r, l')(i) when l=ll = l'

#insertionSortEquiv_congr_apply

Let α\alpha be a type and rr be a decidable binary relation on α\alpha. For any two lists l,lList(α)l, l' \in \text{List}(\alpha) such that l=ll = l', and for any index iFin(l)i \in \text{Fin}(|l|), applying the index equivalence insertionSortEquiv(r,l)\text{insertionSortEquiv}(r, l) to ii yields the same result as applying insertionSortEquiv(r,l)\text{insertionSortEquiv}(r, l') to the index ii (cast as an index of ll'), with the result cast back to the index set of the sorted list insertionSort(r,l)\text{insertionSort}(r, l).

theorem

lsorted.getϕ=l.getl_{\text{sorted}}.\text{get} \circ \phi = l.\text{get}

#insertionSort_get_comp_insertionSortEquiv

Let α\alpha be a type equipped with a decidable binary relation rr. For any list ll of elements in α\alpha, let lsorted=insertionSort(r,l)l_{\text{sorted}} = \text{insertionSort}(r, l) be the list resulting from the insertion sort of ll according to rr. Let ϕ:Fin(l)Fin(lsorted)\phi: \text{Fin}(|l|) \cong \text{Fin}(|l_{\text{sorted}}|) be the index equivalence insertionSortEquiv(r,l)\text{insertionSortEquiv}(r, l), which maps each index of the original list to its corresponding position in the sorted list. Then, the composition of the element retrieval function (the `get` operation) of the sorted list with the index mapping ϕ\phi is equal to the retrieval function of the original list: lsorted.getϕ=l.getl_{\text{sorted}}.\text{get} \circ \phi = l.\text{get}

theorem

insertionSort(r,l)=List.ofFn(l.getϕ1)\text{insertionSort}(r, l) = \text{List.ofFn}(l.\text{get} \circ \phi^{-1})

#insertionSort_eq_ofFn

Let α\alpha be a type equipped with a decidable binary relation rr. For any list ll of elements in α\alpha, let ϕ:Fin(l)Fin(insertionSort(r,l))\phi : \text{Fin}(|l|) \cong \text{Fin}(|\text{insertionSort}(r, l)|) be the index equivalence insertionSortEquiv(r,l)\text{insertionSortEquiv}(r, l) that maps indices from the original list to their positions in the sorted list. Then, the sorted list is equal to the list constructed by applying the original list's retrieval function to the indices mapped by the inverse equivalence ϕ1\phi^{-1}. That is, insertionSort(r,l)=List.ofFn(l.getϕ1)\text{insertionSort}(r, l) = \text{List.ofFn}(l.\text{get} \circ \phi^{-1}) where List.ofFn\text{List.ofFn} creates a list from a function defined on the indices.

theorem

i<j and ϕ(j)<ϕ(i)    ¬r(l[i],l[j])i < j \text{ and } \phi(j) < \phi(i) \implies \neg r(l[i], l[j]) for insertion sort index equivalence ϕ\phi

#insertionSortEquiv_order

Let α\alpha be a type and rr be a decidable binary relation on α\alpha. For any list ll of elements in α\alpha, let ϕ:Fin(l)Fin(l)\phi: \text{Fin}(|l|) \to \text{Fin}(|l|) be the index equivalence insertionSortEquiv(r,l)\text{insertionSortEquiv}(r, l), which maps each index kk of the original list to its corresponding index in the sorted list insertionSort(r,l)\text{insertionSort}(r, l). For any two indices i,ji, j in the original list such that i<ji < j, if the element originally at jj is placed before the element originally at ii in the sorted list (i.e., ϕ(j)<ϕ(i)\phi(j) < \phi(i)), then the relation rr does not hold for the pair (l[i],l[j])(l[i], l[j]), denoted ¬r(l[i],l[j])\neg r(l[i], l[j]).

definition

Optional erasure of a list element by index

#optionErase

Given a list ll of elements of type II and an optional index iOption({0,,length(l)1})i \in \text{Option}(\{0, \dots, \text{length}(l) - 1\}), this function returns the original list ll if ii is `none`. If ii is `some } k,itreturnsthelist, it returns the list lwiththeelementatindex with the element at index k$ removed.

theorem

Length of list after erasing an index equals length(l)1\text{length}(l) - 1

#eraseIdx_length'

For any list ll of elements of type II and any valid index ii (where 0i<length(l)0 \le i < \text{length}(l)), the length of the list obtained by erasing the element at index ii is equal to the length of ll minus 11.

theorem

length(eraseIdx(l,i))+1=length(l)\text{length}(\text{eraseIdx}(l, i)) + 1 = \text{length}(l)

#eraseIdx_length

Let ll be a list of elements of type II and ii be a valid index for ll (i.e., 0i<length(l)0 \le i < \text{length}(l)). Then the length of the list obtained by removing the element at index ii, denoted by eraseIdx(l,i)\text{eraseIdx}(l, i), satisfies the property that length(eraseIdx(l,i))+1=length(l)\text{length}(\text{eraseIdx}(l, i)) + 1 = \text{length}(l).

theorem

(length(eraseIdx(l,i)))+1=length(l)(\text{length}(\text{eraseIdx}(l, i))) + 1 = \text{length}(l)

#eraseIdx_length_succ

For any list ll of elements of type II and any valid index ii (where i<length(l)i < \text{length}(l)), the successor of the length of the list obtained by removing the element at index ii is equal to the original length of ll.

theorem

The length of a::la :: l after erasing an element equals the length of ll

#eraseIdx_cons_length

Let II be a type, aIa \in I be an element, and ll be a list of elements of II. For any valid index ii in the list a::la :: l, the length of the list obtained by erasing the element at index ii from a::la :: l is equal to the length of ll.

theorem

Indexing of eraseIdx(l,i)\text{eraseIdx}(l, i) equals l.getsuccAboveil.\text{get} \circ \text{succAbove}_i

#eraseIdx_get

Let ll be a list of elements of type II and ii be a valid index in ll. Let eraseIdx(l,i)\text{eraseIdx}(l, i) be the list formed by removing the element of ll at index ii. The retrieval function for the resulting list, (eraseIdx(l,i)).get(\text{eraseIdx}(l, i)).\text{get}, is equal to the composition of the original retrieval function l.getl.\text{get} and the function succAbovei\text{succAbove}_i. That is, for any valid index jj in the shortened list, the element is retrieved from index jj of ll if j<ij < i, and from index j+1j+1 of ll if jij \ge i.

theorem

eraseIdx(insertionSort(r),ϕ(n))=insertionSort(eraseIdx(r,n))\text{eraseIdx}(\text{insertionSort}(r), \phi(n)) = \text{insertionSort}(\text{eraseIdx}(r, n))

#eraseIdx_insertionSort

Let II be a type equipped with a decidable binary relation \le that is both total and transitive. For any list rr of elements in II and any natural number nn such that n<rn < |r|, let ϕ\phi denote the index equivalence insertionSortEquiv(,r)\text{insertionSortEquiv}(\le, r), which maps the indices of the original list rr to the indices of the list sorted by insertion sort, insertionSort(,r)\text{insertionSort}(\le, r). Then, erasing the element at index ϕ(n)\phi(n) from the sorted list is equivalent to sorting the list obtained by erasing the element at index nn from the original list: eraseIdx(insertionSort(,r),ϕ(n))=insertionSort(,eraseIdx(r,n))\text{eraseIdx}(\text{insertionSort}(\le, r), \phi(n)) = \text{insertionSort}(\le, \text{eraseIdx}(r, n))

theorem

eraseIdx(insertionSort(r),ϕ(n))=insertionSort(eraseIdx(r,n))\text{eraseIdx}(\text{insertionSort}(r), \phi(n)) = \text{insertionSort}(\text{eraseIdx}(r, n)) for nFin(r)n \in \text{Fin}(|r|)

#eraseIdx_insertionSort_fin

Let II be a type equipped with a decidable binary relation \le that is both total and transitive. For any list rr of elements in II and any valid index nFin(r)n \in \text{Fin}(|r|), let ϕ\phi denote the index equivalence `insertionSortEquiv` which maps the indices of the original list rr to the indices of the list sorted by insertion sort. Then, erasing the element at index ϕ(n)\phi(n) from the sorted list is equivalent to sorting the list obtained by erasing the element at index nn from the original list: eraseIdx(insertionSort(,r),ϕ(n))=insertionSort(,eraseIdx(r,n))\text{eraseIdx}(\text{insertionSort}(\le, r), \phi(n)) = \text{insertionSort}(\le, \text{eraseIdx}(r, n))

definition

Left-most minimal index of i::li :: l with respect to rr

#insertionSortMinPos

Given a type α\alpha with a decidable binary relation rr, an element iαi \in \alpha, and a list ll of elements in α\alpha, this function returns the index aa in the list i::li :: l that corresponds to the first element (the head) of the list after it has been sorted using insertion sort. Specifically, it uses the inverse of the index equivalence insertionSortEquiv(r,i::l)\text{insertionSortEquiv}(r, i :: l) to map the index 00 of the sorted list back to its original position in i::li :: l. This index represents the leftmost minimal position in i::li :: l with respect to rr, such that for every element (i::l)b(i :: l)_b appearing before index aa, the relation r((i::l)b,(i::l)a)r((i :: l)_b, (i :: l)_a) is false.

definition

Minimum element of i::li :: l via insertion sort with respect to rr

#insertionSortMin

Given a type α\alpha with a decidable binary relation rr, an element iαi \in \alpha, and a list ll of elements in α\alpha, this function returns the element of the list i::li :: l located at the index aa defined by insertionSortMinPos(r,i,l)\text{insertionSortMinPos}(r, i, l). This element is the one that would appear at the head (the first position) of the list i::li :: l after it has been sorted using the insertion sort algorithm with respect to the relation rr.

theorem

insertionSortMin(r,i,l)=(insertionSort(r,i::l)).head\text{insertionSortMin}(r, i, l) = (\text{insertionSort}(r, i :: l)).\text{head}

#insertionSortMin_eq_insertionSort_head

Let α\alpha be a type with a decidable binary relation rr. For any element iαi \in \alpha and list ll of elements in α\alpha, the value insertionSortMin(r,i,l)\text{insertionSortMin}(r, i, l) is equal to the first element (the head) of the list obtained by sorting i::li :: l using the insertion sort algorithm with respect to rr.

definition

List i::li :: l with the leftmost minimal element removed

#insertionSortDropMinPos

Let rr be a decidable binary relation on a type α\alpha. Given an element iαi \in \alpha and a list ll of elements in α\alpha, this function returns the list obtained by removing the element at the position a=insertionSortMinPos(r,i,l)a = \text{insertionSortMinPos}(r, i, l) from the list i::li :: l. Here, aa is the index of the leftmost minimal element of i::li :: l with respect to the relation rr.

theorem

insertionSort(r,i::l)=insertionSortMin(r,i,l)::insertionSort(r,insertionSortDropMinPos(r,i,l))\text{insertionSort}(r, i :: l) = \text{insertionSortMin}(r, i, l) :: \text{insertionSort}(r, \text{insertionSortDropMinPos}(r, i, l))

#insertionSort_eq_insertionSortMin_cons

Let α\alpha be a type equipped with a decidable binary relation rr that is both total and transitive. For any element iαi \in \alpha and list ll of elements in α\alpha, sorting the list i::li :: l using insertion sort with respect to rr is equivalent to prepending the minimum element of i::li :: l to the sorted version of the list obtained by removing that same minimum element from i::li :: l: insertionSort(r,i::l)=insertionSortMin(r,i,l)::insertionSort(r,insertionSortDropMinPos(r,i,l))\text{insertionSort}(r, i :: l) = \text{insertionSortMin}(r, i, l) :: \text{insertionSort}(r, \text{insertionSortDropMinPos}(r, i, l)) where insertionSortMin(r,i,l)\text{insertionSortMin}(r, i, l) is the element that appears at the head of the sorted list, and insertionSortDropMinPos(r,i,l)\text{insertionSortDropMinPos}(r, i, l) is the list i::li :: l with that specific element removed.

definition

Insert aa or erase the ii-th element of a list

#optionEraseZ

Let ll be a list of elements of type II, aa be an element of type II, and ii be an optional index into ll (represented as an element of the type Option(Fin length(l))\text{Option}(\text{Fin } \text{length}(l))). The function returns a new list of type II according to the following cases: - If i=nonei = \text{none}, the result is the list a::la :: l, which is ll with aa prepended to the front. - If i=some ji = \text{some } j for some index jj, the result is the list ll with the element at index jj removed.

theorem

length(optionEraseZ(l,a,some i))=length(l)1\text{length}(\text{optionEraseZ}(l, a, \text{some } i)) = \text{length}(l) - 1

#optionEraseZ_some_length

Let ll be a list of elements of type II and aa be an element of type II. For any index ii of the list ll (represented as an element of type Fin(length(l))\text{Fin}(\text{length}(l))), the length of the list resulting from optionEraseZ(l,a,some i)\text{optionEraseZ}(l, a, \text{some } i)—which is the list ll with the element at index ii removed—is equal to length(l)1\text{length}(l) - 1.

theorem

Extensionality of `optionEraseZ`

#optionEraseZ_ext

Let ll and ll' be lists of elements of type II, and let aa and aa' be elements of type II. Let ii and ii' be optional indices into ll and ll' respectively (where an index is either `none` or a specific position within the list). If the lists are equal (l=ll = l'), the elements are equal (a=aa = a'), and the indices are equal (i=ii = i'), then the result of the `optionEraseZ` operation is the same for both: optionEraseZ(l,a,i)=optionEraseZ(l,a,i)\text{optionEraseZ}(l, a, i) = \text{optionEraseZ}(l', a', i') where the `optionEraseZ` operation returns a::la :: l if the index is `none`, and returns ll with the element at the specified index removed if the index is a specific position.

theorem

atake m (finRange n)    a<ma \in \text{take } m \ (\text{finRange } n) \iff a < m

#mem_take_finrange

For any natural numbers nn and mm, and for any element a{0,1,,n1}a \in \{0, 1, \dots, n-1\} (represented by the type `Fin n`), aa is a member of the list containing the first mm elements of the finite range [0,1,,n1][0, 1, \dots, n-1] if and only if the value of aa is strictly less than mm (a<ma < m).