PhyslibSearch

Physlib.Mathematics.List.InsertionSort

26 declarations

theorem

insertionSortMinPos(r,i,l)<length(insertionSortDropMinPos(r,i,l))+1\text{insertionSortMinPos}(r, i, l) < \text{length}(\text{insertionSortDropMinPos}(r, i, l)) + 1

#insertionSortMin_lt_length_succ

Let rr be a decidable binary relation on a type α\alpha. For any element iαi \in \alpha and any list ll of elements in α\alpha, let a=insertionSortMinPos(r,i,l)a = \text{insertionSortMinPos}(r, i, l) be the index of the leftmost minimal element of the list i::li :: l (this is the index that corresponds to the first element of the list after it is sorted). Let ldrop=insertionSortDropMinPos(r,i,l)l_{drop} = \text{insertionSortDropMinPos}(r, i, l) be the list obtained by removing the element at index aa from i::li :: l. Then the index aa is strictly less than the length of ldropl_{drop} plus one, i.e., a<length(ldrop)+1a < \text{length}(l_{drop}) + 1.

definition

Leftmost minimal index of i::li :: l in Fin(length(ldrop)+1)\text{Fin}(\text{length}(l_{\text{drop}}) + 1)

#insertionSortMinPosFin

Let α\alpha be a type with a decidable binary relation rr. For an element iαi \in \alpha and a list ll over α\alpha, let a=insertionSortMinPos(r,i,l)a = \text{insertionSortMinPos}(r, i, l) be the index of the leftmost minimal element of the list i::li :: l. This definition returns aa as an element of the finite type Fin(n+1)\text{Fin}(n + 1), where n=length(insertionSortDropMinPos(r,i,l))n = \text{length}(\text{insertionSortDropMinPos}(r, i, l)) is the length of the list i::li :: l after the element at index aa has been removed.

theorem

r(insertionSortMin(r,a,l),insertionSortDropMinPos(r,a,l)[i])r(\text{insertionSortMin}(r, a, l), \text{insertionSortDropMinPos}(r, a, l)[i])

#insertionSortMin_lt_mem_insertionSortDropMinPos

Let α\alpha be a type equipped with a decidable binary relation rr that is both total and transitive. For any element aαa \in \alpha and list ll of elements in α\alpha, let m=insertionSortMin(r,a,l)m = \text{insertionSortMin}(r, a, l) be the minimum element of the list a::la :: l (the element that would appear at the head of the list after sorting). Let lrem=insertionSortDropMinPos(r,a,l)l_{\text{rem}} = \text{insertionSortDropMinPos}(r, a, l) be the list a::la :: l with that specific minimum element removed. Then, for any index ii of the list lreml_{\text{rem}}, the relation r(m,lrem[i])r(m, l_{\text{rem}}[i]) holds.

theorem

`insertionSortEquiv` maps `insertionSortMinPos` to the first index 00

#insertionSortMinPos_insertionSortEquiv

Let α\alpha be a type and rr be a decidable binary relation on α\alpha. For any element aαa \in \alpha and any list ll of elements in α\alpha, let σ\sigma denote the bijection `insertionSortEquiv` that maps the indices of the list a::la :: l to the indices of the list sorted by insertion sort. Let kk be the index in a::la :: l defined by `insertionSortMinPos(r, a, l)`. Then the equivalence σ\sigma maps the index kk to the first position of the sorted list, i.e., σ(k)=0\sigma(k) = 0.

theorem

kinsertionSortMinPos    insertionSortEquiv(k)>0k \neq \text{insertionSortMinPos} \implies \text{insertionSortEquiv}(k) > 0

#insertionSortEquiv_gt_zero_of_ne_insertionSortMinPos

Let α\alpha be a type equipped with a decidable binary relation rr. For any element aαa \in \alpha and list ll over α\alpha, let σ\sigma be the bijection (equivalence) between the indices of the list a::la :: l and the indices of the sorted list insertionSort(r,a::l)\text{insertionSort}(r, a :: l). Let kmink_{\text{min}} be the index in the original list a::la :: l that corresponds to the first element (index 00) of the sorted list. For any index kk of the list a::la :: l, if kkmink \neq k_{\text{min}}, then the index of the corresponding element in the sorted list is strictly greater than zero, i.e., σ(k)>0\sigma(k) > 0.

