Last active
January 30, 2018 08:26
-
-
Save louisswarren/07868cccc56b47b377c51156915e9bf8 to your computer and use it in GitHub Desktop.
Multiple parameters as vectors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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