Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
127 commits
Select commit Hold shift + click to select a range
9c7e81f
fix for latest idris, helper
Jul 27, 2018
6cf5098
Traversal/Index/At
Jul 30, 2018
fe3a5cd
generalize Index/At for Maps/Sets
Jul 30, 2018
75ae610
fix Choice (Kleislimorphism m)
clayrat Jul 3, 2019
c0063ce
Arr -> Morphism
clayrat Jul 5, 2019
49666c3
flip arguments of ^.
clayrat Jul 5, 2019
b5311dd
add elba manifest
clayrat Jul 5, 2019
5dde409
add operators
clayrat Jul 7, 2019
d965012
expand index/at
clayrat Jul 7, 2019
11761e5
syntax
clayrat Jul 7, 2019
e71c599
update
clayrat Jul 7, 2019
674c79d
refactor, add grates
clayrat Jul 22, 2019
9844509
instances
clayrat Jul 22, 2019
c186ce6
getter
clayrat Jul 22, 2019
c7f08c9
Update export statement syntax
pinselimo May 23, 2022
46381d0
Quantify Profunctor free type variable
pinselimo May 23, 2022
a8f3729
Use map function instead of liftA
pinselimo May 23, 2022
5754348
Remove profunctor type variable from determining parameters
pinselimo May 23, 2022
9e16608
Provide type declarations for Forgotten record
pinselimo May 23, 2022
642a215
Rewrite WrappedArrow rmap for type disambiguation
pinselimo May 23, 2022
a551ab8
Import mirror from Data.Either
pinselimo May 24, 2022
8e4f5dd
Quantify UnsafeProfunctor free type variable
pinselimo May 24, 2022
a1ec348
Substitute liftA2 with map and ap
pinselimo May 24, 2022
e7d97dd
Quantify Closed type variable
pinselimo May 24, 2022
a19a4a5
Rename type variables in Environize to prevent shadowing of Closed.Cl…
pinselimo May 24, 2022
3f83e6a
Quantify Procompose implicit type variables
pinselimo May 24, 2022
fbb7d99
Rename type variables in Rifted to prevent shadowing of Closed.Closure.x
pinselimo May 24, 2022
3bf2f8b
Quantify Comonad's type variable
pinselimo May 24, 2022
ad09bd8
Rename & operator to .&
pinselimo May 24, 2022
9abde46
Import mirror from Data.Either
pinselimo May 24, 2022
0fce330
Remove explicit implicit type from function implementation
pinselimo May 24, 2022
3facaa4
Fix Profunctor type restriction in Iso and Iso'
pinselimo May 24, 2022
f9dfeaf
Explicitly apply Force and Delay
pinselimo May 24, 2022
f5a58ec
Comment out enum and denum relying on Enum in base
pinselimo May 24, 2022
d739448
Remove top level Profunctor resrictions
pinselimo May 24, 2022
b3b3f08
Remove redundant implicit typing
pinselimo May 25, 2022
735789d
Quantify Lensing's type variable
pinselimo May 25, 2022
931747d
Fix Lensing type restriction in Iso and Iso'
pinselimo May 25, 2022
272a1e7
Remove top level Lensing resrictions
pinselimo May 25, 2022
964c95c
Rename HVect type variable to avoid clash
pinselimo May 25, 2022
bf1760c
Fix typing of view
pinselimo May 25, 2022
b8ec8a9
Fix type check of over
pinselimo May 25, 2022
60b6972
Prepent Prism accessor functions with leading p
pinselimo May 25, 2022
5336e0a
Add isNothing to imports
pinselimo May 25, 2022
3fbf65d
Add lambda for Delay call
pinselimo May 25, 2022
cfde277
Wrap laziness keywords in lambdas
pinselimo May 25, 2022
2de86db
Substitute liftA2 with map and ap
pinselimo May 25, 2022
8e236ae
ipkg: Fix sourcedir value has type string
pinselimo May 30, 2022
e530e1e
Unify snooze and ring functions
pinselimo May 30, 2022
b7ab047
Fix Prisming type restriction in Prism and Prism'
pinselimo May 30, 2022
ddac1e9
Explicitly type helper function of review
pinselimo May 30, 2022
e164161
Fix Prisming type restriciton in prism functions
pinselimo May 30, 2022
8c17a8b
Declare type of a and b
pinselimo May 30, 2022
a02673b
Add mirror to imports
pinselimo May 31, 2022
b47b571
Quantify Choice's type variable
pinselimo May 31, 2022
cee02bd
Quantify Strong's type variable
pinselimo May 31, 2022
171e64f
Quantify Wander's type variable and f in wander
pinselimo May 31, 2022
bc536dd
Remove %implementation qualifiers
pinselimo May 31, 2022
e689deb
Remove implicit Applicative instance
pinselimo May 31, 2022
4e5c609
Fix module name
pinselimo May 31, 2022
1e2eddc
Remove import of Data.Bitraversable
pinselimo May 31, 2022
9505fd6
Fix Wander type restriction in Traversal and Traversal'
pinselimo May 31, 2022
954b40b
Remove Wander type restriction in traversed
pinselimo May 31, 2022
8bdb7c9
Remove explicit implicit typing in traversed
pinselimo May 31, 2022
bc936c2
Remove trailing whitespace
pinselimo May 31, 2022
657dc20
Quantify Index' type variables
pinselimo May 31, 2022
8bdb293
Remove magical f1 implicits
pinselimo May 31, 2022
540f9dd
Remove Wander type restrictions
pinselimo May 31, 2022
d645eea
Import Iso
pinselimo May 31, 2022
38f41f2
Quantify At's type variables
pinselimo May 31, 2022
aa46010
Remove Lensing type restrictions
pinselimo May 31, 2022
a430033
Add type declarations for functions in where clause
pinselimo May 31, 2022
c778ffe
Import Iso
pinselimo May 31, 2022
6743f61
Remove bifunctors dependeny
pinselimo May 31, 2022
18f441e
Erase x in closed
pinselimo May 31, 2022
aaee4e0
Fix Closed type restrictions
pinselimo May 31, 2022
452337d
Remove import of inexistent Monad module
pinselimo May 31, 2022
c64c494
Finish implementation of Ran record
pinselimo May 31, 2022
652bf5a
Fix curryRan implementation
pinselimo Jun 27, 2022
3bf7ba3
Move docstrings of constructors to record docs
pinselimo Jun 27, 2022
54c0431
Fix Choice implementation of Klaislimorphism
pinselimo Jun 27, 2022
adb2bce
Quantify Prisming's type variable
pinselimo Jun 27, 2022
7cf6dc6
Prepend lens accessors with an 'l'
pinselimo Jun 29, 2022
98300fa
Add implementation for Applicative liftA2 function
pinselimo Jun 29, 2022
8e83ed5
Fix scanR function
pinselimo Jun 29, 2022
7e785af
Add type annotations for step functions
pinselimo Jun 29, 2022
bbc78e2
Remove redundant Ord type restriction
pinselimo Jun 29, 2022
567bd88
Add laziness type annotations and fix lambdas
pinselimo Jun 29, 2022
e394f4e
Use liftA2 function
pinselimo Jun 29, 2022
c3d889f
Add holes for missing proofs
pinselimo Jun 29, 2022
d4c884f
Update readme to idris2
pinselimo Jun 29, 2022
61d2273
Use Const provided by base
pinselimo Jun 30, 2022
4b061e2
Add Sieve module and interface
pinselimo Jul 1, 2022
6a75b62
Add Proxy record and implementations
pinselimo Jul 1, 2022
549ac43
Add Cosieve record and implementations
pinselimo Jul 1, 2022
32f09cd
Add Strong, Choice and Closed instance of Procomposition
pinselimo Jul 1, 2022
f0fd9ea
Add Representable interface and implementations
pinselimo Jul 1, 2022
d6f3ba7
Remove legacy implementation of Const
pinselimo Jul 1, 2022
9655d18
Update package description with new modules
pinselimo Jul 1, 2022
ae4c4eb
Add useful functions for representables
pinselimo Jul 4, 2022
59c02b8
Update package module listing
pinselimo Jul 4, 2022
15cd852
Update package description to use comonad package
pinselimo Jul 7, 2022
894b48b
Remove redundant comonad definitions
pinselimo Jul 7, 2022
b17a59d
Move Tagged comonad implementation to profunctor module
pinselimo Jul 7, 2022
29c4741
Add Monad module
pinselimo Jul 7, 2022
5be1f5c
Add Fold interface and implementations
pinselimo Oct 31, 2022
8cafa99
Add foldable interface with verified toList instance
pinselimo Oct 31, 2022
ddae687
Add extensionality axiom for folds
pinselimo Oct 31, 2022
f47ef2b
Use named functions for Applicative instance
pinselimo Oct 31, 2022
e7c8717
Show distribution of run functions in semigroups
pinselimo Oct 31, 2022
ef9c28d
Show associativity of fold semigroup instances
pinselimo Oct 31, 2022
d3f5a24
Show neutrality of neutral fold element
pinselimo Oct 31, 2022
29c0d45
Show folds form a group
pinselimo Oct 31, 2022
2f8e611
Show fold group ops commute
pinselimo Oct 31, 2022
d792986
Show distribution of run functions in rings
pinselimo Oct 31, 2022
d4a3860
Show associativity and distributivity of fold ringOps
pinselimo Oct 31, 2022
b921142
Show fold ring unity is identity
pinselimo Oct 31, 2022
e6925ff
Add FoldableV instances for prelude foldables
pinselimo Oct 31, 2022
c5960d4
Add other trivial FoldableV instances
pinselimo Oct 31, 2022
2307812
Verify toList neutrality for vectors
pinselimo Oct 31, 2022
64104c5
Verify toList neutrality for sorted sets
pinselimo Oct 31, 2022
3ca5a45
Verify toList neutrality for LazyLists and SnocLists
pinselimo Oct 31, 2022
d57107b
Verify toList neutrality for sorted maps
pinselimo Oct 31, 2022
b1bd25b
Verify toList neutrality for alternating lists
pinselimo Oct 31, 2022
35758b9
Verify toList neutrality for MaybeT
pinselimo Nov 2, 2022
b370401
Verify toList neutrality for EitherT
pinselimo Nov 2, 2022
82dfe41
Merge pull request #2 from pinselimo/add-fold-proofs
pinselimo Nov 3, 2022
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.ibc
*.o
target/
elba.lock
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Idris Profunctors
=================
Idris2 Profunctors
==================