theorem

k<insertionSortMinPos    ¬r(L[k],insertionSortMin)k < \text{insertionSortMinPos} \implies \neg r(L[k], \text{insertionSortMin})

#insertionSortMin_lt_mem_insertionSortDropMinPos_of_lt

Let rr be a decidable binary relation on a type α\alpha. For any element aαa \in \alpha and list ll over α\alpha, let kmin=insertionSortMinPos(r,a,l)k_{\text{min}} = \text{insertionSortMinPos}(r, a, l) be the index of the leftmost minimal element in the list a::la :: l with respect to rr, and let xmin=insertionSortMin(r,a,l)x_{\text{min}} = \text{insertionSortMin}(r, a, l) be the value at that index. Let Ldrop=insertionSortDropMinPos(r,a,l)L_{\text{drop}} = \text{insertionSortDropMinPos}(r, a, l) be the list formed by removing the element at index kmink_{\text{min}} from a::la :: l. For any index ii of LdropL_{\text{drop}}, if the corresponding index kk in the original list a::la :: l (defined by k=succAbovekmin(i)k = \text{succAbove}_{k_{\text{min}}}(i)) satisfies k<kmink < k_{\text{min}}, then the relation rr does not hold for the pair (Ldrop[i],xmin)(L_{\text{drop}}[i], x_{\text{min}}), i.e., ¬r(Ldrop[i],xmin)\neg r(L_{\text{drop}}[i], x_{\text{min}}).

theorem

Idempotency of insertion sort

#insertionSort_insertionSort

Let α\alpha be a type equipped with a decidable, total, and transitive relation rr. For any list ll of elements in α\alpha, applying the insertion sort algorithm with respect to rr twice is equivalent to applying it once: insertionSortr(insertionSortrl)=insertionSortrl \text{insertionSort}_r (\text{insertionSort}_r l) = \text{insertionSort}_r l

theorem

¬r(a,b)\neg r(a, b) implies ordered insertion of aa and bb commutes

#orderedInsert_commute

Let rr be a total and transitive binary relation on a type α\alpha. For any elements a,bαa, b \in \alpha and any list ll of elements in α\alpha, if the relation r(a,b)r(a, b) does not hold, then the result of performing an ordered insertion of bb then aa into ll is equal to the result of performing an ordered insertion of aa then bb. That is, orderedInsert(r,a,orderedInsert(r,b,l))=orderedInsert(r,b,orderedInsert(r,a,l)) \text{orderedInsert}(r, a, \text{orderedInsert}(r, b, l)) = \text{orderedInsert}(r, b, \text{orderedInsert}(r, a, l)) where orderedInsert\text{orderedInsert} is the operation that inserts an element into a list at the first position that maintains the ordering according to rr.

theorem

insertionSort(orderedInsert(a,l1)++l2)=insertionSort((a::l1)++l2)\text{insertionSort}(\text{orderedInsert}(a, l_1) ++ l_2) = \text{insertionSort}((a :: l_1) ++ l_2)

#insertionSort_orderedInsert_append

Let α\alpha be a type equipped with a decidable, total, and transitive binary relation rr. For any element aαa \in \alpha and any lists l1,l2l_1, l_2 of elements in α\alpha, applying insertion sort to the concatenation of (the ordered insertion of aa into l1l_1) and l2l_2 yields the same result as applying insertion sort to the concatenation of (the list formed by prepending aa to l1l_1) and l2l_2. That is, insertionSortr(orderedInsert(r,a,l1)++l2)=insertionSortr((a::l1)++l2) \text{insertionSort}_r (\text{orderedInsert}(r, a, l_1) ++ l_2) = \text{insertionSort}_r ((a :: l_1) ++ l_2) where orderedInsert(r,a,l1)\text{orderedInsert}(r, a, l_1) is the operation that inserts aa into l1l_1 at the first position maintaining the order according to rr, a::l1a :: l_1 denotes the list with aa prepended to l1l_1, and ++++ denotes list concatenation.

