Suppose we have a binary operation \(P: X\times X \rightarrow X\) written multiplicatively as \(P\langle x, y \rangle= x\cdot y\). In informal mathematics we can take a sequence \(\{ x_k \}_{k\in 0.. n}\) of elements of \(X\) and consider the product \(x_0\cdot x_1 \cdot .. \cdot x_n\). To do the same thing in formalized mathematics we have to define precisely what is meant by that "\(\cdot .. \cdot\)". The definitition we want to use is based on the notion of sequence defined by induction discussed in InductiveSeq_ZF. We don't really want to derive the terminology for this from the word "product" as that would tie it conceptually to the multiplicative notation. This would be awkward when we want to reuse the same notions to talk about sums like \(x_0 + x_1 + .. + x_n\). In functional programming there is something called "fold". Namely for a function \(f\), initial point \(a\) and list \(\left[ b, c, d\right]\) the expression \( fold(f, a, [b,c,d]) \) is defined to be \( f(f(f(a,b),c),d) \) (in Haskell something like this is called foldl). If we write \(f\) in multiplicative notation we get \(a\cdot b \cdot c\cdot d\), so this is exactly what we need. The notion of folds in functional programming is actually much more general that what we need here (not that I know anything about that). In this theory file we just make a slight generalization and talk about folding a list with a binary operation \(f:X\times Y \rightarrow X\) with \(X\) not necessarily the same as \(Y\).
Suppose we have a binary operation \(f : X\times Y \rightarrow X\). Then every \(y\in Y\) defines a transformation of \(X\) defined by \(T_y(x) = f\langle x,y\rangle\). In IsarMathLib such transformation is called as \( \text{Fix2ndVar}(f,y) \). Using this notion, given a function \(f: X\times Y\rightarrow X\) and a sequence \(y = \{y_k\}_{k\in N}\) of elements of \(X\) we can get a sequence of transformations of \(X\). This is defined in Seq2TransSeq below. Then we use that sequence of tranformations to define the sequence of partial folds (called FoldSeq) by means of InductiveSeqVarFN (defined in InductiveSeq_ZF theory) which implements the inductive sequence determined by a starting point and a sequence of transformations. Finally, we define the fold of a sequence as the last element of the sequence of the partial folds.
Definition that specifies how to convert a sequence \(a\) of elements of \(Y\) into a sequence of transformations of \(X\), given a binary operation \(f :X\times Y \rightarrow X\).
definition
\( \text{Seq2TrSeq}(f,a) \equiv \{\langle k, \text{Fix2ndVar}(f,a(k))\rangle .\ k \in domain(a)\} \)
Definition of a sequence of partial folds.
definition
\( \text{FoldSeq}(f,x,a) \equiv \) \( \text{InductiveSeqVarFN}(x, \text{fstdom}(f), \text{Seq2TrSeq}(f,a),domain(a)) \)
Definition of a fold.
definition
\( \text{Fold}(f,x,a) \equiv \text{Last}( \text{FoldSeq}(f,x,a)) \)
If \(X\) is a set with a binary operation \(f:X\times Y \rightarrow X\) then \( Seq2TransSeqN(f,a) \) converts a sequence \(a\) of elements of \(Y\) into the sequence of corresponding transformations of \(X\).
lemma seq2trans_seq_props:
assumes A1: \( n \in nat \) and A2: \( f : X\times Y \rightarrow X \) and A3: \( a:n\rightarrow Y \) and A4: \( T = \text{Seq2TrSeq}(f,a) \)
shows \( T : n \rightarrow (X\rightarrow X) \) and \( \forall k\in n.\ \forall x\in X.\ (T(k))(x) = f\langle x,a(k)\rangle \)proofBasic properties of the sequence of partial folds of a sequence \(a = \{y_k\}_{k\in \{0,..,n\} }\).
theorem fold_seq_props:
assumes A1: \( n \in nat \) and A2: \( f : X\times Y \rightarrow X \) and A3: \( y:n\rightarrow Y \) and A4: \( x\in X \) and A5: \( Y\neq 0 \) and A6: \( F = \text{FoldSeq}(f,x,y) \)
shows \( F: succ(n) \rightarrow X \), \( F(0) = x \) and \( \forall k\in n.\ F(succ(k)) = f\langle F(k), y(k)\rangle \)proofA consistency condition: if we make the list shorter, then we get a shorter sequence of partial folds with the same values as in the original sequence. This can be proven as a special case of fin_indseq_var_f_restrict but a proof using fold_seq_props and induction turns out to be shorter.
lemma foldseq_restrict:
assumes \( n \in nat \), \( k \in succ(n) \) and \( i \in nat \), \( f : X\times Y \rightarrow X \), \( a : n \rightarrow Y \), \( b : i \rightarrow Y \) and \( n \subseteq i \), \( \forall j \in n.\ b(j) = a(j) \), \( x \in X \), \( Y \neq 0 \)
shows \( \text{FoldSeq}(f,x,b)(k) = \text{FoldSeq}(f,x,a)(k) \)proofA special case of foldseq_restrict when the longer sequence is created from the shorter one by appending one element.
corollary fold_seq_append:
assumes \( n \in nat \), \( f : X\times Y \rightarrow X \), \( a:n \rightarrow Y \) and \( x\in X \), \( k \in succ(n) \), \( y\in Y \)
shows \( \text{FoldSeq}(f,x, \text{Append}(a,y))(k) = \text{FoldSeq}(f,x,a)(k) \)proofWhat we really will be using is the notion of the fold of a sequence, which we define as the last element of (inductively defined) sequence of partial folds. The next theorem lists some properties of the product of the fold operation.
theorem fold_props:
assumes A1: \( n \in nat \) and A2: \( f : X\times Y \rightarrow X \), \( a:n \rightarrow Y \), \( x\in X \), \( Y\neq 0 \)
shows \( \text{Fold}(f,x,a) = \text{FoldSeq}(f,x,a)(n) \) and \( \text{Fold}(f,x,a) \in X \)proofA corner case: what happens when we fold an empty list?
theorem fold_empty:
assumes A1: \( f : X\times Y \rightarrow X \) and A2: \( a:0\rightarrow Y \), \( x\in X \), \( Y\neq 0 \)
shows \( \text{Fold}(f,x,a) = x \)proofThe next theorem tells us what happens to the fold of a sequence when we add one more element to it.
theorem fold_append:
assumes A1: \( n \in nat \) and A2: \( f : X\times Y \rightarrow X \) and A3: \( a:n\rightarrow Y \) and A4: \( x\in X \) and A5: \( y\in Y \)
shows \( \text{FoldSeq}(f,x, \text{Append}(a,y))(n) = \text{Fold}(f,x,a) \) and \( \text{Fold}(f,x, \text{Append}(a,y)) = f\langle \text{Fold}(f,x,a), y\rangle \)proofAnother way of formulating information contained in fold_append is to start with a longer sequence \(a:n+1\rightarrow X\) and then detach the last element from it. This provides an identity between the fold of the longer sequence and the value of the folding function on the fold of the shorter sequence and the last element of the longer one.
lemma fold_detach_last:
assumes \( n \in nat \), \( f : X\times Y \rightarrow X \), \( x\in X \), \( \forall k\in n + 1.\ q(k) \in Y \)
shows \( \text{Fold}(f,x,\{\langle k,q(k)\rangle .\ k\in n + 1\}) = f\langle \text{Fold}(f,x,\{\langle k,q(k)\rangle .\ k\in n\}), q(n)\rangle \)proofThe tail of the sequence of partial folds defined by the folding function \(f\), starting point \(x\) and a sequence \(y\) is the same as the sequence of partial folds starting from \(f(x,y(0))\).
lemma fold_seq_detach_first:
assumes \( n \in nat \), \( f : X\times Y \rightarrow X \), \( y:succ(n)\rightarrow Y \), \( x\in X \)
shows \( \text{FoldSeq}(f,f\langle x,y(0)\rangle , \text{Tail}(y)) = \text{Tail}( \text{FoldSeq}(f,x,y)) \)proofTaking a fold of a sequence \(y\) with a function \(f\) with the starting point \(x\) is the same as the fold starting from \(f\langle x,y(0)\rangle\) of the tail of \(y\).
lemma fold_detach_first:
assumes \( n \in nat \), \( f : X\times Y \rightarrow X \), \( y:succ(n)\rightarrow Y \), \( x\in X \)
shows \( \text{Fold}(f,x,y) = \text{Fold}(f,f\langle x,y(0)\rangle , \text{Tail}(y)) \)proofassumes \( f:A\rightarrow C \)
shows \( domain(f) = A \)assumes \( f : X\times Y \rightarrow Z \) and \( y\in Y \)
shows \( \text{Fix2ndVar}(f,y) : X \rightarrow Z \)assumes \( \forall x\in X.\ b(x) \in Y \)
shows \( \{\langle x,b(x)\rangle .\ x\in X\} : X\rightarrow Y \)assumes \( f:X\rightarrow Y \) and \( f = \{\langle x,b(x)\rangle .\ x\in X\} \)
shows \( \forall x\in X.\ f(x) = b(x) \)assumes \( f : X\times Y \rightarrow Z \) and \( x\in X \), \( y\in Y \)
shows \( \text{Fix1stVar}(f,x)(y) = f\langle x,y\rangle \), \( \text{Fix2ndVar}(f,y)(x) = f\langle x,y\rangle \)assumes \( f: X\times Y \rightarrow Z \) and \( Y\neq \emptyset \)
shows \( \text{fstdom}(f) = X \)assumes \( n \in nat \) and \( f : X\times Y \rightarrow X \) and \( a:n\rightarrow Y \) and \( T = \text{Seq2TrSeq}(f,a) \)
shows \( T : n \rightarrow (X\rightarrow X) \) and \( \forall k\in n.\ \forall x\in X.\ (T(k))(x) = f\langle x,a(k)\rangle \)assumes \( n \in nat \) and \( x\in X \) and \( F: n \rightarrow (X\rightarrow X) \) and \( a = \text{InductiveSeqVarFN}(x,X,F,n) \)
shows \( a: succ(n) \rightarrow X \), \( a(0) = x \), \( \forall k\in n.\ a(succ(k)) = F(k)(a(k)) \)assumes \( n \in nat \) and \( f : X\times Y \rightarrow X \) and \( y:n\rightarrow Y \) and \( x\in X \) and \( Y\neq 0 \) and \( F = \text{FoldSeq}(f,x,y) \)
shows \( F: succ(n) \rightarrow X \), \( F(0) = x \) and \( \forall k\in n.\ F(succ(k)) = f\langle F(k), y(k)\rangle \)assumes \( n \in nat \) and \( k \in succ(n) \) and \( P(0) \) and \( \forall j\in n.\ P(j) \longrightarrow P(succ(j)) \)
shows \( P(k) \)assumes \( a: n \rightarrow X \) and \( x\in X \) and \( b = \text{Append}(a,x) \)
shows \( b : succ(n) \rightarrow X \), \( \forall k\in n.\ b(k) = a(k) \), \( b(n) = x \)assumes \( n \in nat \), \( k \in succ(n) \) and \( i \in nat \), \( f : X\times Y \rightarrow X \), \( a : n \rightarrow Y \), \( b : i \rightarrow Y \) and \( n \subseteq i \), \( \forall j \in n.\ b(j) = a(j) \), \( x \in X \), \( Y \neq 0 \)
shows \( \text{FoldSeq}(f,x,b)(k) = \text{FoldSeq}(f,x,a)(k) \)assumes \( a: succ(n) \rightarrow X \)
shows \( \text{Last}(a) = a(n) \)assumes \( n \in nat \) and \( f : X\times Y \rightarrow X \), \( a:n \rightarrow Y \), \( x\in X \), \( Y\neq 0 \)
shows \( \text{Fold}(f,x,a) = \text{FoldSeq}(f,x,a)(n) \) and \( \text{Fold}(f,x,a) \in X \)assumes \( n \in nat \), \( f : X\times Y \rightarrow X \), \( a:n \rightarrow Y \) and \( x\in X \), \( k \in succ(n) \), \( y\in Y \)
shows \( \text{FoldSeq}(f,x, \text{Append}(a,y))(k) = \text{FoldSeq}(f,x,a)(k) \)assumes \( n \in nat \) and \( f : X\times Y \rightarrow X \) and \( a:n\rightarrow Y \) and \( x\in X \) and \( y\in Y \)
shows \( \text{FoldSeq}(f,x, \text{Append}(a,y))(n) = \text{Fold}(f,x,a) \) and \( \text{Fold}(f,x, \text{Append}(a,y)) = f\langle \text{Fold}(f,x,a), y\rangle \)assumes \( n\in nat \) and \( \forall k\in n + 1.\ q(k) \in X \)
defines \( a\equiv \{\langle k,q(k)\rangle .\ k\in n + 1\} \)
shows \( a: n + 1 \rightarrow X \), \( \{\langle k,q(k)\rangle .\ k \in n\}: n \rightarrow X \), \( \text{Init}(a) = \{\langle k,q(k)\rangle .\ k \in n\} \), \( a = \text{Append}(\{\langle k,q(k)\rangle .\ k \in n\},q(n)) \), \( a = \text{Append}( \text{Init}(a), q(n)) \), \( a = \text{Append}( \text{Init}(a), a(n)) \)assumes \( n \in nat \)
shows \( 0 \in succ(n) \)assumes \( n \in nat \) and \( f : X\times Y \rightarrow X \) and \( y:n\rightarrow Y \) and \( x\in X \) and \( Y\neq 0 \) and \( F = \text{FoldSeq}(f,x,y) \)
shows \( F: succ(n) \rightarrow X \), \( F(0) = x \) and \( \forall k\in n.\ F(succ(k)) = f\langle F(k), y(k)\rangle \)assumes \( n \in nat \) and \( a: succ(n) \rightarrow X \)
shows \( \text{Tail}(a) : n \rightarrow X \), \( \forall k \in n.\ \text{Tail}(a)(k) = a(succ(k)) \)assumes \( a:succ(n)\rightarrow Y \)
shows \( Y\neq 0 \)assumes \( n \in nat \) and \( a: succ(n) \rightarrow X \)
shows \( \text{Tail}(a) : n \rightarrow X \), \( \forall k \in n.\ \text{Tail}(a)(k) = a(succ(k)) \)assumes \( n \in nat \) and \( f : X\times Y \rightarrow X \) and \( y:n\rightarrow Y \) and \( x\in X \) and \( Y\neq 0 \) and \( F = \text{FoldSeq}(f,x,y) \)
shows \( F: succ(n) \rightarrow X \), \( F(0) = x \) and \( \forall k\in n.\ F(succ(k)) = f\langle F(k), y(k)\rangle \)assumes \( n \in nat \) and \( f : X\times Y \rightarrow X \) and \( y:n\rightarrow Y \) and \( x\in X \) and \( Y\neq 0 \) and \( F = \text{FoldSeq}(f,x,y) \)
shows \( F: succ(n) \rightarrow X \), \( F(0) = x \) and \( \forall k\in n.\ F(succ(k)) = f\langle F(k), y(k)\rangle \)assumes \( n \in nat \)
shows \( \forall i \in n.\ succ(i) \in succ(n) \)assumes \( n \in nat \) and \( f : X\times Y \rightarrow X \) and \( a:n\rightarrow Y \) and \( T = \text{Seq2TrSeq}(f,a) \)
shows \( T : n \rightarrow (X\rightarrow X) \) and \( \forall k\in n.\ \forall x\in X.\ (T(k))(x) = f\langle x,a(k)\rangle \)assumes \( n\in nat \), \( x\in X \), \( F: n \rightarrow (X\rightarrow X) \) and \( a: succ(n) \rightarrow X \), \( a(0) = x \), \( \forall k\in n.\ a(succ(k)) = (F(k))(a(k)) \)
shows \( a = \text{InductiveSeqVarFN}(x,X,F,n) \)assumes \( n\in nat \), \( a: succ(succ(n)) \rightarrow X \)
shows \( \text{Last}( \text{Tail}(a)) = \text{Last}(a) \)assumes \( n \in nat \), \( f : X\times Y \rightarrow X \), \( y:succ(n)\rightarrow Y \), \( x\in X \)
shows \( \text{FoldSeq}(f,f\langle x,y(0)\rangle , \text{Tail}(y)) = \text{Tail}( \text{FoldSeq}(f,x,y)) \)