4

遵循这篇文章中出色答案的引导中出色答案的引导下,我试图获得一个ArrowLoop不使用箭头符号的工作示例。在我完全理解箭头在引擎盖下的工作原理之前,我对使用箭头表示法感到不舒服。话虽如此,我已经构建了一个基于我对 Arrows 的(有限)理解的小程序应该可以工作。但是,它最终以可怕的<<loop>>异常终止:

module Main where

import Control.Wire
import FRP.Netwire

farr :: SimpleWire (Int, Float) (String, Float)
farr = let
  fn :: Int -> Float -> ((String, Float), SimpleWire (Int, Float) (String, Float))
  fn i f = (("f+i: " ++ (show (fromIntegral i + f)), f + 0.1), loopFn)

  loopFn :: SimpleWire (Int, Float) (String, Float)
  loopFn = mkSFN $ \(i, f) -> fn i f
  in
   mkSFN $ \(i, _) -> fn i 0.0

main :: IO ()
main = do
  let sess = clockSession_ :: Session IO (Timed NominalDiffTime ())
  (ts, sess2) <- stepSession sess

  let wire = loop farr
      (Right s, wire2) = runIdentity $ stepWire wire ts (Right 0)

  putStrLn ("s: " ++ s)

  (ts2, _) <- stepSession sess2
  let (Right s2, _) = runIdentity $ stepWire wire2 ts (Right 1)

  putStrLn ("s2: " ++ s2)

我的直觉告诉我<<loop>>当您不向循环提供初始值时,通常会出现异常。我没有用包含的行做到这一点fn i 0.0吗?输出不同意:

$ ./test
s: f+i: 0.0
test.exe: <<loop>>

有谁知道我做错了什么?

4

1 回答 1

2

ArrowLoop混淆的主要点似乎是和之间的整体关系mfix。对于初学者来说,fix是一个找到给定函数的不动点的函数:

fix :: (a -> a) -> a
fix f = let x = f x in x

mfix是此函数的一元扩展,其类型签名毫不奇怪:

mfix :: (a -> m a) -> m a

那么这有什么关系ArrowLoop呢?好吧,ArrowLoopNetwire 的实例mfix在传递的线的第二个参数上运行。换句话说,考虑以下的类型签名loop

loop :: a (b, d) (c, d) -> a b c

在 Netwire 中,的实例ArrowLoop是:

instance MonadFix m => ArrowLoop (Wire s e m)

这意味着loop与电线一起使用时函数的类型是:

loop :: MonadFix m => Wire s e m (b, d) (c, d) -> Wire s e m b c

由于loop不采用 type 的初始参数d,这意味着无法通过线路初始化任何类型的常规“循环”。从中获取值的唯一方法是继续将输出用作输入,直到找到终止条件,这类似于工作方式fix。作为参数传递的线loop从不实际执行超过一次,因为stepWire通过不同的输入一遍又一遍地应用于同一根线。只有当连线实际产生一个固定值时,函数才会步进并产生另一条连线(其行为方式与第一个连线相同)。

为了完整起见,这里是我最初对loop应该如何工作的直觉的代码,我将其命名为semiLoop

semiLoop :: (Monad m, Monoid s, Monoid e) => c -> Wire s e m (a, c) (b, c) -> Wire s e m a b
semiLoop initialValue loopWire = let
  runLoop :: (Monad m, Monoid s, Monoid e) =>
             Wire s e m (a, c) (b, c) -> s -> a -> c -> m (Either e b, Wire s e m a b)
  runLoop wire ts ipt x = do
    (result, nextWire) <- stepWire wire ts (Right (ipt, x))
    case result of
      Left i -> return (Left i, mkEmpty)
      Right (value, nextX) ->
        return (Right value, mkGen $ \ts' ipt' -> runLoop nextWire ts' ipt' nextX)
  in
   mkGen $ \ts input -> runLoop loopWire ts input initialValue

编辑

在Petr 提供的精彩答案delay之后,组合器对于防止loop组合器发散至关重要。只需在上述循环部分中delay使用下一个值的惰性之间创建一个单值缓冲区。mfix因此,上述相同的定义semiLoop是:

semiLoop :: (MonadFix m, Monoid s, Monoid e) =>
            c -> Wire s e m (a, c) (b, c) -> Wire s e m a b
semiLoop initialValue loopWire = loop $ second (delay initialValue) >>> loopWire
于 2014-08-12T04:15:09.590 回答