Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit f442a41

Browse files
cipher1024mergify[bot]
authored andcommitted
docs(category/monad,bitraversable): add module docstrings #1260 (#1286)
* docs(category/monad,bitraversable): add module docstrings * more docs * still more doc * doc about traversable
1 parent a489719 commit f442a41

File tree

5 files changed

+171
-9
lines changed

5 files changed

+171
-9
lines changed

src/category/bitraversable/basic.lean

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,39 @@
22
Copyright (c) 2018 Simon Hudon. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Author: Simon Hudon
5-
6-
Functors with two arguments
75
-/
86

97
import category.functor
108
category.bifunctor
119
category.traversable.basic
1210
tactic.basic
1311

12+
/-!
13+
# Bitraversable type class
14+
15+
Type class for traversing bifunctors. The concepts and laws are taken from
16+
https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Bitraversable.html
17+
18+
Simple examples of `bitraversable` are `prod` and `sum`. A more elaborate example is
19+
to define an a-list as:
20+
21+
```
22+
def alist (key val : Type) := list (key × val)
23+
```
24+
25+
Then we can use `f : key → io key'` and `g : val → io val'` to manipulate the `alist`'s key
26+
and value respectively with `bitraverse f g : alist key val → io (alist key' val')`
27+
28+
## Main definitions
29+
* bitraversable - exposes the `bitraverse` function
30+
* is_lawful_bitraversable - laws similar to is_lawful_traversable
31+
32+
## Tags
33+
34+
traversable bitraversable iterator functor bifunctor applicative
35+
36+
-/
37+
1438
universes u
1539

1640
class bitraversable (t : Type u → Type u → Type u)

src/category/bitraversable/instances.lean

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
1+
/-
2+
Copyright (c) 2019 Simon Hudon. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Author(s): Simon Hudon
5+
-/
6+
17
import category.bitraversable.basic
28
category.bitraversable.lemmas
39
category.traversable.lemmas
410
tactic.solve_by_elim
511

12+
/-!
13+
# bitraversable instances
14+
15+
## Instances
16+
17+
* prod
18+
* sum
19+
* const
20+
* flip
21+
* bicompl
22+
* bicompr
23+
24+
## References
25+
26+
* Hackage: https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Bitraversable.html
27+
28+
## Tags
29+
30+
traversable bitraversable functor bifunctor applicative
31+
32+
-/
33+
634
universes u v w
735

836
variables {t : Type u → Type u → Type u} [bitraversable t]

src/category/bitraversable/lemmas.lean

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
1+
/-
2+
Copyright (c) 2019 Simon Hudon. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Author(s): Simon Hudon
5+
-/
6+
17
import category.bitraversable.basic
28

9+
/-!
10+
# Bitraversable Lemmas
11+
12+
## Main definitions
13+
* tfst - traverse on first functor argument
14+
* tsnd - traverse on second functor argument
15+
16+
## Lemmas
17+
18+
Combination of
19+
* bitraverse
20+
* tfst
21+
* tsnd
22+
23+
with the applicatives `id` and `comp`
24+
25+
## References
26+
27+
* Hackage: https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Bitraversable.html
28+
29+
## Tags
30+
31+
traversable bitraversable functor bifunctor applicative
32+
33+
34+
-/
35+
336
universes u
437

538
variables {t : Type u → Type u → Type u} [bitraversable t]
@@ -10,12 +43,12 @@ open functor is_lawful_applicative
1043
variables {F G : Type u → Type u}
1144
[applicative F] [applicative G]
1245

