Run-time Irrelevance

From version 2.6.1 Agda supports run-time irrelevance (or erasure) annotations. Values marked as erased are not present at run time, and consequently the type checker enforces that no computations depend on erased values.

Syntax

A function or constructor argument is declared erased using the @0 or @erased annotation. For example, the following definition of vectors guarantees that the length argument to _∷_ is not present at runtime:

  1. data Vec (A : Set a) : @0 Nat Set a where
  2. [] : Vec A 0
  3. __ : {@0 n} A Vec A n Vec A (suc n)

The GHC backend compiles this to a datatype where the cons constructor takes only two arguments.

Note

In this particular case, the compiler identifies that the length argument can be erased also without the annotation, using Brady et al’s forcing analysis [1]. Marking it erased explictly, however, ensures that it is erased without relying on the analysis.

Erasure annotations can also appear in function arguments (both first-order and higher-order). For instance, here is an implementation of foldl on vectors:

  1. foldl : (B : @0 Nat Set b)
  2. (f : {@0 n} B n A B (suc n))
  3. (z : B 0)
  4. {@0 n} Vec A n B n
  5. foldl B f z [] = z
  6. foldl B f z (x xs) = foldl n B (suc n)) {n} f {suc n}) (f z x) xs

Here the length arguments to foldl and to f have been marked erased. As a result it gets compiled to the following Haskell code (modulo renaming):

  1. foldl f z xs
  2. = case xs of
  3. [] -> z
  4. x xs -> foldl f (f _ z x) xs

In contrast to constructor arguments, erased arguments to higher-order functions are not removed completely, but instead replaced by a placeholder value _. The crucial optimization enabled by the erasure annotation is compiling λ {n} → f {suc n} to simply f, removing a terrible space leak from the program. Compare to the result of compiling without erasure:

  1. foldl f z xs
  2. = case xs of
  3. [] -> z
  4. x xs -> foldl (\ n -> f (1 + n)) (f 0 z x) xs

It is also possible to mark top-level definitions as erased. This guarantees that they are only used in erased arguments and can be useful to ensure that code intended only for compile-time evaluation is not executed at run time. (One can also use erased things in the bodies of erased definitions.) For instance,

  1. @0 spec : Nat Nat -- slow, but easy to verify
  2. impl : Nat Nat -- fast, but hard to understand
  3. proof : n spec n impl n

Erased record fields become erased arguments to the record constructor and the projection functions are treated as erased definitions.

Constructors can also be marked as erased. Here is one example:

  1. Is-proposition : Set a Set a
  2. Is-proposition A = (x y : A) x y
  3. data _ (A : Set a) : Set a where
  4. _ : A A
  5. @0 trivial : Is-proposition A
  6. rec : @0 Is-proposition B (A B) A B
  7. rec p f x = f x
  8. rec p f (trivial x y i) = p (rec p f x) (rec p f y) i

In the code above the constructor trivial is only available at compile-time, whereas ∣_∣ is also available at run-time. Clauses that match on erased constructors in non-erased positions are omitted by (at least some) compiler backends, so one can use erased names in the bodies of such clauses. (There is an exception for constructors that were not declared as erased, but that are treated as erased because they were defined using Cubical Agda, and are used in a module that uses the option --erased-cubical.)

Rules

The typing rules are based on Conor McBride’s “I Got Plenty o’Nuttin’” [2] and Bob Atkey’s “The Syntax and Semantics of Quantitative Type Theory” [3]. In essence the type checker keeps track of whether it is running in run-time mode, checking something that is needed at run time, or compile-time mode, checking something that will be erased. In compile-time mode everything to do with erasure can safely be ignored, but in run-time mode the following restrictions apply:

  • Cannot use erased variables or definitions.

  • Cannot pattern match on erased arguments, unless there is at most one valid case. If --without-K is enabled and there is one valid case, then the datatype must also not be indexed.

Consider the function foo taking an erased vector argument:

  1. foo : (n : Nat) (@0 xs : Vec Nat n) Nat
  2. foo zero [] = 0
  3. foo (suc n) (x xs) = foo n xs

This is okay (when the K rule is on), since after matching on the length, the matching on the vector does not provide any computational information, and any variables in the pattern (x and xs in this case) are marked erased in turn. On the other hand, if we don’t match on the length first, the type checker complains:

  1. foo : (n : Nat) (@0 xs : Vec Nat n) Nat
  2. foo n [] = 0
  3. foo n (x xs) = foo _ xs
  4. -- Error: Cannot branch on erased argument of datatype Vec Nat n

The type checker enters compile-time mode when

  • checking erased arguments to a constructor or function,

  • checking the body of an erased definition,

  • checking the body of a clause that matches (in a non-erased position) on a constructor that was originally defined as erased (it does not suffice for the constructor to be currently treated as erased),

  • checking the domain of an erased Π type, or

  • checking a type, i.e. when moving to the right of a :, with some exceptions:

    • Compile-time mode is not entered for the domains of non-erased Π types.

    • If the K rule is off then compile-time mode is not entered for non-erased constructors (of fibrant type) or record fields.

Note that the type checker does not enter compile-time mode based on the type a term is checked against (except that a distinction is sometimes made between fibrant and non-fibrant types). In particular, checking a term against Set does not trigger compile-time mode.

References

[1] Brady, Edwin, Conor McBride, and James McKinna. “Inductive Families Need Not Store Their Indices.” International Workshop on Types for Proofs and Programs. Springer, Berlin, Heidelberg, 2003.

[2] McBride, Conor. “I Got Plenty o’Nuttin’.” A List of Successes That Can Change the World. Springer, Cham, 2016.

[3] Atkey, Robert. “The Syntax and Semantics of Quantitative Type Theory”. In LICS ‘18: Oxford, United Kingdom. 2018.