Is it possible?: Behavior t [Behavior t a] -> Behavior t [a]

mcj*_*s45 3 haskell frp reactive-banana

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]
Run Code Online (Sandbox Code Playgroud)

If this is not possible, is that because of a logical impossibility or a limitation in reactive-banana?

Pet*_*lák 12

这种类型通常适用于任何类型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 []
Run Code Online (Sandbox Code Playgroud)

这显然不是你想要的.所以让我们请求居住

(Traversable t) => Behavior u (t (Behavior u a)) -> Behavior u (t a)
Run Code Online (Sandbox Code Playgroud)

或更一般地说,我们可以构建的应用程序

(T.Traversable t) => f (t (f a)) -> f (t a)
Run Code Online (Sandbox Code Playgroud)

这适用于任何f也是monad的人:

f :: (Monad m, T.Traversable t) => m (t (m a)) -> m (t a)
f = join . liftM T.sequence
Run Code Online (Sandbox Code Playgroud)

一个显而易见的问题出现了:如果一个申请者有这样的话f,它必须是一个单子吗?答案是肯定的.我们只适用f于Identity穿越(一元的收藏- Traversable的情况下Identity),并构建join为

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
Run Code Online (Sandbox Code Playgroud)

所以我们的功能正好适用于那些也是monad的应用程序.

总之:你正在寻找的功能,当且仅当存在将Behavior是一个Monad.而且因为它不是,很可能没有这样的功能.(我相信如果有一种方法可以让它成为一个monad,它将被包含在库中.)