Skip to content

Commit b831cb3

Browse files
committed
Merge pull request #6 from purescript/docs
Update docs
2 parents 0bfa021 + 50f0f6a commit b831cb3

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,46 @@ class Profunctor p where
99
dimap :: forall a b c d. (a -> b) -> (c -> d) -> p b c -> p a d
1010
```
1111

12+
A `Profunctor` is a `Functor` from the pair category `(Type^op, Type)`
13+
to `Type`.
14+
15+
In other words, a `Profunctor` is a type constructor of two type
16+
arguments, which is contravariant in its first argument and covariant
17+
in its second argument.
18+
19+
The `dimap` function can be used to map functions over both arguments
20+
simultaneously.
21+
22+
A straightforward example of a profunctor is the function arrow `(->)`.
23+
24+
Laws:
25+
26+
- Identity: `dimap id id = id`
27+
- Composition: `dimap f1 g1 <<< dimap f2 g2 = dimap (f1 >>> f2) (g1 <<< g2)`
1228

1329
#### `lmap`
1430

1531
``` purescript
1632
lmap :: forall a b c p. (Profunctor p) => (a -> b) -> p b c -> p a c
1733
```
1834

35+
Map a function over the (contravariant) first type argument only.
1936

2037
#### `rmap`
2138

2239
``` purescript
2340
rmap :: forall a b c p. (Profunctor p) => (b -> c) -> p a b -> p a c
2441
```
2542

43+
Map a function over the (covariant) second type argument only.
2644

2745
#### `arr`
2846

2947
``` purescript
3048
arr :: forall a b p. (Category p, Profunctor p) => (a -> b) -> p a b
3149
```
3250

51+
Lift a pure function into any `Profunctor` which is also a `Category`.
3352

3453
#### `profunctorArr`
3554

@@ -49,6 +68,12 @@ class (Profunctor p) <= Choice p where
4968
right :: forall a b c. p b c -> p (Either a b) (Either a c)
5069
```
5170

71+
The `Choice` class extends `Profunctor` with combinators for working with
72+
sum types.
73+
74+
`left` and `right` lift values in a `Profunctor` to act on the `Left` and
75+
`Right` components of a sum, respectively.
76+
5277

5378
#### `choiceArr`
5479

@@ -63,13 +88,20 @@ instance choiceArr :: Choice Prim.Function
6388
(+++) :: forall p a b c d. (Category p, Choice p) => p a b -> p c d -> p (Either a c) (Either b d)
6489
```
6590

91+
Compose a value acting on a sum from two values, each acting on one of
92+
the components of the sum.
6693

6794
#### `(|||)`
6895

6996
``` purescript
7097
(|||) :: forall p a b c. (Category p, Choice p) => p a c -> p b c -> p (Either a b) c
7198
```
7299

100+
Compose a value which eliminates a sum from two values, each eliminating
101+
one side of the sum.
102+
103+
This combinator is useful when assembling values from smaller components,
104+
because it provides a way to support two different types of input.
73105

74106

75107
## Module Data.Profunctor.Strong
@@ -82,6 +114,12 @@ class (Profunctor p) <= Strong p where
82114
second :: forall a b c. p b c -> p (Tuple a b) (Tuple a c)
83115
```
84116

117+
The `Strong` class extends `Profunctor` with combinators for working with
118+
product types.
119+
120+
`first` and `first` lift values in a `Profunctor` to act on the first and
121+
second components of a `Tuple`, respectively.
122+
85123

86124
#### `strongArr`
87125

@@ -96,9 +134,17 @@ instance strongArr :: Strong Prim.Function
96134
(***) :: forall p a b c d. (Category p, Strong p) => p a b -> p c d -> p (Tuple a c) (Tuple b d)
97135
```
98136

137+
Compose a value acting on a `Tuple` from two values, each acting on one of
138+
the components of the `Tuple`.
99139

100140
#### `(&&&)`
101141

102142
``` purescript
103143
(&&&) :: forall p a b c. (Category p, Strong p) => p a b -> p a c -> p a (Tuple b c)
104-
```
144+
```
145+
146+
Compose a value which introduces a `Tuple` from two values, each introducing
147+
one side of the `Tuple`.
148+
149+
This combinator is useful when assembling values from smaller components,
150+
because it provides a way to support two different types of output.

src/Data/Profunctor.purs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
module Data.Profunctor where
22