13-
@[reducible]
14-
def tfst {α α'} (f : α → F α') : t α β → F (t α' β) :=
46+
/-- traverse on the first functor argument -/
47+
@[reducible] def tfst {α α'} (f : α → F α') : t α β → F (t α' β) :=
1548
bitraverse f pure
1649

17-
@[reducible]
18-
def tsnd {α α'} (f : α → F α') : t β α → F (t β α') :=
50+
/-- traverse on the second functor argument -/
51+
@[reducible] def tsnd {α α'} (f : α → F α') : t β α → F (t β α') :=
1952
bitraverse pure f
2053

2154
variables [is_lawful_bitraversable t]

src/category/monad/basic.lean

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,47 @@
1+
/-
2+
Copyright (c) 2019 Simon Hudon. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Author(s): Simon Hudon
5+
-/
16

27
import tactic.basic
38

9+
/-!
10+
# Monad
11+
12+
## Attributes
13+
14+
* extensionality
15+
* functor_norm
16+
* monad_norm
17+
18+
## Implementation Details
19+
20+
Set of rewrite rules and automation for monads in general and
21+
`reader_t`, `state_t`, `except_t` and `option_t` in particular.
22+
23+
The rewrite rules for monads are carefully chosen so that `simp with
24+
functor_norm` will not introduce monadic vocabulary in a context where
25+
applicatives would do just fine but will handle monadic notation
26+
already present in an expression.
27+
28+
In a context where monadic reasoning is desired `simp with monad_norm`
29+
will translate functor and applicative notation into monad notation
30+
and use regular `functor_norm` rules as well.
31+
32+
## Tags
33+
34+
functor, applicative, monad, simp
35+
36+
-/
37+
38+
run_cmd mk_simp_attr `monad_norm [`functor_norm]
39+
440
attribute [extensionality] reader_t.ext state_t.ext except_t.ext option_t.ext
541
attribute [functor_norm] bind_assoc pure_bind bind_pure
42+
attribute [monad_norm] seq_eq_bind_map
643
universes u v
744

45+
@[monad_norm]
846
lemma map_eq_bind_pure_comp (m : Type u → Type v) [monad m] [is_lawful_monad m] {α β : Type u} (f : α → β) (x : m α) :
947
f <$> x = x >>= pure ∘ f := by rw bind_pure_comp_eq_map

src/category/traversable/basic.lean

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,53 @@
22
Copyright (c) 2018 Simon Hudon. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Author: Simon Hudon
5-
6-
Type classes for traversing collections. The concepts and laws are taken from
7-
http://hackage.haskell.org/package/base-4.11.1.0/docs/Data-Traversable.html
85
-/
96

107
import tactic.cache
118
import category.applicative
129

10+
/-!
11+
# Traversable type class
12+
13+
Type classes for traversing collections. The concepts and laws are taken from
14+
http://hackage.haskell.org/package/base-4.11.1.0/docs/Data-Traversable.html
15+
16+
Traversable collections are a generalization of functors. Whereas
17+
functors (such as `list`) allow us to apply a function to every
18+
element, it does not allow functions which external effects encoded in
19+
a monad. Consider for instance a functor `invite : email -> io response`
20+
that takes an email address, sends an email and wait for a
21+
response. If we have a list `guests : list email`, using calling
22+
`invite` using `map` gives us the following: `map invite guests : list
23+
(io response)`. It is not what we need. We need something of type `io
24+
(list response)`. Instead of using `map`, we can use `traverse` to
25+
send all the invites: `traverse invite guests : io (list response)`.
26+
`traverse` applies `invite` to every element of `guests` and combines
27+
all the resulting effects. In the example, the effect is encoded in the
28+
monad `io` but any applicative functor is accepted by `traverse`.
29+
30+
For more on how to use traversable, consider the Haskell tutorial:
31+
https://en.wikibooks.org/wiki/Haskell/Traversable
32+
33+
## Main definitions
34+
* `traversable` type class - exposes the `traverse` function
35+
* `sequence` - based on `traverse`, turns a collection of effects into an effect returning a collection
36+
* is_lawful_traversable - laws
37+
38+
## Tags
39+
40+
traversable iterator functor applicative
41+
42+
## References
43+
44+
* "Applicative Programming with Effects", by Conor McBride and Ross Paterson, Journal of Functional Programming 18:1 (2008) 1-13, online at http://www.soi.city.ac.uk/~ross/papers/Applicative.html.
45+
* "The Essence of the Iterator Pattern", by Jeremy Gibbons and Bruno Oliveira, in Mathematically-Structured Functional Programming, 2006, online at http://web.comlab.ox.ac.uk/oucl/work/jeremy.gibbons/publications/#iterator.
46+
* "An Investigation of the Laws of Traversals", by Mauro Jaskelioff and Ondrej Rypacek, in Mathematically-Structured Functional Programming, 2012, online at http://arxiv.org/pdf/1202.2919.
47+
Synopsis
48+
49+
50+
-/
51+
1352
open function (hiding comp)
1453

1554
universes u v w

0 commit comments

Comments
 (0)