Skip to content

Instantly share code, notes, and snippets.

@xieyuschen
Created June 6, 2023 11:31
Show Gist options
  • Save xieyuschen/681df286c665b50c4afe24e8f5abeded to your computer and use it in GitHub Desktop.
Save xieyuschen/681df286c665b50c4afe24e8f5abeded to your computer and use it in GitHub Desktop.
Haskell: difference between return/pure and lift

Haskell: difference between return/pure and lift

In the following code snippet, return/pure cannot work as it constructs StateT s IO (IO ()), not StateT s IO ().

return/pure will construct the value based on the currect monad in context, which is StateT s IO. lift will upgrade the current monad(IO) to the new monad through the transformer.

printState :: Show s => StateT s IO ()
printState = do
  state <- get
  -- print state fails due to IO () doesn't match to StateT s
  -- so you could liftIO, lift also can work due to the StateT
  -- pure/return cannot work as, due to you `StateT s IO` is the new monad, 
  -- and you want to return the IO () to it
  --   Expected: StateT s IO ()
  -- Actual: StateT s IO (IO ())
  -- lift means, use the current monad to construct the new monad
  lift $ print state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment