Agda is a
dependently typed functional programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
originally developed by Ulf Norell at
Chalmers University of Technology
Chalmers University of Technology (, commonly referred to as Chalmers) is a private university, private research university located in Gothenburg, Sweden. Chalmers focuses on engineering and science, but more broadly it also conducts research ...
with implementation described in his PhD thesis. The original Agda system was developed at Chalmers by Catarina Coquand in 1999. The current version, originally named Agda 2, is a full rewrite, which should be considered a new language that shares a name and tradition.
Agda is also a
proof assistant
In computer science and mathematical logic, a proof assistant or interactive theorem prover is a software tool to assist with the development of formal proofs by human–machine collaboration. This involves some sort of interactive proof edi ...
based on the ''propositions-as-types'' paradigm (
Curry–Howard correspondence
In programming language theory and proof theory, the Curry–Howard correspondence is the direct relationship between computer programs and mathematical proofs. It is also known as the Curry–Howard isomorphism or equivalence, or the proofs-as-p ...
), but unlike
Rocq, has no separate ''tactics'' language, and proofs are written in a functional programming style. The language has ordinary programming constructs such as
data type
In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
s,
pattern matching
In computer science, pattern matching is the act of checking a given sequence of tokens for the presence of the constituents of some pattern. In contrast to pattern recognition, the match usually must be exact: "either it will or will not be a ...
,
records,
let expression
In computer science, a "let" expression associates a function definition with a restricted scope.
The "let" expression may also be defined in mathematics, where it associates a Boolean condition with a restricted scope.
The "let" expression may ...
s and modules, and a
Haskell
Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
-like
syntax
In linguistics, syntax ( ) is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure (constituenc ...
. The system has
Emacs
Emacs (), originally named EMACS (an acronym for "Editor Macros"), is a family of text editors that are characterized by their extensibility. The manual for the most widely used variant, GNU Emacs, describes it as "the extensible, customizable, s ...
,
Atom
Atoms are the basic particles of the chemical elements. An atom consists of a atomic nucleus, nucleus of protons and generally neutrons, surrounded by an electromagnetically bound swarm of electrons. The chemical elements are distinguished fr ...
, and
VS Code
Visual Studio Code, commonly referred to as VS Code, is an integrated development environment developed by Microsoft for Windows, Linux, macOS and web browsers. Features include support for debugging, syntax highlighting, intelligent code comple ...
interfaces
but can also be run in
batch processing
Computerized batch processing is a method of running software programs called jobs in batches automatically. While users are required to submit the jobs, no other interaction by the user is required to process the batch. Batches may automatically ...
mode from a
command-line interface
A command-line interface (CLI) is a means of interacting with software via command (computing), commands each formatted as a line of text. Command-line interfaces emerged in the mid-1960s, on computer terminals, as an interactive and more user ...
.
Agda is based on Zhaohui Luo's unified theory of dependent types (UTT), a type theory similar to
Martin-Löf type theory.
Agda is named after the
Swedish song "Hönan Agda", written by
Cornelis Vreeswijk
Cornelis Vreeswijk (8 August 1937 – 12 November 1987) was a Dutch singer-songwriter and poet who lived and worked primarily in Sweden.
Born to Dutch parents in IJmuiden, Netherlands, he emigrated to Sweden with his parents in 1949 at the age ...
, which is about a
hen
Hen commonly refers to a female animal: a female chicken, other gallinaceous bird, any type of bird in general, or a lobster. It is also a slang term for a woman.
Hen, HEN or Hens may also refer to:
Places Norway
*Hen, Buskerud, a village in R ...
named Agda. This alludes to the name of the theorem prover
Coq, which was named after
Thierry Coquand.
Features
Inductive types
The main way of defining data types in Agda is via inductive data types which are similar to
algebraic data type
In computer programming, especially functional programming and type theory, an algebraic data type (ADT) is a kind of composite data type, i.e., a data type formed by combining other types.
Two common classes of algebraic types are product ty ...
s in non-dependently typed programming languages.
Here is a definition of
Peano numbers in Agda:
data ℕ : Set where
zero : ℕ
suc : ℕ → ℕ
Basically, it means that there are two ways to construct a value of type
, representing a natural number. To begin,
zero
is a natural number, and if
n
is a natural number, then
suc n
, standing for the
successor
Successor may refer to:
* An entity that comes after another (see Succession (disambiguation))
Film and TV
* ''The Successor'' (1996 film), a film including Laura Girling
* The Successor (2023 film), a French drama film
* ''The Successor'' ( ...
of
n
, is a natural number too.
Here is a definition of the "less than or equal" relation between two natural numbers:
data _≤_ : ℕ → ℕ → Set where
z≤n : → zero ≤ n
s≤s : → n ≤ m → suc n ≤ suc m
The first constructor,
z≤n
, corresponds to the axiom that zero is less than or equal to any natural number. The second constructor,
s≤s
, corresponds to an inference rule, allowing to turn a proof of
n ≤ m
into a proof of
suc n ≤ suc m
. So the value
s≤s (z≤n )
is a proof that one (the successor of zero), is less than or equal to two (the successor of one). The parameters provided in
curly bracket
A bracket is either of two tall fore- or back-facing punctuation marks commonly used to isolate a segment of text or data from its surroundings. They come in four main pairs of shapes, as given in the box to the right, which also gives their n ...
s may be omitted if they can be inferred.
Dependently typed pattern matching
In core type theory, induction and recursion principles are used to prove theorems about
inductive type
In type theory, a system has inductive types if it has facilities for creating a new type from constants and functions that create terms of that type. The feature serves a role similar to data structures in a programming language and allows a ty ...
s. In Agda, dependently typed pattern matching is used instead. For example, natural number addition can be defined like this:
add zero n = n
add (suc m) n = suc (add m n)
This way of writing recursive functions/inductive proofs is more natural than applying raw induction principles. In Agda, dependently typed pattern matching is a primitive of the language; the core language lacks the induction/recursion principles that pattern matching translates to.
Metavariables
One of the distinctive features of Agda, when compared with other similar systems such as
Coq, is heavy reliance on metavariables for program construction. For example, one can write functions like this in Agda:
add : ℕ → ℕ → ℕ
add x y = ?
?
here is a metavariable. When interacting with the system in Emacs mode, it will show the user the expected type and allow them to refine the metavariable, i.e., to replace it with more detailed code. This feature allows incremental program construction in a way similar to tactics-based proof assistants such as Coq.
Proof automation
Programming in pure type theory involves a lot of tedious and repetitive proofs. Although Agda has no separate tactics language, it is possible to program useful tactics within Agda. Typically, this works by writing an Agda function that optionally returns a proof of some property of interest. A tactic is then constructed by running this function at type-checking time, for example using the following auxiliary definitions:
data Maybe (A : Set) : Set where
Just : A → Maybe A
Nothing : Maybe A
data isJust : Maybe A → Set where
auto : ∀ → isJust (Just x)
Tactic : ∀ (x : Maybe A) → isJust x → A
Tactic Nothing ()
Tactic (Just x) auto = x
(The pattern
()
, called ''absurd'', matches if the type checker finds that its type is uninhabited, i.e. proves that it stands for a false proposition, typically because all possible constructors have arguments that are unavailable, i.e. they have unsatisfiable premisses. Here no value of type
isJust A
can be constructed because, in that context, no value of type
A
exists to which we could apply the constructor
Just
. The right hand side is omitted from any equation that contains absurd patterns.) Given a function
check-even : (n : ) → Maybe (Even n)
that inputs a number and optionally returns a proof of its evenness, a tactic can then be constructed as follows:
check-even-tactic : → isJust (check-even n) → Even n
check-even-tactic = Tactic (check-even n)
lemma0 : Even zero
lemma0 = check-even-tactic auto
lemma2 : Even (suc (suc zero))
lemma2 = check-even-tactic auto
The actual proof of each lemma will be automatically constructed at type-checking time. If the tactic fails, type-checking will fail.
Further, to write more complex tactics, Agda supports automation via
reflective programming
In computer science, reflective programming or reflection is the ability of a process to examine, introspect, and modify its own structure and behavior.
Historical background
The earliest computers were programmed in their native assembly lang ...
(reflection). The reflection mechanism allows quoting program fragments into, or unquoting them from, the
abstract syntax tree
An abstract syntax tree (AST) is a data structure used in computer science to represent the structure of a program or code snippet. It is a tree representation of the abstract syntactic structure of text (often source code) written in a formal ...
. The way reflection is used is similar to the way
Template Haskell works.
Another mechanism for proof automation is proof search action in
Emacs
Emacs (), originally named EMACS (an acronym for "Editor Macros"), is a family of text editors that are characterized by their extensibility. The manual for the most widely used variant, GNU Emacs, describes it as "the extensible, customizable, s ...
mode. It enumerates possible proof terms (limited to 5 seconds), and if one of the terms fits the specification, it will be put in the meta variable where the action is invoked. This action accepts hints, e.g., which theorems and from which modules can be used, whether the action can use pattern matching, etc.
Termination checking
Agda is a
total functional programming
Total functional programming (also known as strong functional programming, to be contrasted with ordinary, or ''weak'' functional programming) is a programming paradigm that restricts the range of programs to those that are provably terminating.
...
language, i.e., each program in it must terminate and all possible patterns must be matched. Without this feature, the logic behind the language becomes inconsistent, and it becomes possible to prove arbitrary statements. For
termination checking, Agda uses the approach of the Foetus termination checker.
[Abel, Andreas. "foetus – Termination checker for simple functional programs." ''Programming Lab Report'' 474 (1998)]
/ref>
Standard library
Agda has an extensive de facto standard library
In computer programming, a standard library is the library (computing), library made available across Programming language implementation, implementations of a programming language. Often, a standard library is specified by its associated program ...
, which includes many useful definitions and theorems about basic data structures, such as natural numbers, lists, and vectors. The library
A library is a collection of Book, books, and possibly other Document, materials and Media (communication), media, that is accessible for use by its members and members of allied institutions. Libraries provide physical (hard copies) or electron ...
is in beta, and is under active development.
Unicode
One of the more notable features of Agda is a heavy reliance on Unicode
Unicode or ''The Unicode Standard'' or TUS is a character encoding standard maintained by the Unicode Consortium designed to support the use of text in all of the world's writing systems that can be digitized. Version 16.0 defines 154,998 Char ...
in program source code. The standard Emacs mode uses shortcuts for input, such as \Sigma
for Σ.
Backends
There are two compiler backends, MAlonzo for Haskell and one for JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
.
See also
* List of proof assistants
References
Further reading
*
*
External links
*
Introduction to Agda
a five-part YouTube playlist by Daniel Peebles
Brutal Introduction to Dependent Types in Agda
* ttps://www.youtube.com/playlist?list=PLtIZ5qxwSNnzpNqfXzJjlHI9yCAzRzKtx HoTTEST Summer School 2022 66 lectures on Homotopy Type Theory, including many introductory lectures and exercises on Agda
{{Chalmers University of Technology
Programming languages
Dependently typed languages
Functional languages
Pattern matching programming languages
Academic programming languages
Statically typed programming languages
Proof assistants
Free software programmed in Haskell
Haskell programming language family
Cross-platform free software
Free and open source compilers
Chalmers University of Technology
Programming languages created in 2007
2007 software
Articles with example Haskell code