使用 foldp 折叠多个信号

Folding multiple signals with foldp

我正在尝试将 foldp 与结合了两个信号的输入函数的结果一起使用。

这是我的代码。

import Graphics.Element (..)
import Graphics.Collage (..)
import Color (..)
import Signal
import Keyboard
import Text (asText)
import Time (..)

-- Display
render : (Int, Int) -> Element
render (xDiff, yDiff) = collage 200 200 [
                          rotate (degrees (toFloat yDiff))
                          (filled blue (ngon 5 (10 * toFloat xDiff))) ]

-- Combine two signals to be a pair
input = Signal.map2 (,) (fps 25) Keyboard.arrows

-- Fold past combined signals from input and pass resulting signal to render
main : Signal Element
main = Signal.map render
           (Signal.foldp (\dir (upd, {x, y}) ->
                        (x + dir.x, y + dir.y)) (0,0) input)

-- Fold past Keyboard.arrows and pass resulting signal to render
--main : Signal Element
--main = Signal.map render
--           (Signal.foldp (\dir (x, y) ->
--                        (x + dir.x, y + dir.y)) (0,0) Keyboard.arrows)

我得到的错误是:

Type mismatch between the following types on line 34, column 55 to 60:
       (Float, { x : Int, y : Int })

       { a |   x : Int, y : number }

   It is related to the following expression:

       input

Type mismatch between the following types on line 34, column 26 to 46:

       { a |   x : Int, y : number }

       number

   Looks like something besides an Int or Float is being used as a number.
   It is related to the following expression:

       (x + dir.x,y + dir.y)

Type mismatch between the following types on line 34, column 26 to 46:

       Int

       { a |   x : number, y : number }

   It is related to the following expression:

       (x + dir.x,y + dir.y)`

在 main 中,我可以用 asText 替换渲染并得到类似的错误,所以我认为即使渲染处理输入可能存在问题,我认为我在 foldp 中使用的函数也存在问题。

TL;DR

这是 main 的正确代码:

main : Signal Element
main = Signal.map render
           (Signal.foldp (\(upd, dir) (x, y) ->
                        (x + dir.x, y + dir.y)) (0,0) input)

寻找解决方案

你的类型有问题。那么让我们看看一些类型:

foldp : (a -> b -> b) -> b -> Signal a -> Signal b
input : Signal (Float, {x : Int, y : Int})
render : (Int, Int) -> Element
main : Signal Element

main 的定义中,问题似乎起源于 foldp,因此让我们在 main 的上下文中使 foldp 的类型更具体。 render 映射到输出上,因此 b 应该是 (Int, Int),它对应于 (0,0) 初始值。 a 应该像输入一样。因此,对于 main 中的特定 foldp,您有:

foldp :  ((Float, {x : Int, y : Int}) -> (Int, Int) -> (Int, Int)) -- the function
      -> (Int, Int) -- the initial value
      -> Signal (Float, {x : Int, y : Int}) -- the input
      -> Signal (Int, Int) -- the output

好的,所以我们知道 foldp 的函数参数应该是什么类型。但是当前函数有什么错误类型?

(\dir (upd, {x, y}) -> (x + dir.x, y + dir.y)) : {recordExt1 | x : number, y : number'} -> (something, {recordExt2 | x : number, y : number'}) -> (number, number')

嗯..这看起来太复杂了。让我们用一些假设来简化。 * 我们可能不需要扩展记录,所以采用 recordExt1 = recordExt2 = {}。 * 两个 number 很可能是 Int。 * something 是记录 xy 的元组中的第一个,因此它必须是来自 input 信号的 Float

-- simplified type of inline function
{ x : Int, y : Int} -> (Float, { x : Int, y : Int}) -> (Int, Int)
-- function deduced from specialised `foldp` type
(Float, {x : Int, y : Int}) -> (Int, Int) -> (Int, Int)

它们看起来很相似。函数的第一个和第二个参数被交换,第二个参数应该是整数元组而不是整数 xy 的记录。

回顾

您可以在评论中看到 main 的其他定义是如何工作的,但随后您将 (x,y) 更改为 {x,y} 并添加了 (upd,... ) 绕错参数。

最后一个音符

如果您实际上不打算使用 fps 25 中的时间增量,您还可以使用:

input = Signal.sampleOn (fps 25) Keyboard.arrows

main = Signal.map render
       (Signal.foldp (\dir (x, y) ->
                    (x + dir.x, y + dir.y)) (0,0) input)

这给你几乎相同的行为。

即使有 Apanatshka 的出色回答,当从 foldp 调用更新函数时,我仍然对参数交换感到困惑,所以这里有一个小的文章来澄清:

module Foldp2 where

import Graphics.Element (..)
import Graphics.Collage (..)
import Color (..)
import Signal
import Keyboard
import Text (asText)
import Time (..)

-- Display
render : (Int, Int) -> Element
render (xDiff, yDiff) = collage 200 200 [
                          rotate (degrees (toFloat yDiff))
                          (filled blue (ngon 5 (10 * toFloat xDiff))) ]

-- `input` produces a (Float, Record)-Tuple wrapped in a Signal
input : Signal (Float, {x : Int, y : Int})
input = Signal.map2 (,) (fps 25) Keyboard.arrows

-- define an update function operating on the unwrapped `input` function
-- `foldp` will make it work on the wrapped `input'
update : ( Float, {x:Int, y:Int}) -> (Int, Int) -> (Int, Int)
update (upd, dir) (x,y) = (x + dir.x, y + dir.y)

-- Making the type of 'foldp' applied to 'update' explicit:
-- Notice that the order of arguments compared to `update` is swapped.
-- The swapping, I think, was the origin of confusion for OP (mine indeed it was).
folding : (Int, Int) -> Signal (Float, {x:Int, y:Int}) -> Signal (Int, Int)
folding = Signal.foldp update

-- Further notice that "aligning" the arguments in `update` with those of
-- `folding` by:

-- update : (Int, Int) -> ( Float, {x:Int, y:Int}) -> (Int, Int)
-- update (x,y) (upd, dir) = (x + dir.x, y + dir.y)

-- gives error:

-- Type mismatch between the following types on line 26, column 28 to 48:
--        { x : Int, y : Int }
--        number
--    It is related to the following expression:
--        (x + dir.x,y + dir.y) 

-- unless you `flip` the order arguments in `folding`:

-- folding : (Int, Int) -> Signal (Float, {x:Int, y:Int}) -> Signal (Int, Int)
-- folding = Signal.foldp (flip update)

main : Signal Element
main = Signal.map render (folding (0,0) input)