In Types and Programming Languages by Pierce, Chapter 11 is simple extensions of the simply typed lambda calculus with any simple base types. Section 11.12 introduces Lists.
How does the section allow list operations to be applied to lists with elements of any type? Specifically, my confusions are:
Does Section 11.12 introduce lists and list operations by making use of some kind of polymorphism, or type inference? (The book doesn't introduce type inference till Chapter 22 and parametric polymorphism till Chapter 23.)
Does "When we first introduced lists in §11.12, we used “custom” inference rules to allow the operations to be applied to lists with elements of any type" in Section 23.4 mean that Section 11.12 makes use of type inference to achieve similar behavior of "polymorphism"?
Does "For every type
T
, the typeList T
describes finite-length lists whose elements are drawn fromT
" in Section 11.12 mean- that
List T
is a single literal type name for all possible specific typess forT
, or - that
List T
should be instantiated differently for different specific types forT
, e.g.List Boolean
,List Nat
?
- that
Is
[T]
part of the the names of the list operators, e.g.nil[T]
,cons[T]
,isnil[T]
?Should
[T]
innil[T]
be instantiated differently for different specific types forT
, e.g.nil[Boolean]
,nil[Nat]
?Does Section 23.4 use the same type
List T
as in Section 11.12? Where isList T
defined?I think lists are not simple types but recursive types (not introduced till Chapter 20), so why does Section 11.12 cover lists?
Thanks.
11 Simple Extensions
11.12 Lists
The typing features we have seen can be classified into base types like Bool and Unit, and type constructors like → and × that build new types from old ones. Another useful type constructor is List. For every type
T
, the typeList T
describes finite-length lists whose elements are drawn fromT
.Figure 11-13 summarizes the syntax, semantics, and typing rules for lists. Except for syntactic differences (List T instead of T list, etc.) and the explicit type annotations on all the syntactic forms in our presentation, these lists are essentially identical to those found in ML and other functional languages. The empty list (with elements of type
T
) is writtennil[T]
. The list formed by adding a new elementt1
(of typeT
) to the front of a listt2
is writtencons[T] t1 t2
. The head and tail of a list t are writtenhead[T] t
andtail[T] t
. The boolean predicateisnil[T] t
yields true iff t is empty.
and
23 Universal Types
23.4 Examples
Polymorphic Lists
As an example of straightforward polymorphic programming, suppose our programming language is equipped with a type constructor List and term constructors for the usual list manipulation primitives, with the following types.
ñ nil : ∀X. List X cons : ∀X. X → List X → List X isnil : ∀X. List X → Bool head : ∀X. List X → X tail : ∀X. List X → List X
When we first introduced lists in §11.12, we used “custom” inference rules to allow the operations to be applied to lists with elements of any type. Here, we can give the operations polymorphic types expressing exactly the same constraints