Welcome to Agdaâs documentation!Â¶
OverviewÂ¶
Note
The Agda User Manual is a workinprogress and is still incomplete. Contributions, additions and corrections to the Agda manual are greatly appreciated. To do so, please open a pull request or issue on the GitHub Agda page.
This is the manual for the Agda programming language, its type checking, compilation and editing system and related resources/tools.
You can find a lot of useful resources on Agda Wiki site, like tutorials, introductions, publications and books. If youâre new to Agda, you should make use of the resources on Agda Wiki and chapter Getting Started instead of chapter Language Reference.
A description of the Agda language is given in chapter Language Reference. Guidance on how the Agda editing and compilation system can be used can be found in chapter Tools.
Getting StartedÂ¶
What is Agda?Â¶
Agda is a dependently typed programming language. It is an extension of MartinLĂ¶fâs type theory, and is the latest in the tradition of languages developed in the programming logic group at Chalmers. Other languages in this tradition are Alf, Alfa, Agda 1, Cayenne. Some other loosely related languages are Coq, Epigram, and Idris.
Because of strong typing and dependent types, Agda can be used as a proof assistant, allowing to prove mathematical theorems (in a constructive setting) and to run such proofs as algorithms.
Dependent typesÂ¶
Typing for programmersÂ¶
Type theory is concerned both with programming and logic. We see the
type system as a way to express syntactic correctness. A type correct
program has a meaning.
Lisp
is a totally untyped programming language, and so are its derivatives
like
Scheme. In
such languages, if f
is a function, one can apply it to anything,
including itself. This makes it easy to write programs (almost all
programs are well formed), but it also makes it easy to write erroneous
programs. Programs will raise exceptions or loop forever. And it is
very difficult to analyse where the problems are.
Haskell or ML and its derivatives like Standard ML and Caml are typed languages, where functions come with a type expressing what type of arguments the program expects and what the result type is.
Between these two families of languages come languages, which may or may not have a typing discipline. Most imperative languages do not come with a rich type system. For example, C is typed, but very loosely (almost everything is an integer, or a variant thereof). Moreover, the typing system does not allow the definition of trees or graphs without using pointers.
All these languages are examples of partial languages, i.e. the
result of computing the value of an expression e
of type T
is
one of the following:
 the program terminates with a value in the type
T
 the program
