IsarMathLib

A library of formalized mathematics for Isabelle/ZF theorem proving environment

theory Fold_ZF imports InductiveSeq_ZF
begin

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\).

Folding in ZF

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 \)proof
from \( a:n\rightarrow Y \) have D: \( domain(a) = n \) using func1_1_L1
with A2, A3, A4 show \( T : n \rightarrow (X\rightarrow X) \) using apply_funtype , fix_2nd_var_fun , ZF_fun_from_total , Seq2TrSeq_def
with A4, D have I: \( \forall k \in n.\ T(k) = \text{Fix2ndVar}(f,a(k)) \) using Seq2TrSeq_def , ZF_fun_from_tot_val0
{
fix \( k \) \( fix \) \( x \)
assume A5: \( k\in n \), \( x\in X \)
with A1, A3 have \( a(k) \in Y \) using apply_funtype
with A2, A5, I have \( (T(k))(x) = f\langle x,a(k)\rangle \) using fix_var_val
}
thus \( \forall k\in n.\ \forall x\in X.\ (T(k))(x) = f\langle x,a(k)\rangle \)
qed

Basic 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 \)proof
let \( T = \text{Seq2TrSeq}(f,y) \)
from A1, A3 have D: \( domain(y) = n \) using func1_1_L1
from \( f : X\times Y \rightarrow X \), \( Y\neq 0 \) have I: \( \text{fstdom}(f) = X \) using fstdomdef
with A1, A2, A3, A4, A6, D show II: \( F: succ(n) \rightarrow X \) and \( F(0) = x \) using seq2trans_seq_props , FoldSeq_def , fin_indseq_var_f_props
from A1, A2, A3, A4, A6, I, D have \( \forall k\in n.\ F(succ(k)) = T(k)(F(k)) \) using seq2trans_seq_props , FoldSeq_def , fin_indseq_var_f_props
moreover {
fix \( k \)
assume A5: \( k\in n \)
hence \( k \in succ(n) \)
with A1, A2, A3, II, A5 have \( (T(k))(F(k)) = f\langle F(k),y(k)\rangle \) using apply_funtype , seq2trans_seq_props
} ultimately show \( \forall k\in n.\ F(succ(k)) = f\langle F(k), y(k)\rangle \)
qed

A 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) \)proof
let \( P = \text{FoldSeq}(f,x,a) \)
let \( Q = \text{FoldSeq}(f,x,b) \)
from assms have \( n \in nat \), \( k \in succ(n) \), \( Q(0) = P(0) \) and \( \forall j \in n.\ Q(j) = P(j) \longrightarrow Q(succ(j)) = P(succ(j)) \) using fold_seq_props
then show \( Q(k) = P(k) \) by (rule fin_nat_ind )
qed

A 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) \)proof
let \( b = \text{Append}(a,y) \)
from assms have \( b : succ(n) \rightarrow Y \), \( \forall j \in n.\ b(j) = a(j) \) using append_props
with assms show \( thesis \) using foldseq_restrict
qed

What 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 \)proof
from assms have \( \text{FoldSeq}(f,x,a) : succ(n) \rightarrow X \) using fold_seq_props
with A1 show \( \text{Fold}(f,x,a) = \text{FoldSeq}(f,x,a)(n) \) and \( \text{Fold}(f,x,a) \in X \) using last_seq_elem , apply_funtype , Fold_def
qed

A 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 \)proof
let \( F = \text{FoldSeq}(f,x,a) \)
from assms have I: \( 0 \in nat \), \( f : X\times Y \rightarrow X \), \( a:0\rightarrow Y \), \( x\in X \), \( Y\neq 0 \)
then have \( \text{Fold}(f,x,a) = F(0) \) by (rule fold_props )
moreover
from I have \( 0 \in nat \), \( f : X\times Y \rightarrow X \), \( a:0\rightarrow Y \), \( x\in X \), \( Y\neq 0 \) and \( F = \text{FoldSeq}(f,x,a) \)
then have \( F(0) = x \) by (rule fold_seq_props )
ultimately show \( \text{Fold}(f,x,a) = x \)
qed

The 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 \)proof
let \( b = \text{Append}(a,y) \)
let \( P = \text{FoldSeq}(f,x,b) \)
from A5 have I: \( Y \neq 0 \)
with assms show thesis1: \( P(n) = \text{Fold}(f,x,a) \) using fold_seq_append , fold_props
from assms, I have II: \( succ(n) \in nat \), \( f : X\times Y \rightarrow X \), \( b : succ(n) \rightarrow Y \), \( x\in X \), \( Y \neq 0 \), \( P = \text{FoldSeq}(f,x,b) \) using append_props
then have \( \forall k \in succ(n).\ P(succ(k)) = f\langle P(k), b(k)\rangle \) by (rule fold_seq_props )
with A3, A5, thesis1 have \( P(succ(n)) = f\langle \text{Fold}(f,x,a), y\rangle \) using append_props
moreover
from II have \( P : succ(succ(n)) \rightarrow X \) by (rule fold_seq_props )
then have \( \text{Fold}(f,x,b) = P(succ(n)) \) using last_seq_elem , Fold_def
ultimately show \( \text{Fold}(f,x, \text{Append}(a,y)) = f\langle \text{Fold}(f,x,a), y\rangle \)
qed
end
lemma func1_1_L1:

assumes \( f:A\rightarrow C \)

shows \( domain(f) = A \)
lemma fix_2nd_var_fun:

assumes \( f : X\times Y \rightarrow Z \) and \( y\in Y \)

shows \( \text{Fix2ndVar}(f,y) : X \rightarrow Z \)
lemma ZF_fun_from_total:

assumes \( \forall x\in X.\ b(x) \in Y \)

shows \( \{\langle x,b(x)\rangle .\ x\in X\} : X\rightarrow Y \)
Definition of Seq2TrSeq: \( \text{Seq2TrSeq}(f,a) \equiv \{\langle k, \text{Fix2ndVar}(f,a(k))\rangle .\ k \in domain(a)\} \)
lemma ZF_fun_from_tot_val0:

assumes \( f:X\rightarrow Y \) and \( f = \{\langle x,b(x)\rangle .\ x\in X\} \)

shows \( \forall x\in X.\ f(x) = b(x) \)
lemma fix_var_val:

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 \)
lemma fstdomdef:

assumes \( f: X\times Y \rightarrow Z \) and \( Y\neq 0 \)

shows \( \text{fstdom}(f) = X \)
lemma seq2trans_seq_props:

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 \)
Definition of FoldSeq: \( \text{FoldSeq}(f,x,a) \equiv \) \( \text{InductiveSeqVarFN}(x, \text{fstdom}(f), \text{Seq2TrSeq}(f,a),domain(a)) \)
theorem fin_indseq_var_f_props:

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)) \)
theorem fold_seq_props:

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 \)
lemma fin_nat_ind:

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) \)
theorem append_props:

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 \)
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) \)
lemma last_seq_elem:

assumes \( a: succ(n) \rightarrow X \)

shows \( \text{Last}(a) = a(n) \)
Definition of Fold: \( \text{Fold}(f,x,a) \equiv \text{Last}( \text{FoldSeq}(f,x,a)) \)
theorem fold_props:

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 \)
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) \)
43
33
9
9