Cubical

The Cubical mode extends Agda with a variety of features from Cubical Type Theory. In particular, computational univalence and higher inductive types which hence gives computational meaning to Homotopy Type Theory and Univalent Foundations. The version of Cubical Type Theory that Agda implements is a variation of the CCHM Cubical Type Theory where the Kan composition operations are decomposed into homogeneous composition and generalized transport. This is what makes the general schema for higher inductive types work, following the CHM paper.

To use the cubical mode Agda needs to be run with the --cubical command-line-option or with {-# OPTIONS --cubical #-} at the top of the file. There is also a variant of the cubical mode, activated using --erased-cubical, which is described below.

The cubical mode adds the following features to Agda:

  1. An interval type and path types

  2. Generalized transport (transp)

  3. Partial elements

  4. Homogeneous composition (hcomp)

  5. Glue types

  6. Higher inductive types

  7. Cubical identity types

There is a standard agda/cubical library for Cubical Agda available at https://github.com/agda/cubical. This documentation uses the naming conventions of this library, for a detailed list of all of the built-in Cubical Agda files and primitives see Appendix: Cubical Agda primitives. The main design choices of the core part of the library are explained in https://homotopytypetheory.org/2018/12/06/cubical-agda/ (lagda rendered version: https://ice1000.org/2018/12-06-CubicalAgda.html).

The recommended way to get access to the Cubical primitives is to add the following to the top of a file (this assumes that the agda/cubical library is installed and visible to Agda).

  1. {-# OPTIONS --cubical #-}
  2. open import Cubical.Core.Everything

For detailed install instructions for agda/cubical see: https://github.com/agda/cubical/blob/master/INSTALL.md. In order to make this library visible to Agda add /path/to/cubical/cubical.agda-lib to .agda/libraries and cubical to .agda/defaults (where path/to is the absolute path to where the agda/cubical library has been installed). For details of Agda’s library management see Library Management.

Expert users who do not want to rely on agda/cubical can just add the relevant import statements at the top of their file (for details see Appendix: Cubical Agda primitives). However, for beginners it is recommended that one uses at least the core part of the agda/cubical library.

There is also an older version of the library available at https://github.com/Saizan/cubical-demo/. However this is relying on deprecated features and is not recommended to use.

The interval and path types

The key idea of Cubical Type Theory is to add an interval type I : IUniv (the reason this is in a special sort IUniv is because it doesn’t support the transp and hcomp operations). A variable i : I intuitively corresponds to a point in the real unit interval. In an empty context, there are only two values of type I: the two endpoints of the interval, i0 and i1.

  1. i0 : I
  2. i1 : I

Elements of the interval form a De Morgan algebra, with minimum (), maximum () and negation (~).

  1. __ : I I I
  2. __ : I I I
  3. ~_ : I I

All the properties of De Morgan algebras hold definitionally. The endpoints of the interval i0 and i1 are the bottom and top elements, respectively.

  1. i0 i = i
  2. i i1 = i1
  3. i j = j i
  4. i0 i = i0
  5. i1 i = i
  6. i j = j i
  7. ~ (~ i) = i
  8. i0 = ~ i1
  9. ~ (i j) = ~ i ~ j
  10. ~ (i j) = ~ i ~ j

The core idea of Homotopy Type Theory and Univalent Foundations is a correspondence between paths (as in topology) and (proof-relevant) equalities (as in Martin-Löf’s identity type). This correspondence is taken very literally in Cubical Agda where a path in a type A is represented like a function out of the interval, I → A. A path type is in fact a special case of the more general built-in heterogeneous path types:

  1. -- PathP : {ℓ} (A : I Set ℓ) A i0 A i1 Set
  2. -- Non dependent path types
  3. Path : {ℓ} (A : Set ℓ) A A Set
  4. Path A a b = PathP _ A) a b

The central notion of equality in Cubical Agda is hence heterogeneous equality (in the sense of PathOver in HoTT). To define paths we use λ-abstractions and to apply them we use regular application. For example, this is the definition of the constant path (or proof of reflexivity):

  1. refl : {ℓ} {A : Set ℓ} {x : A} Path A x x
  2. refl {x = x} = λ i x

Although they use the same syntax, a path is not exactly the same as a function. For example, the following is not valid:

  1. refl : {ℓ} {A : Set ℓ} {x : A} Path A x x
  2. refl {x = x} = λ (i : I) x

Because of the intuition that paths correspond to equality PathP (λ i → A) x y gets printed as x ≡ y when A does not mention i. By iterating the path type we can define squares, cubes, and higher cubes in Agda, making the type theory cubical. For example a square in A is built out of 4 points and 4 lines:

  1. Square : {ℓ} {A : Set ℓ} {x0 x1 y0 y1 : A}
  2. x0 x1 y0 y1 x0 y0 x1 y1 Set
  3. Square p q r s = PathP i p i q i) r s