theorem

insertionSort(insertionSort(l1)++l2)=insertionSort(l1++l2)\text{insertionSort}(\text{insertionSort}(l_1) ++ l_2) = \text{insertionSort}(l_1 ++ l_2)

#insertionSort_insertionSort_append

Let α\alpha be a type equipped with a decidable, total, and transitive binary relation rr. For any two lists l1l_1 and l2l_2 of elements in α\alpha, applying insertion sort to the concatenation of the sorted version of l1l_1 and the list l2l_2 yields the same result as applying insertion sort to the concatenation of the original l1l_1 and l2l_2. That is, insertionSortr(insertionSortr(l1)++l2)=insertionSortr(l1++l2) \text{insertionSort}_r (\text{insertionSort}_r(l_1) ++ l_2) = \text{insertionSort}_r (l_1 ++ l_2) where insertionSortr\text{insertionSort}_r denotes the insertion sort algorithm with respect to the relation rr, and ++++ denotes list concatenation.

theorem

insertionSort(l1++insertionSort(l2)++l3)=insertionSort(l1++l2++l3)\text{insertionSort}(l_1 ++ \text{insertionSort}(l_2) ++ l_3) = \text{insertionSort}(l_1 ++ l_2 ++ l_3)

#insertionSort_append_insertionSort_append

Let α\alpha be a type equipped with a decidable, total, and transitive binary relation rr. For any three lists l1,l2l_1, l_2, and l3l_3 of elements in α\alpha, applying insertion sort to the concatenation of l1l_1, the sorted version of l2l_2, and l3l_3 yields the same result as applying insertion sort to the concatenation of the original lists l1,l2l_1, l_2, and l3l_3. That is, insertionSortr(l1++insertionSortr(l2)++l3)=insertionSortr(l1++l2++l3) \text{insertionSort}_r (l_1 ++ \text{insertionSort}_r(l_2) ++ l_3) = \text{insertionSort}_r (l_1 ++ l_2 ++ l_3) where insertionSortr\text{insertionSort}_r denotes the insertion sort algorithm with respect to the relation rr, and ++++ denotes list concatenation.

theorem

length(orderedInsert r a l)=length(a::l)\text{length}(\text{orderedInsert } r \ a \ l) = \text{length}(a :: l)

#orderedInsert_length

For any type α\alpha, a decidable binary relation rr on α\alpha, an element aαa \in \alpha, and a list ll of elements in α\alpha, the length of the list resulting from the ordered insertion of aa into ll with respect to rr is equal to the length of the list formed by prepending aa to ll (i.e., length(a::l)\text{length}(a :: l) or length(l)+1\text{length}(l) + 1).

theorem

length(takeWhile (¬r(a,))(orderedInsert r b l))=length(takeWhile (¬r(a,)) l)+1\text{length}(\text{takeWhile } (\neg r(a, \cdot)) (\text{orderedInsert } r \text{ } b \text{ } l)) = \text{length}(\text{takeWhile } (\neg r(a, \cdot)) \text{ } l) + 1 when ¬r(a,b)\neg r(a, b)

#takeWhile_orderedInsert

Let α\alpha be a type with a decidable, total, and transitive relation rr. Given two elements a,bαa, b \in \alpha such that the relation r(a,b)r(a, b) does not hold, for any list ll over α\alpha, the length of the prefix of the list obtained by performing an ordered insertion of bb into ll (denoted orderedInsert(r,b,l)\text{orderedInsert}(r, b, l)) consisting of elements cc for which r(a,c)r(a, c) is false, is equal to the length of the corresponding prefix in ll plus one. That is, length(takeWhile (λc.¬r(a,c))(orderedInsert r b l))=length(takeWhile (λc.¬r(a,c)) l)+1 \text{length}(\text{takeWhile } (\lambda c. \neg r(a, c)) (\text{orderedInsert } r \text{ } b \text{ } l)) = \text{length}(\text{takeWhile } (\lambda c. \neg r(a, c)) \text{ } l) + 1 where takeWhile\text{takeWhile} is the function that returns the longest prefix of a list satisfying a given predicate.

theorem

length(takeWhile (¬r(b,))(orderedInsert r a l))=length(takeWhile (¬r(b,)) l)\text{length}(\text{takeWhile } (\neg r(b, \cdot)) (\text{orderedInsert } r \text{ } a \text{ } l)) = \text{length}(\text{takeWhile } (\neg r(b, \cdot)) \text{ } l) when ¬r(a,b)\neg r(a, b)

#takeWhile_orderedInsert'

Let α\alpha be a type equipped with a decidable, total, and transitive binary relation rr. For any two elements a,bαa, b \in \alpha such that the relation r(a,b)r(a, b) does not hold, and for any list ll over α\alpha, the length of the longest prefix of the list obtained by performing an ordered insertion of aa into ll (denoted orderedInsert(r,a,l)\text{orderedInsert}(r, a, l)) consisting of elements cc for which r(b,c)r(b, c) is false, is equal to the length of the corresponding prefix in the original list ll. That is, length(takeWhile (λc.¬r(b,c))(orderedInsert r a l))=length(takeWhile (λc.¬r(b,c)) l) \text{length}(\text{takeWhile } (\lambda c. \neg r(b, c)) (\text{orderedInsert } r \text{ } a \text{ } l)) = \text{length}(\text{takeWhile } (\lambda c. \neg r(b, c)) \text{ } l) where takeWhile\text{takeWhile} is the function that returns the longest prefix of a list satisfying a given predicate.

theorem

insertionSortEquiv(a::b::l,n+2)=insertionSortEquiv(b::a::l,n+2)\text{insertionSortEquiv}(a::b::l, n+2) = \text{insertionSortEquiv}(b::a::l, n+2) when ¬r(a,b)\neg r(a, b)

#insertionSortEquiv_commute

Let α\alpha be a type equipped with a decidable, total, and transitive binary relation rr. Let a,bαa, b \in \alpha be elements such that the relation r(a,b)r(a, b) does not hold. For any list ll and any natural number nn such that n+2n+2 is a valid index for the list a::b::la :: b :: l, let ϕL\phi_L denote the bijection `insertionSortEquiv` which maps the indices of a list LL to the indices of its sorted version insertionSort(r,L)\text{insertionSort}(r, L). Then it holds that: ϕa::b::l(n+2)=ϕb::a::l(n+2)\phi_{a :: b :: l}(n+2) = \phi_{b :: a :: l}(n+2) This theorem states that for elements originally located in the tail ll of a list, their final position in the sorted list is independent of the initial order of the head elements aa and bb, provided that aa and bb are the same pair and their relative sorted order is fixed (by ¬r(a,b)\neg r(a, b)).

theorem

ϕorderedInsert(a,l1)+ ⁣+(a2::l2)(l1+1)=ϕ(a::l1)+ ⁣+(a2::l2)(l1+1)\phi_{\text{orderedInsert}(a, l_1) \mathbin{+\!+} (a_2 :: l_2)}(|l_1| + 1) = \phi_{(a :: l_1) \mathbin{+\!+} (a_2 :: l_2)}(|l_1| + 1)

#insertionSortEquiv_orderedInsert_append

Let α\alpha be a type equipped with a decidable, total, and transitive binary relation rr. For any elements a,a2αa, a_2 \in \alpha and lists l1,l2l_1, l_2 of elements in α\alpha, let ϕL\phi_L denote the bijection `insertionSortEquiv` which maps the indices of a list LL to the indices of its sorted version insertionSort(r,L)\text{insertionSort}(r, L). Then, the final position (index) in the sorted list for the element a2a_2 is the same whether aa is prepended to l1l_1 or inserted into l1l_1 using an ordered insertion. That is: ϕorderedInsert(r,a,l1)+ ⁣+(a2::l2)(l1+1)=ϕ(a::l1)+ ⁣+(a2::l2)(l1+1)\phi_{\text{orderedInsert}(r, a, l_1) \mathbin{+\!+} (a_2 :: l_2)}(|l_1| + 1) = \phi_{(a :: l_1) \mathbin{+\!+} (a_2 :: l_2)}(|l_1| + 1) where l1|l_1| is the length of list l1l_1, and the index l1+1|l_1| + 1 refers to the position of element a2a_2 in both concatenated lists.

