Skip to content

Commit 3939c48

Browse files
authored
Merge pull request #22 from purescript/newtypes
Add some bifunctors-style newtypes
2 parents b4cff5e + 0f7bb5b commit 3939c48

File tree

7 files changed

+153
-0
lines changed

7 files changed

+153
-0
lines changed

bower.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
"package.json"
2323
],
2424
"dependencies": {
25+
"purescript-contravariant": "^3.0.0",
2526
"purescript-distributive": "^3.0.0",
2627
"purescript-either": "^3.0.0",
28+
"purescript-exists": "^3.0.0",
2729
"purescript-tuples": "^4.0.0"
2830
}
2931
}

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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
import Data.Monoid (class Monoid)
9+
10+
-- | Turns a `Profunctor` into a `Invariant` functor by equating the two type
11+
-- | arguments.
12+
newtype Join p a = Join (p a a)
13+
14+
derive instance newtypeJoin :: Newtype (Join p a) _
15+
derive newtype instance eqJoin :: Eq (p a a) => Eq (Join p a)
16+
derive newtype instance ordJoin :: Ord (p a a) => Ord (Join p a)
17+
18+
instance showJoin :: Show (p a a) => Show (Join p a) where
19+
show (Join x) = "(Join " <> show x <> ")"
20+
21+
instance semigroupJoin :: Semigroupoid p => Semigroup (Join p a) where
22+
append (Join a) (Join b) = Join (a <<< b)
23+
24+
instance monoidJoin :: Category p => Monoid (Join p a) where
25+
mempty = Join id
26+
27+
instance invariantJoin :: Profunctor p => Invariant (Join p) where
28+
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/Split.purs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Data.Profunctor.Split
2+
( Split
3+
, split
4+
, unSplit
5+
, liftSplit
6+
, lowerSplit
7+
, hoistSplit
8+
) where
9+
10+
import Prelude
11+
12+
import Data.Exists (Exists, mkExists, runExists)
13+
import Data.Functor.Invariant (class Invariant, imap)
14+
import Data.Profunctor (class Profunctor)
15+
16+
newtype Split f a b = Split (Exists (SplitF f a b))
17+
18+
data SplitF f a b x = SplitF (a -> x) (x -> b) (f x)
19+
20+
instance functorSplit :: Functor (Split f a) where
21+
map f = unSplit \g h fx -> split g (f <<< h) fx
22+
23+
instance profunctorSplit :: Profunctor (Split f) where
24+
dimap f g = unSplit \h i -> split (h <<< f) (g <<< i)
25+
26+
split :: forall f a b x. (a -> x) -> (x -> b) -> f x -> Split f a b
27+
split f g fx = Split (mkExists (SplitF f g fx))
28+
29+
unSplit :: forall f a b r. (forall x. (a -> x) -> (x -> b) -> f x -> r) -> Split f a b -> r
30+
unSplit f (Split e) = runExists (\(SplitF g h fx) -> f g h fx) e
31+
32+
liftSplit :: forall f a. f a -> Split f a a
33+
liftSplit = split id id
34+
35+
lowerSplit :: forall f a. Invariant f => Split f a a -> f a
36+
lowerSplit = unSplit (flip imap)
37+
38+
hoistSplit :: forall f g a b. (f ~> g) -> Split f a b -> Split g a b
39+
hoistSplit nat = unSplit (\f g -> split f g <<< nat)

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)