Viewing equalities as functions out of the interval makes it possible to do a lot of equality reasoning in a very direct way:

  1. sym : {ℓ} {A : Set ℓ} {x y : A} x y y x
  2. sym p = λ i p (~ i)
  3. cong : {ℓ} {A : Set ℓ} {x y : A} {B : A Set ℓ} (f : (a : A) B a) (p : x y)
  4. PathP i B (p i)) (f x) (f y)
  5. cong f p i = f (p i)

Because of the way functions compute these satisfy some new definitional equalities compared to the standard Agda definitions:

  1. symInv : {ℓ} {A : Set ℓ} {x y : A} (p : x y) sym (sym p) p
  2. symInv p = refl
  3. congId : {ℓ} {A : Set ℓ} {x y : A} (p : x y) cong a a) p p
  4. congId p = refl
  5. congComp : {ℓ} {A B C : Set ℓ} (f : A B) (g : B C) {x y : A} (p : x y)
  6. cong a g (f a)) p cong g (cong f p)
  7. congComp f g p = refl

Path types also lets us prove new things are not provable in standard Agda, for example function extensionality (pointwise equal functions are equal) has an extremely simple proof:

  1. funExt : {ℓ} {A : Set ℓ} {B : A Set ℓ} {f g : (x : A) B x}
  2. ((x : A) f x g x) f g
  3. funExt p i x = p x i

Transport

While path types are great for reasoning about equality they don’t let us transport along paths between types or even compose paths, which in particular means that we cannot yet prove the induction principle for paths. In order to remedy this we also have a built-in (generalized) transport operation transp and homogeneous composition operations hcomp. The transport operation is generalized in the sense that it lets us specify where it is the identity function.

  1. transp : {ℓ} (A : I Set ℓ) (r : I) (a : A i0) A i1

There is an additional side condition to be satisfied for a usage of transp to type-check: A should be a constant function whenever the constraint r = i1 is satisfied. By constant here we mean that A is definitionally equal to λ _ → A i0, which in turn requires A i0 and A i1 to be definitionally equal as well.

When r is i1, transp A r will compute as the identity function.

  1. transp A i1 a = a

This is only sound if in such a case A is a trivial path, as the side condition requires.

It might seems strange that the side condition expects r and A to interact, but both of them can depend on any of the interval variables in scope, so assuming a specific value for r can affect what A looks like.

Some examples of the side condition for different values of r:

  • If r is some in-scope variable i, on which A may depend as well, then A only needs to be a constant function when substituting i1 for i.

  • If r is i0 then there is no restrition on A, since the side condition is vacuously true.

  • If r is i1 then A must be a constant function.

We can use transp to define regular transport:

  1. transport : {ℓ} {A B : Set ℓ} A B A B
  2. transport p a = transp i p i) i0 a

By combining the transport and min operations we can define the induction principle for paths:

  1. J : {ℓ} {A : Set ℓ} {x : A} (P : y x y Set ℓ)
  2. (d : P x refl) {y : A} (p : x y)
  3. P y p
  4. J P d p = transport i P (p i) j p (i j))) d

One subtle difference between paths and the propositional equality type of Agda is that the computation rule for J does not hold definitionally. If J is defined using pattern-matching as in the Agda standard library then this holds, however as the path types are not inductively defined this does not hold for the above definition of J. In particular, transport in a constant family is only the identity function up to a path which implies that the computation rule for J only holds up to a path:

  1. transportRefl : {ℓ} {A : Set ℓ} (x : A) transport refl x x
  2. transportRefl {A = A} x i = transp _ A) i x
  3. JRefl : {ℓ} {A : Set ℓ} {x : A} (P : y x y Set ℓ)
  4. (d : P x refl) J P d refl d
  5. JRefl P d = transportRefl d

Internally in Agda the transp operation computes by cases on the type, so for example for Σ-types it is computed elementwise. For path types it is however not yet possible to provide the computation rule as we need some way to remember the endpoints of the path after transporting it. Furthermore, this must work for arbitrary higher dimensional cubes (as we can iterate the path types). For this we introduce the “homogeneous composition operations” (hcomp) that generalize binary composition of paths to n-ary composition of higher dimensional cubes.

Partial elements

In order to describe the homogeneous composition operations we need to be able to write partially specified n-dimensional cubes (i.e. cubes where some faces are missing). Given an element of the interval r : I there is a predicate IsOne which represents the constraint r = i1. This comes with a proof that i1 is in fact equal to i1 called 1=1 : IsOne i1. We use Greek letters like φ or ψ when such an r should be thought of as being in the domain of IsOne.

Using this we introduce a type of partial elements called Partial φ A, this is a special version of IsOne φ → A with a more extensional judgmental equality (two elements of Partial φ A are considered equal if they represent the same subcube, so the faces of the cubes can for example be given in different order and the two elements will still be considered the same). The idea is that Partial φ A is the type of cubes in A that are only defined when IsOne φ. There is also a dependent version of this called PartialP φ A which allows A to be defined only when IsOne φ.

  1. Partial : {ℓ} I Set SSet
  2. PartialP : {ℓ} : I) Partial φ (Set ℓ) SSet

There is a new form of pattern matching that can be used to introduce partial elements:

  1. partialBool : i Partial (i ~ i) Bool
  2. partialBool i (i = i0) = true
  3. partialBool i (i = i1) = false

The term partialBool i should be thought of a boolean with different values when (i = i0) and (i = i1). Terms of type Partial φ A can also be introduced using a Pattern matching lambda.

  1. partialBool' : ∀ i → Partial (i ∨ ~ i) Bool
  2. partialBool' i = λ { (i = i0) true
  3. ; (i = i1) false }

When the cases overlap they must agree (note that the order of the cases doesn’t have to match the interval formula exactly):

  1. partialBool'' : i j Partial (~ i i (i j)) Bool
  2. partialBool'' i j = λ { (i = i1) true
  3. ; (i = i1) (j = i1) true
  4. ; (i = i0) false }

Furthermore IsOne i0 is actually absurd.

  1. empty : {A : Set} Partial i0 A
  2. empty = λ { () }

Cubical Agda also has cubical subtypes as in the CCHM type theory:

  1. _[__] : {ℓ} (A : Set ℓ) : I) (u : Partial φ A) SSet
  2. A [ φ u ] = Sub A φ u

A term v : A [ φ ↦ u ] should be thought of as a term of type A which is definitionally equal to u : A when IsOne φ is satisfied. Any term u : A can be seen as an term of A [ φ ↦ u ] which agrees with itself on φ:

  1. inS : {ℓ} {A : Set ℓ} : I} (u : A) A [ φ _ u) ]

One can also forget that a partial element agrees with u on φ:

  1. outS : {ℓ} {A : Set ℓ} : I} {u : Partial φ A} A [ φ u ] A

They satisfy the following equalities:

  1. outS (inS a) = a
  2. inS = φ} (outS = φ} a) = a
  3. outS = i1} {u} _ = u 1=1

Note that given a : A [ φ ↦ u ] and α : IsOne φ, it is not the case that outS a = u α; however, underneath the pattern binding (φ = i1), one has outS a = u 1=1.

With all of this cubical infrastructure we can now describe the hcomp operations.

Homogeneous composition

The homogeneous composition operations generalize binary composition of paths so that we can compose multiple composable cubes.

  1. hcomp : {ℓ} {A : Set ℓ} : I} (u : I Partial φ A) (u0 : A) A

When calling hcomp {φ = φ} u u0 Agda makes sure that u0 agrees with u i0 on φ. The idea is that u0 is the base and u specifies the sides of an open box. This is hence an open (higher dimensional) cube where the side opposite of u0 is missing. The hcomp operation then gives us the missing side opposite of u0. For example binary composition of paths can be written as:

  1. compPath : {ℓ} {A : Set ℓ} {x y z : A} x y y z x z
  2. compPath {x = x} p q i = hcomp j λ { (i = i0) x
  3. ; (i = i1) q j })
  4. (p i)

Pictorially we are given p : x ≡ y and q : y ≡ z, and the composite of the two paths is obtained by computing the missing lid of this open square:

  1. x z
  2. ^ ^
  3. | |
  4. x | | q j
  5. | |
  6. x ----------> y
  7. p i

In the drawing the direction i goes left-to-right and j goes bottom-to-top. As we are constructing a path from x to z along i we have i : I in the context already and we put p i as bottom. The direction j that we are doing the composition in is abstracted in the first argument to hcomp.

Note that the partial element u does not have to specify all the sides of the open box, giving more sides simply gives you more control on the result of hcomp. For example if we omit the (i = i0) → x side in the definition of compPath we still get a valid term of type A. However, that term would reduce to hcomp (\ j → \ { () }) x when i = i0 and so that definition would not build a path that starts from x.

We can also define homogeneous filling of cubes as

  1. hfill : {ℓ} {A : Set ℓ} : I}
  2. (u : i Partial φ A) (u0 : A [ φ u i0 ])
  3. (i : I) A
  4. hfill = φ} u u0 i = hcomp j λ { = i1) u (i j) 1=1
  5. ; (i = i0) outS u0 })
  6. (outS u0)

When i is i0 this is u0 and when i is i1 this is hcomp u u0. This can hence be seen as giving us the interior of an open box. In the special case of the square above hfill gives us a direct cubical proof that composing p with refl is p.

  1. compPathRefl : {ℓ} {A : Set ℓ} {x y : A} (p : x y) compPath p refl p
  2. compPathRefl {x = x} {y = y} p j i = hfill _ λ { (i = i0) x
  3. ; (i = i1) y })
  4. (inS (p i))
  5. (~ j)

Glue types

In order to be able to prove the univalence theorem we also have to add “Glue” types. These lets us turn equivalences between types into paths between types. An equivalence of types A and B is defined as a map f : A → B such that its fibers are contractible.

  1. fiber : {ℓ} {A B : Set ℓ} (f : A B) (y : B) Set
  2. fiber {A = A} f y = Σ[ x A ] f x y
  3. isContr : {ℓ} Set Set
  4. isContr A = Σ[ x A ] (∀ y x y)
  5. record isEquiv {ℓ} {A B : Set ℓ} (f : A B) : Set where
  6. field
  7. equiv-proof : (y : B) isContr (fiber f y)
  8. __ : {ℓ} (A B : Set ℓ) Set
  9. A B = Σ[ f (A B) ] (isEquiv f)

