ITP Exercise Sheet 01

Discussion: 23 April 2026

The exercise sheets themselves are written in "Literate Agda" with Markdown (*.lagda.md). If you download them (see the link at the very bottom), you can edit them interactively just like other *.agda files. That is why the last Agda snippet in them has to be followed by another ```.

{-# OPTIONS --allow-unsolved-metas #-}

Setting up Agda

  • Install Agda (version 2.8.0), cf. Setup. After a successful installation, agda can be invoked as follows:
$ agda --version
Agda version 2.8.0

Creating an Agda project

In the root directory of your personal Agda project, create a file itp.agda-lib with the following content:

name: itp
depend:
  standard-library-2.3
include:
  src
  • The name of the project is itp (freely choosable)
  • The project requires the agda stdlib at version 2.3
  • The Agda files of our project live in a subdirectory src/

Testing the Agda project

Create a file src/Test.agda with the following content:

id :  (S : Set)  S  S
id S x = x
  • Let the editor type-check the file.
  • Run agda src/Test.agda manually.

Even/Odd

open import Data.Unit
open import Data.Empty
open import Data.Nat using (; zero ; suc; _+_)
open import Data.Bool using (Bool; true; false; not; T)
open import Function.Bundles using (_⇔_)

data Even :   Set where
  0-Even : Even zero
  2+-Even :  k  Even k  Even (suc (suc k))

Define a function

even? :   Bool
even? zero = true
even? (suc zero) = false
even? (suc (suc x)) = even? x

even?' :   Bool
even?' zero = true
even?' (suc x) = not (even?' x)

open import Relation.Binary.PropositionalEquality

not∘not≡id :  b  not (not b)  b
not∘not≡id false = refl
not∘not≡id true = refl

even?≗even?' :  n  even? n  even?' n
even?≗even?' zero = refl
even?≗even?' (suc zero) = refl
even?≗even?' (suc (suc n)) = sym goal
  where
    IH : even?' n  even? n
    IH = sym (even?≗even?' n)
    x = even?' n
    y = even? n

    goal : not (not x)  y
    goal = trans (not∘not≡id x) IH

    -- Alternative proof:
    P : Bool  Set
    P z = not (not x)  z

    goal' : not (not x)  y
    goal' = subst P IH (not∘not≡id x)

Prove its correctness:

even?-correct₁ :  k  Even k  T (even? k)
even?-correct₁ 0 0-Even = tt
even?-correct₁ (suc (suc k)) (2+-Even k e) = even?-correct₁ k e

even?-correct₂ :  k  T (even? k)  Even k
even?-correct₂ zero e = 0-Even
even?-correct₂ (suc (suc k)) e = 2+-Even k (even?-correct₂ k e)

Church numerals

Convert natural numbers into Church numerals and back again:

Numeral : Set₁
Numeral =  {X : Set}  (X  X)  (X  X)

⌈_⌉ :   Numeral
 zero  f x = x
 suc k  f x = f ( k  f x)

toℕ : Numeral  
toℕ g = g suc zero

Prove that the conversion is correct (in one direction):

toℕ⌈n⌉≡n :  n  toℕ  n   n
toℕ⌈n⌉≡n zero = refl
toℕ⌈n⌉≡n (suc n) = cong suc (toℕ⌈n⌉≡n n)

Agda Quellcode herunterladen