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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:38:19+00:00 2026-05-13T08:38:19+00:00

Haskell is a pure functional language, which means Haskell functions have no side affects.

  • 0

Haskell is a pure functional language, which means Haskell functions have no side affects. I/O is implemented using monads that represent chunks of I/O computation.

Is it possible to test the return value of Haskell I/O functions?

Let’s say we have a simple ‘hello world’ program:

main :: IO ()
main = putStr "Hello world!"

Is it possible for me to create a test harness that can run main and check that the I/O monad it returns the correct ‘value’? Or does the fact that monads are supposed to be opaque blocks of computation prevent me from doing this?

Note, I’m not trying to compare the return values of I/O actions. I want to compare the return value of I/O functions – the I/O monad itself.

Since in Haskell I/O is returned rather than executed, I was hoping to examine the chunk of I/O computation returned by an I/O function and see whether or not it was correct. I thought this could allow I/O functions to be unit tested in a way they cannot in imperative languages where I/O is a side-effect.

  • 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-13T08:38:19+00:00Added an answer on May 13, 2026 at 8:38 am

    The way I would do this would be to create my own IO monad which contained the actions that I wanted to model. The I would run the monadic computations I want to compare within my monad and compare the effects they had.

    Let’s take an example. Suppose I want to model printing stuff. Then I can model my IO monad like this:

    data IO a where
      Return  :: a -> IO a
      Bind    :: IO a -> (a -> IO b) -> IO b
      PutChar :: Char -> IO ()
    
    instance Monad IO where
      return a = Return a
      Return a  >>= f = f a
      Bind m k  >>= f = Bind m (k >=> f)
      PutChar c >>= f = Bind (PutChar c) f
    
    putChar c = PutChar c
    
    runIO :: IO a -> (a,String)
    runIO (Return a) = (a,"")
    runIO (Bind m f) = (b,s1++s2)
      where (a,s1) = runIO m
            (b,s2) = runIO (f a)
    runIO (PutChar c) = ((),[c])
    

    Here’s how I would compare the effects:

    compareIO :: IO a -> IO b -> Bool
    compareIO ioA ioB = outA == outB
      where ioA = runIO ioA ioB
    

    There are things that this kind of model doesn’t handle. Input, for instance, is tricky. But I hope that it will fit your usecase. I should also mention that there are more clever and efficient ways of modelling effects in this way. I’ve chosen this particular way because I think it’s the easiest one to understand.

    For more information I can recommend the paper “Beauty in the Beast: A Functional Semantics for the Awkward Squad” which can be found on this page along with some other relevant papers.

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

Sidebar

Ask A Question

Stats

  • Questions 365k
  • Answers 365k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use the data in the /proc filesystem to… May 14, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer Do you still get the error when you use a… May 14, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer You can use array_chunk to create a single array comprised… May 14, 2026 at 3:57 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.