3+
-- | A `Profunctor` is a `Functor` from the pair category `(Type^op, Type)`
4+
-- | to `Type`.
5+
-- |
6+
-- | In other words, a `Profunctor` is a type constructor of two type
7+
-- | arguments, which is contravariant in its first argument and covariant
8+
-- | in its second argument.
9+
-- |
10+
-- | The `dimap` function can be used to map functions over both arguments
11+
-- | simultaneously.
12+
-- |
13+
-- | A straightforward example of a profunctor is the function arrow `(->)`.
14+
-- |
15+
-- | Laws:
16+
-- |
17+
-- | - Identity: `dimap id id = id`
18+
-- | - Composition: `dimap f1 g1 <<< dimap f2 g2 = dimap (f1 >>> f2) (g1 <<< g2)`
319
class Profunctor p where
420
dimap :: forall a b c d. (a -> b) -> (c -> d) -> p b c -> p a d
521

22+
-- | Map a function over the (contravariant) first type argument only.
623
lmap :: forall a b c p. (Profunctor p) => (a -> b) -> p b c -> p a c
724
lmap a2b = dimap a2b id
825

26+
-- | Map a function over the (covariant) second type argument only.
927
rmap :: forall a b c p. (Profunctor p) => (b -> c) -> p a b -> p a c
1028
rmap b2c = dimap id b2c
1129

30+
-- | Lift a pure function into any `Profunctor` which is also a `Category`.
1231
arr :: forall a b p. (Category p, Profunctor p) => (a -> b) -> p a b
1332
arr f = rmap f id
1433

src/Data/Profunctor/Choice.purs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ module Data.Profunctor.Choice where
33
import Data.Either (Either(..), either)
44
import Data.Profunctor
55

6+
-- | The `Choice` class extends `Profunctor` with combinators for working with
7+
-- | sum types.
8+
-- |
9+
-- | `left` and `right` lift values in a `Profunctor` to act on the `Left` and
10+
-- | `Right` components of a sum, respectively.
11+
-- |
612
class (Profunctor p) <= Choice p where
713
left :: forall a b c. p a b -> p (Either a c) (Either b c)
814
right :: forall a b c. p b c -> p (Either a b) (Either a c)
@@ -16,9 +22,16 @@ module Data.Profunctor.Choice where
1622
infixr 2 +++
1723
infixr 2 |||
1824

25+
-- | Compose a value acting on a sum from two values, each acting on one of
26+
-- | the components of the sum.
1927
(+++) :: forall p a b c d. (Category p, Choice p) => p a b -> p c d -> p (Either a c) (Either b d)
2028
(+++) l r = left l >>> right r
2129

30+
-- | Compose a value which eliminates a sum from two values, each eliminating
31+
-- | one side of the sum.
32+
-- |
33+
-- | This combinator is useful when assembling values from smaller components,
34+
-- | because it provides a way to support two different types of input.
2235
(|||) :: forall p a b c. (Category p, Choice p) => p a c -> p b c -> p (Either a b) c
2336
(|||) l r = (l +++ r) >>> join
2437
where

src/Data/Profunctor/Strong.purs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ module Data.Profunctor.Strong where
33
import Data.Profunctor
44
import Data.Tuple (Tuple(..))
55

6+
-- | The `Strong` class extends `Profunctor` with combinators for working with
7+
-- | product types.
8+
-- |
9+
-- | `first` and `first` lift values in a `Profunctor` to act on the first and
10+
-- | second components of a `Tuple`, respectively.
11+
-- |
612
class (Profunctor p) <= Strong p where
713
first :: forall a b c. p a b -> p (Tuple a c) (Tuple b c)
814
second :: forall a b c. p b c -> p (Tuple a b) (Tuple a c)
@@ -14,9 +20,16 @@ module Data.Profunctor.Strong where
1420
infixr 3 ***
1521
infixr 3 &&&
1622

23+
-- | Compose a value acting on a `Tuple` from two values, each acting on one of
24+
-- | the components of the `Tuple`.
1725
(***) :: forall p a b c d. (Category p, Strong p) => p a b -> p c d -> p (Tuple a c) (Tuple b d)
1826
(***) l r = first l >>> second r
1927

28+
-- | Compose a value which introduces a `Tuple` from two values, each introducing
29+
-- | one side of the `Tuple`.
30+
-- |
31+
-- | This combinator is useful when assembling values from smaller components,
32+
-- | because it provides a way to support two different types of output.
2033
(&&&) :: forall p a b c. (Category p, Strong p) => p a b -> p a c -> p a (Tuple b c)
2134
(&&&) l r = split >>> (l *** r)
2235
where

0 commit comments

Comments
 (0)