This is a profunctor library for idris based off the excellent [Haskell Profunctors](https://github.com/ekmett/profunctors) package from Edward Kmett. Contributions, bug reports, and feature requests are welcome.
This is a profunctor library for Idris2 based off the excellent [Haskell Profunctors](https://github.com/ekmett/profunctors) package from Edward Kmett. Contributions, bug reports, and feature requests are welcome.

Contains
--------
Expand All @@ -16,9 +16,14 @@ Contains

* Prisms

Misses
------

* Proofs for algebraic properties of Folds.

Installation
------------

Run `idris --install profunctors.ipkg` from inside this directory, and then if
Run `idris2 --install profunctors.ipkg` from inside this directory, and then if
you want to use it with anything, invoke idris with `-p profunctors` (i.e.
`idris -p profunctors hack_the_planet.idr`)
`idris2 -p profunctors hack_the_planet.idr`)
27 changes: 27 additions & 0 deletions elba.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "japesinator/Idris-Profunctors"
version = "0.1.0"
authors = []

[dependencies]
"japesinator/Idris-Bifunctors" = { git = "https://github.com/andrevidela/Idris-Bifunctors" }

[targets.lib]
path = "src"
name = "profunctors"
mods = [ "Data.Profunctor"
, "Data.Profunctor.Cayley"
, "Data.Profunctor.Closed"
, "Data.Profunctor.Codensity"
, "Data.Profunctor.Composition"
, "Data.Profunctor.Fold"
, "Data.Profunctor.Iso"
, "Data.Profunctor.Lens"
, "Data.Profunctor.Lens.At"
, "Data.Profunctor.Prism"
, "Data.Profunctor.Trace"
, "Data.Profunctor.Traversal"
, "Data.Profunctor.Traversal.Index"
, "Data.Verified.Profunctor"
]
idris_opts = ["-p", "contrib"]
20 changes: 18 additions & 2 deletions profunctors.ipkg
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
package profunctors

sourcedir = src
sourcedir = "src"

modules = Data.Profunctor
, Data.Profunctor.Cayley
, Data.Profunctor.Choice
, Data.Profunctor.Closed
, Data.Profunctor.Codensity
, Data.Profunctor.Composition
, Data.Profunctor.Comonad
, Data.Profunctor.Fold
, Data.Profunctor.Grate
, Data.Profunctor.Iso
, Data.Profunctor.Lens
, Data.Profunctor.Lens.At
, Data.Profunctor.Monad
, Data.Profunctor.Prism
, Data.Profunctor.Ran
, Data.Profunctor.Rep
, Data.Profunctor.Sieve
, Data.Profunctor.Strong
, Data.Profunctor.Trace
, Data.Profunctor.Traversal
, Data.Profunctor.Traversal.Index
, Data.Profunctor.Unsafe
, Data.Profunctor.Wander
, Data.Proxy
, Data.Verified.Profunctor

opts = "-p contrib"
depends = comonad

opts = "-p contrib comonad"
174 changes: 53 additions & 121 deletions src/Data/Profunctor.idr
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
module Data.Profunctor

import Control.Monad.Identity
import Control.Arrow
import Control.Category
import Control.Comonad
import Data.Either
import Data.Morphisms

%access public export
%default total

||| Profunctors
||| @p The action of the Profunctor on pairs of objects
interface Profunctor (p : Type -> Type -> Type) where
public export
interface Profunctor (0 p : Type -> Type -> Type) where
||| Map over both arguments
|||
||| ````idris example
Expand Down Expand Up @@ -36,48 +40,34 @@ interface Profunctor (p : Type -> Type -> Type) where
rmap : (a -> b) -> p c a -> p c b
rmap = dimap id

export
implementation Monad m => Profunctor (Kleislimorphism m) where
dimap f g (Kleisli h) = Kleisli $ liftA g . h . f
dimap f g (Kleisli h) = Kleisli $ map g . h . f

||| An injective (->)
|||
||| ````idris example
||| believe_me : Arr a b
||| ````
|||
record Arr a b where
constructor MkArr
runArr : (a -> b)

implementation Category Arr where
id = assert_total id
(.) = assert_total (.)

implementation Arrow Arr where
arrow = MkArr
first = MkArr . (\f,(a,b) => (f a,b)) . runArr
second = MkArr . (\f,(a,b) => (a,f b)) . runArr
(MkArr f) *** (MkArr g) = MkArr $ \(a,b) => (f a, g b)
(MkArr f) &&& (MkArr g) = MkArr $ \a => (f a, g a)

implementation Profunctor Arr where
dimap f g (MkArr h) = MkArr $ g . h . f

implementation Functor (Arr a) where
map = rmap
export
implementation Profunctor Morphism where
dimap f g (Mor h) = Mor $ g . h . f

||| A method of attaching a phantom type as a "tag"
public export
record Tagged a b where
constructor Tag
runTagged : b

export
implementation Profunctor Tagged where
lmap = const $ Tag . runTagged
rmap f = Tag . f . runTagged

export
implementation Functor (Tagged a) where
map = rmap

export
implementation Comonad (Tagged a) where
duplicate = Tag
extract = runTagged

-- UpStar
-- {{{

Expand All @@ -87,20 +77,30 @@ implementation Functor (Tagged a) where
||| UpStar $ \x => Just $ isDigit x
||| ````
|||
public export
record UpStarred (f : Type -> Type) d c where
constructor UpStar
runUpStar : d -> f c

export
implementation Functor f => Profunctor (UpStarred f) where
dimap ab cd (UpStar bfc) = UpStar $ map cd . bfc . ab
dimap ab cd (UpStar bfc) = UpStar $ \a => map cd $ bfc $ ab a

export
implementation Functor f => Functor (UpStarred f a) where
map = rmap

export
implementation Applicative f => Applicative (UpStarred f a) where
pure = UpStar . const . pure
(UpStar ff) <*> (UpStar fx) = UpStar $ \a => ff a <*> fx a

export
implementation Alternative f => Alternative (UpStarred f a) where
empty = UpStar $ const empty
(UpStar fa) <|> (UpStar fb) = UpStar $ \x => (fa x) <|> (fb x)

export
implementation Monad f => Monad (UpStarred f a) where
(UpStar m) >>= f = UpStar $ \e => m e >>= flip runUpStar e . f

Expand All @@ -114,20 +114,25 @@ implementation Monad f => Monad (UpStarred f a) where
||| DownStar $ show
||| ````
|||
public export
record DownStarred (f : Type -> Type) d c where
constructor DownStar
runDownStar : f d -> c

export
implementation Functor f => Profunctor (DownStarred f) where
dimap ab cd (DownStar fbc) = DownStar $ cd . fbc . map ab

export
implementation Functor (DownStarred f a) where
map = (DownStar .) . (. runDownStar) . (.)

export
implementation Applicative (DownStarred f a) where
pure = DownStar . const
(DownStar ff) <*> (DownStar fx) = DownStar $ \a => ff a $ fx a

export
implementation Monad (DownStarred f a) where
(DownStar m) >>= f = DownStar $ \x => runDownStar (f $ m x) x

Expand All @@ -141,24 +146,28 @@ implementation Monad (DownStarred f a) where
||| WrapArrow $ arrow ((+) 1)
||| ````
|||
public export
record WrappedArrow (p : Type -> Type -> Type) a b where
constructor WrapArrow
unwrapArrow : p a b

export
implementation Category p => Category (WrappedArrow p) where
(WrapArrow f) . (WrapArrow g) = WrapArrow $ f . g
id = WrapArrow id

export
implementation Arrow p => Arrow (WrappedArrow p) where
arrow = WrapArrow . arrow
first = WrapArrow . first . unwrapArrow
second = WrapArrow . second . unwrapArrow
(WrapArrow a) *** (WrapArrow b) = WrapArrow $ a *** b
(WrapArrow a) &&& (WrapArrow b) = WrapArrow $ a &&& b

export
implementation Arrow p => Profunctor (WrappedArrow p) where
lmap = (>>>) . arrow
rmap = (.) . arrow
rmap f = (arrow f .)

-- }}}
-- Forget
Expand All @@ -170,110 +179,33 @@ implementation Arrow p => Profunctor (WrappedArrow p) where
||| Forget ((+) 1)
||| ````
|||
record Forgotten r a b where
public export
record Forgotten (r : Type) (a : Type) (b : Type) where
constructor Forget
runForget : a -> r

export
implementation Profunctor (Forgotten r) where
dimap f _ (Forget k) = Forget $ k . f

export
implementation Functor (Forgotten r a) where
map = const $ Forget . runForget

export
implementation Foldable (Forgotten r a) where
foldr = const const

export
implementation Traversable (Forgotten r a) where
traverse = const $ pure . Forget . runForget

-- }}}
-- Strong
-- {{{

||| Generalized UpStar of a Strong Functor
interface Profunctor p => Strong (p : Type -> Type -> Type) where
||| Create a new Profunctor of tuples with first element from the original
|||
||| ````idris example
||| first' (Kleisli $ \x => Just $ reverse x)
||| ````
|||
first' : p a b -> p (a, c) (b, c)
first' = dimap (\x => (snd x, fst x)) (\x => (snd x, fst x)) . second'

||| Create a new Profunctor of tuples with second element from the original
|||
||| ````idris example
||| second' (Kleisli $ \x => Just $ reverse x)
||| ````
|||
second' : p a b -> p (c, a) (c, b)
second' = dimap (\x => (snd x, fst x)) (\x => (snd x, fst x)) . first'

implementation Monad m => Strong (Kleislimorphism m) where
first' (Kleisli f) = Kleisli $ \ac => f (fst ac) >>= \b => pure (b, snd ac)
second' (Kleisli f) = Kleisli $ \ca => f (snd ca) >>= pure . MkPair (fst ca)

implementation Strong Arr where
first' (MkArr f) = MkArr $ \(a,c) => (f a, c)
second' (MkArr f) = MkArr $ \(c,a) => (c, f a)

implementation Functor m => Strong (UpStarred m) where
first' (UpStar f) = UpStar $ \ac => map (\b' => (b', snd ac)) . f $ fst ac
second' (UpStar f) = UpStar $ \ca => map (MkPair $ fst ca) . f $ snd ca

implementation Arrow p => Strong (WrappedArrow p) where
first' = WrapArrow . first . unwrapArrow
second' = WrapArrow . second . unwrapArrow

implementation Strong (Forgotten r) where
first' (Forget k) = Forget $ k . fst
second' (Forget k) = Forget $ k . snd

-- }}}
-- Choice
-- {{{

||| Generalized DownStar of a Costrong Functor
interface Profunctor p => Choice (p : Type -> Type -> Type) where
||| Like first' but with sum rather than product types
|||
||| ````idris example
||| left' (Kleisli $ \x => Just $ reverse x)
||| ````
|||
left' : p a b -> p (Either a c) (Either b c)
left' = dimap mirror mirror . right'

||| Like second' but with sum rather than product types
|||
||| ````idris example
||| right' (Kleisli $ \x => Just $ reverse x)
||| ````
|||
right' : p a b -> p (Either c a) (Either c b)
right' = dimap mirror mirror . left'

implementation Monad m => Choice (Kleislimorphism m) where
left' f = Kleisli $ either (applyKleisli $ f >>> arrow Left)
(applyKleisli $ arrow id >>> arrow Right)
right' f = Kleisli $ either (applyKleisli $ arrow id >>> arrow Left)
(applyKleisli $ f >>> arrow Right)

implementation Choice Arr where
left' (MkArr f) = MkArr $ either (Left . f) Right
right' (MkArr f) = MkArr $ either Left (Right . f)

implementation Choice Tagged where
left' = Tag . Left . runTagged
right' = Tag . Right . runTagged

implementation Applicative f => Choice (UpStarred f) where
left' (UpStar f) = UpStar $ either (map Left . f ) (map Right . pure)
right' (UpStar f) = UpStar $ either (map Left . pure) (map Right . f )

implementation Monoid r => Choice (Forgotten r) where
left' (Forget k) = Forget . either k $ const neutral
right' (Forget k) = Forget . flip either k $ const neutral
public export
record Zipping a b where
constructor MkZipping
runZipping : a -> a -> b

export
implementation Profunctor Zipping where
dimap f g (MkZipping h) = MkZipping $ \a1, a2 => g $ h (f a1) (f a2)
-- }}}
Loading