TITLE: Representing lists and trees in System F
QUESTION [7 upvotes]: System F (also known as second-order lambda calculus or polymorphic lambda calculus) is defined as follows.
Types are defined starting from type variables $X, Y, Z, \ldots$ by means of two operations:

if U and V are types, then $U\rightarrow V$ is a type
if V  is a type, and X a type variable, then $\Pi X.V$ is a type

There are five schemes for forming terms:
variables: $x^T,y^T,z^T,\ldots$ of type T
application: tu of type V, where t is of type $U\rightarrow V$ and $u$ is of type $U$
$\lambda$-abstraction: $\lambda u.v $ of type $U\rightarrow V$ where $x^U$ is a variable of type U and v is of type V
universal abstraction: if v is a term of type V, then we can form $\Lambda X.v$ of type $\Pi X.V$, so long as the variable X is not free in the type of a free variable of v
universal application if t is a term of type $\Pi X.V$ and U is a type, then tU is a term of type V[U/X]
Now, what I ask is a simple and informal explanation (or alink to an article or book that has it) of how it is possible to encode data strutures like lists and trees using System F. I understand Church numerals, but I can't go on with more complicated structures.
Church encoding
Using untyped lambda calculus it is easier to do that, because you don't have types to worry about types.
$$\operatorname{pair} \equiv \operatorname{cons} \equiv \lambda x.\lambda y.\lambda z.z\ x\ y $$
 $$\operatorname{first} \equiv \lambda p.p\ (\lambda x.\lambda y.x) $$
 $$\operatorname{second} \equiv \lambda p.p\ (\lambda x.\lambda y.y) $$
$$\operatorname{true} \equiv \lambda a.\lambda b.a$$
$$\operatorname{nil} \equiv \operatorname{pair}\ \operatorname{true}\ \operatorname{true}$$
For example,
 $$ \operatorname{first}\ (\operatorname{pair}\ a\ b) $$
 $$ \rightarrow_\beta (\lambda p.p\ (\lambda x.\lambda y.x))\ ((\lambda x.\lambda y.\lambda z.z\ x\ y)\ a\ b) $$
 $$ \rightarrow_\beta (\lambda p.p\ (\lambda x.\lambda y.x))\ (\lambda z.z\ a\ b)  $$
 $$ \rightarrow_\beta (\lambda z.z\ a\ b)\ (\lambda x.\lambda y.x) $$
 $$ \rightarrow_\beta (\lambda x.\lambda y.x)\ a\ b = a $$
So the list (1, 2, 3) will be represented as (cons 1 (cons 2 (cons 3 nil))).
System F
In System F things get complicated, as you can see from the following definitions from page 90 of Proofs And Types by Jean-Yves Girard, Paul Taylor and Yves Lafont :
$$\operatorname{List U} = \Pi X.X\rightarrow (U\rightarrow X\rightarrow X)\rightarrow X$$
$$\operatorname{nil} = \Lambda X. \lambda x^X. \lambda y^{U\rightarrow X\rightarrow X}. x$$
$$\operatorname{cons u t } = \Lambda  X. \lambda x^X. \lambda y^{U\rightarrow X\rightarrow X}. y u (tX x y)$$
I can't understand their meaning.

REPLY [3 votes]: Pages 347-353 of the Types and Programming Languages book explain these encodings in detail. The main idea is this: "for lists, where a list with elements x, y, and z is represented as a function
that, given any function f and starting value v, calculates f x (f y (f z v)).
In OCaml terminology, a list is represented as its own fold_right function.
The type List X of lists with elements of type X is defined as follows:
List X = ∀R. (X→R→R) → R → R;

The nil value for this representation of lists is easy to write.
nil = λX. (λR. λc:X→R→R. λn:R. n) as List X;
nil : ∀X. List X

The cons and isnil operations are also easy:
cons = λX. λhd:X. λtl:List X.
       (λR. λc:X→R→R. λn:R. c hd (tl [R] c n)) as List X;
cons : ∀X. X → List X → List X

isnil = λX. λl:List X. l [Bool] (λhd:X. λtl:Bool. false) true;
isnil : ∀X. List X → Bool"