-
Notifications
You must be signed in to change notification settings - Fork 237
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
Add Effect.Foldable
and Data.List.Effectful.Foldable
implementation
#2300
Draft
jamesmckinna
wants to merge
14
commits into
agda:master
Choose a base branch
from
jamesmckinna:foldable
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d2a7ee5
draft initial implementation
jamesmckinna a1b575b
added `List` instances
jamesmckinna 3559055
fix module name
jamesmckinna 5aec64c
non-dependent instance for `vec`
jamesmckinna a71af57
tweak
jamesmckinna a2b5679
added Nathan's morphism proofs
jamesmckinna d53e216
add module for `Vec`
jamesmckinna 009914a
tweak `import`s
jamesmckinna a8e9182
removed dependency
jamesmckinna 8e7b7be
removed dependency; more permissive `open`
jamesmckinna 815a7aa
simplify with a local definition of the hom
jamesmckinna 5856073
rename lemma; add a redundant lemma
jamesmckinna 68d1763
streamline `import` interface(s)
jamesmckinna df2a304
remove the word instance from any of the comments. Tighten imports.
JacquesCarette File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
------------------------------------------------------------------------ | ||
-- The Agda standard library | ||
-- | ||
-- The Foldable instance of List | ||
------------------------------------------------------------------------ | ||
|
||
{-# OPTIONS --cubical-compatible --safe #-} | ||
|
||
module Data.List.Effectful.Foldable where | ||
|
||
open import Algebra.Bundles using (Monoid) | ||
open import Algebra.Bundles.Raw using (RawMonoid) | ||
open import Algebra.Morphism.Structures using (IsMonoidHomomorphism) | ||
open import Data.List.Base as List using (List; []; _∷_; _++_) | ||
open import Effect.Foldable | ||
open import Function.Base | ||
open import Level | ||
import Relation.Binary.PropositionalEquality as ≡ | ||
|
||
|
||
private | ||
variable | ||
a c ℓ : Level | ||
A : Set a | ||
|
||
|
||
------------------------------------------------------------------------ | ||
-- Root implementation | ||
|
||
module _ (M : RawMonoid c ℓ) where | ||
|
||
open RawMonoid M | ||
|
||
foldMap : (A → Carrier) → List A → Carrier | ||
foldMap f [] = ε | ||
foldMap f (x ∷ xs) = f x ∙ foldMap f xs | ||
|
||
------------------------------------------------------------------------ | ||
-- Basic instance: using supplied defaults | ||
|
||
foldableWithDefaults : RawFoldableWithDefaults (List {a}) | ||
foldableWithDefaults = record { foldMap = λ M → foldMap M } | ||
|
||
------------------------------------------------------------------------ | ||
-- Specialised instance: using optimised implementations | ||
|
||
foldable : RawFoldable (List {a}) | ||
foldable = record | ||
{ foldMap = λ M → foldMap M | ||
; foldr = List.foldr | ||
; foldl = List.foldl | ||
; toList = id | ||
} | ||
|
||
------------------------------------------------------------------------ | ||
-- Properties | ||
|
||
module _ (M : Monoid c ℓ) (f : A → Monoid.Carrier M) where | ||
|
||
open Monoid M | ||
|
||
private | ||
h = foldMap rawMonoid f | ||
|
||
[]-homo : h [] ≈ ε | ||
[]-homo = refl | ||
|
||
++-homo : ∀ xs {ys} → h (xs ++ ys) ≈ h xs ∙ h ys | ||
++-homo [] = sym (identityˡ _) | ||
++-homo (x ∷ xs) = trans (∙-congˡ (++-homo xs)) (sym (assoc _ _ _)) | ||
|
||
foldMap-morphism : IsMonoidHomomorphism (List.++-[]-rawMonoid A) rawMonoid h | ||
foldMap-morphism = record | ||
{ isMagmaHomomorphism = record | ||
{ isRelHomomorphism = record | ||
{ cong = reflexive ∘ ≡.cong h } | ||
; homo = λ xs _ → ++-homo xs | ||
} | ||
; ε-homo = []-homo | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
------------------------------------------------------------------------ | ||
-- The Agda standard library | ||
-- | ||
-- The Foldable instance of Vec | ||
------------------------------------------------------------------------ | ||
|
||
{-# OPTIONS --cubical-compatible --safe #-} | ||
|
||
module Data.Vec.Effectful.Foldable where | ||
|
||
open import Algebra.Bundles.Raw using (RawMonoid) | ||
open import Data.Nat.Base | ||
open import Data.Vec.Base | ||
open import Data.Vec.Properties | ||
open import Effect.Foldable | ||
open import Function.Base | ||
open import Level | ||
open import Relation.Binary.PropositionalEquality as ≡ | ||
using (_≡_; _≢_; _≗_; refl; module ≡-Reasoning) | ||
|
||
private | ||
variable | ||
a c ℓ : Level | ||
A : Set a | ||
n : ℕ | ||
|
||
|
||
------------------------------------------------------------------------ | ||
-- Root implementation | ||
|
||
module _ (M : RawMonoid c ℓ) where | ||
|
||
open RawMonoid M | ||
|
||
foldMap : (A → Carrier) → Vec A n → Carrier | ||
foldMap f [] = ε | ||
foldMap f (x ∷ xs) = f x ∙ foldMap f xs | ||
|
||
------------------------------------------------------------------------ | ||
-- Basic instance: using supplied defaults | ||
|
||
foldableWithDefaults : RawFoldableWithDefaults {a} (λ A → Vec A n) | ||
foldableWithDefaults = record { foldMap = λ M → foldMap M } | ||
|
||
------------------------------------------------------------------------ | ||
-- Specialised instance: using optimised implementations | ||
|
||
foldable : RawFoldable {a} (λ A → Vec A n) | ||
foldable = record | ||
{ foldMap = λ M → foldMap M | ||
; foldr = foldr′ | ||
; foldl = foldl′ | ||
; toList = toList | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
------------------------------------------------------------------------ | ||
-- The Agda standard library | ||
-- | ||
-- Foldable functors | ||
------------------------------------------------------------------------ | ||
|
||
-- Note that currently the Foldable laws are not included here. | ||
|
||
{-# OPTIONS --cubical-compatible --safe #-} | ||
|
||
module Effect.Foldable where | ||
|
||
open import Algebra.Bundles.Raw using (RawMonoid) | ||
open import Algebra.Bundles using (Monoid) | ||
import Algebra.Construct.Flip.Op as Op | ||
|
||
open import Data.List.Base as List using (List; [_]; _++_) | ||
|
||
open import Effect.Functor as Fun using (RawFunctor) | ||
|
||
open import Function.Base using (id; flip) | ||
open import Function.Endomorphism.Propositional using (∘-id-monoid) | ||
open import Level using (Level; Setω) | ||
open import Relation.Binary.Bundles using (Setoid) | ||
|
||
private | ||
variable | ||
f g c ℓ : Level | ||
A : Set f | ||
C : Set c | ||
|
||
|
||
------------------------------------------------------------------------ | ||
-- The type of raw Foldables: | ||
-- all fields can be given non-default implementations | ||
|
||
record RawFoldable (F : Set f → Set g) : Setω where | ||
field | ||
foldMap : (M : RawMonoid c ℓ) (open RawMonoid M) → | ||
(A → Carrier) → F A → Carrier | ||
|
||
fold : (M : RawMonoid f ℓ) (open RawMonoid M) → F Carrier → Carrier | ||
fold M = foldMap M id | ||
|
||
field | ||
foldr : (A -> C -> C) -> C -> F A -> C | ||
foldl : (C -> A -> C) -> C -> F A -> C | ||
toList : F A → List A | ||
|
||
|
||
------------------------------------------------------------------------ | ||
-- The type of raw Foldables, with default implementations a la haskell | ||
|
||
record RawFoldableWithDefaults (F : Set f → Set g) : Setω where | ||
field | ||
foldMap : (M : RawMonoid c ℓ) (open RawMonoid M) → | ||
(A → Carrier) → F A → Carrier | ||
|
||
foldr : (A -> C -> C) -> C -> F A -> C | ||
foldr {C = C} f z t = foldMap M₀ f t z | ||
where M = ∘-id-monoid C ; M₀ = Monoid.rawMonoid M | ||
|
||
foldl : (C -> A -> C) -> C -> F A -> C | ||
foldl {C = C} f z t = foldMap M₀ (flip f) t z | ||
where M = Op.monoid (∘-id-monoid C) ; M₀ = Monoid.rawMonoid M | ||
|
||
toList : F A → List A | ||
toList {A = A} = foldMap (List.++-[]-rawMonoid A) [_] | ||
|
||
rawFoldable : RawFoldable F | ||
rawFoldable = record | ||
{ foldMap = foldMap | ||
; foldr = foldr | ||
; foldl = foldl | ||
; toList = toList | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would we want the version with defaults?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for
Vec
, or in general?Re: the latter, I suppose I should have had a
Foldable.Biased
module instead?