21

I have the following function:

get :: Chars -> IO Chars
get cs = do
    char <- getChar
    let (dats, idx) = (curData cs, curIndex cs)
    let (x,y:xs) = splitAt idx dats
    let replacement = x ++ (ord char) : xs
    return $ Chars replacement idx

and I'd like to get a Chars value out of it, not an IO action. I have no idea how to do this, or if it is even possible.

The Chars type is basically just a container:

data Chars = Chars {
               curData  :: [Int],
               curIndex :: Int
               -- etc.
             }

The specifics aren't that important, I just want to know if there's a way for this function to return a Chars instead of an IO Chars.

If not, how do I pass this as an argument to a function that takes a Chars? I'm kind of new to Haskell I/O, but I don't think I want all of my functions that take Chars as arguments to instead have to take IO Chars as arguments, and then extract and repackage them. It seems unnecessary.

Thanks!

atravers
  • 455
  • 4
  • 8
Benjamin Kovach
  • 3,190
  • 1
  • 24
  • 38
  • 6
    Do we not have a canonical "How do I get an `a` out of an `IO a`?" question to mark this a duplicate of yet? – Daniel Wagner Jul 13 '12 at 12:08
  • It isn't canonical (being marked as a duplicate itself), but it's title and age would make it a suitable candidate: https://stackoverflow.com/q/16773528 . – atravers Sep 20 '21 at 22:07

2 Answers2

29

You can't, because that would violate referential transparency.

IO in Haskell is made this way exactly to distinguish between actions whose result and effects may vary depending on the interaction with the environment/user and pure functions whose results are not going to change when you call them with the same input parameters.

In order to pass the result to a pure function taking a Chars in input you have to call your IO action into another IO action, bind the result with the <- operator to a variable and pass it to your pure function. Pseudocode example:

myPureFunction :: Chars -> ...

otherAction :: Chars -> IO ()
otherAction cs = do
  myChars <- get cs
  let pureResult = myPureFunction myChars
  ...

If you're new to IO in haskell, you may wish to have a look at the Input and Output chapters in Learn You a Haskell for a Great Good! and Real World Haskell.

There is actually a way to simply get a pure value out of an IO action, but in your case you shouldn't do it, as you're interacting with the environment: the unsafe way is ok only when you can guarantee you're not violating referential transparency.

Riccardo T.
  • 8,907
  • 5
  • 38
  • 78
  • Thank you! I think this makes sense now. I've read all of LYAH, but it's been a while since I've done anything with IO so I wasn't sure exactly how to handle this. So, I have a few functions that take a `Chars` and return a `Chars` but don't touch IO. Should I modify those to return an `IO Chars` just for the sake of simplicity? I'm hesitant to change everything around like that, but the program should theoretically still work given that's the case. – Benjamin Kovach Jul 13 '12 at 16:19
  • 2
    No, you shouldn't give up on referential transparency guaranteed by the types when you can. So let those functions be, do not make them return something like `IO a`. – Riccardo T. Jul 13 '12 at 16:50
12

It's impossible (I lie, there is an extremely unsafe way to cheat your way out of it).

The point is that if any I/O is performed, the behaviour and result of your programme may not depend only on explicit arguments to the used functions, thus that must be declared in the type by having it IO something.

You use the result of an IO a action in a pure function by binding the result in main (or something called from main) and then applying the pure function, binding the result in a let,

cs ::Chars
cs = undefined

main = do
  chars <- get cs
  let result = pureFunction chars
  print result

or, if the function you want to apply to chars has type Chars -> IO b

main = do
    chars <- get cs
    doSomething chars
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431