-
-
Save anonymous/10040e221a6fd01f7821 to your computer and use it in GitHub Desktop.
vectors.agda
This file contains 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
module vectors where | |
-- For every inductive recursor: | |
-- d is the base case | |
-- e is the inductive step | |
-- | |
-- Natural numbers | |
-- | |
data ℕ : Set where | |
zero : ℕ | |
succ : ℕ → ℕ | |
natrec : {P : Set} → ℕ → P → (ℕ → P → P) → P | |
natrec zero d e = d | |
natrec (succ n) d e = e n (natrec n d e) | |
-- | |
-- Lists | |
-- | |
data List (A : Set) : Set where | |
listnil : List A | |
listcons : A → List A → List A | |
listrec : {A P : Set} → List A → P → (A → List A → P → P) → P | |
listrec listnil d e = d | |
listrec (listcons a l) d e = e a l (listrec l d e) | |
listmap : {A B : Set} -> (A -> B) -> List A -> List B | |
listmap f l = listrec l listnil (λ x y z → listcons (f x) z) | |
-- | |
-- Vectors | |
-- | |
data Vect (A : Set) : ℕ → Set where | |
vectnil : Vect A zero | |
vectcons : {n : ℕ} → A → Vect A n → Vect A (succ n) | |
vectrec : {A P : Set} → {n : ℕ} → Vect A n → P → ({m : ℕ} → A → Vect A m → P → P) → P | |
vectrec vectnil d e = d | |
vectrec (vectcons a v) d e = e a v (vectrec v d e) | |
forgetlength : {A : Set} → {n : ℕ} → Vect A n → List A | |
forgetlength v = vectrec v listnil (λ x y z → listcons x z) | |
vectmap : {A B : Set} → {n : ℕ} → (A → B) → Vect A n → Vect B n | |
vectmap f (vectcons a v) = vectrec v vectnil (λ x y z → vectcons (f x) z) | |
Saizan
commented
Nov 26, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment