## 4.2 Typed terms

As objects of type theory, terms are subjected to type discipline. The well typing of a term depends on an environment which consists in a global environment (see below) and a local context.

##### Local context.

A local context (or shortly context) is an ordered list of declarations of variables. The declaration of some variable x is either an assumption, written x:T (T is a type) or a definition, written x:=t:T. We use brackets to write contexts. A typical example is [x:T;y:=u:U;z:V]. Notice that the variables declared in a context must be distinct. If G declares some x, we write x ÎG. By writing (x:T)ÎG we mean that either x:T is an assumption in G or that there exists some t such that x:=t:T is a definition in G. If G defines some x:=t:T, we also write (x:=t:T)ÎG. Contexts must be themselves well formed. For the rest of the chapter, the notation G::(y:T) (resp G::(y:=t:T)) denotes the context G enriched with the declaration y:T (resp y:=t:T). The notation [] denotes the empty context.
A variable x is said to be free in G if G contains a declaration y:T such that x is free in T.

##### Environment.

Because we are manipulating global declarations (constants and global assumptions), we also need to consider a global environment E.
An environment is an ordered list of declarations of global names. Declarations are either assumptions or ``standard'' definitions, that is abbreviations for well-formed terms but also definitions of inductive objects. In the latter case, an object in the environment will define one or more constants (that is types and constructors, see section 4.5).
An assumption will be represented in the environment as Assum(G)(c:T) which means that c is assumed of some type T well-defined in some context G. An (ordinary) definition will be represented in the environment as Def(G)(c:=t:T) which means that c is a constant which is valid in some context G whose value is t and type is T.
The rules for inductive definitions (see section 4.5) have to be considered as assumption rules to which the following definitions apply: if the name c is declared in E, we write c Î E and if c:T or c:=t:T is declared in E, we write (c : T) Î E.

##### Typing rules.

In the following, we assume E is a valid environment wrt to inductive definitions. We define simultaneously two judgments. The first one E[G] |- t : T means the term t is well-typed and has type T in the environment E and context G. The second judgment WF(E)[G] means that the environment E is well-formed and the context G is a valid context in this environment. It also means a third property which makes sure that any constant in E was defined in an environment which is included in G 1.
A term t is well typed in an environment E iff there exists a context G and a term T such that the judgment E[G] |- t : T can be derived from the following rules.

- W-E
  WF([])[[]]
- W-S

  E[G] |- T : s sÎ S x Ï G
  WF(E)[G::(x:T)]
  E[G] |- t : T x Ï G
  WF(E)[G::(x:=t:T)]

- Def

  E[G] |- t : T c Ï EÈ G
  WF(E;Def(G)(c:=t:T))[G]

- Ax

  WF(E)[G]
  E[G] |- Prop : Type(p)
  WF(E)[G]
  E[G] |- Set : Type(q)

  WF(E)[G] i<j
  E[G] |- Type(i) : Type(j)

- Var

  WF(E)[G] (x:T)ÎG or (x:=t:T)ÎG for some t
  E[G] |- x : T

- Const

  WF(E)[G] (c:T) Î E
  E[G] |- c : T

- Prod

  E[G] |- T : s1 E[G::(x:T)] |- U : s2 s1Î{Prop, Set} or s2Î{Prop, Set}
  E[G] |- (x:T)U : s2

  E[G] |- T : Type(i) E[G::(x:T)] |- U : Type(j) i£ k j £ k
  E[G] |- (x:T)U : Type(k)

- Lam

  E[G] |- (x:T)U : s E[G::(x:T)] |- t : U
  E[G] |- [x:T]t : (x:T)U

- App

  E[G] |- t : (x:U)T E[G] |- u : U
  E[G] |- (t u) : T{x/u}

- Let

  E[G] |- t : T E[G::(x:=t:T)] |- u : U
  E[G] |- [x:=t]u : U{x/t}

Remark. We may have [x:=t]u well-typed without having ([x:T]u t) well-typed (where T is a type of t). This is because the value t associated to x may be used in a conversion rule (see section 4.3).