Home > Enterprise >  Getting Type error outputting Int despite using realToFrac on value quoted by compiler
Getting Type error outputting Int despite using realToFrac on value quoted by compiler

Time:09-17

I have here func13 which should convert an array of Ints in a tuple to Doubles in a tuple but doesn't and i don't know why.

func13 :: Data.Massiv.Core.Array U Ix1 (Double,Double,Double,Double)
func13 = runST (do tarr <- func12 
                   Data.Massiv.Array.Mutable.forPrimM tarr (pure . (\x -> (( (realToFrac (sel1 x)) / 100 ), ((realToFrac (sel2 x)) / 100), ((realToFrac (sel3 x)) / 100), ((realToFrac (sel4 x)) / 100 )) ))
                   Data.Massiv.Array.Unsafe.unsafeFreeze Par tarr)

The type of Func12 is ST s (Data.Massiv.Array.Mutable.MArray (PrimState (ST s)) U Ix1 (Int,Int,Int,Int)) which has been confirmed by the compiler.

The function forPrimM maps a function (e -> m e) over a mutable array.

This is the compiler error i get

Couldn't match typeInt’ with ‘Double
  Expected type: ST s (Array U Ix1 (Double, Double, Double, Double))
    Actual type: ST s (Array U Ix1 (Int, Int, Int, Int))In a stmt of a 'do' block:
    Data.Massiv.Array.Unsafe.unsafeFreeze Par tarr
  In the first argument of ‘runST’, namely
    ‘(do tarr <- func12
         forPrimM
           tarr
           (pure
              . (\ x
                   -> (((realToFrac (sel1 x)) / 100), ((realToFrac (sel2 x)) / 100), 
                       ((realToFrac (sel3 x)) / 100), ((realToFrac (sel4 x)) / 100))))
         Data.Massiv.Array.Unsafe.unsafeFreeze Par tarr)’
  In the expression:
    runST
      (do tarr <- func12
          forPrimM
            tarr
            (pure
               . (\ x
                    -> (((realToFrac (sel1 x)) / 100), ((realToFrac (sel2 x)) / 100), 
                        ((realToFrac (sel3 x)) / 100), ((realToFrac (sel4 x)) / 100))))
          Data.Massiv.Array.Unsafe.unsafeFreeze Par tarr)
|

161 | Data.Massiv.Array.Unsafe.unsafeFreeze Par tarr)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Can anyone help me figure out why this is occurring please?

CodePudding user response:

The function forPrimM maps a function (e -> m e) over a mutable array.

You have it right there. The function you map over the array must be type-preserving. That's because the updates happen in-place.

Theoretically I suppose it's possible to re-use the storage of an Int array to store Double values, but that's not something I'd attempt except as a last-resort optimisation. In your case I'd suspect it's a much better idea to modify func12 so it can generate Double values by itself, perhaps you can make it (Unbox n, Num n) parametric?

  • Related