Content uploaded by Herman Geuvers

Author content

All content in this area was uploaded by Herman Geuvers on Nov 07, 2017

Content may be subject to copyright.

Higher Inductive Types in Programming

Henning Basold

(Radboud University and CWI Amsterdam, The Netherlands

h.basold@cs.ru.nl)

Herman Geuvers

(Radboud University and Technical University Eindhoven, The Netherlands

herman@cs.ru.nl)

Niels van der Weide

(Radboud University, The Netherlands

nweide@cs.ru.nl)

Abstract: We propose general rules for higher inductive types with non-dependent

and dependent elimination rules. These can be used to give a formal treatment of data

types with laws as has been discussed by David Turner in his earliest papers on Mi-

randa [Turner(1985)]. The non-dependent elimination scheme is particularly useful for

deﬁning functions by recursion and pattern matching, while the dependent elimina-

tion scheme gives an induction proof principle. We have rules for non-recursive higher

inductive types, like the integers, but also for recursive higher inductive types like

the truncation. In the present paper we only allow path constructors (so there are no

higher path constructors), which is suﬃcient for treating various interesting examples

from functional programming, as we will brieﬂy show in the paper: arithmetic modulo,

integers and ﬁnite sets.

Key Words: Functional Programming, Homotopy Type Theory, Higher Inductive

Types

Category: D.3.1, F.4.m

1 Introduction

Already in the early days of programming it has been observed that type systems

can help to ensure certain basic correctness properties of programs. For example,

type systems can prevent the confusion of an integer value for a string value

inside a memory cell. Much research and literature has since been devoted to

type systems that allow more and more properties of programs to be checked,

while retaining decidability of type checking, see [Pierce(2002), Pierce(2004)].

The very idea of using types to ensure some basic correctness properties

stems from the realm of logic, namely from the monumental project of Russell

and Whitehead [Whitehead and Russell(1912)] to ﬁnd a logical foundation of

mathematics. Since then, type systems had not been very successful in logic

until Martin-L¨of proposed a type system, now called Martin-L¨of type theory

(MLTT), that gives a computational reading to intuitionistic higher-order logic

Journal of Universal Computer Science, vol. 23, no. 1 (2017), 63-88

submitted: 4/8/16, accepted: 3/12/16, appeared: 28/1/17 © J.UCS

[Martin-L¨of(1975), Martin-L¨of(1982), Nordstr¨om et al.(1990)] based on Russell’s

theory of types [Russell(1996)]. This turned type systems from tools to merely

ensure correctness properties into ﬁrst-class logics.

The main idea underlying MLTT is that terms (i.e., programs) can be used

inside types, we say that MLTT has dependent types. For example, given two

terms s, t,onecanformatypes=t. Its inhabitants, that is terms of type s=t,

should be thought of as proofs for the identity of sand t. It was then also realized

that dependent types can be used to give even stronger correctness speciﬁcations

of programs. For instance, suppose we can form for a type Aand natural number

natypeVecAn, the elements of which are lists over Aof length n.Thistype

allows us, for instance, to write a safe function head: Vec A(n+1) →Athat

returns the ﬁrst element of a given list. Hence, dependent types allow us to

establish statically veriﬁable invariants based on runtime data.

Invariants as the one described above are very useful, but we often want to

express more sophisticated invariants through types. An example is the type

Fin(A) of ﬁnite subsets of a given type A. Finite sets are generated by the

empty set, the singleton sets and the union of two sets together with a bunch

of equations for these operations. For instance, the empty set should be neutral

with respect to the union: ∅∪X=X=X∪∅. In many programming languages

this would be implemented by using lists over Aas underlying type and exposing

Fin(A) through the three mentioned operations as interface. The implementation

of these operations then needs to maintain some invariants of the underlying

lists, such that the desired equations hold. If these equations are used to prove

correctness properties of programs, then the programmer needs to prove that

the interface indeed preserves the invariants. This is a laborious task and is thus

very often not carried out. So we may ask to what extent data types can be

speciﬁed by an interface and invariants.

A possible extension of type systems to deal with this are quotient types.

These are available in a few functional programming languages, for example

Miranda [Turner(1985)], where they are called algebraic data types with associ-

ated laws [Thompson(1986), Thompson(1990)]. On the other hand, in the proof

assistant NuPRL quotient types are implemented using squash types and non-

determinism [Nogin(2002)]. In dependent types they have been introduced in a

limited form in [Barthe and Geuvers(1995)], where they are called congruence

types, and in [Hofmann(1995)]. Quotient types are fairly easy to use but have

a major drawback: quotients of types whose elements are inﬁnite, like general

function spaces, often require some form of the axiom of choice, see for ex-

ample [Chapman et al.(2015)]. Moreover, quotient types detach the equational

speciﬁcation of a data type from its interface, thus making their speciﬁcation

harder to read. This is because the type and its equality are deﬁned separately.

Both problems can be ﬁxed through by using of higher inductive types

64 Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

[Lumsdaine and Shulman(2012), Lumsdaine(2011), Sojakova(2015)], and some

examples are given in [The Univalent Foundations Program(2013)]. A limited

form of higher inductive types has been proposed in [Altenkirch et al.(2016)]

where they are deﬁned using dialgebras. These are inductively constructed types,

but unlike inductive types, one can also specify propositional equalities in their

deﬁnition. So, there are not just term constructors, but also path constructors,

and the elimination rule also depends on the constructors for the path.

In this paper, we propose a general scheme for higher inductive types (HITs)

with non-dependent and dependent elimination rules and associated computa-

tion rules. We demonstrate our scheme through the use of HITs as replacement

for quotient types in programming by studying some illustrative examples. We

begin with arithmetic on integers modulo a ﬁxed number. This example serves

as an introduction to the concept of higher inductive types, and the structures

and principles that are derived from their speciﬁcation. Next, we give several de-

scriptions of the integers and study their diﬀerences. Especially interesting here

is that the elements of two HITs can be the same but the equality of one type

can be decidable whereas that of the other is not. The last example we give are

ﬁnite subsets of a given type. We show how set comprehension for ﬁnite sets can

be deﬁned. All the examples are accompanied with proofs of some basic facts

that illustrate the proof principles coming with higher inductive types.

The rest of the paper is structured as follows. We ﬁrst give in Section 2 a

brief introduction to Martin-L¨of type theory and the language of homotopy type

theory, as far as it is necessary. Next, we introduce in Section 3 the syntax for

the higher inductive types we will use throughout the paper. This is based on the

Master’s thesis of the third author [van der Weide(2016)], which also discusses

the semantics of HITs that are not recursive in the equality constructors. In

the following sections we study the mentioned examples of modulo arithmetic

(Section 4), integers (Section 5) and ﬁnite sets (Section 6). We close with some

ﬁnal remarks and possibilities for future work in Section 7.

The results have been formalized in Coq using the homotopy type theory

library in [Bauer et al.(2016)].

2Martin-L¨of Type Theory and Homotopy Type Theory

In this section, we introduce the variant of Martin-L¨of type theory (MLTT)

[Nordstr¨om et al.(1990)] that we are going to use throughout the paper, and we

introduce homotopy type theory [The Univalent Foundations Program(2013)].

This type theory has as type constructors dependent function spaces (also known

as Π-types), dependent binary products (aka Σ-types), binary sum types (co-

products) and identity types. Later, in Section 3, we will extend the type theory

with higher inductive types, which will give us some base types like natural

numbers.

65

Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

Next, we will restate some well-known facts about MLTT and the identity

types in particular. The properties of identity types lead us naturally towards

the terminology of homotopy theory, which we discuss at the end of the section.

2.1 Martin-L¨of Type Theory

