5.2 The HH and Applicative Languages
5.2.3 Syntax of HH
The syntax of HH(see5.2) is a subset of Haskell, with some minor syntactic differences. These are:
1. Type annotations are specified using single colon,:, instead of double colon ::.
2. Type annotations must be provided for top-level definitions.
3. Code blocks and definitions are always separated by a separator char- acter. In the case of data alternatives, it is |. Case blocks, let-in definitions and top-level definitions are separated by a semicolon, ;.
4. Either types are named +, and tuple types by ×. For example, a 3-tuple that contains types A, B and C is represented as A×B×C1.
HH Type System
The type system of HHis apredicative rank-1 polymorphic type system[Pie02].
Rank-1 In a polymorphic type system, some types will depend on type variables. This is specified with the ∀construct. Consider, for example, the type of the length function for lists. This type accepts lists that contain elements of any type, so the type annotation is parameterised by a type variable A.
length : ∀ A. List A → Int
Rank-1 restricts the position of the quantifiers to the outermost level of the type. In general, for any fixed k, a rank-k polymorphic type system restricts the position of the quantifiers to the left of less than k arrows. A
rank-n orhigher-rank type system is one in which quantifiers may occur in unrestricted positions.
5.2.3 Example | A Rank-2 Type. In this example, the position of∀appears to the left of one arrow, here marked with the superscript ‘∗’, →∗, so this type would not be valid in a rank-1 polymorphic type system, but it is a valid rank-2, or higher-rank type.
f : (∀ A. (A→A)→A →List A
| {z }
scope of A
) →∗ List Int
Predicative Types parameterised by type variables are calledtype schemas. In a predicative type system, type variables cannot be instantiated by type schemas.
1For presentation purposes, some symbols and keywords are “prettified” in this doc- ument. In a real implementation, the symbol ‘*’ will be used instead of ‘×’, and the keywordforallwill be used instead of the symbol ∀.
5.2. THE HHAND APPLICATIVE LANGUAGES 153
Kinds Kinds can be thought of as the types of the types. They are used to statically check that a type is well-formed.
hkindi ::= ‘Type’
| hkindi‘→’hkindi
The kind ‘Type’ is the kind of the types. Any primitive, constant type has this kind. For example, the fact that “Int is a type” can be stated as
Int:Type. Functions, products, either types have kind ‘Type’.
Type constructors have kind ‘→’. Type constructors take a kind as an argument, and produce a kind as a result. For example, the type constructor
Listhas kindType→Type. The type constructorListapplied to the type
Int is a type, List Int : Type. However, the type constructor List :
Type → Type, applied to the type constructor Tree : Type → Type is not “well kinded”. Every type in the language HH is kind-checked with kind
Type. A kind environment is used for kind-checking purposes. A well- kinded data definition data T A1· · ·An = · · · extends the environment
with T : K1 → · · ·Kn → Type, where Ki are the kinds inferred for the
typesAi in the definition of T. The kind inference rules for HH types and
data definitions are entirely standard.
Inference Rules
The set of rules that define the HH type system is given in Figure 5.3 on page 154. These rules are entirely standard for a predicative rank-1 polymorphic type system, and they have the standard soundness property. As usual, the soundness property of this type system is the formalisation of the notion that no type error can happen in run-time. In Section 5.3, these inference rules will be modified slightly so that they can be used to statically check whether a program can be parallelised according to some given parallel structure, and they can be used to systematically explore the space of all possible alternative parallel implementations for a given function in HH.
Gen Γ`M :T A6∈fv(T) Γ`M :∀A.T Spec Γ`M :∀A.T1 Γ`M :T1[T2/A] Var Γ, x:T `x:T Abs Γ, x:T1 `M :T2 Γ`λx.M :T1 →T2 App Γ`M :T1 →T2 Γ`N :T1 Γ`M N :T2 Case Γ`M :T1 Γ`ap1 →N1 :T1 →T2 · · · Γ`a pk →Nk :T1 →T2 Γ`caseM of {p1 →N1;· · · ;pk →Nk} :T2 Let Γ, x:T1 `M :T1 Γ, x:T1 `N :T2 Γ`letx=M in N :T2 Alt Γ,Γ 0 `N :T 2 Γ`p p:T1; Γ0 Γ`a p→N :T1 →T2 VPat Γ`p x:T1; [x:T1] CPat Γ`C :T11→ · · · →T1m →T2 Γ`p p1 :T11; Γ1 · · · Γ`p pm :T1m; Γm Γ`p C p1· · ·pm :T2; Γ1, . . . ,Γm
Figure 5.3: Typechecking HH Expressions.