The simplest example of an equivalence is the identity function.

  1. idfun : {ℓ} (A : Set ℓ) A A
  2. idfun _ x = x
  3. idIsEquiv : {ℓ} (A : Set ℓ) isEquiv (idfun A)
  4. equiv-proof (idIsEquiv A) y =
  5. ((y , refl) , λ z i z .snd (~ i) , λ j z .snd (~ i j))
  6. idEquiv : {ℓ} (A : Set ℓ) A A
  7. idEquiv A = (idfun A , idIsEquiv A)

An important special case of equivalent types are isomorphic types (i.e. types with maps going back and forth which are mutually inverse): https://github.com/agda/cubical/blob/master/Cubical/Foundations/Isomorphism.agda.

As everything has to work up to higher dimensions the Glue types take a partial family of types that are equivalent to the base type A:

  1. Glue : {ℓ '} (A : Set ℓ) {φ : I}
  2. → Partial φ (Σ[ T ∈ Set ℓ' ] T A) Set '

These come with a constructor and eliminator:

  1. glue : {ℓ '} {A : Set ℓ} {φ : I} {Te : Partial φ (Σ[ T ∈ Set ℓ' ] T A)}
  2. PartialP φ T A Glue A Te
  3. unglue : {ℓ '} {A : Set ℓ} (φ : I) {Te : Partial φ (Σ[ T ∈ Set ℓ' ] T A)}
  4. Glue A Te A

Using Glue types we can turn an equivalence of types into a path as follows:

  1. ua : {ℓ} {A B : Set ℓ} A B A B
  2. ua {_} {A} {B} e i = Glue B { (i = i0) (A , e)
  3. ; (i = i1) (B , idEquiv B) })

The idea is that we glue A together with B when i = i0 using e and B with itself when i = i1 using the identity equivalence. This hence gives us the key part of univalence: a function for turning equivalences into paths. The other part of univalence is that this map itself is an equivalence which follows from the computation rule for ua:

  1. uaβ : {ℓ} {A B : Set ℓ} (e : A B) (x : A) transport (ua e) x e .fst x
  2. uaβ e x = transportRefl (e .fst x)

Transporting along the path that we get from applying ua to an equivalence is hence the same as applying the equivalence. This is what makes it possible to use the univalence axiom computationally in Cubical Agda: we can package up our equivalences as paths, do equality reasoning using these paths, and in the end transport along the paths in order to compute with the equivalences.

We have the following equalities:

  1. Glue A {i1} Te = Te 1=1 .fst
  2. unglue φ (glue t a) = a
  3. glue (\ { = i1) -> g}) (unglue φ g) = g
  4. unglue i1 {Te} g = Te 1=1 .snd .fst g
  5. glue = i1} t a = t 1=1

For more results about Glue types and univalence see https://github.com/agda/cubical/blob/master/Cubical/Core/Glue.agda and https://github.com/agda/cubical/blob/master/Cubical/Foundations/Univalence.agda. For some examples of what can be done with this for working with binary and unary numbers see https://github.com/agda/cubical/blob/master/Cubical/Data/BinNat/BinNat.agda.

Higher inductive types

Cubical Agda also lets us directly define higher inductive types as datatypes with path constructors. For example the circle and torus can be defined as:

  1. data S¹ : Set where
  2. base : S¹
  3. loop : base base
  4. data Torus : Set where
  5. point : Torus
  6. line1 : point point
  7. line2 : point point
  8. square : PathP i line1 i line1 i) line2 line2

Functions out of higher inductive types can then be defined using pattern-matching:

  1. t2c : Torus S¹ × S¹
  2. t2c point = (base , base)
  3. t2c (line1 i) = (loop i , base)
  4. t2c (line2 j) = (base , loop j)
  5. t2c (square i j) = (loop i , loop j)
  6. c2t : S¹ × S¹ Torus
  7. c2t (base , base) = point
  8. c2t (loop i , base) = line1 i
  9. c2t (base , loop j) = line2 j
  10. c2t (loop i , loop j) = square i j

When giving the cases for the path and square constructors we have to make sure that the function maps the boundary to the right thing. For instance the following definition does not pass Agda’s typechecker as the boundary of the last case does not match up with the expected boundary of the square constructor (as the line1 and line2 cases are mixed up).

  1. c2t_bad : S¹ × S¹ Torus
  2. c2t_bad (base , base) = point
  3. c2t_bad (loop i , base) = line2 i
  4. c2t_bad (base , loop j) = line1 j
  5. c2t_bad (loop i , loop j) = square i j

Functions defined by pattern-matching on higher inductive types compute definitionally, for all constructors.

  1. c2t-t2c : (t : Torus) c2t (t2c t) t
  2. c2t-t2c point = refl
  3. c2t-t2c (line1 _) = refl
  4. c2t-t2c (line2 _) = refl
  5. c2t-t2c (square _ _) = refl
  6. t2c-c2t : (p : S¹ × S¹) t2c (c2t p) p
  7. t2c-c2t (base , base) = refl
  8. t2c-c2t (base , loop _) = refl
  9. t2c-c2t (loop _ , base) = refl
  10. t2c-c2t (loop _ , loop _) = refl

