0

As a simple example I have this.

import Prelude hiding ((.))
import FRP.Netwire
import Control.Wire

f :: Int -> Char -> String
f = replicate

w :: => Wire s e m Int Char
w = mkSF_ fromInt
    where
        fromInt :: Int -> Char
        fromInt 1 = 'a'
        fromInt 2 = 'b'
        fromInt _ = '_'

w2 :: Wire s e m Int String
w2 = undefined -- This is where I get stuck

And I would like to be able to create a wire from Int's to Strings.

I think it should be easy, but I'm having no luck.

4

3 回答 3

2

另一种选择是使用更简洁的应用语法,imo

w2 :: Monad m => Wire s e m Int String
w2 = f <$> id <*> w

这概括为

(Category cat, Applicative (cat a)) => (a -> b -> c) -> cat a b -> cat a c

请注意,每个Arrow都会产生上述约束。

于 2014-12-23T17:38:37.283 回答
2

您需要拆分原始Int输入并将其扇出到warr f。解释它的最简单方法是使用箭头符号:

{-# LANGUAGE Arrows #-}
w2 :: (Monad m) => Wire s e m Int String
w2 = proc n -> do
    c <- w -< n
    returnA -< f n c

现在,我不知道关于 Netwire 的第一件事,但那个约束Monad m是存在的,因为.ArrowWire s e m

如果你想摆脱箭头符号,上面可以重写为

w2 :: (Monad m) => Wire s e m Int String
w2 = arr (uncurry f) . (id &&& w)

当然,您可以将这个概念概括为这个抽象:

-- I want to write (Arrow (~>)) => (a -> b -> c) -> (a ~> b) -> (a ~> c)!
-- Damn you TypeOperators!
arr2 :: (Arrow arr) => (a -> b -> c) -> arr a b -> arr a c
arr2 f arr = proc x -> do
    y <- arr -< x
    returnA -< f x y
于 2014-12-21T08:11:59.670 回答
1

我想出的另一个解决方案是

w2 :: Monad m => Wire s e m Int String
w2 = liftA2 ($) (mkSF_ f) w
-- or w2 = liftA2 ($) (arr f) w
-- or w2 = arr f <*> w
于 2014-12-21T14:07:52.743 回答