PhyslibSearch

Physlib.Mathematics.List.InsertIdx

14 declarations

theorem

Mapping a function over a list commutes with element insertion: (r.insertIdx(i,r0)).map(f)=(r.map(f)).insertIdx(i,f(r0))(r.\text{insertIdx}(i, r_0)).\text{map}(f) = (r.\text{map}(f)).\text{insertIdx}(i, f(r_0))

#insertIdx_map

Let II and JJ be types and f:IJf: I \to J be a function. For any index iNi \in \mathbb{N}, any list rr of elements in II, and any element r0Ir_0 \in I, the list obtained by inserting r0r_0 into rr at index ii and then applying ff to each element of the resulting list is equal to the list obtained by applying ff to each element of rr and then inserting f(r0)f(r_0) at index ii. That is, (insertIdx(r,i,r0)).map(f)=insertIdx(r.map(f),i,f(r0)) (\text{insertIdx}(r, i, r_0)).\text{map}(f) = \text{insertIdx}(r.\text{map}(f), i, f(r_0))

theorem

Erasing an element preserves the pairwise property of a list

#eraseIdx_sorted

Let II be a type and \le be a binary relation on II. For any list rr of elements in II and any index nNn \in \mathbb{N}, if rr satisfies the pairwise relation \le (meaning \le holds for all pairs of elements in the list in their original order), then the list obtained by erasing the element at index nn also satisfies the pairwise relation \le.

theorem

il.eraseIdx n    ilil[n]i \in l.\text{eraseIdx } n \iff i \in l \land i \neq l[n] for lists with no duplicates

#mem_eraseIdx_nodup

Let ll be a list of elements of type II and nn be a natural number representing an index such that n<length(l)n < \text{length}(l). If ll contains no duplicate elements (Nodup l\text{Nodup } l), then for any element iIi \in I, ii is a member of the list obtained by erasing the element at index nn (denoted l.eraseIdx nl.\text{eraseIdx } n) if and only if ii is a member of the original list ll and ii is not equal to the element of ll at index nn (denoted l[n]l[n]).

theorem

insertIdx(r,n,i)=take(n,r)++[i]++drop(n,r)\text{insertIdx}(r, n, i) = \text{take}(n, r) \mathbin{+\mkern-10mu+} [i] \mathbin{+\mkern-10mu+} \text{drop}(n, r)

#insertIdx_eq_take_drop

Let rr be a list of elements of type II and ii be an element of type II. For any index nn such that 0nlength(r)0 \le n \le \text{length}(r), the list obtained by inserting ii into rr at position nn is equal to the concatenation of the first nn elements of rr, the element ii, and the remaining elements of rr starting from index nn. Mathematically, this is expressed as: insertIdx(r,n,i)=take(n,r)++(i::drop(n,r))\text{insertIdx}(r, n, i) = \text{take}(n, r) \mathbin{+\mkern-10mu+} (i :: \text{drop}(n, r)) where take(n,r)\text{take}(n, r) is the prefix of rr of length nn, and drop(n,r)\text{drop}(n, r) is the suffix of rr starting after the first nn elements.

theorem

length(insertIdx(r,n,i))=length(r)+1\text{length}(\text{insertIdx}(r, n, i)) = \text{length}(r) + 1

#insertIdx_length_fin

Let rr be a list of elements of type II and iIi \in I be an element. For any index nn in the range 0nlength(r)0 \le n \le \text{length}(r), the length of the list obtained by inserting ii into rr at position nn is equal to length(r)+1\text{length}(r) + 1.

theorem

(r.insertIdx(k,i))[k.succAbove(m)]=r[m](r.\text{insertIdx}(k, i))[k.\text{succAbove}(m)] = r[m]

#insertIdx_getElem_fin

Let rr be a list of elements of type II, iIi \in I be an element, k{0,,r}k \in \{0, \dots, |r|\} be an insertion index, and m{0,,r1}m \in \{0, \dots, |r|-1\} be an index. Let rr' be the list obtained by inserting ii into rr at index kk (i.e., `List.insertIdx r k i`). Then the element at position mm in the original list rr is equal to the element at the shifted index jj in rr', where j=k.succAbove(m)j = k.\text{succAbove}(m). The mapping k.succAbove(m)k.\text{succAbove}(m) is defined such that j=mj = m if m<km < k and j=m+1j = m + 1 if mkm \geq k.

theorem

Inserting r[k]r[k] at index kk after erasing it restores rr

#insertIdx_eraseIdx_fin

For any list rr and any valid index kk such that 0k<length(r)0 \le k < \text{length}(r), inserting the original element r[k]r[k] at index kk into the list formed by erasing the element at index kk from rr results in the original list rr.

theorem

insertIdx(ϕs+ ⁣+ϕs,ϕs,ϕ)=ϕs+ ⁣+(ϕ::ϕs)\text{insertIdx} (\phi_s \mathbin{+\!+} \phi_s', |\phi_s|, \phi) = \phi_s \mathbin{+\!+} (\phi :: \phi_s')

#insertIdx_length_fst_append

For any type II, an element ϕI\phi \in I, and two lists ϕs,ϕs\phi_s, \phi_s' of elements of II, the result of inserting ϕ\phi into the concatenated list ϕs+ ⁣+ϕs\phi_s \mathbin{+\!+} \phi_s' at the index equal to the length of ϕs\phi_s is equal to ϕs+ ⁣+(ϕ::ϕs)\phi_s \mathbin{+\!+} (\phi :: \phi_s').

theorem

r.get=(List.insertIdx r k i).getk.succAbover.\text{get} = (\text{List.insertIdx } r \ k \ i).\text{get} \circ k.\text{succAbove}

#get_eq_insertIdx_succAbove

Let rr be a list of elements of type II and iIi \in I be an element. For any insertion index k{0,,r}k \in \{0, \dots, |r|\}, the function that retrieves the mm-th element of rr (for 0m<r0 \le m < |r|) is equal to the composition of the index mapping k.succAbovek.\text{succAbove} and the retrieval function of the list obtained by inserting ii into rr at index kk. In other words, for any m{0,,r1}m \in \{0, \dots, |r|-1\}: r[m]=(List.insertIdx r k i)[k.succAbove(m)]r[m] = (\text{List.insertIdx } r \ k \ i)[k.\text{succAbove}(m)] where the mapping k.succAbove(m)k.\text{succAbove}(m) is defined as mm if m<km < k and m+1m + 1 if mkm \ge k.

theorem

List.take n(List.insertIdx rni)=List.take nr\text{List.take } n (\text{List.insertIdx } r \, n \, i) = \text{List.take } n \, r

#take_insert_same

For any list rr of elements of type II, an element iIi \in I, and a natural number nn, the first nn elements of the list obtained by inserting ii into rr at index nn is equal to the first nn elements of the original list rr: List.take n(List.insertIdx rni)=List.take nr\text{List.take } n (\text{List.insertIdx } r \, n \, i) = \text{List.take } n \, r

theorem

take n(eraseIdx r n)=take n r\text{take } n (\text{eraseIdx } r \ n) = \text{take } n \ r

#take_eraseIdx_same

For any list rr of elements of type II and any natural number nn, taking the first nn elements of the list obtained by erasing the element at index nn from rr is equal to taking the first nn elements of the original list rr. In symbols, take n(eraseIdx r n)=take n r\text{take } n (\text{eraseIdx } r \ n) = \text{take } n \ r.

theorem

r[n]::drop(n,eraseIdx(r,n))=drop(n,r)r[n] :: \text{drop}(n, \text{eraseIdx}(r, n)) = \text{drop}(n, r)

#drop_eraseIdx_succ

For any list rr of elements of type II and any natural number nn such that n<length(r)n < \text{length}(r), it holds that: r[n]::drop(n,eraseIdx(r,n))=drop(n,r)r[n] :: \text{drop}(n, \text{eraseIdx}(r, n)) = \text{drop}(n, r) where r[n]r[n] denotes the nn-th element of the list, eraseIdx(r,n)\text{eraseIdx}(r, n) is the list rr with the element at index nn removed, drop(n,r)\text{drop}(n, r) is the sublist of rr starting from index nn, and :::: denotes the operation of prepending an element to a list.

theorem

Taking nn elements is invariant under insertion at index m>nm > n

#take_insert_gt

For any type II, an element iIi \in I, natural numbers nn and mm such that n<mn < m, and a list rr of elements from II, the first nn elements of the list obtained by inserting ii into rr at index mm are the same as the first nn elements of the original list rr.

theorem

Taking n+1n + 1 elements after insertion at index mnm \leq n is a permutation of ii prepended to taking nn elements

#take_insert_let

Let II be a type, ii an element of II, and rr a list of elements of II. For any natural numbers nn and mm such that mnm \leq n and mm is less than or equal to the length of rr, the first n+1n + 1 elements of the list obtained by inserting ii into rr at index mm is a permutation of the list formed by prepending ii to the first nn elements of rr.

Kernel SciencePhyslibSearch · powered by Physlib & Gemini embeddings