By turning this isomorphism into an equivalence we get a direct proof that the torus is equal to two circles.

  1. TorusS¹×S¹ : Torus S¹ × S¹
  2. TorusS¹×S¹ = isoToPath (iso t2c c2t t2c-c2t c2t-t2c)

Cubical Agda also supports parameterized and recursive higher inductive types, for example propositional truncation (squash types) is defined as:

  1. data _ {ℓ} (A : Set ℓ) : Set where
  2. _ : A A
  3. squash : (x y : A ∥) x y
  4. isProp : {ℓ} Set Set
  5. isProp A = (x y : A) x y
  6. recPropTrunc : {ℓ} {A : Set ℓ} {P : Set ℓ} isProp P (A P) A P
  7. recPropTrunc Pprop f x = f x
  8. recPropTrunc Pprop f (squash x y i) =
  9. Pprop (recPropTrunc Pprop f x) (recPropTrunc Pprop f y) i

For many more examples of higher inductive types see: https://github.com/agda/cubical/tree/master/Cubical/HITs.

Indexed inductive types

Cubical Agda has experimental support for the transp primitive when used to substitute the indices of an indexed inductive type. A handful of definitions (satisfying a technical restriction on their pattern matching) will compute when applied to a transport along indices. As an example of what works, let us consider the following running example:

  1. data Eq {a} {A : Set a} (x : A) : A Set a where
  2. reflEq : Eq x x
  3. data Vec {a} (A : Set a) : Nat Set a where
  4. [] : Vec A zero
  5. __ : {n} A Vec A n Vec A (suc n)

Functions which match on Eq when all of its endpoints are variables, that is, very generic lemmas like symEq and transpEq below, will compute on all cases: they will compute to the given right-hand-side definitionally when their argument is reflEq, and will compute to a transport in the codomain when their argument has been transported in the second variable.

  1. symEq : {a} {A : Set a} {x y : A} Eq x y Eq y x
  2. symEq reflEq = reflEq
  3. transpEq : {a} {A B : Set a} Eq A B A B
  4. transpEq reflEq x = x
  5. pathToEq : {a} {A : Set a} {x y : A} x y Eq x y
  6. pathToEq {x = x} p = transp i Eq x (p i)) i0 reflEq
  7. module _ {a} {A B : Set a} {x y : A} {f : A B} where
  8. _ : symEq (reflEq {x = x}) reflEq
  9. _ = refl
  10. _ : transpEq (pathToEq (ua (idEquiv Bool))) λ x x
  11. _ = refl

Matching on indexed types in situations where types are assumed (so their transports are also open) often generates many more transports than the comparable construction with paths would. As an example, compare the proof of uaβEq below has four pending transports, whereas uaβ only has one!

  1. uaβEq : transpEq (pathToEq (ua f)) f .fst
  2. uaβEq = funExt λ z
  3. compPath (transportRefl (f .fst _))
  4. (cong (f .fst) (compPath
  5. (transportRefl _)
  6. (compPath
  7. (transportRefl _)
  8. (transportRefl _))))

In more concrete situations, such as when the indices are constructors of some other inductive type, pattern-matching definitions will not compute when applied to transports. For specific unsupported cases, see What works, and what doesn’t.

If the UnsupportedIndexedMatch warning is enabled (it is by default), Agda will print a warning for every definition whose computational behaviour could not be extended to cover transports. Internally, transports are represented by an additional constructor, and pattern-matching definitions must be extended to cover these constructors. To do this, the results of pattern-matching unification must be translated into an embedding (in the HoTT sense). This is work-in-progress.

For the day-to-day use of Cubical Agda, it is advisable to disable the UnsupportedIndexedMatch warnings. You can do this using the -WnoUnsupportedIndexedMatch option in an OPTIONS pragma or in your agda-lib file.

What works, and what doesn’t

This section lists some of the common cases where pattern-matching unification produces something that can not be extended to cover transports, and the cases in which it can.

The following pair of definitions relies on injectivity for data constructors (specifically of the constructor suc), and so will not compute on transported values.

  1. sucInjEq : {n k} Eq (suc n) (suc k) Eq n k
  2. sucInjEq reflEq = reflEq
  3. head : {n} {a} {A : Set a} Vec A (suc n) A
  4. head (x _) = x

To demonstrate the failure of computation, we can set up the following artificial example using head. By passing the vector true ∷ [] through two transports, even if they would cancel out, head’s computation gets stuck.

  1. module _ (n : Nat) (p : n 1) where private
  2. vec : Vec Bool n
  3. vec = transport i Vec Bool (p (~ i))) (true [])
  4. hd : Bool
  5. hd = head (transport i Vec Bool (p i)) vec)
  6. -- Does not type-check:
  7. -- _ : hd true
  8. -- _ = refl
  9. -- Instead, hd is some big expression involving head applied to a
  10. -- transport

