Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8534367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:10:46+00:00 2026-06-11T10:10:46+00:00

I need to read some amount of bits from Get monad. Now my code

  • 0

I need to read some amount of bits from Get monad. Now my code looks like

readBits :: Int -> Int -> Get (Word32, Int)
readBits count state = ...

readValue :: Get (Word32, Word32)
readValue = do
   -- read fst bit count 
   (bits1, s0) <- readBits 5 0
   -- read bits1 bits as fst
   (fst, s1) <- readBits bits1 s0
   -- read snd bit count
   (bits2, s2) <- readBits 5 s1
   -- read bits2 bits as snd
   (snd, s3) <- readBits bits2 s2  
   -- flush incomplete byte
   when (s3 /= 0) $ skip 1
   return (fst, snd)

I want to wrap this into some kind of state monad, to have code like

readBits :: Int -> BitReader Word32
readBits count = ...

runBitReader :: BitReader a -> Get a

readValue :: Get (Word32, Word32) 
readValue = runBitReader $ do
      bits1 <- readBits 5
      fst <- readBits bits1
      bits2 <- readBits 5
      snd <- readBits bits2
      return (fst, snd)

What functions should I implement? How should they be implemented?

I have looked into Get and BitGet source code, but not fully understand what’s going on.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:10:47+00:00Added an answer on June 11, 2026 at 10:10 am

    This is the most typical usecase for Monad Transformers.

    You have defined most of the structure correctly. To answer your questions

    What functions should I implement? 
    
    • Well you first need to wrap Get monad into the StateT Transformer to get BitReader.
    • You need to implement proper definition for readBits using get to get the current state and put to save the state back.
    • You need to run your code wrapped in BitReader to get back the output in Get Monad. So you need to define runBitReader using runStateT.

    To answer your next question.

    How should they be implemented?
    

    I have given the possible implementation. You still need to define some functions to make it work.

    import Control.Monad.State 
    import qualified Control.Monad.State as ST
    import Data.Binary 
    
    type BitReader = StateT Int Get
    
    readBits' :: Int -> Int -> Get (Word32, Int)
    readBits' = undefined   
    
    readBits :: Int -> BitReader Word32
    readBits n = do 
        s0 <- ST.get 
        (a,s1) <- lift $ readBits' n s0
        ST.put s1 
        return a
    
    
    runBitReader :: BitReader a -> Get a
    runBitReader w = do 
        (a,s) <- runStateT w 0
        return a
    
    readValue = do
          fst <- readBits 5
          snd <- readBits 10
          return (fst, snd) 
    

    I don’t know how looking into code of Get was going to help you. You were looking in the wrong house. You need to read about State Monads and Monad Transformers.

    You can read more about monad transformers here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some graphics work I need to read in a large amount of data
I need to read some data from an input file and plot a graph
I need to write a script in Matlab, which will read some data from
I need to read 16 bits from the binary file as std::string or char
I have a code where I need to capture some information from a equipment.
I need to read through some gigantic log files on a Linux system. There's
I need to read and write some data on .mdb Access file and over
I need to read and write some data through named pipes. I have tested
We need to read and count different types of messages/run some statistics on a
I need to display to the screen some values which are read in method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.