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 6578403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:46:01+00:00 2026-05-25T15:46:01+00:00

I’m looking over the Cloud Haskell package’s Encoding.hs , and encountered some strange code

  • 0

I’m looking over the Cloud Haskell package’s Encoding.hs, and encountered some strange code that I was hoping someone could help me better understand. Included is the necessary code:

class (Binary a,Typeable a) => Serializable a
instance (Binary a,Typeable a) => Serializable a

data Payload = Payload
    { 
        payloadType :: !ByteString,
        payloadContent :: !ByteString
    } deriving (Typeable)

serialDecodePure :: (Serializable a) => Payload -> Maybe a
serialDecodePure a = (\id -> 
    let pc = payloadContent a
    in pc `seq`
        if (decode $! payloadType a) == show (typeOf $ id undefined)
        then Just (id $! decode pc)
        else Nothing ) id

I’m just curious about what the $! does (I’m guessing just strictly evaluates), and also why we need the id trick (something with lazy evaluation?). Also I am specifically having problems with this line:

if (decode $! payloadType a) == show (typeOf $ id undefined)

I’m guessing this is seeing if the payloadType is invalid for whatever reason, but if that is the case shouldn’t the then and else clauses be switched, ie change:

if (decode $! payloadType a) == show (typeOf $ id undefined)
    then Just (id $! decode pc)
    else Nothing

to

if (decode $! payloadType a) == show (typeOf $ id undefined)
    then Nothing
    else Just (id $! decode pc)

Thanks for any help you can provide.

  • 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-05-25T15:46:02+00:00Added an answer on May 25, 2026 at 3:46 pm

    You are correct that $! is a strict evaluator. It’s type is identical to $, and the only semantic difference is that the second argument is seq‘d before being passed to the function.

    I think the id is actually there to help type inference. Within the function block (\id -> ...), the function id is forced to have type a -> a, where a is not just any type variable, but the same a as in

    serialDecodePure :: (Serializable a) => Payload -> Maybe a
    

    This is due to this line:

    Just (id $! decode pc)
    

    since this has type Maybe a, id has the inferred type a -> a. As a consequence, on the line you’re looking at,

    if (decode $! payloadType a) == show (typeOf $ id undefined)
    

    id undefined :: a, where a is again the same as the output.

    Now we can get to the type checking. Since this function is polymorphic and will decode to any type, it needs to check that the encoded data is compatible with the type it’s decoding to. What if you encoded a String and are trying to decode to an Int? The LHS would decode to “[Char]”, which is the TypeRep representation of a String. The RHS would instead be “Int”, the type it’s attempting to decode to. Since they aren’t equal, the “else” path is the one that returns None.

    Rather than this id function type restriction, you could accomplish the same thing with the ScopedTypeVariables extension.

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

Sidebar

Related Questions

No related questions found

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.