#
# Preliminary notions from naive set theory
#

[a set/sets] [an element/elements of x]
[x belongs/belong to y @ x is an element of y]
[x is in y @ x belongs to y] [x << y @ x is in y]
[the empty set] [x is empty @ x is the empty set]
[x is nonempty @ x is not empty] [{} @ the empty set]
[x is finite] [x is infinite @ x is not finite]
[x is countable]

Axiom _EmpSet.      {} is a finite set.

Axiom Empty.        Let S be a set.
    S is empty iff S has no elements.

Axiom CountNFin.    Every countable set is infinite.

Lemma CountNEmp.    No countable set is empty.


[a subset/subsets of x] [x [= y @ x is a subset of y]

Axiom DefSubF.      Let A be a set.
    Every subset X of A is a set such that every x << X belongs to A.

Axiom DefSubB.      Let A be a set.
    Every set X such that every x << X belongs to A is a subset of A.

Lemma _SubSet.      Every subset of every set is a set.
Axiom _SubFSet.     Every subset of every finite set is finite.

Lemma SubRefl.      For every set A  A [= A.

Axiom SubASymm.     Let A,B be sets.
    If A [= B and B [= A then A = B.

Lemma SubTrans.     Let A,B,C be sets.
    If A [= B and B [= C then A [= C.

#
# adding and removing an element
#

[the cons of S and x] [S + x @ the cons of S and x]
[the diff of S and x] [S - x @ the diff of S and x]

Axiom DefCons.  Let S be a set and x belong to some set.
    S + x is a set such that (x << S + x and S [= S + x
        and every element of S + x nonequal to x is in S).

Axiom DefDiff.  Let S be a set and x belong to some set.
    S - x is a set such that (x is not in S - x and S - x [= S
        and every element of S nonequal to x is in S - x).

Lemma _ConsSet. The cons of every set and every element of every set is a set.
Lemma _DiffSet. The diff of every set and every element of every set is a set.

Lemma ConsDiff. Let S be a set and x belong to S.
    (S - x) + x = S (by DefSubB,SubASymm).
Proof.
    (S - x) + x is a set.
    Every (z << (S - x) + x) belongs to S.
    Every (z << S) belongs to (S - x) + x.
end.

Lemma DiffCons. Let S be a set and x belong to some set.
    Assume x is not in S. (S + x) - x = S (by DefSubB,SubASymm).
Proof.
    (S + x) - x is a set.
    Every (z << (S + x) - x) belongs to S.
    Every (z << S) belongs to (S + x) - x.
end.

Axiom _CConsSet. The cons of every countable set and
                    every element of every set is a countable set.

Axiom _CDiffSet. The diff of every countable set and
                    every element of every set is a countable set.

Axiom _FConsSet. The cons of every finite set and
                    every element of every set is a finite set.

Axiom _FDiffSet. The diff of every finite set and
                    every element of every set is a finite set.

#
# Preliminary notions about functions
#

[a function]
[the domain of x] [Dom f @ the domain of f]
[the range of x] [Ran f @ the range of f]
[a function from x @ a function f such that Dom f = x]
[a function from x to y @ a function f from x such that Ran f = y]
[f : x @ f is a function from x] [f : x -> y @ f is a function from x to y]

[the image of x wrt y] [f ( x ) @ the image of x wrt f]
[the prototype of x wrt y] [f [ x ] @ the prototype of x wrt f]
[the set image of x wrt y] [ f { x } @ the set image of x wrt f]
[the restriction of x onto y] [x ! y @ the restriction of x onto y]

Axiom _DomSet.      The domain of every function is a set.
Axiom _RanSet.      The range of every function is a set.
Axiom _ImgRan.      For every function M and every (x << Dom M)
                        M(x) << Ran M.

Axiom DefPtt.       Let M be a function and y << Ran M.
    M[y] is a subset of Dom M such that
        for every (x << Dom M)  x << M[y]  iff  M(x) = y.

Lemma _PttSet.      For every function M and every (x << Ran M)
                        M[x] [= Dom M.

Axiom DefSImg.      Let M be a function and X [= Dom M.
    M{X} is a subset of Ran M such that for every (y << Ran M)
        y << M{X}  iff  for some (x << X) M(x) = y.

Lemma _SImgRan.     For every function M and every (X [= Dom M)
                        M{X} [= Ran M.

Axiom DefRst.       Let M be a function and X [= Dom M.
    M ! X is a function from X to Ran M such that
        for every (x << X) (M!X)(x) = M(x).

Axiom ImgCount.     Let M be a function and S be a countable subset of Dom M.
    If for all nonequal (i,j << Dom M) M(i) != M(j) then M{S} is countable.


[the dirichlet value of F] [Dir F @ the dirichlet value of F]

Axiom Dirichlet.
    Let F be a function with the countable domain and the finite range.
    Dir F is an element of Ran F with the countable prototype wrt F.

#
# Preliminary notions about natural numbers
#

[the omega] [NAT @ the omega]
[a natural number/numbers @ an element of NAT]
[the zero] [0 @ the zero] [x is nonzero @ x != 0]
[the successor of x] [succ x @ the successor of x]

Axiom _NATSet.      NAT is a countable set.
Axiom _ZeroNum.     0 is a natural number.
Axiom _SuccNum.     The successor of every natural number is a natural number.

Axiom SuccNotZero.  For every (n << NAT) succ n != 0.

Axiom SuccEquSucc.  Let A, B be natural numbers.
                    If succ A = succ B then A = B.

Axiom NatExtra.     For every (x << NAT) x = 0
                        or x = succ y for some natural number y.

Axiom NatNSucc.     No natural number x is equal to succ x.


[x is lesseq than y] [x <= y @ x is lesseq than y]

Axiom ZeroLess.     For every (x << NAT) 0 <= x.
Axiom NoScLessZr.   For no (x << NAT) succ x <= 0.
Axiom SuccLess.     For every (x,y << NAT) x <= y  <=>  succ x <= succ y.
Axiom LessSucc.     For every (x << NAT) x <= succ x.

Axiom LessRefl.     For every (x << NAT) x <= x.
Axiom LessASymm.    For every (x,y << NAT) x <= y /\ y <= x  =>  x = y.
Axiom LessTrans.    For every (x,y,z << NAT) x <= y /\ y <= z  =>  x <= z.
Axiom LessTotal.    For every (x,y << NAT) x <= y \/ succ y <= x.

#
# Proof by induction in ForTheL are based on a predefined binary
# predicate symbol (-<-) considered to be a well-founded ordering.
# We give concrete axioms for (-<-) to simulate natural induction.
#

Axiom IH.           For every natural number x  (x -<- succ x).


[the minimal element of x] [min x @ the minimal element of x]
[the maximal element of x] [max x @ the maximal element of x]
[cdr x @ x - min x]

Axiom DefMin.       Let S be a nonempty subset of NAT.
    min S is an element of S lesseq than any element of S.

Lemma MinMin.       Let S,T be nonempty subsets of NAT.
    Assume min S << T and min T << S. Then min S = min T.
Proof.
    min S <= min T and min T <= min S.
qed.

Axiom DefMax.       Let S be a nonempty finite subset of NAT.
    max S is an element of S such that for all (x << S) x <= max S.

Lemma _MinNum.
    The minimal element of every nonempty subset of NAT is a natural number.

Lemma _MaxNum.
    The maximal element of every nonempty finite subset of NAT is a natural number.


[the cardinality of x] [|x| @ the cardinality of x]

Axiom _CardNum.  The cardinality of every finite set is a natural number.

Axiom CardEmpty. For every set S  |S| = 0 iff S is empty.

Axiom CardCons.  Let S be a finite set and x belong to some set.
                 If x does not belong to S then |S + x| = succ |S|.

Lemma CardDiff.  Let S be a finite set and x belong to S.
                 succ |S - x| = |S|.
Proof.
    x does not belong to S - x. Then |(S - x) + x| = succ |S - x|.
qed.

Axiom CardSub.   Let S be a finite set and T be a subset of S.
                 Then |T| <= |S|.

Axiom CardSubEx. Let S be a finite set and k be a natural number. Let k <= |S|.
                 Then there exists a subset T of S such that |T| = k.

#
# [x] = { 0, ..., x - 1 }
#

[the segment up to x] [[x] @ the segment up to x]

Axiom DefSeg.   For every (t << NAT) [t] is a finite subset of NAT
        such that for every natural number n (n << [t]  <=>  succ n <= t).

Lemma _SegFin.  For every (t << NAT) [t] is a finite subset of NAT.
Lemma _SegNum.  For every (t << NAT) all elements of [t] are natural numbers.

Lemma SegZero.  [0] = {}.

Lemma SegSucc.  Let n,m be natural numbers.
                m << [succ n]  <=>  m << [n] \/ m = n.
Proof.
    If m << [succ n] then succ m <= n or m = n.
    Suppose m << [n]. We have succ m <= n.
    Hence succ m <= succ n (by LessSucc,LessTrans).
qed.

Lemma SegLess.  Let n,m be natural numbers. Then m <= n  <=>  [m] [= [n].
Proof.
    If [m] [= [n] then n does not belong to [m]. Assume m <= n.
    Every element of [m] belongs to [n] (by DefSeg,LessTrans).
    Hence [m] [= [n] (by DefSubB).
qed.

Lemma FinSubSeg.    Let S be a finite subset of NAT.
                    S is a subset of [k] for some natural number k.
Proof.
    Case S is empty. Obvious.
    Case S is nonempty (by DefSubB).
        Every element of S belongs to [succ max S].
    end.
end.

Axiom CardSeg.   For every natural number k  |[k]| = k.

#
#  [ S / k ] = { T | T [= S /\ |T| = k }
#

[the selection of k from s] [ [s/k] @ the selection of k from s]

Axiom DefSel.   Let S be a set and k be a natural number.
    [S/k] is a set such that (every element of [S/k] is a
        finite subset of S of the cardinality equal to k
        and every finite subset of S of the cardinality
        equal to k belongs to [S/k]).

Lemma _SelSet.  The selection of every natural
                number from every set is a set.

Axiom _SelFSet. The selection of every natural number
                from every finite set is finite.

Axiom _SelNSet. The selection of every natural number
                from every countable set is nonempty.

Axiom _SelCSet. The selection of every nonzero natural number
                from every countable set is countable.

Lemma SelSub.   Let S,T be sets and k be a nonzero natural number.
    Assume [S/k] is a nonempty subset of [T/k].
    Then S is a subset of T (by DefSubB).
Proof.
    Choose (Q << [S/k]). Choose (y << Q).
    Let x belong to S. Show that x << T.
    Case x << Q. Obvious (by DefSel,DefSubF).
    Case not x << Q.
        Then x does not belong to Q - y (by DefSubF,DefDiff,DefSel).
        Q is a finite set. Q - y, (Q - y) + x are finite sets.
        succ |Q - y| = k. |(Q - y) + x| = k (by CardCons).
        (Q - y) + x is a subset of S (by DefSubB).
        Indeed every z << (Q - y) + x belongs to S. end.
        Then (Q - y) + x belongs to [T/k] (by DefSel,DefSubF).
        Hence the thesis.
    end. end.
qed.

Axiom SelExtra. Let S be a set.
    For every natural number k for every finite subset X of [S/k]
        there exists a finite T [= S such that X [= [T/k].


#
# Infinite Ramsey Theorem
#

Theorem RamseyInf. Let T be a finite set.
    For every (K << NAT) and every countable (S [= NAT) and every (c : [S/K] -> T)
        there exists an element u of T and a countable X [= S such that
            for every (Q << [X/K]) c(Q) = u.
Proof by induction.

    Let K be a natural number.
    Let S be a countable subset of NAT.
    Let c : [S/K] -> T.

    Case K = 0 (by SubRefl).
        The empty set belongs to [S/0].
        For all (Q << [S/0]) c(Q) = c({}).
    end.

    Case K != 0 (by _).
        Take a natural number k so that K = succ k.
#
#  Here we define the structures we are going to use.
#  So far we cannot verify correctness of complex function
#  definitions, so we turn verification off for this block.
#
[none]
        Choose (C : NAT) and (N : NAT).

        For every (i << NAT)  C(i) : [cdr N(i) / k] -> T
            and for every Q << [cdr N(i) / k]
                C(i)(Q) = c(Q + min N(i)).

        N(0) = S and for every (i << NAT)
            N(succ i) is a countable subset of cdr N(i)
                such that for some element u of T
                    for every (Q << [N(succ i)/k])  C(i)(Q) = u.
[full]
#
# Here we prove some obvious properties of the above-defined structures
#
        For every (i << NAT) N(i) is a nonempty subset of NAT.
        proof by induction.
            Let i be a natural number.
            Let m be a natural number so that i = succ m.
            cdr N(m) [= N(m) (by IH,DefDiff).
            N(i) [= NAT (by IH,SubTrans).
        end.

        For every (i,j << NAT) if j <= i then N(i) [= N(j).
        proof by induction.
            Let i,j be natural numbers so that j <= i.
            Let m be a natural number so that i = succ m.
            Case i <= j. Obvious.
            Case j <= m.
                Then N(m) [= N(j).
                cdr N(m) [= N(m) (by DefDiff).
                N(i) [= N(j) (by SubTrans).
            end.
        end.

        For every (i,j << NAT) if i != j then min N(i) != min N(j).
        proof.
            Let i,j be natural numbers such that succ j <= i.
            Then N(i) [= cdr N(j) (by SubTrans).
            min N(j) does not belong to cdr N(j) (by DefDiff).
            Then min N(j) is not in N(i) (by DefSubF).
            Then min N(i) != min N(j).
        end.
#
# Now, the main sequence N(i) being introduced and investigated,
# we construct a countable subset of S which we need for the step.
#
[none]
        Choose (e : NAT -> NAT) and (d : NAT -> T).

        For every (i << NAT) e(i) = min N(i).

        For every (i << NAT) and some (Q << [N(succ i)/k])
            d(i) = c(Q + min N(i)).
[full]
#
# And here the proof of the theorem begins
#
        Let O = e{d[Dir d]}.

        O is countable and Dir d << T (by ImgCount,Dirichlet).

        Let us prove that O [= S.
            Let x belong to O.
            For some natural number n (x = e(n)) (by DefSImg,DefSubF).
            Then x << S.
        end.

        Let Q << [O/succ k].
        Show c(Q) = Dir d.

            Q is a nonempty subset of NAT.
            min Q belongs to Q. min Q is a natural number in O.
            cdr Q is a subset of Q. cdr Q is a subset of O.
            |cdr Q| = k (by DefSel,CardDiff,SuccEquSucc).
            Then cdr Q belongs to [O/k] (by DefSel).

            Choose (n << d[Dir d]) such that min Q = e(n) (by DefSImg).
            n is a natural number. d(n) = Dir d (by DefPtt).

            cdr Q [= N(succ n) (by DefSubB).
            proof.
                Let x belong to cdr Q.
                Then x is a natural number in O (by DefSel,SubTrans,DefSubF).
                Choose a natural number m such that x = e(m) (by DefSImg,DefSubF).
                x = min N(m). Show by case analysis that x << N(succ n).
                Case N(m) [= N(succ n). Obvious (by DefMin,DefSubF).
                Case N(n) [= N(m).
                    min Q << N(m) (by DefMin,DefSubF).
                    min N(m) << Q. Then min Q = min N(m) (by MinMin).
                    Hence min Q << cdr Q and we have a contradiction.
                end. end.
            end.

            Then c(Q) = d(n) (by ConsDiff).
            proof.
                cdr N(n) [= N(n).
                cdr N(n) [= NAT (by SubTrans).

                cdr Q [= cdr N(n) (by SubTrans).
                cdr Q << [cdr N(n) / k].
                Then c(cdr Q + min N(n)) = C(n)(cdr Q) (by _).

                Let P << [(N(succ n))/k].
                P [= cdr N(n) (by DefSel,SubTrans).
                P << [cdr N(n) / k].
                Then c(P + min N(n)) = C(n)(P) (by _).

                cdr Q << [(N(succ n))/k].
                Then C(n)(cdr Q) = C(n)(P) (by _).
            end.
        end.
    end.
qed.


[a hypergraph/hypergraphs] [a subgraph/subgraphs of x]
[the vertex set of x] [the hyperedge set of x]
[a vertex/vertice of x @ an element of the vertex set of x]
[an edge/edges of x @ an element of the hyperedge set of x]
[x is monochromatic in h wrt y] [x is chromatic for h]
[Ver H @ the vertex set of H] [Edg H @ the hyperedge set of H]

Axiom _VSSet. The vertex set of every hypergraph is a set.
Axiom _ESSet. The hyperedge set of every hypergraph is a set.
Axiom _ESet.  Every edge of every hypergraph H is a subset of Ver H.

Definition DefMono.
    Let H be a hypergraph and E be an edge of H.
    Let L : Ver H -> T for some set T.
    E is monochromatic in H wrt L iff there exists (c << Ran L)
        such that for every (x << E)  L(x) = c.

Definition DefChromC.
    Let H be a hypergraph and L : Ver H -> T for some finite set T.
    L is chromatic for H iff H has no edge that is monochromatic in H wrt L.

Definition DefChromT.
    Let H be a hypergraph and T be a finite set.
    T is chromatic for H iff there exists L : Ver H -> T chromatic for H.

Definition DefSubGr.
    Let H be a hypergraph.
    A subgraph of H is a hypergraph J such that
        Ver J [= Ver H and Edg J [= Edg H.

Lemma _SubgrGr.
    Every subgraph of every hypergraph is a hypergraph.

Definition DefFinGr.
    Let H be a hypergraph.
    H is finite iff Ver H is finite.

Definition DefRstGr.
    Let H be a hypergraph and V [= Ver H.
    H ! V is a hypergraph J such that
        Ver J = V and for every edge E of H
            E is an edge of J  iff  E [= V.

Axiom GraphEx.
    Let V be a set and E be a set such that
        every element of E is a subset of V.
    There exists a hypergraph H such that Ver H = V and Edg H = E.

#
# Compactness Principle used to prove Finite Ramsey Theorem
#

Theorem Compactness.
    Assume H is a hypergraph and Ver H = NAT.
    Assume every edge of H is finite.
    Let T be a finite set chromatic for every finite subgraph of H.
    Then T is chromatic for H (by DefChromT).
Proof.
[none]
    Choose (c : NAT), (m : NAT), (C : Ver H -> T), (N : NAT).

    For every (i << NAT) c(i) : Ver(H![i]) -> T
        and c(i) is chromatic for H![i].

    For every (i << NAT) m(i) : N(i) -> T
        and for every j << Dom m(i)  m(i)(j) = c(j)(i).

    For every (i << NAT) C(i) = Dir(m(i)).

    N(0) = NAT and for every i << NAT  N(succ i) = m(i)[C(i)] - i.
[full]

    For every (i << NAT) N(succ i) [= N(i) (by SubTrans).
    Indeed for all (i << NAT) N(succ i) [= m(i)[C(i)] (by DefDiff). end.

    For every (i << NAT) N(i) is a countable subset of NAT.
    proof by induction.
        Let i be a natural number.
        Let k be a natural number such that i = succ k.
        m(k)[C(k)] is a countable subset of N(k) (by IH,Dirichlet).
        N(i) is a countable subset of m(k)[C(k)].
        Hence the thesis (by IH,SubTrans).
    end.

    For every (i << NAT) and every (j << N(i)) i <= j.
    proof by induction.
        Let i be a natural number and j belong to N(i).
        Let k be a natural number such that i = succ k.
        j is a natural number in N(k) (by DefSubF).
        Then k <= j. j != k (by DefDiff).
        Hence i <= j (by LessASymm,LessTotal).
    end.

    For every (i << NAT) and every (j << N(i))
        and every (x << [i])  c(j)(x) = C(x).
    proof by induction.
        Let i be a natural number and j << N(i) and x << [i].
        Let k be a natural number such that i = succ k.
        Case x << [k]. Obvious (by IH,DefSubF).
        Case x = k.
            j belongs to m(k)[C(k)] (by DefDiff,DefSubF).
            c(j)(x) = C(x) (by DefSubF,DefPtt).
        end.
    end.

    Then C is chromatic for H (by DefChromC).
    proof.
        Let E be an edge of H.
        Choose a natural number k such that E [= [k].
        Choose a natural number t that belongs to N(k).
        [k] [= [t]. Then E [= [t] (by SubTrans).

        Let J be H![t] and L = c(t).
        J is a hypergraph and E << Edg J (by DefRstGr).
        L : Ver J -> T and L is chromatic for J.
        Then E is not monochromatic in J wrt L.

        For every x << E  L(x) = C(x) (by DefSubF).
        Then E is not monochromatic in H wrt C (by DefMono).
    end.
end.

[none]
Theorem CompactnessC.
    Assume H is a hypergraph and Ver H is countable.
    Assume every edge of H is finite.
    Let T be a finite set chromatic for every finite subgraph of H.
    Then T is chromatic for H.
[full]


#
# Finite Ramsey Theorem
#

Theorem RamseyFin. Let T be a finite set. Let k,l be natural numbers.
    Then there exists a natural number n such that for every (c : [[n]/k] -> T)
        there exists an element u of T and an element X of [[n]/l]
            such that for every (Q << [X/k]) c(Q) = u.
Proof.
[none]
    Choose a set Z such that
        (for every (X << [NAT/l]) [X/k] belongs to Z)
        and (for every (E << Z) E = [X/k] for some (X << [NAT/l])).
[full]

    Every element of Z is a subset of [NAT/k] (by DefSubB,DefSel).
    proof.
        Let E be in Z. E = [X/k] for some (X << [NAT/l]).
        Let Q belong to E. Q is a subset of NAT.
        Hence Q belongs to [NAT/k].
    end.

    Then choose a hypergraph H such that
        Ver H = [NAT/k] and Edg H = Z (by GraphEx).

    NAT [= NAT.

    T is not chromatic for H (by DefChromT).
    proof.
        Let c : [NAT/k] -> T.
        Choose an element u of T and a countable X [= NAT
            such that for every (Q << [X/k]) c(Q) = u (by RamseyInf).
        Choose P << [X/l].
        P is a subset of X. P is a subset of NAT.
        [P/k] is an edge of H (by DefSubF,DefSel).
        [P/k] is monochromatic in H wrt c (by DefMono).
        proof.
            Let Q << [P/k]. Q is a subset of X.
            Then Q << [X/k]. Hence c(Q) = u.
        end.
        Hence c is not chromatic for H (by DefChromC).
    end.

    Case k = 0 (by _).
        [l] belongs to [[l]/l]. {} belongs to [[l]/0].
        Let c : [[l]/0] -> T. Let Q belong to [[l]/0].
        Then c(Q) = c({}) (by DefSel,CardEmpty).
    end.

    Case k != 0 and H has empty edges (by _).
        Choose an empty edge E of H.
        Choose (Y << [NAT/l]) such that E = [Y/k].
        Y is a finite subset of NAT and |Y| = l.
        Let us prove that succ l <= k.
            Assume the contrary. Then k <= l.
            Then take (Q [= Y) such that |Q| = k (by CardSubEx).
            Then Q << [Y/k] (by DefSel).
            We have a contradiction.
        end.
        Then l <= k (by LessSucc,LessTrans).
        |[l]| = l. [l] belongs to [[k]/l].
        Let us show that [[l]/k] is empty.
            Assume the contrary.
            Take Q << [[l]/k].
            Q [= [l] and |Q| = k.
            Then k <= l (by CardSub,DefSel).
            We have a contradiction.
        end.
        [k] belongs to [[k]/k].
        Let c : [[k]/k] -> T.
        For every (Q << [[l]/k]) c(Q) = c([k]).
    end.

    Case k != 0 and no edge of H is empty (by _).
        Ver H is a countable set. Every edge of H is finite.
        Then take a finite subgraph G of H
            such that T is not chromatic for G (by CompactnessC).

        Ver G is a finite subset of [NAT/k].
        Choose a finite S [= NAT such that  Ver G [= [S/k] (by SelExtra).
        Choose a natural number n such that S [= [n].
        Show Ver G [= [[n]/k] (by DefSubB).
            Let Q << Ver G.
            Q << [S/k] (by DefSubF,DefSel).
            Q [= [n] (by DefSel,SubTrans).
            Then Q << [[n]/k].
        end.

        Let c : [[n]/k] -> T.
        (c!Ver G) is not chromatic for G (by DefRst,DefChromT).
        Choose an edge E of G such that
            E is monochromatic in G wrt (c!Ver G) (by DefRst,DefChromC).

        Choose (X << [NAT/l]) such that E = [X/k] (by DefSubF,DefSubGr).
        [X/k] is a subset of [[n]/k]. Then X << [[n]/l] (by DefSel,SelSub).
        For every (Q << E) c(Q) = (c!Ver G)(Q) (by DefRst,DefSubF).
        For some (u << T) and every (Q << E) c(Q) = u (by DefMono,DefRst).
    end.
qed.
