Prefer using Prelude.id over Identity

This commit is contained in:
Kiana Sheibani 2023-03-30 14:08:24 -04:00
parent 5b55d39ab5
commit 3c87261627
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
5 changed files with 53 additions and 29 deletions

View file

@ -1,6 +1,5 @@
module Data.Profunctor.Mapping module Data.Profunctor.Mapping
import Control.Monad.Identity
import Data.Morphisms import Data.Morphisms
import Data.Tensor import Data.Tensor
import Data.Profunctor import Data.Profunctor
@ -23,7 +22,7 @@ functor = MkFunctor (\f => (. (. f)))
||| * `map' . lmap f = lmap (map f) . map'` ||| * `map' . lmap f = lmap (map f) . map'`
||| * `map' . rmap f = rmap (map f) . map'` ||| * `map' . rmap f = rmap (map f) . map'`
||| * `map' . map' = map' @{Compose}` ||| * `map' . map' = map' @{Compose}`
||| * `dimap Identity runIdentity . map' = id` ||| * `dimap Id runIdentity . map' = id`
public export public export
interface (Traversing p, Closed p) => Mapping p where interface (Traversing p, Closed p) => Mapping p where
map' : Functor f => p a b -> p (f a) (f b) map' : Functor f => p a b -> p (f a) (f b)
@ -80,7 +79,7 @@ ProfunctorFunctor CofreeMapping where
public export public export
ProfunctorComonad CofreeMapping where ProfunctorComonad CofreeMapping where
proextract (MkCFM p) = dimap Id runIdentity p proextract (MkCFM p) = p @{FunctorId}
produplicate (MkCFM p) = MkCFM $ MkCFM $ p @{Compose} produplicate (MkCFM p) = MkCFM $ MkCFM $ p @{Compose}
public export public export
@ -99,7 +98,7 @@ roamCofree f (MkCFM p) = MkCFM $ dimap (map (flip f)) (map ($ id)) $
public export public export
Profunctor p => Traversing (CofreeMapping p) where Profunctor p => Traversing (CofreeMapping p) where
traverse' (MkCFM p) = MkCFM (p @{Compose}) traverse' (MkCFM p) = MkCFM (p @{Compose})
wander f = roamCofree $ f @{MkApplicative @{MkFunctor id} id id} wander f = roamCofree $ f @{ApplicativeId}
public export public export
Profunctor p => Mapping (CofreeMapping p) where Profunctor p => Mapping (CofreeMapping p) where
@ -128,7 +127,7 @@ ProfunctorFunctor FreeMapping where
public export public export
ProfunctorMonad FreeMapping where ProfunctorMonad FreeMapping where
propure p = MkFM runIdentity p Id propure p = MkFM @{FunctorId} id p id
projoin (MkFM l' (MkFM l m r) r') = MkFM @{Compose} (l' . map l) m (map r . r') projoin (MkFM l' (MkFM l m r) r') = MkFM @{Compose} (l' . map l) m (map r . r')
public export public export
@ -155,7 +154,7 @@ roamFree f (MkFM l m r) = MkFM @{Compose @{functor}} (($ id) . map @{functor} l)
public export public export
Traversing (FreeMapping p) where Traversing (FreeMapping p) where
traverse' (MkFM l m r) = MkFM @{Compose} (map l) m (map r) traverse' (MkFM l m r) = MkFM @{Compose} (map l) m (map r)
wander f = roamFree $ f @{MkApplicative @{MkFunctor id} id id} wander f = roamFree $ f @{ApplicativeId}
public export public export
Mapping (FreeMapping p) where Mapping (FreeMapping p) where

View file

@ -50,9 +50,9 @@ Representable Morphism Identity where
||| A named implementation of `Representable` for function types. ||| A named implementation of `Representable` for function types.
||| Use this to avoid having to use a type wrapper like `Morphism`. ||| Use this to avoid having to use a type wrapper like `Morphism`.
export export
[Function] Representable (\a,b => a -> b) Identity [Function] Representable (\a,b => a -> b) Prelude.id
using Sieve.Function Strong.Function where using Sieve.Function Strong.Function where
tabulate = (runIdentity .) tabulate = id
export export
Functor f => Representable (Kleislimorphism f) f where Functor f => Representable (Kleislimorphism f) f where
@ -74,8 +74,8 @@ namespace Corepresentable
||| A named implementation of `Corepresentable` for function types. ||| A named implementation of `Corepresentable` for function types.
||| Use this to avoid having to use a type wrapper like `Morphism`. ||| Use this to avoid having to use a type wrapper like `Morphism`.
export export
[Function] Corepresentable (\a,b => a -> b) Identity using Cosieve.Function where [Function] Corepresentable (\a,b => a -> b) Prelude.id using Cosieve.Function where
cotabulate = (. Id) cotabulate = id
export export
Functor f => Corepresentable (Costar f) f where Functor f => Corepresentable (Costar f) f where

View file

