Skip to content

Instantly share code, notes, and snippets.

@louisswarren
Last active January 30, 2018 08:26
Show Gist options
  • Select an option

  • Save louisswarren/07868cccc56b47b377c51156915e9bf8 to your computer and use it in GitHub Desktop.

Select an option

Save louisswarren/07868cccc56b47b377c51156915e9bf8 to your computer and use it in GitHub Desktop.
Multiple parameters as vectors
open import Agda.Builtin.Bool
open import Agda.Builtin.Nat renaming (Nat to ℕ)
data False : Set where
record True : Set where
isTrue : Bool → Set
isTrue false = False
isTrue true = True
data Vec (A : Set) : ℕ → Set where
[] : Vec A zero
_∷_ : ∀{n} → A → Vec A n → Vec A (suc n)
infixr 5 _∷_
--------------------------------------------------------------------------------
-- Naive approach: access the elements
-- For fun
_^_⇒_ : Set → ℕ → Set → Set
A ^ n ⇒ B = (Vec A n) → B
_!_ : ∀{n} → {A : Set} → Vec A n → (k : ℕ) → {_ : isTrue (k < n)} → A
([] ! k) {()}
(x ∷ xs) ! zero = x
((x ∷ xs) ! suc k) {pf} = (xs ! k) {pf}
badder : ℕ ^ 2 ⇒ ℕ
badder pair = (pair ! 0) + (pair ! 1)
--------------------------------------------------------------------------------
-- Better: Agda knows that two elements are passed
-- More fun
ℕ² = Vec ℕ 2
adder : ℕ² → ℕ
adder (x ∷ y ∷ []) = x + y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment