Skip to content

Commit b9dc6ce

Browse files
committed
Add some bifunctors-style newtypes
1 parent b4cff5e commit b9dc6ce

File tree

6 files changed

+106
-0
lines changed

6 files changed

+106
-0
lines changed

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"package.json"
2323
],
2424
"dependencies": {
25+
"purescript-contravariant": "^3.0.0",
2526
"purescript-distributive": "^3.0.0",
2627
"purescript-either": "^3.0.0",
2728
"purescript-tuples": "^4.0.0"

src/Data/Profunctor/Clown.purs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Data.Profunctor.Clown where
2+
3+
import Prelude
4+
5+
import Data.Profunctor (class Profunctor)
6+
import Data.Newtype (class Newtype)
7+
import Data.Functor.Contravariant (class Contravariant, cmap)
8+
9+
-- | Makes a trivial `Profunctor` for a `Contravariant` functor.
10+
newtype Clown f a b = Clown (f a)
11+
12+
derive instance newtypeClown :: Newtype (Clown f a b) _
13+
derive newtype instance eqClown :: Eq (f a) => Eq (Clown f a b)
14+
derive newtype instance ordClown :: Ord (f a) => Ord (Clown f a b)
15+
16+
instance showClown :: Show (f a) => Show (Clown f a b) where
17+
show (Clown x) = "(Clown " <> show x <> ")"
18+
19+
instance functorClown :: Functor (Clown f a) where
20+
map _ (Clown a) = Clown a
21+
22+
instance profunctorClown :: Contravariant f => Profunctor (Clown f) where
23+
dimap f g (Clown a) = Clown (cmap f a)

src/Data/Profunctor/Cowrap.purs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Data.Profunctor.Cowrap where
2+
3+
import Prelude
4+
5+
import Data.Newtype (class Newtype)
6+
import Data.Functor.Contravariant (class Contravariant)
7+
import Data.Profunctor (class Profunctor, lmap)
8+
9+
-- | Provides a `Contravariant` over the first argument of a `Profunctor`.
10+
newtype Cowrap p b a = Cowrap (p a b)
11+
12+
derive instance newtypeCowrap :: Newtype (Cowrap p b a) _
13+
derive newtype instance eqCowrap :: Eq (p a b) => Eq (Cowrap p b a)
14+
derive newtype instance ordCowrap :: Ord (p a b) => Ord (Cowrap p b a)
15+
16+
instance showCowrap :: Show (p a b) => Show (Cowrap p b a) where
17+
show (Cowrap x) = "(Cowrap " <> show x <> ")"
18+
19+
instance contravariantCowrap :: Profunctor p => Contravariant (Cowrap p b) where
20+
cmap f (Cowrap a) = Cowrap (lmap f a)

src/Data/Profunctor/Join.purs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Data.Profunctor.Join where
2+
3+
import Prelude
4+
5+
import Data.Functor.Invariant (class Invariant)
6+
import Data.Newtype (class Newtype)
7+
import Data.Profunctor (class Profunctor, dimap)
8+
9+
-- | Turns a `Profunctor` into a `Invariant` functor by equating the two type
10+
-- | arguments.
11+
newtype Join p a = Join (p a a)
12+
13+
derive instance newtypeJoin :: Newtype (Join p a) _
14+
derive newtype instance eqJoin :: Eq (p a a) => Eq (Join p a)
15+
derive newtype instance ordJoin :: Ord (p a a) => Ord (Join p a)
16+
17+
instance showJoin :: Show (p a a) => Show (Join p a) where
18+
show (Join x) = "(Join " <> show x <> ")"
19+
20+
instance invariantJoin :: Profunctor p => Invariant (Join p) where
21+
imap f g (Join a) = Join (dimap g f a)

src/Data/Profunctor/Joker.purs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Data.Profunctor.Joker where
2+
3+
import Prelude
4+
5+
import Data.Profunctor (class Profunctor)
6+
import Data.Newtype (class Newtype)
7+
8+
-- | Makes a trivial `Profunctor` for a covariant `Functor`.
9+
newtype Joker f a b = Joker (f b)
10+
11+
derive instance newtypeJoker :: Newtype (Joker f a b) _
12+
derive newtype instance eqJoker :: Eq (f b) => Eq (Joker f a b)
13+
derive newtype instance ordJoker :: Ord (f b) => Ord (Joker f a b)
14+
15+
instance showJoker :: Show (f b) => Show (Joker f a b) where
16+
show (Joker x) = "(Joker " <> show x <> ")"
17+
18+
instance functorJoker :: Functor f => Functor (Joker f a) where
19+
map f (Joker a) = Joker (map f a)
20+
21+
instance profunctorJoker :: Functor f => Profunctor (Joker f) where
22+
dimap f g (Joker a) = Joker (map g a)

src/Data/Profunctor/Wrap.purs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Data.Profunctor.Wrap where
2+
3+
import Prelude
4+
5+
import Data.Newtype (class Newtype)
6+
import Data.Profunctor (class Profunctor, rmap)
7+
8+
-- | Provides a `Functor` over the second argument of a `Profunctor`.
9+
newtype Wrap p a b = Wrap (p a b)
10+
11+
derive instance newtypeWrap :: Newtype (Wrap p a b) _
12+
derive newtype instance eqWrap :: Eq (p a b) => Eq (Wrap p a b)
13+
derive newtype instance ordWrap :: Ord (p a b) => Ord (Wrap p a b)
14+
15+
instance showWrap :: Show (p a b) => Show (Wrap p a b) where
16+
show (Wrap x) = "(Wrap " <> show x <> ")"
17+
18+
instance functorWrap :: Profunctor p => Functor (Wrap p a) where
19+
map f (Wrap a) = Wrap (rmap f a)

0 commit comments

Comments
 (0)