If a definition is stuck on a transport, often the best workaround is to avoid treating it like the reducible expression it should be, and managing the transports yourself. For example, using the proof that transport (sym p) (transport p x) ≡ x, we can compute with hd up to a path, even if it’s definitionally stuck.

  1. -- Continuing from above..
  2. _ : hd true
  3. _ = cong head (transportTransport i Vec Bool (p (~ i))) (true []))

In other cases, it may be possible to rephrase the proof in ways that avoid unsupported cases in pattern matching, and so, compute. For example, returning to sucInj, we can define it in terms of apEq (which always computes), and the fact that suc has a partially-defined inverse:

  1. apEq : {a b} {A : Set a} {B : Set b} (f : A B) {x y : A}
  2. Eq x y Eq (f x) (f y)
  3. apEq f reflEq = reflEq
  4. sucInjEq : {n k} Eq (suc n) (suc k) Eq n k
  5. sucInjEq = apEq λ { (suc n) n ; zero zero }

Definitions which rely on principles incompatible with Cubical Agda (K, injectivity of type constructors) will never compute on transports. Note that enabling both Cubical and K is not compatible with --safe.

Absurd clauses do not need any special handling (since the transport of an absurdity is still absurd), so definitions which rely on Agda’s ability to automatically separate constructors of inductive types will not generate a UnsupportedIndexedMatch warning.

  1. zeroNotSucEq : {n} {a} {A : Set a} Eq zero (suc n) A
  2. zeroNotSucEq ()

Definitions whose elaboration involves using an equality derived from pattern-matching in a type in Setω can not be extended yet. The following example is very artificial because it minimises an example from the Cubical library. The point is that to extend test to cover transports, we would need to, given p : ℓ′ ≡ ℓ, produce a PathP (λ i → Argh ℓ (p i)) _ _, but Setω is not considered fibrant yet.

  1. data Argh (ℓ : Level) : Level Setω where
  2. argh : {ℓ′} Argh ℓ′ Argh ℓ′
  3. test : {ℓ ℓ′} Argh ℓ′ Bool
  4. test {ℓ} (argh _) = true

Modalities & indexed matching

When using indexed matching in Cubical Agda, clauses’ arguments (and their right-hand-sides) need to be transported to account for indexing, meaning that the types of those arguments must be well-formed terms.

For example, the following code is forbidden in Cubical Agda, and when --without-K is enabled:

  1. subst : (@0 P : A Set p) x y P x P y
  2. subst _ refl p = p

This is because the predicate P is erased, but internally, we have to transport along the argument p along a path involving P, in a relevant position.

Any argument which is used in the result type, or appears after a forced (dot) pattern, must have a modality-correct type.

Cubical identity types and computational HoTT/UF

As mentioned above the computation rule for J does not hold definitionally for path types. Cubical Agda solves this by introducing a cubical identity type. The https://github.com/agda/cubical/blob/master/Cubical/Core/Id.agda file exports all of the primitives for this type, including the notation _≡_ and a J eliminator that computes definitionally on refl.

The cubical identity types and path types are equivalent, so all of the results for one can be transported to the other one (using univalence). Using this we have implemented an interface to HoTT/UF which provides the user with the key primitives of Homotopy Type Theory and Univalent Foundations implemented using cubical primitives under the hood. This hence gives an axiom free version of HoTT/UF which computes properly.

  1. module Cubical.Core.HoTT-UF where
  2. open import Cubical.Core.Id public
  3. using ( __ -- The identity type.
  4. ; refl -- Its constructor.
  5. ; J -- Its eliminator (can be defined by pattern matching)
  6. ; transport -- As in the HoTT Book.
  7. ; ap
  8. ; __
  9. ; _⁻¹
  10. ; _≡⟨__ -- Standard equational reasoning.
  11. ; _
  12. ; funExt -- Function extensionality
  13. -- (can also be derived from univalence).
  14. ; Σ -- Sum type. Needed to define contractible types, equivalences
  15. ; _,_ -- and univalence.
  16. ; pr -- The eta rule is available.
  17. ; pr
  18. ; isProp -- The usual notions of proposition, contractible type, set.
  19. ; isContr
  20. ; isSet
  21. ; isEquiv -- A map with contractible fibers
  22. -- (Voevodsky's version of the notion).
  23. ; _≃_ -- The type of equivalences between two given types.
  24. ; EquivContr -- A formulation of univalence.
  25. ; ∥_∥ -- Propositional truncation.
  26. ; ∣_∣ -- Map into the propositional truncation.
  27. ; ∥∥-isProp -- A truncated type is a proposition.
  28. ; ∥∥-recursion -- Non-dependent elimination.
  29. ; ∥∥-induction -- Dependent elimination.
  30. )

In order to get access to only the HoTT/UF primitives start a file as follows:

  1. {-# OPTIONS --cubical #-}
  2. open import Cubical.Core.HoTT-UF

However, even though this interface exists, we recommend that users of cubical mode use the path types rather than the cubical identity types. Primarily, this is because many operations for path types are implemented directly, rather than by induction (e.g. ap, funExt, happly, sym, etc), and thus enjoy better computational behaviour. In addition to using J directly, it is possible to match on the reflexivity constructor, as if Id were an inductive type:

  1. symId : {ℓ} {A : Set ℓ} {x y : A} Id x y Id y x
  2. symId reflId = reflId

Cubical identity types are not inductively defined, and we may observe this using the primitives conid and primIdElim. These primitives expose underlying representation: terms of the cubical identity type can be thought of pairs consisting of a path p and a cofibration φ, such that, under the cofibration φ, the path p is the reflexivty path. These primitives are very low-level, and their use is not recommended.

  1. apId : {ℓ ℓ′} {A : Set ℓ} {B : Set ℓ′} (f : A B)
  2. {x y : A} Id x y Id (f x) (f y)
  3. apId f {x = x} = primIdElim y _ Id (f x) (f y))
  4. λ φ y w conid φ λ i f (outS w i)

Even though it is possible to define the reflexivity path using conid, the name reflId is special, in that it is treated as a “matchable” constructor, whereas conid is not. Depending on your syntax highlighting scheme, this can be observed using agda-mode: they are different colours. However, for computation, they are treated as the same:

  1. _ : {ℓ} {A : Set ℓ} {x : A} reflId conid i1 _ x)
  2. _ = refl

