Main features of Agda
- Dependent types
- Indexed datatypes and dependent pattern matching
- Termination checking and productivity checking
- A universe hierachy with universe polymorphism
- Implicit arguments
- Parametrized modules (~ ML functors)
Lecture 1: Getting started with Agda
Jesper Cockx
31 August 2019
“A computer will do what you tell it to do, but that may be much different from what you had in mind.”
–Joseph Weizenbaum
“Program testing can be used to show the presence of bugs, but never to show their absence!”
–Edsger Dijkstra
“That is the very purpose of declarative programming – to make it more likely that we mean what we say by improving our ability to say what we mean.”
–Conor McBride
With dependent types, we can statically verify that a program satisfies a given correctness property.
Verification is built into the language itself.
The intrinsic approach is also called correct-by-construction programming.
module Extrinsic where sort : List ℕ → List ℕ sort = ⋯ IsSorted : List ℕ → Set IsSorted = ⋯ sort-is-sorted : ∀ xs → IsSorted (sort xs) sort-is-sorted = ⋯
module Intrinsic where SortedList : Set SortedList = ⋯ sort : List ℕ → SortedList sort = ⋯
Building invariants into the types of our program, to make it impossible to write an incorrect program in the first place.
No proving required!
Implementation of a correct-by-construction typechecker + interpreter for a C-like language (WHILE)
Agda is…
We will mostly use 1. in this course.
For this tutorial, you will need to install Agda, the Agda standard library, and the BNFC tool.
Installation instructions:
We will use many of these in the course of this tutorial!
Basic commands:
Programs may contain holes (? or {! !}).
Agda’s Emacs mode interprets many latex-like commands as unicode symbols:
\lambda
= λ
\forall
= ∀
\r
= →
, \l
= ←
\Gamma
= Γ
, \Sigma
= Σ
, …\equiv
= ≡
\::
= ∷
\bN
= ℕ
, \bZ
= ℤ
, …To get information about specific character, use M-x describe-char
data Bool : Set where true : Bool false : Bool data ℕ : Set where zero : ℕ suc : (n : ℕ) → ℕ
_+_ : ℕ → ℕ → ℕ zero + y = y suc x + y = suc (x + y)
Note: underscores indicate argument positions for mixfix functions
f : Bool → Bool f = λ { true → false ; false → true }Alternative syntax:
f′ : Bool → Bool f′ = λ where true → false false → true
The identity type x ≡ y
is inhabited by refl
iff x
and y
are (definitionally) equal.
We can use this to write checked tests for our Agda functions!
open import Relation.Binary.PropositionalEquality using (_≡_; refl) testPlus : 1 + 1 ≡ 2 testPlus = refl
data List (A : Set) : Set where [] : List A _∷_ : A → List A → List A data Maybe (A : Set) : Set where nothing : Maybe A just : A → Maybe A
if_then_else_ : {A : Set} → Bool → A → A → A if false then x else y = y if true then x else y = x
Note: {A : Set}
indicates an implicit argument
open import Data.Char using (Char) open import Data.Integer using (ℤ) data Id : Set where mkId : List Char → Id data Exp : Set where eId : (x : Id) → Exp eInt : (i : ℤ) → Exp eBool : (b : Bool) → Exp ePlus : (e e' : Exp) → Exp eGt : (e e' : Exp) → Exp eAnd : (e e' : Exp) → Exp
data Val : Set where intV : ℤ → Val boolV : Bool → Val eval : Exp → Maybe Val eval = ⋯
git clone https://github.com/jespercockx/ohrid19-agda
~
or -
) and add it to AST.agda
and UntypedInterpreter.agda