问题描述
有没有一种方法可以让行为t [a] ,其中t时刻[a]的值是包含在行为t [行为ta] 在时间t? I.e,一个类型为的函数:
行为t [行为t a] - >行为t [a]
如果这是不可能的,那是因为逻辑上的不可能性或限制在反应香蕉?
该类型是平凡居住的任何应用程序:
$ b
import Control.Applicative
import Control.Monad
导入Data.Functor.Identity
将限定的Data.Traversable导入为T
f'::(Applicative f)=> f [f a] - > f [a]
f'= const $ pure []
这显然不是什么你打算。因此,让我们问一下:
(Traversable t)=>行为u(t(行为u a)) - >行为u(ta)
或更普遍的适用于我们可以构建的应用程序
(T.Traversable t)=> f(t(f a)) - > f(ta)
任何 f $ c $这也是一个monad:
f ::(Monad m,T.Traversable t)=> m(t(m a)) - > m(t a)
f =联接。 liftM T.sequence
一个明显的问题出现了:如果应用程序有这样一个 f ,是否必须是monad?答案是是。我们只需将 f 应用于 Identity traversable(一元素集合 - Traversable 实例身份)和构造加入作为
g ::(Applicative m)=> (t.Traversable t)=> m(t(m a)) - > m(t a))
- > (m(m a) - > m a)
g f = fmap runIdentity。 F 。 fmap Identity
所以我们的函数正好适用于那些也是monad的应用程序。
结束语:您正在寻找的函数存在当且仅当 Behavior 是 Monad 。因为它不是,所以很可能没有这样的功能。 (我相信如果有办法让它成为monad,它会包含在库中)。
Is there a way to have a Behavior t [a] where the values of [a] at time t are the values contained in a Behavior t [Behavior t a] at time t? I.e, a function with the type of:
Behavior t [Behavior t a] -> Behavior t [a]
If this is not possible, is that because of a logical impossibility or a limitation in reactive-banana?
The type is trivially inhabited for any Applicative:
{-# LANGUAGE RankNTypes #-} import Control.Applicative import Control.Monad import Data.Functor.Identity import qualified Data.Traversable as T f' :: (Applicative f) => f [f a] -> f [a] f' = const $ pure []
which is clearly not what you intended. So let's ask for inhabitation of
(Traversable t) => Behavior u (t (Behavior u a)) -> Behavior u (t a)
or more generally for which applicatives we can construct
(T.Traversable t) => f (t (f a)) -> f (t a)
This is inhabited for any f that is also a monad:
f :: (Monad m, T.Traversable t) => m (t (m a)) -> m (t a) f = join . liftM T.sequence
An obvious question arises: If an applicative has such an f, does it have to be a monad? The answer is yes. We just apply f to the Identity traversable (one-element collection - the Traversable instance of Identity) and construct join as
g :: (Applicative m) => (forall t . (T.Traversable t) => m (t (m a)) -> m (t a)) -> (m (m a) -> m a) g f = fmap runIdentity . f . fmap Identity
So our function is inhabited precisely for those applicatives that are also monads.
To conclude: The function you're seeking would exist if and only if Behavior were a Monad. And because it is not, most likely there is no such function. (I believe that if there were a way how to make it a monad, it'd be included in the library.)
这篇关于可能吗?:行为t [行为t a] - >行为t [a]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!