theorem

ϕinsertionSort(r,l1)+ ⁣+(a::l2)(l1)=ϕl1+ ⁣+(a::l2)(l1)\phi_{\text{insertionSort}(r, l_1) \mathbin{+\!+} (a :: l_2)}(|l_1|) = \phi_{l_1 \mathbin{+\!+} (a :: l_2)}(|l_1|)

#insertionSortEquiv_insertionSort_append

Let α\alpha be a type equipped with a decidable, total, and transitive binary relation rr. For any element aαa \in \alpha and any two lists l1,l2l_1, l_2 of elements in α\alpha, let ϕL\phi_L denote the bijection `insertionSortEquiv` which maps the indices of a list LL to the indices of its sorted version insertionSort(r,L)\text{insertionSort}(r, L). Then, the final position (index) in the sorted list for the element aa is the same whether the prefix l1l_1 is sorted prior to the global sort or not. That is: ϕinsertionSort(r,l1)+ ⁣+(a::l2)(l1)=ϕl1+ ⁣+(a::l2)(l1)\phi_{\text{insertionSort}(r, l_1) \mathbin{+\!+} (a :: l_2)}(|l_1|) = \phi_{l_1 \mathbin{+\!+} (a :: l_2)}(|l_1|) where l1|l_1| is the length of the list l1l_1, and the index l1|l_1| refers to the position of element aa in both concatenated lists.

theorem

filter p(orderedInsert r a l)=orderedInsert r a(filter p l)\text{filter } p (\text{orderedInsert } r \ a \ l) = \text{orderedInsert } r \ a (\text{filter } p \ l) when p(a)p(a) holds and ll is rr-ordered

#orderedInsert_filter_of_pos

Let α\alpha be a type, rr be a transitive and decidable binary relation on α\alpha, and pp be a decidable predicate on α\alpha. If an element aαa \in \alpha satisfies the predicate pp, then for any list ll that is pairwise ordered by rr, filtering the result of an ordered insertion of aa into ll is equal to performing an ordered insertion of aa into the filtered version of ll: filter p(orderedInsert r a l)=orderedInsert r a(filter p l)\text{filter } p (\text{orderedInsert } r \ a \ l) = \text{orderedInsert } r \ a (\text{filter } p \ l) Here, `orderedInsert` denotes the operation of inserting an element into a list while maintaining the order defined by rr, and `filter` denotes the selection of elements in a list that satisfy the predicate pp.

theorem

filter p(orderedInsert r a l)=filter p l\text{filter } p (\text{orderedInsert } r \ a \ l) = \text{filter } p \ l if ¬p(a)\neg p(a)

#orderedInsert_filter_of_neg

Let α\alpha be a type with a decidable binary relation rr. For any element aαa \in \alpha and any decidable predicate pp such that p(a)p(a) is false, and for any list ll of elements in α\alpha, the result of filtering the list after performing an ordered insertion of aa into ll is the same as filtering the original list ll. That is, filter p(orderedInsert r a l)=filter p l\text{filter } p (\text{orderedInsert } r \ a \ l) = \text{filter } p \ l

theorem

insertionSort\text{insertionSort} commutes with filter\text{filter}

#insertionSort_filter

Let α\alpha be a type, rr be a decidable, total, and transitive binary relation on α\alpha, and pp be a decidable predicate on α\alpha. For any list ll of elements in α\alpha, performing an insertion sort on the list filtered by pp yields the same result as filtering the list after it has been sorted using insertion sort. That is, insertionSort(r,filter(p,l))=filter(p,insertionSort(r,l))\text{insertionSort}(r, \text{filter}(p, l)) = \text{filter}(p, \text{insertionSort}(r, l))

theorem

For a sorted list ll, takeWhile(¬r(a,),l)=filter(¬r(a,),l)\text{takeWhile}(\neg r(a, \cdot), l) = \text{filter}(\neg r(a, \cdot), l)

#takeWhile_sorted_eq_filter

