Joerg Fritsch | 18 Jul 19:08 2013

what is wrong w my IORef Word32 ?

All, what is wrong w the below code?

I get an type error related to the operation shiftL

import Data.Bits
import Data.Word
import Data.IORef

data Word32s = Word32s { x :: IORef Word32 }

bitfield :: Word32
bitfield = 0

mkbitfield :: Word32 -> IO Word32s
mkbitfield i = do the_bf <- newIORef i
                  return (Word32s the_bf)

sLbitfield :: Integer -> Word32s -> IO ()
sLbitfield i (Word32s bf) = do modifyIORef bf (shiftL i)

main::IO()
main = do
     oper_bf <- mkbitfield bitfield 
     sLbitfield 2 oper_bf

bf_003.hs:15:48:
    Couldn't match type `Int' with `Word32'
    Expected type: Word32 -> Word32
      Actual type: Int -> Word32
    In the return type of a call of `shiftL'
(Continue reading)

Edward Z. Yang | 18 Jul 19:19 2013
Picon

Re: what is wrong w my IORef Word32 ?

shiftL has the wrong type:  Bits a => a -> Int -> a
so it is expecting the value in the IORef to be an Int.

Edward

Excerpts from Joerg Fritsch's message of Thu Jul 18 10:08:22 -0700 2013:
> All, what is wrong w the below code?
> 
> I get an type error related to the operation shiftL
> 
> import Data.Bits
> import Data.Word
> import Data.IORef
> 
> data Word32s = Word32s { x :: IORef Word32 }
> 
> bitfield :: Word32
> bitfield = 0
> 
> mkbitfield :: Word32 -> IO Word32s
> mkbitfield i = do the_bf <- newIORef i
>                   return (Word32s the_bf)
> 
> sLbitfield :: Integer -> Word32s -> IO ()
> sLbitfield i (Word32s bf) = do modifyIORef bf (shiftL i)
> 
> main::IO()
> main = do
>      oper_bf <- mkbitfield bitfield 
>      sLbitfield 2 oper_bf
(Continue reading)

Joerg Fritsch | 18 Jul 19:22 2013

Re: what is wrong w my IORef Word32 ?

Can I easily fix the modifyIORef statement by swapping "something" ?
--Joerg

On Jul 18, 2013, at 7:19 PM, "Edward Z. Yang" <ezyang <at> MIT.EDU> wrote:

> shiftL has the wrong type:  Bits a => a -> Int -> a
> so it is expecting the value in the IORef to be an Int.
> 
> Edward
> 
> Excerpts from Joerg Fritsch's message of Thu Jul 18 10:08:22 -0700 2013:
>> All, what is wrong w the below code?
>> 
>> I get an type error related to the operation shiftL
>> 
>> import Data.Bits
>> import Data.Word
>> import Data.IORef
>> 
>> data Word32s = Word32s { x :: IORef Word32 }
>> 
>> bitfield :: Word32
>> bitfield = 0
>> 
>> mkbitfield :: Word32 -> IO Word32s
>> mkbitfield i = do the_bf <- newIORef i
>>                  return (Word32s the_bf)
>> 
>> sLbitfield :: Integer -> Word32s -> IO ()
>> sLbitfield i (Word32s bf) = do modifyIORef bf (shiftL i)
(Continue reading)

Taylor Hedberg | 18 Jul 19:42 2013

Re: what is wrong w my IORef Word32 ?

Joerg Fritsch, Thu 2013-07-18  <at>  19:22:46+0200:
> Can I easily fix the modifyIORef statement by swapping "something" ?
> --Joerg

Replace `shiftL` with `flip shiftL` and change the type signature of
`sLbitfield` to take an Int instead of an Integer.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane