IsarMathLib

Proofs by humans, for humans, formally verified by Isabelle/ZF proof assistant

theory FiniteSeq_ZF imports Nat_ZF_IML func1
begin

This theory treats finite sequences (i.e. maps nX, where n={0,1,..,n1} is a natural number) as lists. It defines and proves the properties of basic operations on lists: concatenation, appending and element etc.

Lists as finite sequences

A natural way of representing (finite) lists in set theory is through (finite) sequences. In such view a list of elements of a set X is a function that maps the set {0,1,..n1} into X. Since natural numbers in set theory are defined so that n={0,1,..n1}, a list of length n can be understood as an element of the function space nX.

We define the set of lists with values in set X as Lists(X).

definition

Lists(X)nnat. (nX)

The set of nonempty X-value listst will be called NELists(X).

definition

NELists(X)nnat. (succ(n)X)

We first define the shift that moves the second sequence to the domain {n,..,n+k1}, where n,k are the lengths of the first and the second sequence, resp. To understand the notation in the definitions below recall that in Isabelle/ZF pred(n) is the previous natural number and denotes the difference between natural numbers n and k.

definition

ShiftedSeq(b,n){j,b(jn). jNatInterval(n,domain(b))}

We define concatenation of two sequences as the union of the first sequence with the shifted second sequence. The result of concatenating lists a and b is called Concat(a,b).

definition

Concat(a,b)aShiftedSeq(b,domain(a))

For a finite sequence we define the sequence of all elements except the first one. This corresponds to the "tail" function in Haskell. We call it Tail here as well.

definition

Tail(a){k,a(succ(k)). kpred(domain(a))}

A dual notion to Tail is the list of all elements of a list except the last one. Borrowing the terminology from Haskell again, we will call this Init.

definition

Init(a)restrict(a,pred(domain(a)))

Another obvious operation we can talk about is appending an element at the end of a sequence. This is called Append.

definition

Append(a,x)a{domain(a),x}

If lists are modeled as finite sequences (i.e. functions on natural intervals {0,1,..,n1}=n) it is easy to get the first element of a list as the value of the sequence at 0. The last element is the value at n1. To hide this behind a familiar name we define the Last element of a list.

definition

Last(a)a(pred(domain(a)))

A formula for tail of a finite list.

lemma tail_as_set:

assumes nnat and a:n+1X

shows Tail(a)={k,a(k+1). kn} using assms, func1_1_L1, elem_nat_is_nat(2), succ_add_one(1) unfolding Tail_def

Formula for the tail of a list defined by an expression:

lemma tail_formula:

assumes nnat and kn+1. q(k)X

shows Tail({k,q(k). kn+1})={k,q(k+1). kn}proof

Codomain of a nonempty list is nonempty.

lemma nelist_vals_nonempty:

assumes a:succ(n)Y

shows Y0 using assms, codomain_nonempty

Shifted sequence is a function on a the interval of natural numbers.

lemma shifted_seq_props:

assumes A1: nnat, knat and A2: b:kX

shows ShiftedSeq(b,n):NatInterval(n,k)X, iNatInterval(n,k). ShiftedSeq(b,n)(i)=b(in), jk. ShiftedSeq(b,n)(n+j)=b(j)proof

Basis properties of the contatenation of two finite sequences.

theorem concat_props:

assumes A1: nnat, knat and A2: a:nX, b:kX

shows Concat(a,b):n+kX, in. Concat(a,b)(i)=a(i), iNatInterval(n,k). Concat(a,b)(i)=b(in), jk. Concat(a,b)(n+j)=b(j)proof

Properties of concatenating three lists.

lemma concat_concat_list:

assumes A1: nnat, knat, mnat and A2: a:nX, b:kX, c:mX and A3: d=Concat(Concat(a,b),c)

shows d:n+k+mX, jn. d(j)=a(j), jk. d(n+j)=b(j), jm. d(n+k+j)=c(j)proof

Properties of concatenating a list with a concatenation of two other lists.

lemma concat_list_concat:

assumes A1: nnat, knat, mnat and A2: a:nX, b:kX, c:mX and A3: e=Concat(a,Concat(b,c))

shows e:n+k+mX, jn. e(j)=a(j), jk. e(n+j)=b(j), jm. e(n+k+j)=c(j)proof

Concatenation is associative.

theorem concat_assoc:

assumes A1: nnat, knat, mnat and A2: a:nX, b:kX, c:mX

shows Concat(Concat(a,b),c)=Concat(a,Concat(b,c))proof

Properties of Tail.

theorem tail_props:

assumes A1: nnat and A2: a:succ(n)X

shows Tail(a):nX, kn. Tail(a)(k)=a(succ(k))proof

Essentially the second assertion of tail_props but formulated using notation n+1 instead of succ(n):

lemma tail_props2:

assumes nnat, a:n+1X, kn

shows Tail(a)(k)=a(k+1) using assms, succ_add_one(1), tail_props(2), elem_nat_is_nat(2)

A nonempty list can be decomposed into concatenation of its first element and the tail.

lemma first_concat_tail:

assumes nnat, a:succ(n)X

shows a=Concat({0,a(0)},Tail(a))proof

Properties of Append. It is a bit surprising that the we don't need to assume that n is a natural number.

theorem append_props:

assumes A1: a:nX and A2: xX and A3: b=Append(a,x)

shows b:succ(n)X, kn. b(k)=a(k), b(n)=xproof

A special case of append_props: appending to a nonempty list does not change the head (first element) of the list.

corollary head_of_append:

assumes nnat and a:succ(n)X and xX

shows Append(a,x)(0)=a(0) using assms, append_props, empty_in_every_succ

Tail commutes with Append.

theorem tail_append_commute:

assumes A1: nnat and A2: a:succ(n)X and A3: xX

shows Append(Tail(a),x)=Tail(Append(a,x))proof

@{term NELists} are non-empty lists

lemma non_zero_List_func_is_NEList:

shows NELists(X)={aLists(X). a0}proof

Properties of Init.

theorem init_props:

assumes A1: nnat and A2: a:succ(n)X

shows Init(a):nX, kn. Init(a)(k)=a(k), a=Append(Init(a),a(n))proof

The initial part of a non-empty list is a list, and the domain of the original list is the successor of its initial part.

theorem init_NElist:

assumes aNELists(X)

shows Init(a)Lists(X) and succ(domain(Init(a)))=domain(a)proof

If we take init of the result of append, we get back the same list.

lemma init_append:

assumes A1: nnat and A2: a:nX and A3: xX

shows Init(Append(a,x))=aproof

A reformulation of definition of Init.

lemma init_def:

assumes nnat and a:succ(n)X

shows Init(a)=restrict(a,n) using assms, func1_1_L1, Init_def

Another reformulation of the definition of Init, starting with the expression defining the list.

lemma init_def_alt:

assumes nnat and kn+1. q(k)X

shows Init({k,q(k). kn+1})={k,q(k). kn}proof

A lemma about extending a finite sequence by one more value. This is just a more explicit version of append_props.

lemma finseq_extend:

assumes a:nX, yX, b=a{n,y}

shows b:succ(n)X, kn. b(k)=a(k), b(n)=y using assms, Append_def, func1_1_L1, append_props

The next lemma is a bit displaced as it is mainly about finite sets. It is proven here because it uses the notion of Append. Suppose we have a list of element of A is a bijection. Then for every element that does not belong to A we can we can construct a bijection for the set A{x} by appending x. This is just a specialised version of lemma bij_extend_point from func1.thy.

lemma bij_append_point:

assumes A1: nnat and A2: bbij(n,X) and A3: xX

shows Append(b,x)bij(succ(n),X{x})proof

The next lemma rephrases the definition of Last. Recall that in ZF we have {0,1,2,..,n}=n+1=succ(n).

lemma last_seq_elem:

assumes a:succ(n)X

shows Last(a)=a(n) using assms, func1_1_L1, pred_succ_eq, Last_def

The last element of a non-empty list valued in X is in X.

lemma last_type:

assumes aNELists(X)

shows Last(a)X using assms, last_seq_elem, apply_funtype unfolding NELists_def

The last element of a list of length at least 2 is the same as the last element of the tail of that list.

lemma last_tail_last:

assumes nnat, a:succ(succ(n))X

shows Last(Tail(a))=Last(a)proof

If two finite sequences are the same when restricted to domain one shorter than the original and have the same value on the last element, then they are equal.

lemma finseq_restr_eq:

assumes A1: nnat and A2: a:succ(n)X, b:succ(n)X and A3: restrict(a,n)=restrict(b,n) and A4: a(n)=b(n)

shows a=bproof

Concatenating a list of length 1 is the same as appending its first (and only) element. Recall that in ZF set theory 1={0}.

lemma append_1elem:

assumes A1: nnat and A2: a:nX and A3: b:1X

shows Concat(a,b)=Append(a,b(0))proof

If xX then the singleton set with the pair 0,x as the only element is a list of length 1 and hence a nonempty list.

