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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:39:48+00:00 2026-05-14T00:39:48+00:00

Could anyone give some pointers on why the impure computations in Haskell are modelled

  • 0

Could anyone give some pointers on why the impure computations in Haskell are modelled as monads?

I mean monad is just an interface with 4 operations, so what was the reasoning to modelling side-effects in it?

  • 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-14T00:39:49+00:00Added an answer on May 14, 2026 at 12:39 am

    Suppose a function has side effects. If we take all the effects it produces as the input and output parameters, then the function is pure to the outside world.

    So, for an impure function

    f' :: Int -> Int
    

    we add the RealWorld to the consideration

    f :: Int -> RealWorld -> (Int, RealWorld)
    -- input some states of the whole world,
    -- modify the whole world because of the side effects,
    -- then return the new world.
    

    then f is pure again. We define a parametrized data type type IO a = RealWorld -> (a, RealWorld), so we don’t need to type RealWorld so many times, and can just write

    f :: Int -> IO Int
    

    To the programmer, handling a RealWorld directly is too dangerous—in particular, if a programmer gets their hands on a value of type RealWorld, they might try to copy it, which is basically impossible. (Think of trying to copy the entire filesystem, for example. Where would you put it?) Therefore, our definition of IO encapsulates the states of the whole world as well.

    Composition of "impure" functions

    These impure functions are useless if we can’t chain them together. Consider

    getLine     :: IO String            ~            RealWorld -> (String, RealWorld)
    getContents :: String -> IO String  ~  String -> RealWorld -> (String, RealWorld)
    putStrLn    :: String -> IO ()      ~  String -> RealWorld -> ((),     RealWorld)
    

    We want to

    • get a filename from the console,
    • read that file, and
    • print that file’s contents to the console.

    How would we do it if we could access the real world states?

    printFile :: RealWorld -> ((), RealWorld)
    printFile world0 = let (filename, world1) = getLine world0
                           (contents, world2) = (getContents filename) world1 
                       in  (putStrLn contents) world2 -- results in ((), world3)
    

    We see a pattern here. The functions are called like this:

    ...
    (<result-of-f>, worldY) = f               worldX
    (<result-of-g>, worldZ) = g <result-of-f> worldY
    ...
    

    So we could define an operator ~~~ to bind them:

    (~~~) :: (IO b) -> (b -> IO c) -> IO c
    
    (~~~) ::      (RealWorld -> (b,   RealWorld))
          ->                    (b -> RealWorld -> (c, RealWorld))
          ->      (RealWorld                    -> (c, RealWorld))
    (f ~~~ g) worldX = let (resF, worldY) = f worldX
                       in g resF worldY
    

    then we could simply write

    printFile = getLine ~~~ getContents ~~~ putStrLn
    

    without touching the real world.

    "Impurification"

    Now suppose we want to make the file content uppercase as well. Uppercasing is a pure function

    upperCase :: String -> String
    

    But to make it into the real world, it has to return an IO String. It is easy to lift such a function:

    impureUpperCase :: String -> RealWorld -> (String, RealWorld)
    impureUpperCase str world = (upperCase str, world)
    

    This can be generalized:

    impurify :: a -> IO a
    
    impurify :: a -> RealWorld -> (a, RealWorld)
    impurify a world = (a, world)
    

    so that impureUpperCase = impurify . upperCase, and we can write

    printUpperCaseFile = 
        getLine ~~~ getContents ~~~ (impurify . upperCase) ~~~ putStrLn
    

    (Note: Normally we write getLine ~~~ getContents ~~~ (putStrLn . upperCase))

    We were working with monads all along

    Now let’s see what we’ve done:

    1. We defined an operator (~~~) :: IO b -> (b -> IO c) -> IO c which chains two impure functions together
    2. We defined a function impurify :: a -> IO a which converts a pure value to impure.

    Now we make the identification (>>=) = (~~~) and return = impurify, and see? We’ve got a monad.


    Technical note

    To ensure it’s really a monad, there’s still a few axioms which need to be checked too:

    1. return a >>= f = f a

       impurify a                =  (\world -> (a, world))
      (impurify a ~~~ f) worldX  =  let (resF, worldY) = (\world -> (a, world )) worldX 
                                    in f resF worldY
                                 =  let (resF, worldY) =            (a, worldX)       
                                    in f resF worldY
                                 =  f a worldX
      
    2. f >>= return = f

      (f ~~~ impurify) worldX  =  let (resF, worldY) = f worldX 
                                  in impurify resF worldY
                               =  let (resF, worldY) = f worldX      
                                  in (resF, worldY)
                               =  f worldX
      
    3. f >>= (\x -> g x >>= h) = (f >>= g) >>= h

      Left as exercise.

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

Sidebar

Related Questions

Could anyone could point me to some code/give me ideas on how to create
C#: Could anyone give me good example on how anchoring controls at runtime is
Could anyone explain with some examples when it is better to call functions by
ASP.NET: Could anyone provide some C# paging codes in Repeater or ListView controls for
Could anyone point me to an example implementation of a HOT Queue or give
Could any one give an explanation on how a DHT works? Nothing too heavy,
Could anyone recommend a good BAML Decompiler / Viewer besides BAML Viewer plugin for
Could anyone suggest a good packet sniffer class for c++? Looking for a easy
Could anyone explain me why: function doAjax() { var xmlHttpReq = false; try {
Could anyone can tell me where I can find full ASP.NET MVC beta documentation?

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.