monads

Can monad with broken associativity law yield incorrect result in for-comprehension?

*爱你&永不变心* 提交于 2020-03-18 15:47:42
问题 Here is a Monad instance for ListT (copied from montrivo) case class ListT[M[_], A](value: M[List[A]]) implicit def listTMonad[M[_]: Monad] = new Monad[ListT[M, *]] { override def flatMap[A, B](fa: ListT[M, A])(f: A => ListT[M, B]): ListT[M, B] = ListT( Monad[M].flatMap[List[A], List[B]](fa.value)( list => Traverse[List].flatTraverse[M, A, B](list)(a => f(a).value) ) ) override def pure[A](a: A): ListT[M, A] = ListT(Monad[M].pure(List(a))) override def tailRecM[A, B](a: A)(f: A => ListT[M,

Can monad with broken associativity law yield incorrect result in for-comprehension?

核能气质少年 提交于 2020-03-18 15:47:07
问题 Here is a Monad instance for ListT (copied from montrivo) case class ListT[M[_], A](value: M[List[A]]) implicit def listTMonad[M[_]: Monad] = new Monad[ListT[M, *]] { override def flatMap[A, B](fa: ListT[M, A])(f: A => ListT[M, B]): ListT[M, B] = ListT( Monad[M].flatMap[List[A], List[B]](fa.value)( list => Traverse[List].flatTraverse[M, A, B](list)(a => f(a).value) ) ) override def pure[A](a: A): ListT[M, A] = ListT(Monad[M].pure(List(a))) override def tailRecM[A, B](a: A)(f: A => ListT[M,

Purpose of a single case discriminated union

江枫思渺然 提交于 2020-02-29 10:20:06
问题 I'm defining a monadic observable/reactive parser. This behaves quite differently to a normal parser as it is a continuous query. The underlying type is: IObservable<'a> -> IObservable<'b> From looking at various parser implementations in functional languages, it seems as though the more appropriate way to define things is a single case discriminated union: type Pattern<'a,'b> = Pattern of (IObservable<'a> -> IObservable<'b>) Which means I then need to extract the underlying function to use

Understanding do notation for simple Reader monad: a <- (*2), b <- (+10), return (a+b)

孤者浪人 提交于 2020-02-24 07:56:05
问题 instance Monad ((->) r) where return x = \_ -> x h >>= f = \w -> f (h w) w import Control.Monad.Instances addStuff :: Int -> Int addStuff = do a <- (*2) b <- (+10) return (a+b) I'm trying to understand this monad by unwiding the do notation, because I think the do notation hides what happens. If I understood correctly, this is what happens: (*2) >>= (\a -> (+10) >>= (\b -> return (a+b))) Now, if we take the rule for >>= , we must understand (*2) as h and (\a -> (+10) >>= (\b -> return (a+b)))

Here is the C# Monad, where is the problem?

丶灬走出姿态 提交于 2020-02-24 00:52:52
问题 Reading a Previous SO Question I was confused to find Eric Lippert saying that an interface cannot be defined in C# for all Monads, using an implementation as below: typeInterface Monad<MonadType<A>> { static MonadType<A> Return(A a); static MonadType<B> Bind<B>(MonadType<A> x, Func<A, MonadType<B>> f); } My problem is all the problems listed in the question seem to have easy solutions: no "higher kinded types" => use parent interfaces no static method in interface. => why use static?! just

Here is the C# Monad, where is the problem?

一个人想着一个人 提交于 2020-02-24 00:49:32
问题 Reading a Previous SO Question I was confused to find Eric Lippert saying that an interface cannot be defined in C# for all Monads, using an implementation as below: typeInterface Monad<MonadType<A>> { static MonadType<A> Return(A a); static MonadType<B> Bind<B>(MonadType<A> x, Func<A, MonadType<B>> f); } My problem is all the problems listed in the question seem to have easy solutions: no "higher kinded types" => use parent interfaces no static method in interface. => why use static?! just

Here is the C# Monad, where is the problem?

天涯浪子 提交于 2020-02-24 00:49:28
问题 Reading a Previous SO Question I was confused to find Eric Lippert saying that an interface cannot be defined in C# for all Monads, using an implementation as below: typeInterface Monad<MonadType<A>> { static MonadType<A> Return(A a); static MonadType<B> Bind<B>(MonadType<A> x, Func<A, MonadType<B>> f); } My problem is all the problems listed in the question seem to have easy solutions: no "higher kinded types" => use parent interfaces no static method in interface. => why use static?! just

Converting Monad notation to Arrow notation

戏子无情 提交于 2020-02-17 13:35:36
问题 I'm trying to understand arrow notation, in particularly how it works with Monads. With Monads I can define the following: f = (*2) g = Just 5 >>= (return . f) and g is Just 10 How do I do the above but using arrow notation? 回答1: Changing your Monad thinking to Arrow thinking The first step to translating into Arrow is to move from thinking about m b on its own to thinking about a -> m b . With a monad, you'd write use x = do ..... .... doThis = do .... ... thing = doThis >>= use whereas an

Converting Monad notation to Arrow notation

北城以北 提交于 2020-02-17 13:32:43
问题 I'm trying to understand arrow notation, in particularly how it works with Monads. With Monads I can define the following: f = (*2) g = Just 5 >>= (return . f) and g is Just 10 How do I do the above but using arrow notation? 回答1: Changing your Monad thinking to Arrow thinking The first step to translating into Arrow is to move from thinking about m b on its own to thinking about a -> m b . With a monad, you'd write use x = do ..... .... doThis = do .... ... thing = doThis >>= use whereas an

Is Java 8 missing an OptionalBoolean?

為{幸葍}努か 提交于 2020-02-12 19:14:14
问题 As a primitive version of Optional*, Java 1.8 provides OptionalInt, OptionalLong and OptionalDouble. But I cannot find the equivalent OptionalBoolean class. Are there any technical reasons against having an OptionalBoolean ? * An Optional may or may not have the presence of a value, is used as an alternative to null . 回答1: This quote explains the considerations behind having primitive streams. I'm assuming the same applied to primitive Optionals. In short, primitive streams (and probably