Let rr be a transitive binary relation on a type α\alpha. For any element aαa \in \alpha and any list ll of elements in α\alpha that is pairwise related by rr (i.e., for any elements x,yx, y in ll, if xx appears before yy, then r(x,y)r(x, y) holds), the operation of taking elements from the beginning of ll while ¬r(a,c)\neg r(a, c) holds is equivalent to filtering the entire list ll for elements satisfying ¬r(a,c)\neg r(a, c). That is, takeWhile(λc.¬r(a,c),l)=filter(λc.¬r(a,c),l)\text{takeWhile}(\lambda c. \neg r(a, c), l) = \text{filter}(\lambda c. \neg r(a, c), l).

theorem

`dropWhile ¬r` equals `filter r` for lists sorted by rr

#dropWhile_sorted_eq_filter

Let rr be a transitive and decidable binary relation on a type α\alpha. For any element aαa \in \alpha and any list ll that is pairwise related by rr (i.e., ll is sorted with respect to rr), the list obtained by dropping elements from the beginning of ll that do not satisfy r(a,c)r(a, c) is equal to the list obtained by filtering ll to keep only those elements cc such that r(a,c)r(a, c) holds.

theorem

Decomposition of filter(r(a,),l)\text{filter}(r(a, \cdot), l) into equivalence and strict relation parts for rr-pairwise lists

#dropWhile_sorted_eq_filter_filter

Let rr be a transitive binary relation on a type α\alpha. For any list ll of elements in α\alpha that is pairwise related by rr (meaning for any elements xx and yy in ll such that xx appears before yy, the relation r(x,y)r(x, y) holds), and for any element aαa \in \alpha, the sublist of ll containing elements cc for which r(a,c)r(a, c) holds is equal to the concatenation of: 1. The sublist of elements clc \in l satisfying both r(a,c)r(a, c) and r(c,a)r(c, a). 2. The sublist of elements clc \in l satisfying r(a,c)r(a, c) but not r(c,a)r(c, a). In mathematical notation, this is expressed as: filter(λc.r(a,c),l)=filter(λc.r(a,c)r(c,a),l)++filter(λc.r(a,c)¬r(c,a),l)\text{filter}(\lambda c. r(a, c), l) = \text{filter}(\lambda c. r(a, c) \wedge r(c, a), l) ++ \text{filter}(\lambda c. r(a, c) \wedge \neg r(c, a), l) where filter(P,l)\text{filter}(P, l) is the sublist of elements in ll satisfying property PP, and ++++ denotes the concatenation of two lists.

theorem

filter(r(a,)r(,a),insertionSort r l)=filter(r(a,)r(,a),l)\text{filter}(r(a, \cdot) \wedge r(\cdot, a), \text{insertionSort } r \ l) = \text{filter}(r(a, \cdot) \wedge r(\cdot, a), l)

#filter_rel_eq_insertionSort

Let rr be a decidable, total, and transitive binary relation on a type α\alpha. For any element aαa \in \alpha and any list ll of elements in α\alpha, the sublist of elements cc that are equivalent to aa under rr (satisfying r(a,c)r(c,a)r(a, c) \wedge r(c, a)) is the same whether it is extracted from the original list ll or from the list obtained after performing an insertion sort on ll with respect to rr. In mathematical notation: filter(λc.r(a,c)r(c,a),insertionSort r l)=filter(λc.r(a,c)r(c,a),l)\text{filter}(\lambda c. r(a, c) \wedge r(c, a), \text{insertionSort } r \ l) = \text{filter}(\lambda c. r(a, c) \wedge r(c, a), l) where insertionSort r l\text{insertionSort } r \ l denotes the list ll sorted by the insertion sort algorithm according to the relation rr.

theorem

Decomposition of insertionSort(r,l1++l++l2)\text{insertionSort}(r, l_1 ++ l ++ l_2) for a block ll of elements equivalent to aa

#insertionSort_of_eq_list

