You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 5, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: Manual/Monads.lean
+37-1Lines changed: 37 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -27,25 +27,43 @@ set_option pp.rawOnError true
27
27
set_option linter.unusedVariables false
28
28
set_option maxRecDepth 1024
29
29
30
+
/-
30
31
#doc (Manual) "Functors, Monads and `do`-Notation" =>
32
+
-/
33
+
#doc (Manual) "関手・モナド・do記法(Functors, Monads and `do`-Notation)" =>
31
34
32
35
%%%
33
36
tag := "monads-and-do"
34
37
%%%
35
38
39
+
:::comment
36
40
The type classes {name}`Functor`, {name}`Applicative`, and {name}`Monad` provide fundamental tools for functional programming.{margin}[An introduction to programming with these abstractions is available in [_Functional Programming in Lean_](https://lean-lang.org/functional_programming_in_lean/functor-applicative-monad.html).]
37
41
While they are inspired by the concepts of functors and monads in category theory, the versions used for programming are more limited.
38
42
The type classes in Lean's standard library represent the concepts as used for programming, rather than the general mathematical definition.
Instances of {deftech}[{name}`Functor`] allow an operation to be applied consistently throughout some polymorphic context.
41
50
Examples include transforming each element of a list by applying a function and creating new {lean}`IO` actions by arranging for a pure function to be applied to the result of an existing {lean}`IO` action.
42
51
Instances of {deftech}[{name}`Monad`] allow side effects with data dependencies to be encoded; examples include using a tuple to simulate mutable state, a sum type to simulate exceptions, and representing actual side effects with {lean}`IO`.
43
52
{deftech}[{name}`Applicative` functors] occupy a middle ground: like monads, they allow functions computed with effects to be applied to arguments that are computed with effects, but they do not allow sequential data dependencies where the output of an effect forms an input into another effectful operation.
The additional type classes {name}`Pure`, {name}`Bind`, {name}`SeqLeft`, {name}`SeqRight`, and {name}`Seq` capture individual operations from {name}`Applicative` and {name}`Monad`, allowing them to be overloaded and used with types that are not necessarily {name}`Applicative` functors or {name}`Monad`s.
46
60
The {name}`Alternative` type classdescribesapplicativefunctorsthatadditionallyhavesomenotionoffailureandrecovery.
The well-behaved {name}`Applicative`instanceappliesfunctionstoargumentselement-wise.
107
134
Because {name}`Applicative`extends {name}`Functor`, a separate {name}`Functor`instanceisnotnecessary,and {name Functor.map}`map` can be defined as part of the {name}`Applicative` instance.
0 commit comments