Cubical Agda with erased Glue

The option --erased-cubical enables a variant of Cubical Agda in which Glue (and the other builtins defined in Agda.Builtin.Cubical.Glue) must only be used in erased settings.

Regular Cubical Agda code can import code that uses --erased-cubical. Regular Cubical Agda code can also be imported from code that uses --erased-cubical, but names defined using Cubical Agda are treated as if they had been marked as erased, with an exception related to pattern matching:

  • Matching on a non-erased imported constructor does not, on its own, make Agda treat the right-hand side as erased.

The reason for this exception is that it should be possible to import the code from modules that use --cubical, in which the non-erased constructors are not treated as erased.

Note that names that are re-exported from a Cubical Agda module using open import M args public are seen as defined using Cubical Agda.

References

Cyril Cohen, Thierry Coquand, Simon Huber and Anders Mörtberg; “Cubical Type Theory: a constructive interpretation of the univalence axiom”.

Thierry Coquand, Simon Huber, Anders Mörtberg; “On Higher Inductive Types in Cubical Type Theory”.

Appendix: Cubical Agda primitives

The Cubical Agda primitives and internals are exported by a series of files found in the lib/prim/Agda/Builtin/Cubical directory of Agda. The agda/cubical library exports all of these primitives with the names used throughout this document. Experts might find it useful to know what is actually exported as there are quite a few primitives available that are not really exported by agda/cubical, so the goal of this section is to list the contents of these files. However, for regular users and beginners the agda/cubical library should be sufficient and this section can safely be ignored.

Warning: Many of the built-ins whose definitions can be written in Agda are nonetheless used internally in the implementation of cubical Agda, and using different implementations can easily lead to unsoundness. Even though they are definable in user code, this is not a supported use-case.

The key file with primitives is Agda.Primitive.Cubical. It exports the following BUILTIN, primitives and postulates:

  1. {-# BUILTIN CUBEINTERVALUNIV IUniv #-} -- IUniv : SSet₁
  2. {-# BUILTIN INTERVAL I #-} -- I : IUniv
  3. {-# BUILTIN IZERO i0 #-}
  4. {-# BUILTIN IONE i1 #-}
  5. infix 30 primINeg
  6. infixr 20 primIMin primIMax
  7. primitive
  8. primIMin : I I I -- __
  9. primIMax : I I I -- __
  10. primINeg : I I -- ~_
  11. {-# BUILTIN ISONE IsOne #-} -- IsOne : I → SSet
  12. postulate
  13. itIsOne : IsOne i1 -- 1=1
  14. IsOne1 : i j IsOne i IsOne (primIMax i j)
  15. IsOne2 : i j IsOne j IsOne (primIMax i j)
  16. {-# BUILTIN ITISONE itIsOne #-}
  17. {-# BUILTIN ISONE1 IsOne1 #-}
  18. {-# BUILTIN ISONE2 IsOne2 #-}
  19. {-# BUILTIN PARTIAL Partial #-}
  20. {-# BUILTIN PARTIALP PartialP #-}
  21. postulate
  22. isOneEmpty : {a} {A : Partial i0 (Set a)} PartialP i0 A
  23. {-# BUILTIN ISONEEMPTY isOneEmpty #-}
  24. primitive
  25. primPOr : {a} (i j : I) {A : Partial (primIMax i j) (Set a)}
  26. PartialP i z A (IsOne1 i j z)) PartialP j z A (IsOne2 i j z))
  27. PartialP (primIMax i j) A
  28. -- Computes in terms of primHComp and primTransp
  29. primComp : {a} (A : (i : I) Set (a i)) : I} (∀ i Partial φ (A i)) (a : A i0) A i1
  30. syntax primPOr p q u t = [ p u , q t ]
  31. primitive
  32. primTransp : {a} (A : (i : I) Set (a i)) : I) (a : A i0) A i1
  33. primHComp : {a} {A : Set a} : I} (∀ i Partial φ A) A A

The interval I belongs to its own sort, IUniv. Types in this sort do not support composition and transport (unlike Set), but function types from types in this sort to types in Set do (unlike SSet).

The Path types are exported by Agda.Builtin.Cubical.Path:

  1. postulate
  2. PathP : {ℓ} (A : I Set ℓ) A i0 A i1 Set
  3. {-# BUILTIN PATHP PathP #-}
  4. infix 4 __
  5. __ : {ℓ} {A : Set ℓ} A A Set
  6. __ {A = A} = PathP _ A)
  7. {-# BUILTIN PATH __ #-}

The Cubical subtypes are exported by Agda.Builtin.Cubical.Sub:

  1. {-# BUILTIN SUB Sub #-}
  2. postulate
  3. inc : {ℓ} {A : Set ℓ} {φ} (x : A) Sub A φ _ x)
  4. {-# BUILTIN SUBIN inS #-}
  5. primitive
  6. primSubOut : {ℓ} {A : Set ℓ} : I} {u : Partial φ A} Sub _ φ u A

The Glue types are exported by Agda.Builtin.Cubical.Glue:

  1. record isEquiv {ℓ '} {A : Set ℓ} {B : Set ℓ'} (f : A B) : Set (ℓ ') where
  2. field
  3. equiv-proof : (y : B) → isContr (fiber f y)
  4. infix 4 _≃_
  5. _≃_ : ∀ {ℓ ℓ'} (A : Set ℓ) (B : Set ') → Set (ℓ ⊔ ℓ')
  6. A B = Σ (A B) \ f (isEquiv f)
  7. equivFun : {ℓ '} {A : Set ℓ} {B : Set ℓ'} A B A B
  8. equivFun e = fst e
  9. equivProof : {la lt} (T : Set la) (A : Set lt) (w : T A) (a : A)
  10. ψ (f : Partial ψ (fiber (w .fst) a)) fiber (w .fst) a [ ψ f ]
  11. equivProof A B w a ψ fb = contr' {A = fiber (w .fst) a} (w .snd .equiv-proof a) ψ fb
  12. where
  13. contr' : {ℓ} {A : Set ℓ} isContr A : I) (u : Partial φ A) A
  14. contr' {A = A} (c , p) φ u = hcomp (λ i → λ { (φ = i1) → p (u 1=1) i
  15. ; (φ = i0) → c }) c
  16. {-# BUILTIN EQUIV _≃_ #-}
  17. {-# BUILTIN EQUIVFUN equivFun #-}
  18. {-# BUILTIN EQUIVPROOF equivProof #-}
  19. primitive
  20. primGlue : ∀ {ℓ ℓ'} (A : Set ℓ) : I}
  21. (T : Partial φ (Set ')) → (e : PartialP φ (λ o → T o ≃ A))
  22. → Set ℓ'
  23. prim^glue : {ℓ '} {A : Set ℓ} {φ : I}
  24. → {T : Partial φ (Set ℓ')} {e : PartialP φ o T o A)}
  25. PartialP φ T A primGlue A T e
  26. prim^unglue : {ℓ '} {A : Set ℓ} {φ : I}
  27. → {T : Partial φ (Set ℓ')} {e : PartialP φ o T o A)}
  28. primGlue A T e A
  29. primFaceForall : (I I) I
  30. -- pathToEquiv proves that transport is an equivalence (for details
  31. -- see Agda.Builtin.Cubical.Glue). This is needed internally.
  32. {-# BUILTIN PATHTOEQUIV pathToEquiv #-}

Note that the Glue types are uncurried in agda/cubical to make them more pleasant to use:

  1. Glue : {ℓ '} (A : Set ℓ) {φ : I}
  2. → (Te : Partial φ (Σ[ T ∈ Set ℓ' ] T A))
  3. Set '
  4. Glue A Te = primGlue A (λ x → Te x .fst) (λ x → Te x .snd)

The Agda.Builtin.Cubical.Id exports the cubical identity types:

  1. postulate
  2. Id : {ℓ} {A : Set ℓ} A A Set
  3. {-# BUILTIN ID Id #-}
  4. {-# BUILTIN CONID conid #-}
  5. {-# BUILTIN REFLID reflId #-}
  6. primitive
  7. primDepIMin : _
  8. primIdFace : {ℓ} {A : Set ℓ} {x y : A} Id x y I
  9. primIdPath : {ℓ} {A : Set ℓ} {x y : A} Id x y x y
  10. primitive
  11. primIdElim : {a c} {A : Set a} {x : A}
  12. (C : (y : A) Id x y Set c)
  13. ((φ : I) (y : A [ φ _ x) ])
  14. (w : (x outS y) [ φ { = i1) \ _ x}) ])
  15. C (outS y) (conid φ (outS w)))
  16. {y : A} (p : Id x y) C y p