We already argued in the introduction for the usefulness of dependent type

theories, so let us now come to the technical details of how to realize such a

theory. The most diﬃcult part of deﬁning such a theory is the fact that contexts,

types, terms and computation rules have to be given simultaneously, as these

rules use each other. Thus the following rules should be taken as simultaneous

inductive deﬁnition of a calculus.

We begin by introducing a notion of context. The purpose of contexts is to

capture the term variables and their types that can be used in a type, which

makes the type theory dependent, or a term. These can be formed inductively

by the following two rules.

· Ctx

ΓCtx ΓA:Type

Γ, x :ACtx

Note that in the second rule the type Amay use variables in Γ, thus the order

of variables in a context is important. We adopt the convention to leave out the

empty context ·on the left of a turnstile, whenever we give judgments for term

or type formations.

The next step is to introduce judgments for kinds, types and terms. Here,

the judgment ΓA:Type says that Ais a well-formed type in the context Γ,

while Γt:Adenotes that tis a well-formed term of type Ain context Γ.For

kinds we only have the following judgment.

ΓCtx

ΓType :Kind

To ease readability, we adopt the following convention.

Notation 1. If we are given a type Bwith Γ, x :AB:Type and a term

Γt:A, we denote by B[t] the type in which thas been substituted for x.In

particular, we indicate that Bhas xas free variable by writing B[x].

The type formation rules for dependent function spaces, dependent binary

products and sum types, and the corresponding term formation rules are given

as follows. To avoid duplication of rules, we use to denote either Type or

66 Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

Kind.Thus,wewriteΓM:whenever Mis a type or the universe Type.

Γ, x :AM:

Γ(x:A)→M:

Γ, x :AB:Type

Γ(x:A)×B:Type

ΓA, B :Type

ΓA+B:Type

Γ, x :AM:Γ, x :At:M

Γλx.t :(x:A)→M

Γ, x :AM:Γt:(x:A)→MΓs:A

Γts:M[s]

Γt:(x:A)×B[x]

Γπ1t:A

Γt:(x:A)×B[x]

Γπ2t:B[π1t]

Γt:AΓs:B[t]

Γ(t, s):(x:A)×B[x]

j∈{1,2}Γt:Aj

Γinjt:A1+A2

Γ, z :A+BM:Γ, x :At:M[in1x]Γ, y :Bs:M[in2y]

Γ{in1x→ t;in

2y→ s}:(z:A+B)→M

If ΓA, B :Type,thenwewriteA→Band A×Binstead of (x:A)→B

and (x:A)×B, respectively.

Note that we can obtain two kinds of function spaces: A→Bfor a type

Band A→Type. The latter models families of types indexed by the type A.

Also note that the elimination rule for the sum type gives us what is called large

elimination, in the sense that we can eliminate a sum type to produce a new

type by case distinction. For instance, later we can deﬁne the unit type 1as an

inductive type and then a type family

X={in1x→ A;in

2y→ B}:1+1→Type,

such that Xtreduces to either Aor B, depending on t.

Next, identity types and their introduction and elimination terms are given

by the following rules.

ΓA:Type Γs, t :A

Γs=t:Type

Γt:A

Γreﬂ t:t=t

Γ, x :A, y :A, p :x=yY:Type Γt:(x:A)→Y[x, x, reﬂ x]

ΓJx,y,p(t):(xy:A)→(p:x=y)→Y[x, y, p]

Higher inductive types will allow us to add more constructors, besides reﬂ, to

identity types. This will, surprisingly so, not aﬀect the elimination principle

given by J.TheJ-rule does not imply uniqueness of identity proofs, so it is also

valid if there are other equality proofs. We discuss as part of the introduction to

homotopy type theory.

67

Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

To be able to evaluate computations in MLTT, we introduce a rewriting

relation −→ on terms and types [Nordstr¨om et al.(1990)]. This rewriting relation

is given on terms as the compatible closure of the following clauses.

(λx.t)s−→ t[s/x]

πk(t1,t

2)−→ tk

{in1x1→ t1;in

2x2→ t2}(inks)−→ tk[s/xk]

Jx,y,p(t)ss(reﬂ s)−→ ts

On types, the reduction relation is obtained as the compatible closure of

s−→ t

Ys−→ Yt

Let us denote the relation for reductions in either direction by ←→ :=←− ∪ −→ .

That is to say, we have s←→ tif either s−→ tor t−→ s.Moreover,we

obtain deﬁnitional equivalence, denoted by ≡, as the equivalence closure of the

rewriting relation. Since deﬁnitionally equal terms are considered to carry the

same information, we use the following conversion rule that allows us to mix

rewriting steps in types with type checking.

ΓX, Y :Type Γu:XX←→ Y

Γu:Y(1)

By repeatedly applying this rule, we can also replace X←→ Yby X≡Yin it.

Let us now establish some facts about identity types, which will prove very

useful later and are also relevant to the discussion of homotopy type theory.

First of all, we can prove that the identity is symmetric and transitive, thus an

equivalence relation. In type theoretical terms we establish that for each type

Athere are terms symmAand transA, as indicated below. We also say that the

corresponding types are inhabited.

symmA:(xy:A)→(x=y)→(y=x)

transA:(xyz :A)→(x=y)→(y=z)→(x=z)

Proof. To demonstrate a typical use of the J-rule, let us prove transitivity by

giving the corresponding term transA. We put

Y[x, y, p]:=(z:A)→(y=z)→(x=z)

t:=λx z q.q,

so t:(x:A)→(z:A)→(x=z)→(x=z), hence t:(x:A)→Y[x, x, reﬂ x].

These deﬁnitions give us then that

Jx,y,p(t):(xy:A)→(x=y)→(z:A)→(y=z)→(x=z),

thus

transA:=λx y z q.Jx,y,p(t)xyqz

is of the correct type.

68 Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

In a similar spirit, one can use the J-rule to also prove the following facts

about identity types.

Proposition 2. Let XType :and x:XY[x]:Type be types. There are

terms of the following types.

ap : (f:X→Y)→(xy:X)→x=y→fx=fy

transport : (xy:X)→x=y→Y[x]→Y[y]

The latter we abbreviate to

p∗:= transport xyp.

This allows us to deﬁne a term

apd : (f:(x:X)→Y[x]) →(xy :X)→(p:x=y)→p∗(fx)=fy.

We also can derive the following deﬁnitional equivalences for these terms

ap ftt(reﬂ t)≡reﬂ (fx)

transport tt(reﬂ t)s≡reﬂ s

apd ftt(reﬂ t)≡reﬂ (fx)

Note that the names “ap” and “apd” stand for “apply” and “dependent

apply”, respectively. Also, note that transport is Leibniz’ law.

Since the kind of equality that occurs in the type of apd appears frequently

in the following, we use the more symmetric notation

s=Y

pt:=(p∗s)=t,

where x:XY[x]isatype,x, y :X,s:Y[x], t:Y[y]andp:x=y,sothis

denotes an equality in the type Y[y].

Using this notation, apd has the following type.

apd : (f:(x:X)→Y[x]) →(xy :X)→(p:x=y)→fx=Y

pfy

We abbreviate ap fxypby ap(f,p) and apd fxypby apd(f, p).

2.2 Homotopy Type Theory

We have discussed several types now, and most of these have a clear meaning.

For example, product types should be seen as the type of pairs. For the identity

type, however, it is more complicated. An inhabitant p:a=bis supposed to be

a proof that aand bare equal.

69

Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

In homotopy type theory types Tare seen as spaces X, inhabitants x:X

are seen as points of X, and inhabitants p:a=bare seen as paths between the

points aand b. The path reﬂ ais interpreted as the constant path. For example,

the type Nis the space with points xnfor every natural number n, and the only

paths are constant paths. But we could also look at types in which there are

more paths from ato b. For example, we could look at the interval which has

two points 0 and 1 and a path seg between 0 and 1. Now there are two paths

from 0 to 0, namely the constant path, but we can also ﬁrst go from 0 to 1 via

seg and then go back.

This seems rather boring now, because the most common types in type theory

just have a trivial interpretation. They just consist of points, and we cannot ﬁnd

any non-constant path. However, one of the important features of homotopy

type theory is higher inductive types which allow us to add paths to types.

Even though new paths are added, the J-rule will still hold. For normal spaces

this is not strange: the J-rule says how every constant path is mapped which is

suﬃcient to deﬁne a map.

There are also two other features of homotopy type theory, but they do not

play a major role in this paper. These are function extensionality and univalence.

Univalence roughly says that isomorphic types are equal, and using this axiom

one can prove function extensionality.

Before studying higher inductive types in Section 3, we ﬁrst need to introduce

some preliminary facts. For given s, t, u :A,p:s=tand q:t=uwe denote

the corresponding symmetry and transitivity proofs by

p−1:=symm

Astp

p•q:=trans

Astupq.

These can be interpreted as operations on paths. The path p−1is made by

reversing p, and the path p•qis the path which starts by walking along pand

then q. Again we abbreviate apd(f,x,y,p)byapd(f,p).

It is often required in homotopy type theory to compute the map p∗more

concretely, and we shall do so as well. For a proof, we refer the reader to The-

orem 2.11.3 in [The Univalent Foundations Program(2013)]. It is expressed as a

composition of paths which is easier to determine in concrete situations.

Proposition 3. Let Aand Bbe types and f,g :A→Bbe function terms.

Furthermore, suppose that we have inhabitants a, a:Aand paths p:a=aand

q:fa=ga. There is a path of type

p∗(q) = ap(f,p)−1•q•ap(g, p),

where p∗transports along Y:=fx=gx.

70 Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

3 Higher Inductive Types

Regular inductive types are usually speciﬁed by their constructors, which then

give rise to canonical elimination principles, in the form of recursion or induction,

and the corresponding computation principles. A higher inductive type (HIT)

can additionally be equipped with path constructors for that type. The examples

discussed in this paper just require paths between points, so our syntax will be re-

stricted to this case and will not allow constructors for paths between paths. A se-

mantical justiﬁcation of this syntax has been proposed in [van der Weide(2016)].

There, the semantics is given for non-recursive higher inductive types, that is,

HITs in which the path constructors do not quantify over the HIT that is being

deﬁned.

As already mentioned, a higher inductive type Tcan have regular data con-

structors and path constructors. Data constructors can take as argument a poly-

nomial over T, which is the ﬁrst notion we introduce in this section. Afterwards,

we introduce a special kind of terms, called constructor terms, that will be al-

lowed in the path constructors. These two deﬁnitions will then allow us to give

(dependent) elimination principles and well-behaved computation rules for HITs.

The syntax of higher inductive types consists of two parts. First, we have the

standard inductive type with a number of point constructors. On top of that,

higher inductive type allow the speciﬁcation of paths between elements of that

type. Thus, we need to devise a syntax for adding path constructors between

two elements of the type at hand.

We begin by introducing polynomial type constructors that allow us to give

well-behaved constructor argument types. They ensure that a (higher) inductive

type given in our syntax is strictly positive. To ease readability in the following

deﬁnitions, we use the following notations for terms t:A→Cand s:B→D.

idA:=λx.x :A→A

t×s:=λx.(t(π1x),s(π2x)) : A×B→C×D

t+s:={in1x→ in1(tx); in

2y→ in2(sy)}:A+B→C+D

Deﬁnition 4. Let Xbe a variable. We say that Fis a polynomial (type con-

structor) if it is given by the following grammar.

F, G ::=A:Type |X|F×G|F+G

For a ty p e B,wedenotebyF[B] the type that is obtained by substituting B

for the variable Xand interpreting ×and + as type constructors. Let Hbe a

polynomial and f:B→Cbe a term. We deﬁne a term H[f]:H[B]→H[C],

the action of Hon f, by induction in Has follows.

A[f]:=id

A(F×G)[f]:=F[f]×G[f]

X[f]:=f(F+G)[f]:=F[f]+G[f]

71

Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

Remark. The notion of polynomial could be generalized to that of contain-

ers [Abbott et al.(2005)] or in the sense of [Gambino and Kock(2013)]. However,

we stick to the above simple deﬁnition to make the development, especially the

lifting to type families, more accessible.

To give the dependent elimination principle for higher inductive types, we

need to be able to lift polynomials to type families (predicates) and maps between

them. This is provided by the following deﬁnition.

Deﬁnition 5. Suppose Fis a polynomial type constructor. We deﬁne a lifting

of Fto type families as follows. Let U:B→Type be a type family, then we

can deﬁne F(U):F[B]→Type by induction:

A(U):=λx. A (F×G)(U):=λx. F(U)(π1x)×G(U)(π2x)

X(U):=U(F+G)(U):={in1x→ F(U)x;in

2y→ G(U)y}

Moreover, given a term f:(b:B)→Ub→Vbwe deﬁne another term

H(f):(b:H[B]) →H(U)b→H(V)bagain by induction in H:

A(f):=λb. idA(F×G)(f):=λb. F(f)(π1b)×G(f)(π2b)

X(f):=f(F+G)(f):={in1x→ F(f)x;in

2y→ G(f)y}

A special case that we will use frequently is the choice U=1, which allows us

to obtain H(f):(b:H[B]) →H(V)bfrom f:(b:B)→Vb.

The correctness of this deﬁnition, that is, the typings announced in Deﬁni-

tion 5 are valid, is proved by induction in the polynomial H[X].

Next, we give a preparatory deﬁnition for path constructors that allow us

to specify paths between two terms of the type at hand. To be able to give

type-correct computation rules, these terms must be, however, of a special form,

called constructor terms. Such constructor terms are built from a restricted

term syntax, possibly involving the data constructors and an argument for the

corresponding path constructor. We introduce constructor terms in the following

deﬁnition, for which we assume the type theory introduced in Section 2.1 to be

extended by the variable Xas base type.

Deﬁnition 6. Let kbe a positive natural number, and let H1,...,H

kbe poly-

nomials and c1:H1[X]→X,...,c

k:Hk[X]→Xbe constants. We say that r

is a constructor term (over c1,...,c

k), if there is a context Γin which no type

uses X,avariablexthat does not occur in Γ, and polynomials F[X]andG[X],

72 Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

such that x:Fr:Gcan be derived using the following rules.

t:AXdoes not occur in A

x:Ft:A x :Fx:F

x:Fr:Hi[X]

x:Fcir:X

j∈{1,2}x:Fr:G1×G2

x:Fπjr:Gj

j=1,2x:Frj:Gj

x:F(r1,r

2):G1×G2

j∈{1,2}x:Fr:Gj

x:Finjr:G1+G2

If xdoes not occur in r,wesaythatris a non-recursive constructor term.

Remark. We could have extended the type theory in Section 2.1 with constants

c1,...,c

kand use restricted terms of that theory as constructor terms. Again,

to make the following development more accessible, we stick to the explicit def-

inition given above.

We now extend MLTT with higher inductive types. To this end, we devise a

scheme, whose syntax is similar to the syntax for inductive types in Coq,that

allows us to introduce a new type with data constructors and path constructors.

For this type we then have an elimination rule in form of dependent iteration

(induction) and the corresponding computation rules. Higher inductive types

that can be introduced through this scheme are of a restricted form, in that

we only allow data and path constructors, but no constructors for higher paths.

