我正在尝试编译此小段代码。
module Sodium where
import Prelude
import Control.Monad.Free
import Data.Coyoneda
import Data.Tuple
data ReactiveF more
= RFNewEvent (forall a. (Tuple (Event a) (a -> Reactive Unit) -> more))
type Reactive a = FreeC ReactiveF a
data Event a = Event a
newEvent :: forall a. Reactive (Tuple (Event a) (a -> Reactive Unit))
newEvent = liftFC $ RFNewEvent id
如果我改为在RFNewEvent中使用“ Number”而不是“ a”,那么一切都可以编译。但是那一刻我走到了“永远”。并将“ Number”替换为不再编译的“ a”。
我收到以下错误消息
Cannot unify type
a1
with type
a0
有人知道如何进行这项工作吗?
我正在使用无purescript的0.5.0版本。
编辑
如果我使用以下
data NewEventData = NewEventData forall a. Tuple (Event a) (a -> Reactive Unit)
并将其替换为RFNewEvent,然后将进行编译。但是我最终得到了newEvent不需要的类型签名。
newEvent :: Reactive NewEventData
newEvent = liftFC $ RFNewEvent id
这使我可以创建事件,但可以让我向事件流拍摄不同的事件值,而不是相同类型的值。 (现在在newEvent上完全丢失a。)
我可能会犯错。
总体目标是使用Free Monad模拟SodiumFRP的界面。然后,在解释Free Monad时,通过FFI插入一个与Sodium类似的现有JavaScript FRP库。
这可能吗?
最佳答案
现在,以下代码可以编译并具有“ newEvent”的所需类型签名
module FRP.Sodium where
import Prelude
import Control.Monad.Free
import Data.Coyoneda
import Data.Tuple
data ReactiveF more
= RFNewEvent (NewEventData -> more)
type Reactive a = FreeC ReactiveF a
data NewEventData = NewEventData forall a. Tuple (Event a) (a -> Reactive Unit)
data Event a
= ENever
| EMerge (Event a) (Event a)
| EFilterJust (Event (Maybe a))
| ECoalesce (a -> a -> a) (Event a)
| EOnce (Event a)
| ESplit (Event (Array a))
| EVar Int
data Behaviour a = BVar Int
extractNewEventData :: forall a. NewEventData -> (Tuple (Event a) (a -> Reactive Unit))
extractNewEventData (NewEventData x) = x
newEvent :: forall a. Reactive (Tuple (Event a) (a -> Reactive Unit))
newEvent = map extractNewEventData $ liftFC $ RFNewEvent id
编辑
还尝试了purescript-exists。可以定义“样本”
将RFSample添加到ReactiveF ...
.
.
.
data ReactiveF more
= RFNewEvent (NewEventData -> more)
| RFSample (SampleData more)
.
.
.
data SampleDataF more a = SampleDataF (Behaviour a) (a -> more)
type SampleData more = Exists (SampleDataF more)
sample :: forall a. Behaviour a -> Reactive a
sample beh = liftFC $ RFSample $ mkExists $ SampleDataF beh id
谢谢Phil Freeman的评论。
关于purescript - Purescript中的免费Monad中的多态指令,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33318973/