e
does not terminate  the program raises an exception (which has been caused by an incomplete definition â for instance a function is only defined for positive integers, but is applied to a negative integer.
Agda and other languages based on type theory are total languages
in the sense that a program e
of type T
will always terminate
with a value in T
. No runtime error can occur and no
nonterminating programs can be written (unless explicitly requested by
the programmer).
Dependent typesÂ¶
Dependent types are introduced by having families of types indexed by
objects in another type. For instance, we can define the type Vec
n
of vectors of length n
. This is a family of types indexed by
objects in Nat
(a type parameterized by natural numbers).
Having dependent types, we must generalize the type of functions and the type of pairs.
The dependent function space (a : A) > (B a)
is the type of
functions taking an argument a
in a type A
and a result in B
a
. Here, A
is a type and B
is a family of types indexed by
elements in A
.
For example, we could define the type of n x m
matrices as a type
indexed by two natural numbers. Call this type Mat n m
. The
function identity
which takes a natural number n
as argument
and produces the n x n
identity matrix is then a function of type
identity : (n : Nat) > (Mat n n)
.
Remark: We could of course just specify the identity
function
with the type Nat > Nat > Mat
, where Mat
is the type of
matrices; but this is not as precise as the dependent version.
The advantage of using dependent types is that it makes it possible to
express properties of programs in the typing system. We saw above that
it is possible to express the type of square matrices of length n
,
it is also possible to define the type of operations on matrices so
that the lengths are correct. For instance the type of matrix
multiplication is
â {i j k} â (Mat i j) > (Mat j k) > (Mat i k)
and the type system can check that a program for matrix multiplication really takes arguments of the correct size. It can also check that matrix multiplication is only applied to matrices where the number of columns of the first argument is the same as the number of rows in the second argument.
Dependent types and logicÂ¶
Thanks to the CurryHoward correspondence, one can express a logical specification using dependent types. Using only typing, it is for example possible to define
 equality on natural numbers
 properties of arithmetical operations
 the type
(n : Nat) > (PrimRoot n)
consisting of functions computing primitive root in modular arithmetic.
Of course a program of the above type will be more difficult to write
than the corresponding program of type Nat > Nat
which produces a
natural number which is a primitive root. However, the difficulty can
be compensated by the fact that the program is guaranteed to work: it
cannot produce something which is not a primitive root.
On a more mathematical level, we can express formulas and prove them
using an algorithm. For example, a function of type (n : Nat) >
(PrimRoot n)
is also a proof that every natural number has a
primitive root.
PrerequisitesÂ¶
You need recent versions of the following programs to compile Agda:
 GHC: https://www.haskell.org/ghc/
 Agda have been tested with GHC 8.0.2, 8.2.2, 8.4.4 and 8.6.5.
 cabalinstall: https://www.haskell.org/cabal/
 Alex: https://www.haskell.org/alex/
 Happy: https://www.haskell.org/happy/
 GNU Emacs: http://www.gnu.org/software/emacs/
You should also make sure that programs installed by cabalinstall are on your shellâs search path.
For instructions on installing a suitable version of Emacs under Windows, see Installing Emacs under Windows.
NonWindows users need to ensure that the development files for the C libraries zlib* and ncurses* are installed (see http://zlib.net and http://www.gnu.org/software/ncurses/). Your package manager may be able to install these files for you. For instance, on Debian or Ubuntu it should suffice to run
aptget install zlib1gdev libncurses5dev
as root to get the correct files installed.
Optionally one can also install the ICU library, which is used to implement
the countclusters
flag. Under Debian or Ubuntu it may suffice
to install libicudev. Once the ICU library is installed one can
hopefully enable the countclusters
flag by giving the
fenableclustercounting
flag to cabal install.
Installing Emacs under WindowsÂ¶
A precompiled version of Emacs 26.1, with the necessary mathematical fonts, is available at http://www.cs.uiowa.edu/~astump/agda.
InstallationÂ¶
There are several ways to install Agda:
 Using a released source package from Hackage
 Using a binary package prepared for your platform
 Using the development version from the Git repository
Agda can be installed using different flags (see Installation Flags).
Installation from HackageÂ¶
You can install the latest released version of Agda from Hackage. Install the prerequisites and then run the following commands:
cabal update
cabal install Agda
agdamode setup
The last command tries to set up Emacs for use with Agda via the Emacs mode. As an alternative you can copy the following text to your .emacs file:
(loadfile (let ((codingsystemforread 'utf8))
(shellcommandtostring "agdamode locate")))
It is also possible (but not necessary) to compile the Emacs modeâs files:
agdamode compile
This can, in some cases, give a noticeable speedup.
Warning: If you reinstall the Agda mode without recompiling the Emacs Lisp files, then Emacs may continue using the old, compiled files.
Prebuilt Packages and SystemSpecific InstructionsÂ¶
Arch LinuxÂ¶
The following prebuilt packages are available:
However, due to significant packaging bugs [such as this](https://bugs.archlinux.org/task/61904?project=5&string=agda), you might want to use alternative installation methods.
Debian / UbuntuÂ¶
Prebuilt packages are available for Debian and Ubuntu from Karmic onwards. To install:
aptget install agdamode
This should install Agda and the Emacs mode.
The standard library is available in Debian and Ubuntu from Lucid onwards. To install:
aptget install agdastdlib
More information:
Reporting bugs:
Please report any bugs to Debian, using:
reportbug B debian agda
reportbug B debian agdastdlib
FedoraÂ¶
Agda is packaged in Fedora (since before Fedora 18).
yum install Agda
will pull in emacsagdamode and ghcAgdadevel.
FreeBSDÂ¶
Packages are available from FreshPorts for Agda and Agda standard library.
NixOSÂ¶
Agda is part of the Nixpkgs collection that is used by https://nixos.org/nixos. To install Agda and agdamode for Emacs, type:
nixenv f "<nixpkgs>" iA haskellPackages.Agda
If youâre just interested in the library, you can also install the library without the executable. The Agda standard library is currently not installed automatically.
OS XÂ¶
Homebrew provides prebuilt packages for OS X. To install:
brew install agda
This should take less than a minute, and install Agda together with the Emacs mode and the standard library.
By default, the standard library is installed in
/usr/local/lib/agda/
. To use the standard library, it is
convenient to add /usr/local/lib/agda/standardlibrary.agdalib
to
~/.agda/libraries
, and specify standardlibrary
in
~/.agda/defaults
. Note this is not performed automatically.
It is also possible to install withoutstdlib
,
withoutghc
, or from HEAD
. Note this will require
building Agda from source.
For more information, refer to the Homebrew documentation.
Note
If Emacs cannot find the agdamode
executable, it might help to
install the execpathfromshell package by doing Mx
packageinstall RET execpathfromshell RET
, and adding
(execpathfromshellinitialize)
to your .emacs
file.
Installation of the Development VersionÂ¶
After getting the development version following the instructions in the Agda wiki:
Install the prerequisites
In the toplevel directory of the Agda source tree
Follow the instructions for installing Agda from Hackage (except run
cabal install
instead ofcabal install Agda
) orYou can try to install Agda (including a compiled Emacs mode) by running the following command:
make install
Note that on a Mac, because ICU is installed in a nonstandard location, you need to specify this location on the command line:
make installbin CABAL_OPTS='extralibdirs=/usr/local/opt/icu4c/lib extraincludedirs=/usr/local/opt/icu4c/include'
Installation FlagsÂ¶
When installing Agda the following flags can be used:
cpphs
 Use cpphs instead of cpp. Default: off.
debug
 Enable debugging features that may slow Agda down. Default: off.
flag enableclustercounting
 Enable the
countclusters
flag (see Counting Extended Grapheme Clusters). Note that ifenableclustercounting
isFalse
, then thecountclusters
flag triggers an error message. Default: off.
âHello worldâ in AgdaÂ¶
Below is a complete âhello worldâ program in Agda (defined in a file helloworld.agda
)
module helloworld where
open import IO
main = run (putStrLn "Hello, World!")
To compile the Agda file, either open it in Emacs and press Cc Cx
Cc
or run agda compile helloworld.agda
from the command
line.
A quick linebyline explanation:
 Agda programs are structured in modules. The first module in each file is the toplevel module whose name matches the filename. The contents of a module are declaration such as data types and function definitions.
 Other modules can be imported using an
import
statement, for exampleopen import IO
. This imports the IO module from the standard library and brings its contents into scope.  A module exporting a function
main : IO a
can be compiled to a standalone executable. For example:main = run (putStrLn "Hello, World!")
runs theIO
commandputStrLn "Hello, World!"
and then quits the program.
Quick Guide to Editing, Type Checking and Compiling Agda CodeÂ¶
IntroductionÂ¶
Agda programs are commonly edited using Emacs or Atom. To edit a module (assuming you
have installed Agda and its Emacs mode (or
Atomâs) properly), start the editor and open a file ending in
.agda
. Programs are developed interactively, which means that
one can type check code which is not yet complete: if a question mark
(?
) is used as a placeholder for an expression, and the buffer is
then checked, Agda will replace the question mark with a âholeâ which
can be filled in later. One can also do various other things in the
context of a hole: listing the context, inferring the type of an
expression, and even evaluating an open term which mentions variables
bound in the surrounding context.
The following commands are the most common (see Notation for key combinations):
 Cc Cl
 Load. Typechecks the contents of the file.
 Cc C,
 Shows the goal type, i.e. the type expected in the current hole, along with the types of locally defined identifiers.
 Cc C.
 A variant of
Cc C,
that also tries to infer the type of the current holeâs contents.  Cc CSPC
 Give. Checks whether the term written in the current hole has the right type and, if it does, replaces the hole with that term.
 Cc Cr
 Refine. Checks whether the return type of the
expression
e
in the hole matches the expected type. If so, the hole is replaced bye { }1 ... { }n
, where a sufficient number of new holes have been inserted. If the hole is empty, then the refine command instead inserts a lambda or constructor (if there is a unique typecorrect choice).  Cc Cc
 Case split. If the cursor is positioned in a hole which denotes the right hand side of a definition, then this command automatically performs pattern matching on variables of your choice.
 Cc Cn
 Normalise. The system asks for a term which is then evaluated.
 M.
 Go to definition. Goes to the definition site of the identifier under the cursor (if known).
 M*
 Go back (Emacs < 25.1)
 M,
 Go back (Emacs â„ 25.1)
For information related to the Emacs mode (configuration, keybindings, Unicode input, etc.) see Emacs Mode.
Writing mathematical symbols in source codeÂ¶
Agda uses Unicode
characters in source files (more specifically: the UTF8 character encoding). Almost
any character can be used in an identifier (like â
, Î±
, â§
,
or â
, for example). It is therefore necessary to have spaces
between most lexical units.
Many mathematical symbols can be typed using the corresponding LaTeX command names. For instance,
you type \forall
to input â
. A more detailed description of
how to write various characters is available.
(Note that if you try to read Agda code using another program, then you have to make sure that it uses the right character encoding when decoding the source files.)
ErrorsÂ¶
If a file does not type check Agda will complain. Often the cursor will jump to the position of the error, and the error will (by default) be underlined. Some errors are treated a bit differently, though. If Agda cannot see that a definition is terminating/productive it will highlight it in light salmon, and if some metavariable other than the goals cannot be solved the code will be highlighted in yellow (the highlighting may not appear until after you have reloaded the file). In case of the latter kinds of errors you can still work with the file, but Agda will (by default) refuse to import it into another module, and if your functions are not terminating Agda may hang.
If you do not like the way errors are highlighted (if you are
colourblind, for instance), then you can tweak the settings by typing
Mx customizegroup RET agda2highlight RET
in Emacs (after
loading an Agda file) and following the instructions.
Compiling Agda programsÂ¶
To compile a module containing a function main :: IO A
for some
A
(where IO
can be found in the Primitive.agda),
use Cc Cx Cc
. If the module is named A.B.C
the resulting
binary will be called C
(located in the projectâs toplevel
directory, the one containing the A
directory).
Batchmode commandÂ¶
There is also a batchmode command line tool: agda
. To find out
more about this command, use agda help
.
A List of TutorialsÂ¶
Introduction to AgdaÂ¶
 Ulf Norell and James Chapman.
 Dependently Typed Programming in Agda. This is aimed at functional programmers.
 Ana Bove and Peter Dybjer.
 Dependent Types at Work. A gentle introduction including logic and proofs of programs.
 Ana Bove, Peter Dybjer, and Ulf Norell.
 A Brief Overview of Agda  A Functional Language with Dependent Types (in TPHOLs 2009) with an example of reflection. Code
 Anton Setzer.
 Lecture notes on Interactive Theorem Proving. Swansea University. These lecture notes are based on Agda and contain an introduction of Agda for students with a very basic background in logic and functional programming.
 Daniel Peebles.
 Introduction to Agda. Video of talk from the January 2011 Boston Haskell session at MIT.
 Conor McBride.
 Introduction to Dependently Typed Programming using Agda. (videos of lectures). Associated source files, with exercises.
 Andreas Abel.
 Agda lecture notes. Lecture notes used in teaching functional programming: basic introduction to Agda, CurryHoward, equality, and verification of optimizations like fusion.
 Jan Malakhovski.
 Thorsten Altenkirch.
 Computer Aided Formal Reasoning  online lecture notes
 Daniel Licata.
 Dependently Typed Programming in Agda (OPLSS 2013).
 Phil Wadler.
 Aaron Stump.
 DiviĂĄnszky PĂ©ter.
 Musa Alhassy.
Courses using AgdaÂ¶
 Computer Aided Reasoning Material for a 3rd / 4th year course (g53cfr, g54 cfr) at the university of Nottingham 2010 by Thorsten Altenkirch
 Type Theory in Rosario Material for an Agda course in Rosario, Argentina in 2011 by Thorsten Altenkirch
 Software System Design and Implementation , undergrad(?) course at the University of New South Wales by Manuel Chakravarty.
 TĂŒĂŒbiteooria / Type Theory , graduate course at the University of Tartu by Varmo Vene and James Chapman.
 Advanced Topics in Programming Languages: Dependent Type Systems , course at the University of Pennsylvania by Stephanie Weirich.
 Categorical Logic , course at the University of Cambridge by Samuel Staton.  More info and feedback
 Dependently typed functional languages , master level course at EAFIT University by AndrĂ©s SicardRamĂrez.
 Introduction to Dependently Typed Programming using Agda , research level course at the University of Edinburgh by Conor McBride.
 Agda , introductory course for master students at ELTE EĂ¶tvĂ¶s Collegium in Budapest by PĂ©ter DiviĂĄnszky and Ambrus Kaposi.
 Types for Programs and Proofs , course at Chalmers University of Technology.
 Advanced Functional Programming (in German), course at LudwigMaximiliansUniversity Munich.
 Dependently typed metaprogramming (in Agda) , Summer (2013) course at the University of Cambridge by Conor McBride.
 ComputerChecked Programs and Proofs (COMP 3601), Dan Licata, Wesleyan, Fall 2013.
 Advanced Functional Programming Fall 2013 (CS410), Conor McBride, Strathclyde, notes from 2015, videos from 2017.
 Interactive Theorem proving (CS__336), Anton Setzer, Swansea University, Lent 2008.
 Inductive and inductiverecursive definitions in Intuitionistic Type Theory , lectures by Peter Dybjer at the Oregon Programming Languages Summer School 2015.
 Introduction to Univalent Foundations of Mathematics with Agda , MGS 2019 MartĂn HĂ¶tzel EscardĂł
MiscellaneousÂ¶
 Agda has a Wikipedia page
Language ReferenceÂ¶
Abstract definitionsÂ¶
Definitions can be marked as abstract, for the purpose of hiding implementation details, or to speed up typechecking of other parts. In essence, abstract definitions behave like postulates, thus, do not reduce/compute. For instance, proofs whose content does not matter could be marked abstract, to prevent Agda from unfolding them (which might slow down typechecking).
As a guiding principle, all the rules concerning abstract
are
designed to prevent the leaking of implementation details of abstract
definitions. Similar concepts of other programming language include
(nonrepresentative sample):
UCSD Pascalâs and Javaâs interfaces and MLâs signatures.
(Especially when abstract definitions are used in combination with modules.)
SynopsisÂ¶
 Declarations can be marked as abstract using the block keyword
abstract
.  Outside of abstract blocks, abstract definitions do not reduce, they are treated as postulates,
in particular:
 Abstract functions never match, thus, do not reduce.
 Abstract data types do not expose their constructors.
 Abstract record types do not expose their fields nor constructor.
 Other declarations cannot be abstract.
 Inside abstract blocks, abstract definitions reduce while type checking definitions, but not while checking their type signatures. Otherwise, due to dependent types, one could leak implementation details (e.g. expose reduction behavior by using propositional equality).
 Inside
private
type signatures inabstract
blocks, abstract definitions do reduce. However, there are some problems with this. See Issue #418.  The reach of the
abstract
keyword block extends recursively to thewhere
blocks of a function and the declarations inside of arecord
declaration, but not inside modules declared in an abstract block.
ExamplesÂ¶
Integers can be implemented in various ways, e.g. as difference of two natural numbers:
module Integer where
abstract
â€ = Nat Ă Nat
0â€ : â€
0â€ = 0 , 0
1â€ : â€
1â€ = 1 , 0
_+â€_ : (x y : â€) â â€
(p , n) +â€ (p' , n') = (p + p') , (n + n')
â€_ : â€ â â€
â€ (p , n) = (n , p)
_âĄâ€_ : (x y : â€) â Set
(p , n) âĄâ€ (p' , n') = (p + n') âĄ (p' + n)
private
postulate
+comm : â n m â (n + m) âĄ (m + n)
invâ€ : â x â (x +â€ (â€ x)) âĄâ€ 0â€
invâ€ (p , n) rewrite +comm (p + n) 0  +comm p n = refl
Using abstract
we do not give away the actual representation of
integers, nor the implementation of the operations. We can construct
them from 0â€
, 1â€
, _+â€_
, and â€
, but only reason about
equality âĄâ€
with the provided lemma invâ€
.
The following property shapeof0â€
of the integer zero exposes the
representation of integers as pairs. As such, it is rejected by Agda:
when checking its type signature, projâ x
fails to type check
since x
is of abstract type â€
. Remember that the abstract
definition of â€
does not unfold in type signatures, even when in
an abstract block! However, if we make shapeofâ€
private,
unfolding of abstract definitions like â€
is enabled, and we
succeed:
 A property about the representation of zero integers:
abstract
private
shapeof0â€ : â (x : â€) (is0â€ : x âĄâ€ 0â€) â projâ x âĄ projâ x
shapeof0â€ (p , n) refl rewrite +comm p 0 = refl
By requiring shapeof0â€
to be private to typecheck, leaking of
representation details is prevented.
Scope of abstractionÂ¶
In child modules, when checking an abstract definition, the abstract definitions of the parent module are transparent:
module M1 where
abstract
x = 0
module M2 where
abstract
xis0 : x âĄ 0
xis0 = refl
Thus, child modules can see into the representation choices of their parent modules. However, parent modules cannot see like this into child modules, nor can sibling modules see through each others abstract definitions. An exception to this is anonymous modules, which share abstract scope with their parent module, allowing parent or sibling modules to see inside their abstract definitions.
The reach of the abstract
keyword does not extend into modules:
module Parent where
abstract
module Child where
y = 0
x = 0  to avoid "useless abstract" error
yis0 : Child.y âĄ 0
yis0 = refl
The declarations in module Child
are not abstract!
Abstract definitions with whereblocksÂ¶
Definitions in a where
block of an abstract definition are abstract
as well. This means, they can see through the abstractions of their
uncles:
module Where where
abstract
x : Nat
x = 0
y : Nat
y = x
where
xâĄy : x âĄ 0
xâĄy = refl
Type signatures in where
blocks are private, so it is fine to make
type abbreviations in where
blocks of abstract definitions:
module WherePrivate where
abstract
x : Nat
x = projâ t
where
T = Nat Ă Nat
t : T
t = 0 , 1
p : projâ t âĄ 0
p = refl
Note that if p
was not private, application projâ t
in its type
would be illformed, due to the abstract definition of T
.
Named where
modules do not make their declarations private, thus
this example will fail if you replace x
âs where
by module M
where
.
BuiltinsÂ¶
The Agda type checker knows about, and has special treatment for, a number of
different concepts. The most prominent is natural numbers, which has a special
representation as Haskell integers and support for fast arithmetic. The surface
syntax of these concepts are not fixed, however, so in order to use the special
treatment of natural numbers (say) you define an appropriate data type and then
bind that type to the natural number concept using a BUILTIN
pragma.
Some builtin types support primitive functions that have no corresponding Agda
definition. These functions are declared using the primitive
keyword by
giving their type signature.
Using the builtin typesÂ¶
While it is possible to define your own versions of the builtin types and bind
them using BUILTIN
pragmas, it is recommended to use the definitions in the
Agda.Builtin
modules. These modules are installed when you install Agda and
so are always available. For instance, builtin natural numbers are defined in
Agda.Builtin.Nat
. The standard library and the agdaprelude
reexport the definitions from these modules.
The unit typeÂ¶
module Agda.Builtin.Unit
The unit type is bound to the builtin UNIT
as follows:
record â€ : Set where
{# BUILTIN UNIT â€ #}
Agda needs to know about the unit type since some of the primitive operations in the reflected type checking monad return values in the unit type.
BooleansÂ¶
module Agda.Builtin.Bool where
Builtin booleans are bound using the BOOL
, TRUE
and FALSE
builtins:
data Bool : Set where
false true : Bool
{# BUILTIN BOOL Bool #}
{# BUILTIN TRUE true #}
{# BUILTIN FALSE false #}
Note that unlike for natural numbers, you need to bind the constructors separately. The reason for this is that Agda cannot tell which constructor should correspond to true and which to false, since you are free to name them whatever you like.
The effect of binding the boolean type is that you can then use primitive
functions returning booleans, such as builtin NATEQUALS
, and letting the
GHC backend know to compile the type to Haskell Bool.
Natural numbersÂ¶
module Agda.Builtin.Nat
Builtin natural numbers are bound using the NATURAL
builtin as follows:
data Nat : Set where
zero : Nat
suc : Nat â Nat
{# BUILTIN NATURAL Nat #}
The names of the data type and the constructors can be chosen freely, but the shape of the datatype needs to match the one given above (modulo the order of the constructors). Note that the constructors need not be bound explicitly.
Binding the builtin natural numbers as above has the following effects:
 The use of natural number literals is
enabled. By default the type of a natural number literal will be
Nat
, but it can be overloaded to include other types as well.  Closed natural numbers are represented as Haskell integers at compiletime.
 The compiler backends compile natural numbers to the appropriate number type in the target language.
 Enabled binding the builtin natural number functions described below.
Functions on natural numbersÂ¶
There are a number of builtin functions on natural numbers. These are special in that they have both an Agda definition and a primitive implementation. The primitive implementation is used to evaluate applications to closed terms, and the Agda definition is used otherwise. This lets you prove things about the functions while still enjoying good performance of compiletime evaluation. The builtin functions are the following:
_+_ : Nat â Nat â Nat
zero + m = m
suc n + m = suc (n + m)
{# BUILTIN NATPLUS _+_ #}
__ : Nat â Nat â Nat
n  zero = n
zero  suc m = zero
suc n  suc m = n  m
{# BUILTIN NATMINUS __ #}
_*_ : Nat â Nat â Nat
zero * m = zero
suc n * m = (n * m) + m
{# BUILTIN NATTIMES _*_ #}
_==_ : Nat â Nat â Bool
zero == zero = true
suc n == suc m = n == m
_ == _ = false
{# BUILTIN NATEQUALS _==_ #}
_<_ : Nat â Nat â Bool
_ < zero = false
zero < suc _ = true
suc n < suc m = n < m
{# BUILTIN NATLESS _<_ #}
divhelper : Nat â Nat â Nat â Nat â Nat
divhelper k m zero j = k
divhelper k m (suc n) zero = divhelper (suc k) m n m
divhelper k m (suc n) (suc j) = divhelper k m n j
{# BUILTIN NATDIVSUCAUX divhelper #}
modhelper : Nat â Nat â Nat â Nat â Nat
modhelper k m zero j = k
modhelper k m (suc n) zero = modhelper 0 m n m
modhelper k m (suc n) (suc j) = modhelper (suc k) m n j
{# BUILTIN NATMODSUCAUX modhelper #}
The Agda definitions are checked to make sure that they really define the corresponding builtin function. The definitions are not required to be exactly those given above, for instance, addition and multiplication can be defined by recursion on either argument, and you can swap the arguments to the addition in the recursive case of multiplication.
The NATDIVSUCAUX
and NATMODSUCAUX
are builtins bind helper functions
for defining natural number division and modulo operations, and satisfy the
properties
div n (suc m) âĄ divhelper 0 m n m
mod n (suc m) âĄ modhelper 0 m n m
Machine wordsÂ¶
module Agda.Builtin.Word
module Agda.Builtin.Word.Properties
Agda supports builtin 64bit machine words, bound with the WORD64
builtin:
postulate Word64 : Set
{# BUILTIN WORD64 Word64 #}
Machine words can be converted to and from natural numbers using the following primitives:
primitive
primWord64ToNat : Word64 â Nat
primWord64FromNat : Nat â Word64
Converting to a natural number is the trivial embedding, and converting from a natural number gives you the remainder modulo . The proof of the former theorem:
primitive
primWord64ToNatInjective : â a b â primWord64ToNat a âĄ primWord64ToNat b â a âĄ b
is in the Properties
module. The proof of the latter theorem is not primitive,
but can be defined in a library using primTrustMe.
Basic arithmetic operations can be defined on Word64
by converting to
natural numbers, performing the corresponding operation, and then converting
back. The compiler will optimise these to use 64bit arithmetic. For
instance:
addWord : Word64 â Word64 â Word64
addWord a b = primWord64FromNat (primWord64ToNat a + primWord64ToNat b)
subWord : Word64 â Word64 â Word64
subWord a b = primWord64FromNat ((primWord64ToNat a + 18446744073709551616)  primWord64ToNat b)
These compile to primitive addition and subtraction on 64bit words, which in the
GHC backend map to operations on Haskell 64bit words
(Data.Word.Word64
).
IntegersÂ¶
module Agda.Builtin.Int
Builtin integers are bound with the INTEGER
builtin to a data type with
two constructors: one for positive and one for negative numbers. The builtins
for the constructors are INTEGERPOS
and INTEGERNEGSUC
.
data Int : Set where
pos : Nat â Int
negsuc : Nat â Int
{# BUILTIN INTEGER Int #}
{# BUILTIN INTEGERPOS pos #}
{# BUILTIN INTEGERNEGSUC negsuc #}
Here negsuc n
represents the integer n  1
. Unlike for natural
numbers, there is no special representation of integers at compiletime since
the overhead of using the data type compared to Haskell integers is not that
big.
Builtin integers support the following primitive operation (given a suitable binding for String):
primitive
primShowInteger : Int â String
FloatsÂ¶
module Agda.Builtin.Float
module Agda.Builtin.Float.Properties
Floating point numbers are bound with the FLOAT
builtin:
postulate Float : Set
{# BUILTIN FLOAT Float #}
This lets you use floating point literals. Floats are represented by the type checker as IEEE 754 binary64 double precision floats, with the restriction that there is exactly one NaN value. The following primitive functions are available (with suitable bindings for Nat, Bool, String and Int):
primitive
primNatToFloat : Nat â Float
primFloatPlus : Float â Float â Float
primFloatMinus : Float â Float â Float
primFloatTimes : Float â Float â Float
primFloatNegate : Float â Float
primFloatDiv : Float â Float â Float
primFloatEquality : Float â Float â Bool
primFloatLess : Float â Float â Bool
primFloatNumericalEquality : Float â Float â Bool
primFloatNumericalLess : Float â Float â Bool
primRound : Float â Int
primFloor : Float â Int
primCeiling : Float â Int
primExp : Float â Float
primLog : Float â Float
primSin : Float â Float
primCos : Float â Float
primTan : Float â Float
primASin : Float â Float
primACos : Float â Float
primATan : Float â Float
primATan2 : Float â Float â Float
primShowFloat : Float â String
The primFloatEquality
primitive is intended to be used for decidable
propositional equality. To enable proof carrying comparisons while preserving
consistency, the following laws apply:
nan=nan : primFloatEquality NaN NaN âĄ true
nan=nan = refl
nan=nan : primFloatEquality NaN (primFloatNegate NaN) âĄ true
nan=nan = refl
neg0â 0 : primFloatEquality 0.0 0.0 âĄ false
neg0â 0 = refl
Correspondingly, the primFloatLess
can be used to provide a decidable total order,
given by the following laws:
_[<]_ : Float â Float â Set
x [<] y = primFloatLess x y && not (primFloatLess y x) âĄ true
inf<nan : Inf [<] NaN
nan<neg : NaN [<] 1.0
neg<neg0 : 1.0 [<] 0.0
neg0<0 : 0.0 [<] 0.0
0<pos : 0.0 [<] 1.0
pos<Inf : 1.0 [<] Inf
inf<nan = refl
nan<neg = refl
neg<neg0 = refl
neg0<0 = refl
0<pos = refl
pos<Inf = refl
For numerical comparisons, use the primFloatNumericalEquality
and
primFloatNumericalLess
primitives. These are implemented by the
corresponding IEEE functions.
Floating point numbers can be converted to its raw representation using the primitive:
primitive
primFloatToWord64 : Float â Word64
which normalises all NaN
to a canonical NaN
with an injectivity proof:
primFloatToWord64Injective : â a b â primFloatToWord64 a âĄ primFloatToWord64 b â a âĄ b
in the Properties
module. These primitives can be used to define
a decidable propositional equality with the safe
option.
ListsÂ¶
module Agda.Builtin.List
Builtin lists are bound using the LIST
builtin:
data List {a} (A : Set a) : Set a where
[] : List A
_â·_ : (x : A) (xs : List A) â List A
{# BUILTIN LIST List #}
infixr 5 _â·_
The constructors are bound automatically when binding the type. Lists are not
required to be level polymorphic; List : Set â Set
is also accepted.
As with booleans, the effect of binding the LIST
builtin is to let
you use primitive functions working with lists, such as primStringToList
and primStringFromList
, and letting the GHC backend
know to compile the List type to Haskell lists.
CharactersÂ¶
module Agda.Builtin.Char
module Agda.Builtin.Char.Properties
The character type is bound with the CHARACTER
builtin:
postulate Char : Set
{# BUILTIN CHAR Char #}
Binding the character type lets you use character literals. The following primitive functions are available on characters (given suitable bindings for Bool, Nat and String):
primitive
primIsLower : Char â Bool
primIsDigit : Char â Bool
primIsAlpha : Char â Bool
primIsSpace : Char â Bool
primIsAscii : Char â Bool
primIsLatin1 : Char â Bool
primIsPrint : Char â Bool
primIsHexDigit : Char â Bool
primToUpper : Char â Char
primToLower : Char â Char
primCharToNat : Char â Nat
primNatToChar : Nat â Char
primShowChar : Char â String
These functions are implemented by the corresponding Haskell functions from
Data.Char (ord
and chr
for primCharToNat
and
primNatToChar
). To make primNatToChar
total chr
is applied to the
natural number modulo 0x110000
.
Converting to a natural number is the obvious embedding, and its proof:
primitive
primCharToNatInjective : â a b â primCharToNat a âĄ primCharToNat b â a âĄ b
can be found in the Properties
module.
StringsÂ¶
module Agda.Builtin.String
module Agda.Builtin.String.Properties
The string type is bound with the STRING
builtin:
postulate String : Set
{# BUILTIN STRING String #}
Binding the string type lets you use string literals. The following primitive functions are available on strings (given suitable bindings for Bool, Char and List):
primitive
primStringToList : String â List Char
primStringFromList : List Char â String
primStringAppend : String â String â String
primStringEquality : String â String â Bool
primShowString : String â String
String literals can be overloaded.
Converting to a list is injective, and its proof:
primitive
primStringToListInjective : â a b â primStringToList a âĄ primStringToList b â a âĄ b
can found in the Properties
module.
EqualityÂ¶
module Agda.Builtin.Equality
The identity type can be bound to the builtin EQUALITY
as follows
infix 4 _âĄ_
data _âĄ_ {a} {A : Set a} (x : A) : A â Set a where
refl : x âĄ x
{# BUILTIN EQUALITY _âĄ_ #}
This lets you use proofs of type lhs âĄ rhs
in the rewrite
construction.
Other variants of the identity type are also accepted as builtin:
data _âĄ_ {A : Set} : (x y : A) â Set where
refl : (x : A) â x âĄ x
The type of primEraseEquality
has to match the flavor of identity type.
module Agda.Builtin.Equality.Erase
Binding the builtin equality type also enables the primEraseEquality
primitive:
primitive
primEraseEquality : â {a} {A : Set a} {x y : A} â x âĄ y â x âĄ y
The function takes a proof of an equality between two values x
and y
and stays
stuck on it until x
and y
actually become definitionally equal. Whenever that
is the case, primEraseEquality e
reduces to refl
.
One use of primEraseEquality
is to replace an equality proof computed using an expensive
function (e.g. a proof by reflection) by one which is trivially refl
on the diagonal.
primTrustMeÂ¶
module Agda.Builtin.TrustMe
From the primEraseEquality
primitive, we can derive a notion of primTrustMe
:
primTrustMe : â {a} {A : Set a} {x y : A} â x âĄ y
primTrustMe {x = x} {y} = primEraseEquality unsafePrimTrustMe
where postulate unsafePrimTrustMe : x âĄ y
As can be seen from the type, primTrustMe
must be used with the
utmost care to avoid inconsistencies. What makes it different from a
postulate is that if x
and y
are actually definitionally
equal, primTrustMe
reduces to refl
. One use of primTrustMe
is to lift the primitive boolean equality on builtin types like
String to something that returns a proof
object:
eqString : (a b : String) â Maybe (a âĄ b)
eqString a b = if primStringEquality a b
then just primTrustMe
else nothing
With this definition eqString "foo" "foo"
computes to just refl
.
Universe levelsÂ¶
module Agda.Primitive
Universe levels are also declared using BUILTIN
pragmas. In contrast to the Agda.Builtin
modules, the Agda.Primitive
module
is autoimported and thus it is not possible to change the level builtins. For
reference these are the bindings:
postulate
Level : Set
lzero : Level
lsuc : Level â Level
_â_ : Level â Level â Level
{# BUILTIN LEVEL Level #}
{# BUILTIN LEVELZERO lzero #}
{# BUILTIN LEVELSUC lsuc #}
{# BUILTIN LEVELMAX _â_ #}
Sized typesÂ¶
module Agda.Builtin.Size
The builtins for sized types are different from other
builtins in that the names are defined by the BUILTIN
pragma. Hence, to
bind the size primitives it is enough to write:
{# BUILTIN SIZEUNIV SizeUniv #}  SizeUniv : SizeUniv
{# BUILTIN SIZE Size #}  Size : SizeUniv
{# BUILTIN SIZELT Size<_ #}  Size<_ : ..Size â SizeUniv
{# BUILTIN SIZESUC â_ #}  â_ : Size â Size
{# BUILTIN SIZEINF â #}  â : Size
{# BUILTIN SIZEMAX _âËą_ #}  _âËą_ : Size â Size â Size
CoinductionÂ¶
module Agda.Builtin.Coinduction
The following builtins are used for coinductive definitions:
postulate
â : â {a} (A : Set a) â Set a
âŻ_ : â {a} {A : Set a} â A â â A
â : â {a} {A : Set a} â â A â A
{# BUILTIN INFINITY â #}
{# BUILTIN SHARP âŻ_ #}
{# BUILTIN FLAT â #}
See Coinduction for more information.
IOÂ¶
module Agda.Builtin.IO
The sole purpose of binding the builtin IO
type is to let Agda check that
the main
function has the right type (see Compilers).
postulate IO : Set â Set
{# BUILTIN IO IO #}
Literal overloadingÂ¶
module Agda.Builtin.FromNat
module Agda.Builtin.FromNeg
module Agda.Builtin.FromString
The machinery for overloading literals uses builtins for the conversion functions.
ReflectionÂ¶
module Agda.Builtin.Reflection
The reflection machinery has builtin types for representing Agda programs. See Reflection for a detailed description.
RewritingÂ¶
The experimental and totally unsafe rewriting machinery (not
to be confused with the rewrite construct) has a builtin
REWRITE
for the rewriting relation:
postulate _âŠ_ : â {a} {A : Set a} â A â A â Set a
{# BUILTIN REWRITE _âŠ_ #}
This builtin is bound to the builtin equality type from Agda.Builtin.Equality
in
Agda.Builtin.Equality.Rewrite
.
Static valuesÂ¶
The STATIC
pragma can be used to mark definitions which should
be normalised before compilation. The typical use case for this is
to mark the interpreter of an embedded language as STATIC
:
{# STATIC <Name> #}
StrictnessÂ¶
module Agda.Builtin.Strict
There are two primitives for controlling evaluation order:
primitive
primForce : â {a b} {A : Set a} {B : A â Set b} (x : A) â (â x â B x) â B x
primForceLemma : â {a b} {A : Set a} {B : A â Set b} (x : A) (f : â x â B x) â primForce x f âĄ f x
where _âĄ_
is the builtin equality. At compiletime
primForce x f
evaluates to f x
when x
is in weak head normal form (whnf),
i.e. one of the following:
 a constructor application
 a literal
 a lambda abstraction
 a type constructor application (data or record type)
 a function type
 a universe (
Set _
)
Similarly primForceLemma x f
, which lets you reason about programs using
primForce
, evaluates to refl
when x
is in whnf. At runtime,
primForce e f
is compiled (by the GHC backend)
to let x = e in seq x (f x)
.
For example, consider the following function:
 powâ n a = a 2âż
powâ : Nat â Nat â Nat
powâ zero a = a
powâ (suc n) a = powâ n (a + a)
There is a space leak here (both for compiletime and runtime evaluation),
caused by unevaluated a + a
thunks. This problem can be fixed with
primForce
:
infixr 0 _$!_
_$!_ : â {a b} {A : Set a} {B : A â Set b} â (â x â B x) â â x â B x
f $! x = primForce x f
 pow n a = a 2âż
pow : Nat â Nat â Nat
pow zero a = a
pow (suc n) a = pow n $! a + a
CoinductionÂ¶
The corecursive definitions below are accepted if the option
guardedness
is active:
{# OPTIONS guardedness #}
(An alternative approach is to use Sized Types.)
Coinductive RecordsÂ¶
It is possible to define the type of infinite lists (or streams) of
elements of some type A
as follows,
record Stream (A : Set) : Set where
coinductive
field
hd : A
tl : Stream A
As opposed to inductive record types, we have to introduce the keyword
coinductive
before defining the fields that constitute the record.
It is interesting to note that is not necessary to give an explicit
constructor to the record type Stream A
.
We can as well define bisimilarity (equivalence) of a pair of Stream A
as a
coinductive record.
record _â_ {A : Set} (xs : Stream A) (ys : Stream A) : Set where
coinductive
field
hdâ : hd xs âĄ hd ys
tlâ : tl xs â tl ys
Using copatterns we can define a pair of functions
on Stream
such that one returns a Stream
with the elements in
the even positions and the other the elements in odd positions.
even : â {A} â Stream A â Stream A
hd (even x) = hd x
tl (even x) = even (tl (tl x))
odd : â {A} â Stream A â Stream A
odd x = even (tl x)
split : â {A} â Stream A â Stream A Ă Stream A
split xs = even xs , odd xs
And merge a pair of Stream
by interleaving their elements.
merge : â {A} â Stream A Ă Stream A â Stream A
hd (merge (fst , snd)) = hd fst
tl (merge (fst , snd)) = merge (snd , tl fst)
Finally, we can prove that split is the left inverse of merge.
mergesplitid : â {A} (xs : Stream A) â merge (split xs) â xs
hdâ (mergesplitid _) = refl
tlâ (mergesplitid xs) = mergesplitid (tl xs)
Old CoinductionÂ¶
Note
This is the old way of coinduction support in Agda. You are advised to use Coinductive Records instead.
To use coinduction it is recommended that you import the module Coinduction from the standard library. Coinductive types can then be defined by labelling coinductive occurrences using the delay operator â
:
data Coâ : Set where
zero : Coâ
suc : â Coâ â Coâ
The type â A
can be seen as a suspended computation of type A
. It comes with delay and force functions:
âŻ_ : â {a} {A : Set a} â A â â A
â : â {a} {A : Set a} â â A â A
Values of coinductive types can be constructed using corecursion, which does not need to terminate, but has to be productive. As an approximation to productivity the termination checker requires that corecursive definitions are guarded by coinductive constructors. As an example the infinite ânatural numberâ can be defined as follows:
inf : Coâ
inf = suc (âŻ inf)
The check for guarded corecursion is integrated with the check for sizechange termination, thus allowing interesting combinations of inductive and coinductive types. We can for instance define the type of stream processors, along with some functions:
 Infinite streams.
data Stream (A : Set) : Set where
_â·_ : (x : A) (xs : â (Stream A)) â Stream A
 A stream processor SP A B consumes elements of A and produces
 elements of B. It can only consume a finite number of Aâs before
 producing a B.
data SP (A B : Set) : Set where
get : (f : A â SP A B) â SP A B
put : (b : B) (sp : â (SP A B)) â SP A B
 The function eat is defined by an outer corecursion into Stream B
 and an inner recursion on SP A B.
eat : â {A B} â SP A B â Stream A â Stream B
eat (get f) (a â· as) = eat (f a) (â as)
eat (put b sp) as = b â· âŻ eat (â sp) as
 Composition of stream processors.
_â_ : â {A B C} â SP B C â SP A B â SP A C
get fâ â put x spâ = fâ x â â spâ
put x spâ â spâ = put x (âŻ (â spâ â spâ))
spâ â get fâ = get (Î» x â spâ â fâ x)
It is also possible to define âcoinductive familiesâ. It is recommended not to use the delay constructor (âŻ_
) in a constructorâs index expressions. The following definition of equality between coinductive ânatural numbersâ is discouraged:
data _ââ_ : Coâ â Coâ â Set where
zero : zero ââ zero
suc : â {m n} â â (m ââ n) â suc (âŻ m) ââ suc (âŻ n)
The recommended definition is the following one:
data _â_ : Coâ â Coâ â Set where
zero : zero â zero
suc : â {m n} â â (â m â â n) â suc m â suc n
The current implementation of coinductive types comes with some limitations.
CopatternsÂ¶
Consider the following record:
record Enumeration (A : Set) : Set where
constructor enumeration
field
start : A
forward : A â A
backward : A â A
This gives an interface that allows us to move along the elements of a
data type A
.
For example, we can get the âthirdâ element of a type A
:
open Enumeration
3rd : {A : Set} â Enumeration A â A
3rd e = forward e (forward e (forward e (start e)))
Or we can go back 2 positions starting from a given a
:
backward2 : {A : Set} â Enumeration A â A â A
backward2 e a = backward (backward a)
where
open Enumeration e
Now, we want to use these methods on natural numbers. For this, we need
a record of type Enumeration Nat
. Without copatterns, we would
specify all the fields in a single expression:
open Enumeration
enumNat : Enumeration Nat
enumNat = record {
start = 0
; forward = suc
; backward = pred
}
where
pred : Nat â Nat
pred zero = zero
pred (suc x) = x
testâ : 3rd enumNat âĄ 3
testâ = refl
testâ : backward2 enumNat 5 âĄ 3
testâ = refl
Note that if we want to use automated casesplitting and pattern matching to implement one of the fields, we need to do so in a separate definition.
With copatterns, we can define the fields of a record as separate declarations, in the same way that we would give different cases for a function:
open Enumeration
enumNat : Enumeration Nat
start enumNat = 0
forward enumNat n = suc n
backward enumNat zero = zero
backward enumNat (suc n) = n
The resulting behaviour is the same in both cases:
testâ : 3rd enumNat âĄ 3
testâ = refl
testâ : backward2 enumNat 5 âĄ 3
testâ = refl
Copatterns in function definitionsÂ¶
In fact, we do not need to start at 0
. We can allow the user to
specify the starting element.
Without copatterns, we just add the extra argument to the function declaration:
open Enumeration
enumNat : Nat â Enumeration Nat
enumNat initial = record {
start = initial
; forward = suc
; backward = pred
}
where
pred : Nat â Nat
pred zero = zero
pred (suc x) = x
testâ : 3rd (enumNat 10) âĄ 13
testâ = refl
With copatterns, the function argument must be repeated once for each field in the record:
open Enumeration
enumNat : Nat â Enumeration Nat
start (enumNat initial) = initial
forward (enumNat _) n = suc n
backward (enumNat _) zero = zero
backward (enumNat _) (suc n) = n
Mixing patterns and copatternsÂ¶
Instead of allowing an arbitrary value, we want to limit the user to
two choices: 0
or 42
.
Without copatterns, we would need an auxiliary definition to choose which value to start with based on the userprovided flag:
open Enumeration
if_then_else_ : {A : Set} â Bool â A â A â A
if true then x else _ = x
if false then _ else y = y
enumNat : Bool â Enumeration Nat
enumNat ahead = record {
start = if ahead then 42 else 0
; forward = suc
; backward = pred
}
where
pred : Nat â Nat
pred zero = zero
pred (suc x) = x
With copatterns, we can do the case analysis directly by pattern matching:
open Enumeration
enumNat : Bool â Enumeration Nat
start (enumNat true) = 42
start (enumNat false) = 0
forward (enumNat _) n = suc n
backward (enumNat _) zero = zero
backward (enumNat _) (suc n) = n
Tip
When using copatterns to define an element of a record type,
the fields of the record must be in scope. In the examples above,
we use open Enumeration
to bring the fields of the record into
scope.
Consider the first example:
enumNat : Enumeration Nat
start enumNat = 0
forward enumNat n = suc n
backward enumNat zero = zero
backward enumNat (suc n) = n
If the fields of the Enumeration
record are not in scope (in
particular, the start
field), then Agda will not be able to
figure out what the first copattern means:
Could not parse the lefthand side start enumNat
Operators used in the grammar:
None
when scope checking the lefthand side start enumNat in the
definition of enumNat
The solution is to open the record before using its fields:
open Enumeration
enumNat : Enumeration Nat
start enumNat = 0
forward enumNat n = suc n
backward enumNat zero = zero
backward enumNat (suc n) = n
Core languageÂ¶
Note
This is a stub
data Term = Var Int Elims
 Def QName Elims  ^ @f es@, possibly a delta/iotaredex
 Con ConHead Args  ^ @c vs@
 Lam ArgInfo (Abs Term)  ^ Terms are beta normal. Relevance is ignored
 Lit Literal
 Pi (Dom Type) (Abs Type)  ^ dependent or nondependent function space
 Sort Sort
 Level Level
 MetaV MetaId Elims
 DontCare Term
 ^ Irrelevant stuff in relevant position, but created
 in an irrelevant context.
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
commandlineoption or with {# OPTIONS cubical #}
at the top
of the file.
The cubical mode adds the following features to Agda:
 An interval type and path types
 Generalized transport (
transp
)  Partial elements
 Homogeneous composition (
hcomp
)  Glue types
 Higher inductive types
 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 builtin 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/cubicalagda/ (lagda rendered
version: https://ice1000.org/lagda/CubicalAgdaLiterate.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).
{# OPTIONS cubical #}
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.agdalib
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/cubicaldemo/. 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 :
SetÏ
(the reason this is in SetÏ
is because it doesnât support
the transp
and hcomp
operations). A variable i : I
intuitively corresponds to a point 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
.
i0 : I
i1 : I
Elements of the interval form a De Morgan algebra, with minimum
(â§
), maximum (âš
) and negation (~
).
_â§_ : I â I â I
_âš_ : I â I â I
~_ : 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.
i0 âš i = i
i âš i1 = i1
i âš j = j âš i
i0 â§ i = i0
i1 â§ i = i
i â§ j = j â§ i
~ (~ i) = i
i0 = ~ i1
~ (i âš j) = ~ i â§ ~ j
~ (i â§ j) = ~ i âš ~ j
The core idea of Homotopy Type Theory and Univalent Foundations is a
correspondence between paths (as in topology) and (proofrelevant)
equalities (as in MartinLĂ¶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 builtin
heterogeneous path types:
 PathP : â {â} (A : I â Set â) â A i0 â A i1 â Set â
 Non dependent path types
Path : â {â} (A : Set â) â A â A â Set â
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):
refl : â {â} {A : Set â} {x : A} â Path A x x
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:
refl : â {â} {A : Set â} {x : A} â Path A x x
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:
Square : â {â} {A : Set â} {x0 x1 y0 y1 : A} â
x0 âĄ x1 â y0 âĄ y1 â x0 âĄ y0 â x1 âĄ y1 â Set â
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:
sym : â {â} {A : Set â} {x y : A} â x âĄ y â y âĄ x
sym p = Î» i â p (~ i)
cong : â {â} {A : Set â} {x y : A} {B : A â Set â} (f : (a : A) â B a) (p : x âĄ y)
â PathP (Î» i â B (p i)) (f x) (f y)
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:
symInv : â {â} {A : Set â} {x y : A} (p : x âĄ y) â sym (sym p) âĄ p
symInv p = refl
congId : â {â} {A : Set â} {x y : A} (p : x âĄ y) â cong (Î» a â a) p âĄ p
congId p = refl
congComp : â {â} {A B C : Set â} (f : A â B) (g : B â C) {x y : A} (p : x âĄ y) â
cong (Î» a â g (f a)) p âĄ cong g (cong f p)
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:
funExt : â {â} {A : Set â} {B : A â Set â} {f g : (x : A) â B x} â
((x : A) â f x âĄ g x) â f âĄ g
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 builtin (generalized) transport operation and homogeneous composition operations. The transport operation is generalized in the sense that it lets us specify where it is the identity function.
transp : â {â} (A : I â Set â) (r : I) (a : A i0) â A i1
There is an additional side condition to be satisfied for transp A r
a
to typecheck, which is that A
has to be constant on
r
. This means that A
should be a constant function whenever
the constraint r = i1
is satisfied. This side condition is
vacuously true when r
is i0
, so there is nothing to check when
writing transp A i0 a
. However when r
is equal to i1
the
transp
function will compute as the identity function.
transp A i1 a = a
This requires A
to be constant for it to be welltyped.
We can use transp
to define regular transport:
transport : â {â} {A B : Set â} â A âĄ B â A â B
transport p a = transp (Î» i â p i) i0 a
By combining the transport and min operations we can define the induction principle for paths:
J : â {â} {A : Set â} {x : A} (P : â y â x âĄ y â Set â)
(d : P x refl) {y : A} (p : x âĄ y)
â P y p
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 patternmatching 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:
transportRefl : â {â} {A : Set â} (x : A) â transport refl x âĄ x
transportRefl {A = A} x i = transp (Î» _ â A) i x
JRefl : â {â} {A : Set â} {x : A} (P : â y â x âĄ y â Set â)
(d : P x refl) â J P d refl âĄ d
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 nary composition of higher
dimensional cubes.
Partial elementsÂ¶
In order to describe the homogeneous composition operations we need to
be able to write partially specified ndimensional 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
Ï
.
Partial : â {â} â I â Set â â SetÏ
PartialP : â {â} â (Ï : I) â Partial Ï (Set â) â SetÏ
There is a new form of pattern matching that can be used to introduce partial elements:
partialBool : â i â Partial (i âš ~ i) Bool
partialBool i (i = i0) = true
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.
partialBool' : â i â Partial (i âš ~ i) Bool
partialBool' i = Î» { (i = i0) â true
; (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):
partialBool'' : â i j â Partial (~ i âš i âš (i â§ j)) Bool
partialBool'' i j = Î» { (i = i1) â true
; (i = i1) (j = i1) â true
; (i = i0) â false }
Furthermore IsOne i0
is actually absurd
empty : {A : Set} â Partial i0 A
empty = Î» { () }
Cubical Agda also has cubical subtypes as in the CCHM type theory:
_[_âŠ_] : â {â} (A : Set â) (Ï : I) (u : Partial Ï A) â SetÏ
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 Ï
:
inc : â {â} {A : Set â} {Ï : I} (u : A) â A [ Ï âŠ (Î» _ â u) ]
One can also forget that a partial element agrees with u
on Ï
:
ouc : â {â} {A : Set â} {Ï : I} {u : Partial Ï A} â A [ Ï âŠ u ] â A
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.
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:
compPath : â {â} {A : Set â} {x y z : A} â x âĄ y â y âĄ z â x âĄ z
compPath {x = x} p q i = hcomp (Î» j â Î» { (i = i0) â x
; (i = i1) â q j })
(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:
x z
^ ^
 
x   q j
 
x > y
p i
In the drawing the direction i
goes lefttoright and j
goes
bottomtotop. 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
hfill : â {â} {A : Set â} {Ï : I}
(u : â i â Partial Ï A) (u0 : A [ Ï âŠ u i0 ])
(i : I) â A
hfill {Ï = Ï} u u0 i = hcomp (Î» j â Î» { (Ï = i1) â u (i â§ j) 1=1
; (i = i0) â ouc u0 })
(ouc 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
.
compPathRefl : â {â} {A : Set â} {x y : A} (p : x âĄ y) â compPath p refl âĄ p
compPathRefl {x = x} {y = y} p j i = hfill (Î» _ â Î» { (i = i0) â x
; (i = i1) â y })
(inc (p i))
(~ 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.
fiber : â {â} {A B : Set â} (f : A â B) (y : B) â Set â
fiber {A = A} f y = ÎŁ[ x â A ] f x âĄ y
isContr : â {â} â Set â â Set â
isContr A = ÎŁ[ x â A ] (â y â x âĄ y)
record isEquiv {â} {A B : Set â} (f : A â B) : Set â where
field
equivproof : (y : B) â isContr (fiber f y)
_â_ : â {â} (A B : Set â) â Set â
A â B = ÎŁ[ f â (A â B) ] (isEquiv f)
The simplest example of an equivalence is the identity function.
idfun : â {â} â (A : Set â) â A â A
idfun _ x = x
idIsEquiv : â {â} (A : Set â) â isEquiv (idfun A)
equivproof (idIsEquiv A) y =
((y , refl) , Î» z i â z .snd (~ i) , Î» j â z .snd (~ i âš j))
idEquiv : â {â} (A : Set â) â A â A
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
:
Glue : â {â â'} (A : Set â) {Ï : I}
â Partial Ï (ÎŁ[ T â Set â' ] T â A) â Set â'
These come with a constructor and eliminator:
glue : â {â â'} {A : Set â} {Ï : I} {Te : Partial Ï (ÎŁ[ T â Set â' ] T â A)}
â PartialP Ï T â A â Glue A Te
unglue : â {â â'} {A : Set â} (Ï : I) {Te : Partial Ï (ÎŁ[ T â Set â' ] T â A)}
â Glue A Te â A
Using Glue types we can turn an equivalence of types into a path as follows:
ua : â {â} {A B : Set â} â A â B â A âĄ B
ua {_} {A} {B} e i = Glue B (Î» { (i = i0) â (A , e)
; (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
:
uaÎČ : â {â} {A B : Set â} (e : A â B) (x : A) â transport (ua e) x âĄ e .fst x
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.
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:
data SÂč : Set where
base : SÂč
loop : base âĄ base
data Torus : Set where
point : Torus
line1 : point âĄ point
line2 : point âĄ point
square : PathP (Î» i â line1 i âĄ line1 i) line2 line2
Functions out of higher inductive types can then be defined using patternmatching:
t2c : Torus â SÂč Ă SÂč
t2c point = (base , base)
t2c (line1 i) = (loop i , base)
t2c (line2 j) = (base , loop j)
t2c (square i j) = (loop i , loop j)
c2t : SÂč Ă SÂč â Torus
c2t (base , base) = point
c2t (loop i , base) = line1 i
c2t (base , loop j) = line2 j
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).
c2t_bad : SÂč Ă SÂč â Torus
c2t_bad (base , base) = point
c2t_bad (loop i , base) = line2 i
c2t_bad (base , loop j) = line1 j
c2t_bad (loop i , loop j) = square i j
Functions defined by patternmatching on higher inductive types compute definitionally, for all constructors.
c2tt2c : â (t : Torus) â c2t (t2c t) âĄ t
c2tt2c point = refl
c2tt2c (line1 _) = refl
c2tt2c (line2 _) = refl
c2tt2c (square _ _) = refl
t2cc2t : â (p : SÂč Ă SÂč) â t2c (c2t p) âĄ p
t2cc2t (base , base) = refl
t2cc2t (base , loop _) = refl
t2cc2t (loop _ , base) = refl
t2cc2t (loop _ , loop _) = refl
By turning this isomorphism into an equivalence we get a direct proof that the torus is equal to two circles.
TorusâĄSÂčĂSÂč : Torus âĄ SÂč Ă SÂč
TorusâĄSÂčĂSÂč = isoToPath (iso t2c c2t t2cc2t c2tt2c)
Cubical Agda also supports parameterized and recursive higher inductive types, for example propositional truncation (squash types) is defined as:
data â„_â„ {â} (A : Set â) : Set â where
âŁ_âŁ : A â â„ A â„
squash : â (x y : â„ A â„) â x âĄ y
isProp : â {â} â Set â â Set â
isProp A = (x y : A) â x âĄ y
recPropTrunc : â {â} {A : Set â} {P : Set â} â isProp P â (A â P) â â„ A â„ â P
recPropTrunc Pprop f âŁ x âŁ = f x
recPropTrunc Pprop f (squash x y i) =
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.
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 type and the path type 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 in https://github.com/agda/cubical/blob/master/Cubical/Core/HoTTUF.agda 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.
module Cubical.Core.HoTTUF where
open import Cubical.Core.Id public
using ( _âĄ_  The identity type.
; refl  Unfortunately, pattern matching on refl is not available.
; J  Until it is, you have to use the induction principle J.
; transport  As in the HoTT Book.
; ap
; _â_
; _â»Âč
; _âĄâš_â©_  Standard equational reasoning.
; _â
; funExt  Function extensionality
 (can also be derived from univalence).
; ÎŁ  Sum type. Needed to define contractible types, equivalences
; _,_  and univalence.
; prâ  The eta rule is available.
; prâ
; isProp  The usual notions of proposition, contractible type, set.
; isContr
; isSet
; isEquiv  A map with contractible fibers
 (Voevodsky's version of the notion).
; _â_  The type of equivalences between two given types.
; EquivContr  A formulation of univalence.
; â„_â„  Propositional truncation.
; âŁ_âŁ  Map into the propositional truncation.
; â„â„isProp  A truncated type is a proposition.
; â„â„recursion  Nondependent elimination.
; â„â„induction  Dependent elimination.
)
In order to get access to only the HoTT/UF primitives start a file as follows:
{# OPTIONS cubical #}
open import Cubical.Core.HoTTUF
However, even though this interface exists it is still recommended
that one uses the cubical identity types unless one really need J
to compute on refl
. The reason for this is that the syntax for
path types does not work for the identity types, making many proofs
more involved as the only way to reason about them is using J
.
Furthermore, the path types satisfy many useful definitional
equalities that the identity types donât.
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.
The key file with primitives is Agda.Primitive.Cubical
. It exports
the following BUILTIN
, primitives and postulates:
{# BUILTIN INTERVAL I #}  I : SetÏ
{# BUILTIN IZERO i0 #}
{# BUILTIN IONE i1 #}
infix 30 primINeg
infixr 20 primIMin primIMax
primitive
primIMin : I â I â I  _â§_
primIMax : I â I â I  _âš_
primINeg : I â I  ~_
{# BUILTIN ISONE IsOne #}  IsOne : I â SetÏ
postulate
itIsOne : IsOne i1  1=1
IsOne1 : â i j â IsOne i â IsOne (primIMax i j)
IsOne2 : â i j â IsOne j â IsOne (primIMax i j)
{# BUILTIN ITISONE itIsOne #}
{# BUILTIN ISONE1 IsOne1 #}
{# BUILTIN ISONE2 IsOne2 #}
{# BUILTIN PARTIAL Partial #}
{# BUILTIN PARTIALP PartialP #}
postulate
isOneEmpty : â {a} {A : Partial i0 (Set a)} â PartialP i0 A
{# BUILTIN ISONEEMPTY isOneEmpty #}
primitive
primPOr : â {a} (i j : I) {A : Partial (primIMax i j) (Set a)}
â PartialP i (Î» z â A (IsOne1 i j z)) â PartialP j (Î» z â A (IsOne2 i j z))
â PartialP (primIMax i j) A
 Computes in terms of primHComp and primTransp
primComp : â {a} (A : (i : I) â Set (a i)) (Ï : I) â (â i â Partial Ï (A i)) â (a : A i0) â A i1
syntax primPOr p q u t = [ p âŠ u , q âŠ t ]
primitive
primTransp : â {a} (A : (i : I) â Set (a i)) (Ï : I) â (a : A i0) â A i1
primHComp : â {a} {A : Set a} {Ï : I} â (â i â Partial Ï A) â A â A
The Path types are exported by Agda.Builtin.Cubical.Path
:
postulate
PathP : â {â} (A : I â Set â) â A i0 â A i1 â Set â
{# BUILTIN PATHP PathP #}
infix 4 _âĄ_
_âĄ_ : â {â} {A : Set â} â A â A â Set â
_âĄ_ {A = A} = PathP (Î» _ â A)
{# BUILTIN PATH _âĄ_ #}
The Cubical subtypes are exported by Agda.Builtin.Cubical.Sub
:
{# BUILTIN SUB Sub #}
postulate
inc : â {â} {A : Set â} {Ï} (x : A) â Sub A Ï (Î» _ â x)
{# BUILTIN SUBIN inc #}
primitive
primSubOut : â {â} {A : Set â} {Ï : I} {u : Partial Ï A} â Sub _ Ï u â A
The Glue types are exported by Agda.Builtin.Cubical.Glue
:
record isEquiv {â â'} {A : Set â} {B : Set â'} (f : A â B) : Set (â â â') where
field
equivproof : (y : B) â isContr (fiber f y)
infix 4 _â_
_â_ : â {â â'} (A : Set â) (B : Set â') â Set (â â â')
A â B = ÎŁ (A â B) \ f â (isEquiv f)
equivFun : â {â â'} {A : Set â} {B : Set â'} â A â B â A â B
equivFun e = fst e
equivProof : â {la lt} (T : Set la) (A : Set lt) â (w : T â A) â (a : A)
â â Ï â (Partial Ï (fiber (w .fst) a)) â fiber (w .fst) a
equivProof A B w a Ï fb = contr' {A = fiber (w .fst) a} (w .snd .equivproof a) Ï fb
where
contr' : â {â} {A : Set â} â isContr A â (Ï : I) â (u : Partial Ï A) â A
contr' {A = A} (c , p) Ï u = hcomp (Î» i â Î» { (Ï = i1) â p (u 1=1) i
; (Ï = i0) â c }) c
{# BUILTIN EQUIV _â_ #}
{# BUILTIN EQUIVFUN equivFun #}
{# BUILTIN EQUIVPROOF equivProof #}
primitive
primGlue : â {â â'} (A : Set â) {Ï : I}
â (T : Partial Ï (Set â')) â (e : PartialP Ï (Î» o â T o â A))
â Set â'
prim^glue : â {â â'} {A : Set â} {Ï : I}
â {T : Partial Ï (Set â')} â {e : PartialP Ï (Î» o â T o â A)}
â PartialP Ï T â A â primGlue A T e
prim^unglue : â {â â'} {A : Set â} {Ï : I}
â {T : Partial Ï (Set â')} â {e : PartialP Ï (Î» o â T o â A)}
â primGlue A T e â A
primFaceForall : (I â I) â I
 pathToEquiv proves that transport is an equivalence (for details
 see Agda.Builtin.Cubical.Glue). This is needed internally.
{# BUILTIN PATHTOEQUIV pathToEquiv #}
Note that the Glue types are uncurried in agda/cubical
to make
them more pleasant to use:
Glue : â {â â'} (A : Set â) {Ï : I}
â (Te : Partial Ï (ÎŁ[ T â Set â' ] T â A))
â Set â'
Glue A Te = primGlue A (Î» x â Te x .fst) (Î» x â Te x .snd)
The Agda.Builtin.Cubical.Id
exports the cubical identity types:
postulate
Id : â {â} {A : Set â} â A â A â Set â
{# BUILTIN ID Id #}
{# BUILTIN CONID conid #}
primitive
primDepIMin : _
primIdFace : â {â} {A : Set â} {x y : A} â Id x y â I
primIdPath : â {â} {A : Set â} {x y : A} â Id x y â x âĄ y
primitive
primIdJ : â {â â'} {A : Set â} {x : A} (P : â y â Id x y â Set â') â
P x (conid i1 (Î» i â x)) â â {y} (p : Id x y) â P y p
primitive
primIdElim : â {a c} {A : Set a} {x : A}
(C : (y : A) â Id x y â Set c) â
((Ï : I) (y : A [ Ï âŠ (Î» _ â x) ])
(w : (x âĄ ouc y) [ Ï âŠ (Î» { (Ï = i1) â \ _ â x}) ]) â
C (ouc y) (conid Ï (ouc w))) â
{y : A} (p : Id x y) â C y p
Data TypesÂ¶
Simple datatypesÂ¶
Example datatypesÂ¶
In the introduction we already showed the definition of the data type of natural numbers (in unary notation):
data Nat : Set where
zero : Nat
suc : Nat â Nat
We give a few more examples. First the data type of truth values:
data Bool : Set where
true : Bool
false : Bool
The True
set represents the trivially true proposition:
data True : Set where
tt : True
The False
set has no constructor and hence no elements. It
represents the trivially false proposition:
data False : Set where
Another example is the data type of nonempty binary trees with natural numbers in the leaves:
data BinTree : Set where
leaf : Nat â BinTree
branch : BinTree â BinTree â BinTree
Finally, the data type of Brouwer ordinals:
data Ord : Set where
zeroOrd : Ord
sucOrd : Ord â Ord
limOrd : (Nat â Ord) â Ord
General formÂ¶
The general form of the definition of a simple datatype D
is the
following
data D : Setá”ą where
câ : Aâ
...
câ : Aâ
The name D
of the data type and the names câ
, âŠ, câ
of
the constructors must be new w.r.t. the current signature and context,
and the types Aâ
, âŠ, Aâ
must be function types ending in
D
, i.e. they must be of the form
(yâ : Bâ) â ... â (yâ : Bâ) â D
Parametrized datatypesÂ¶
Datatypes can have parameters. They are declared after the name of the datatype but before the colon, for example:
data List (A : Set) : Set where
[] : List A
_â·_ : A â List A â List A
Indexed datatypesÂ¶
In addition to parameters, datatypes can also have indices. In
contrast to parameters which are required to be the same for all
constructors, indices can vary from constructor to constructor. They
are declared after the colon as function arguments to Set
. For
example, fixedlength vectors can be defined by indexing them over
their length of type Nat
:
data Vector (A : Set) : Nat â Set where
[] : Vector A zero
_â·_ : {n : Nat} â A â Vector A n â Vector A (suc n)
Notice that the parameter A
is bound once for all constructors,
while the index {n : Nat}
must be bound locally in the constructor
_â·_
.
Indexed datatypes can also be used to describe predicates, for example
the predicate Even : Nat â Set
can be defined as follows:
data Even : Nat â Set where
evenzero : Even zero
evenplus2 : {n : Nat} â Even n â Even (suc (suc n))
General formÂ¶
The general form of the definition of a (parametrized, indexed)
datatype D
is the following
data D (xâ : Pâ) ... (xâ : Pâ) : (yâ : Qâ) â ... â (yâ : Qâ) â Set â where
câ : Aâ
...
câ : Aâ
where the types Aâ
, âŠ, Aâ
are function types of the form
(zâ : Bâ) â ... â (zâ : Bâ) â D xâ ... xâ tâ ... tâ
Strict positivityÂ¶
When defining a datatype D
, Agda poses an additional requirement
on the types of the constructors of D
, namely that D
may only
occur strictly positively in the types of their arguments.
Concretely, for a datatype with constructors câ : Aâ
, âŠ, câ :
Aâ
, Agda checks that each Aá”ą has the form
(yâ : Bâ) â ... â (yâ : Bâ) â D
where an argument types Bá”ą of the constructors is either
noninductive (a side condition) and does not mention
D
at all,or inductive and has the form
(zâ : Câ) â ... â (zâ : Câ) â D
where
D
must not occur in any Câ±Œ.
The strict positivity condition rules out declarations such as
data Bad : Set where
bad : (Bad â Bad) â Bad
 A B C
 A is in a negative position, B and C are OK
since there is a negative occurrence of Bad
in the type of the
argument of the constructor. (Note that the corresponding data type
declaration of Bad
is allowed in standard functional languages
such as Haskell and ML.).
Non strictlypositive declarations are rejected because they admit nonterminating functions.
If the positivity check is disabled, so that a similar declaration of
Bad
is allowed, it is possible to construct a term of the empty
type, even without recursion.
{# OPTIONS nopositivitycheck #}
data â„ : Set where
data Bad : Set where
bad : (Bad â â„) â Bad
selfapp : Bad â â„
selfapp (bad f) = f (bad f)
absurd : â„
absurd = selfapp (bad selfapp)
For more general information on termination see Termination Checking.
Flat ModalityÂ¶
The flat/crisp attribute @â/@flat
is an idempotent comonadic
modality modeled after Spatial Type Theory and Crisp Type Theory. It is similar to a necessity modality.
We can define â A
as a type for any (@â A : Set l)
via an
inductive definition:
data â {@â l : Level} (@â A : Set l) : Set l where
con : (@â x : A) â â A
counit : {@â l : Level} {@â A : Set l} â â A â A
counit (con x) = x
When trying to provide a @â
arguments only other @â
variables will be available, the others will be marked as @â€
in the context.
For example the following will not typecheck:
unit : {@â l : Level} {@â A : Set l} â A â â A
unit x = con x
Pattern Matching on @â
Â¶
Agda allows matching on @â
arguments by default.
When matching on a @â
argument the flat
status gets propagated to the arguments of the constructor
data _â_ (A B : Set) : Set where
inl : A â A â B
inr : B â A â B
flatsum : {@â A B : Set} â (@â x : A â B) â â A â â B
flatsum (inl x) = inl (con x)
flatsum (inr x) = inr (con x)
When refining @â
variables the equality also needs to be
provided as @â
flatsubst : {@â A : Set} {P : A â Set} (@â x y : A) (@â eq : x âĄ y) â P x â P y
flatsubst x .x refl p = p
if we simply had (eq : x âĄ y)
the code would be rejected.
Pattern matching on @â
arguments can be disabled entirely by using
the noflatsplit
flag
{# OPTIONS noflatsplit #}
Foreign Function InterfaceÂ¶
Compiler PragmasÂ¶
There are two backendgeneric pragmas used for the FFI:
{# COMPILE <Backend> <Name> <Text> #}
{# FOREIGN <Backend> <Text> #}
The COMPILE
pragma associates some information <Text>
with a
name <Name>
defined in the same module, and the FOREIGN
pragma
associates <Text>
with the current toplevel module. This
information is interpreted by the specific backend during compilation
(see below). These pragmas were added in Agda 2.5.3.
Haskell FFIÂ¶
Note
This section applies to the GHC Backend.
The FOREIGN
pragmaÂ¶
The GHC backend interprets FOREIGN
pragmas as inline Haskell code and can
contain arbitrary code (including import statements) that will be added to the
compiled module. For instance:
{# FOREIGN GHC import Data.Maybe #}
{# FOREIGN GHC
data Foo = Foo  Bar Foo
countBars :: Foo > Integer
countBars Foo = 0
countBars (Bar f) = 1 + countBars f
#}
The COMPILE
pragmaÂ¶
There are four forms of COMPILE
annotations recognized by the GHC backend
{# COMPILE GHC <Name> = <HaskellCode> #}
{# COMPILE GHC <Name> = type <HaskellType> #}
{# COMPILE GHC <Name> = data <HaskellData> (<HsCon1>  ..  <HsConN>) #}
{# COMPILE GHC <Name> as <HaskellName> #}
The first three tells the compiler how to compile a given Agda definition and the last exposes an Agda definition under a particular Haskell name allowing Agda libraries to be used from Haskell.
Using Haskell Types from AgdaÂ¶
In order to use a Haskell function from Agda its type must be mapped to an Agda
type. This mapping can be configured using the type
and data
forms of the
COMPILE
pragma.
Opaque typesÂ¶
Opaque Haskell types are exposed to Agda by postulating an Agda type and
associating it to the Haskell type using the type
form of the COMPILE
pragma:
{# FOREIGN GHC import qualified System.IO #}
postulate FileHandle : Set
{# COMPILE GHC FileHandle = type System.IO.Handle #}
This tells the compiler that the Agda type FileHandle
corresponds to the Haskell
type System.IO.Handle
and will enable functions using file handles to be used
from Agda.
Data typesÂ¶
Nonopaque Haskell data types can be mapped to Agda datatypes using the data
form
of the COMPILED
pragma:
data Maybe (A : Set) : Set where
nothing : Maybe A
just : A â Maybe A
{# COMPILE GHC Maybe = data Maybe (Nothing  Just) #}
The compiler checks that the types of the Agda constructors match the types of the corresponding Haskell constructors and that no constructors have been left out (on either side).
Builtin TypesÂ¶
The GHC backend compiles certain Agda builtin types to special Haskell types. The mapping between Agda builtin types and Haskell types is as follows:
Agda Builtin  Haskell Type 

NAT 
Integer 
INTEGER 
Integer 
STRING 
Data.Text.Text 
CHAR 
Char 
BOOL 
Bool 
FLOAT 
Double 
Warning
Haskell code manipulating Agda natural numbers as integers must take care to avoid negative values.
Warning
Agda FLOAT
values have only one logical NaN
value. At runtime,
there might be multiple different NaN
representations present. All
such NaN
values must be treated equal by FFI calls.
Using Haskell functions from AgdaÂ¶
Once a suitable mapping between Haskell types and Agda types has been set
up, Haskell functions whose types map to an Agda type can be exposed to Agda
code with a COMPILE
pragma:
open import Agda.Builtin.IO
open import Agda.Builtin.String
open import Agda.Builtin.Unit
{# FOREIGN GHC
import qualified Data.Text.IO as Text
import qualified System.IO as IO
#}
postulate
stdout : FileHandle
hPutStrLn : FileHandle â String â IO â€
{# COMPILE GHC stdout = IO.stdout #}
{# COMPILE GHC hPutStrLn = Text.hPutStrLn #}
The compiler checks that the type of the given Haskell code matches the
type of the Agda function. Note that the COMPILE
pragma only affects
the runtime behaviourâat typechecking time the functions are treated as
postulates.
Warning
It is possible to give Haskell definitions to defined (nonpostulate) Agda functions. In this case the Agda definition will be used at typechecking time and the Haskell definition at runtime. However, there are no checks to ensure that the Agda code and the Haskell code behave the same and discrepancies may lead to undefined behaviour.
This feature can be used to let you reason about code involving calls to Haskell functions under the assumption that you have a correct Agda model of the behaviour of the Haskell code.
Using Agda functions from HaskellÂ¶
Since Agda 2.3.4 Agda functions can be exposed to Haskell code using
the as
form of the COMPILE
pragma:
module IdAgda where
idAgda : â {A : Set} â A â A
idAgda x = x
{# COMPILE GHC idAgda as idAgdaFromHs #}
This tells the compiler that the Agda function idAgda
should be compiled
to a Haskell function called idAgdaFromHs
. Without this pragma, functions
are compiled to Haskell functions with unpredictable names and, as a result,
cannot be invoked from Haskell. The type of idAgdaFromHs
will be the translated
type of idAgda
.
The compiled and exported function idAgdaFromHs
can then be imported and
invoked from Haskell like this:
 file UseIdAgda.hs
module UseIdAgda where
import MAlonzo.Code.IdAgda (idAgdaFromHs)
 idAgdaFromHs :: () > a > a
idAgdaApplied :: a > a
idAgdaApplied = idAgdaFromHs ()
Polymorphic functionsÂ¶
Agda is a monomorphic language, so polymorphic functions are modeled as functions taking types as arguments. These arguments will be present in the compiled code as well, so when calling polymorphic Haskell functions they have to be discarded explicitly. For instance,
postulate
ioReturn : {A : Set} â A â IO A
{# COMPILE GHC ioReturn = \ _ x > return x #}
In this case compiled calls to ioReturn
will still have A
as an
argument, so the compiled definition ignores its first argument
and then calls the polymorphic Haskell return
function.
Levelpolymorphic typesÂ¶
Levelpolymorphic types face a similar problem to polymorphic functions. Since Haskell does not have universe levels the Agda type will have more arguments than the corresponding type. This can be solved by defining a Haskell type synonym with the appropriate number of phantom arguments. For instance
data Either {a b} (A : Set a) (B : Set b) : Set (a â b) where
left : A â Either A B
right : B â Either A B
{# FOREIGN GHC type AgdaEither a b = Either #}
{# COMPILE GHC Either = data AgdaEither (Left  Right) #}
Handling typeclass constraintsÂ¶
There is (currently) no way to map a Haskell type with type class constraints to an Agda type. This means that functions with class constraints cannot be used from Agda. However, this can be worked around by wrapping class constraints in Haskell data types, and providing Haskell functions using explicit dictionary passing.
For instance, suppose we have a simple GUI library in Haskell:
module GUILib where
class Widget w
setVisible :: Widget w => w > Bool > IO ()
data Window
instance Widget Window
newWindow :: IO Window
To use this library from Agda we first define a Haskell type for widget dictionaries and map this
to an Agda type Widget
:
{# FOREIGN GHC import GUILib #}
{# FOREIGN GHC data WidgetDict w = Widget w => WidgetDict #}
postulate
Widget : Set â Set
{# COMPILE GHC Widget = type WidgetDict #}
We can then expose setVisible
as an Agda function taking a Widget
instance argument:
postulate
setVisible : {w : Set} {{_ : Widget w}} â w â Bool â IO â€
{# COMPILE GHC setVisible = \ _ WidgetDict > setVisible #}
Note that the Agda Widget
argument corresponds to a WidgetDict
argument
on the Haskell side. When we match on the WidgetDict
constructor in the Haskell
code, the packed up dictionary will become available for the call to setVisible
.
The window type and functions are mapped as expected and we also add an Agda instance
packing up the Widget Window
Haskell instance into a WidgetDict
:
postulate
Window : Set
newWindow : IO Window
instance WidgetWindow : Widget Window
{# COMPILE GHC Window = type Window #}
{# COMPILE GHC newWindow = newWindow #}
{# COMPILE GHC WidgetWindow = WidgetDict #}
We can then write code like this:
openWindow : IO Window
openWindow = newWindow >>= Î» w â
setVisible w true >>= Î» _ â
return w
JavaScript FFIÂ¶
The JavaScript backend recognizes COMPILE
pragmas of the following form:
{# COMPILE JS <Name> = <JsCode> #}
where <Name>
is a postulate, constructor, or data type. The code for a data type is used to compile
pattern matching and should be a function taking a value of the data type and a table of functions
(corresponding to case branches) indexed by the constructor names. For instance, this is the compiled
code for the List
type, compiling lists to JavaScript arrays:
data List {a} (A : Set a) : Set a where
[] : List A
_â·_ : (x : A) (xs : List A) â List A
{# COMPILE JS List = function(x,v) {
if (x.length < 1) {
return v["[]"]();
} else {
return v["_â·_"](x[0], x.slice(1));
}
} #}
{# COMPILE JS [] = Array() #}
{# COMPILE JS _â·_ = function (x) { return function(y) { return Array(x).concat(y); }; } #}
Function DefinitionsÂ¶
IntroductionÂ¶
A function is defined by first declaring its type followed by a number of
equations called clauses. Each clause consists of the function being defined
applied to a number of patterns, followed by =
and a term called the
righthand side. For example:
not : Bool â Bool
not true = false
not false = true
Functions are allowed to call themselves recursively, for example:
twice : Nat â Nat
twice zero = zero
twice (suc n) = suc (suc (twice n))
General formÂ¶
The general form for defining a function is
f : (xâ : Aâ) â âŠ â (xâ : Aâ) â B
f pâ âŠ pâ = d
âŠ
f qâ âŠ qâ = e
where f
is a new identifier, pá”ą
and qá”ą
are patterns of type Aá”ą
,
and d
and e
are expressions.
The declaration above gives the identifier f
the type
(xâ : Aâ) â âŠ â (xâ : Aâ) â B
and f
is defined by the defining
equations. Patterns are matched from top to bottom, i.e., the first pattern
that matches the actual parameters is the one that is used.
By default, Agda checks the following properties of a function definition:
 The patterns in the lefthand side of each clause should consist only of constructors and variables.
 No variable should occur more than once on the lefthand side of a single clause.
 The patterns of all clauses should together cover all possible inputs of the function.
 The function should be terminating on all possible inputs, see Termination Checking.
Special patternsÂ¶
In addition to constructors consisting of constructors and variables, Agda supports two special kinds of patterns: dot patterns and absurd patterns.
Dot patternsÂ¶
A dot pattern (also called inaccessible pattern) can be used when
the only typecorrect value of the argument is determined by the
patterns given for the other arguments.
The syntax for a dot pattern is .t
.
As an example, consider the datatype Square
defined as follows
data Square : Nat â Set where
sq : (m : Nat) â Square (m * m)
Suppose we want to define a function root : (n : Nat) â Square n â Nat
that
takes as its arguments a number n
and a proof that it is a square, and
returns the square root of that number. We can do so as follows:
root : (n : Nat) â Square n â Nat
root .(m * m) (sq m) = m
Notice that by matching on the argument of type Square n
with the constructor
sq : (m : Nat) â Square (m * m)
, n
is forced to be equal to m * m
.
In general, when matching on an argument of type D iâ âŠ iâ
with a constructor
c : (xâ : Aâ) â âŠ â (xâ : Aâ) â D jâ âŠ jâ
, Agda will attempt to unify
iâ âŠ iâ
with jâ âŠ jâ
. When the unification algorithm instantiates a
variable x
with value t
, the corresponding argument of the function
can be replaced by a dot pattern .t
. Using a dot pattern is optional, but
can help readability. The following are also legal definitions of
root
:
Since Agda 2.4.2.4:
rootâ : (n : Nat) â Square n â Nat
rootâ _ (sq m) = m
Since Agda 2.5.2:
rootâ : (n : Nat) â Square n â Nat
rootâ n (sq m) = m
In the case of rootâ
, n
evaluates to m * m
in the body of the
function and is thus equivalent to
rootâ : (n : Nat) â Square n â Nat
rootâ _ (sq m) = let n = m * m in m
Absurd patternsÂ¶
Absurd patterns can be used when none of the constructors for a particular
argument would be valid. The syntax for an absurd pattern is ()
.
As an example, if we have a datatype Even
defined as follows
data Even : Nat â Set where
evenzero : Even zero
evenplus2 : {n : Nat} â Even n â Even (suc (suc n))
then we can define a function onenoteven : Even 1 â â„
by using an absurd
pattern:
onenoteven : Even 1 â â„
onenoteven ()
Note that if the lefthand side of a clause contains an absurd pattern, its righthand side must be omitted.
In general, when matching on an argument of type D iâ âŠ iâ
with an absurd
pattern, Agda will attempt for each constructor
c : (xâ : Aâ) â âŠ â (xâ : Aâ) â D jâ âŠ jâ
of the datatype D
to unify
iâ âŠ iâ
with jâ âŠ jâ
. The absurd pattern will only be accepted if all
of these unifications end in a conflict.
AspatternsÂ¶
Aspatterns (or @patterns
) can be used to name a pattern. The name has the
same scope as normal pattern variables (i.e. the righthand side, where clause,
and dot patterns). The name reduces to the value of the named pattern. For example:
module _ {A : Set} (_<_ : A â A â Bool) where
merge : List A â List A â List A
merge xs [] = xs
merge [] ys = ys
merge xs@(x â· xsâ) ys@(y â· ysâ) =
if x < y then x â· merge xsâ ys
else y â· merge xs ysâ
Aspatterns are properly supported since Agda 2.5.2.
Case treesÂ¶
Internally, Agda represents function definitions as case trees. For example, a function definition
max : Nat â Nat â Nat
max zero n = n
max m zero = m
max (suc m) (suc n) = suc (max m n)
will be represented internally as a case tree that looks like this:
max m n = case m of
zero â n
suc m' â case n of
zero â suc m'
suc n' â suc (max m' n')
Note that because Agda uses this representation of the function
max
, the clause max m zero = m
does not hold definitionally
(i.e. as a reduction rule). If you would try to prove that this
equation holds, you would not be able to write refl
:
data _âĄ_ {A : Set} (x : A) : A â Set where
refl : x âĄ x
 Does not work!
lemma : (m : Nat) â max m zero âĄ m
lemma = refl
Clauses which do not hold definitionally are usually (but not always) the result of writing clauses by hand instead of using Agdaâs case split tactic. These clauses are highlighted by Emacs.
The exactsplit
commandline and pragma option causes Agda to raise an error whenever a
clause in a definition by pattern matching cannot be made to hold
definitionally. Specific clauses can be excluded from this check by
means of the {# CATCHALL #}
pragma.
For instance, the above definition of max
will be rejected when
using the exactsplit
flag because its second clause does not to
hold definitionally.
When using the exactsplit
flag, catchall clauses have to be
marked as such, for instance:
eq : Nat â Nat â Bool
eq zero zero = true
eq (suc m) (suc n) = eq m n
{# CATCHALL #}
eq _ _ = false
The noexactsplit
commandline and pragma option can be used to override a global
exactsplit
in a file, by adding a pragma
{# OPTIONS noexactsplit #}
. This option is enabled by
default.
Function TypesÂ¶
Function types are written (x : A) â B
, or in the case of nondependent functions simply A â B
. For instance, the type of the addition function for natural numbers is:
Nat â Nat â Nat
and the type of the addition function for vectors is:
(A : Set) â (n : Nat) â (u : Vec A n) â (v : Vec A n) â Vec A n
where Set
is the type of sets and Vec A n
is the type of vectors with n
elements of type A
. Arrows between consecutive hypotheses of the form (x : A)
may also be omitted, and (x : A) (y : A)
may be shortened to (x y : A)
:
(A : Set) (n : Nat)(u v : Vec A n) â Vec A n
Functions are constructed by lambda abstractions, which can be either typed or untyped. For instance, both expressions below have type (A : Set) â A â A
(the second expression checks against other types as well):
exampleâ = \ (A : Set)(x : A) â x
exampleâ = \ A x â x
You can also use the Unicode symbol Î»
(type â\lambdaâ in the Emacs Agda mode) instead of \\
.
The application of a function f : (x : A) â B
to an argument a : A
is written f a
and the type of this is B[x := a]
.
Notational conventionsÂ¶
Function types:
propâ : ((x : A) (y : B) â C) isthesameas ((x : A) â (y : B) â C)
propâ : ((x y : A) â C) isthesameas ((x : A)(y : A) â C)
propâ : (forall (x : A) â C) isthesameas ((x : A) â C)
propâ : (forall x â C) isthesameas ((x : _) â C)
propâ
: (forall x y â C) isthesameas (forall x â forall y â C)
You can also use the Unicode symbol â
(type â\allâ in the Emacs Agda mode) instead of forall
.
Functional abstraction:
(\x y â e) isthesameas (\x â (\y â e))
Functional application:
(f a b) isthesameas ((f a) b)
Generalization of Declared VariablesÂ¶
OverviewÂ¶
Since version 2.6.0, Agda supports implicit generalization over variables in types.
Variables to be generalized over must be declared with their types in a variable
block. For example:
variable
â : Level
n m : Nat
data Vec (A : Set â) : Nat â Set â where
[] : Vec A 0
_â·_ : A â Vec A n â Vec A (suc n)
Here the parameter â
and the n
in the type of _â·_
are not bound explicitly,
but since they are declared as generalizable variables, bindings for them are inserted
automatically. The level â
is added as a parameter to the datatype and n
is added
as an argument to _â·_
. The resulting declaration is
data Vec {â : Set} (A : Set â) : Nat â Set â where
[] : Vec A 0
_â·_ : {n : Nat} â A â Vec A n â Vec A (suc n)
See Placement of generalized bindings below for more details on where bindings are inserted.
Variables are generalized in toplevel type signatures, module telescopes, and record and datatype parameter telescopes.
Issues related to this feature are marked with generalize in the issue tracker.
Nested generalizationÂ¶
When generalizing a variable, any generalizable variables in its type are also generalized
over. For instance, you can declare A
to be a type at some level â
as
variable
A : Set â
Now if A
is mentioned in a type, the level â
will also be generalized over:
 id : {A.â : Level} {A : Set â} â A â A
id : A â A
id x = x
The nesting can be arbitrarily deep, so
variable
x : A
reflâČ : x âĄ x
reflâČ = refl
expands to
reflâČ : {x.A.â : Level} {x.A : Set x.A.â} {x : x.A} â x âĄ x
See Naming of nested variables below for how the names are chosen.
Nested variables are not necessarily generalized over. In this example, if the universe
level of A
is fixed there is nothing to generalize:
postulate
 pure : {A : Set} {F : Set â Set} â A â F A
pure : {F : Set â Set} â A â F A
See Generalization over unsolved metavariables for more details.
Note
Nested generalized variables are local to each variable, so if you declare
variable
B : Set â
then A
and B
can still be generalized at different levels. For instance,
 _$_ : {A.â : Level} {A : Set A.â} {B.â : Level} {B : Set B.â} â (A â B) â A â B
_$_ : (A â B) â A â B
f $ x = f x
Generalization over unsolved metavariablesÂ¶
Generalization over nested variables is implemented by creating a metavariable for each
nested variable and generalize over any such meta that is still unsolved after type
checking. This is what makes the pure
example from the previous section work: the
metavariable created for â
is solved to level 0 and is thus not generalized over.
A typical case where this happens is when you have dependencies between different nested variables. For instance:
postulate
Con : Set
variable
Î Î Î : Con
postulate
Sub : Con â Con â Set
idS : Sub Î Î
_â_ : Sub Î Î â Sub Î Î â Sub Î Î
variable
ÎŽ Ï Îł : Sub Î Î
postulate
assoc : ÎŽ â (Ï â Îł) âĄ (ÎŽ â Ï) â Îł
In the type of assoc
each substitution gets two nested variable metas for their contexts,
but the type of _â_
requires the contexts of its arguments to match up, so some of
these metavariables are solved. The resulting type is
assoc : {ÎŽ.Î ÎŽ.Î : Con} {ÎŽ : Sub ÎŽ.Î ÎŽ.Î} {Ï.Î : Con} {Ï : Sub ÎŽ.Î Ï.Î}
{Îł.Î : Con} {Îł : Sub Ï.Î Îł.Î} â (ÎŽ â (Ï â Îł)) âĄ ((ÎŽ â Ï) â Îł)
where we can see from the names that Ï.Î
was unified with ÎŽ.Î
and Îł.Î
with
Ï.Î
. In general, when unifying two metavariables the âyoungestâ one is eliminated which
is why ÎŽ.Î
and Ï.Î
are the ones that remain in the type.
If a metavariable for a nested generalizable variable is partially solved, the leftover metas are generalized over. For instance,
variable
xs : Vec A n
head : Vec A (suc n) â A
head (x â· _) = x
 lemma : {n : Nat} {xs : Vec Nat (suc n)} â head xs âĄ 1 â (0 < sum xs) âĄ true
lemma : head xs âĄ 1 â (0 < sum xs) âĄ true
In the type of lemma
a metavariable is created for the length of xs
, which
the application head xs
refines to suc n
, for some new metavariable n
.
Since there are no further constraints on n
, itâs generalized over, creating the
type given in the comment.
Note
Only metavariables originating from nested variables are generalized over. An exception
to this is in variable
blocks where all unsolved metas are turned into nested variables.
This means writing
variable
A : Set _
is equivalent to A : Set â
up to naming of the nested variable (see below).
Naming of nested variablesÂ¶
The general naming scheme for nested generalized variables is
parentVar.nestedVar
. So, in the case of the identity function
id : A â A
expanding to
id : {A.â : Level} {A : Set â} â A â A
the name of the level variable is A.â
since the name of the nested variable is
â
and its parent is the named variable A
. For multiple levels of nesting the
parent can be another nested variable as in the reflâČ
case above
reflâČ : {x.A.â : Level} {x.A : Set x.A.â} {x : x.A} â x âĄ x
If a variable comes from a free unsolved metavariable in a variable
block
(see this note), its name is chosen as follows:
 If it is a labelled argument to a function, the label is used as the name,
 otherwise the name is its lefttoright index (starting at 1) in the list of unnamed variables in the type.
It is then given a hierarchical name based on the named variable whose type it occurs in. For example,
postulate
V : (A : Set) â Nat â Set
P : V A n â Set
variable
v : V _ _
postulate
thm : P v
Here there are two unnamed variables in the type of v
, namely the two arguments to V
.
The first argument has the label A
in the definition of V
, so this variable gets the name
v.A
. The second argument has no label and thus gets the name v.2
since it is the second
unnamed variable in the type of v
.
If the variable comes from a partially instantiated nested variable the name of the metavariable is used unqualified.
Note
Currently it is not allowed to use hierarchical names when giving parameters to functions, see Issue #3208.
Placement of generalized bindingsÂ¶
The following rules are used to place generalized variables:
 Generalized variables are placed at the front of the type signature or telescope.
 Variables mentioned eariler are placed before variables mentioned later, where nested variables count as being mentioned together with their parent.
Note
This means that an implicitly quantified variable cannot depend on an explicitly quantified one. See Issue #3352 for the feature request to lift this restriction.
Indexed datatypesÂ¶
When generalizing datatype parameters and indicies a variable is turned into an index if it is only mentioned in indices and into a parameter otherwise. For instance,
data All (P : A â Set) : Vec A n â Set where
[] : All P []
_â·_ : P x â All P xs â All P (x â· xs)
Here A
is generalized as a parameter and n
as an index. That is, the
resulting signature is
data All {A : Set} (P : A â Set) : {n : Nat} â Vec A n â Set where
Instance and irrelevant variablesÂ¶
Generalized variables are introduced as implicit arguments by default, but this can be changed to instance arguments or irrelevant arguments by annotating the declaration of the variable:
record Eq (A : Set) : Set where
field eq : A â A â Bool
variable
{{EqA}} : Eq A  generalized as an instance argument
.ignore : A  generalized as an irrelevant (implicit) argument
Variables are never generalized as explicit arguments.
Importing and exporting variablesÂ¶
Generalizable variables are treated in the same way as other declared symbols
(functions, datatypes, etc) and use the same mechanisms for importing and exporting
between modules. This means that unless marked private
they are exported from a
module.
InteractionÂ¶
When developing types interactively, generalizable variables can be used in holes if they have already been generalized, but it is not possible to introduce new generalizations interactively. For instance,
works : (A â B) â Vec A n â Vec B {!n!}
fails : (A â B) â Vec A {!n!} â Vec B {!n!}
In works
you can give n
in the hole, since a binding for n
has been introduced
by its occurrence in the argument vector. In fails
on the other hand, there is no reference
to n
so neither hole can be filled interactively.
Implicit ArgumentsÂ¶
It is possible to omit terms that the type checker can figure out for
itself, replacing them by _
.
If the type checker cannot infer the value of an _
it will report
an error.
For instance, for the polymorphic identity function
id : (A : Set) â A â A
the first argument can be inferred from the type of the second argument,
so we might write id _ zero
for the application of the identity function to zero
.
We can even write this function application without the first argument. In that case we declare an implicit function space:
id : {A : Set} â A â A
and then we can use the notation id zero
.
Another example:
_==_ : {A : Set} â A â A â Set
subst : {A : Set} (C : A â Set) {x y : A} â x == y â C x â C y
Note how the first argument to _==_
is left implicit.
Similarly, we may leave out the implicit arguments A
, x
, and y
in an
application of subst
.
To give an implicit argument explicitly, enclose it in curly braces.
The following two expressions are equivalent:
x1 = subst C eq cx
x2 = subst {_} C {_} {_} eq cx
It is worth noting that implicit arguments are also inserted at the end of an application,
if it is required by the type.
For example, in the following, y1
and y2
are equivalent.
y1 : a == b â C a â C b
y1 = subst C
y2 : a == b â C a â C b
y2 = subst C {_} {_}
Implicit arguments are inserted eagerly in lefthand sides so y3
and y4
are equivalent. An exception is when no type signature is given, in which case
no implicit argument insertion takes place. Thus in the definition of y5
the only implicit is the A
argument of subst
.
y3 : {x y : A} â x == y â C x â C y
y3 = subst C
y4 : {x y : A} â x == y â C x â C y
y4 {x} {y} = subst C {_} {_}
y5 = subst C
It is also possible to write lambda abstractions with implicit arguments. For
example, given id : (A : Set) â A â A
, we can define the identity function with
implicit type argument as
idâ = Î» {A} â id A
Implicit arguments can also be referred to by name,
so if we want to give the expression e
explicitly for y
without giving a value for x
we can write
subst C {y = e} eq cx
In rare circumstances it can be useful to separate the name used to give an argument by name from the name of the bound variable, for instance if the desired name shadows an existing name. To do this you write
idâ : {A = X : Set} â X â X  name of bound variable is X
idâ x = x
useidâ : (Y : Set) â Y â Y
useidâ Y = idâ {A = Y}  but the label is A
Labeled bindings must appear by themselves when typed, so the type Set
needs to
be repeated in this example:
const : {A = X : Set} {B = Y : Set} â A â B â A
const x y = x
When constructing implicit function spaces the implicit argument can be omitted,
so both expressions below are valid expressions of type {A : Set} â A â A
:
z1 = Î» {A} x â x
z2 = Î» x â x
The â
(or forall
) syntax for function types also has implicit variants:
â : (â {x : A} â B) isthesameas ({x : A} â B)
âĄ : (â {x} â B) isthesameas ({x : _} â B)
âą : (â {x y} â B) isthesameas (â {x} â â {y} â B)
In very special situations it makes sense to declare unnamed hidden arguments
{A} â B
. In the following example
, the hidden argument to scons
of type
zero â€ zero
can be solved by Î·expansion, since this type reduces to â€
.
data â„ : Set where
_â€_ : Nat â Nat â Set
zero â€ _ = â€
suc m â€ zero = â„
suc m â€ suc n = m â€ n
data SList (bound : Nat) : Set where
[] : SList bound
scons : (head : Nat) â {head â€ bound} â (tail : SList head) â SList bound
example : SList zero
example = scons zero []
There are no restrictions on when a function space can be implicit. Internally, explicit and implicit function spaces are treated in the same way. This means that there are no guarantees that implicit arguments will be solved. When there are unsolved implicit arguments the type checker will give an error message indicating which application contains the unsolved arguments. The reason for this liberal approach to implicit arguments is that limiting the use of implicit argument to the cases where we guarantee that they are solved rules out many useful cases in practice.
Tactic argumentsÂ¶
You can declare tactics to be used to solve a particular implicit argument using
the @(tactic t)
attribute, where t : Term â TC â€
. For instance:
cleversearch : Term â TC â€
cleversearch hole = unify hole (lit (nat 17))
thebestnumber : {@(tactic cleversearch) n : Nat} â Nat
thebestnumber {n} = n
check : thebestnumber âĄ 17
check = refl
The tactic can be an arbitrary term of the right type and may depend on previous arguments to the function:
default : {A : Set} â A â Term â TC â€
default x hole = bindTC (quoteTC x) (unify hole)
search : (depth : Nat) â Term â TC â€
example : {@(tactic default 10) depth : Nat}
{@(tactic search depth) proof : Proof} â
Goal
MetavariablesÂ¶
UnificationÂ¶
Instance ArgumentsÂ¶
Instance arguments are a special kind of implicit arguments that get solved by a special instance resolution algorithm, rather than by the unification algorithm used for normal implicit arguments. Instance arguments are the Agda equivalent of Haskell type class constraints and can be used for many of the same purposes.
An instance argument will be resolved if its type is a named type (i.e. a data type or record type) or a variable type (i.e. a previously bound variable of type Set â), and a unique instance of the required type can be built from declared instances and the current context.
UsageÂ¶
Instance arguments are enclosed in double curly braces {{ }}
, e.g. {{x : T}}
.
Alternatively they can be enclosed, with proper spacing, e.g. âŠ x : T âŠ
, in the
unicode braces âŠ âŠ
(U+2983
and U+2984
, which can be typed as
\{{
and \}}
in the Emacs mode).
For instance, given a function _==_
_==_ : {A : Set} {{eqA : Eq A}} â A â A â Bool
for some suitable type Eq
, you might define
elem : {A : Set} {{eqA : Eq A}} â A â List A â Bool
elem x (y â· xs) = x == y  elem x xs
elem x [] = false
Here the instance argument to _==_
is solved by the corresponding argument
to elem
. Just like ordinary implicit arguments, instance arguments can be
given explicitly. The above definition is equivalent to
elem : {A : Set} {{eqA : Eq A}} â A â List A â Bool
elem {{eqA}} x (y â· xs) = _==_ {{eqA}} x y  elem {{eqA}} x xs
elem x [] = false
A very useful function that exploits this is the function it
which lets you
apply instance resolution to solve an arbitrary goal:
it : â {a} {A : Set a} â {{A}} â A
it {{x}} = x
As the last example shows, the name of the instance argument can be omitted in the type signature:
_==_ : {A : Set} â {{Eq A}} â A â A â Bool
Defining type classesÂ¶
The type of an instance argument should have the form {Î} â C vs
,
where C
is a postulated name, a bound variable, or the name of a
data or record type, and {Î}
denotes an arbitrary number of
implicit or instance arguments (see Dependent instances below
for an example where {Î}
is nonempty).
Instances with explicit arguments are also accepted but will not be considered as instances because the value of the explicit arguments cannot be derived automatically. Having such an instance has no effect and thus raises a warning.
Instance arguments whose types end in any other type are currently also accepted but cannot be resolved by instance search, so they must be given by hand. For this reason it is not recommended to use such instance arguments. Doing so will also raise a warning.
Other than that there are no requirements on the type of an instance argument. In particular, there is no special declaration to say that a type is a âtype classâ. Instead, Haskellstyle type classes are usually defined as record types. For instance,
record Monoid {a} (A : Set a) : Set a where
field
mempty : A
_<>_ : A â A â A
In order to make the fields of the record available as functions taking instance arguments you can use the special module application
open Monoid {{...}} public
This will bring into scope
mempty : â {a} {A : Set a} â {{Monoid A}} â A
_<>_ : â {a} {A : Set a} â {{Monoid A}} â A â A â A
Superclass dependencies can be implemented using Instance fields.
See Module application and Record modules for details about how
the module application is desugared. If defined by hand, mempty
would be
mempty : â {a} {A : Set a} â {{Monoid A}} â A
mempty {{mon}} = Monoid.mempty mon
Although record types are a natural fit for Haskellstyle type classes, you can use instance arguments with data types to good effect. See the Examples below.
Declaring instancesÂ¶
As seen above, instance arguments in the context are available when solving
instance arguments, but you also need to be able to
define toplevel instances for concrete types. This is done using the
instance
keyword, which starts a block in
which each definition is marked as an instance available for instance
resolution. For example, an instance Monoid (List A)
can be defined as
instance
ListMonoid : â {a} {A : Set a} â Monoid (List A)
ListMonoid = record { mempty = []; _<>_ = _++_ }
Or equivalently, using copatterns:
instance
ListMonoid : â {a} {A : Set a} â Monoid (List A)
mempty {{ListMonoid}} = []
_<>_ {{ListMonoid}} xs ys = xs ++ ys
Toplevel instances must target a named type (Monoid
in this case), and
cannot be declared for types in the context.
You can define local instances in letexpressions in the same way as a toplevel instance. For example:
mconcat : â {a} {A : Set a} â {{Monoid A}} â List A â A
mconcat [] = mempty
mconcat (x â· xs) = x <> mconcat xs
sum : List Nat â Nat
sum xs =
let instance
NatMonoid : Monoid Nat
NatMonoid = record { mempty = 0; _<>_ = _+_ }
in mconcat xs
Instances can have instance arguments themselves, which will be filled in recursively during instance resolution. For instance,
record Eq {a} (A : Set a) : Set a where
field
_==_ : A â A â Bool
open Eq {{...}} public
instance
eqList : â {a} {A : Set a} â {{Eq A}} â Eq (List A)
_==_ {{eqList}} [] [] = true
_==_ {{eqList}} (x â· xs) (y â· ys) = x == y && xs == ys
_==_ {{eqList}} _ _ = false
eqNat : Eq Nat
_==_ {{eqNat}} = natEquals
ex : Bool
ex = (1 â· 2 â· 3 â· []) == (1 â· 2 â· [])  false
Note the two calls to _==_
in the righthand side of the second clause. The
first uses the Eq A
instance and the second uses a recursive call to
eqList
. In the example ex
, instance resolution, needing a value of type Eq
(List Nat)
, will try to use the eqList
instance and find that it needs an
instance argument of type Eq Nat
, it will then solve that with eqNat
and return the solution eqList {{eqNat}}
.
Note
At the moment there is no termination check on instances, so it is possible
to construct nonsensical instances like
loop : â {a} {A : Set a} â {{Eq A}} â Eq A
.
To prevent looping in cases like this, the search depth of instance search
is limited, and once the maximum depth is reached, a type error will be
thrown. You can set the maximum depth using the instancesearchdepth
flag.
Restricting instance searchÂ¶
To restrict an instance to the current module, you can mark it as private. For instance,
record Default (A : Set) : Set where
field default : A
open Default {{...}} public
module M where
private
instance
defaultNat : Default Nat
defaultNat .default = 6
testâ : Nat
testâ = default
_ : testâ âĄ 6
_ = refl
open M
instance
defaultNat : Default Nat
defaultNat .default = 42
testâ : Nat
testâ = default
_ : testâ âĄ 42
_ = refl
Constructor instancesÂ¶
Although instance arguments are most commonly used for record types,
mimicking Haskellstyle type classes, they can also be used with data
types. In this case you often want the constructors to be instances,
which is achieved by declaring them inside an instance
block. Constructors can only be declared as instances if all their
arguments are implicit or instance arguments. See
Instance resolution below for the details.
A simple example of a constructor that can be made an instance is the reflexivity constructor of the equality type:
data _âĄ_ {a} {A : Set a} (x : A) : A â Set a where
instance refl : x âĄ x
This allows trivial equality proofs to be inferred by instance resolution,
which can make working with functions that have preconditions less of a burden.
As an example, here is how one could use this to define a function that takes a
natural number and gives back a Fin n
(the type of naturals smaller than
n
):
data Fin : Nat â Set where
zero : â {n} â Fin (suc n)
suc : â {n} â Fin n â Fin (suc n)
mkFin : â {n} (m : Nat) â {{suc m  n âĄ 0}} â Fin n
mkFin {zero} m {{}}
mkFin {suc n} zero = zero
mkFin {suc n} (suc m) = suc (mkFin m)
five : Fin 6
five = mkFin 5  OK
In the first clause of mkFin
we use an absurd pattern to discharge the impossible assumption suc m âĄ
0
. See the next section for
another example of constructor instances.
Record fields can also be declared instances, with the effect that the corresponding projection function is considered a toplevel instance.
Overlapping instancesÂ¶
By default, Agda does not allow overlapping instances. Two instances are defined to overlap if they could both solve the instance goal when given appropriate solutions for their recursive (instance) arguments.
For example, in code below, the instances zero and suc overlap for the goal exâ, because either one of them can be used to solve the goal when given appropriate arguments, hence instance search fails.
infix 4 _â_
data _â_ {A : Set} (x : A) : List A â Set where
instance
zero : â {xs} â x â x â· xs
suc : â {y xs} â {{x â xs}} â x â y â· xs
exâ : 1 â 1 â· 2 â· 3 â· 4 â· []
exâ = it  overlapping instances
Overlapping instances can be enabled via the overlappinginstances
flag. Be aware that enabling this flag might lead to an exponential
slowdown in instance resolution and possibly (apparent) looping
behaviour.
ExamplesÂ¶
Dependent instancesÂ¶
Consider a variant on the Eq
class where the equality function produces a
proof in the case the arguments are equal:
record Eq {a} (A : Set a) : Set a where
field
_==_ : (x y : A) â Maybe (x âĄ y)
open Eq {{...}} public
A simple booleanvalued equality function is problematic for types with dependencies, like the ÎŁtype
data ÎŁ {a b} (A : Set a) (B : A â Set b) : Set (a â b) where
_,_ : (x : A) â B x â ÎŁ A B
since given two pairs x , y
and xâ , yâ
, the types of the second
components y
and yâ
can be completely different and not admit an
equality test. Only when x
and xâ
are really equal can we hope to
compare y
and yâ
. Having the equality function return a proof means
that we are guaranteed that when x
and xâ
compare equal, they really
are equal, and comparing y
and yâ
makes sense.
An Eq
instance for ÎŁ
can be defined as follows:
instance
eqÎŁ : â {a b} {A : Set a} {B : A â Set b} â {{Eq A}} â {{â {x} â Eq (B x)}} â Eq (ÎŁ A B)
_==_ {{eqÎŁ}} (x , y) (xâ , yâ) with x == xâ
_==_ {{eqÎŁ}} (x , y) (xâ , yâ)  nothing = nothing
_==_ {{eqÎŁ}} (x , y) (.x , yâ)  just refl with y == yâ
_==_ {{eqÎŁ}} (x , y) (.x , yâ)  just refl  nothing = nothing
_==_ {{eqÎŁ}} (x , y) (.x , .y)  just refl  just refl = just refl
Note that the instance argument for B
states that there should be
an Eq
instance for B x
, for any x : A
. The argument x
must be implicit, indicating that it needs to be inferred by
unification whenever the B
instance is used. See
Instance resolution below for more details.
Instance resolutionÂ¶
Given a goal that should be solved using instance resolution we proceed in the following four stages:
 Verify the goal
 First we check that the goal type has the right shape to be solved
by instance resolution. It should be of the form
{Î} â C vs
, where the target typeC
is a variable from the context or the name of a data or record type, and{Î}
denotes a telescope of implicit or instance arguments. If this is not the case instance resolution fails with an error message[1].  Find candidates
 In the second stage we compute a set of
candidates. Letbound variables and
toplevel definitions in scope are candidates if they are defined in
an
instance
block. Lambdabound variables, i.e. variables bound in lambdas, function types, lefthand sides, or module parameters, are candidates if they are bound as instance arguments using{{ }}
. Only candidates of type{Î} â C us
, whereC
is the target type computed in the previous stage and{Î}
only contains implicit or instance arguments, are considered.  Check the candidates
We attempt to use each candidate in turn to build an instance of the goal type
{Î} â C vs
. First we extend the current context by{Î}
. Then, given a candidatec : {Î} â A
we generate fresh metavariablesÎ±s : {Î}
for the arguments ofc
, with ordinary metavariables for implicit arguments, and instance metavariables, solved by a recursive call to instance resolution, for instance arguments.Next we unify
A[Î := Î±s]
withC vs
and apply instance resolution to the instance metavariables inÎ±s
. Both unification and instance resolution have three possible outcomes: yes, no, or maybe. In case we get a no answer from any of them, the current candidate is discarded, otherwise we return the potential solutionÎ» {Î} â c Î±s
. Compute the result
From the previous stage we get a list of potential solutions. If the list is empty we fail with an error saying that no instance for
C vs
could be found (no). If there is a single solution we use it to solve the goal (yes), and if there are multiple solutions we check if they are all equal. If they are, we solve the goal with one of them (yes), but if they are not, we postpone instance resolution (maybe), hoping that some of the maybes will turn into nos once we know more about the involved metavariables.If there are leftover instance problems at the end of type checking, the corresponding metavariables are printed in the Emacs status buffer together with their types and source location. The candidates that gave rise to potential solutions can be printed with the show constraints command (
Cc C=
).
[1]  Instance goal verification is buggy at the moment. See issue #1322. 
IrrelevanceÂ¶
Since version 2.2.8 Agda supports irrelevancy annotations. The general rule is that anything prepended by a dot (.) is marked irrelevant, which means that it will only be typechecked but never evaluated.
Motivating exampleÂ¶
One intended use case of irrelevance is data structures with embedded proofs, like sorted lists.
data _â€_ : Nat â Nat â Set where
zeroâ€ : {n : Nat} â zero â€ n
sucâ€suc : {m n : Nat} â m â€ n â suc m â€ suc n
postulate
pâ : 0 â€ 1
pâ : 0 â€ 1
module NoIrrelevance where
data SList (bound : Nat) : Set where
[] : SList bound
scons : (head : Nat)
â (head â€ bound)
â (tail : SList head)
â SList bound
Usually, when we define datatypes with embedded proofs we are forced to reason about the values of these proofs. For example, suppose we have two lists lâ
and lâ
with the same elements but different proofs:
lâ : SList 1
lâ = scons 0 pâ []
lâ : SList 1
lâ = scons 0 pâ []
Now suppose we want to prove that lâ
and lâ
are equal:
lââĄlâ : lâ âĄ lâ
lââĄlâ = refl
Itâs not so easy! Agda gives us an error:
pâ != pâ of type 0 â€ 1
when checking that the expression refl has type lâ âĄ lâ
We canât show that lâ âĄ lâ
by refl
when pâ
and pâ
are relevant. Instead, we need to reason about proofs of 0 â€ 1
.
postulate
proofequality : pâ âĄ pâ
Now we can prove lâ âĄ lâ
by rewriting with this equality:
lââĄlâ : lâ âĄ lâ
lââĄlâ rewrite proofequality = refl
Reasoning about equality of proofs becomes annoying quickly. We would like to avoid this kind of reasoning about proofs here  in this case we only care that a proof of head â€ bound
exists, i.e. any proof suffices. We can use irrelevance annotations to tell Agda we donât care about the values of the proofs:
data SList (bound : Nat) : Set where
[] : SList bound
scons : (head : Nat)
â .(head â€ bound)  note the dot!
â (tail : SList head)
â SList bound
The effect of the irrelevant type in the signature of scons is that sconsâs second argument is never inspected after Agda has ensured that it has the right type. The typechecker ignores irrelevant arguments when checking equality, so two lists can be equal even if they contain different proofs:
lâ : SList 1
lâ = scons 0 pâ []
lâ : SList 1
lâ = scons 0 pâ []
lââĄlâ : lâ âĄ lâ
lââĄlâ = refl
Irrelevant function typesÂ¶
For starters, consider irrelevant nondependent function types:
f : .A â B
This type implies that f
does not depend computationally on its argument.
What can be done to irrelevant argumentsÂ¶
Example 1. We can prove that two applications of an unknown irrelevant function to two different arguments are equal.
 an unknown function that does not use its second argument
postulate
f : {A B : Set} > A > .B > A
 the second argument is irrelevant for equality
proofIrr : {A : Set}{x y z : A} > f x y âĄ f x z
proofIrr = refl
Example 2. We can use irrelevant arguments as arguments to other irrelevant functions.
id : {A B : Set} > (.A > B) > .A > B
id g x = g x
Example 3. We can match on an irrelevant argument of an empty type with an absurd pattern ()
.
data â„ : Set where
zeronotone : .(0 âĄ 1) â â„
zeronotone ()
What canât be done to irrelevant argumentsÂ¶
Example 1. You canât use an irrelevant value in a nonirrelevant context.
badplus : Nat â .Nat â Nat
badplus n m = m + n
Variable m is declared irrelevant, so it cannot be used here
when checking that the expression m has type Nat
Example 2. You canât declare the functionâs return type as irrelevant.
bad : Nat â .Nat
bad n = 1
Invalid dotted expression
when checking that the expression .Nat has type Set _47
Example 3. You canât pattern match on an irrelevant value.
badMatching : Nat â .Nat â Nat
badMatching n zero = n
badMatching n (suc m) = n
Cannot pattern match against irrelevant argument of type Nat
when checking that the pattern zero has type Nat
Example 4. We also canât match on an irrelevant record (see Record Types).
record ÎŁ (A : Set) (B : A â Set) : Set where
constructor _,_
field
fst : A
snd : B fst
irrElim : {A : Set} {B : A â Set} â .(ÎŁ A B) â _
irrElim (a , b) = ?
Cannot pattern match against irrelevant argument of type ÎŁ A B
when checking that the pattern a , b has type ÎŁ A B
If this were allowed, b would have type B a but this type is not even wellformed because a is irrelevant!
Irrelevant declarationsÂ¶
Postulates and functions can be marked as irrelevant by prefixing the name with a dot when the name is declared. Irrelevant definitions can only be used as arguments of functions of an irrelevant function type .A â B
.
Examples:
.irrFunction : Nat â Nat
irrFunction zero = zero
irrFunction (suc n) = suc (suc (irrFunction n))
postulate
.assumefalse : (A : Set) â A
An important example is the irrelevance axiom irrAx
:
postulate
.irrAx : â {â} {A : Set â} > .A > A
This axiom is not provable inside Agda, but it is often very useful when working with irrelevance.
Irrelevant record fieldsÂ¶
Record fields (see Record Types) can be marked as irrelevant by prefixing their name with a dot in the definition of the record type. Projections for irrelevant fields are only created if option irrelevantprojections
is supplied (since Agda > 2.5.4).
Example 1. A record type containing pairs of numbers satisfying certain properties.
record InterestingNumbers : Set where
field
n : Nat
m : Nat
.prop1 : n + m âĄ n * m + 2
.prop2 : suc m â€ n
Example 2. For any type A
, we can define a âsquashedâ version Squash A
where all elements are equal.
record Squash (A : Set) : Set where
constructor squash
field
.proof : A
open Squash
.unsquash : â {A} â Squash A â A
unsquash x = proof x
Example 3. We can define the subset of x : A
satisfying P x
with irrelevant membership certificates.
record Subset (A : Set) (P : A > Set) : Set where
constructor _#_
field
elem : A
.certificate : P elem
.certificate : {A : Set}{P : A > Set} > (x : Subset A P) > P (Subset.elem x)
certificate (a # p) = irrAx p
Dependent irrelevant function typesÂ¶
Just like nondependent functions, we can also make dependent functions irrelevant. The basic syntax is as in the following examples:
f : .(x y : A) â B
f : .{x y z : A} â B
f : .(xs {ys zs} : A) â B
f : â x .y â B
f : â x .{y} {z} .v â B
f : .{{x : A}} â B
The declaration
f : .(x : A) â B[x]
f x = t[x]
requires that x
is irrelevant both in t[x]
and in B[x]
. This is possible if, for instance, B[x] = C x
, with C : .A â Set
.
Dependent irrelevance allows us to define the eliminator for the Squash type:
elimSquash : {A : Set} (P : Squash A â Set)
(ih : .(a : A) â P (squash a)) â
(aâ» : Squash A) â P aâ»
elimSquash P ih (squash a) = ih a
Note that this would not typecheck with (ih : (a : A) â P (squash a))
.
Irrelevant instance argumentsÂ¶
Contrary to normal instance arguments, irrelevant instance arguments (see Instance Arguments) are not required to have a unique solution.
record â€ : Set where
instance constructor tt
NonZero : Nat â Set
NonZero zero = â„
NonZero (suc _) = â€
predâČ : (n : Nat) .{{_ : NonZero n}} â Nat
predâČ zero {{}}
predâČ (suc n) = n
findnonzero : (n : Nat) {{x y : NonZero n}} â Nat
findnonzero n = predâČ n
Lambda AbstractionÂ¶
Pattern matching lambdaÂ¶
Anonymous pattern matching functions can be defined using the syntax:
\ { p11 .. p1n > e1 ; âŠ ; pm1 .. pmn > em }
(where, as usual, \
and >
can be replaced by Î»
and â
). Internally this is translated into a function definition of the following form:
.extlam p11 .. p1n = e1
âŠ
.extlam pm1 .. pmn = em
This means that anonymous pattern matching functions are generative. For instance, refl
will not be accepted as an inhabitant of the type
(Î» { true â true ; false â false }) ==
(Î» { true â true ; false â false })
because this is equivalent to extlam1 âĄ extlam2
for some distinct fresh names extlam1
and extlam2
.
Currently the where
and with
constructions are not allowed in (the toplevel clauses of) anonymous pattern matching functions.
Examples:
and : Bool â Bool â Bool
and = Î» { true x â x ; false _ â false }
xor : Bool â Bool â Bool
xor = Î» { true true â false
; false false â false
; _ _ â true
}
fst : {A : Set} {B : A â Set} â ÎŁ A B â A
fst = Î» { (a , b) â a }
snd : {A : Set} {B : A â Set} (p : ÎŁ A B) â B (fst p)
snd = Î» { (a , b) â b }
Local Definitions: let and whereÂ¶
There are two ways of declaring local definitions in Agda:
 letexpressions
 whereblocks
letexpressionsÂ¶
A letexpression defines an abbreviation. In other words, the expression that we define in a letexpression can neither be recursive, nor can let bound functions be defined by pattern matching.
Example:
f : Nat
f = let h : Nat â Nat
h m = suc (suc m)
in h zero + h (suc zero)
letexpressions have the general form
let fâ : Aââ â âŠ â Aââ â Aâ
fâ xâ âŠ xâ = eâ
âŠ
fâ : Aââ â âŠ â Aââ â Aâ
fâ xâ âŠ xâ = eâ
in eâ
where previous definitions are in scope in later definitions. The
type signatures can be left out if Agda can infer them.
After typechecking, the meaning of this is simply the substitution
eâ[fâ := Î» xâ âŠ xâ â e; âŠ; fâ := Î» xâ âŠ xâ â eâ]
. Since Agda
substitutes away letbindings, they do not show up in terms Agda
prints, nor in the goal display in interactive mode.
Let binding record patternsÂ¶
For a record
record R : Set where
constructor c
field
f : X
g : Y
h : Z
a let expression of the form
let (c x y z) = t
in u
will be translated internally to as
let x = f t
y = g t
z = h t
in u
This is not allowed if R
is declared coinductive
.
whereblocksÂ¶
whereblocks are much more powerful than letexpressions, as they
support arbitrary local definitions.
A where
can be attached to any function clause.
whereblocks have the general form
clause
where
decls
or
clause
module M where
decls
A simple instance is
g ps = e
where
f : Aâ â âŠ â Aâ â A
f pââ âŠ pââ= eâ
âŠ
âŠ
f pââ âŠ pââ= eâ
Here, the pá”ąâ±Œ
are patterns of the corresponding types and eá”ą
is an expression that can contain occurrences of f
.
Functions defined with a whereexpression must follow the rules for general definitions by pattern matching.
Example:
reverse : {A : Set} â List A â List A
reverse {A} xs = revappend xs []
where
revappend : List A â List A â List A
revappend [] ys = ys
revappend (x â· xs) ys = revappend xs (x â· ys)
Variable scopeÂ¶
The pattern variables of the parent clause of the whereblock are in
scope; in the previous example, these are A
and xs
. The
variables bound by the type signature of the parent clause are not in
scope. This is why we added the hidden binder {A}
.
Scope of the local declarationsÂ¶
The where
definitions are not visible outside of the clause that
owns these definitions (the parent clause). If the where
block is
given a name (form module M where
), then the definitions are
available as qualified by M
, since module M
is visible even
outside of the parent clause. The special form of an anonymous module
(module _ where
) makes the definitions visible outside of the
parent clause without qualification.
If the parent function of a named where
block
(form module M where
) is private
,
then module M
is also private
.
However, the declarations inside M
are not private unless declared
so explicitly. Thus, the following example scope checks fine:
module Parentâ where
private
parent = local
module Private where
local = Set
module Public = Private
testâ = Parentâ.Public.local
Likewise, a private
declaration for a parent function does not
affect the privacy of local functions defined under a
module _ where
block:
module Parentâ where
private
parent = local
module _ where
local = Set
testâ = Parentâ.local
They can be declared private
explicitly, though:
module Parentâ where
parent = local
module _ where
private
local = Set
Now, Parentâ.local
is not in scope.
A private
declaration for the parent of an ordinary
where
block has no effect on the local definitions, of course.
They are not even in scope.
Proving propertiesÂ¶
Sometimes one needs to refer to local definitions in proofs about the
parent function. In this case, the module âŻ where
variant is preferable.
reverse : {A : Set} â List A â List A
reverse {A} xs = revappend xs []
module Rev where
revappend : List A â List A â List A
revappend [] ys = ys
revappend (x :: xs) ys = revappend xs (x :: ys)
This gives us access to the local function as
Rev.revappend : {A : Set} (xs : List A) â List A â List A â List A
Alternatively, we can define local functions as private to the module we are working in; hence, they will not be visible in any module that imports this module but it will allow us to prove some properties about them.
private
revappend : {A : Set} â List A â List A â List A
revappend [] ys = ys
revappend (x â· xs) ys = revappend xs (x â· ys)
reverse' : {A : Set} â List A â List A
reverse' xs = revappend xs []
More Examples (for Beginners)Â¶
Using a letexpression:
twmap : {A : Set} â List A â List (List A)
twmap {A} xs = let twice : List A â List A
twice xs = xs ++ xs
in map (\ x â twice [ x ]) xs
Same definition but with less type information:
twmap' : {A : Set} â List A â List (List A)
twmap' {A} xs = let twice : _
twice xs = xs ++ xs
in map (\ x â twice [ x ]) xs
Same definition but with a whereexpression
twmap'' : {A : Set} â List A â List (List A)
twmap'' {A} xs = map (\ x â twice [ x ]) xs
where twice : List A â List A
twice xs = xs ++ xs
Even less type information using let:
g : Nat â List Nat
g zero = [ zero ]
g (suc n) = let sing = [ suc n ]
in sing ++ g n
Same definition using where:
g' : Nat â List Nat
g' zero = [ zero ]
g' (suc n) = sing ++ g' n
where sing = [ suc n ]
More than one definition in a let:
h : Nat â Nat
h n = let add2 : Nat
add2 = suc (suc n)
twice : Nat â Nat
twice m = m * m
in twice add2
More than one definition in a where:
fibfact : Nat â Nat
fibfact n = fib n + fact n
where fib : Nat â Nat
fib zero = suc zero
fib (suc zero) = suc zero
fib (suc (suc n)) = fib (suc n) + fib n
fact : Nat â Nat
fact zero = suc zero
fact (suc n) = suc n * fact n
Combining let and where:
k : Nat â Nat
k n = let aux : Nat â Nat
aux m = pred (h m) + fibfact m
in aux (pred n)
where pred : Nat â Nat
pred zero = zero
pred (suc m) = m
Lexical StructureÂ¶
Agda code is written in UTF8 encoded plain text files with the
extension .agda
. Most unicode characters can be used in
identifiers and whitespace is important, see Names and
Layout below.
TokensÂ¶
Keywords and special symbolsÂ¶
Most nonwhitespace unicode can be used as part of an Agda name, but there are two kinds of exceptions:
 special symbols
 Characters with special meaning that cannot appear at all in a name. These are
.;{}()@"
.  keywords
Reserved words that cannot appear as a name part, but can appear in a name together with other characters.
=

>
â
:
?
\
Î»
â..
...
abstract
codata
coinductiveconstructor
data
doetaequality
field
forallhiding
import
in
inductive
infix
infixl
infixr
instance
let
macromodule
mutual
noetaequality
open
overlappattern
postulate
primitive
private
public
quotequoteContext
quoteGoal
quoteTermrecord
renaming
rewrite
Set
syntax
tactic
unquote unquoteDecl unquoteDefusing
variablewhere
with
The
Set
keyword can appear with a number suffix, optionally subscripted (see Universe Levels). For instanceSet42
andSetââ
are both keywords.
NamesÂ¶
A qualified name is a nonempty sequence of names separated by
dots (.
). A name is an alternating sequence of name parts and
underscores (_
), containing at least one name part. A name part
is a nonempty sequence of unicode characters, excluding whitespace,
_
, and special symbols. A
name part cannot be one of the
keywords above, and cannot start
with a single quote, '
(which are used for character literals, see
Literals below).
 Examples
 Valid:
data?
,::
,if_then_else_
,0b
,_âą_â_
,x=y
 Invalid:
data_?
,foo__bar
,_
,a;b
,[_.._]
 Valid:
The underscores in a name indicate where the arguments go when the name is used
as an operator. For instance, the application _+_ 1 2
can be written as 1
+ 2
. See Mixfix Operators for more information. Since most sequences
of characters are valid names, whitespace is more important than in other
languages. In the example above the whitespace around +
is required, since
1+2
is a valid name.
Qualified names are used to refer to entities defined in other modules. For
instance Prelude.Bool.true
refers to the name true
defined in the
module Prelude.Bool
. See Module System for more information.
LiteralsÂ¶
There are four types of literal values: integers, floats, characters, and strings. See Builtins for the corresponding types, and Literal Overloading for how to support literals for userdefined types.
 Integers
Integer values in decimal or hexadecimal (prefixed by
0x
) notation. Nonnegative numbers map by default to builtin natural numbers, but can be overloaded. Negative numbers have no default interpretation and can only be used through overloading.Examples:
123
,0xF0F080
,42
,0xF
 Floats
Floating point numbers in the standard notation (with square brackets denoting optional parts):
float ::= [] decimal . decimal [exponent]  [] decimal exponent exponent ::= (e  E) [+  ] decimal
These map to builtin floats and cannot be overloaded.
Examples:
1.0
,5.0e+12
,1.01e16
,4.2E9
,50e3
.
 Characters
Character literals are enclosed in single quotes (
'
). They can be a single (unicode) character, other than'
or\
, or an escaped character. Escaped characters start with a backslash\
followed by an escape code. Escape codes are natural numbers in decimal or hexadecimal (prefixed byx
) between0
and0x10ffff
(1114111
), or one of the following special escape codes:Code ASCII Code ASCII Code ASCII Code ASCII a
7 b
8 t
9 n
10 v
11 f
12 \
\
'
'
"
"
NUL
0 SOH
1 STX
2 ETX
3 EOT
4 ENQ
5 ACK
6 BEL
7 BS
8 HT
9 LF
10 VT
11 FF
12 CR
13 SO
14 SI
15 DLE
16 DC1
17 DC2
18 DC3
19 DC4
20 NAK
21 SYN
22 ETB
23 CAN
24 EM
25 SUB
26 ESC
27 FS
28 GS
29 RS
30 US
31 SP
32 DEL
127 Character literals map to the builtin character type and cannot be overloaded.
Examples:
'A'
,'â'
,'\x2200'
,'\ESC'
,'\32'
,'\n'
,'\''
,'"'
.
 Strings
String literals are sequences of, possibly escaped, characters enclosed in double quotes
"
. They follow the same rules as character literals except that double quotes"
need to be escaped rather than single quotes'
. String literals map to the builtin string type by default, but can be overloaded.Example:
"ĐŃĐžĐČĐ”Ń \"ĐŒĐžŃ\"\n"
.
HolesÂ¶
Holes are an integral part of the interactive development supported by the
Emacs mode. Any text enclosed in {!
and !}
is a
hole and may contain nested holes. A hole with no contents can be written
?
. There are a number of Emacs commands that operate on the contents of a
hole. The type checker ignores the contents of a hole and treats it as an
unknown (see Implicit Arguments).
Example: {! f {!x!} 5 !}
CommentsÂ¶
Singleline comments are written with a double dash 
followed by
arbitrary text. Multiline comments are enclosed in {
and }
and can be nested. Comments cannot appear in string
literals.
Example:
{ Here is a { nested }
comment }
s : String line comment {
s = "{ not a comment }"
LayoutÂ¶
Agda is layout sensitive using similar rules as Haskell, with the exception
that layout is mandatory: you cannot use explicit {
, }
and ;
to
avoid it.
A layout block contains a sequence of statements and is started by one of the layout keywords:
abstract do field instance let macro mutual postulate primitive private where
The first token after the layout keyword decides the indentation of the block. Any token indented more than this is part of the previous statement, a token at the same level starts a new statement, and a token indented less lies outside the block.
data Nat : Set where  starts a layout block
 comments are not tokens
zero : Nat  statement 1
suc : Nat â  statement 2
Nat  also statement 2
one : Nat  outside the layout block
one = suc zero
Note that the indentation of the layout keyword does not matter.
An Agda file contains one toplevel layout block, with the special rule that the contents of the toplevel module need not be indented.
module Example where
NotIndented : Setâ
NotIndented = Set
Literate AgdaÂ¶
Agda supports literate programming with multiple typesetting
tools like LaTeX, Markdown and reStructuredText. For instance, with LaTeX,
everything in a file is a comment unless enclosed in \begin{code}
,
\end{code}
. Literate Agda files have special file extensions, like
.lagda
and .lagda.tex
for LaTeX, .lagda.md
for Markdown,
.lagda.rst
for reStructuredText instead of .agda
. The main use case
for literate Agda is to generate LaTeX documents from Agda code. See
Generating HTML and Generating LaTeX for more information.
\documentclass{article}
% some preamble stuff
\begin{document}
Introduction usually goes here
\begin{code}
module MyPaper where
open import Prelude
five : Nat
five = 2 + 3
\end{code}
Now, conclusions!
\end{document}
Literal OverloadingÂ¶
Natural numbersÂ¶
By default natural number literals are
mapped to the builtin natural number type. This can be
changed with the FROMNAT
builtin, which binds to a function accepting a
natural number:
{# BUILTIN FROMNAT fromNat #}
This causes natural number literals n
to be desugared to fromNat n
.
Note that the desugaring happens before implicit argument are inserted so fromNat
can have any number of
implicit or instance arguments. This can be
exploited to support overloaded literals by defining a type class containing fromNat
:
module numbersimple where
record Number {a} (A : Set a) : Set a where
field fromNat : Nat â A
open Number {{...}} public
{# BUILTIN FROMNAT fromNat #}
This definition requires that any natural number can be mapped into the given
type, so it wonât work for types like Fin n
. This can be solved by refining
the Number
class with an additional constraint:
record Number {a} (A : Set a) : Set (lsuc a) where
field
Constraint : Nat â Set a
fromNat : (n : Nat) {{_ : Constraint n}} â A
open Number {{...}} public using (fromNat)
{# BUILTIN FROMNAT fromNat #}
This is the definition used in Agda.Builtin.FromNat
.
A Number
instance for Nat
is simply this:
instance
NumNat : Number Nat
NumNat .Number.Constraint _ = â€
NumNat .Number.fromNat m = m
A Number
instance for Fin n
can be defined as follows:
_â€_ : (m n : Nat) â Set
zero â€ n = â€
suc m â€ zero = â„
suc m â€ suc n = m â€ n
fromNâ€ : â m n â m â€ n â Fin (suc n)
fromNâ€ zero _ _ = zero
fromNâ€ (suc _) zero ()
fromNâ€ (suc m) (suc n) p = suc (fromNâ€ m n p)
instance
NumFin : â {n} â Number (Fin (suc n))
NumFin {n} .Number.Constraint m = m â€ n
NumFin {n} .Number.fromNat m {{mâ€n}} = fromNâ€ m n mâ€n
test : Fin 5
test = 3
It is important that the constraint for literals is trivial. Here,
3 â€ 5
evaluates to â€
whose inhabitant is found by unification.
Using predefined function from the standard library and instance NumNat
,
the NumFin
instance can be simply:
open import Data.Fin using (Fin; #_)
open import Data.Nat using (suc; _â€?_)
open import Relation.Nullary.Decidable using (True)
instance
NumFin : â {n} â Number (Fin n)
NumFin {n} .Number.Constraint m = True (suc m â€? n)
NumFin {n} .Number.fromNat m {{m<n}} = #_ m {m<n = m<n}
Negative numbersÂ¶
Negative integer literals have no default mapping and can only be used through
the FROMNEG
builtin. Binding this to a function fromNeg
causes
negative integer literals n
to be desugared to fromNeg n
, where n
is a builtin natural number. From Agda.Builtin.FromNeg
:
record Negative {a} (A : Set a) : Set (lsuc a) where
field
Constraint : Nat â Set a
fromNeg : (n : Nat) {{_ : Constraint n}} â A
open Negative {{...}} public using (fromNeg)
{# BUILTIN FROMNEG fromNeg #}
StringsÂ¶
String literals are overloaded with
the FROMSTRING
builtin, which works just like FROMNAT
. If it is not
bound string literals map to builtin strings. From
Agda.Builtin.FromString
:
record IsString {a} (A : Set a) : Set (lsuc a) where
field
Constraint : String â Set a
fromString : (s : String) {{_ : Constraint s}} â A
open IsString {{...}} public using (fromString)
{# BUILTIN FROMSTRING fromString #}
RestrictionsÂ¶
Currently only integer and string literals can be overloaded.
Overloading does not work in patterns yet.
Mixfix OperatorsÂ¶
A name containing one or more name parts and one or more _
can be used as an operator where the arguments go in place of the _
. For instance, an application of the name if_then_else_
to arguments x
, y
, and z
can be written either as a normal application if_then_else_ x y z
or as an operator application if x then y else z
.
Examples:
_and_ : Bool â Bool â Bool
true and x = x
false and _ = false
if_then_else_ : {A : Set} â Bool â A â A â A
if true then x else y = x
if false then x else y = y
_â_ : Bool â Bool â Bool
true â b = b
false â _ = true
PrecedenceÂ¶
Consider the expression true and false â false
.
Depending on which of _and_
and _â_
has more precedence,
it can either be read as (false and true) â false = true
,
or as false and (true â false) = true
.
Each operator is associated to a precedence, which is a floating point number (can be negative and fractional!). The default precedence for an operator is 20.
If we give _and_
more precedence than _â_
, then we will get the first result:
infix 30 _and_
 infix 20 _â_ (default)
pand : {x y z : Bool} â x and y â z âĄ (x and y) â z
pand = refl
eand : false and true â false âĄ true
eand = refl
But, if we declare a new operator _andâ_
and give it less precedence than
_â_
, then we will get the second result:
_andâ_ : Bool â Bool â Bool
_andâ_ = _and_
infix 15 _andâ_
 infix 20 _â_ (default)
pâ : {x y z : Bool} â x andâ y â z âĄ x andâ (y â z)
pâ = refl
eâ : false andâ true â false âĄ false
eâ = refl
AssociativityÂ¶
Consider the expression true â false â false
. Depending on whether _â_
is
associates to the left or to the right, it can be read as
(false â true) â false = false
, or false â (true â false) = true
,
respectively.
If we declare an operator _â_
as infixr
, it will associate to the right:
infixr 20 _â_
pright : {x y z : Bool} â x â y â z âĄ x â (y â z)
pright = refl
eright : false â true â false âĄ true
eright = refl
If we declare an operator _ââ_
as infixl
, it will associate to the left:
infixl 20 _ââ_
_ââ_ : Bool â Bool â Bool
_ââ_ = _â_
pleft : {x y z : Bool} â x ââ y ââ z âĄ (x ââ y) ââ z
pleft = refl
eleft : false ââ true ââ false âĄ false
eleft = refl
Ambiguity and ScopeÂ¶
If you have not yet declared the fixity of an operator, Agda will complain if you try to use ambiguously:
eambiguous : Bool
eambiguous = true â true â true
Could not parse the application true â true â true
Operators used in the grammar:
â (infix operator, level 20)
Fixity declarations may appear anywhere in a module that other declarations may appear. They then apply to the entire scope in which they appear (i.e. before and after, but not outside).
Module SystemÂ¶
Module applicationÂ¶
Anonymous modulesÂ¶
BasicsÂ¶
First let us introduce some terminology. A definition is a syntactic construction defining an entity such as a function or a datatype. A name is a string used to identify definitions. The same definition can have many names and at different points in the program it will have different names. It may also be the case that two definitions have the same name. In this case there will be an error if the name is used.
The main purpose of the module system is to structure the way names are used in a program. This is done by organising the program in an hierarchical structure of modules where each module contains a number of definitions and submodules. For instance,
module Main where
module B where
f : Nat â Nat
f n = suc n
g : Nat â Nat â Nat
g n m = m
Note that we use indentation to indicate which definitions are part of a module. In the example f
is in the module Main.B
and g
is in Main
. How to refer to a particular definition is determined by where it is located in the module hierarchy. Definitions from an enclosing module are referred to by their given names as seen in the type of f above. To access a definition from outside its defining module a qualified name has to be used.
module Mainâ where
module B where
f : Nat â Nat
f n = suc n
ff : Nat â Nat
ff x = B.f (B.f x)
To be able to use the short names for definitions in a module the module has to be opened.
module Mainâ where
module B where
f : Nat â Nat
f n = suc n
open B
ff : Nat â Nat
ff x = f (f x)
If A.qname
refers to a definition d
, then after open A
, qname
will also refer to d
. Note that qname
can itself be a qualified name. Opening a module only introduces new names for a definition, it never removes the old names. The policy is to allow the introduction of ambiguous names, but give an error if an ambiguous name is used.
Modules can also be opened within a local scope by putting the open B
within a where
clause:
ffâ : Nat â Nat
ffâ x = f (f x) where open B
Private definitionsÂ¶
To make a definition inaccessible outside its defining module it can be declared private
. A private definition is treated as a normal definition inside the module that defines it, but outside the module the definition has no name. In a dependently type setting there are some problems with private definitionsâsince the type checker performs computations, private names might show up in goals and error messages. Consider the following (contrived) example
module Mainâ where
module A where
private
IsZeroâ : Nat â Set
IsZeroâ zero = â€
IsZeroâ (suc n) = â„
IsZero : Nat â Set
IsZero n = IsZeroâ n
open A
prf : (n : Nat) â IsZero n
prf n = ?
The type of the goal ?0
is IsZero n
which normalises to IsZeroâ n
. The question is how to display this normal form to the user. At the point of ?0
there is no name for IsZeroâ
. One option could be try to fold the term and print IsZero n
. This is a very hard problem in general, so rather than trying to do this we make it clear to the user that IsZeroâ
is something that is not in scope and print the goal as ;Mainâ.A.IsZeroâ n
. The leading semicolon indicates that the entity is not in scope. The same technique is used for definitions that only have ambiguous names.
In effect using private definitions means that, from the userâs perspective, we do not have subject reduction. This is just an illusion, howeverâthe type checker has full access to all definitions.
Name modifiersÂ¶
An alternative to making definitions private is to exert finer control over what names are introduced when opening a module. This is done by qualifying an open
statement with one or more of the modifiers using
, hiding
, or renaming
. You can combine both using
and hiding
with renaming
, but not with each other. The effect of
open A using (xs) renaming (ys to zs)
is to introduce the names xs
and zs
where xs
refers to the same definition as A.xs
and zs
refers to A.ys
. We do not permit xs
, ys
and zs
to overlap. The other forms of opening are defined in terms of this one.
An omitted renaming
modifier is equivalent to an empty renaming.
Since 2.6.1: The fixity of an operator can be set or changed in a renaming
directive:
module ExampleRenamingFixity where
module ArithFoo where
postulate
A : Set
_&_ _^_ : A â A â A
infixr 10 _&_
open ArithFoo renaming (_&_ to infixl 8 _+_; _^_ to infixl 10 _^_)
Here, we change the fixity of _&_
while renaming it to _+_
, and assign a new fixity to _^_
which has the default fixity in module ArithFoo
.
Reexporting namesÂ¶
A useful feature is the ability to reexport names from another module. For instance, one may want to create a module to collect the definitions from several other modules. This is achieved by qualifying the open statement with the public keyword:
module Example where
module Natâ where
data Natâ : Set where
zero : Natâ
suc : Natâ â Natâ
module Boolâ where
data Boolâ : Set where
true false : Boolâ
module Prelude where
open Natâ public
open Boolâ public
isZero : Natâ â Boolâ
isZero zero = true
isZero (suc _) = false
The module Prelude
above exports the names Nat
, zero
, Bool
, etc., in addition to isZero
.
Parameterised modulesÂ¶
So far, the module system features discussed have dealt solely with scope manipulation. We now turn our attention to some more advanced features.
It is sometimes useful to be able to work temporarily in a given signature. For instance, when defining functions for sorting lists it is convenient to assume a set of list elements A
and an ordering over A
. In Coq this can be done in two ways: using a functor, which is essentially a function between modules, or using a section. A section allows you to abstract some arguments from several definitions at once. We introduce parameterised modules analogous to sections in Coq. When declaring a module you can give a telescope of module parameters which are abstracted from all the definitions in the module. For instance, a simple implementation of a sorting function looks like this:
module Sort (A : Set)(_â€_ : A â A â Bool) where
insert : A â List A â List A
insert x [] = x â· []
insert x (y â· ys) with x â€ y
insert x (y â· ys)  true = x â· y â· ys
insert x (y â· ys)  false = y â· insert x ys
sort : List A â List A
sort [] = []
sort (x â· xs) = insert x (sort xs)
As mentioned parametrising a module has the effect of abstracting the parameters over the definitions in the module, so outside the Sort module we have
Sort.insert : (A : Set)(_â€_ : A â A â Bool) â
A â List A â List A
Sort.sort : (A : Set)(_â€_ : A â A â Bool) â
List A â List A
For function definitions, explicit module parameter become explicit arguments to the abstracted function, and implicit parameters become implicit arguments. For constructors, however, the parameters are always implicit arguments. This is a consequence of the fact that module parameters are turned into datatype parameters, and the datatype parameters are implicit arguments to the constructors. It also happens to be the reasonable thing to do.
Something which you cannot do in Coq is to apply a section to its arguments. We allow this through the module application statement. In our example:
module SortNat = Sort Nat leqNat
This will define a new module SortNat as follows
module SortNat where
insert : Nat â List Nat â List Nat
insert = Sort.insert Nat leqNat
sort : List Nat â List Nat
sort = Sort.sort Nat leqNat
The new module can also be parameterised, and you can use name modifiers to control what definitions from the original module are applied and what names they have in the new module. The general form of a module application is
module M1 Î = M2 terms modifiers
A common pattern is to apply a module to its arguments and then open the resulting module. To simplify this we introduce the shorthand
open module M1 Î = M2 terms [public] mods
for
module M1 Î = M2 terms mods
open M1 [public]
Splitting a program over multiple filesÂ¶
When building large programs it is crucial to be able to split the program over multiple files and to not have to type check and compile all the files for every change. The module system offers a structured way to do this. We define a program to be a collection of modules, each module being defined in a separate file. To gain access to a module defined in a different file you can import the module:
import M
In order to implement this we must be able to find the file in which a module is defined. To do this we require that the toplevel module A.B.C
is defined in the file C.agda
in the directory A/B/
. One could imagine instead to give a file name to the import statement, but this would mean cluttering the program with details about the file system which is not very nice.
When importing a module M
, the module and its contents are brought into scope as if the module had been defined in the current file. In order to get access to the unqualified names of the module contents it has to be opened. Similarly to module application we introduce the shorthand
open import M
for
import M
open M
Sometimes the name of an imported module clashes with a local module. In this case it is possible to import the module under a different name.
import M as Mâ
It is also possible to attach modifiers to import statements, limiting or changing what names are visible from inside the module.
Datatype modules and record modulesÂ¶
When you define a datatype it also defines a module so constructors can now be referred to qualified by their data type. For instance, given:
module DatatypeModules where
data Natâ : Set where
zero : Natâ
suc : Natâ â Natâ
data Fin : Natâ â Set where
zero : â {n} â Fin (suc n)
suc : â {n} â Fin n â Fin (suc n)
you can refer to the constructors unambiguously as Natâ.zero
, Natâ.suc
, Fin.zero
, and Fin.suc
(Natâ
and Fin
are modules containing the respective constructors). Example:
inj : (n m : Natâ) â Natâ.suc n âĄ suc m â n âĄ m
inj .m m refl = refl
Previously you had to write something like
injâ : (n m : Natâ) â _âĄ_ {A = Natâ} (suc n) (suc m) â n âĄ m
injâ .m m refl = refl
to make the type checker able to figure out that you wanted the natural number suc in this case.
Also record declarations define a corresponding module, see Record modules.
Mutual RecursionÂ¶
Mutual recursive functions can be written by placing the type signatures of all mutually recursive function before their definitions:
f : A
g : B[f]
f = a[f, g]
g = b[f, g].
You can mix arbitrary declarations, such as modules and postulates, with mutually recursive definitions. For data types and records the following syntax is used to separate the declaration from the definition:
 Declaration.
data Vec (A : Set) : Nat â Set  Note the absence of âwhereâ.
 Definition.
data Vec A where
[] : Vec A zero
_::_ : {n : Nat} â A â Vec A n â Vec A (suc n)
 Declaration.
record Sigma (A : Set) (B : A â Set) : Set
 Definition.
record Sigma A B where
constructor _,_
field fst : A
snd : B fst
The parameter lists in the second part of a data or record declaration behave like variables lefthand sides (although infix syntax is not supported). That is, they should have no type signatures, but implicit parameters can be omitted or bound by name.
Such a separation of declaration and definition is for instance needed when defining a set of codes for types and their interpretation as actual types (a socalled universe):
 Declarations.
data TypeCode : Set
Interpretation : TypeCode â Set
 Definitions.
data TypeCode where
nat : TypeCode
pi : (a : TypeCode) (b : Interpretation a â TypeCode) â TypeCode
Interpretation nat = Nat
Interpretation (pi a b) = (x : Interpretation a) â Interpretation (b x)
When making separated declarations/definitions private or abstract you should attach the private
keyword to the declaration and the abstract
keyword to the definition. For instance, a private, abstract function can be defined as
private
f : A
abstract
f = e
Old Syntax: Keyword mutual
Â¶
Note
You are advised to avoid using this old syntax if possible, but the old syntax is still supported.
Mutual recursive functions can be written by placing the type signatures of all mutually recursive function before their definitions:
mutual
f : A
f = a[f, g]
g : B[f]
g = b[f, g]
Using the mutual
keyword,
the universe example from above is expressed as follows:
mutual
data TypeCode : Set where
nat : TypeCode
pi : (a : TypeCode) (b : Interpretation a â TypeCode) â TypeCode
Interpretation : TypeCode â Set
Interpretation nat = Nat
Interpretation (pi a b) = (x : Interpretation a) â Interpretation (b x)
This alternative syntax desugars into the new syntax.
Pattern SynonymsÂ¶
A pattern synonym is a declaration that can be used on the left hand side (when pattern matching) as well as the right hand side (in expressions). For example:
data Nat : Set where
zero : Nat
suc : Nat â Nat
pattern z = zero
pattern ss x = suc (suc x)
f : Nat â Nat
f z = z
f (suc z) = ss z
f (ss n) = n
Pattern synonyms are implemented by substitution on the abstract syntax, so definitions are scopechecked but not typechecked. They are particularly useful for universe constructions.
OverloadingÂ¶
Pattern synonyms can be overloaded as long as all candidates have the same shape. Two pattern synonym definitions have the same shape if they are equal up to variable and constructor names. Shapes are checked at resolution time and after expansion of nested pattern synonyms.
For example:
data List (A : Set) : Set where
lnil : List A
lcons : A â List A â List A
data Vec (A : Set) : Nat â Set where
vnil : Vec A zero
vcons : â {n} â A â Vec A n â Vec A (suc n)
pattern [] = lnil
pattern [] = vnil
pattern _â·_ x xs = lcons x xs
pattern _â·_ y ys = vcons y ys
lmap : â {A B} â (A â B) â List A â List B
lmap f [] = []
lmap f (x â· xs) = f x â· lmap f xs
vmap : â {A B n} â (A â B) â Vec A n â Vec B n
vmap f [] = []
vmap f (x â· xs) = f x â· vmap f xs
Flipping the arguments in the synonym for vcons
, changing it to pattern
_â·_ ys y = vcons y ys
, results in the following error when trying to use the
synonym:
Cannot resolve overloaded pattern synonym _â·_, since candidates
have different shapes:
pattern _â·_ x xs = lcons x xs
at patternsynonyms.lagda.rst:51,1316
pattern _â·_ ys y = vcons y ys
at patternsynonyms.lagda.rst:52,1316
(hint: overloaded pattern synonyms must be equal up to variable and
constructor names)
when checking that the clause lmap f (x â· xs) = f x â· lmap f xs has
type {A B : Set} â (A â B) â List A â List B
RefoldingÂ¶
For each pattern pattern lhs = rhs
, Agda declares a DISPLAY
pragma refolding rhs
to lhs
(see The DISPLAY pragma for
more details).
Positivity CheckingÂ¶
Note
This is a stub.
The NO_POSITIVITY_CHECK
pragmaÂ¶
The pragma switches off the positivity checker for data/record definitions and mutual blocks. This pragma was added in Agda 2.5.1
The pragma must precede a data/record definition or a mutual
block. The pragma cannot be used in safe
mode.
Examples:
Skipping a single data definition:
{# NO_POSITIVITY_CHECK #} data D : Set where lam : (D â D) â D
Skipping a single record definition:
{# NO_POSITIVITY_CHECK #} record U : Set where field ap : U â U
Skipping an oldstyle mutual block. Somewhere within a mutual block before a data/record definition:
mutual data D : Set where lam : (D â D) â D {# NO_POSITIVITY_CHECK #} record U : Set where field ap : U â U
Skipping an oldstyle mutual block. Before the
mutual
keyword:{# NO_POSITIVITY_CHECK #} mutual data D : Set where lam : (D â D) â D record U : Set where field ap : U â U
Skipping a newstyle mutual block. Anywhere before the declaration or the definition of a data/record in the block:
record U : Set data D : Set record U where field ap : U â U {# NO_POSITIVITY_CHECK #} data D where lam : (D â D) â D
POLARITY pragmasÂ¶
Polarity pragmas can be attached to postulates. The polarities express how the postulateâs arguments are used. The following polarities are available:
_
: Unused.++
: Strictly positive.+
: Positive.
: Negative.*
: Unknown/mixed.
Polarity pragmas have the form {# POLARITY name <zero or more
polarities> #}
, and can be given wherever fixity declarations can
be given. The listed polarities apply to the given postulateâs
arguments (explicit/implicit/instance), from left to right. Polarities
currently cannot be given for module parameters. If the postulate
takes n arguments (excluding module parameters), then the number of
polarities given must be between 0 and n (inclusive).
Polarity pragmas make it possible to use postulated type formers in recursive types in the following way:
postulate
â„_â„ : Set â Set
{# POLARITY â„_â„ ++ #}
data D : Set where
c : â„ D â„ â D
Note that one can use postulates that may seem benign, together with polarity pragmas, to prove that the empty type is inhabited:
postulate
_â_ : Set â Set â Set
lambda : {A B : Set} â (A â B) â A â B
apply : {A B : Set} â A â B â A â B
{# POLARITY _â_ ++ #}
data â„ : Set where
data D : Set where
c : D â â„ â D
notinhabited : D â â„
notinhabited (c f) = apply f (c f)
d : D
d = c (lambda notinhabited)
bad : â„
bad = notinhabited d
Polarity pragmas are not allowed in safe mode.
PostulatesÂ¶
A postulate is a declaration of an element of some type without an accompanying definition. With postulates we can introduce elements in a type without actually giving the definition of the element itself.
The general form of a postulate declaration is as follows:
postulate
c11 ... c1i : <Type>
...
cn1 ... cnj : <Type>
Example:
postulate
A B : Set
a : A
b : B
_=AB=_ : A > B > Set
a==b : a =AB= b
Introducing postulates is in general not recommended. Once postulates are introduced the consistency of the whole development is at risk, because there is nothing that prevents us from introducing an element in the empty set.
data False : Set where
postulate bottom : False
A preferable way to work is to define a module parametrised by the elements we need
module Absurd (bt : False) where
 ...
module M (A B : Set) (a : A) (b : B)
(_=AB=_ : A > B > Set) (a==b : a =AB= b) where
 ...
PragmasÂ¶
Pragmas are comments that are not ignored by Agda but have some special meaning. The general format is:
{# <PRAGMA_NAME> <arguments> #}
Index of pragmasÂ¶
 BUILTIN
 CATCHALL
 COMPILE
 FOREIGN
 NO_POSITIVITY_CHECK
 NO_TERMINATION_CHECK
 NON_TERMINATING
NON_COVERING
 POLARITY
 STATIC
 TERMINATING
 INLINE
 NOINLINE
 WARNING_ON_USAGE
 WARNING_ON_IMPORT
See also Commandline and pragma options.
The DISPLAY
pragmaÂ¶
Users can declare a DISPLAY
pragma:
{# DISPLAY f e1 .. en = e #}
This causes f e1 .. en
to be printed in the same way as e
, where
ei
can bind variables used in e
. The expressions ei
and e
are scope checked, but not type checked.
For example this can be used to print overloaded (instance) functions with the overloaded name:
instance
NumNat : Num Nat
NumNat = record { ..; _+_ = natPlus }
{# DISPLAY natPlus a b = a + b #}
Limitations
 Lefthand sides are restricted to variables, constructors, defined functions or types, and literals. In particular, lambdas are not allowed in lefthand sides.
 Since DISPLAY pragmas are not type checked implicit argument insertion may not work properly if the type of f computes to an implicit function space after pattern matching.
The INLINE
and NOINLINE
pragmasÂ¶
A definition marked with an INLINE
pragma is inlined during compilation. If it is a simple
definition that does no pattern matching, it is also inlined in function bodies at typechecking
time.
Definitions are automatically marked INLINE
if they satisfy the following criteria:
 No pattern matching.
 Uses each argument at most once.
 Does not use all its arguments.
Automatic inlining can be prevented using the NOINLINE
pragma.
Example:
 Would be autoinlined since it doesn't use the type arguments.
_â_ : {A B C : Set} â (B â C) â (A â B) â A â C
(f â g) x = f (g x)
{# NOINLINE _â_ #}  prevents autoinlining
 Would not be autoinlined since it's using all its arguments
_o_ : (Set â Set) â (Set â Set) â Set â Set
(F o G) X = F (G X)
{# INLINE _o_ #}  force inlining
The WARNING_ON_
pragmasÂ¶
A library author can use a WARNING_ON_USAGE
pragma to attach to a defined
name a warning to be raised whenever this name is used.
Similarly they can use a WARNING_ON_IMPORT
pragma to attach to a module
a warning to be raised whenever this module is imported.
This would typically be used to declare a name or a module âDEPRECATEDâ and advise the enduser to port their code before the feature is dropped.
Users can turn these warnings off by using the warn=noUserWarning
option.
For more information about the warning machinery, see Warnings.
Example:
 The new name for the identity
id : {A : Set} â A â A
id x = x
 The deprecated name
Î»xâx = id
 The warning
{# WARNING_ON_USAGE Î»xâx "DEPRECATED: Use `id` instead of `Î»xâx`" #}
{# WARNING_ON_IMPORT "DEPRECATED: Use module `Function.Identity` rather than `Identity`" #}
PropÂ¶
Prop
is Agdaâs builtin sort of definitionally proofirrelevant
propositions. It is similar to the sort Set
, but all elements of
a type in Prop
are considered to be (definitionally) equal.
The implementation of Prop
is based on the POPL 2019 paper
Definitional ProofIrrelevance without K by GaĂ«tan Gilbert, Jesper Cockx,
Matthieu Sozeau, and Nicolas Tabareau.
UsageÂ¶
Just as for Set
, we can define new types in Prop
âs as data or
record types:
data â„ : Prop where
record â€ : Prop where
constructor tt
When defining a function from a data type in Prop
to a type in
Set
, pattern matching is restricted to the absurd pattern ()
:
absurd : (A : Set) â â„ â A
absurd A ()
Unlike for Set
, all elements of a type in Prop
are
definitionally equal. This implies all applications of absurd
are
the same:
onlyoneabsurdity : {A : Set} â (p q : â„) â absurd A p âĄ absurd A q
onlyoneabsurdity p q = refl
Since pattern matching on datatypes in Prop
is limited, it is
recommended to define types in Prop
as recursive functions rather
than inductive datatypes. For example, the relation _â€_
on natural
numbers can be defined as follows:
_â€_ : Nat â Nat â Prop
zero â€ y = â€
suc x â€ zero = â„
suc x â€ suc y = x â€ y
The induction principle for _â€_
can then be defined by matching on
the arguments of type Nat
:
module _ (P : (m n : Nat) â Set)
(pzy : (y : Nat) â P zero y)
(pss : (x y : Nat) â P x y â P (suc x) (suc y)) where
â€ind : (m n : Nat) â m â€ n â P m n
â€ind zero y pf = pzy y
â€ind (suc x) (suc y) pf = pss x y (â€ind x y pf)
â€ind (suc _) zero ()
Note that while it is also possible to define _â€_ as a datatype in Prop, it is hard to use that version because of the limitations to matching.
When defining a record type in Set
, the types of the fields can be
both in Set
and Prop
. For example:
record Fin (n : Nat) : Set where
constructor _[_]
field
âŠ_â§ : Nat
proof : suc âŠ_â§ â€ n
open Fin
FinâĄ : â {n} (x y : Fin n) â âŠ x â§ âĄ âŠ y â§ â x âĄ y
FinâĄ x y refl = refl
The predicative hierarchy of Prop
Â¶
Just as for Set
, Agda has a predicative hierarchy of sorts
Propâ
(= Prop
), Propâ
, Propâ
, âŠ where Propâ :
Setâ
, Propâ : Setâ
, Propâ : Setâ
, etc. Like Set
,
Prop
also supports universe polymorphism (see universe
levels), so for each â : Level
we have the sort
Prop â
. For example:
True : â {â} â Prop (lsuc â)
True {â} = â (P : Prop â) â P â P
The propositional squash typeÂ¶
When defining a datatype in Prop â
, it is allowed to have
constructors that take arguments in Set ââČ
for any ââČ â€ â
.
For example, this allows us to define the propositional squash type
and its eliminator:
data Squash {â} (A : Set â) : Prop â where
squash : A â Squash A
squashelim : â {ââ ââ} (A : Set ââ) (P : Prop ââ) â (A â P) â Squash A â P
squashelim A P f (squash x) = f x
This type allows us to simulate Agdaâs existing irrelevant arguments (see irrelevance) by replacing .A with Squash A.
LimitationsÂ¶
It is possible to define an equality type in Prop as follows:
data _â_ {â} {A : Set â} (x : A) : A â Prop â where
refl : x â x
However, the corresponding eliminator cannot be defined because of the limitations on pattern matching. As a consequence, this equality type is only useful for refuting impossible equations:
0âą1 : 0 â 1 â â„
0âą1 ()
Record TypesÂ¶
Records are types for grouping values together. They generalise the dependent product type by providing named fields and (optional) further components.
Record types can be declared using the record
keyword
record Pair (A B : Set) : Set where
field
fst : A
snd : B
This defines a new type Pair : Set â Set â Set
and two projection
functions
Pair.fst : {A B : Set} â Pair A B â A
Pair.snd : {A B : Set} â Pair A B â B
Elements of record types can be defined using a record expression
p23 : Pair Nat Nat
p23 = record { fst = 2; snd = 3 }
or using copatterns
p34 : Pair Nat Nat
Pair.fst p34 = 3
Pair.snd p34 = 4
If you use the constructor
keyword, you can also use the named
constructor to define elements of the record type:
record Pair (A B : Set) : Set where
constructor _,_
field
fst : A
snd : B
p45 : Pair Nat Nat
p45 = 4 , 5
In this sense, record types behave much like single constructor datatypes (but see Etaexpansion below).
Declaring, constructing and decomposing recordsÂ¶
Declarating record typesÂ¶
The general form of a record declaration is as follows:
record <recordname> <parameters> : Set <level> where
<directives>
constructor <constructorname>
field
<fieldname1> : <type1>
<fieldname2> : <type2>
 ...
<declarations>
All the components are optional, and can be given in any order. In particular, fields can be given in more than one block, interspersed with other declarations. Each field is a component of the record. Types of later fields can depend on earlier fields.
The directives available are etaequality
, noetaequality
(see Etaexpansion), inductive
and coinductive
(see
Recursive records).
Constructing record valuesÂ¶
Record values are constructed by giving a value for each record field:
record { <fieldname1> = <term1> ; <fieldname2> = <term2> ; ... }
where the types of the terms match the types of the fields. If a
constructor <constructorname>
has been declared for the record,
this can also be written
<constructorname> <term1> <term2> ...
For named definitions, this can also be expressed using copatterns:
<nameddef> : <recordname> <parameters>
<recordname>.<fieldname1> <nameddef> = <term1>
<recordname>.<fieldname2> <nameddef> = <term2>
...
Records can also be constructed by updating other records.
Building records from modulesÂ¶
The record { <fields> }
syntax also accepts module names. Fields
are defined using the corresponding definitions from the given module.
For instance assuming this record type R and module M:
record R : Set where
field
x : X
y : Y
z : Z
module M where
x = ...
y = ...
r : R
r = record { M; z = ... }
This construction supports any combination of explicit field definitions and applied modules. If a field is both given explicitly and available in one of the modules, then the explicit one takes precedence. If a field is available in more than one module then this is ambiguous and therefore rejected. As a consequence the order of assignments does not matter.
The modules can be both applied to arguments and have import directives such as hiding, using, and renaming. Here is a contrived example building on the example above:
module M2 (a : A) where
w = ...
z = ...
r2 : A â R
r2 a = record { M hiding (y); M2 a renaming (w to y) }
Decomposing record valuesÂ¶
With the field name, we can project the corresponding component out of a record value. It is also possible to pattern match against inductive records:
sum : Pair Nat Nat â Nat
sum (x , y) = x + y
Or, using a let binding record pattern:
sum' : Pair Nat Nat â Nat
sum' p = let (x , y) = p in x + y
Note
Naming the constructor is not required to enable pattern matching against record values. Record expressions can appear as patterns.
Record updateÂ¶
Assume that we have a record type and a corresponding value:
record MyRecord : Set where
field
a b c : Nat
old : MyRecord
old = record { a = 1; b = 2; c = 3 }
Then we can update (some of) the record valueâs fields in the following way:
new : MyRecord
new = record old { a = 0; c = 5 }
Here new
normalises to record { a = 0; b = 2; c = 5 }
. Any
expression yielding a value of type MyRecord
can be used instead of
old
. Using that records can be built from module names, together with the fact that all records define a module, this can also be written as
new' : MyRecord
new' = record { MyRecord old; a = 0; c = 5}
Record updating is not allowed to change types: the resulting value must have the same type as the original one, including the record parameters. Thus, the type of a record update can be inferred if the type of the original record can be inferred.
The record update syntax is expanded before type checking. When the expression
record old { updfields }
is checked against a record type R
, it is expanded to
let r = old in record { newfields }
where old
is required to have type R
and newfields
is defined as
follows: for each field x
in R
,
 if
x = e
is contained inupdfields
thenx = e
is included innewfields
, and otherwise if
x
is an explicit field thenx = R.x r
is included innewfields
, and if
x
is an implicit or instance field, then it is omitted fromnewfields
.
The reason for treating implicit and instance fields specially is to allow code like the following:
data Vec (A : Set) : Nat â Set where
[] : Vec A zero
_â·_ : â{n} â A â Vec A n â Vec A (suc n)
record R : Set where
field
{length} : Nat
vec : Vec Nat length
 More fields ...
xs : R
xs = record { vec = 0 â· 1 â· 2 â· [] }
ys = record xs { vec = 0 â· [] }
Without the special treatment the last expression would need to
include a new binding for length
(for instance length = _
).
Record modulesÂ¶
Along with a new type, a record declaration also defines a module with the same name, parameterised over an element of the record type containing the projection functions. This allows records to be âopenedâ, bringing the fields into scope. For instance
swap : {A B : Set} â Pair A B â Pair B A
swap p = snd , fst
where open Pair p
In the example, the record module Pair
has the shape
module Pair {A B : Set} (p : Pair A B) where
fst : A
snd : B
Itâs possible to add arbitrary definitions to the record module, by defining them inside the record declaration
record Functor (F : Set â Set) : Setâ where
field
fmap : â {A B} â (A â B) â F A â F B
_<$_ : â {A B} â A â F B â F A
x <$ fb = fmap (Î» _ â x) fb
Note
In general new definitions need to appear after the field declarations, but
simple nonrecursive function definitions without pattern matching can be
interleaved with the fields. The reason for this restriction is that the
type of the record constructor needs to be expressible using letexpressions.
In the example below Dâ
can only contain declarations for which the
generated type of mkR
is wellformed.
record R Î : Setá”ą where
constructor mkR
field fâ : Aâ
Dâ
field fâ : Aâ
mkR : â {Î} (fâ : Aâ) (let Dâ) (fâ : Aâ) â R Î
EtaexpansionÂ¶
The eta rule for a record type
record R : Set where
field
a : A
b : B
c : C
states that every x : R
is definitionally equal to record { a =
R.a x ; b = R.b x ; c = R.c x }
. By default, all (inductive) record
types enjoy etaequality if the positivity checker has confirmed it is
safe to have it. The keywords etaequality
/noetaequality
enable/disable eta rules for the record type being declared.
Recursive recordsÂ¶
Recursive records need to be declared as either inductive or coinductive.
record Tree (A : Set) : Set where
inductive
constructor tree
field
elem : A
subtrees : List (Tree A)
record Stream (A : Set) : Set where
coinductive
constructor _::_
field
head : A
tail : Stream A
Inductive records have etaequality
on by default, while
noetaequality
is the default for coinductive records. In fact,
the etaequality
and coinductive
directives are not allowed
together, since this can easily make Agda loop. This can be overridden
at your own risk by using the pragma ETA
instead.
It is possible to pattern match on inductive records, but not on coinductive ones.
Instance fieldsÂ¶
Instance fields, that is record fields marked with {{ }}
can be used to
model âsuperclassâ dependencies. For example:
record Eq (A : Set) : Set where
field
_==_ : A â A â Bool
open Eq {{...}}
record Ord (A : Set) : Set where
field
_<_ : A â A â Bool
{{eqA}} : Eq A
open Ord {{...}} hiding (eqA)
Now anytime you have a function taking an Ord A
argument the Eq A
instance
is also available by virtue of Î·expansion. So this works as you would expect:
_â€_ : {A : Set} {{OrdA : Ord A}} â A â A â Bool
x â€ y = (x == y)  (x < y)
There is a problem however if you have multiple record arguments with conflicting
instance fields. For instance, suppose we also have a Num
record with an Eq
field
record Num (A : Set) : Set where
field
fromNat : Nat â A
{{eqA}} : Eq A
open Num {{...}} hiding (eqA)
_â€3 : {A : Set} {{OrdA : Ord A}} {{NumA : Num A}} â A â Bool
x â€3 = (x == fromNat 3)  (x < fromNat 3)
Here the Eq A
argument to _==_
is not resolved since there are two conflicting
candidates: Ord.eqA OrdA
and Num.eqA NumA
. To solve this problem you can declare
instance fields as overlappable using the overlap
keyword:
record Ord (A : Set) : Set where
field
_<_ : A â A â Bool
overlap {{eqA}} : Eq A
open Ord {{...}} hiding (eqA)
record Num (A : Set) : Set where
field
fromNat : Nat â A
overlap {{eqA}} : Eq A
open Num {{...}} hiding (eqA)
_â€3 : {A : Set} {{OrdA : Ord A}} {{NumA : Num A}} â A â Bool
x â€3 = (x == fromNat 3)  (x < fromNat 3)
Whenever there are multiple valid candidates for an instance goal, if all candidates
are overlappable, the goal is solved by the leftmost candidate. In the example above
that means that the Eq A
goal is solved by the instance from the Ord
argument.
Clauses for instance fields can be omitted when defining values of record
types. For instance we can define Nat
instances for Eq
, Ord
and
Num
as follows, leaving out cases for the eqA
fields:
instance
EqNat : Eq Nat
_==_ {{EqNat}} = Agda.Builtin.Nat._==_
OrdNat : Ord Nat
_<_ {{OrdNat}} = Agda.Builtin.Nat._<_
NumNat : Num Nat
fromNat {{NumNat}} n = n
ReflectionÂ¶
Builtin typesÂ¶
NamesÂ¶
The builtin QNAME
type represents quoted names and comes equipped with
equality, ordering, and a show function.
postulate Name : Set
{# BUILTIN QNAME Name #}
primitive
primQNameEquality : Name â Name â Bool
primQNameLess : Name â Name â Bool
primShowQName : Name â String
The fixity of a name can also be retrived.
primitive
primQNameFixity : Name â Fixity
To define a decidable propositional equality with the option safe
,
one can use the conversion to a pair of builtin 64bit machine words
primitive
primQNameToWord64s : Name â ÎŁ Word64 (Î» _ â Word64)
with the injectivity proof in the Properties
module.:
primitive
primQNameToWord64sInjective : â a b â primQNameToWord64s a âĄ primQNameToWord64s b â a âĄ b
Name literals are created using the quote
keyword and can appear both in
terms and in patterns
nameOfNat : Name
nameOfNat = quote Nat
isNat : Name â Bool
isNat (quote Nat) = true
isNat _ = false
Note that the name being quoted must be in scope.
MetavariablesÂ¶
Metavariables are represented by the builtin AGDAMETA
type. They have
primitive equality, ordering, show, and conversion to Nat:
postulate Meta : Set
{# BUILTIN AGDAMETA Meta #}
primitive
primMetaEquality : Meta â Meta â Bool
primMetaLess : Meta â Meta â Bool
primShowMeta : Meta â String
primMetaToNat : Meta â Nat
Builtin metavariables show up in reflected terms. In Properties
, there is a proof of injectivity
of primMetaToNat
primitive
primMetaToNatInjective : â a b â primMetaToNat a âĄ primMetaToNat b â a âĄ b
which can be used to define a decidable propositional equality with the option safe
.
LiteralsÂ¶
Literals are mapped to the builtin AGDALITERAL
datatype. Given the appropriate
builtin binding for the types Nat
, Float
, etc, the AGDALITERAL
datatype
has the following shape:
data Literal : Set where
nat : (n : Nat) â Literal
word64 : (n : Word64) â Literal
float : (x : Float) â Literal
char : (c : Char) â Literal
string : (s : String) â Literal
name : (x : Name) â Literal
meta : (x : Meta) â Literal
{# BUILTIN AGDALITERAL Literal #}
{# BUILTIN AGDALITNAT nat #}
{# BUILTIN AGDALITWORD64 word64 #}
{# BUILTIN AGDALITFLOAT float #}
{# BUILTIN AGDALITCHAR char #}
{# BUILTIN AGDALITSTRING string #}
{# BUILTIN AGDALITQNAME name #}
{# BUILTIN AGDALITMETA meta #}
ArgumentsÂ¶
Arguments can be (visible), {hidden}, or {{instance}}:
data Visibility : Set where
visible hidden instanceâČ : Visibility
{# BUILTIN HIDING Visibility #}
{# BUILTIN VISIBLE visible #}
{# BUILTIN HIDDEN hidden #}
{# BUILTIN INSTANCE instanceâČ #}
Arguments can be relevant or irrelevant:
data Relevance : Set where
relevant irrelevant : Relevance
{# BUILTIN RELEVANCE Relevance #}
{# BUILTIN RELEVANT relevant #}
{# BUILTIN IRRELEVANT irrelevant #}
Visibility and relevance characterise the behaviour of an argument:
data ArgInfo : Set where
arginfo : (v : Visibility) (r : Relevance) â ArgInfo
data Arg (A : Set) : Set where
arg : (i : ArgInfo) (x : A) â Arg A
{# BUILTIN ARGINFO ArgInfo #}
{# BUILTIN ARGARGINFO arginfo #}
{# BUILTIN ARG Arg #}
{# BUILTIN ARGARG arg #}
PatternsÂ¶
Reflected patterns are bound to the AGDAPATTERN
builtin using the
following data type.
data Pattern : Set where
con : (c : Name) (ps : List (Arg Pattern)) â Pattern
dot : Pattern
var : (s : String) â Pattern
lit : (l : Literal) â Pattern
proj : (f : Name) â Pattern
absurd : Pattern
{# BUILTIN AGDAPATTERN Pattern #}
{# BUILTIN AGDAPATCON con #}
{# BUILTIN AGDAPATDOT dot #}
{# BUILTIN AGDAPATVAR var #}
{# BUILTIN AGDAPATLIT lit #}
{# BUILTIN AGDAPATPROJ proj #}
{# BUILTIN AGDAPATABSURD absurd #}
Name abstractionÂ¶
data Abs (A : Set) : Set where
abs : (s : String) (x : A) â Abs A
{# BUILTIN ABS Abs #}
{# BUILTIN ABSABS abs #}
TermsÂ¶
Terms, sorts and clauses are mutually recursive and mapped to the AGDATERM
,
AGDASORT
and AGDACLAUSE
builtins respectively. Types are simply
terms. Terms use de Bruijn indices to represent variables.
data Term : Set
data Sort : Set
data Clause : Set
Type = Term
data Term where
var : (x : Nat) (args : List (Arg Term)) â Term
con : (c : Name) (args : List (Arg Term)) â Term
def : (f : Name) (args : List (Arg Term)) â Term
lam : (v : Visibility) (t : Abs Term) â Term
patlam : (cs : List Clause) (args : List (Arg Term)) â Term
pi : (a : Arg Type) (b : Abs Type) â Term
agdasort : (s : Sort) â Term
lit : (l : Literal) â Term
meta : (x : Meta) â List (Arg Term) â Term
unknown : Term  Treated as '_' when unquoting.
data Sort where
set : (t : Term) â Sort  A Set of a given (possibly neutral) level.
lit : (n : Nat) â Sort  A Set of a given concrete level.
unknown : Sort
data Clause where
clause : (ps : List (Arg Pattern)) (t : Term) â Clause
absurdclause : (ps : List (Arg Pattern)) â Clause
{# BUILTIN AGDASORT Sort #}
{# BUILTIN AGDATERM Term #}
{# BUILTIN AGDACLAUSE Clause #}
{# BUILTIN AGDATERMVAR var #}
{# BUILTIN AGDATERMCON con #}
{# BUILTIN AGDATERMDEF def #}
{# BUILTIN AGDATERMMETA meta #}
{# BUILTIN AGDATERMLAM lam #}
{# BUILTIN AGDATERMEXTLAM patlam #}
{# BUILTIN AGDATERMPI pi #}
{# BUILTIN AGDATERMSORT agdasort #}
{# BUILTIN AGDATERMLIT lit #}
{# BUILTIN AGDATERMUNSUPPORTED unknown #}
{# BUILTIN AGDASORTSET set #}
{# BUILTIN AGDASORTLIT lit #}
{# BUILTIN AGDASORTUNSUPPORTED unknown #}
{# BUILTIN AGDACLAUSECLAUSE clause #}
{# BUILTIN AGDACLAUSEABSURD absurdclause #}
Absurd lambdas Î» ()
are quoted to extended lambdas with an absurd clause.
The builtin constructors AGDATERMUNSUPPORTED
and AGDASORTUNSUPPORTED
are translated to meta variables when unquoting.
DeclarationsÂ¶
There is a builtin type AGDADEFINITION
representing definitions. Values of
this type is returned by the AGDATCMGETDEFINITION
builtin described
below.
data Definition : Set where
function : (cs : List Clause) â Definition
datatype : (pars : Nat) (cs : List Name) â Definition  parameters and constructors
recordtype : (c : Name) (fs : List (Arg Name)) â  c: name of record constructor
Definition  fs: fields
datacons : (d : Name) â Definition  d: name of data type
axiom : Definition
primfun : Definition
{# BUILTIN AGDADEFINITION Definition #}
{# BUILTIN AGDADEFINITIONFUNDEF function #}
{# BUILTIN AGDADEFINITIONDATADEF datatype #}
{# BUILTIN AGDADEFINITIONRECORDDEF recordtype #}
{# BUILTIN AGDADEFINITIONDATACONSTRUCTOR datacons #}
{# BUILTIN AGDADEFINITIONPOSTULATE axiom #}
{# BUILTIN AGDADEFINITIONPRIMITIVE primfun #}
Type errorsÂ¶
Type checking computations (see below)
can fail with an error, which is a list of ErrorPart
s. This
allows metaprograms to generate nice errors without having to
implement pretty printing for reflected terms.
 Error messages can contain embedded names and terms.
data ErrorPart : Set where
strErr : String â ErrorPart
termErr : Term â ErrorPart
nameErr : Name â ErrorPart
{# BUILTIN AGDAERRORPART ErrorPart #}
{# BUILTIN AGDAERRORPARTSTRING strErr #}
{# BUILTIN AGDAERRORPARTTERM termErr #}
{# BUILTIN AGDAERRORPARTNAME nameErr #}
Type checking computationsÂ¶
Metaprograms, i.e. programs that create other programs, run in a builtin type
checking monad TC
:
postulate
TC : â {a} â Set a â Set a
returnTC : â {a} {A : Set a} â A â TC A
bindTC : â {a b} {A : Set a} {B : Set b} â TC A â (A â TC B) â TC B
{# BUILTIN AGDATCM TC #}
{# BUILTIN AGDATCMRETURN returnTC #}
{# BUILTIN AGDATCMBIND bindTC #}
The TC
monad provides an interface to the Agda type checker using the
following primitive operations:
postulate
 Unify two terms, potentially solving metavariables in the process.
unify : Term â Term â TC â€
 Throw a type error. Can be caught by catchTC.
typeError : â {a} {A : Set a} â List ErrorPart â TC A
 Block a type checking computation on a metavariable. This will abort
 the computation and restart it (from the beginning) when the
 metavariable is solved.
blockOnMeta : â {a} {A : Set a} â Meta â TC A
 Prevent current solutions of metavariables from being rolled back in
 case 'blockOnMeta' is called.
commitTC : TC â€
 Backtrack and try the second argument if the first argument throws a
 type error.
catchTC : â {a} {A : Set a} â TC A â TC A â TC A
 Infer the type of a given term
inferType : Term â TC Type
 Check a term against a given type. This may resolve implicit arguments
 in the term, so a new refined term is returned. Can be used to create
 new metavariables: newMeta t = checkType unknown t
checkType : Term â Type â TC Term
 Compute the normal form of a term.
normalise : Term â TC Term
 Compute the weak head normal form of a term.
reduce : Term â TC Term
 Get the current context. Returns the context in reverse order, so that
 it is indexable by deBruijn index. Note that the types in the context are
 valid in the rest of the context. To use in the current context they need
 to be weakened by 1 + their position in the list.
getContext : TC (List (Arg Type))
 Extend the current context with a variable of the given type.
extendContext : â {a} {A : Set a} â Arg Type â TC A â TC A
 Set the current context. Takes a context telescope with the outermost
 entry first, in contrast to 'getContext'. Each type should be valid in the
 context formed by the preceding elements in the list.
inContext : â {a} {A : Set a} â List (Arg Type) â TC A â TC A
 Quote a value, returning the corresponding Term.
quoteTC : â {a} {A : Set a} â A â TC Term
 Unquote a Term, returning the corresponding value.
unquoteTC : â {a} {A : Set a} â Term â TC A
 Create a fresh name.
freshName : String â TC Name
 Declare a new function of the given type. The function must be defined
 later using 'defineFun'. Takes an Arg Name to allow declaring instances
 and irrelevant functions. The Visibility of the Arg must not be hidden.
declareDef : Arg Name â Type â TC â€
 Declare a new postulate of the given type. The Visibility of the Arg
 must not be hidden. It fails when executed from commandline with safe
 option.
declarePostulate : Arg Name â Type â TC â€
 Define a declared function. The function may have been declared using
 'declareDef' or with an explicit type signature in the program.
defineFun : Name â List Clause â TC â€
 Get the type of a defined name. Replaces 'primNameType'.
getType : Name â TC Type
 Get the definition of a defined name. Replaces 'primNameDefinition'.
getDefinition : Name â TC Definition
 Check if a name refers to a macro
isMacro : Name â TC Bool
 Change the behaviour of inferType, checkType, quoteTC, getContext
 to normalise (or not) their results. The default behaviour is no
 normalisation.
withNormalisation : â {a} {A : Set a} â Bool â TC A â TC A
 Prints the third argument to the debug buffer in Emacs
 if the verbosity level (set by the v flag to Agda)
 is higher than the second argument. Note that Level 0 and 1 are printed
 to the info buffer instead. For instance, giving v a.b.c:10 enables
 printing from debugPrint "a.b.c.d" 10 msg.
debugPrint : String â Nat â List ErrorPart â TC â€
 Fail if the given computation gives rise to new, unsolved
 "blocking" constraints.
noConstraints : â {a} {A : Set a} â TC A â TC A
 Tries to solve all constraints.
solveConstraints : TC â€
 Wakes up all constraints mentioning the given metavariables,
 and then tries to solve all awake constraints.
solveConstraintsMentioning : List Meta â TC â€
 Run the given TC action and return the first component. Resets to
 the old TC state if the second component is 'false', or keep the
 new TC state if it is 'true'.
runSpeculative : â {a} {A : Set a} â TC (ÎŁ A Î» _ â Bool) â TC A
{# BUILTIN AGDATCMUNIFY unify #}
{# BUILTIN AGDATCMTYPEERROR typeError #}
{# BUILTIN AGDATCMBLOCKONMETA blockOnMeta #}
{# BUILTIN AGDATCMCATCHERROR catchTC #}
{# BUILTIN AGDATCMINFERTYPE inferType #}
{# BUILTIN AGDATCMCHECKTYPE checkType #}
{# BUILTIN AGDATCMNORMALISE normalise #}
{# BUILTIN AGDATCMREDUCE reduce #}
{# BUILTIN AGDATCMGETCONTEXT getContext #}
{# BUILTIN AGDATCMEXTENDCONTEXT extendContext #}
{# BUILTIN AGDATCMINCONTEXT inContext #}
{# BUILTIN AGDATCMQUOTETERM quoteTC #}
{# BUILTIN AGDATCMUNQUOTETERM unquoteTC #}
{# BUILTIN AGDATCMFRESHNAME freshName #}
{# BUILTIN AGDATCMDECLAREDEF declareDef #}
{# BUILTIN AGDATCMDECLAREPOSTULATE declarePostulate #}
{# BUILTIN AGDATCMDEFINEFUN defineFun #}
{# BUILTIN AGDATCMGETTYPE getType #}
{# BUILTIN AGDATCMGETDEFINITION getDefinition #}
{# BUILTIN AGDATCMCOMMIT commitTC #}
{# BUILTIN AGDATCMISMACRO isMacro #}
{# BUILTIN AGDATCMWITHNORMALISATION withNormalisation #}
{# BUILTIN AGDATCMDEBUGPRINT debugPrint #}
{# BUILTIN AGDATCMNOCONSTRAINTS noConstraints #}
{# BUILTIN AGDATCMSOLVECONSTRAINTS solveConstraints #}
{# BUILTIN AGDATCMSOLVECONSTRAINTSMENTIONING solveConstraintsMentioning #}
{# BUILTIN AGDATCMRUNSPECULATIVE runSpeculative #}
MetaprogrammingÂ¶
There are three ways to run a metaprogram (TC
computation). To run
a metaprogram in a term position you use a macro. To
run metaprograms to create toplevel definitions you can use the
unquoteDecl
and unquoteDef
primitives (see Unquoting
Declarations).
MacrosÂ¶
Macros are functions of type tâ â tâ â .. â Term â TC â€
that are defined in
a macro
block. The last argument is supplied by the type checker and will
be the representation of a metavariable that should be instantiated with the
result of the macro.
Macro application is guided by the type of the macro, where Term
and
Name
arguments are quoted before passed to the macro. Arguments of any
other type are preserved asis.
For example, the macro application f u v w
where
f : Term â Name â Bool â Term â TC â€
desugars into:
unquote (f (quoteTerm u) (quote v) w)
where quoteTerm u
takes a u
of arbitrary type and returns its
representation in the Term
data type, and unquote m
runs a computation
in the TC
monad. Specifically, when checking unquote m : A
for some
type A
the type checker proceeds as follows:
 Check
m : Term â TC â€
. Create a fresh metavariable
hole : A
. Let
qhole : Term
be the quoted representation ofhole
. Execute
m qhole
. Return (the now hopefully instantiated)
hole
.
Reflected macro calls are constructed using the def
constructor, so given a
macro g : Term â TC â€
the term def (quote g) []
unquotes to a macro
call to g
.
Note
The quoteTerm
and unquote
primitives are available in the language,
but it is recommended to avoid using them in favour of macros.
Limitations:
 Macros cannot be recursive. This can be worked around by defining the recursive function outside the macro block and have the macro call the recursive function.
Silly example:
macro
plustotimes : Term â Term â TC â€
plustotimes (def (quote _+_) (a â· b â· [])) hole = unify hole (def (quote _*_) (a â· b â· []))
plustotimes v hole = unify hole v
thm : (a b : Nat) â plustotimes (a + b) âĄ a * b
thm a b = refl
Macros lets you write tactics that can be applied without any syntactic overhead. For instance, suppose you have a solver:
magic : Type â Term
that takes a reflected goal and outputs a proof (when successful). You can then define the following macro:
macro
bymagic : Term â TC â€
bymagic hole =
bindTC (inferType hole) Î» goal â
unify hole (magic goal)
This lets you apply the magic tactic as a normal function:
thm : ÂŹ P âĄ NP
thm = bymagic
Unquoting DeclarationsÂ¶
While macros let you write metaprograms to create terms, it is also useful to
be able to create toplevel definitions. You can do this from a macro using the
declareDef
and defineFun
primitives, but there is no way to bring such
definitions into scope. For this purpose there are two toplevel primitives
unquoteDecl
and unquoteDef
that runs a TC
computation in a
declaration position. They both have the same form:
unquoteDecl xâ .. xâ = m
unquoteDef xâ .. xâ = m
except that the list of names can be empty for unquoteDecl
, but not for
unquoteDef
. In both cases m
should have type TC â€
. The main
difference between the two is that unquoteDecl
requires m
to both
declare (with declareDef
) and define (with defineFun
) the xá”ą
whereas unquoteDef
expects the xá”ą
to be already declared. In other
words, unquoteDecl
brings the xá”ą
into scope, but unquoteDef
requires them to already be in scope.
In m
the xá”ą
stand for the names of the functions being defined (i.e.
xá”ą : Name
) rather than the actual functions.
One advantage of unquoteDef
over unquoteDecl
is that
unquoteDef
is allowed in mutual blocks, allowing mutually
recursion between generated definitions and handwritten definitions.
Example usage:
 Defining: idname {A} x = x
defId : (idname : Name) â TC â€
defId idname = do
defineFun idname
[ clause
( arg (arginfo hidden relevant) (var "A")
â· arg (arginfo visible relevant) (var "x")
â· [] )
(var 0 [])
]
id : {A : Set} (x : A) â A
unquoteDef id = defId id
mkId : (idname : Name) â TC â€
mkId idname = do
ty â quoteTC ({A : Set} (x : A) â A)
declareDef (arg (arginfo visible relevant) idname) ty
defId idname
unquoteDecl idâČ = mkId idâČ
RewritingÂ¶
This is the stub for rewrite
and the associated REWRITE
builtin. You might also be looking for the documentation on the rewrite
constuct.
Note
This is a stub.
Safe AgdaÂ¶
By using the option safe
(as a pragma option, or on the
commandline), a user can specify that Agda should ensure that
features leading to possible inconsistencies should be disabled.
Here is a list of the features safe
is incompatible with:
postulate
; can be used to assume any axiom.allowunsolvedmetas
; forces Agda to accept unfinished proofs.allowincompletematches
; forces Agda to accept unfinished proofs.nopositivitycheck
; makes it possible to write nonterminating programs by structural âinductionâ on non strictly positive datatypes.noterminationcheck
; gives loopy programs any type.typeintype
andomegainomega
; allow the user to encode the GirardHurken paradox.injectivetypeconstructors
; together with excluded middle leads to an inconsistency via ChungKil Hurâs construction.guardedness
together withsizedtypes
; currently can be used to define a type which is both inductive and coinductive, which leads to an inconsistency. This might be fixed in the future.experimentalirrelevance
andirrelevantprojections
; enables potentially unsound irrelevance features (irrelevant levels, irrelevant data matching, and projection of irrelevant record fields, respectively).rewriting
; turns any equation into one that holds definitionally. It can at the very least break convergence.cubical
together withwithK
; the univalence axiom is provable using cubical constructions, which falsifies the K axiom. The
primEraseEquality
primitive together withwithoutK
; usingprimEraseEquality
, one can derive the K axiom.
The option safe
is coinfective (see
Consistency checking of options used); if a module is declared safe,
then all its imported modules must also be declared safe.
Note
The guardedness
and sizedtypes
options are both on by default.
However, unless they have been set explicitly by the user, setting the
safe
option will turn them both off. That is to say that
{# OPTIONS safe #}
will correspond to safe
, noguardedness
, and nosizedtypes
.
When both
{# OPTIONS safe guardedness #}
and
{# OPTIONS guardedness safe #}
will turn on safe
, guardedness
, and nosizedtypes
.
Setting both sizedtypes
and guardedness
whilst demanding that the
module is safe
will lead to an error as combining these options currently
is inconsistent.
Sized TypesÂ¶
Note
This is a stub.
Sizes help the termination checker by tracking the depth of data structures across definition boundaries.
The builtin combinators for sizes are described in Sized types.
Example for coinduction: finite languagesÂ¶
See Abel 2017 and Traytel 2017.
Decidable languages can be represented as infinite trees. Each node has as many
children as the number of characters in the alphabet A
. Each path from the root
of the tree to a node determines a possible word in the language. Each node
has a boolean label, which is true
if and only if the word corresponding
to that node is in the language. In particular, the root node of the tree
is labelled true
if and only if the word Î”
belongs to the language.
These infinite trees can be represented as the following coinductive datatype:
record Lang (i : Size) (A : Set) : Set where
coinductive
field
Îœ : Bool
ÎŽ : â{j : Size< i} â A â Lang j A
open Lang
As we said before, given a language a : Lang A
, Îœ a âĄ true
iff
Î” â a
. On the other hand, the language ÎŽ a x : Lang A
is the
Brzozowski derivative of a
with respect to the character x
, that is,
w â ÎŽ a x
iff xw â a
.
With this data type, we can define some regular languages. The first one, the
empty language, contains no words; so all the nodes are labelled false
:
â
: â {i A} â Lang i A
Îœ â
= false
ÎŽ â
_ = â
The second one is the language containing a single word; the empty word. The
root node is labelled true
, and all the others are labelled false
:
Î” : â {i A} â Lang i A
Îœ Î” = true
ÎŽ Î” _ = â
To compute the union (or sum) of two languages, we do a pointwise or
operation on the labels of their nodes:
_+_ : â {i A} â Lang i A â Lang i A â Lang i A
Îœ (a + b) = Îœ a âš Îœ b
ÎŽ (a + b) x = ÎŽ a x + ÎŽ b x
infixl 10 _+_
Now, lets define concatenation.
The base case (Îœ
) is straightforward: Î” â a Â· b
iff Î” â a
and Î” â b
.
For the derivative (ÎŽ
), assume that we have a word w
, w â ÎŽ (a Â· b)
x
. This means that xw = Î±ÎČ
, with Î± â a
and ÎČ â b
.
We have to consider two cases:
Î” â a
. Then, either:
Î± = Î”
, andÎČ = xw
, wherew â ÎŽ b x
.Î± = xÎ±â
, withÎ±â â ÎŽ a x
, andw = Î±âÎČ â ÎŽ a x Â· b
.Î” â a
. Then, only the second case above is possible:
Î± = xÎ±â
, withÎ±â â ÎŽ a x
, andw = Î±âÎČ â ÎŽ a x Â· b
.
_Â·_ : â {i A} â Lang i A â Lang i A â Lang i A
Îœ (a Â· b) = Îœ a â§ Îœ b
ÎŽ (a Â· b) x = if Îœ a then ÎŽ a x Â· b + ÎŽ b x else ÎŽ a x Â· b
infixl 20 _Â·_
Here is where sized types really shine. Without sized types, the termination
checker would not be able to recognize that _+_
or if_then_else
are not
inspecting the tree, which could render the definition nonproductive. By contrast,
with sized types, we know that the a + b
is defined to the same
depth as a
and b
are.
In a similar spirit, we can define the Kleene star:
_* : â {i A} â Lang i A â Lang i A
Îœ (a *) = true
ÎŽ (a *) x = ÎŽ a x Â· a *
infixl 30 _*
Again, because the types tell us that _Â·_ preserves the size of its
inputs, we can have the recursive call to a *
under a function
call to _Â·_
.
TestingÂ¶
First, we want to give a precise notion of membership in a language.
We consider a word as a List
of characters.
_â_ : â {i} {A} â List i A â Lang i A â Bool
[] â a = Îœ a
(x â· w) â a = w â ÎŽ a x
Note how the size of the word we test for membership cannot be larger than the depth to which the language tree is defined.
If we want to use regular, nonsized lists, we need to ask for the
language to have size â
.
_â_ : â {A} â List A â Lang â A â Bool
[] â a = Îœ a
(x â· w) â a = w â ÎŽ a x
Intuitively, â
is a Size
larger than the size of any term
than one could possibly define in Agda.
Now, letâs consider binary strings as words. First, we
define the languages âŠ x â§
containing the single word âxâ of length 1,
for alphabet A = Bool
:
âŠ_â§ : â {i} â Bool â Lang i Bool
Îœ âŠ _ â§ = false
ÎŽ âŠ false â§ false = Î”
ÎŽ âŠ true â§ true = Î”
ÎŽ âŠ false â§ true = â
ÎŽ âŠ true â§ false = â
Now we can define the bipbop language, consisting of strings of even length alternating letters âtrueâ and âfalseâ.
bipbop = (âŠ true â§ Â· âŠ false â§)*
Letâs test a few words for membership in the language bipbop
!
testâ : (true â· false â· true â· false â· true â· false â· []) â bipbop âĄ true
testâ = refl
testâ : (true â· false â· true â· false â· true â· []) â bipbop âĄ false
testâ = refl
testâ : (true â· true â· false â· []) â bipbop âĄ false
testâ = refl
Syntactic SugarÂ¶
DonotationÂ¶
A doblock consists of the layout keyword
do
followed by a sequence of dostatements, where
dostmt ::= pat â expr [where lamclauses]
 let decls
 expr
lamclause ::= pat â expr
The where
clause of a bind is used to handle the cases not matched by the pattern
left of the arrow. See details below.
Note
Arrows can use either unicode (â
/â
) or ASCII (<
/>
) variants.
For example:
filter : {A : Set} â (A â Bool) â List A â List A
filter p xs = do
x â xs
true â p x â· []
where false â []
x â· []
Donotation is desugared before scope checking and is translated into calls to _>>=_
and _>>_
, whatever those happen to be bound in the context of the doblock. This means that doblocks are not tied to any particular notion of monad. In fact if there are no monadic statements in the do block it can be used as sugar for a let
:
puredo : Nat â Nat
puredo n = do
let p2 m = m * m
p4 m = p2 (p2 m)
p4 n
checkpuredo : puredo 5 âĄ 625
checkpuredo = refl
DesugaringÂ¶
Statement  Sugar  Desugars to 

Simple bind  do x â m
m'

m >>= Î» x â
m'

Pattern bind  do p â m
where pá”ą â má”ą
m'

m >>= Î» where
p â m'
pá”ą â má”ą

Absurd match  do () â m

m >>= Î» ()

Nonbinding statement  do m
m'

m >>
m'

Let  do let ds
m'

let ds in
m'

If the pattern in the bind is exhaustive, the whereclause can be omitted.
ExampleÂ¶
Donotation becomes quite powerful together with pattern matching on indexed data. As an example, let us write a correctbyconstruction type checker for simply typed Î»calculus.
First we define the raw terms, using de Bruijn indices for variables and explicit type annotations on the lambda:
infixr 6 _=>_
data Type : Set where
nat : Type
_=>_ : (A B : Type) â Type
data Raw : Set where
var : (x : Nat) â Raw
lit : (n : Nat) â Raw
suc : Raw
app : (s t : Raw) â Raw
lam : (A : Type) (t : Raw) â Raw
Next up, welltyped terms:
Context = List Type
 A proof of x â xs is the index into xs where x is located.
infix 2 _â_
data _â_ {A : Set} (x : A) : List A â Set where
zero : â {xs} â x â x â· xs
suc : â {y xs} â x â xs â x â y â· xs
data Term (Î : Context) : Type â Set where
var : â {A} (x : A â Î) â Term Î A
lit : (n : Nat) â Term Î nat
suc : Term Î (nat => nat)
app : â {A B} (s : Term Î (A => B)) (t : Term Î A) â Term Î B
lam : â A {B} (t : Term (A â· Î) B) â Term Î (A => B)
Given a welltyped term we can mechaincally erase all the type information (except the annotation on the lambda) to get the corresponding raw term:
rawIndex : â {A} {x : A} {xs} â x â xs â Nat
rawIndex zero = zero
rawIndex (suc i) = suc (rawIndex i)
eraseTypes : â {Î A} â Term Î A â Raw
eraseTypes (var x) = var (rawIndex x)
eraseTypes (lit n) = lit n
eraseTypes suc = suc
eraseTypes (app s t) = app (eraseTypes s) (eraseTypes t)
eraseTypes (lam A t) = lam A (eraseTypes t)
Now weâre ready to write the type checker. The goal is to have a function that takes a raw term and either fails with a type error, or returns a welltyped term that erases to the raw term it started with. First, lets define the return type. Itâs parameterised by a context and the raw term to be checked:
data WellTyped Î e : Set where
ok : (A : Type) (t : Term Î A) â eraseTypes t âĄ e â WellTyped Î e
Weâre going to need a corresponding type for variables:
data InScope Î n : Set where
ok : (A : Type) (i : A â Î) â rawIndex i âĄ n â InScope Î n
Lets also have a type synonym for the case when the erasure proof is refl
:
infix 2 _ofType_
pattern _ofType_ x A = ok A x refl
Since this is a donotation example we had better have a monad. Lets use the either monad with string errors:
TC : Set â Set
TC A = Either String A
typeError : â {A} â String â TC A
typeError = left
For the monad operations, we are using instance arguments to infer which monad is being used.
We are going to need to compare types for equality. This is our first opportunity to take advantage of pattern matching binds:
_=?=_ : (A B : Type) â TC (A âĄ B)
nat =?= nat = pure refl
nat =?= (_ => _) = typeError "type mismatch: nat ââ _ => _"
(_ => _) =?= nat = typeError "type mismatch: _ => _ â nat"
(A => B) =?= (Aâ => Bâ) = do
refl â A =?= Aâ
refl â B =?= Bâ
pure refl
We will also need to look up variables in the context:
lookupVar : â Î n â TC (InScope Î n)
lookupVar [] n = typeError "variable out of scope"
lookupVar (A â· Î) zero = pure (zero ofType A)
lookupVar (A â· Î) (suc n) = do
i ofType B â lookupVar Î n
pure (suc i ofType B)
Note how the proof obligation that the welltyped deBruijn index erases to
the given raw index is taken care of completely under the hood (in this case
by the refl
pattern in the ofType
synonym).
Finally we are ready to implement the actual type checker:
infer : â Î e â TC (WellTyped Î e)
infer Î (var x) = do
i ofType A â lookupVar Î x
pure (var i ofType A)
infer Î (lit n) = pure (lit n ofType nat)
infer Î suc = pure (suc ofType nat => nat)
infer Î (app e eâ) = do
s ofType A => B â infer Î e
where _ ofType nat â typeError "numbers cannot be applied to arguments"
t ofType Aâ â infer Î eâ
refl â A =?= Aâ
pure (app s t ofType B)
infer Î (lam A e) = do
t ofType B â infer (A â· Î) e
pure (lam A t ofType A => B)
In the app
case we use a whereclause to handle the error case when the
function to be applied is welltyped, but does not have a function type.
Idiom bracketsÂ¶
Idiom brackets is a notation used to make it more convenient to work with applicative
functors, i.e. functors F
equipped with two operations
pure : â {A} â A â F A
_<*>_ : â {A B} â F (A â B) â F A â F B
As donotation, idiom brackets desugar before scope checking, so whatever the names pure
and _<*>_
are bound to gets used when desugaring the idiom brackets.
The syntax for idiom brackets is
( e aâ .. aâ )
or using unicode lens brackets âŠ
(U+2987) and âŠ
(U+2988):
âŠ e aâ .. aâ âŠ
This expands to (assuming left associative _<*>_
)
pure e <*> aâ <*> .. <*> aâ
Idiom brackets work well with operators, for instance
( if a then b else c )
desugars to
pure if_then_else_ <*> a <*> b <*> c
Idiom brackets also support none or multiple applications. If the applicative functor has an additional binary operation
_<>_ : â {A B} â F A â F A â F A
then idiom brackets support multiple applications separated by a vertical bar 
, i.e.
( eâ aâ .. aâ  eâ aâ .. aâ  ..  eâ aâ .. aâ )
which expands to (assuming right associative _<>_
)
(pure eâ <*> aâ <*> .. <*> aâ) <> ((pure eâ <*> aâ <*> .. <*> aâ) <> (pure eâ <*> aâ <*> .. <*> aâ))
Idiom brackets without any application ()
or âŠâŠ
expend to empty
if
empty : â {A} â F A
is in scope. An applicative functor with empty
and _<>_
is typically
called Alternative
.
Note that pure
, _<*>_
, and _<>_
need not be in scope to use ()
.
Limitations:
Binding syntax and operator sections cannot appear immediately inside idiom brackets.
The toplevel application inside idiom brackets cannot include implicit applications, so
( foo {x = e} a b )
is illegal. In case the
e
is pure you can write( (foo {x = e}) a b )
which desugars to
pure (foo {x = e}) <*> a <*> b
Syntax DeclarationsÂ¶
Note
This is a stub
It is now possible to declare userdefined syntax that binds identifiers. Example:
postulate
State : Set â Set â Set
put : â {S} â S â State S â€
get : â {S} â State S S
return : â {A S} â A â State S A
bind : â {A B S} â State S B â (B â State S A) â State S A
syntax bind eâ (Î» x â eâ) = x â eâ , eâ
increment : State â â€
increment = x â get ,
put (suc x)
The syntax declaration for bind
implies that x
is in scope in
eâ
, but not in eâ
.
You can give fixity declarations along with syntax declarations:
infixr 40 bind
syntax bind eâ (Î» x â eâ) = x â eâ , eâ
The fixity applies to the syntax, not the name; syntax declarations are also restricted to ordinary, nonoperator names. The following declaration is disallowed:
syntax _==_ x y = x === y
Syntax declarations must also be linear; the following declaration is disallowed:
syntax wrong x = x + x
Syntax declarations can have implicit arguments. For example:
id : â {a}{A : Set a} > A > A
id x = x
syntax id {A} x = x â A
TelescopesÂ¶
Note
This is a stub.
Irrefutable Patterns in Binding PositionsÂ¶
Since Agda 2.6.1, irrefutable patterns can be used at every binding site in a telescope to take the bound value of record type apart. The type of the second projection out of a dependent pair will for instance naturally mention the value of the first projection. Its type can be defined directly using an irrefutable pattern as follows:
projâ : ((a , _) : ÎŁ A B) â B a
And this second projection can be implemented with a lambaabstraction using one of these irrefutable patterns taking the pair apart:
projâ = Î» (_ , b) â b
Using an aspattern makes it possible to name the argument and to take it apart at the same time. We can for instance prove that any pair is equal to the pairing of its first and second projections, a property commonly called etaequality:
eta : (p@(a , b) : ÎŁ A B) â p âĄ (a , b)
eta p = refl
Termination CheckingÂ¶
Not all recursive functions are permitted  Agda accepts only these recursive schemas that it can mechanically prove terminating.
Primitive recursionÂ¶
In the simplest case, a given argument must be exactly one constructor smaller in each recursive call. We call this scheme primitive recursion. A few correct examples:
plus : Nat â Nat â Nat
plus zero m = m
plus (suc n) m = suc (plus n m)
natEq : Nat â Nat â Bool
natEq zero zero = true
natEq zero (suc m) = false
natEq (suc n) zero = false
natEq (suc n) (suc m) = natEq n m
Both plus
and natEq
are defined by primitive recursion.
The recursive call in plus
is OK because n
is a subexpression
of suc n
(so n
is structurally smaller than suc n
). So
every time plus is recursively called the first argument is getting
smaller and smaller. Since a natural number can only have a finite
number of suc constructors we know that plus will always terminate.
natEq
terminates for the same reason, but in this case we can say
that both the first and second arguments of natEq are decreasing.
Structural recursionÂ¶
Agdaâs termination checker allows more definitions than just primitive recursive ones â it allows structural recursion.
This means that we require recursive calls to be on a (strict)
subexpression of the argument (see fib
below)  this is more
general that just taking away one constructor at a time.
fib : Nat â Nat
fib zero = zero
fib (suc zero) = suc zero
fib (suc (suc n)) = plus (fib n) (fib (suc n))
It also means that arguments may decrease in an lexicographic order 
this can be thought of as nested primitive recursion (see ack
below).
ack : Nat â Nat â Nat
ack zero m = suc m
ack (suc n) zero = ack n (suc zero)
ack (suc n) (suc m) = ack n (ack (suc n) m)
In ack
either the first argument decreases or it stays the same and the second one decreases.
This is the same as a lexicographic ordering.
WithfunctionsÂ¶
Pragmas and OptionsÂ¶
The
NON_TERMINATING
pragmaThis is a safer version of TERMINATING which doesnât treat the affected functions as terminating. This means that
NON_TERMINATING
functions do not reduce during type checking. They do reduce at runtime and when invokingCc Cn
at toplevel (but not in a hole). The pragma was added in Agda 2.4.2.
The
TERMINATING
pragmaSwitches off termination checker for individual function definitions and mutual blocks and marks them as terminating. Since Agda 2.4.2.1 replaced the
NO_TERMINATION_CHECK
pragma.The pragma must precede a function definition or a mutual block. The pragma cannot be used in
safe
mode.Examples:
Skipping a single definition: before type signature:
{# TERMINATING #} a : A a = a
Skipping a single definition: before first clause:
b : A {# TERMINATING #} b = b
Skipping an oldstyle mutual block: Before mutual keyword:
{# TERMINATING #} mutual c : A c = d d : A d = c
Skipping an oldstyle mutual block: Somewhere within mutual block before a type signature or first function clause:
mutual {# TERMINATING #} e : A e = f f : A f = e
Skipping a newstyle mutual block: Anywhere before a type signature or first function clause in the block:
g : A h : A g = h {# TERMINATING #} h = g
Universe LevelsÂ¶
Introduction to universesÂ¶
Russellâs paradox implies that the collection of all sets is not itself a set. Namely, if there were such a set U
, then one could form the subset A â U
of all sets that do not contain themselves. Then we would have A â A
if and only if A â A
, a contradiction.
For similar reasons, not every Agda type is a Set
. For example, we have
Bool : Set
Nat : Set
but not Set : Set
. However, it is often convenient for Set
to have a type of its own, and so in Agda, it is given the type Setâ
:
Set : Setâ
In many ways, expressions of type Setâ
behave just like expressions of type Set
; for example, they can be used as types of other things. However, the elements of Setâ
are potentially larger; when A : Setâ
, then A
is sometimes called a large set. In turn, we have
Setâ : Setâ
Setâ : Setâ
and so on. A type whose elements are types is called a universe;
Agda provides an infinite number of universes Set
, Setâ
,
Setâ
, Setâ
, âŠ, each of which is an element of the next
one. In fact, Set
itself is just an abbreviation for
Setâ
. The subscript n
is called the level of the
universe Setâ
.
A note on syntax: you can also write Set1
, Set2
, etc., instead
of Setâ
, Setâ
. To enter a subscript in the Emacs mode, type
â\_1
â.
Universe exampleÂ¶
So why are universes useful? Because sometimes it is necessary to
define, and prove theorems about, functions that operate not just on
sets but on large sets. In fact, most Agda users sooner or later
experience an error message where Agda complains that Setâ !=
Set
. These errors usually mean that a small set was used where a
large one was expected, or vice versa.
For example, suppose you have defined the usual datatypes for lists and cartesian products:
data List (A : Set) : Set where
[] : List A
_::_ : A â List A â List A
data _Ă_ (A B : Set) : Set where
_,_ : A â B â A Ă B
infixr 5 _::_
infixr 4 _,_
infixr 2 _Ă_
Now suppose you would like to define an operator Prod
that inputs
a list of n
sets and takes their cartesian product, like this:
Prod (A :: B :: C :: []) = A Ă B Ă C
There is only one small problem with this definition. The type of Prod
should be
Prod : List Set â Set
However, the definition of List A
specified that A
had to be a
Set
. Therefore, List Set
is not a valid type. The solution is
to define a special version of the List
operator that works for
large sets:
data Listâ (A : Setâ) : Setâ where
[] : Listâ A
_::_ : A â Listâ A â Listâ A
With this, we can indeed define:
Prod : Listâ Set â Set
Prod [] = â€
Prod (A :: As) = A Ă Prod As
Universe polymorphismÂ¶
Although we were able to give a type to the Prod
operator by
defining a special notion of large list, this quickly gets
tiresome. Sooner or later, we find that we require yet another list
type Listâ
, and it doesnât stop there. Also every function on
lists (such as append
) must be redefined, and every theorem about
such functions must be reproved, for every possible level.
The solution to this problem is universe polymorphism. Agda provides a
special primitive type Level
, whose elements are possible levels
of universes. In fact, the notation for the n
th universe,
Setâ
, is just an abbreviation for Set n
, where n :
Level
is a level. We can use this to write a polymorphic List
operator that works at any level. The library Agda.Primitive
must
be imported to access the Level
type. The definition then looks
like this:
open import Agda.Primitive
data List {n : Level} (A : Set n) : Set n where
[] : List A
_::_ : A â List A â List A
This new operator works at all levels; for example, we have
List Nat : Set
List Set : Setâ
List Setâ : Setâ
Level arithmeticÂ¶
Even though we donât have the number of levels specified, we know that
there is a lowest level lzero
, and for each level n
, there
exists some higher level lsuc n
; therefore, the set of levels is
infinite. In addition, we can also take the least upper bound n
â m
of two levels. In summary, the following (and only the
following) operations on levels are provided:
lzero : Level
lsuc : (n : Level) â Level
_â_ : (n m : Level) â Level
This is sufficient for most purposes; for example, we can define the cartesian product of two types of arbitrary (and not necessarily equal) levels like this:
data _Ă_ {n m : Level} (A : Set n) (B : Set m) : Set (n â m) where
_,_ : A â B â A Ă B
With this definition, we have, for example:
Nat Ă Nat : Set
Nat x Set : Setâ
Set Ă Set : Setâ
forall
notationÂ¶
From the fact that we write Set n
, it can always be inferred that
n
is a level. Therefore, when defining universepolymorphic
functions, it is common to use the â (or forall) notation. For
example, the type of the universepolymorphic map
operator on
lists can be written
map : â {n m} {A : Set n} {B : Set m} â (A â B) â List A â List B
which is equivalent to
map : {n m : Level} {A : Set n} {B : Set m} â (A â B) â List A â List B
Expressions of kind SetÏ
Â¶
In a sense, universes were introduced to ensure that every Agda
expression has a type, including expressions such as Set
,
Setâ
, etc. However, the introduction of universe polymorphism
inevitably breaks this property again, by creating some new terms that
have no type. Consider the polymorphic singleton set Unit n :
Setâ
, defined by
data Unit (n : Level) : Set n where
<> : Unit n
It is welltyped, and has type
Unit : (n : Level) â Set n
However, the type (n : Level) â Set n
, which is a valid Agda
expression, does not belong to any universe. Indeed, the expression
denotes a function mapping levels to universes, so if it had a type,
it should be something like Level â Universe
, where Universe
is the collection of all universes. But since the elements of
Universe
are types, Universe
is itself a universe, so we have
Universe : Universe
. This leads to circularity and
inconsistency. In other words, just as we cannot have a set of all
sets, we also canât have a universe of all universes.
As a consequence, although the expression (n : Level) â Set n
is a type, it does not have a type. It does, however, have a
âkindâ, which Agda calls SetÏ
. The expression SetÏ
itself is a
valid Agda type but cannot appear as part of an Agda term. For
example, the following definition is valid:
largeType : SetÏ
largeType = (n : Level) â Set n
As a counterexample which attempts to use SetÏ
as part of a term,
consider trying to form the singleton list [ Unit ]
:
badList : List ((n : Level) â Set n)
badList = Unit :: []
This generates an error message stating that SetÏ
is not of the
form Set _
. The problem is that List
can only be applied to
types that are part of Set n
for some n : Level
, but (n :
Level) â Set n
belongs to SetÏ
which is not of this form. The
only type constructor that can be applied to expressions of kind
SetÏ
is â
.
WithAbstractionÂ¶
Withabstraction was first introduced by Conor McBride [McBride2004] and lets you pattern match on the result of an intermediate computation by effectively adding an extra argument to the lefthand side of your function.
UsageÂ¶
In the simplest case the with
construct can be used just to discriminate on
the result of an intermediate computation. For instance
filter : {A : Set} â (A â Bool) â List A â List A
filter p [] = []
filter p (x â· xs) with p x
filter p (x â· xs)  true = x â· filter p xs
filter p (x â· xs)  false = filter p xs
The clause containing the withabstraction has no righthand side. Instead it
is followed by a number of clauses with an extra argument on the left,
separated from the original arguments by a vertical bar (
).
When the original arguments are the same in the new clauses you can use the
...
syntax:
filter : {A : Set} â (A â Bool) â List A â List A
filter p [] = []
filter p (x â· xs) with p x
...  true = x â· filter p xs
...  false = filter p xs
In this case ...
expands to filter p (x â· xs)
. There are three cases
where you have to spell out the lefthand side:
 If you want to do further pattern matching on the original arguments.
 When the pattern matching on the intermediate result refines some of the other arguments (see Dot patterns).
 To disambiguate the clauses of nested withabstractions (see Nested withabstractions below).
GeneralisationÂ¶
The power of withabstraction comes from the fact that the goal type
and the type of the original arguments are generalised over the value
of the scrutinee. See Technical details below for the details.
This generalisation is important when you have to prove properties
about functions defined using with
. For instance, suppose we want
to prove that the filter
function above satisfies some property
P
. Starting out by pattern matching of the list we get the
following (with the goal types shown in the holes)
postulate P : â {A} â List A â Set
postulate pnil : P []
postulate Q : Set
postulate qnil : Q
proof : {A : Set} (p : A â Bool) (xs : List A) â P (filter p xs)
proof p [] = {! P [] !}
proof p (x â· xs) = {! P (filter p xs  p x) !}
In the cons case we have to prove that P
holds for filter p xs  p x
.
This is the syntax for a stuck withabstractionâfilter
cannot reduce
since we donât know the value of p x
. This syntax is used for printing, but
is not accepted as valid Agda code. Now if we withabstract over p x
, but
donât pattern match on the result we get:
proof : {A : Set} (p : A â Bool) (xs : List A) â P (filter p xs)
proof p [] = pnil
proof p (x â· xs) with p x
...  r = {! P (filter p xs  r) !}
Here the p x
in the goal type has been replaced by the variable r
introduced for the result of p x
. If we pattern match on r
the
withclauses can reduce, giving us:
proof : {A : Set} (p : A â Bool) (xs : List A) â P (filter p xs)
proof p [] = pnil
proof p (x â· xs) with p x
...  true = {! P (x â· filter p xs) !}
...  false = {! P (filter p xs) !}
Both the goal type and the types of the other arguments are generalised, so it
works just as well if we have an argument whose type contains filter p xs
.
proofâ : {A : Set} (p : A â Bool) (xs : List A) â P (filter p xs) â Q
proofâ p [] _ = qnil
proofâ p (x â· xs) H with p x
...  true = {! H : P (filter p xs) !}
...  false = {! H : P (x â· filter p xs) !}
The generalisation is not limited to scrutinees in other withabstractions. All occurrences of the term in the goal type and argument types will be generalised.
Note that this generalisation is not always type correct and may result in a (sometimes cryptic) type error. See Illtyped withabstractions below for more details.
Nested withabstractionsÂ¶
Withabstractions can be nested arbitrarily. The only thing to keep in mind in
this case is that the ...
syntax applies to the closest withabstraction.
For example, suppose you want to use ...
in the definition below.
compare : Nat â Nat â Comparison
compare x y with x < y
compare x y  false with y < x
compare x y  false  false = equal
compare x y  false  true = greater
compare x y  true = less
You might be tempted to replace compare x y
with ...
in all the
withclauses as follows.
compare : Nat â Nat â Comparison
compare x y with x < y
...  false with y < x
...  false = equal
...  true = greater
...  true = less  WRONG
This, however, would be wrong. In the last clause the ...
is interpreted as
belonging to the inner withabstraction (the whitespace is not taken into
account) and thus expands to compare x y  false  true
. In this case you
have to spell out the lefthand side and write
compare : Nat â Nat â Comparison
compare x y with x < y
...  false with y < x
...  false = equal
...  true = greater
compare x y  true = less
Simultaneous abstractionÂ¶
You can abstract over multiple terms in a single withabstraction. To do this
you separate the terms with vertical bars (
).
compare : Nat â Nat â Comparison
compare x y with x < y  y < x
...  true  _ = less
...  _  true = greater
...  false  false = equal
In this example the order of abstracted terms does not matter, but in general it does. Specifically, the types of later terms are generalised over the values of earlier terms. For instance
postulate pluscommute : (a b : Nat) â a + b âĄ b + a
postulate P : Nat â Set
thm : (a b : Nat) â P (a + b) â P (b + a)
thm a b t with a + b  pluscommute a b
thm a b t  ab  eq = {! t : P ab, eq : ab âĄ b + a !}
Note that both the type of t
and the type of the result eq
of
pluscommute a b
have been generalised over a + b
. If the terms in the
withabstraction were flipped around, this would not be the case. If we now
pattern match on eq
we get
thm : (a b : Nat) â P (a + b) â P (b + a)
thm a b t with a + b  pluscommute a b
thm a b t  .(b + a)  refl = {! t : P (b + a) !}
and can thus fill the hole with t
. In effect we used the
commutativity proof to rewrite a + b
to b + a
in the type of
t
. This is such a useful thing to do that there is special syntax
for it. See Rewrite below.
A limitation of generalisation is that only occurrences of the term that are
visible at the time of the abstraction are generalised over, but more instances
of the term may appear once you start filling in the righthand side or do
further matching on the left. For instance, consider the following contrived
example where we need to match on the value of f n
for the type of q
to
reduce, but we then want to apply q
to a lemma that talks about f n
:
postulate
R : Set
P : Nat â Set
f : Nat â Nat
lemma : â n â P (f n) â R
Q : Nat â Set
Q zero = â„
Q (suc n) = P (suc n)
proof : (n : Nat) â Q (f n) â R
proof n q with f n
proof n ()  zero
proof n q  suc fn = {! q : P (suc fn) !}
Once we have generalised over f n
we can no longer apply the lemma, which
needs an argument of type P (f n)
. To solve this problem we can add the
lemma to the withabstraction:
proof : (n : Nat) â Q (f n) â R
proof n q with f n  lemma n
proof n ()  zero  _
proof n q  suc fn  lem = lem q
In this case the type of lemma n
(P (f n) â R
) is generalised over f
n
so in the righthand side of the last clause we have q : P (suc fn)
and
lem : P (suc fn) â R
.
See The inspect idiom below for an alternative approach.
Using underscores and variables in pattern repetitionÂ¶
If an ellipsis âŠ cannot be used, the withclause has to repeat (or refine) the patterns of the parent clause. Since Agda 2.5.3, such patterns can be replaced by underscores _ if the variables they bind are not needed. Here is a (slightly contrived) example:
record R : Set where
coinductive  disallows matching
field f : Bool
n : Nat
data P (r : R) : Nat â Set where
fTrue : R.f r âĄ true â P r zero
nSuc : P r (suc (R.n r))
data Q : (b : Bool) (n : Nat) â Set where
true! : Q true zero
suc! : â{b n} â Q b (suc n)
test : (r : R) {n : Nat} (p : P r n) â Q (R.f r) n
test r nSuc = suc!
test r (fTrue p) with R.f r
test _ (fTrue ())  false
test _ _  true = true!  underscore instead of (isTrue _)
Since Agda 2.5.4, patterns can also be replaced by a variable:
f : List Nat â List Nat
f [] = []
f (x â· xs) with f xs
f xs0  r = ?
The variable xs0 is treated as a letbound variable with value .x â· .xs (where .x : Nat and .xs : List Nat are out of scope). Since withabstraction may change the type of variables, the instantiation of such letbound variables are type checked again after withabstraction.
Irrefutable WithÂ¶
When a pattern is irrefutable, we can use a patternmatching with
instead of a traditional with
block. This gives us a lightweight
syntax to make a lot of observations before using a âproperâ with
block. For a basic example of such an irrefutable pattern, see this
unfolding lemma for pred
pred : Nat â Nat
pred zero = zero
pred (suc n) = n
NotNull : Nat â Set
NotNull zero = â„  false
NotNull (suc n) = â€  trivially true
predcorrect : â n (pr : NotNull n) â suc (pred n) âĄ n
predcorrect n pr with suc p â n = refl
In the above code snippet we do not need to entertain the idea that n
could be equal to zero
: Agda detects that the proof pr
allows us
to dismiss such a case entirely.
The patterns used in such an inversion clause can be arbitrary. We can for instance have deep patterns, e.g. projecting out the second element of a vector whose length is neither 0 nor 1:
infixr 5 _â·_
data Vec {a} (A : Set a) : Nat â Set a where
[] : Vec A zero
_â·_ : â {n} â A â Vec A n â Vec A (suc n)
second : â {n} {pr : NotNull (pred n)} â Vec A n â A
second vs with (_ â· v â· _) â vs = v
Remember example of simultaneous
abstraction from above. A simultaneous
rewrite / patternmatching with
is to be understood as being nested.
That is to say that the type refinements introduced by the first
case analysis may be necessary to type the following ones.
In the following example, in focusAt
we are only able to perform
the splitAt
we are interested in because we have massaged the type
of the vector argument using suc+
first.
suc+ : â m n â suc m + n âĄ m + suc n
suc+ zero n = refl
suc+ (suc m) n rewrite suc+ m n = refl
infixr 1 _Ă_
_Ă_ : â {a b} (A : Set a) (B : Set b) â Set ?
A Ă B = ÎŁ A (Î» _ â B)
splitAt : â m {n} â Vec A (m + n) â Vec A m Ă Vec A n
splitAt zero xs = ([] , xs)
splitAt (suc m) (x â· xs) with (ys , zs) â splitAt m xs = (x â· ys , zs)
 focusAt m (xâ â· âŻ â· xâââ â· xâ â· xâââ â· âŻ â· xâââ)
 returns ((xâ â· âŻ â· xâââ) , xâ , (xâââ â· âŻ â· xâââ))
focusAt : â m {n} â Vec A (suc (m + n)) â Vec A m Ă A Ă Vec A n
focusAt m {n} vs rewrite suc+ m n
with (before , focus â· after) â splitAt m vs
= (before , focus , after)
You can alternate arbitrarily many rewrite
and patternmatching
with
clauses and still perform a with
abstraction afterwards
if necessary.
RewriteÂ¶
Remember example of simultaneous abstraction from above.
postulate pluscommute : (a b : Nat) â a + b âĄ b + a
thm : (a b : Nat) â P (a + b) â P (b + a)
thm a b t with a + b  pluscommute a b
thm a b t  .(b + a)  refl = t
This pattern of rewriting by an equation by withabstracting over it and its lefthand side is common enough that there is special syntax for it:
thm : (a b : Nat) â P (a + b) â P (b + a)
thm a b t rewrite pluscommute a b = t
The rewrite
construction takes a term eq
of type lhs âĄ rhs
, where _âĄ_
is the builtin equality type, and expands to a
withabstraction of lhs
and eq
followed by a match of the result of
eq
against refl
:
f ps rewrite eq = v
>
f ps with lhs  eq
...  .rhs  refl = v
One limitation of the rewrite
construction is that you cannot do further
pattern matching on the arguments after the rewrite, since everything happens
in a single clause. You can however do withabstractions after the rewrite. For
instance,
postulate T : Nat â Set
isEven : Nat â Bool
isEven zero = true
isEven (suc zero) = false
isEven (suc (suc n)) = isEven n
thmâ : (a b : Nat) â T (a + b) â T (b + a)
thmâ a b t rewrite pluscommute a b with isEven a
thmâ a b t  true = t
thmâ a b t  false = t
Note that the withabstracted arguments introduced by the rewrite (lhs
and
eq
) are not visible in the code.
The inspect idiomÂ¶
When you withabstract a term t
you lose the connection between
t
and the new argument representing its value. Thatâs fine as long
as all instances of t
that you care about get generalised by the
abstraction, but as we saw above this is not
always the case. In that example we used simultaneous abstraction to
make sure that we did capture all the instances we needed. An
alternative to that is to use the inspect idiom, which retains a
proof that the original term is equal to its abstraction.
In the simplest form, the inspect idiom uses a singleton type:
data Singleton {a} {A : Set a} (x : A) : Set a where
_withâĄ_ : (y : A) â x âĄ y â Singleton x
inspect : â {a} {A : Set a} (x : A) â Singleton x
inspect x = x withâĄ refl
Now instead of withabstracting t
, you can abstract over inspect t
. For
instance,
filter : {A : Set} â (A â Bool) â List A â List A
filter p [] = []
filter p (x â· xs) with inspect (p x)
...  true withâĄ eq = {! eq : p x âĄ true !}
...  false withâĄ eq = {! eq : p x âĄ false !}
Here we get proofs that p x âĄ true
and p x âĄ false
in the respective
branches that we can use on the right. Note that since the withabstraction is
over inspect (p x)
rather than p x
, the goal and argument types are no
longer generalised over p x
. To fix that we can replace the singleton type
by a function graph type as follows (see Anonymous modules to learn
about the use of a module to bind the type arguments to Graph
and
inspect
):
module _ {a b} {A : Set a} {B : A â Set b} where
data Graph (f : â x â B x) (x : A) (y : B x) : Set b where
ingraph : f x âĄ y â Graph f x y
inspect : (f : â x â B x) (x : A) â Graph f x (f x)
inspect _ _ = ingraph refl
To use this on a term g v
you withabstract over both g v
and inspect
g v
. For instance, applying this to the example from above we get
postulate
R : Set
P : Nat â Set
f : Nat â Nat
lemma : â n â P (f n) â R
Q : Nat â Set
Q zero = â„
Q (suc n) = P (suc n)
proof : (n : Nat) â Q (f n) â R
proof n q with f n  inspect f n
proof n ()  zero  _
proof n q  suc fn  ingraph eq = {! q : P (suc fn), eq : f n âĄ suc fn !}
We could then use the proof that f n âĄ suc fn
to apply lemma
to q
.
This version of the inspect idiom is defined (using slightly different names)
in the standard library in the module
Relation.Binary.PropositionalEquality
and in the agdaprelude in
Prelude.Equality.Inspect
(reexported by Prelude
).
Alternatives to withabstractionÂ¶
Although withabstraction is very powerful there are cases where you cannot or donât want to use it. For instance, you cannot use withabstraction if you are inside an expression in a righthand side. In that case there are a couple of alternatives.
Pattern lambdasÂ¶
Agda does not have a primitive case
construct, but one can be emulated
using pattern matching lambdas. First you define a
function case_of_
as follows:
case_of_ : â {a b} {A : Set a} {B : Set b} â A â (A â B) â B
case x of f = f x
You can then use this function with a pattern matching lambda as the second argument to get a Haskellstyle case expression:
filter : {A : Set} â (A â Bool) â List A â List A
filter p [] = []
filter p (x â· xs) =
case p x of
Î» { true â x â· filter p xs
; false â filter p xs
}
This version of case_of_
only works for nondependent functions. For
dependent functions the target type will in most cases not be inferrable, but
you can use a variant with an explicit B
for this case:
case_return_of_ : â {a b} {A : Set a} (x : A) (B : A â Set b) â (â x â B x) â B x
case x return B of f = f x
The dependent version will let you generalise over the scrutinee, just like a withabstraction, but you have to do it manually. Two things that it will not let you do is
 further pattern matching on arguments on the lefthand side, and
 refine arguments on the left by the patterns in the case expression. For
instance if you matched on a
Vec A n
then
would be refined by the nil and cons patterns.
Helper functionsÂ¶
Internally withabstractions are translated to auxiliary functions
(see Technical details below) and you can
always[1] write these functions manually. The downside
is that the type signature for the helper function needs to be written
out explicitly, but fortunately the Emacs Mode has a command
(Cc Ch
) to generate it using the same algorithm that generates
the type of a withfunction.
Performance considerationsÂ¶
The generalisation step of a withabstraction needs to normalise the scrutinee and the goal and argument types to make sure that all instances of the scrutinee are generalised. The generalisation also needs to be type checked to make sure that itâs not illtyped. This makes it expensive to type check a withabstraction if
 the normalisation is expensive,
 the normalised form of the goal and argument types are big, making finding the instances of the scrutinee expensive,
 type checking the generalisation is expensive, because the types are big, or because checking them involves heavy computation.
In these cases it is worth looking at the alternatives to withabstraction from above.
Technical detailsÂ¶
Internally withabstractions are translated to auxiliary functionsâthere are no withabstractions in the Core language. This translation proceeds as follows. Given a withabstraction
where (i.e. types the variables bound in ), we
Infer the types of the scrutinees .
Partition the context into and such that is the smallest context where for all , i.e., where the scrutinees are welltyped. Note that the partitioning is not required to be a split, can be a (wellformed) reordering of .
Generalise over the s, by computing
such that the normal form of does not contain any and
where is equality of the normal forms of and . The type of the auxiliary function is then .
Check that is type correct, which is not guaranteed (see below).
Add a function , mutually recursive with , with the definition
where , and and are the patterns from corresponding to the variables of . Note that due to the possible reordering of the partitioning of into and , the patterns and can be in a different order from how they appear .
Replace the withabstraction by a call to resulting in the final definition
where and and are the variables from corresponding to and respectively.
ExamplesÂ¶
Below are some examples of withabstractions and their translations.
postulate
A : Set
_+_ : A â A â A
T : A â Set
mkT : â x â T x
P : â x â T x â Set
 the type A of the with argument has no free variables, so the with
 argument will come first
fâ : (x y : A) (t : T (x + y)) â T (x + y)
fâ x y t with x + y
fâ x y t  w = {!!}
 Generated with function
fauxâ : (w : A) (x y : A) (t : T w) â T w
fauxâ w x y t = {!!}
 x and p are not needed to type the with argument, so the context
 is reordered with only y before the with argument
fâ : (x y : A) (p : P y (mkT y)) â P y (mkT y)
fâ x y p with mkT y
fâ x y p  w = {!!}
fauxâ : (y : A) (w : T y) (x : A) (p : P y w) â P y w
fauxâ y w x p = {!!}
postulate
H : â x y â T (x + y) â Set
 Multiple with arguments are always inserted together, so in this case
 t ends up on the left since itâs needed to type h and thus x + y isnât
 abstracted from the type of t
fâ : (x y : A) (t : T (x + y)) (h : H x y t) â T (x + y)
fâ x y t h with x + y  h
fâ x y t h  wâ  wâ = {! t : T (x + y), goal : T wâ !}
fauxâ : (x y : A) (t : T (x + y)) (h : H x y t) (wâ : A) (wâ : H x y t) â T wâ
fauxâ x y t h wâ wâ = {!!}
 But earlier with arguments are abstracted from the types of later ones
fâ : (x y : A) (t : T (x + y)) â T (x + y)
fâ x y t with x + y  t
fâ x y t  wâ  wâ = {! t : T (x + y), wâ : T wâ, goal : T wâ !}
fauxâ : (x y : A) (t : T (x + y)) (wâ : A) (wâ : T wâ) â T wâ
fauxâ x y t wâ wâ = {!!}
Illtyped withabstractionsÂ¶
As mentioned above, generalisation does not always produce welltyped results. This happens when you abstract over a term that appears in the type of a subterm of the goal or argument types. The simplest example is abstracting over the first component of a dependent pair. For instance,
postulate
A : Set
B : A â Set
H : (x : A) â B x â Set
badwith : (p : ÎŁ A B) â H (fst p) (snd p)
badwith p with fst p
...  _ = {!!}
Here, generalising over fst p
results in an illtyped application H w
(snd p)
and you get the following type error:
fst p != w of type A
when checking that the type (p : ÎŁ A B) (w : A) â H w (snd p) of
the generated with function is wellformed
This message can be a little difficult to interpret since it only prints the
immediate problem (fst p != w
) and the full type of the withfunction. To
get a more informative error, pointing to the location in the type where the
error is, you can copy and paste the withfunction type from the error message
and try to type check it separately.
[1]  The termination checker has special treatment for withfunctions, so replacing a with by the equivalent helper function might fail termination. 
[McBride2004]  C. McBride and J. McKinna. The view from the left. Journal of Functional Programming, 2004. http://strictlypositive.org/vfl.pdf. 
Without KÂ¶
The option withoutK
adds some restrictions to Agdaâs
typechecking algorithm in order to ensure compatability with versions
of type theory that do not support UIP (uniqueness of identity
proofs), such as HoTT (homotopy type theory).
The option withK
can be used to override a global
withoutK
in a file, by adding a pragma
{# OPTIONS withK #}
. This option is enabled by default.
Restrictions on pattern matchingÂ¶
When the option withoutK
is enabled, then Agda only accepts
certain case splits. More specifically, the unification algorithm for
checking case splits cannot make use of the deletion rule to solve
equations of the form x = x
.
For example, the obvious implementation of the K rule is not accepted:
K : {A : Set} {x : A} (P : x âĄ x â Set) â
P refl â (xâĄx : x âĄ x) â P xâĄx
K P p refl = p
Pattern matching with the constructor refl
on the argument xâĄx
causes x
to be unified with x
, which fails because the deletion
rule cannot be used when withoutK
is enabled.
On the other hand, the obvious implementation of the J rule is accepted:
J : {A : Set} (P : (x y : A) â x âĄ y â Set) â
((x : A) â P x x refl) â (x y : A) (xâĄy : x âĄ y) â P x y xâĄy
J P p x .x refl = p x
Pattern matching with the constructor refl
on the argument xâĄy
causes x
to be unified with y
. The same applies to Christine
PaulinMohringâs version of the J rule:
JâČ : {A : Set} {x : A} (P : (y : A) â x âĄ y â Set) â
P x refl â (y : A) (xâĄy : x âĄ y) â P y xâĄy
JâČ P p ._ refl = p
For more details, see Jesper Cockxâs PhD thesis Dependent Pattern Matching and ProofRelevant Unification [Cockx (2017)].
Restrictions on termination checkingÂ¶
When withoutK
is enabled, Agdaâs termination checker restricts
structural descent to arguments ending in data types or Size
.
Likewise, guardedness is only tracked when result type is data or
record type:
data â„ : Set where
mutual
data WOne : Set where wrap : FOne â WOne
FOne = â„ â WOne
postulate iso : WOne âĄ FOne
noo : (X : Set) â (WOne âĄ X) â X â â„
noo .WOne refl (wrap f) = noo FOne iso f
noo
is rejected since at type X
the structural descent
f < wrap f
is discounted withoutK
:
data Pandora : Set where
C : â â„ â Pandora
postulate foo : â„ âĄ Pandora
loop : (A : Set) â A âĄ Pandora â A
loop .Pandora refl = C (âŻ (loop â„ foo))
loop
is rejected since guardedness is not tracked at type A
withoutK
.
Restrictions on universe levelsÂ¶
When withoutK
is enabled, some indexed datatypes must be
defined in a higher universe level. In particular, the types of all
indices should fit in the sort of the datatype.
For example, usually (i.e. withK
) Agda allows the following
definition of equality:
data _âĄâ_ {â} {A : Set â} (x : A) : A â Set where
refl : x âĄâ x
However, with withoutK
it must be defined at a higher
universe level:
data _âĄâČ_ {â} {A : Set â} : A â A â Set â where
refl : {x : A} â x âĄâČ x
ToolsÂ¶
Automatic Proof Search (Auto)Â¶
Agda supports (since version 2.2.6) the command Auto
, that searches
for type inhabitants and fills a hole when one is found. The type
inhabitant found is not necessarily unique.
Auto can be used as an aid when interactively constructing terms in Agda. In a system with dependent types it can be meaningful to use such a tool for finding fragments of, not only proofs, but also programs. For instance, giving the type signature of the map function over vectors, you will get the desired function as the first solution.
The tool is based on a term search implementation independent of Agda called Agsy. Agsy is a general purpose search algorithm for a dependently typed language similar to Agda. One should not expect it to handle large problems of any particular kind, but small enough problems of almost any kind.
Any solution coming from Auto is checked by Agda. Also, the main search algorithm has a timeout mechanism. Therefore, there is little harm in trying Auto and it might save you key presses.
UsageÂ¶
The tool is invoked by placing the cursor on a hole and choosing
Auto
in the goal menu or pressing Cc Ca
. Agsyâs behaviour
can be changed by using various options which are passed directly
in the hole.
Option  Meaning 

t N 
Set timeout to N seconds 
c 
Allow Agsy to use casesplit 
d 
Attempt to disprove the goal 
ID 
Use definition ID as a hint 
m 
Use the definitions in the current module as hints 
r 
Use the unqualified definitions in scope as hints 
l 
List up to ten solutions, does not commit to any 
s N 
Commit to the N th solution 
Giving no arguments is fine and results in a search with default parameters. The search carries on until either a (not necessarily unique) solution is found, the search space is fully (and unsuccessfully) explored or it times out (one second by default). Here follows a list of the different modes and parameters.
Case splitÂ¶
Auto normally only tries to find a term that could replace the current
hole. However, if the hole constitutes the entire RHS of the clause
(same as for the makecase command), you can instruct Auto to try case
splitting by writing (since version 2.2.8) c
.
Note that if a solution is found the whole file will be reloaded (as
for makecase) resulting in a delay. Case splitting cannot yet be
combined with l
or s <n>
.
Equality reasoningÂ¶
If the constants _âĄ_ begin_ _âĄâš_â©_ _â sym cong
from the standard
library are in scope, then Auto will do equality reasoning using these
constructs. However, it will not do anything more clever than things
like not nesting several sym
or cong
. Hence long chains of
equalities should not be expected and required arithmetical rules have
to be given as hints.
HintsÂ¶
Auto does not by default try using constants in scope. If there is a
lemma around that might help in constructing the term you can include
it in the search by giving hints. There are two ways of doing
this. One way is to provide the exact list of constants to
include. Such a list is given by writing a number of constant names
separated by space: <hint1> <hint2> ...
.
The other way is to write m
. This includes all constants in scope
which are defined or postulated in the innermost module surrounding
the current hole. It is also possible to combine m
with a list of
named constants (not included by m
).
There are a few exceptions to what you have to specify as hints:
Datatypes and constants that can be deduced by unifying the two sides of an equality constraint can be omitted.
E.g., if the constraint
? = List A
occurs during the search, then refining?
toList ...
will happen without having to provideList
as a hint. The constants that you can leave out overlap more or less with the ones appearing in hidden arguments, i.e. you wouldnât have written them when giving the term by hand either.Constructors and projection functions are automatically tried, so should never be given as hints.
Recursive calls, although currently only the function itself, not all functions in the same mutual block.
TimeoutÂ¶
The timeout is one second by default but can be changed by adding
t <n>
to the parameters, where <n>
is the number of seconds.
Listing and choosing among several solutionsÂ¶
Normally, Auto replaces the hole with the first solution found. If you
are not happy with that particular solution, you can list the ten (at
most) first solutions encountered by including the flag l
.
You can then pick a particular solution by writing s <n>
where
<n>
is the number of solutions to skip (as well as the number
appearing before the solution in the list). The options l
and
s <n>
can be combined to list solutions other than the ten first
ones.
DisprovingÂ¶
If you are uncertain about the validity of what you are trying to
prove, you can use Auto to try to find a counterproof. The flag d
makes Auto negate the current goal and search for a term disproving
it. If such a term is found, it will be displayed in the info
buffer. The flag d
can be combined with l
and l s <n>
.
Auto refine / suggestÂ¶
There is a special mode for searching (part of) the scope of constants
for possible refinement candidates. The flag r
chooses this
mode. By default all constants which are in scope unqualified are
included.
The constants that matches the current goal are sorted in order of how
many constructs their result type contains. This means that the
constants which in some sense match the goal most specifically will
appear first and the most general ones last. By default, Auto will
present a list of candidates, rather than refining using the topmost
constant. To select one of them for refinement, combine r
with
s <n>
. In order to list constants other than the ten first ones,
write r l s <n>
.
The auto refine feature has little to do with the rest of the Auto tool. If it turns out to be useful it could be improved and made into a separate Emacs mode command.
Dependencies between meta variablesÂ¶
If the goal type or type of local variables contain meta variables, then the constraints for these are also included in the search. If a solution is found it means that Auto has also found solutions for the occurring meta variables. Those solutions will be inserted into your file along with that of the hole from where you called Auto. Also, any unsolved equality constraints that contain any of the involved meta variables are respected in the search.
LimitationsÂ¶
 Irrelevance is not yet respected. Agsy will happily use a parameter marked as irrelevant and does not disregard irrelevant arguments when comparing terms.
 Records that lack a constructor name are still deconstructed when case splitting, but the name of the record type is used instead of a constructor name in the resulting pattern.
 Literals representing natural numbers are supported (but any generated natural number will be given in constructor form). Apart from this, literals are not supported.
 Primitive functions are not supported.
 Copatterns are not supported.
 Termination checking for recursive calls is done locally, so a nonterminating set of clauses might be sent back to Agda.
 Agsy currently does not automatically pick a datatype when
instantiating types. A frequently occurring situation is when you
try to disprove a generic property. Then Agsy must come up with a
particular type as part of the disproof. You can either fix your
generic type to e.g.
Nat
orFin n
(for an arbitraryn
if you wish), or you giveNat
orFin
as a hint to the search.  Case split mode currently does not do case on expressions
(
with
).  Case split mode sometimes gives a unnecessarily complex RHS for some clause when the solution consists of several clauses.
 The special constraints that apply to
codata
are not respected by Agsy. Agsy treatscodata
just likedata
.  Agsy has universe subtyping, so sometimes suggests solutions not accepted by Agda.
 Universe polymorphism is only partially supported. Agsy may fail when trying to construct universe polymorphic definitions, but will probably succeed (with respect to this) when constructing terms which refer to, or whose type is defined in terms of, universe polymorphic definitions.
 In case split and disproving modes, the current goal may not depend on any other meta variables. For disproving mode this means that there may be implicitly universally quantified but not existentially quantified stuff.
 Searching for simultaneous solutions of several holes does not combine well with parameterised modules and recursive calls.
User feedbackÂ¶
When sending bug reports, please use Agdaâs bug tracker. Apart from that, receiving nice examples (via the bug tracker) would be much appreciated. Both such examples which Auto does not solve, but you have a feeling itâs not larger than for that to be possible. And examples that Auto only solves by increasing timeout. The examples sent in will be used for tuning the heuristics and hopefully improving the performance.
Commandline optionsÂ¶
Commandline optionsÂ¶
Agda accepts the following options.
General optionsÂ¶
version V
 Show version number
help[=TOPIC] ?[TOPIC]
 Show basically this help, or more help about
TOPIC
. Current topics available:warning
. interactive I
 Start in interactive mode (no longer supported)
interaction
 For use with the Emacs mode (no need to invoke yourself)
interactionjson
 For use with other editors such as Atom (no need to invoke yourself)
onlyscopechecking
 Only scopecheck the toplevel module, do not typecheck it
CompilationÂ¶
See Compilers for backendspecific options.
nomain
 Do not treat the requested module as the main module of a program when compiling
compiledir=DIR
 Set
DIR
as directory for compiler output (default: the project root) noforcing
 Disable the forcing optimisation
withcompiler=PATH
 Set
PATH
as the executable to call to compile the backendâs output (default: ghc for the GHC backend).
Generating highlighted source codeÂ¶
vim
 Generate Vim highlighting files
latex
 Generate LaTeX with highlighted source code (see Generating LaTeX)
latexdir=DIR
 Set directory in which LaTeX files are
placed to
DIR
(default: latex) countclusters
 Count extended grapheme clusters when generating LaTeX code (see Counting Extended Grapheme Clusters)
html
 Generate HTML files with highlighted source code (see Generating HTML)
htmldir=DIR
 Set directory in which HTML files are placed
to
DIR
(default: html) css=URL
 Set URL of the CSS file used by the HTML files to
URL
(can be relative) htmlhighlight=[code,all,auto]
 Whether to highlight nonAgda code as comments in generated HTML files (default: all; see :ref: generatinghtml)
dependencygraph=FILE
 Generate a Dot file
FILE
with a module dependency graph
Imports and librariesÂ¶
(see Library Management)
ignoreinterfaces
 Ignore interface files (retype check everything, except for builtin and primitive modules)
ignoreallinterfaces
 Ignore all interface files, including builtin and primitive modules; only use this if you know what you are doing!
localinterfaces
 Read and write interface files next to the Agda files they
correspond to (i.e. do not attempt to regroup them in a
_build/
directory at the projectâs root). includepath=DIR i=DIR
 Look for imports in
DIR
library=DIR l=LIB
 Use library
LIB
libraryfile=FILE
 Use
FILE
instead of the standard libraries file nolibraries
 Donât use any library files
nodefaultlibraries
 Donât use default library files
Commandline and pragma optionsÂ¶
The following options can also be given in .agda files in the
{# OPTIONS {optâ} {optâ} ... #}
form at the top of the file.
CachingÂ¶
caching
 Enable caching of typechecking (default)
nocaching
 Disable caching of typechecking
Printing and debuggingÂ¶
showimplicit
 Show implicit arguments when printing
showirrelevant
 Show irrelevant arguments when printing
nounicode
 Donât use unicode characters to print terms
verbose=N v=N
 Set verbosity level to
N
Copatterns and projectionsÂ¶
copatterns
 Enable definitions by copattern matching (default; see Copatterns)
nocopatterns
 Disable definitions by copattern matching
postfixprojections
 Make postfix projection notation the default
Experimental featuresÂ¶
injectivetypeconstructors
 Enable injective type constructors (makes Agda anticlassical and possibly inconsistent)
experimentalirrelevance
 Enable potentially unsound irrelevance features (irrelevant levels, irrelevant data matching) (see Irrelevance)
rewriting
 Enable declaration and use of REWRITE rules (see Rewriting)
cubical
 Enable cubical features. Turns on
withoutK
(see Cubical)
Errors and warningsÂ¶
allowunsolvedmetas
 Succeed and create interface file regardless of unsolved meta variables (see Metavariables)
allowincompletematches
 Succeed and create interface file regardless of incomplete patternmatching definitions
nopositivitycheck
 Do not warn about not strictly positive data types (see Positivity Checking)
noterminationcheck
 Do not warn about possibly nonterminating code (see Termination Checking)
warning=GROUPFLAG W GROUPFLAG
 Set warning group or flag (see Warnings)
Pattern matching and equalityÂ¶
withoutK
 Disables definitions using Streicherâs K axiom (see Without K)
withK
 Overrides a global
withoutK
in a file (see Without K) nopatternmatching
 Disable pattern matching completely
exactsplit
 Require all clauses in a definition to hold as
definitional equalities unless marked
CATCHALL
(see Case trees) noexactsplit
 Do not require all clauses in a definition to hold as definitional equalities (default)
noetaequality
 Default records to noetaequality (see Etaexpansion)
Search depth and instancesÂ¶
terminationdepth=N
 Allow termination checker to count
decrease/increase upto
N
(default: 1; see Termination Checking) instancesearchdepth=N
 Set instance search depth to
N
(default: 500; see Instance Arguments) inversionmaxdepth=N
 Set maximum depth for pattern match inversion to
N
(default: 50). Should only be needed in pathological cases. nooverlappinginstances
 Donât consider recursive instance arguments during pruning of instance candidates (default)
overlappinginstances
 Consider recursive instance arguments during pruning of instance candidates
Other featuresÂ¶
safe
 Disable postulates, unsafe
OPTION
pragmas andprimTrustMe
. Turns offsizedtypes
andguardedness
(at most one can be turned back on again) (see Safe Agda) typeintype
 Ignore universe levels (this makes Agda inconsistent; see Universe Levels)
omegainomega
 Enable typing rule SetÏ : SetÏ (this makes Agda inconsistent).
sizedtypes
 Enable sized types (default, inconsistent with constructorbased
guarded corecursion; see Sized Types). Turned off by
safe
(but can be turned on again, as long as not alsoguardedness
is on). nosizedtypes
 Disable sized types (see Sized Types)
guardedness
 Enable constructorbased guarded corecursion (default, inconsistent
with sized types; see Coinduction). Turned off by
safe
(but can be turned on again, as long as not alsosizedtypes
is on). noguardedness
 Disable constructorbased guarded corecursion (see Coinduction)
universepolymorphism
 Enable universe polymorphism (default; see Universe Levels)
nouniversepolymorphism
 Disable universe polymorphism (see Universe Levels)
noirrelevantprojections
 Disable projection of irrelevant record fields (see Irrelevance)
noautoinline
 Disable automatic compiletime inlining. Only definitions marked INLINE will be inlined.
noprintpatternsynonyms
 Always expand Pattern Synonyms during printing. With this option enabled you can use pattern synonyms freely, but Agda will not use any pattern synonyms when printing goal types or error messages, or when generating patterns for case splits.
doublecheck
 Enable doublechecking of all terms using the internal typechecker
nosyntacticequality
 Disable the syntactic equality shortcut in the conversion checker
nofastreduce
 Disable reduction using the Agda Abstract Machine
WarningsÂ¶
The W
or warning
option can be used to disable or enable
different warnings. The flag W error
(or warning=error
) can
be used to turn all warnings into errors, while W noerror
turns
this off again.
A group of warnings can be enabled by W {group}
, where
group
is one of the following:
all
 All of the existing warnings
warn
 Default warning level
ignore
 Ignore all warnings
Individual warnings can be turned on and off by W {Name}
and W
{noName}
respectively. The flags available are:
AbsurdPatternRequiresNoRHS
 RHS given despite an absurd pattern in the LHS.
CantGeneralizeOverSorts
 Attempt to generalize over sort metas in âvariableâ declaration.
CoverageIssue
 Failed coverage checks.
CoverageNoExactSplit
 Failed exact split checks.
DeprecationWarning
 Feature deprecation.
EmptyAbstract
 Empty
abstract
blocks. EmptyInstance
 Empty
instance
blocks. EmptyMacro
 Empty
macro
blocks. EmptyMutual
 Empty
mutual
blocks. EmptyPostulate
 Empty
postulate
blocks. EmptyPrimitive
 Empty
primitive
blocks. EmptyPrivate
 Empty
private
blocks. EmptyRewritePragma
 Empty
REWRITE
pragmas. IllformedAsClause
 Illformed
as
clauses inimport
statements. InstanceNoOutputTypeName
 Instance arguments whose type does not end in a named or variable type are never considered by instance search.
InstanceArgWithExplicitArg
 Instance arguments with explicit arguments are never considered by instance search.
InstanceWithExplicitArg
 Instance declarations with explicit arguments are never considered by instance search.
InvalidCatchallPragma
CATCHALL
pragmas before a nonfunction clause.InvalidNoPositivityCheckPragma
 No positivity checking pragmas before nondata`,
record
ormutual
blocks. InvalidTerminationCheckPragma
 Termination checking pragmas before nonfunction or
mutual
blocks. InversionDepthReached
 Inversions of patternmatching failed due to exhausted inversion depth.
LibUnknownField
 Unknown field in library file.
MissingDefinitions
 Names declared without an accompanying definition.
ModuleDoesntExport
 Names mentioned in an import statement which are not exported by the module in question.
NotAllowedInMutual
 Declarations not allowed in a mutual block.
NotStrictlyPositive
 Failed strict positivity checks.
OldBuiltin
 Deprecated
BUILTIN
pragmas. OverlappingTokensWarning
 Multiline comments spanning one or more literate text blocks.
PolarityPragmasButNotPostulates
 Polarity pragmas for nonpostulates.
PragmaCompiled
COMPILE
pragmas not allowed in safe mode.PragmaCompileErased
COMPILE
pragma targeting an erased symbol.PragmaNoTerminationCheck
NO_TERMINATION_CHECK
pragmas are deprecated.RewriteMaybeNonConfluent
 Failed confluence checks while computing overlap.
RewriteNonConfluent
 Failed confluence checks while joining critical pairs.
SafeFlagNonTerminating
NON_TERMINATING
pragmas with the safe flag.SafeFlagNoPositivityCheck
NO_POSITIVITY_CHECK
pragmas with the safe flag.SafeFlagNoUniverseCheck
NO_UNIVERSE_CHECK
pragmas with the safe flag.SafeFlagPolarity
POLARITY
pragmas with the safe flag.SafeFlagPostulate
postulate
blocks with the safe flagSafeFlagPragma
 Unsafe
OPTIONS
pragmas with the safe flag. SafeFlagTerminating
TERMINATING
pragmas with the safe flag.SafeFlagWithoutKFlagPrimEraseEquality
primEraseEquality
used with the safe and withoutK flags.ShadowingInTelescope
 Repeated variable name in telescope.
TerminationIssue
 Failed termination checks.
UnknownFixityInMixfixDecl
 Mixfix names without an associated fixity declaration.
UnknownNamesInFixityDecl
 Names not declared in the same scope as their syntax or fixity declaration.
UnknownNamesInPolarityPragmas
 Names not declared in the same scope as their polarity pragmas.
UnreachableClauses
 Unreachable function clauses.
UnsolvedConstraints
 Unsolved constraints.
UnsolvedInteractionMetas
 Unsolved interaction meta variables.
UnsolvedMetaVariables
 Unsolved meta variables.
UselessAbstract
abstract
blocks where they have no effect.UselessInline
INLINE
pragmas where they have no effect.UselessInstance
instance
blocks where they have no effect.UselessPrivate
private
blocks where they have no effect.UselessPublic
public
blocks where they have no effect.WithoutKFlagPrimEraseEquality
primEraseEquality
used with the withoutK flags.WrongInstanceDeclaration
 Terms marked as eligible for instance search should end with a name.
CoInfectiveImport
 Importing a file not using e.g.
safe
from one which does. InfectiveImport
 Importing a file using e.g.
cubical
into one which doesnât.
For example, the following command runs Agda with all warnings
enabled, except for warnings about empty abstract
blocks:
agda W all warning=noEmptyAbstract file.agda
Consistency checking of options usedÂ¶
Agda checks that options used in imported modules are consistent with each other.
An infective option is an option that if used in one module, must be used in all modules that depend on this module. The following options are infective:
cubical
prop
A coinfective option is an option that if used in one module, must be used in all modules that this module depends on. The following options are coinfective:
safe
withoutK
nouniversepolymorphism
nosizedtypes
noguardedness
Agda records the options used when generating an interface file. If any of the following options differ when trying to load the interface again, the source file is retypechecked instead:
terminationdepth
nounicode
allowunsolvedmetas
allowincompletematches
nopositivitycheck
noterminationcheck
typeintype
omegainomega
nosizedtypes
noguardedness
injectivetypeconstructors
prop
nouniversepolymorphism
irrelevantprojections
experimentalirrelevance
withoutK
exactsplit
noetaequality
rewriting
cubical
overlappinginstances
safe
doublecheck
nosyntacticequality
noautoinline
nofastreduce
instancesearchdepth
inversionmaxdepth
warning
CompilersÂ¶
See also Foreign Function Interface.
BackendsÂ¶
GHC BackendÂ¶
The GHC backend translates Agda programs into GHC Haskell programs.
UsageÂ¶
The backend can be invoked from the command line using the flag
compile
:
agda compile [compiledir=<DIR>] [ghcflag=<FLAG>] <FILE>.agda
PragmasÂ¶
ExampleÂ¶
The following âHello, World!â example requires some Builtins and uses the Foreign Function Interface:
module HelloWorld where
open import Agda.Builtin.IO
open import Agda.Builtin.Unit
open import Agda.Builtin.String
postulate
putStrLn : String â IO â€
{# FOREIGN GHC import qualified Data.Text.IO as Text #}
{# COMPILE GHC putStrLn = Text.putStrLn #}
main : IO â€
main = putStrLn "Hello, World!"
After compiling the example
agda compile HelloWorld.agda
you can run the HelloWorld program which prints Hello, World!
.
JavaScript BackendÂ¶
The JavaScript backend translates Agda code to JavaScript code.
UsageÂ¶
The backend can be invoked from the command line using the flag
js
:
agda js [compiledir=<DIR>] <FILE>.agda
OptimizationsÂ¶
Builtin natural numbersÂ¶
Builtin natural numbers are represented as arbitraryprecision integers. The builtin functions on natural numbers are compiled to the corresponding arbitraryprecision integer functions.
Note that pattern matching on an Integer is slower than on an unary natural number. Code that does a lot of unary manipulations and doesnât use builtin arithmetic likely becomes slower due to this optimization. If you find that this is the case, it is recommended to use a different, but isomorphic type to the builtin natural numbers.
Erasable typesÂ¶
A data type is considered erasable if it has a single constructor whose arguments are all erasable types, or functions into erasable types. The compilers will erase
 calls to functions into erasable types
 pattern matches on values of erasable type
At the moment the compilers only have enough type information to erase calls of toplevel functions that can be seen to return a value of erasable type without looking at the arguments of the call. In other words, a function call will not be erased if it calls a lambda bound variable, or the result is erasable for the given arguments, but not for others.
Typical examples of erasable types are the equality type and the accessibility predicate used for wellfounded recursion:
data _âĄ_ {a} {A : Set a} (x : A) : A â Set a where
refl : x âĄ x
data Acc {a} {A : Set a} (_<_ : A â A â Set a) (x : A) : Set a where
acc : (â y â y < x â Acc _<_ y) â Acc _<_ x
The erasure means that equality proofs will (mostly) be erased, and never looked at, and functions defined by wellfounded recursion will ignore the accessibility proof.
Emacs ModeÂ¶
ConfigurationÂ¶
If you want to you can customise the Emacs mode. Just start Emacs and type the following:
Mx loadlibrary RET agda2mode RET
Mx customizegroup RET agda2 RET
If you want some specific settings for the Emacs mode you can add them
to agda2modehook
. For instance, if you do not want to use the
Agda input method (for writing various symbols like ââ„ââÏâŠâ§) you can
add the following to your .emacs:
(addhook 'agda2modehook
'(lambda ()
; If you do not want to use any input method:
(deactivateinputmethod)
; (In some versions of Emacs you should use
; inactivateinputmethod instead of
; deactivateinputmethod.)
Note that, on some systems, the Emacs mode changes the default font of
the current frame in order to enable many Unicode symbols to be
displayed. This only works if the right fonts are available, though.
If you want to turn off this feature, then you should customise the
agda2fontsetname
variable.
KeybindingsÂ¶
Notation for key combinationsÂ¶
The following notation is used when describing key combinations:
 Cc
 means hitting the
c
key while pressing theCtrl
key.  Mx
 means hitting the
x
key while pressing theMeta
key, which is calledAlt
on many systems. Alternatively one can typeEscape
followed byx
(in separate key strokes).  RET
 is the
Enter
,Return
orâ
key.  SPC
 is the space bar.
Commands working with types can be prefixed with Cu
to compute
type without further normalisation and with Cu Cu
to compute
normalised types.
Global commandsÂ¶
 Cc Cl
 Load file
 Cc Cx Cc
 Compile file
 Cc Cx Cq
 Quit, kill the Agda process
 Cc Cx Cr
 Kill and restart the Agda process
 Cc Cx Ca
 Abort a command
 Cc Cx Cd
 Remove goals and highlighting (deactivate)
 Cc Cx Ch
 Toggle display of hidden arguments
 Cc C=
 Show constraints
 Cc Cs
 Solve constraints
 Cc C?
 Show all goals
 Cc Cf
 Move to next goal (forward)
 Cc Cb
 Move to previous goal (backwards)
 Cc Cd
 Infer (deduce) type
 Cc Co
 Module contents
 Cc Cz
 Search through definitions in scope
 Cc Cn
 Compute normal form
 Cu Cc Cn
 Compute normal form, ignoring
abstract
 Cu Cu Cc Cn
 Compute and print normal form of
show <expression>
 Cc Cx M;
 Comment/uncomment rest of buffer
 Cc Cx Cs
 Switch to a different Agda version
Commands in context of a goalÂ¶
Commands expecting input (for example which variable to case split) will either use the text inside the goal or ask the user for input.
 Cc CSPC
 Give (fill goal)
 Cc Cr
 Refine. Partial give: makes new holes for missing arguments
 Cc Cm
 Elaborate and Give (fill goal with normalized expression).
Takes the same
Cu
prefixes asCc Cn
.  Cc Ca
 Automatic Proof Search (Auto)
 Cc Cc
 Case split
 Cc Ch
 Compute type of helper function and add type signature to kill ring (clipboard)
 Cc Ct
 Goal type
 Cc Ce
 Context (environment)
 Cc Cd
 Infer (deduce) type
 Cc C,
 Goal type and context
 Cc C.
 Goal type, context and inferred type
 Cc C;
 Goal type, context and checked term
 Cc Co
 Module contents
 Cc Cn
 Compute normal form
 Cu Cc Cn
 Compute normal form, ignoring
abstract
 Cu Cu Cc Cn
 Compute and print normal form of
show <expression>
Other commandsÂ¶
 TAB
 Indent current line, cycles between points
 STAB
 Indent current line, cycles in opposite direction
 M.
 Go to definition of identifier under point
 Middle mouse button
 Go to definition of identifier clicked on
 M*
 Go back (Emacs < 25.1)
 M,
 Go back (Emacs â„ 25.1)
Unicode inputÂ¶
How can I write Unicode characters using Emacs?Â¶
The Agda Emacs mode comes with an input method for easily writing
Unicode characters. Most Unicode character can be input by typing
their corresponding TeX/LaTeX commands, eg. typing \lambda
will
input Î»
. Some characters have key bindings which have not been
taken from TeX/LaTeX (typing \bN
results in â
being inserted,
for instance), but all bindings start with \
.
To see all characters you can input using the Agda input method type
Mx describeinputmethod RET Agda
or type Mx
agdainputshowtranslations RET RET
(with some exceptions in
certain versions of Emacs).
If you know the Unicode name of a character you can input it using
Mx ucsinsert RET
(which supports tabcompletion) or Cx 8
RET
. Example: Type Cx 8 RET not SPACE a SPACE sub TAB RET
to
insert the character âNOT A SUBSET OFâ (â
).
(The Agda input method has one drawback: if you make a mistake while typing the name of a character, then you need to start all over again. If you find this terribly annoying, then you can use Abbrev mode instead. However, note that Abbrev mode cannot be used in the minibuffer, which is used to give input to many Agda and Emacs commands.)
The Agda input method can be customised via Mx customizegroup RET
agdainput
.
OK, but how can I find out what to type to get the âŠ character?Â¶
To find out how to input a specific character, eg from the standard
library, position the cursor over the character and type Mx
describechar
or Cu Cx =
.
For instance, for â·
I get the following:
character: â· (displayed as â·) (codepoint 8759, #o21067, #x2237)
preferred charset: unicode (Unicode (ISO10646))
code point in charset: 0x2237
script: symbol
syntax: w which means: word
category: .:Base, c:Chinese
to input: type "\::" with Agda input method
buffer code: #xE2 #x88 #xB7
file code: #xE2 #x88 #xB7 (encoded by coding system utf8unix)
display: by this font (glyph code)
x:miscfixedmediumrnormal202007575c100iso106461 (#x2237)
Character code properties: customize what to show
name: PROPORTION
generalcategory: Sm (Symbol, Math)
decomposition: (8759) ('â·')
There are text properties here:
fontified t
Here it says that I can type \::
to get a â·
. If there is no
âto inputâ line, then you can add a key binding to the Agda input
method by using Mx customizevariable RET
agdainputusertranslations
.
Show me some commonly used charactersÂ¶
Many common characters have a shorter input sequence than the corresponding TeX command:
 Arrows:
\r
forâ
. You can replacer
with another direction:u
,d
,l
. Eg.\d
forâ
. Replace
with=
or==
to get a double and triple arrows.  Greek letters can be input by
\G
followed by the first character of the letters Latin name. Eg.\Gl
will inputÎ»
while\GL
will inputÎ
.  Negation: you can get the negated form of many characters by
appending
n
to the name. Eg. while\ni
inputsâ
,\nin
will inputâ
.  Subscript and superscript: you can input subscript or
superscript forms by prepending the character with
\_
(subscript) or\^
(superscript). Note that not all characters have a subscript or superscript counterpart in Unicode.
Some characters which were used in this documentation or which are commonly used in the standard library (sorted by hexadecimal code):
Hex code  Character  Short keybinding  TeX command 

00AC  ÂŹ 
\neg 

00D7  Ă 
\x 
\times 
02E2  Ëą 
\^s 

03BB  Î» 
\Gl 
\lambda 
041F  Đ 

0432  ĐČ 

0435  Đ” 

0438  Đž 

043C  ĐŒ 

0440  Ń 

0442  Ń 

1D62  á”ą 
\_i 

2032  âČ 
\'1 
\prime 
207F  âż 
\^n 

2081  â 
\_1 

2082  â 
\_2 

2083  â 
\_3 

2084  â 
\_4 

2096  â 
\_k 

2098  â 
\_m 

2099  â 
\_n 
Hex code  Character  Short keybinding  TeX command 

2113  â 
\ell 
Hex code  Character  Short keybinding  TeX command 

2115  â 
\bN 
\Bbb{N} 
2191  â 
\u 
\uparrow 
2192  â 
\r 
\to 
21A6  âŠ 
\r 
\mapsto 
2200  â 
\all 
\forall 
2208  â 
\in 

220B  â 
\ni 

220C  â 
\nin 

2218  â 
\o 
\circ 
2237  â· 
\:: 

223C  âŒ 
\~ 
\sim 
2248  â 
\~~ 
\approx 
2261  âĄ 
\== 
\equiv 
2264  â€ 
\<= 
\le 
2284  â 
\subn 

228E  â 
\u+ 
\uplus 
2294  â 
\lub 

22A2  âą 
\ 
\vdash 
22A4  â€ 
\top 

22A5  â„ 
\bot 

266D  â 
\b 

266F  âŻ 
\# 

27E8  âš 
\< 

27E9  â© 
\> 
Hex code  Character  Short keybinding  TeX command 

2983  âŠ 
\{{ 

2984  âŠ 
\}} 

2985  âŠ

\(( 

2986  âŠ 
\)) 
Hex code  Character  Short keybinding  TeX command 

2C7C  â±Œ 
\_j 
HighlightÂ¶
Clauses which do not hold definitionally (see Case trees) are highlighted in white smoke.
Literate ProgrammingÂ¶
Agda supports a limited form of literate programming, i.e. code interspersed with prose, if the corresponding filename extension is used.
Literate TeXÂ¶
Files ending in .lagda
or .lagda.tex
are interpreted
as literate TeX files. All code has to appear in code blocks:
Ignored by Agda.
\begin{code}[ignored by Agda]
module Whatever where
 Agda code goes here
\end{code}
Text outside of code blocks is ignored, as well as text right after
\begin{code}
, on the same line.
Agda finds code blocks by looking for the first instance of
\begin{code}
that is not preceded on the same line by %
or
\
(not counting \
followed by any code point), then (starting
on the next line) the first instance of \end{code}
that is
preceded by nothing but spaces or tab characters (\t
), and so on
(always starting on the next line). Note that Agda does not try to
figure out if, say, the LaTeX code changes the category code of %
.
If you provide a suitable definition for the code environment, then literate Agda files can double as LaTeX document sources. Example definition:
\usepackage{fancyvrb}
\DefineVerbatimEnvironment
{code}{Verbatim}
{} % Add fancy options here if you like.
The LaTeX backend or the preprocessor lhs2TeX can also be used to produce LaTeX code from literate Agda files. See Known pitfalls and issues for how to make LaTeX accept Agda files using the UTF8 character encoding.
Literate reStructuredTextÂ¶
Files ending in .lagda.rst
are interpreted as literate
reStructuredText files. Agda will parse code following a line ending
in ::
, as long as that line does not start with ..
:
This line is ordinary text, which is ignored by Agda.
::
module Whatever where
 Agda code goes here
Another noncode line.
::
.. This line is also ignored
reStructuredText source files can be turned into other formats such as HTML or LaTeX using Sphinx.
 Code blocks inside an rST comment block will be typechecked by Agda, but not rendered.
 Code blocks delimited by
.. codeblock:: agda
or.. codeblock:: lagda
will be rendered, but not typechecked by Agda.  All lines inside a codeblock must be further indented than the first line of the code block.
 Indentation must be consistent between code blocks. In other words, the file as a whole must be a valid Agda file if all the literate text is replaced by white space.
Literate MarkdownÂ¶
Files ending in .lagda.md
are interpreted as literate
Markdown files. Code blocks start with ```
or ```agda
on
its own line, and end with ```
, also on its own line:
This line is ordinary text, which is ignored by Agda.
```
module Whatever where
 Agda code goes here
```
Here is another code block:
```agda
data â : Set where
zero : â
suc : â â â
```
Markdown source files can be turned into many other formats such as HTML or LaTeX using PanDoc.
 Code blocks which should be typechecked by Agda but should not be
visible when the Markdown is rendered may be enclosed in HTML
comment delimiters (
<!
and>
).  Code blocks which should be ignored by Agda, but rendered in the final document may be indented by four spaces.
 Note that inline code fragments are not supported due to the difficulty of interpreting their indentation level with respect to the rest of the file.
Literate OrgÂ¶
Files ending in .lagda.org
are interpreted as literate
Org files. Code blocks are surrounded by two lines including only
`#+begin_src agda2`
and `#+end_src`
(case insensitive).
This line is ordinary text, which is ignored by Agda.
#+begin_src agda2
module Whatever where
 Agda code goes here
#+end_src
Another noncode line.
 Code blocks which should be ignored by Agda, but rendered in the
final document may be placed in source blocks without the
agda2
label.
Generating HTMLÂ¶
To generate highlighted, hyperlinked web pages from source code, run the following command in a shell:
$ agda html htmldir={output directory} {root module}
You can change the way in which the code is highlighted by providing
your own CSS file instead of the default, included one
(use the css
option).
If youâre using Literate Agda with Markdown or reStructedText and you
want to highlight your Agda codes with Agdaâs HTML backend and render
the rest of the content (letâs call it âliterateâ part for convenience)
with some another renderer, you can use the htmlhighlight=code
option, which makes the Agda compiler:
 not wrapping the literate part into
<a class="Background">
tags  not wrapping the generated document with a
<html>
tag, which means youâll have to specify the CSS location somewhere else, like<link rel="stylesheet" type="text/css" href="Agda.css">
 converting
<a class="Markup">
tags into<pre class="agdacode">
tags that wrap the complete Agda code block below  generating files with extension asis (i.e. .lagda.md becomes .md, .lagda.rst becomes .rst)
 for reStructuredText, a
.. raw:: html
will be inserted before every code blocks
This will affect all the files involved in one compilation, making
pure Agda code files rendered without HTML footer/header as well.
To use code
with literate Agda files and all
with pure Agda
files, use htmlhighlight=auto
, which means autodetection.
Generating LaTeXÂ¶
An experimental LaTeX backend was added in Agda 2.3.2. It can be used as follows:
$ agda latex {file}.lagda
$ cd latex
$ {latexcompiler} {file}.tex
where latexcompiler
could be pdflatex,
xelatex or lualatex, and file.lagda
is
a literate Agda TeX file (it could also be
called file.lagda.tex
). The source file is expected to
import the LaTeX package agda
by including the code
\usepackage{agda}
(possibly with some options). Unlike the
HTML backend only the topmost module is
processed. Imported modules can be processed by invoking
agda latex
manually on each of them.
The LaTeX backend checks if agda.sty
is found by the LaTeX
environment. If it isnât, a default agda.sty
is copied into
the LaTeX output directory (by default latex
). Note that the
appearance of typeset code can be modified by overriding definitions
from agda.sty
.
Known pitfalls and issuesÂ¶
Unicode characters may not be typeset properly out of the box. How to address this problem depends on what LaTeX engine is used.
pdfLaTeX:
The pdfLaTeX program does not by default understand the UTF8 character encoding. You can tell it to treat the input as UTF8 by using the inputenc package:
\usepackage[utf8]{inputenc}
If the inputenc package complains that some Unicode character is ânot set up for use with LaTeXâ, then you can give your own definition. Here is one example:
\usepackage{newunicodechar} \newunicodechar{Î»}{\ensuremath{\mathnormal\lambda}} \newunicodechar{â}{\ensuremath{\mathnormal\from}} \newunicodechar{â}{\ensuremath{\mathnormal\to}} \newunicodechar{â}{\ensuremath{\mathnormal\forall}}
XeLaTeX or LuaLaTeX:
It can sometimes be easier to use LuaLaTeX or XeLaTeX. When these engines are used it might suffice to choose a suitable font, as long as it contains all the right symbols in all the right shapes. If it does not, then
\newunicodechar
can be used as above. Here is one example:\usepackage{unicodemath} \setmathfont{XITS Math} \usepackage{newunicodechar} \newunicodechar{Î»}{\ensuremath{\mathnormal\lambda}}
If
<
and>
are typeset likeÂĄ
andÂż
, then the problem might be that you are using pdfLaTeX and have not selected a suitable font encoding.Possible workaround:
\usepackage[T1]{fontenc}
If a regular text font is used, then

might be typeset as an en dash (â).Possible workarounds:
Use a monospace font.
Turn off ligatures. With pdfLaTeX the following code (which also selects a font encoding, and only turns off ligatures for character sequences starting with

) might work:\usepackage[T1]{fontenc} \usepackage{microtype} \DisableLigatures[]{encoding=T1}
With LuaLaTeX or XeLaTeX the following code (which also selects a font) might work:
\usepackage{fontspec} \defaultfontfeatures[\rmfamily]{} \setmainfont{Latin Modern Roman}
Note that you might not want to turn off all kinds of ligatures in the entire document. See the examples below for information on how to set up special font families without TeX ligatures that are only used for Agda code.
The unicodemath package and older versions of the polytable package are incompatible, which can result in errors in generated LaTeX code.
Possible workaround: Download a more uptodate version of polytable and put it together with the generated files or install it globally.
OptionsÂ¶
The following commandline options change the behaviour of the LaTeX backend:
latexdir=directory
 Changes the output directory where
agda.sty
and the output.tex
file are placed todirectory
. Default:latex
. onlyscopechecking
 Generates highlighting without typechecking the file. See Quicker generation without typechecking.
countclusters
 Count extended grapheme clusters when generating LaTeX code. This
option can be given in
OPTIONS
pragmas. See Counting Extended Grapheme Clusters.
The following options can be given when loading agda.sty
by using
\usepackage[options]{agda}
:
bw
 Colour scheme which highlights in black and white.
conor
 Colour scheme similar to the colours used in Epigram 1.
references
 Enables inline typesetting of referenced code.
links
 Enables hyperlink support.
Quicker generation without typecheckingÂ¶
A faster variant of the backend is available by invoking
QuickLaTeX
from the Emacs mode, or using agda latex
onlyscopechecking
. When this variant of the backend is used the
toplevel module is not typechecked, only scopechecked. Note that
this can affect the generated document. For instance, scopechecking
does not resolve overloaded constructors.
If the module has already been typechecked successfully, then this information is reused; in this case QuickLaTeX behaves like the regular LaTeX backend.
FeaturesÂ¶
Vertical spaceÂ¶
Code blocks are by default surrounded by vertical space. Use
\AgdaNoSpaceAroundCode{}
to avoid this vertical space, and
\AgdaSpaceAroundCode{}
to reenable it.
Note that, if \AgdaNoSpaceAroundCode{}
is used, then empty lines
before or after a code block will not necessarily lead to empty lines
in the generated document. However, empty lines inside the code block
do (by default, with or without \AgdaNoSpaceAroundCode{}
) lead to
empty lines in the output. The height of such empty lines can be
controlled by the length \AgdaEmptySkip
, which by default is
\abovedisplayskip
.
AlignmentÂ¶
Tokens preceded by two or more space characters, as in the following example, are aligned in the typeset output:
\begin{code}
data â : Set where
zero : â
suc : â â â
_+_ : â â â â â
zero + n = n
suc m + n = suc (m + n)
\end{code}
In the case of the first token on a line a single space character sometimes suffices to get alignment. A constraint on the indentation of the first token t on a line is determined as follows:
 Let T be the set containing every previous token (in any code block) that is either the initial token on its line or preceded by at least one whitespace character.
 Let S be the set containing all tokens in T that are not shadowed by other tokens in T. A token tâ is shadowed by tâ if tâ is further down than tâ and does not start to the right of tâ.
 Let L be the set containing all tokens in S that start to the left of t, and E be the set containing all tokens in S that start in the same column as t.
 The constraint is that t must be indented further than every token in L, and aligned with every token in E.
Note that if any token in L or E belongs to a previous code block,
then the constraint may not be satisfied unless (say) the
AgdaAlign
environment is used in
an appropriate way. If custom settings are used, for instance if
\AgdaIndent
is redefined, then the constraint discussed above may
not be satisfied.
Examples:
 Here
C
is indented further thanB
:
postulate
A B
C : Set
 Here
C
is not (necessarily) indented further thanB
, becauseX
shadowsB
:
postulate
A B : Set
X
C : Set
These rules are inspired by, but not identical to, the one used by lhs2TeXâs poly mode (see Section 8.4 of the manual for lhs2TeX version 1.17).
Counting Extended Grapheme ClustersÂ¶
The alignment feature regards the string +ÌČ
, containing +
and a
combining character, as having length two. However, it seems more
reasonable to treat it as having length one, as it occupies a single
column, if displayed âproperlyâ using a monospace font. The flag
countclusters
is an attempt at fixing this. When this flag is
enabled the backend counts âextended grapheme clustersâ
rather than code points.
Note that this fix is not perfect: a single extended grapheme cluster might be displayed in different ways by different programs, and might, in some cases, occupy more than one column. Here are some examples of extended grapheme clusters, all of which are treated as a single character by the alignment algorithm:
â â
â+ÌČâ
âOÌÌâ
âàźšàźżâ
âáí°áčâ
âááááááí°áčáčáčáčáčáčâ
â â
Note also that the layout machinery does not count extended grapheme
clusters, but code points. The following code is syntactically
correct, but if countclusters
is used, then the LaTeX backend
does not align the two field keywords:
record +ÌČ : Setâ where field A : Set
field B : Set
The countclusters
flag is not enabled in all builds of Agda,
because the implementation depends on the ICU library, the
installation of which could cause extra trouble for some users. The
presence of this flag is controlled by the Cabal flag
enableclustercounting
.
Breaking up code blocksÂ¶
Sometimes one might want to break up a code block into multiple
pieces, but keep code in different blocks aligned with respect to each
other. Then one can use the AgdaAlign
environment. Example usage:
\begin{AgdaAlign}
\begin{code}
code
code (more code)
\end{code}
Explanation...
\begin{code}
aligned with "code"
code (aligned with (more code))
\end{code}
\end{AgdaAlign}
Note that AgdaAlign
environments should not be nested.
Sometimes one might also want to hide code in the middle of a code block. This can be accomplished in the following way:
\begin{AgdaAlign}
\begin{code}
visible
\end{code}
\begin{code}[hide]
hidden
\end{code}
\begin{code}
visible
\end{code}
\end{AgdaAlign}
However, the result may be ugly: extra space is perhaps inserted
around the code blocks. The AgdaSuppressSpace
environment ensures
that extra space is only inserted before the first code block, and
after the last one (but not if \AgdaNoSpaceAroundCode{}
is
used). Example usage:
\begin{AgdaAlign}
\begin{code}
code
more code
\end{code}
Explanation...
\begin{AgdaSuppressSpace}
\begin{code}
aligned with "code"
aligned with "more code"
\end{code}
\begin{code}[hide]
hidden code
\end{code}
\begin{code}
also aligned with "more code"
\end{code}
\end{AgdaSuppressSpace}
\end{AgdaAlign}
Note that AgdaSuppressSpace
environments should not be nested. There
is also a combined environment, AgdaMultiCode
, that combines the
effects of AgdaAlign
and AgdaSuppressSpace
.
Hiding codeÂ¶
Code that you do not want to show up in the output can be hidden by
giving the argument hide
to the code block:
\begin{code}[hide]
 the code here will not be part of the final document
\end{code}
Hyperlinks (experimental)Â¶
If the hyperref latex package is loaded before the agda package and
the links
option is passed to the agda package, then the agda package
provides a function called \AgdaTarget
. Identifiers which have been
declared targets, by the user, will become clickable hyperlinks in the
rest of the document. Here is a small example:
\documentclass{article}
\usepackage{hyperref}
\usepackage[links]{agda}
\begin{document}
\AgdaTarget{â}
\AgdaTarget{zero}
\begin{code}
data â : Set where
zero : â
suc : â â â
\end{code}
See next page for how to define \AgdaFunction{two} (doesn't turn into a
link because the target hasn't been defined yet). We could do it
manually though; \hyperlink{two}{\AgdaDatatype{two}}.
\newpage
\AgdaTarget{two}
\hypertarget{two}{}
\begin{code}
two : â
two = suc (suc zero)
\end{code}
\AgdaInductiveConstructor{zero} is of type
\AgdaDatatype{â}. \AgdaInductiveConstructor{suc} has not been defined to
be a target so it doesn't turn into a link.
\newpage
Now that the target for \AgdaFunction{two} has been defined the link
works automatically.
\begin{code}
data Bool : Set where
true false : Bool
\end{code}
The AgdaTarget command takes a list as input, enabling several targets
to be specified as follows:
\AgdaTarget{if, then, else, if\_then\_else\_}
\begin{code}
if_then_else_ : {A : Set} â Bool â A â A â A
if true then t else f = t
if false then t else f = f
\end{code}
\newpage
Mixfix identifier need their underscores escaped:
\AgdaFunction{if\_then\_else\_}.
\end{document}
The borders around the links can be suppressed using hyperrefâs
hidelinks
option:
\usepackage[hidelinks]{hyperref}
Warning
The current approach to links does not keep track of scoping or types, and hence overloaded names might create links which point to the wrong place. Therefore it is recommended to not overload names when using the links option at the moment. This might get fixed in the future.
Inline codeÂ¶
Code can be typeset inline by giving the argument inline
to the
code block:
Assume that we are given a type
%
\begin{code}[hide]
module _ (
\end{code}
\begin{code}[inline]
A : Set
\end{code}
\begin{code}[hide]
) where
\end{code}
%
.
There is also a variant of inline
, inline*
. If inline*
is
used, then space (\AgdaSpace{}
) is added at the end of the code,
and when inline
is used space is not added.
The implementation of these options is a bit of a hack. Only use these options for typesetting a single line of code without multiple consecutive whitespace characters (except at the beginning of the line).
Another way to typeset inline codeÂ¶
An alternative to using inline
and inline*
is to typeset code
manually. Here is an example:
Below we postulate the existence of a type called
\AgdaPostulate{apa}:
%
\begin{code}
postulate apa : Set
\end{code}
You can find all the commands used by the backend (and which you can
use manually) in the agda.sty
file.
Semiautomatically typesetting inline code (experimental)Â¶
Since Agda version 2.4.2 there is experimental support for
semiautomatically typesetting code inside text, using the
references
option. After loading the agda package with this
option, inline Agda snippets will be typeset in the same way as code
blocksâafter postprocessingâif referenced using the \AgdaRef
command. Only the current module is used; should you need to reference
identifiers in other modules, then you need to specify which other
module manually by using \AgdaRef[module]{identifier}
.
In order for the snippets to be typeset correctly, they need to be postprocessed by the postprocesslatex.pl script from the Agda data directory. You can copy it into the current directory by issuing the command
$ cp $(dirname $(dirname $(agdamode locate)))/postprocesslatex.pl .
In order to generate a PDF, you can then do the following:
$ agda latex {file}.lagda
$ cd latex/
$ perl ../postprocesslatex.pl {file}.tex > {file}.processed
$ mv {file}.processed {file}.tex
$ xelatex {file}.tex
Here is a full example, consisting of a Literate Agda file
Example.lagda
and a makefile Makefile
.
\documentclass{article}
\usepackage[references]{agda}
\begin{document}
Here we postulate \AgdaRef{apa}.
%
\begin{code}
postulate apa : Set
\end{code}
\end{document}
AGDA=agda
AFLAGS=i. latex
SOURCE=Example
POSTPROCESS=postprocesslatex.pl
LATEX=latexmk pdf usemake xelatex
all:
$(AGDA) $(AFLAGS) $(SOURCE).lagda
cd latex/ && \
perl ../$(POSTPROCESS) $(SOURCE).tex > $(SOURCE).processed && \
mv $(SOURCE).processed $(SOURCE).tex && \
$(LATEX) $(SOURCE).tex && \
mv $(SOURCE).pdf ..
See Issue #1054 on the bug tracker for implementation details.
Warning
Overloading identifiers should be avoided. If multiple identifiers with the same name exist, then AgdaRef will typeset according to the first one it finds.
Controlling the typesetting of individual tokensÂ¶
The typesetting of (certain) individual tokens can be controlled by
redefining the \AgdaFormat
command. Example:
\usepackage{ifthen}
% Insert extra space before some tokens.
\DeclareRobustCommand{\AgdaFormat}[2]{%
\ifthenelse{
\equal{#1}{âĄâš} \OR
\equal{#1}{âĄâšâ©} \OR
\equal{#1}{â}
}{\ }{}#2}
Note the use of \DeclareRobustCommand
. The first argument to
\AgdaFormat
is the token, and the second argument the thing to be
typeset.
Emulating %format rulesÂ¶
The LaTeX backend has no feature directly comparable to lhs2TeXâs
%format
rules. However, one can hack up something similar by using
a program like sed. For instance, let us say that
replace.sed
contains the following text:
# Turn ÎŁ[ x â X ] into (x : X) Ă.
s/\\AgdaRecord{ÎŁ\[} \(.*\) \\AgdaRecord{â} \(.*\) \\AgdaRecord{]}/\\AgdaSymbol\{(\}\1 \\AgdaSymbol\{:\} \2\\AgdaSymbol\{)\} \\AgdaFunction\{Ă\}/g
The output of the LaTeX backend can then be postprocessed in the following way:
$ sed f replace.sed {file}.tex > {file}.sedded
$ mv {file}.sedded {file}.tex
Including Agda code in a larger LaTeX documentÂ¶
Sometimes you might want to include a bit of code without making the
whole document a literate Agda file. Here is one way in which this can
be accomplished. (Perhaps this technique was invented by Anton
Setzer.) Put the code in a separate file, and use \newcommand
to
give a name to each piece of code that should be typeset:
\newcommand{\nat}{%
\begin{code}
data â : Set where
zero : â
suc : (n : â) â â
\end{code}}
Preprocess this file using Agda, and then include it in another file in the following way:
% In the preamble:
\usepackage{agda}
% Further setup related to Agda code.
% The Agda code can be included either in the preamble or in the
% document's body.
\input{Code}
% Then one can refer to the Agda code in the body of the text:
The natural numbers can be defined in the following way in Agda:
\nat{}
Here it is assumed that agda.sty
is available in the current
directory (or on the TeX search path).
Note that this technique can also be used to present code in a different order, if the rules imposed by Agda are not compatible with the order that you would prefer.
ExamplesÂ¶
Some examples that can be used for inspiration (in the HTML version of the manual you see links to the source code and in the PDF version of the manual you see inline source code).
Note that these examples might not satisfy all your requirements, and might not work in all settings (in particular, for LuaLaTeX or XeLaTeX it might be necessary to install one or more fonts). If you have to follow a particular house style, then you may want to make sure that the Agda code follows this style, and that you do not inadvertently change the style of other text when customising the style of the Agda code.
Library ManagementÂ¶
Agda has a simple package management system to support working with multiple libraries in different locations. The central concept is that of a library.
Example: Using the standard libraryÂ¶
Before we go into details, here is some quick information for the impatient on how to tell Agda about the location of the standard library, using the library management system.
Letâs assume you have downloaded the standard library into a directory which we
will refer to by AGDA_STDLIB
(as an absolute path). A library file
standardlibrary.agdalib
should exist in this directory, with the
following content:
name: standardlibrary
include: src
To use the standard library by default in your Agda projects, you have to do two things:
Create a file
AGDA_DIR/libraries
with the following content:AGDA_STDLIB/standardlibrary.agdalib
(Of course, replace
AGDA_STDLIB
by the actual path.)The
AGDA_DIR
defaults to~/.agda
on unixlike systems andC:\Users\USERNAME\AppData\Roaming\agda
or similar on Windows. (More onAGDA_DIR
below.)Remark: The
libraries
file informs Agda about the libraries you want it to know about.
Create a file
AGDA_DIR/defaults
with the following content:standardlibrary
Remark: The
defaults
file informs Agda which of the libraries pointed to bylibraries
should be used by default (i.e. in the default include path).
Thatâs the short version, if you want to know more, read on!
Library filesÂ¶
A library consists of
 a name
 a set of dependencies
 a set of include paths
Libraries are defined in .agdalib
files with the following syntax:
name: LIBRARYNAME  Comment
depend: LIB1 LIB2
LIB3
LIB4
include: PATH1
PATH2
PATH3
Dependencies are library names, not paths to .agdalib
files, and include
paths are relative to the location of the libraryfile.
Each of the three fields is optional.
Naturally, unnamed libraries cannot be depended upon.
But dropping the name
is possible if the library file only serves to list
include paths and/or dependencies of the current project.
Installing librariesÂ¶
To be found by Agda a library file has to be listed (with its full path) in a
libraries
file
AGDA_DIR/librariesVERSION
, or if that doesnât existAGDA_DIR/libraries
where VERSION
is the Agda version (for instance 2.5.1
). The
AGDA_DIR
defaults to ~/.agda
on unixlike systems and
C:\Users\USERNAME\AppData\Roaming\agda
or similar on Windows, and can be
overridden by setting the AGDA_DIR
environment variable.
Each line of the libraries file shall be the absolute file system path to the root of a library.
Environment variables in the paths (of the form $VAR
or ${VAR}
) are
expanded. The location of the libraries
file used can be overridden using
the libraryfile=FILE
command line option.
You can find out the precise location of the libraries
file by
calling agda l fjdsk Dummy.agda
at the command line and looking at the
error message (assuming you donât have a library called fjdsk
installed).
Note that if you want to install a library so that it is used by default,
it must also be listed in the defaults
file (details below).
Using a libraryÂ¶
There are three ways a library gets used:
You supply the
library=LIB
(orl LIB
) option to Agda. This is equivalent to adding aiPATH
for each of the include paths ofLIB
and its (transitive) dependencies.No explicit
library
flag is given, and the current project root (of the Agda file that is being loaded) or one of its parent directories contains an.agdalib
file defining a libraryLIB
. This library is used as if alibrary=LIB
option had been given, except that it is not necessary for the library to be listed in theAGDA_DIR/libraries
file.No explicit
library
flag, and no.agdalib
file in the project root. In this case the fileAGDA_DIR/defaults
is read and all libraries listed are added to the path. Thedefaults
file should contain a list of library names, each on a separate line. In this case the current directory is also added to the path.To disable default libraries, you can give the flag
nodefaultlibraries
. To disable using libraries altogether, use thenolibraries
flag.
Default librariesÂ¶
If you want to usually use a variety of libraries, it is simplest to list them
all in the AGDA_DIR/defaults
file.
Each line of the defaults file shall be the name of a library resolvable using the paths listed in the libraries file. For example,
standardlibrary library2 library3
where of course library2
and library3
are the libraries you commonly use.
While it is safe to list all your libraries in library
, be aware that listing
libraries with name clashes in defaults
can lead to difficulties, and should be
done with care (i.e. avoid it unless you really must).
Version numbersÂ¶
Library names can end with a version number (for instance, mylib1.2.3
).
When resolving a library name (given in a library
flag, or listed as a
default library or library dependency) the following rules are followed:
 If you donât give a version number, any version will do.
 If you give a version number an exact match is required.
 When there are multiple matches an exact match is preferred, and otherwise the latest matching version is chosen.
For example, suppose you have the following libraries installed: mylib
,
mylib1.0
, otherlib2.1
, and otherlib2.3
. In this case, aside from
the exact matches you can also say library=otherlib
to get
otherlib2.3
.
UpgradingÂ¶
If you are upgrading from a pre 2.5 version of Agda, be aware that you may have
remnants of the previous library management system in your preferences. In particular,
if you get warnings about agda2includedirs
, you will need to find where this is
defined. This may be buried deep in .el
files, whose location is both operating
system and emacs version dependant.
ContributeÂ¶
See also the HACKING file in the root of the Agda repository.
DocumentationÂ¶
Documentation is written in reStructuredText format.
The Agda documentation is shipped together with the main Agda
repository in the doc/usermanual
subdirectory. The content of
this directory is automatically published to https://agda.readthedocs.io.
Rendering documentation locallyÂ¶
To build the user manual locally, you need to install the following dependencies:
Python â„3.3
Sphinx
andsphinxrtdtheme
pip install user r doc/usermanual/requirements.txt
Note that the
user
option puts the Sphinx binaries in$HOME/.local/bin
.LaTeX
dvipng
To see the list of available targets, execute
make help
indoc/usermanual
. E.g., callmake html
to build the documentation in html format.
Typechecking code examplesÂ¶
You can include code examples in your documentation.
If your give the documentation file the extension .lagda.rst
, Agda will
recognise it as an Agda file and typecheck it.
Tip