These are suﬃcient for the present exposition.

Deﬁnition 7. Ahigher inductive type is given according to the following scheme.

Inductive T(B1:Type)...(B:Type):=

|c1:H1[TB

1···B]→TB

1···B

...

|ck:Hk[TB

1···B]→TB

1···B

|p1:(x:A1[TB

1···B]) →t1=r1

...

|pn:(x:An[TB

1···B]) →tn=rn

Here, all Hiand Ajare polynomials that can use B1,...,B

, and all tjand rj

are constructor terms over c1,...,c

kwith x:Ajtj,r

j:T.IfXdoes not occur

in any of the Aj,thenTis called non-recursive and recursive otherwise.

We now give the rules that extend the type theory given in Section 2.1 with

higher inductive types, according to the scheme given in Deﬁnition 7.

Deﬁnition 8 (MLTT with HITs, Introduction Rules). For each instance

Tof the scheme in Deﬁnition 7, we add the following type formation rule to

73

Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

those of MLTT.

ΓB1:Type ··· ΓB:Type

ΓTB

1···B:Type

For the sake of clarity we leave the type parameters in the following out and

just write Tinstead of TB

1···B. The introduction rules for Tare given by the

following data and path constructors.

ΓCtx

Γci:Hi[T]→T

ΓCtx

Γpj:Aj[T]→tj=rj

The dependent elimination rule for higher inductive types provides the in-

duction principle: it allows to construct a term of type (x:T)→Yxfor

Y:T→Type. In the hypothesis of the elimination rule we want to assume

paths between elements of diﬀerent types: the types Y(tj)andY(rj). Concretely

we will assume paths qas follows

q:(x:A)→

t=Y

px r

where pis the path constructor of Tmeaning that p:(x:A)→t=rand

t:Yt

and r:Yr. We need to deﬁne

tby induction on ttostatethishypothesisinthe

elimination rule. This is done in the following deﬁnition.

Deﬁnition 9. Let ci:Hi[X]→Xbe constructors for Twith 1 ≤i≤kas in

Deﬁnition 7. Note that each constructor term x:Fr:Gterm immediately

givesrisetoatermx:F[T]r:G[T]. Given a type family U:T→Type and

terms Γfi:(x:Hi[T]) →Hi(U)x→U(cix) for 1 ≤i≤k, we can deﬁne

Γ, x :F[T],h

x:F(U)xr:G(U)r

by induction in ras follows.

t:=tx:=hxcir:=firr

πjr:=πjr

(r1,r

2):=(r1,r2)

injr:=r

It is straightforward to show that this deﬁnition is type correct.

Lemma 10. The deﬁnition of rin Deﬁnition 9 is type correct, that is, we indeed

have Γ, x :F[T],h

x:F(U)xr:G(U)runder the there given assumptions.

We are now in the position to give the (dependent) elimination rule for higher

inductive types.

74 Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

Deﬁnition 11 (MLTT with HITs, Elimination and Computation). For

each instance Tof the scheme in Deﬁnition 7, the following dependent elimina-

tion rule is added to MLTT.

Y:T→Type

Γfi:(x:Hi[T]) →Hi(Y)x→Y(cix)(fori=1,...,k)

Γqj:(x:Aj[T]) →(hx:Aj(Y)x)→

tj=Y

(pjx)rj(for j=1,...,n)

ΓT-rec(f1,...,f

k,q

1,...,q

n):(x:T)→Yx

Note that

tjand rjin the type of qjdepend on all the fithrough Deﬁni-

tion 9. If all the fiand qjare understood from the context, we abbreviate

T-rec(f1,...,f

k,q

1,...,q

n)toT-rec.

For e very 1 ≤i≤kwe have a term computation rule for each t:Hi[T]

T-rec (cit)−→ fitHi(T-rec) t,(2)

and for every 1 ≤j≤nwe have a path computation rule for each a:Aj[T]

apd(T-rec,p

ja)−→ qjaAj(T-rec) a.(3)

This has to be understood in the sense that we extend the reduction relation

introduced in Section 2.1 with the clauses in (2) and (3), then take the compatible

closure, and allow this extended reduction relation in the conversion rule (1).

We can derive some simpliﬁcations of this deﬁnition for special cases of higher

inductive types. First of all, if a higher inductive type Tis non-recursive, then

the elimination rule in Deﬁnition 11 can be simpliﬁed to

Y:T→Type

Γfi:(x:Hi[T]) →Hi(Y)x→Y(cix)(fori=1,...,k)

Γqj:(x:Aj)→

tj=Y

pjxrj(for j=1,...,n)

ΓT-rec(f1,...,f

k,q

1,...,q

n):(x:T)→Yx

and the path computation rule becomes then

apd(T-rec,p

ja)−→ qja.

Second, if Yis also constant, that is, if there is D:Type with Yt≡Dfor all

t, then we obtain the non-dependent elimination or (primitive) recursion.

Γfi:Hi[T]→Hi[D]→D(for i=1,...,k)

Γqj:(x:Aj)→

tj=rj(for j=1,...,n)

ΓT-rec(f1,...,f

k,q

1,...,q

n):T→D

In this case, the path computation rules simpliﬁes even further to

ap(T-rec,p

ja)−→ qja.

75

Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

An important property of reduction relations in type theories is that com-

putation steps preserve types of terms (su bjec t redu ct ion ). To be able to show

subject reduction for MLTT + HIT presented here, we need the following lemma.

Lemma 12. Let Tbe a higher inductive type and T-rec an instance of Deﬁni-

tion 11. For all constructor terms x:Fr:Gand terms a:F[T]we have

G(T-rec) (r[a/x]) −→ r[a/x, F(T-rec) a/hx].

Proof. This is proved by induction in r.

Proposition 13. The computation rules in Deﬁnition 11 preserve types.

Proof. That the computation rules on terms preserve types can be seen by a

straightforward application of the typing rules on both sides of (2). For the

computation rules on paths, on the other hand, one can derive that

Γapd(T-rec,p

ja):T-rec (tj[a]) =Y

pjaT-rec (rj[a])

and

ΓqjaAj(T-rec) a:

tj[a, Aj(T-rec) a]=

Y

pjarj[a, Aj(T-rec) a].

Using F=Ajand G=X, we obtain from Lemma 12 that

tj[a, Aj(T-rec) a]−→ T-rec (tj[a]).

Thus, by the conversion rule, we ﬁnd that qjaAj(T-rec) aactually has the

same type as apd(T-rec,p

ja).

4 Modular Arithmetic

Modular arithmetic is not convenient to deﬁne using inductive types. One would

like to imitate the inductive deﬁnition of Nby means of constructors 0 for zero

and Sfor the successor. However, that will always give an inﬁnite amount of

elements. If one instead deﬁnes N/mNby taking mcopies of the type with

just one element, then the deﬁnitions will be rather artiﬁcial. This way the usual

deﬁnitions for addition, multiplication or other operations, cannot be given in

the normal way. Instead one either needs to deﬁne them by hand, or code the

N/mNin Nand make a map mod m:N→N/mN.

For higher inductive types this is diﬀerent because one is able to postulate

new identities. This way we can imitate the deﬁnition N, and then add an equality

between 0 and Sm0. However, our deﬁnition for higher inductive types does not

allow dependency on terms. We can deﬁne N/2N,N/3N, and so on, but we

cannot give a deﬁnition for (m:N)→N/mN. Instead of deﬁning N/mNin

general, we thus deﬁne N/100Nwhich is not feasible to deﬁne using inductive

types. For other natural numbers we can give the same deﬁnition.

76 Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

Inductive N/100N:=

|0:N/100N

|S:N/100N→N/100N

|mod : 0 = S100 0

This is a nonrecursive higher inductive type, because the path 0 = Sn0doesnot

dependent on variables of type N/100N. The deﬁnition of N/100Ngives us the

constructors 0 : N/100N,S:N/100N→N/100Nand mod : 0 = S100 0. Further-

more, we obtain for all type families Y:(x:N/100N)→Type the following

dependent recursion principle, which we refer to as induction to emphasize the

relation to induction on natural numbers.

z:Y0s:(x:N/100N)→Yx→Y(Sx)q:

0=

Y

mod

S100 0

N/100Nind(z,s,q):(x:N/100N)→Yx

Remember

0=

Y

mod

S100 0 is deﬁned by mod∗

0=

S100 0 where we deﬁne the

transport mod∗using Proposition 2.

We note that, with this zand s,

0≡zand

S100 0≡s99 (s98 ···(s0z)···)),

where ndenotes Sn0. Finally, we have the following computation rules

N/100Nind(z,s,q)0−→ z,

N/100Nind(z,s,q)(Sx)−→ sx(N/100Nind(z, s,q)x),

apd(N/100Nind(z, s, q),mod) −→ q.

We will now demonstrate the use of the recursion principle by deﬁning addi-

tion. To do so, we will need an inhabitant of the type (n:N/100N)→n=S100 n,

which means that for every n:N/100Nwe have an equality of type n=S100 n.

This can be derived from the deﬁnition of N/100N, as we demonstrate now.

Proposition 14. There is a term gmod: (n:N/100N)→n=S100 n.

Proof. We deﬁne the type family Y:N/100N→Type by λn.n =S100 n.

To apply induction, we ﬁrst need to give an inhabitant zof type Y0whichis

0=S100 0. Since mod is of type 0 = S100 0, we can take z:=mod.

Next, we have to give a function s:(n:N)→Yn→Y(Sn), hence s

must be of type (n:N)→n=S100 n→Sn=S100 (Sn). Thus, we can take

s:=λnλq. ap(S, q).

Finally, we need to give an inhabitant of z=Y

mod

S100 0. To do so, we ﬁrst

note that there is a path

S100 0≡s99 (s98 ···(s0z)···)≡ap(S, ap(S, ···ap(s, mod) ···))

=ap(λn.S100 n, mod),

77

Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

where we used that for all f,g,p there is a path ap(f◦g, p) = ap(f,ap(g, p)).

We can now apply Proposition 3 to f:=id,g:=λn.S100 nand p:=q:=mod

to obtain a path

mod∗(mod) = ap(id,mod)−1•mod •ap(λn.S100 n, mod).

Since there is a path ap(id,mod) = mod, we thus obtain a path q

mod∗(mod) = ap(id,mod)−1•mod •ap(λn.S100 n, mod)

=mod

−1•mod •ap(λn.S100 n, mod)

=ap(λn.S100 n, mod)

=

S100 0,

so that q:z=Y

mod

S100 0, and gmod is given by N/100Nind(z,s,q).

Using this proposition and recursion on N/100N, we can deﬁne addition as

function term + : N/100N→N/100N→N/100N. The recursion principle is, as

we have shown in Section 3, a special case of induction and amounts here to

z:Ys:Y→Yq:z=s100 z

N/100N-rec(z,s,q):N/100N→Y

with computation rules

N/100N-rec(z,s,q)0−→ z,

N/100N-rec(z,s,q)(Sn)−→ s(N/100N-rec(z, s,q)n)and

ap(N/100N-rec(z, s, q),p)−→ q.

To deﬁne addition, we give for every n:N/100Na function fm, which represents

λx.x+m. So, let m:N/100Nbe arbitrary, and next we deﬁne fmusing recursion.

For the inhabitant zof type N/100Nwe take m. Next we give a function s:

N/100N→N/100Nwhich will be S. Lastly, we need to give a path between

mand S100 m, for which we can take gmod mby Proposition 14. This gives

us the desired function fm=N/100N(m, S, q m):N/100N→N/100N.Bythe

computation rules we have

fm0=m, fm(Sx)=S(fmx),ap(fm,p)=qm.

Hence, we can deﬁne + : N/100N→N/100N→N/100Nby the function

λm :N/100Nλn :N/100N.fmn.

78 Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

5 Integers

Another interesting data type, which we will study, are the integers. These can

be deﬁned as a normal inductive type, but also as a higher inductive type.

Both representations have their advantages and disadvantages. To deﬁne it as

an inductive type, we can do the same as in [Licata and Shulman(2013)]. We

ﬁrst need to deﬁne an inductive type for the positive natural numbers. This

type is called Pos and has a constructors one : Pos and S:Pos→Pos.

The inductive typed deﬁnition is the same as for the natural numbers (one

constant and one unary constructor), but we interpret it diﬀerently. For example,

for the type Pos we deﬁne addition in a diﬀerent where one +one would be Sone.

To clarify the distinction between the inductive types Nwe will sometimes write

SNfor the successor of Nand SPos for the successor of Pos. We have a function

i:Pos→Nthat reﬂects the semantics of Pos, sending one to SN0andSPos n

to SN(in). In the reverse direction we have a function j:N→Pos that reﬂects

the semantics of Pos, sending 0 and SN0tooneandSN(SNn)toSPos (j(SNn)).

Now we can deﬁne the integers. We need a constructor for zero, and we need

constructors plus and minus which turn a positive number into an integer. All

in all, we get the following deﬁnition.

Inductive Z1:=

|Z:Z1

|plus : Pos →Z1

|minus : Pos →Z1

We also have a recursion rule.

zY:YplusY:Pos→YminusY:Pos→Y

Z1-rec(z,plusY,minusY):Z1→Y

If we deﬁne the integers this way, then it is possible to deﬁne functions like

addition, and show that every number has an inverse. We can also show that

equality is decidable.

Deﬁnition 15. AtypeAis said to have decidable equality,ifthetype

(xy:A)→(x=y)+¬(x=y)

is inhabited, where as usual ¬T:=T→0and 0is the type with no constructors.

Proposition 16. The type Z1has decidable equality.

The disadvantage of this deﬁnition is that we have to redeﬁne everything

from the natural numbers to the positive numbers. Instead, one would like to

deﬁne the constructors plus and minus using natural numbers. This means that

we replace plus : Pos →Z1 by a constructor plus:N→Z2. However, if we

79

Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

deﬁne it this way, then the number 0 will be added twice. To solve this, we use

higher inductive types, because then we can add equalities as well. We use almost

the same deﬁnition, but in addition, we add an equality plus0=minus

0.

Inductive Z2:=

|plus:N→Z2

|minus:N→Z2

|zero : plus0 = minus0

Forthistypewehavetwoconstructors,namelyplus

:N→Z2andminus

:

N→Z2. We also have a recursion rule.

plus

Y:N→Yminus

Y:N→YzeroY:plus

0=minus

0

Z2-rec(plus

Y,minus

Y,zeroY):Z2→Y

The computation rules say that

Z2-rec(plus

Y,minus

Y,zeroY) (plusn)−→ plus

Yn,

Z2-rec(plus

Y,minus

Y,zeroY)(minus

n)−→ minus

Yn,

ap(Z2-rec(plus

Y,minus

Y,zeroY),zero) −→ zeroY.

Now we have two types which should represent the integers, namely Z1and

Z2. These types are related via an isomorphism.

Theorem 17. We have an isomorphism Z1Z2.

Proof. We just show how to make the map g:Z2→Z1. To make the function

g:Z2→Z1, we use the map j:N→Pos deﬁned before and the recursion

principle of the higher inductive type Z2. We need to say where plusnand

minusnare mapped to, and for that we deﬁne two functions. For the positive

integers, we deﬁne ϕ:N→Z1whichsends0toZand SNnto plus (j(SNn)).

For the negative integers we deﬁne the map ψ:N→Z1 which sends 0 to Zand

SNnto minus (j(SNn)). Finally, we need to give a path between ϕ0andψ0.

Note that by deﬁnition we have ϕ0≡Zand ψ0≡Z,andwechoosereﬂZ.So,

we deﬁne gto be the map Z2-rec(ϕ, ψ, reﬂ Z).

The deﬁnition of Z2 also has a disadvantage, and to illustrate it, we try

to deﬁne + : Z2×Z2→Z2. To do so, we use induction on both arguments.

Now we need to give a value of +(plusn, plusm) which is plus(n+m). The

case +(minusn, minusm) is easy as well, because this is just minus(n+m).

However, deﬁning +(plusn, minusm)and+(minus

n, plusm) requires more

work. We need to compare the values of nand min order to give this. In an

expression it would look like

+(plusn, minusm)=if n>m then plus(n−m) else minus(m−n),

80 Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

+(minusn, plusm)=if n>m then minus(n−m) else plus(m−n).

There is also another way to represent the integers as a higher inductive

type, which makes deﬁning addition easier. The previous data types encoded

the integers by partitioning them into positive and negative numbers. However,

we can try to imitate the deﬁnition of the natural numbers. These have two

constructors, namely 0 and the successor function S. The integers should instead

have three constructors, namely 0, the successor S, and predecessor P. On top,

we need to ensure that Sand Pare inverses, which can be achieved by using

a higher inductive type as follows. As a matter of fact, this is basically the

treatment of the integers that Turner gives in [Turner(1985)].

Inductive Z3:=

|0:Z3

|S:Z3→Z3

|P:Z3→Z3

|inv1:(x:Z3) →P(Sx)=x

|inv2:(x:Z3) →S(Px)=x

Forthistypewehavethreeconstructors0:Z3, S:Z3→Z3, and P:Z3→

Z3 for points, and we have two constructors inv1:(x:Z3) →P(Sx)=xand

inv2:(x:Z3) →S(Px)=xfor paths. We also have a recursion rule

0Y:Y

SY:Y→Y

PY:Y→Y

invY,1:(x:Y)→PY(SYx)=x

invY,2:(x:Y)→SY(PYx)=x

Z3-rec(0Y,S

Y,P

Y,invY,1,invY,2):Z3→Y

This rule is derived from dependent elimination by taking the type family Y

to be constant, see the discussion after Deﬁnition 11. We also get the following

computation rules, where we denote Z3-rec(0Y,S

Y,P

Y,invY,1,invY,2)byZ3-rec:

Z3-rec 0 −→ 0Y,Z3-rec (Sx)−→ SY(Z3-rec x),

Z3-rec (Px)−→ PY(Z3-rec x),ap(Z3-rec,inv1x)−→ invY,1(Z3-rec x),

ap(Z3-rec,inv2x)−→ invY,2(Z3-rec x).

One of the interesting features of homotopy type theory is proof relevance: not all

proofs of equality are considered to be equal. Let us look at the term P(S(P0))

to demonstrate this. There are two ways to prove this term equal to P0. We can

use that P(Sx)=x, but we can also use that S(Px)=x. Hence, we have two

paths from P(S(P0)) to P0, namely inv1(Px) and ap(P, inv2(S(P0))). Since

higher inductive types are freely generated from the points and paths, there is no

reason why these two paths would be the same. As a matter of fact, one would

expect them to be diﬀerent which is indeed the case.

Proposition 18. The paths inv1(P(S(P0))) and ap(P, inv2(S(P0))) are not

equal .

81

Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

Before we give the proof, let us start with a proof sketch. In type theory

one can prove that the empty type 0and the type 1with just one element, are

diﬀerent types. (That is, one can prove 0 1.) One can also deﬁne a type family

(n:N)→Ynsending 0 to 0and Snto 1.Thisprovesthat0andSnare not

equal. More generally, this allows us to prove that diﬀerent constructors of an

inductive type are indeed diﬀerent.

However, for path constructors we cannot copy this argument. If we make a

family of types on Z3, then the paths inv1and inv2do not get sent to types.

Hence, the induction principle cannot be used in this way to show that inv1and

inv2are diﬀerent. Instead we rely on the univalence axiom to prove this.

First we need a type for the circle. The deﬁnition can be given as a higher

inductive type.

Inductive S1:=

|base : S1

|loop : base = base

The main ingredient here is that loop and reﬂ are unequal. One can show this

by using the univalence axiom [Licata and Shulman(2013)]. To ﬁnish the proof

of Proposition 18, we deﬁne a function f:Z3→S1where the point 0 is sent

to base, the maps Sand Pare sent to the identity. Furthermore, we send the

path inv1to reﬂ and inv2to loop. Using the elimination rule, we thus deﬁne f

as Z3-rec(base,id,id,reﬂ,loop). Note that by the computation rules, fsatisﬁes

f0−→ base,f(Sx)−→ id (fx),f(Px)−→ id (fx),

ap(f,inv1)−→ reﬂ,ap(f, inv2)−→ loop .

Now we can ﬁnish the proof of Proposition 18.

Proof. Our goal is to show that inv1(P(S(P0))) and ap(P, inv2(S(P0))) are

not equal, for which it is suﬃcient to show that ap(f, inv1(P(S(P0)))) and

ap(f,ap(P, inv2(S(P0)))) are not equal. From the computation rules we get

that ap(f,inv1(P(S(P0)))) ≡reﬂ. One can prove by path induction that there

is a path from ap(f,ap(g, p)) to ap(f◦g, p) for any fand g,thusthetype

ap(f,ap(P, inv2(S(P0)))) = ap(f◦P, inv2(S(P0)))

is inhabited. Using the computation rules, we see that f◦Pis just f, and thus

ap(f◦P, inv2(S(P0))) is ap(f,inv2(S(P0))). Again we can use the computation

rules, and this time it gives that ap(f,inv2(S(P0))) ≡loop. Hence, the paths

inv1(P(S(P0))) and ap(P, inv2(S(P0))) cannot be equal, because fsends

them to reﬂ and loop respectively.

Proposition 18 might not seem very interesting at ﬁrst, but it actually has

some surprising consequences. For that we need to use Hedberg’s Theorem which

82 Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

says that in types with decidable equality there is only one proof of equality

[Hedberg(1998)].

Theorem 19 Hedberg’s Theorem. If a type Xhas decidable equality, then

we have a term

s:(xy:X)(pq:x=y)→p=q.

Using the contraposition from this theorem, we can thus immediately con-

clude that Z3 cannot have decidable equality.

Theorem 20. The type Z3does not have decidable equality.

This sounds odd at ﬁrst sight, but all it means is that we cannot decide

equality just by using the induction scheme of Z3. There are two, quite similar

ways, to deal with this. We can either weaken the notion of decidable equal-

ity or we enforce that all the HITs, that are introduced through our scheme,

are sets [The Univalent Foundations Program(2013)]. Let us start with the ﬁrst

possibility: weakening the notion of decidable equality. In (homotopy) type the-

ory, proofs of propositions are in general relevant, in the sense that we do not

just care about the existence of a proof but we are actually interested in the

witness. Recall from Proposition 18 that there are two diﬀerent proofs of equal-

ity between P(S(P0)) and P0. Thus, proof relevance prevents equality to be

decidable on Z3. However, if we reason in a proof irrelevant way by neglecting

the fact that there might be several proofs for the same equality, then we obtain

merely decidable equality. To do so, we need the so-called truncation,whichis

given by the following higher inductive type.

Inductive || || (A:Type):=

|ι:A→||A||

|p:(xy :||A||)→x=y

The truncation comes with the recursion rule

ιY:A→Yp

Y:(x, y :Y)→x=y

||A||-rec(ιY,p

Y):||A|| → Y

and computation rules

||A||-rec(ιY,p

Y)(ιx)−→ ιYx,

ap(||A||-rec(ιY,p

Y),pxy)−→ pY(|A||-rec(ιY,p

Y)x)(|A||-rec(ιY,p

Y)y).

In the truncation every element is equal, because we add for each x, y a path

pxy between them. Instead of the proposition x=y, we can now talk about

||x=y||. In the ﬁrst type there are diﬀerent proofs of equality, but in the second

every element is considered to be the same. We can solve the fact that Z3does

not have decidable equality by truncating the identity type for Z3, as in the

following theorem.

83

Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

Theorem 21. The type Z3has a merely decidable equality, that is: the following

type is inhabited:

(xy:Z3) →||x=y|| +||¬(x=y)||.

Thus, if we want to consider the proofs of identities in Z3 to be irrelevant, we

have to replace the type s=tby its truncation ||s=t|| everywhere. There are

two problems with that. First, this is very verbose, in that we need to introduce

the truncation everywhere, and maps out of Z3 with the truncated identity types

are given in terms of the recursion principles of both Z3 and the truncation.

Second, if we want to map Z3 with the truncated identity type to another type

A, then that type must also be a set, that is, also the identity types of Amay

have at most one inhabitant.

Let us now study a diﬀerent approach to solve the problem of decidability for

Z3. Since we do not consider higher inductive types with higher path constructors

in the present setting, we are morally just dealing with quotients. However, this

is not quite true, due to the fact that two paths might not be the same. For

example, Theorem 20 tells us that Z3andZ2 are not isomorphic. To obtain

actual quotients, we need to require that each HIT deﬁnable in our setting is a

set, c.f. [The Univalent Foundations Program(2013), Sec. 11.3.1]. Thus, for every

HIT Tdeﬁned by the scheme in Deﬁnition 7, we add a constructor

isSetT:(xy:T)→(pq:x=y)→p=q. (4)

Note that we would also need to extend the recursion scheme for HIT in Deﬁni-

tion 11 to account for this new constructor, since the constructor isSetTneeds

to be mapped to a corresponding term in the target type. If we add for each

HIT a constructor isSetTthough, then every type is a set and we can keep the

original recursion scheme.

Lemma 22. If for every higher inductive type Tintroduced by the scheme in

Deﬁnition 7 there is a constructor isSetTas in (4), then every type is a set.

Proof. The important property of sets is that they are preserved under the type

constructors in Section 2.1, see [The Univalent Foundations Program(2013), Sec

3.1 & Exerc. 3.2].

Forcing every HIT to be a set allows us to show that Z3 has decidable equality.

This theorem has been proved in the Coq formalization [van der Weide(2016)].

Theorem 23. If every type is a set, then Z3Z2and Z3has decidable equality.

6 Finite Sets

The last type we study here is a data type for ﬁnite sets. In functional pro-

gramming it is diﬃcult to work with ﬁnite sets. Often one represents them as

84 Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

lists on which special operations can be deﬁned. This gives some issues in the

implementation, because diﬀerent lists represent the same set and the deﬁnition

of a set-operation depends on the choice of the representative. For example, one

could remove the duplicates or not, and depending on that choice, functions on

the type will be diﬀerent.

The use of higher inductive types allows to abstract from representation

details. The diﬀerence between sets and lists is that in a list the order of the

elements and the number of occurrences of an element matter, which does not

matter for sets. Higher inductive types oﬀer the possibility to add equalities that

ignore the order of the elements and the number of occurrences. To demonstrate

this, let us start by deﬁning Fin(A) in a similar way as [Bauer(2016)].

Inductive Fin( )(A:Type):=

|∅:Fin(A)

|L:A→Fin(A)

|∪:Fin(A)×Fin(A)→Fin(A)

|assoc : (x, y, z :Fin(A)) →x∪(y∪z)=(x∪y)∪z

|neut1:(x:Fin(A)) →x∪∅=x

|neut2:(x:Fin(A)) →∅∪x=x

|com : (x, y :Fin(A)) →x∪y=y∪x

|idem : (x:A)→Lx∪Lx=Lx

Summarizing, the type of ﬁnite sets on Ais deﬁned as the free join-semilattice

on A. We abbreviate La to {a}. The constructors can be read from the deﬁnition,

but we give the recursion rule and the computation rules.

∅Y:Y

LY:A→Y

∪Y:Y×Y→Y

aY:(x, y, z :Y)→x∪Y(y∪Yz)=(x∪Yy)∪Yz

nY,1:(x:Y)→x∪Y∅Y=x

nY,2:(x:Y)→∅

Y∪Yx=x

cY:(x, y :Y)→x∪Yy=y∪Yx

iY:(x:A)→LYx∪YLYx=LYx

Fin(A)-rec(∅Y,L

y,∪Y,a

Y,n

Y,1,n

Y,2,c

Y,i

Y):Fin(A)→Y

In the following, we abbreviate Fin(A)-rec(∅Y,L

y,∪Y,a

Y,n

Y,1,n

Y,2,c

Y,i

Y)to

Fin(A)-rec. The computation rules are as follows.

Fin(A)-rec ∅−→∅

Y,Fin-rec (La)−→ LYa,

Fin-rec (x∪y)−→ ∪ Y(Fin-rec x) (Fin-rec x).

To demonstrate the possibilities of this deﬁnition, we deﬁne the comprehen-

sion and intersection of sets. We ﬁrst deﬁne “element of a set” as a relation

∈:A×Fin(A)→Bool. For this relation, we need to be able to compare el-

ements of A. This means that Amust have decidable equality, so we assume

that there is a term of type (xy :A)→x=y+¬x=y. By sending every

inhabitant of x=yto True and every inhabitant of ¬x=yto False, we get a

85

Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

function ==: A×A→Bool which decides the equality. Using this notation we

can deﬁne ∈(a, s)fora:Aand s: Fin(A).

Deﬁnition 24. Let Abe a type with decidable equality. We deﬁne the function

∈:A×Fin(A)→Bool by recursion on Fin(A) as follows.

∈(a, ∅)≡False,∈(a, {b})≡a== b,

∈(a, x ∪y)≡∈(a, x)∨∈(a, y)

In the notation of the recursion principle, given a:Awe deﬁne the function

Fin-rec : Fin(A)→Bool, where we use in the recursion scheme the auxiliary

functions ∅Bool := False, ∪Bool :=∨,andLBool :=λb.a == b.

To ﬁnish the recursion, we need to give images of the paths assoc, neut1,

neut2, com, and idem. This is not diﬃcult to do, and we demonstrate how to do

it for neut1. We need to give an inhabitant of type (x:Bool)→x∨Fal s e = x.

That term can be given by using properties of Bool, and thus the path we choose

is reﬂ. For neut2we can do the same thing, and for the images of assoc, com, and

idem we use that ∨on Bool is associative, commutative, and idempotent.

We will denote ∈(a, x)bya∈x. As seen in Deﬁnition 24, to make a map

Fin(A)→Y, we need to give images of ∅,L,and∪, and then verify some

equations. Brieﬂy said, we need to give a join semilattice Yand a map A→Y.

This way we also deﬁne the comprehension.

Deﬁnition 25. We deﬁne {| } : Fin(A)×(A→Bool)→Fin(A). Let

ϕ:A→Bool. We deﬁne {S|ϕ}: Fin(A) by recursion on S: Fin(A).

{∅ | ϕ}≡∅,{{a}|ϕ}≡if ϕa then {a}else ∅,

{x∪y|ϕ}≡{x|ϕ}∪{y|ϕ}.

Thus we use the recursion rule with ∅Y:=∅,LYa:=if ϕa then {a}else ∅,

and ∪Y:=∪. Moreover, we to check that ∪Y≡∪is associative, commutative,

has ∅Y≡∅as neutral element, and is idempotent. This is not diﬃcult to check,

because we have all these equalities from the constructors.

Using the comprehension, we can deﬁne more operators. For example, we can

deﬁne x∩yas {x|λa.a ∈y},andx\y:={x|λa.¬(a∈y)}.

7 Conclusion

We have given general rules for higher inductive types, both non-recursive and

recursive, where we have limited ourselves to higher inductive types with path

constructors. This provides a mechanism for adding data-types-with-laws to

86 Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

functional programming, as it provides a function deﬁnition principle, a proof

(by induction) principle and computation rules. This fulﬁlls at least partly the

desire set out in [Turner(1987)] to have a constructive type theory where compu-

tation rules can be added. The use of higher inductive types and their principles

was then demonstrated for typical examples that occur in functional program-

ming. Especially the case of ﬁnite sets usually requires a considerable amount of

book-keeping, which is lifted by the use of higher inductive types.

We believe that our system can be extended to include higher path construc-

tors. This requires extending the notion of constructor term and extending the

t

construction. It would be interesting to see which examples that arise naturally

in functional programming could be dealt with using higher paths. Furthermore,

it also remains to establish whether these rules are strongly normalizing, satisfy

Church-Rosser and canonicity. The current deﬁnition deﬁnes HITs in type the-

ory rather than languages like Haskell and Miranda. Hence, an open problem is

incorporating HITs in Turing complete functional programming languages.

The system we have may seem limited, because we only allow constructor

terms tand rin the types of equalities t=qfor path constructors. On the

other hand, for these constructor terms we can formulate the elimination rules

in simple canonical way, which we do not know how to do in general. Also, the

examples we have treated (and more examples we could think of) all rely on

constructor terms for path equalities, so these might be suﬃcient in practice.

Acknowledgments

We thank Ralf Hinze for the idea to link Turner’s work and higher inductive

types, and the anonymous referees for their suggestions.

References

[Abbott et al.(2005)] Abbott, M., Altenkirch, T., Ghani, N.: “Containers: Construct-

ing strictly positive types”; Theoretical Computer Science; 342 (2005), 1, 3–27.

[Altenkirch et al.(2016)] Altenkirch, T., Capriotti, P., Dijkstra, G., Forsberg, F. N.:

“Quotient inductive-inductive types”; arXiv preprint arXiv:1612.02346; (2016).

[Barthe and Geuvers(1995)] Barthe, G., Geuvers, H.: “Congruence types”; CSL; vol-

ume 1092 of Lecture Notes in Computer Science; 36–51; Springer, 1995.

[Bauer(2016)] Bauer, A.: “The Real Numbers in Homotopy Type Theory”; (2016);

computability and Complexity in Analysis, 2016.

[Bauer et al.(2016)] Bauer, A., Gross, J., Lumsdaine, P. L., Shulman, M., Sozeau, M.,

Spitters, B.: “The HoTT Library: A Formalization of Homotopy Type Theory in

Coq”; arXiv preprint arXiv:1610.04591; (2016).

[Chapman et al.(2015)] Chapman, J., Uustalu, T., Veltri, N.: “Quotienting the Delay

Monad by Weak Bisimilarity”; ICTAC; volume 9399 of LNCS; 110–125; Springer,

2015.

[Gambino and Kock(2013)] Gambino, N., Kock, J.: “Polynomial functors and polyno-

mial monads”; Math. Proc. Cambridge Phil. Soc.; 154 (2013), 01, 153–192.

87

Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...

[Hedberg(1998)] Hedberg, M.: “A Coherence Theorem for Martin-L¨of’s Type Theory”;

Journal of Functional Programming; 8 (1998), 04, 413–436.

[Hofmann(1995)] Hofmann, M.: “A simple model for quotient types”; TLCA; volume

902 of Lecture Notes in Computer Science; 216–234; Springer, 1995.

[Licata and Shulman(2013)] Licata, D. R., Shulman, M.: “Calculating the Fundamen-

tal Group of the Circle in Homotopy Type Theory”; LICS; 223–232; IEEE Com-

puter Society, 2013.

[Lumsdaine(2011)] Lumsdaine, P. L.: “Higher Inductive Types: A Tour of the

Menagerie”; (2011); post on the Homotopy Type Theory Blog.

[Lumsdaine and Shulman(2012)] Lumsdaine, P. L., Shulman, M.: “Semantics of

Higher Inductive Types”; Preprint; (2012).

[Martin-L¨of(1975)] Martin-L¨of, P.: “An Intuitionistic Theory of Types: Predicative

Part”; Studies in Logic and the Foundations of Mathematics; 80 (1975), 73–118.

[Martin-L¨of(1982)] Martin-L¨of, P.: “Constructive Mathematics and Computer Pro-

gramming”; Studies in Logic and the Foundations of Mathematics; 104 (1982),

153–175.

[Nogin(2002)] Nogin, A.: “Quotient Types: A Modular Approach”; International Con-

ference on Theorem Proving in Higher Order Logics; 263–280; Springer, 2002.

[Nordstr¨om et al.(1990)] Nordstr¨om, B., Petersson, K., Smith, J.: Programming in

Martin-L¨of ’s Type Theory, An Introduction; Oxford University Press (out of print

now available via www.cs.chalmers.se/Cs/Research/Logic), 1990.

[Pierce(2002)] Pierce, B. C.: Types and Programming Languages; The MIT Press,

2002.

[Pierce(2004)] Pierce, B. C.: Advanced Topics in Types and Programming Languages;

The MIT Press, 2004.

[Russell(1996)] Russell, B.: The Principles of Mathematics; WW Norton & Company,

1996.

[Sojakova(2015)] Sojakova, K.: “Higher Inductive Types as Homotopy-Initial Alge-

bras”; POPL; 31–42; ACM, 2015.

[The Univalent Foundations Program(2013)] The Univalent Foundations Program:

Homotopy Type Theory: Univalent Foundations of Mathematics; http://

homotopytypetheory.org/book, Institute for Advanced Study, 2013; accessed: 2

January 2017, 10:20 am.

[Thompson(1986)] Thompson, S.: “Laws in Miranda”; Proceedings of the 1986 ACM

conference on LISP and functional programming; 1–12; ACM, 1986.

[Thompson(1990)] Thompson, S.: “Lawful Functions and Program Veriﬁcation in Mi-

randa”; Science of Computer Programming; 13 (1990), 2-3, 181–218.

[Turner(1985)] Turner, D.: “Miranda: A non-strict functional language with polymor-

phic types”; J. Jouannaud, ed., Proceedings FPCA; volume 201 of LNCS; 1–16;

1985.

[Turner(1987)] Turner, D. N.: A new formulation of constructive type theory; Univer-

sity of Kent at Canterbury, Computing Laboratory, 1987.

[van der Weide(2016)] van der Weide, N.: Higher Inductive Types; Master’s thesis;

Radboud University; Nijmegen (2016).

[van der Weide(2016)] van der Weide, N.: “Implementation of the HIT Examples in

Coq”; (2016); https://github.com/nmvdw/HITs-Examples.

[Whitehead and Russell(1912)] Whitehead, A. N., Russell, B.: Principia Mathematica;

volume 2; University Press, 1912.

88 Basold H., Geuvers H., van der Weide N.: Higher Inductive Types ...