gt; x1 <*> x2 <*> ... <*> xn` to be written in a different style ([View Highlight](https://read.readwise.io/read/01kgfdnybm52gtpgvrvjydm1wx)) ^984198395 ↩︎ ``` do v1 <- x1 v2 <- x2 ... vn <- xn pure (g v1 v2 ... vn) ``` ### Monad - However, many utility functions are found in [`Control.Monad`](https://hackage.haskell.org/package/base/docs/Control-Monad.html) ([View Highlight](https://read.readwise.io/read/01kgfm8hm2emkbx7axmw9qzv7p)) ^984252003 - `(>>)` should be the same as `(*>)` from `Applicative`. ([View Highlight](https://read.readwise.io/read/01kgfmm9qhg51kp3qdj25qk6je)) ^984254202 - the intention is that `m >> n` ignores the *result* of `m`, but not its *effects*. ([View Highlight](https://read.readwise.io/read/01kgfmrac16kjfhsjnn7yxpwe4)) ^984254330 - The only really interesting thing to look at—and what makes `Monad` strictly more powerful than `Applicative`—is `(>>=)`, which is often called *bind*. ([View Highlight](https://read.readwise.io/read/01kgfms0kn2n3f6v0568j80wmp)) ^984254357 - As mentioned earlier, `((->) e)` is known as the *reader monad*, since it describes computations in which a value of type `e` is available as a read-only environment. The [`Control.Monad.Reader`](http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-Reader.html) module provides the `Reader e a` type, which is just a convenient `newtype` wrapper around `(e -> a)`, along with an appropriate `Monad` instance and some `Reader`-specific utility functions such as `ask` (retrieve the environment), `asks` (retrieve a function of the environment), and `local` (run a subcomputation under a different environment). ([View Highlight](https://read.readwise.io/read/01kgfnr4g0j6epa6wer90phrcc)) ^984256621 - The [`Control.Monad.Writer`](http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-Writer-Lazy.html) module provides the `Writer` monad, which allows information to be collected as a computation progresses. `Writer w a` is isomorphic to `(a,w)`, where the output value `a` is carried along with an annotation or “log” of type `w`, which must be an instance of `Monoid` (see [section Monoid](https://wiki.haskell.org/Typeclassopedia/#Monoid)); the special function `tell` performs logging. ([View Highlight](https://read.readwise.io/read/01kgfnsavcb163w5j08yjg49sm)) ^984256654 - The [`Control.Monad.State`](http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State-Lazy.html) module provides the `State s a` type, a `newtype` wrapper around `s -> (a,s)`. Something of type `State s a` represents a stateful computation which produces an `a` but can access and modify the state of type `s` along the way. The module also provides `State`-specific utility functions such as `get` (read the current state), `gets` (read a function of the current state), `put` (overwrite the state), and `modify` (apply a function to the state). ([View Highlight](https://read.readwise.io/read/01kgfnwnxaxxhtz9nc36wqdwg2)) ^984256877 - The structure of an `Applicative` computation is fixed, whereas the structure of a `Monad` computation can change based on intermediate results. ([View Highlight](https://read.readwise.io/read/01kgs443nbkpv7jm8j71bex32c)) ^985446992 - The [`Control.Monad.Cont`](http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-Cont.html) module provides the `Cont` monad, which represents computations in continuation-passing style. It can be used to suspend and resume computations, and to implement non-local transfers of control, co-routines, other complex control structures—all in a functionally pure way. `Cont` has been called the [“mother of all monads”](http://blog.sigfpe.com/2008/12/mother-of-all-monads.html) because of its universal properties. ([View Highlight](https://read.readwise.io/read/01kgfnxhbayazfsykvs9ckrdv1)) ^984256903 - In fact, the canonical definition of monads in category theory is in terms of `return`, `fmap`, and `join` (often called η, T, and μ in the mathematical literature). Haskell uses an alternative formulation with `(>>=)` instead of `join` since it is more convenient to use ∗. ([View Highlight](https://read.readwise.io/read/01kgs4at009nhsam192gvqx64f)) ^985447229 - Ah, much better! The laws simply state that `return` is the identity of `(>=>)`, and that `(>=>)` is associative ∗. ([View Highlight](https://read.readwise.io/read/01kgsygc7fnyp8qkkeqvha5z7p)) ^985516853 A monoid - This can sometimes lead to efficiency gains, even for types which also have `Monad` instances, since in general `Applicative` computations may be run in parallel, whereas monadic ones may not. ([View Highlight](https://read.readwise.io/read/01kgsywvsxfftvgss80jnpqtds)) ^985518195 - When `-XQualifiedDo` is activated, the syntax `[modid.]do` becomes available, where `modid` stands for some module name. The `x <- u` statement now uses `(modid.>>=)`. ([View Highlight](https://read.readwise.io/read/01kgsyyvm9p0cvf24nrekt3ccm)) ^985518387 ### MonadFail - Some monads support a notion of *failure*, without necessarily supporting the notion of *recovery* suggested by `MonadPlus`, and possibly including a primitive error reporting mechanism. This notion is expressed by the relatively unprincipled `MonadFail`. ([View Highlight](https://read.readwise.io/read/01kgsztjbmxs9xhp9znqz56c2n)) ^985520406 ### Monad transformers - Unfortunately, monads do not compose as nicely as applicative functors (yet another reason to use `Applicative` if you don’t need the full power that `Monad` provides), but some monads can be combined in certain ways. ([View Highlight](https://read.readwise.io/read/01kgszw5h13cwx7esrvyvvmzg5)) ^985520454 - The [transformers](http://hackage.haskell.org/package/transformers) library provides a number of standard *monad transformers*. Each monad transformer adds a particular capability/feature/effect to any existing monad. ([View Highlight](https://read.readwise.io/read/01kgszwftavp4vdt2hwv4x64ck)) ^985520539 ### Monoid - `Endo a` is a newtype wrapper for functions `a -> a`, which form a monoid under composition. ([View Highlight](https://read.readwise.io/read/01kgt0w8n8zmmzxs28rpmrxxjr)) ^985521594 Another way to fold. First map each step to `acc -> acc`, mconcat them then apply it to the init value. ### Failure and choice: Alternative, MonadPlus, ArrowPlus - The basic intuition is that `empty` represents some sort of "failure", and `(<|>)` represents a choice between alternatives. ([View Highlight](https://read.readwise.io/read/01kgt31rt1n5dcfeqxw4e9tqma)) ^985529565 - The intuition is that both keep running `v`, collecting its results into a list, until it fails; `some v` requires `v` to succeed at least once, whereas `many v` does not require it to succeed at all. ([View Highlight](https://read.readwise.io/read/01kgt379yfghgw800b13e6fgme)) ^985529945 - `MonadPlus` ([haddock](https://hackage.haskell.org/package/base/docs/Control-Monad.html#t:MonadPlus)) is for `Monad`s with a monoid structure ([View Highlight](https://read.readwise.io/read/01kgt39q3xjfr55bdrtc7neq2z)) ^985530066 - `ArrowZero` and `ArrowPlus` ([haddock](https://hackage.haskell.org/package/base/docs/Control-Arrow.html#t:ArrowZero)) represent `Arrow`s ([see below](https://wiki.haskell.org/Typeclassopedia/#Arrow)) with a monoid structure ([View Highlight](https://read.readwise.io/read/01kgt39x7zhm121z6cs7v0hkwt)) ^985530079 - In the end, `some` and `many` really only make sense when used with some sort of "stateful" `Applicative` instance, for which an action `v`, when run multiple times, can succeed some finite number of times and then fail. ([View Highlight](https://read.readwise.io/read/01kgt3bg5p769t2hwe2f4237s8)) ^985530153 - `some` and `many` work particularly well with parser types having an `Applicative` instance: if `p` is a parser, then `some p` parses one or more consecutive occurrences of `p` (i.e. it will parse as many occurrences of `p` as possible and then stop), and `many p` parses zero or more occurrences. ([View Highlight](https://read.readwise.io/read/01kgt3e1r3cd11vwnhpzyrs228)) ^985530261 ### Foldable - This may look complicated, but in fact, to make a `Foldable` instance you only need to implement one method: your choice of `foldMap` or `foldr`. ([View Highlight](https://read.readwise.io/read/01kgt3j09c2yejh26mt2xty1ff)) ^985530393 ### Traversable - As you can see, every `Traversable` is also a `Foldable` `Functor`. To make a `Traversable` instance, it suffices to implement either `traverse` or `sequenceA`; the other methods all have default implementations in terms of these. ([View Highlight](https://read.readwise.io/read/01kgt3n596pfsp2sqsz7aw6whw)) ^985530541 Intuition: Apply computation on each contained value, creates the transformed tree on success or returns a failure. - Bifunctor ([View Highlight](https://read.readwise.io/read/01kgw81tvvry8rkkkb1van4yrt)) ^985890266 - This observation leads directly to the definition of `Bifunctor`, a class for types of kind `* -> * -> *` where one can functorially map over *both* type parameters. ([View Highlight](https://read.readwise.io/read/01kgw84m705btwhdphap5k868t)) ^985890557 - Category ([View Highlight](https://read.readwise.io/read/01kgw85mdca0e21rs1aw2qdnxt)) ^985890678 - we recover precisely the familiar identity function `id` and function composition operator `(.)` defined in the standard `Prelude`. ([View Highlight](https://read.readwise.io/read/01kgw8wfafq0mgc1m00frgf3h9)) ^985897316 - `Kleisli m a b`, as defined in the `Control.Arrow` module, is just a `newtype` wrapper around `a -> m b`. ([View Highlight](https://read.readwise.io/read/01kgw99ze20qfhb9fz02brvhms)) ^985898041 - The only laws that `Category` instances should satisfy are that `id` should be the identity of `(.)`, and `(.)` should be associative. ([View Highlight](https://read.readwise.io/read/01kgw8yp27q73gkk5nypb1ja9p)) ^985897452 - Finally, the `Category` module exports two additional operators: `(<<<)`, which is just a synonym for `(.)`, and `(>>>)`, which is `(.)` with its arguments reversed. ([View Highlight](https://read.readwise.io/read/01kgw8z2t5p70c4favjnqg3t9q)) ^985897481 - Arrow ([View Highlight](https://read.readwise.io/read/01kgw8zkcse604s26y7tf9wecz)) ^985897495 - However, unlike `Monad` and `Applicative`, whose types only reflect their output, the type of an `Arrow` computation reflects both its input and output. ([View Highlight](https://read.readwise.io/read/01kgw90mjmzpvn7j64vghchbwj)) ^985897529 - In the `(->)` instance of `Arrow` this is just a pure function; in general, however, an arrow may represent some sort of “effectful” computation. ([View Highlight](https://read.readwise.io/read/01kgw91sfs2sdzfd56fs5d13dm)) ^985897605 - The first thing to note is the `Category` class constraint, which means that we get identity arrows and arrow composition for free ([View Highlight](https://read.readwise.io/read/01kgw92qxcygm5qqkfvyf2jd71)) ^985897635 - As should be a familiar pattern by now, the only methods which must be defined when writing a new instance of `Arrow` are `arr` and `first` ([View Highlight](https://read.readwise.io/read/01kgw93j7m6s46esn34e5e3b0b)) ^985897658 - The `arr` function takes any function `b -> c` and turns it into a generalized arrow ``b `arr` c``. The `arr` method justifies the claim that arrows generalize functions, since it says that we can treat any function as an arrow. ([View Highlight](https://read.readwise.io/read/01kgw97dxee5bbgkxfgc08k3ca)) ^985897825 - The `(***)` operator is “parallel composition” of arrows ([View Highlight](https://read.readwise.io/read/01kgw9936kjrvhr05vxghsa6kg)) ^985897945 - The `(&&&)` operator is “fanout composition” of arrows ([View Highlight](https://read.readwise.io/read/01kgw999dgzt04vgmrg0nxz76g)) ^985897957 - The `Arrow` library itself only provides two `Arrow` instances, both of which we have already seen: `(->)`, the normal function constructor, and `Kleisli m`, which makes functions of type `a -> m b` into `Arrow`s for any `Monad m`. ([View Highlight](https://read.readwise.io/read/01kgw9aq4842e3ba00gvem0q9h)) ^985898227