| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Vec is Foldable |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Vec.Effectful.Foldable where |
| 10 |
|
| 11 |
open import Algebra.Bundles.Raw using (RawMonoid) |
| 12 |
open import Data.Nat.Base using (ℕ) |
| 13 |
open import Data.Vec.Base using (Vec; []; _∷_; foldr′; foldl′; toList) |
| 14 |
open import Effect.Foldable using (RawFoldableWithDefaults; RawFoldable) |
| 15 |
open import Function.Base using (id) |
| 16 |
open import Level using (Level) |
| 17 |
|
| 18 |
private |
| 19 |
variable |
| 20 |
a c ℓ : Level |
| 21 |
A : Set a |
| 22 |
n : ℕ |
| 23 |
|
| 24 |
------------------------------------------------------------------------ |
| 25 |
-- Root implementation |
| 26 |
|
| 27 |
module _ (M : RawMonoid c ℓ) where |
| 28 |
|
| 29 |
open RawMonoid M |
| 30 |
|
| 31 |
foldMap : (A → Carrier) → Vec A n → Carrier |
| 32 |
foldMap f [] = ε |
| 33 |
foldMap f (x ∷ xs) = f x ∙ foldMap f xs |
| 34 |
|
| 35 |
------------------------------------------------------------------------ |
| 36 |
-- Basic implementation using supplied defaults |
| 37 |
|
| 38 |
foldableWithDefaults : RawFoldableWithDefaults {a} (λ A → Vec A n) |
| 39 |
foldableWithDefaults = record { foldMap = λ M → foldMap M } |
| 40 |
|
| 41 |
------------------------------------------------------------------------ |
| 42 |
-- Specialised version using optimised implementations |
| 43 |
|
| 44 |
foldable : RawFoldable {a} (λ A → Vec A n) |
| 45 |
foldable = record |
| 46 |
{ foldMap = λ M → foldMap M |
| 47 |
; foldr = foldr′ |
| 48 |
; foldl = foldl′ |
| 49 |
; toList = toList |
| 50 |
} |
| 51 |
|