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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:46:44+00:00 2026-05-15T03:46:44+00:00

Can anyone give me a brief example of testing IO actions using Monadic QuickCheck?

  • 0

Can anyone give me a brief example of testing IO actions using Monadic QuickCheck?

  • 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-15T03:46:44+00:00Added an answer on May 15, 2026 at 3:46 am

    The Test.QuickCheck.Monadic module lets you test monadic code, even things that run in IO.

    A monadic property test is of type PropertyM m a, where m is the monad the test runs in and a is ultimately ignored. In the case of PropertyM IO a, you convert the monadic test to a Property by using monadicIO; for all other monads, you use monadic instead (which takes a function to run the monad, something IO doesn’t have).

    In a monadic test, the value returned out of the monad is ignored. To check an expression, use assert; asserting a false value will fail the test. Use run to execute the code in the monad being tested.

    There are other monadic actions at your disposal. For example, pick will generate new test inputs out of a Gen a, and pre will check test preconditions. These are useful if the test inputs or preconditions themselves depend on values computed via the monad being tested, in which case the normal way of generating inputs or checking precontions won’t work.

    Here’s an example of testing some IO code: we check that after writing something to a temporary file, we can read that same data back. For demonstration purposes, we’ll impose the precondition that we write at least one byte to the file. The two test properties do the same thing; one uses pick and pre unnecessarily while the other does not.

    import System.Directory (removeFile)
    import System.IO (hGetContents, hPutStr, hSeek, openBinaryTempFile, SeekMode (..))
    import Test.QuickCheck (arbitrary, Property, quickCheck, (==>))
    import Test.QuickCheck.Monadic (assert, monadicIO, pick, pre, run)
    
    -- Demonstrating pick and pre as well:
    prop_writeThenRead :: Property
    prop_writeThenRead = monadicIO $ do writtenData <- pick arbitrary
                                        pre $ not (null writtenData)
                                        readData <- run $ writeThenRead writtenData
                                        assert $ writtenData == readData
    
    -- A more idiomatic way to write the above:
    prop_writeThenRead2 :: [Char] -> Property
    prop_writeThenRead2 writtenData = not (null writtenData) ==> monadicIO test
        where test = do readData <- run $ writeThenRead writtenData
                        assert $ writtenData == readData
    
    writeThenRead :: [Char] -> IO [Char]
    writeThenRead output = do (path, h) <- openBinaryTempFile "/tmp" "quickcheck.tmp"
                              removeFile path
                              hPutStr h output
                              hSeek h AbsoluteSeek 0
                              hGetContents h
    
    main :: IO ()
    main = do quickCheck prop_writeThenRead
              quickCheck prop_writeThenRead2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone give me an example of these attributes in action: stroke-dasharray, stroke-linecap, stroke-linejoin
Can anyone give me an example of how I can consume the following web
I'm wondering if anyone can give a solid explanation (with example) of POCO (Plain
Can anyone give me an example of how to return the following json simply
Can anyone give an example of a UDP Hole Punching ? Actually, I want
Can anyone give me the working example of reading two files simultaneously through threads?
can anyone give me an example on how to create Sessions and write data
Can anyone give me an example scenario where Asynchronous Callback should be used in
If anyone can give me brief information about the advantages and disadvantages of the
Are there some help resources, or can anyone give me a brief Idea how

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.