-
Notifications
You must be signed in to change notification settings - Fork 260
Port reverse lemmas to Data.Vec (fixes #942)
#1668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 16 commits
045c907
469f59b
f3335f3
c5ddfb7
6b68630
85b190f
073ebb0
65e7a32
034c787
3ff93b6
6f01fde
7181c79
c85745f
48e5eb5
c7975e7
971703e
2c1bfd7
6632ae5
7e551bd
b9b537d
06ada23
469bf1f
8cc0f1b
cab8782
599ab48
fff90e8
3898aba
8519888
6ab612d
c2734b7
796a412
1ab0e0a
40a2260
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ module Data.Vec.Base where | |
|
|
||
| open import Data.Bool.Base | ||
| open import Data.Nat.Base | ||
| open import Data.Nat.Properties | ||
| open import Data.Fin.Base using (Fin; zero; suc) | ||
| open import Data.List.Base as List using (List) | ||
| open import Data.Product as Prod using (∃; ∃₂; _×_; _,_) | ||
|
|
@@ -183,24 +184,29 @@ module DiagonalBind where | |
| ------------------------------------------------------------------------ | ||
| -- Operations for reducing vectors | ||
|
|
||
| foldr : ∀ {a b} {A : Set a} (B : ℕ → Set b) {m} → | ||
| (∀ {n} → A → B n → B (suc n)) → | ||
| module _ {a b} {A : Set a} (B : ℕ → Set b) where | ||
|
|
||
| Opʳ = ∀ {n} → A → B n → B (suc n) | ||
| Opˡ = ∀ {n} → B n → A → B (suc n) | ||
|
|
||
| foldr : ∀ {m} → | ||
| Opʳ → | ||
| B zero → | ||
| Vec A m → B m | ||
| foldr _⊕_ n [] = n | ||
| foldr _⊕_ n (x ∷ xs) = x ⊕ foldr _⊕_ n xs | ||
|
|
||
| foldl : ∀ {b} (B : ℕ → Set b) {m} → | ||
| Opˡ {A = A} B → | ||
| B zero → | ||
| Vec A m → B m | ||
| foldr b _⊕_ n [] = n | ||
| foldr b _⊕_ n (x ∷ xs) = x ⊕ foldr b _⊕_ n xs | ||
| foldl B _⊕_ n [] = n | ||
| foldl B _⊕_ n (x ∷ xs) = foldl (B ∘ suc) _⊕_ (n ⊕ x) xs | ||
|
|
||
| foldr₁ : ∀ {n} → (A → A → A) → Vec A (suc n) → A | ||
| foldr₁ _⊕_ (x ∷ []) = x | ||
| foldr₁ _⊕_ (x ∷ y ∷ ys) = x ⊕ foldr₁ _⊕_ (y ∷ ys) | ||
|
|
||
| foldl : ∀ {a b} {A : Set a} (B : ℕ → Set b) {m} → | ||
| (∀ {n} → B n → A → B (suc n)) → | ||
| B zero → | ||
| Vec A m → B m | ||
| foldl b _⊕_ n [] = n | ||
| foldl b _⊕_ n (x ∷ xs) = foldl (λ n → b (suc n)) _⊕_ (n ⊕ x) xs | ||
|
|
||
| foldl₁ : ∀ {n} → (A → A → A) → Vec A (suc n) → A | ||
| foldl₁ _⊕_ (x ∷ xs) = foldl _ _⊕_ x xs | ||
|
|
||
|
|
@@ -280,14 +286,30 @@ fromList (List._∷_ x xs) = x ∷ fromList xs | |
| ------------------------------------------------------------------------ | ||
| -- Operations for reversing vectors | ||
|
|
||
| reverse : ∀ {n} → Vec A n → Vec A n | ||
| reverse {A = A} = foldl (Vec A) (λ rev x → x ∷ rev) [] | ||
| -- snoc | ||
|
|
||
| infixl 5 _∷ʳ_ | ||
|
|
||
| _∷ʳ_ : ∀ {n} → Vec A n → A → Vec A (1 + n) | ||
| _∷ʳ_ : ∀ {n} → Vec A n → A → Vec A (suc n) | ||
| [] ∷ʳ y = [ y ] | ||
| (x ∷ xs) ∷ʳ y = x ∷ (xs ∷ʳ y) | ||
| {- better ? | ||
| _∷ʳ_ {A = A} xs y = foldr ((Vec A) ∘ suc) (λ x r → x ∷ r) [ y ] xs | ||
|
||
| -} | ||
|
|
||
| -- vanilla reverse | ||
|
|
||
| reverse : ∀ {n} → Vec A n → Vec A n | ||
| reverse {A = A} = foldl (Vec A) (λ rev x → x ∷ rev) [] | ||
|
|
||
| -- reverse-append | ||
|
|
||
| infix 5 _ʳ++_ | ||
|
|
||
| _ʳ++_ : ∀ {m n} → Vec A m → Vec A n → Vec A (m + n) | ||
| _ʳ++_ {A = A} {n = n} xs ys = foldl ((Vec A) ∘ (_+ n)) (λ rev x → x ∷ rev) ys xs | ||
|
|
||
| -- init and last | ||
|
|
||
| initLast : ∀ {n} (xs : Vec A (1 + n)) → | ||
| ∃₂ λ (ys : Vec A n) (y : A) → xs ≡ ys ∷ʳ y | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.