@ -37,8 +37,8 @@ Sieve Morphism Identity where
||| A named implementation of `Sieve` for function types. ||| A named implementation of `Sieve` for function types.
||| Use this to avoid having to use a type wrapper like `Morphism`. ||| Use this to avoid having to use a type wrapper like `Morphism`.
public export public export
[Function] Sieve (\a,b => a -> b) Identity using Profunctor.Function where [Function] Sieve (\a,b => a -> b) Prelude.id using Profunctor.Function FunctorId where
sieve = (Id .) sieve = id
public export public export
Functor f => Sieve (Kleislimorphism f) f where Functor f => Sieve (Kleislimorphism f) f where
@ -61,8 +61,8 @@ namespace Cosieve
||| A named implementation of `Cosieve` for function types. ||| A named implementation of `Cosieve` for function types.
||| Use this to avoid having to use a type wrapper like `Morphism`. ||| Use this to avoid having to use a type wrapper like `Morphism`.
public export public export
[Function] Cosieve (\a,b => a -> b) Identity using Profunctor.Function where [Function] Cosieve (\a,b => a -> b) Prelude.id using Profunctor.Function FunctorId where
cosieve = (. runIdentity) cosieve = id
public export public export
Functor f => Cosieve (Costar f) f where Functor f => Cosieve (Costar f) f where

View file

@ -1,7 +1,6 @@
module Data.Profunctor.Traversing module Data.Profunctor.Traversing
import Control.Applicative.Const import Control.Applicative.Const
import Control.Monad.Identity
import Data.Morphisms import Data.Morphisms
import Data.Tensor import Data.Tensor
import Data.Profunctor import Data.Profunctor
@ -22,14 +21,6 @@ import Data.Profunctor
[TraversablePair] Traversable (Pair c) using FoldablePair where [TraversablePair] Traversable (Pair c) using FoldablePair where
traverse f (l, r) = (l,) <$> f r traverse f (l, r) = (l,) <$> f r
[FoldableIdentity] Foldable Identity where
foldr f i (Id x) = f x i
foldl f i (Id x) = f i x
null _ = False
[TraversableIdentity] Traversable Identity using FoldableIdentity where
traverse f (Id x) = map Id (f x)
record Bazaar a b t where record Bazaar a b t where
constructor MkBazaar constructor MkBazaar
@ -54,7 +45,7 @@ Functor (Baz t b) where
sold : Baz t a a -> t sold : Baz t a a -> t
sold m = runIdentity (runBaz m Id) sold m = runBaz @{ApplicativeId} m id
Foldable (Baz t b) where Foldable (Baz t b) where
foldr f i bz = runBaz bz @{appEndo} f i foldr f i bz = runBaz bz @{appEndo} f i
@ -103,14 +94,14 @@ interface (Strong p, Choice p) => Traversing p where
public export public export
Traversing Morphism where Traversing Morphism where
traverse' (Mor f) = Mor (map f) traverse' (Mor f) = Mor (map f)
wander f (Mor p) = Mor (runIdentity . (f $ Id . p)) wander f (Mor p) = Mor (f @{ApplicativeId} p)
||| A named implementation of `Traversing` for function types. ||| A named implementation of `Traversing` for function types.
||| Use this to avoid having to use a type wrapper like `Morphism`. ||| Use this to avoid having to use a type wrapper like `Morphism`.
public export public export
[Function] Traversing (\a,b => a -> b) using Strong.Function where [Function] Traversing (\a,b => a -> b) using Strong.Function where
traverse' = map traverse' = map
wander f g = runIdentity . (f $ Id . g) wander f = f @{ApplicativeId}
public export public export
Applicative f => Traversing (Kleislimorphism f) where Applicative f => Traversing (Kleislimorphism f) where
@ -166,7 +157,7 @@ ProfunctorFunctor CofreeTraversing where
public export public export
ProfunctorComonad CofreeTraversing where ProfunctorComonad CofreeTraversing where
proextract (MkCFT p) = dimap Id runIdentity $ p @{TraversableIdentity} proextract (MkCFT p) = p @{TraversableId}
produplicate (MkCFT p) = MkCFT $ MkCFT $ p @{Compose} produplicate (MkCFT p) = MkCFT $ MkCFT $ p @{Compose}
public export public export
@ -220,7 +211,7 @@ ProfunctorFunctor FreeTraversing where
public export public export
ProfunctorMonad FreeTraversing where ProfunctorMonad FreeTraversing where
propure p = MkFT @{TraversableIdentity} runIdentity p Id propure p = MkFT @{TraversableId} id p id
projoin (MkFT l' (MkFT l m r) r') = MkFT @{Compose} (l' . map l) m (map r . r') projoin (MkFT l' (MkFT l m r) r') = MkFT @{Compose} (l' . map l) m (map r . r')
@ -230,4 +221,4 @@ freeTraversing fn (MkFT {f} l m r) = dimap r l (traverse' {f} (fn m))
public export public export
unfreeTraversing : FreeTraversing p :-> q -> p :-> q unfreeTraversing : FreeTraversing p :-> q -> p :-> q
unfreeTraversing f p = f (MkFT @{TraversableIdentity} runIdentity p Id) unfreeTraversing f p = f (MkFT @{TraversableId} id p id)

View file

@ -276,3 +276,37 @@ Profunctor (Coforget r) where
dimap _ f (MkCoforget k) = MkCoforget (f . k) dimap _ f (MkCoforget k) = MkCoforget (f . k)
lmap _ = recoforget lmap _ = recoforget
rmap = map rmap = map
------------------------------------------------------------------------------
-- Identity functor
------------------------------------------------------------------------------
-- A few convenience definitions for use in other modules.
public export
[FunctorId] Functor Prelude.id where
map = id
public export
[ApplicativeId] Applicative Prelude.id using FunctorId where
pure = id
(<*>) = id
public export
[MonadId] Monad Prelude.id using ApplicativeId where
join = id
(>>=) = flip id
public export
[FoldableId] Foldable Prelude.id where
foldr = flip
foldl = id
null _ = False
foldlM = id
toList = pure
foldMap = id
public export
[TraversableId] Traversable Prelude.id using FunctorId FoldableId where
traverse = id