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

  • Home
  • SEARCH
  • 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 8148363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:38:00+00:00 2026-06-06T14:38:00+00:00

I have declared the following type KEY = (IPv4, Integer) type TPSQ = TVar

  • 0

I have declared the following

type KEY = (IPv4, Integer)
type TPSQ = TVar (PSQ.PSQ KEY POSIXTime)
type TMap = TVar (Map.Map KEY [String])

data Qcfg = Qcfg { qthresh :: Int, tdelay :: Rational, cwpsq :: TPSQ, cwmap :: TMap, cw
chan :: TChan String } deriving (Show)

and would like this to be serializable in a sense that Qcfg can either be written to disk or be sent over the network. When I compile this I get the error

No instances for (Show TMap, Show TPSQ, Show (TChan String))
      arising from the 'deriving' clause of a data type declaration
    Possible fix:
      add instance declarations for
      (Show TMap, Show TPSQ, Show (TChan String))
      or use a standalone 'deriving instance' declaration,
           so you can specify the instance context yourself
    When deriving the instance for (Show Qcfg)

I am now not quite sure whether there is a chance at all to serialize my TChan although all individual nodes in it are members of the show class.

For TMap and TPSQ I wonder whether there are ways to show the values in the TVar directly (because it does not get changed, so there should no need to lock it) without having to declare an instance that does a readTVar ?

  • 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-06T14:38:01+00:00Added an answer on June 6, 2026 at 2:38 pm

    I understood your comments to mean that you want to serialize the contents of the TVar and not the TVar itself.

    There is only one way to extract the value from a TVar, and that’s readTVar:

    readTVar :: TVar a -> STM a
    

    … which you can do in the IO monad using atomically:

    atomically . readTVar :: TVar a -> IO a
    

    TChan is more tricky, though, since you can’t inspect the contents without flushing out the entire TChan. This is doable, even if wastefully, by inspecting the entire contents as a single STM action and then reinserting them all. If this is what you choose to do, it would also eventually require being run in the IO monad.

    This means you won’t be able to derive a Show instance for it, since Show expects a pure computation that converts it to a String, and not one residing in the IO monad.

    However, there’s no reason you have to use the Show class. You can just define a custom function to serialize your data type in the IO monad. Also, it’s generally not advisable to use Show for serialization purposes since:

    • Some of your data types (like PSQ) have no Read instance
    • It’s a pain in the butt to define Read instances in general
    • String representations are very space-inefficient

    So I would recommend you use a proper serialization library like binary or cereal to do serialization and deserialization. These convert data types to a binary representation, and they make it very easy to define encoders and decoders.

    However, even those libraries only accept instances for pure conversions and not operations in the IO monad, so what you must do is factor your serialization into a two-step process:

    1. Extract the contents of your TVars in the IO monad.
    2. Serialize the contents (along with the rest of your data-type) using cereal/binary.

    There is still one last caveat, which is that not all of your data types have Binary instances (assuming we use the binary package), but fortunately lists do have a Binary instance, so a convenient work-around is to just convert your data type to a list (using toList and serialize the list. Then, when you deserialize the list, you use fromList to recover your original type.

    So the following function will do all of that (using binary):

    serializeQcfg file (Qcfg qthresh tdelay cwpsq cwmap cwchan) = do
        -- Step 1: Extract contents of concurrency variables
        psq    <- atomically $ readTVar cwpsq
        myMap  <- atomically $ readTVar cwmap
        myChan <- atomically $ entireTChan cwchan
        -- Step 2: Encode the extracted data
        encodeFile file (qthresh, tdelay, toList psq, myMap, myChan)
    

    Edit: Actually, it’s probably better to combine all the atomic transactions into a single transaction as Daniel pointed out, so you would actually do:

    serializeQcfg file (Qcfg qthresh tdelay cwpsq cwmap cwchan) = do
        -- Step 1: Extract contents of concurrency variables
        (psq, myMap, myChain) <- atomically $ (,,) <$> readTVar cwpsq
                                                   <*> readTVar cwmap
                                                   <*> entireTChan cwchan
        -- Step 2: Encode the extracted data
        encodeFile file (qthresh, tdelay, toList psq, myMap, myChan)
    

    I left out the implementation of entireTChan, which would basically flush the TChan to inspect the entire contents and then reload it again, but its type signature would be something like:

    entireTChan :: TChan a -> STM [a]
    

    I also left out the deserialization implementation, but I think if you understand the above example and take the time to learn how to use the binary or cereal packages you should be able to figure it out easily enough.

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

Sidebar

Related Questions

I have declared the following method: private void mockInvokeDBHandler(Map<String, Object>... rows) { List<Map<String, Object>>
I have declared the following enum type in which I want the first member
I have the following declared on a Singleton type object named CicApplication: internal static
I have a Map whose keys are of generic type Key<T> , and values
I have declared the following struct: typedef struct _RECOGNITIONRESULT { int begin_time_ms, end_time_ms; char*
In following program . I have one doubt. I have declared one global variable
I have the declared a class in following way class A: def __init__(self, list_1,
I have the following template method declared in my interface: class IObjectFactory { public:
I have a project that requires the following. Four arrays will be declared in
I have the following as two ways to declare a two dimensional String array.

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.