Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. - `Writer w a` supports monadic logging
  2. - `w` is the monoid type parameter that is `mappend`ed to with every bind
  3. - `a` is the type parameter that contains the result
  4. - "Writer of w with result a"
  5. - In `Control.Monad.Writer`
  6. - If you want to log diagnostic messages, `[String]` can be the left type
  7. - Difference lists
  8. - Are *functions* that prepend some list to their arguments.
  9. - Example of difference list: `(xs++)` (is implementation of `toDiffList`)
  10. - Identity difference list: `([]++)` or `id`
  11. - `mappend` is simply represented as function composition. `f >>= g = f . g`, where both sides are diff lists.
  12. - Exist to allow for *efficient concatenation*
  13. - If you used `Writer [String]` to log a bunch of messages sequentially, you would be doing something like:
  14. - `(((a ++ b) ++ c) ++ d) ++ e`
  15. - Which would be inefficient because with `++` the lhs has to be rewalked, resulting in quadratic behavior. (*inside-out*, left-associative evaluation)
  16. - With `DiffList String` you get something like `a . b . c . d . e`.
  17. - When you convert it to a regular list via `fromDiffList xs` or `xs []`, the list held by `e` is prepended first, then the list by `d`, then `c`, etc.
  18. - This results in *outside-in*, right-associative evaluation: `(++) a' $ (++) b' $ (++) c' $ (++) d' $ (++) e' []`
  19. - Which means linear behavior
  20. - Functions are monads
  21. - `h >>= f = \w -> f (h w) w`
  22. - Very useful to think of a function as a box containing some value, with the context that is needs to be applied to get at that value.
  23. - Their monad instance is in `Control.Monad.Instances`
  24. - Referred to as "reader monad"?
  25. - State monad
  26. - A *stateful computation* (such as `random`) is a function that takes some state, and contains a pair containing a value and some new state.
  27. - `statefulComputation :: s -> (a, s)`
  28. - Stateful computations can be thought of as values with context, too.
  29. - The actual value is the result
  30. - The context is we have to provide some initial state to get that result, and also get a final state back along with the result.
  31. - The `State` monad is a `newtype` wrapper around `s -> (a, s)`.
  32. - Bind implementation can be thought of as: given some initial state, invoke the stateful computation and get an `(a, s)`, get the rest of the code to execute via invoking the rhs with the result `a`, and then invoke that code with the new state `s`.
  33. - Can also view `f` (the rhs) as a continuation? Do the stateful computation and invoke `f` with `a` and `s`?
  34. - `State s a`, `s` is the state and `a` is the result
  35. - To be clear, the `State` monad is a wrapper over *code* to be executed. `s` is the state itself.
  36. - `get`: takes the current state and presents it as the result. `get = State $ \s -> (s, s)`
  37. - `put`: replaces the current state with another state. `put newState = State $ \s -> ((), newState)`
  38. - Notice that the previous state, the bound parameter `s`, is ignored
  39. - `State` is a perfect wrapper for `random`. `state random`
  40. - Can be used by i.e. a function with type `State StdGen (Bool, Bool, Bool)` for flipping 3 coins
  41. - `Either a` is a monad, conditional on `(Error a)`
  42. - It has a special implementation for `fail`: `fail msg = Left (strMsg msg)`
  43. - `strMsg` is a function defined by `Error` types and creates an error of type `a` from a string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement