4

我正在尝试将 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 替换 render 并得到类似的错误,所以我认为即使渲染处理输入可能存在问题,我认为我与 foldp 一起使用的函数也存在问题。

4

2 回答 2

3

TL;博士

这是正确的代码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,所以让我们foldp在 的上下文中使类型更具体mainrender映射到输出上,所以b应该是(Int, Int),它对应于(0,0)初始值。a应该像输入一样。因此,对于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 = {}. * 这两个numbers 很可能Int是 s。* something 是带有xand记录的元组中的第一个y,因此它必须是Floatfrominput信号。

-- 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)

那些看起来很相似。函数的第一个和第二个参数被交换,第二个参数应该是一个整数元组,而不是一个带有整数x和的记录y

回顾

您可以看到mainin comments 的其他定义是如何工作的,但是您更改了(x,y)to并在错误的参数周围{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)

这给了你几乎相同的行为。

于 2015-01-05T19:25:22.763 回答
1

即使有 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)
于 2015-03-22T21:08:31.743 回答