Skip to content
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1a86ef4
Upload new file Effect.Functor.Naperian to stdlib
Sofia-Insa Jun 21, 2023
0dd5051
Update CHANGELOG.md
Sofia-Insa Jun 21, 2023
af6d2dc
Merge branch 'task2-Naperian' of https://github.com/Sofia-Insa/agda-s…
Sofia-Insa Jun 22, 2023
eba481c
Update CHANGELOG.md
Sofia-Insa Jun 22, 2023
603483f
updated `CHANGELOG`
jamesmckinna Mar 17, 2024
7dcb115
added note
jamesmckinna Mar 17, 2024
d76c8df
Merge branch 'master' into task2-Naperian
jamesmckinna Mar 17, 2024
73bd5bd
hopefully now fixed merge conflict with up-to-date `CHANGELOG`
jamesmckinna Mar 17, 2024
e233f0e
restored original details to `CHANGELOG`
jamesmckinna Mar 17, 2024
ad30c67
review comments from me
jamesmckinna Mar 17, 2024
775560f
Merge branch 'master' into task2-Naperian
JacquesCarette May 6, 2024
6804ecd
Setoid version of Naperian -- needs another pair of eyes.
JacquesCarette May 7, 2024
a1de89f
whitespace
JacquesCarette May 7, 2024
527e4d7
[FIX]: Naming + Propositional Naperian
gabriellisboaconegero Aug 21, 2025
3c478d3
[REFAC]: RawFunctor as part of RawNaperian + james revision
gabriellisboaconegero Aug 27, 2025
d697f2c
Merge branch 'master' into functor_naperian
gabriellisboaconegero Aug 27, 2025
0741736
[DOC]: Update CHANGELOG
gabriellisboaconegero Aug 27, 2025
5216bbb
[ADD]: Vec n is Naperian; rawNaperian to rawApplicative
gabriellisboaconegero Aug 29, 2025
ffda892
[NAMING]: Correct naming in Vec.Effectful and Naperian
gabriellisboaconegero Sep 2, 2025
6ad71f0
[NAMING]: ETA contract
gabriellisboaconegero Sep 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ New modules

* `Data.List.Relation.Binary.Permutation.Declarative{.Properties}` for the least congruence on `List` making `_++_` commutative, and its equivalence with the `Setoid` definition.

* New module defining Naperian functors, 'logarithms of containers' (Hancock/McBride)
```
Effect.Functor.Naperian
```
defining
```agda
record RawNaperian (F : Set a → Set b) (c : Level) : Set _
record Naperian (F : Set a → Set b) (c : Level) (S : Setoid a ℓ) : Set _
```
Additions to existing modules
-----------------------------

Expand Down
23 changes: 21 additions & 2 deletions src/Data/Vec/Effectful.agda
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ open import Data.Vec.Base as Vec hiding (_⊛_)
open import Data.Vec.Properties
open import Effect.Applicative as App using (RawApplicative)
open import Effect.Functor as Fun using (RawFunctor)
open import Effect.Functor.Naperian as Nap using (RawNaperian; PropositionalNaperian)
open import Effect.Monad using (RawMonad; module Join; RawMonadT; mkRawMonad)
import Function.Identity.Effectful as Id
open import Function.Base using (flip; _∘_)
open import Level using (Level)
open import Level using (Level; 0ℓ)
open import Relation.Binary.Bundles using (Setoid)
open import Relation.Binary.PropositionalEquality

private
variable
a : Level
a b : Level
A : Set a
n : ℕ

Expand All @@ -33,6 +36,22 @@ functor = record
{ _<$>_ = map
}

naperian : RawNaperian (λ (A : Set a) → Vec A n) 0ℓ
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would name this rawNaperian and the one below naperian.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly: rawApplicative below... etc.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the naming of naperian was easy. However, changing applicative to rawApplicative caused problems with the module TraversableM, as it attempts to open the rawApplicative inside the RawMonad, which can conflict with the naming.

The way I found was to explicitly extract the rawApplicative from RawMonad with open TraversableA (RawMonad.rawApplicative Mon) public

naperian {n = n} = record
{ rawFunctor = functor
; Log = Fin n
; index = lookup
; tabulate = tabulate
}

fullNaperian : PropositionalNaperian (λ (A : Set a) → Vec A n) 0ℓ
fullNaperian A = record
{ rawNaperian = naperian
; index-tabulate = λ f l → lookup∘tabulate f l
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eta contract?

; natural-tabulate = λ f k l → cong (λ fx → lookup fx l) (tabulate-∘ f k)
; natural-index = λ f as l → lookup-map l f as
}

applicative : RawApplicative (λ (A : Set a) → Vec A n)
applicative {n = n} = record
{ rawFunctor = functor
Expand Down
82 changes: 82 additions & 0 deletions src/Effect/Functor/Naperian.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
------------------------------------------------------------------------
-- The Agda standard library
--
-- Naperian functor
--
-- Definitions of Naperian Functors, as named by Hancock and McBride,
-- and subsequently documented by Jeremy Gibbons
-- in the article "APLicative Programming with Naperian Functors"
-- which appeared at ESOP 2017.
-- https://link.springer.com/chapter/10.1007/978-3-662-54434-1_21
------------------------------------------------------------------------

{-# OPTIONS --cubical-compatible --safe #-}

module Effect.Functor.Naperian where

open import Effect.Functor using (RawFunctor)
open import Effect.Applicative using (RawApplicative)
open import Level using (Level; suc; _⊔_)
open import Relation.Binary.Bundles using (Setoid)
open import Relation.Binary.PropositionalEquality.Properties as ≡ using (setoid)
open import Function.Base using (_∘_; const)

private
variable
a b c ℓ : Level
A : Set a

-- From the paper:
-- "Functor f is Naperian if there is a type p of ‘positions’ such that fa≃p→a;
-- then p behaves a little like a logarithm of f
-- in particular, if f and g are both Naperian,
-- then Log(f×g)≃Logf+Logg and Log(f.g) ≃ Log f × Log g"

-- RawNaperian contains just the functions, not the proofs
module _ (F : Set a → Set b) c where
record RawNaperian : Set (suc (a ⊔ c) ⊔ b) where
field
rawFunctor : RawFunctor F
Log : Set c
index : F A → (Log → A)
tabulate : (Log → A) → F A
open RawFunctor rawFunctor public

-- Full Naperian has the coherence conditions too.

record Naperian (S : Setoid a ℓ) : Set (suc (a ⊔ c) ⊔ b ⊔ ℓ) where
field
rawNaperian : RawNaperian
open RawNaperian rawNaperian public
open module S = Setoid S
private
FS : Setoid b (c ⊔ ℓ)
FS = record
{ _≈_ = λ (fx fy : F Carrier) → ∀ (l : Log) → index fx l ≈ index fy l
; isEquivalence = record
{ refl = λ _ → refl
; sym = λ eq l → sym (eq l)
; trans = λ i≈j j≈k l → trans (i≈j l) (j≈k l)
}
}
module FS = Setoid FS
field
index-tabulate : (f : Log → Carrier) → ((l : Log) → index (tabulate f) l ≈ f l)
natural-tabulate : (f : Carrier → Carrier) (k : Log → Carrier) → (tabulate (f ∘ k)) FS.≈ (f <$> (tabulate k))
natural-index : (f : Carrier → Carrier) (as : F Carrier) (l : Log) → (index (f <$> as) l) ≈ f (index as l)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!
Can natural-tabulate also be eliminated by defining it in terms of index-tabulate, natural-index, and tabulate-index?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it can without assuming congruence on the setoid. I was trying to do so, but I was stuck and needed to prove the function congruence on the setoid S. I could figure out that

natural-index f (tabulate k) l -> index (f <$> tabulate k) l ≈ f (index (tabulate k) l)
index-tabulate k l                    -> index (tabulate k) l ≈ k l
index-tabulate (f ∘ k) l            -> index (tabulate (λ x  f (k x))) l ≈ f (k l)

If I assume congruence, then I could prove tabulate-index.

I don't think we can find a proof for tabulate-index without congruence, but I am not sure. I base my argument on the fact that tabulate-∘ needs cong to be proved.


tabulate-index : (fx : F Carrier) → tabulate (index fx) FS.≈ fx
tabulate-index = index-tabulate ∘ index

PropositionalNaperian : Set (suc (a ⊔ c) ⊔ b)
PropositionalNaperian = ∀ A → Naperian (≡.setoid A)

Naperian-Applicative : RawNaperian → RawApplicative F
Naperian-Applicative rn =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Naperian-Applicative : RawNaperian RawApplicative F
Naperian-Applicative rn =
rawApplicative : RawNaperian RawApplicative F
rawApplicative rn =

on our 'usual' naming model?

record
{ rawFunctor = rawFunctor
; pure = tabulate ∘ const
; _<*>_ = λ a b → tabulate (λ i → (index a i) (index b i))
}
where
open RawNaperian rn