lemma list_len1_singleton:

assumes xX

shows {0,x}:1X and {0,x}NELists(X)proof

A singleton list is in fact a singleton set with a pair as the only element.

lemma list_singleton_pair:

assumes A1: x:1X

shows x={0,x(0)}proof

When we append an element to the empty list we get a list with length 1.

lemma empty_append1:

assumes A1: xX

shows Append(0,x):1X and Append(0,x)(0)=xproof

Appending an element is the same as concatenating with certain pair.

lemma append_concat_pair:

assumes nnat and a:nX and xX

shows Append(a,x)=Concat(a,{0,x}) using assms, list_len1_singleton, append_1elem, pair_val

An associativity property involving concatenation and appending. For proof we just convert appending to concatenation and use concat_assoc.

lemma concat_append_assoc:

assumes A1: nnat, knat and A2: a:nX, b:kX and A3: xX

shows Append(Concat(a,b),x)=Concat(a,Append(b,x))proof

An identity involving concatenating with init and appending the last element.

lemma concat_init_last_elem:

assumes nnat, knat and a:nX and b:succ(k)X

shows Append(Concat(a,Init(b)),b(k))=Concat(a,b) using assms, init_props, apply_funtype, concat_append_assoc

A lemma about creating lists by composition and how Append behaves in such case.

lemma list_compose_append:

assumes A1: nnat and A2: a:nX and A3: xX and A4: c:XY

shows cAppend(a,x):succ(n)Y, cAppend(a,x)=Append(ca,c(x))proof

A lemma about appending an element to a list defined by set comprehension.

lemma set_list_append:

assumes A1: isucc(k). b(i)X and A2: a={i,b(i). isucc(k)}

shows a:succ(k)X, {i,b(i). ik}:kX, a=Append({i,b(i). ik},b(k))proof

A version of set_list_append using n+1 instead of succ(n).

lemma set_list_append1:

assumes nnat and kn+1. q(k)X

defines a{k,q(k). kn+1}

shows a:n+1X, {k,q(k). kn}:nX, Init(a)={k,q(k). kn}, a=Append({k,q(k). kn},q(n)), a=Append(Init(a),q(n)), a=Append(Init(a),a(n))proof

An induction theorem for lists.

lemma list_induct:

assumes A1: b1X. P(b) and A2: bNELists(X). P(b)(xX. P(Append(b,x))) and A3: dNELists(X)

shows P(d)proof

A dual notion to Append is Prepend where we add an element to the list at the beginning of the list. We define the value of the list a prepended by an element x as x if index is 0 and a(k1) otherwise.

definition

Prepend(a,x){k,if k=0 then x else a(k1). kdomain(a)+1}

If a:nX is a list, then a with prepended xX is a list as well and its first element is x.

lemma prepend_props:

assumes nnat, a:nX, xX

shows Prepend(a,x):(n+1)X and Prepend(a,x)(0)=xproof

When prepending an element to a list the values at positive indices do not change.

lemma prepend_val:

assumes nnat, a:nX, xX, kn

shows Prepend(a,x)(k+1)=a(k)proof

The tail of a list prepended by an element is equal to the list.

lemma tail_prepend:

assumes nnat, a:nX, xX

shows Tail(Prepend(a,x))=aproof

Lists and cartesian products

Lists of length n of elements of some set X can be thought of as a model of the cartesian product Xn which is more convenient in many applications.

There is a natural bijection between the space (n+1)X of lists of length n+1 of elements of X and the cartesian product (nX)×X.

lemma lists_cart_prod:

assumes nnat

shows {x,Init(x),x(n). xsucc(n)X}bij(succ(n)X,(nX)×X)proof

We can identify a set X with lists of length one of elements of X.

lemma singleton_list_bij:

shows {x,x(0). x1X}bij(1X,X)proof

We can identify a set of X-valued lists of length with X.

lemma list_singleton_bij:

shows {x,{0,x}. xX}bij(X,1X) and {y,y(0). y1X}=converse({x,{0,x}. xX}) and {x,{0,x}. xX}=converse({y,y(0). y1X})proof

What is the inverse image of a set by the natural bijection between X-valued singleton lists and X?

lemma singleton_vimage:

assumes UX

shows {x1X. x(0)U}={{0,y}. yU}proof

A technical lemma about extending a list by values from a set.

lemma list_append_from:

assumes A1: nnat and A2: UnX and A3: VX

shows {xsucc(n)X. Init(x)Ux(n)V}=(yV. {Append(x,y). xU})proof
end