Skip to content
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 new module Effect.Functor.Naperian #2004

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
55 changes: 48 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,55 @@ New modules
Algebra.Module.Bundles.Raw
```

* Nagata's construction of the "idealization of a module":
```agda
Algebra.Module.Construct.Idealization
```

* The unique morphism from the initial, resp. terminal, algebra:
```agda
Algebra.Morphism.Construct.Initial
Algebra.Morphism.Construct.Terminal
```

* Pointwise and equality relations over indexed containers:
```agda
Data.Container.Indexed.Relation.Binary.Pointwise
Data.Container.Indexed.Relation.Binary.Pointwise.Properties
Data.Container.Indexed.Relation.Binary.Equality.Setoid
```

* Prime factorisation of natural numbers.
```
Data.Nat.Primality.Factorisation
```

* Consequences of 'infinite descent' for (accessible elements of) well-founded relations:
* Permutation relation for functional vectors:
```agda
Induction.InfiniteDescent
Data.Vec.Functional.Relation.Binary.Permutation
```
defining `_↭_ : IRel (Vector A) _`

* The unique morphism from the initial, resp. terminal, algebra:
* Properties of `Data.Vec.Functional.Relation.Binary.Permutation`:
```agda
Data.Vec.Functional.Relation.Binary.Permutation.Properties
```
defining
```agda
Algebra.Morphism.Construct.Initial
Algebra.Morphism.Construct.Terminal
↭-refl : Reflexive (Vector A) _↭_
↭-reflexive : xs ≡ ys → xs ↭ ys
↭-sym : Symmetric (Vector A) _↭_
↭-trans : Transitive (Vector A) _↭_
```

* Nagata's construction of the "idealization of a module":
* New module defining Naperian functors, 'logarithms of containers' (Hancock/McBride)
```
Effect.Functor.Naperian
```
defining
```agda
Algebra.Module.Construct.Idealization
record RawNaperian (RF : RawFunctor F) : Set _
record Naperian (RF : RawFunctor F) : Set _
```

* `Data.List.Relation.Binary.Sublist.Propositional.Slice`
Expand Down Expand Up @@ -134,6 +164,11 @@ New modules
_⇨_ = setoid
```

* Consequences of 'infinite descent' for (accessible elements of) well-founded relations:
```agda
Induction.InfiniteDescent
```

* Symmetric interior of a binary relation
```
Relation.Binary.Construct.Interior.Symmetric
Expand Down Expand Up @@ -217,6 +252,12 @@ Additions to existing modules
rawModule : RawModule _ c ℓ
```

* In `Algebra.Construct.Terminal`:
```agda
rawNearSemiring : RawNearSemiring c ℓ
nearSemiring : NearSemiring c ℓ
```

* In `Algebra.Module.Construct.Zero`:
```agda
rawLeftSemimodule : RawLeftSemimodule R c ℓ
Expand Down
78 changes: 78 additions & 0 deletions src/Effect/Functor/Naperian.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
------------------------------------------------------------------------
-- 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.
Comment on lines +6 to +9
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
-- 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.
-- Naperian Functors, as defined by Hancock and McBride, and
-- subsequently documented by Jeremy Gibbons in the paper
-- "APLicative Programming with Naperian Functors" (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 Function.Bundles using (_⟶ₛ_; _⟨$⟩_; Func)
open import Level using (Level; suc; _⊔_)
open import Relation.Binary.Bundles using (Setoid)
open import Relation.Binary.PropositionalEquality.Core using (_≡_)

private
variable
a b c e f : Level
A : Set a
Copy link
Contributor

Choose a reason for hiding this comment

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

If this choice causes ambiguity problems with opening Setoid etc. below, then suggest removing it in favour of ∀ {A} → ... in the types of index and tabulate below...


-- 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
record RawNaperian {F : Set a → Set b} c (RF : RawFunctor F) : Set (suc (a ⊔ c) ⊔ b) where
field
Log : Set c
index : F A → (Log → A)
tabulate : (Log → A) → F A

-- Full Naperian has the coherence conditions too.
-- Propositional version (hard to work with).

module Propositional where
record Naperian {F : Set a → Set b} c (RF : RawFunctor F) : Set (suc (a ⊔ c) ⊔ b) where
field
rawNaperian : RawNaperian c RF
open RawNaperian rawNaperian public
field
tabulate-index : (fa : F A) → tabulate (index fa) ≡ fa
index-tabulate : (f : Log → A) → ((l : Log) → index (tabulate f) l ≡ f l)
Comment on lines +41 to +51
Copy link
Contributor

Choose a reason for hiding this comment

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

Is Propositional.Naperian definable directly from Naperian (≡.setoid A) below?


module _ {F : Set a → Set b} c (RF : RawFunctor F) where
private
FA : (AS : Setoid a e) → (rn : RawNaperian c RF) → Setoid b (c ⊔ e)
FA AS rn = record
{ Carrier = F X
; _≈_ = λ fx fy → (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)
}
}
where
open Setoid AS renaming (Carrier to X)
Comment on lines +55 to +66
Copy link
Contributor

Choose a reason for hiding this comment

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

Conventionally, in the stdlib

  • S is the generic Setoid; AS seems weird here
  • open Setoid S renaming (Carrier to A), ditto.
  • the Carrier field is (usually) not mentioned; I might be tempted just to write
record
      { _≈_ = λ (fx fy : F A)  (l : Log)  index fx l ≈ index fy l
...

but YMMV

open RawNaperian rn

record Naperian (AS : Setoid a e) : Set (suc a ⊔ b ⊔ suc c ⊔ e) where
field
rawNaperian : RawNaperian c RF
open RawNaperian rawNaperian public
Comment on lines +69 to +72
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the one area of the design with which I strongly diverge from your choices: this definition makes the RawNaperian structure potentially depend(ent) on the argument index AS, and that for me is a deal-breaker...

... so I'd be interested to learn the rationale for this choice!

private
module FS = Setoid (FA AS rawNaperian)
module A = Setoid AS
Comment on lines +74 to +75
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar comments to the above about 'conventional' naming?

field
tabulate-index : (fx : F A.Carrier) → tabulate (index fx) FS.≈ fx
index-tabulate : (f : Log → A.Carrier) → ((l : Log) → index (tabulate f) l A.≈ f l)
Loading