monads

Do monad transformers, generally speaking, arise out of adjunctions?

一世执手 提交于 2019-12-03 15:46:14
问题 In Adjoint functors determine monad transformers, but where's lift?, Simon C has shown us the construction... newtype Three u f m a = Three { getThree :: u (m (f a)) } ... which, as the answers there discuss, can be given an instance Adjunction f u => MonadTrans (Three u f) ( adjunctions provides it as AdjointT). Any Hask/Hask adjunction thus leads to a monad transformer; in particular, StateT s arises in this manner from the currying adjunction between (,) s and (->) s . My follow-up

Defining new keywords in F#'s computation expression

眉间皱痕 提交于 2019-12-03 15:08:09
问题 The F# 3.0 beta contains a query {} computation expression with tons of new keywords. How can I define my own keywords in a computation builder? 回答1: In F# 3.0, you can use CustomOperationAttribute for this purpose. The new attribute is not very well-documented, the only examples I find are this great answer by @Tomas and this interesting blog post. 来源: https://stackoverflow.com/questions/9565986/defining-new-keywords-in-fs-computation-expression

New desugaring behavior in Scala 2.10.1

天涯浪子 提交于 2019-12-03 14:46:41
问题 Suppose I have this monadic class: case class Foo[A](xs: List[A]) { def map[B](f: A => B) = Foo(xs map f) def flatMap[B](f: A => Foo[B]) = Foo(xs flatMap f.andThen(_.xs)) def withFilter(p: A => Boolean) = { println("Filtering!") Foo(xs filter p) } } The following is from a 2.10.0 REPL session: scala> for { (a, b) <- Foo(List(1 -> "x")) } yield a res0: Foo[Int] = Foo(List(1)) And here's the same thing in 2.10.1: scala> for { (a, b) <- Foo(List(1 -> "x")) } yield a Filtering! res0: Foo[Int] =

How Monads are considered Pure?

痞子三分冷 提交于 2019-12-03 14:34:52
I am very much new to Haskell, and really impressed by the language's "architecture", but it still bothers me how monads can be pure. As you have any sequence of instructions, it makes it an impure function, especially functions with I/O wouldn't be pure from any point of view. Is it because Haskell assumes, like all pure functions, that IO function has a return value too, but in form of opcode or something? I am really confused. One way to think of this is that a value of type IO a is a "recipe", containing a list of instructions that if performed would have side effects. Constructing that

Examples of a monad whose Applicative part can be better optimized than the Monad part

本秂侑毒 提交于 2019-12-03 14:33:34
问题 In one discussion I heard that Applicative interface of some parsers is implemented differently, more efficiently than their Monad interface. The reason is that with Applicative we know all "effects" in advance, before the whole effectful computation is run. With monads, effects can depend on values during the computation so this optimization is not possible. I'd like to see some good examples of this. It can be some very simple parser or some different monad, that's not important. The

Scala Option object inside another Option object

ε祈祈猫儿з 提交于 2019-12-03 14:02:01
I have a model, which has some Option fields, which contain another Option fields. For example: case class First(second: Option[Second], name: Option[String]) case class Second(third: Option[Third], title: Option[String]) case class Third(numberOfSmth: Option[Int]) I'm receiving this data from external JSON's and sometimes this data may contain null's, that was the reason of such model design. So the question is: what is the best way to get a deepest field? First.get.second.get.third.get.numberOfSmth.get Above method looks really ugly and it may cause exception if one of the objects will be

Use StateT within Web.Scotty

我是研究僧i 提交于 2019-12-03 13:46:20
I'm trying to make a silly webserver that stores data as State . I'm using Web.Scotty . I've used ReaderT before with scotty to access config , but following the same approach doesn't work here. It resets the state on every request. I want to set the initial state when the program starts, then have that same state stick around for the whole life of the program. How can I make this work? (The following creates a new state every request) {-# LANGUAGE OverloadedStrings #-} import Web.Scotty.Trans import Control.Monad.State (StateT, evalStateT, lift) import qualified Control.Monad.State as S

Why must we use state monad instead of passing state directly?

对着背影说爱祢 提交于 2019-12-03 13:29:25
问题 Can someone show a simple example where state monad can be better than passing state directly? bar1 (Foo x) = Foo (x + 1) vs bar2 :: State Foo Foo bar2 = do modify (\(Foo x) -> Foo (x + 1)) get 回答1: State passing is often tedious, error-prone, and hinders refactoring. For example, try labeling a binary tree or rose tree in postorder: data RoseTree a = Node a [RoseTree a] deriving (Show) postLabel :: RoseTree a -> RoseTree Int postLabel = fst . go 0 where go i (Node _ ts) = (Node i' ts', i' +

Is it possible to do the Free Monad in Clojure?

一世执手 提交于 2019-12-03 13:28:12
There has been some outstanding work with Monads in Clojure by Konrad Hinsen , Jim Duey and Leonardo Borges . My question is - is it possible to do the Free Monad in Clojure? This is an example in Haskell from an article on Scala: data Free f r = Free (f (Free f r)) | Pure r This is the corresponding Scala example sealed abstract class Free[S[+_], +A](implicit S: Functor[S]) { final def map[B](f: A => B): Free[S, B] = flatMap(a => Return(f(a))) final def flatMap[B](f: A => Free[S, B]): Free[S, B] = this match { case Gosub(a, g) => Gosub(a, (x: Any) => Gosub(g(x), f)) case a => Gosub(a, f) } ..

How to implement index-core style indexed continuation monad

岁酱吖の 提交于 2019-12-03 13:17:21
I have been looking at (and trying to understand) indexed monads recently. I think I have got my head around one style of indexed monad, as described here: A Neighbourhood of Infinity: Beyond Monads . However, I have found a different style of indexed monad in index-core , which has some parts that seem to correspond to this indexed monad bind with two indexes, for example a similar bind operator !>= . While it clearly has similar changes to the indexes, I can't quite understand how to use these indexes, for example, to control the return types in a continuation monad as with the other style.