Let's play Agda
{-# OPTIONS --cubical-compatible #-}
module Padova2025.ProgrammingBasics.HigherOrder where

Higher-order functions#

open import Padova2025.ProgrammingBasics.Booleans
open import Padova2025.ProgrammingBasics.Operators

Add introduction.

Exercise: Unary tautologies#

Implement a function is-tautology₁? which checks whether a given input function is constantly true.

For instance, for the function f defined by f x = x, the result of is-tautology₁ f should be false.

Exercise 1is-tautology₁
is-tautology₁ : (Bool → Bool) → Bool
is-tautology₁ f = {!!}
The reference solution uses: false _&&_ true
Reference solution
is-tautology₁ : (Bool → Bool) → Bool
is-tautology₁ f = f false && f true
-- There is also the following more cryptic solution:
-- is-tautology₁ f = f (f false)

Exercise: Binary tautologies#

Implement a function is-tautology₂? which checks whether a given input function of two arguments is constantly true.

For instance, for the function f defined by f x y = true, the result of is-tautology₂ f should be true.

Exercise 2is-tautology₂
is-tautology₂ : (Bool → Bool → Bool) → Bool
is-tautology₂ f = {!!}
The reference solution uses: is-tautology₁ false _&&_ true
Reference solution
is-tautology₂ : (Bool → Bool → Bool) → Bool
is-tautology₂ f = is-tautology₁ (f false) && is-tautology₁ (f true)

Exercise: Types are first-class values#

Most statically typed programming languages have a strict separation between values (which then exist only at runtime) and types (which then exist only at compiletime, and which are erased in the resulting machine code). In Agda, types are values in their own right—indeed, they are values of the type Set—and we can freely manipulate them.

For instance, define a function Endo which inputs a type X and outputs the type X → X of functions from X to X:

Exercise 3Endo
Endo : Set → Set
Endo X = {!!}
Reference solution
Endo : Set → Set
Endo X = X → X

Exercise: Composition of functions#

Implement the higher-order function which inputs two composable functions and outputs their composition.

Exercise 4_∘_
infixr 9 _∘_
_∘_ : {X Y Z : Set} → (Y → Z) → (X → Y) → (X → Z)
_∘_ = {!!}
Reference solution
infixr 9 _∘_
_∘_ : {X Y Z : Set} → (Y → Z) → (X → Y) → (X → Z)
_∘_ g f = λ x → g (f x)