Let rr be a decidable, total, and transitive binary relation on a type α\alpha. Let aαa \in \alpha be an element, and let l1,l,l_1, l, and l2l_2 be lists of elements in α\alpha. Suppose that every element bb in the list ll is equivalent to aa with respect to rr (i.e., r(a,b)r(b,a)r(a, b) \wedge r(b, a) holds for all blb \in l). Then the insertion sort of the concatenation l1++l++l2l_1 ++ l ++ l_2 with respect to rr is equal to the concatenation of the following five parts: 1. The prefix of insertionSort(r,l1++l2)\text{insertionSort}(r, l_1 ++ l_2) consisting of elements cc such that ¬r(a,c)\neg r(a, c). 2. The sublist of l1l_1 consisting of elements cc such that r(a,c)r(c,a)r(a, c) \wedge r(c, a). 3. The list ll itself. 4. The sublist of l2l_2 consisting of elements cc such that r(a,c)r(c,a)r(a, c) \wedge r(c, a). 5. The sublist of insertionSort(r,l1++l2)\text{insertionSort}(r, l_1 ++ l_2) consisting of elements cc such that r(a,c)¬r(c,a)r(a, c) \wedge \neg r(c, a). In mathematical notation: insertionSort(r,l1++l++l2)= takeWhile(λc.¬r(a,c),insertionSort(r,l1++l2)) ++filter(λc.r(a,c)r(c,a),l1) ++l ++filter(λc.r(a,c)r(c,a),l2) ++filter(λc.r(a,c)¬r(c,a),insertionSort(r,l1++l2)) \begin{aligned} \text{insertionSort}(r, l_1 ++ l ++ l_2) = \text{ } & \text{takeWhile}(\lambda c. \neg r(a, c), \text{insertionSort}(r, l_1 ++ l_2)) \ ++ \\ & \text{filter}(\lambda c. r(a, c) \wedge r(c, a), l_1) \ ++ \\ & l \ ++ \\ & \text{filter}(\lambda c. r(a, c) \wedge r(c, a), l_2) \ ++ \\ & \text{filter}(\lambda c. r(a, c) \wedge \neg r(c, a), \text{insertionSort}(r, l_1 ++ l_2)) \end{aligned}

theorem

Decomposition of insertionSort(r,l1++l2)\text{insertionSort}(r, l_1 ++ l_2) relative to an element aa

#insertionSort_of_takeWhile_filter

Let rr be a decidable, total, and transitive binary relation on a type α\alpha. For any element aαa \in \alpha and any two lists l1,l2l_1, l_2 of elements in α\alpha, the insertion sort of the concatenation l1++l2l_1 ++ l_2 with respect to rr can be decomposed into the concatenation of the following four parts: 1. The prefix of insertionSort(r,l1++l2)\text{insertionSort}(r, l_1 ++ l_2) consisting of elements cc such that ¬r(a,c)\neg r(a, c). 2. The sublist of l1l_1 consisting of elements cc equivalent to aa (satisfying r(a,c)r(c,a)r(a, c) \wedge r(c, a)). 3. The sublist of l2l_2 consisting of elements cc equivalent to aa (satisfying r(a,c)r(c,a)r(a, c) \wedge r(c, a)). 4. The sublist of insertionSort(r,l1++l2)\text{insertionSort}(r, l_1 ++ l_2) consisting of elements cc such that r(a,c)¬r(c,a)r(a, c) \wedge \neg r(c, a). In mathematical notation: insertionSort(r,l1++l2)= takeWhile(λc.¬r(a,c),insertionSort(r,l1++l2)) ++filter(λc.r(a,c)r(c,a),l1) ++filter(λc.r(a,c)r(c,a),l2) ++filter(λc.r(a,c)¬r(c,a),insertionSort(r,l1++l2)) \begin{aligned} \text{insertionSort}(r, l_1 ++ l_2) = \text{ } & \text{takeWhile}(\lambda c. \neg r(a, c), \text{insertionSort}(r, l_1 ++ l_2)) \ ++ \\ & \text{filter}(\lambda c. r(a, c) \wedge r(c, a), l_1) \ ++ \\ & \text{filter}(\lambda c. r(a, c) \wedge r(c, a), l_2) \ ++ \\ & \text{filter}(\lambda c. r(a, c) \wedge \neg r(c, a), \text{insertionSort}(r, l_1 ++ l_2)) \end{aligned}