Add multiple signals with foldp

I am trying to use foldp with the result of an input function that combines two signals.

Here is the code I have.

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)

And the error I get:

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

Basically, I can replace render with asText and get a similar error, so I think that, despite the fact that there may be problems with processing the documents, I think that there is a problem with the function that I use with foldp.

+4
source share
2 answers

TL DR

Here is the correct code for main:

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

Search for a solution

There is a problem with your types. So let's look at some types:

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 main. render , 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 = {}. * number, , Int s. * - x y, Float input.

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

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

.

+3

, 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)
+1

Source: https://habr.com/ru/post/1570035/


All Articles