Skip to content

Instantly share code, notes, and snippets.

@cideM
Created July 11, 2025 09:02
Show Gist options
  • Select an option

  • Save cideM/68f0be9aa4e6699a8f71d7ea81ae3490 to your computer and use it in GitHub Desktop.

Select an option

Save cideM/68f0be9aa4e6699a8f71d7ea81ae3490 to your computer and use it in GitHub Desktop.
UnliftIO Blog Post Nix
#!/usr/bin/env nix-shell
#! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz
#! nix-shell -p "haskell.packages.ghc96.ghcWithPackages (pkgs: with pkgs; [unliftio])" ghcid
#! nix-shell -i ghcid
import Control.Monad.IO.Unlift
import Control.Monad.Trans.Reader
foo :: (String -> IO ()) -> IO ()
foo func = func "test"
foo2 :: (String -> ReaderT String IO ())
-> ReaderT String IO ()
foo2 func =
askUnliftIO >>=
\u -> liftIO $ foo (unliftIO u . func)
-- ^^^ note that we're still calling
-- the original `foo`. We didn't have to
-- reimplement anything
-- Or alternatively and more concisely
foo2' func =
withRunInIO $ \runInIO -> foo (runInIO . func)
main :: IO ()
main = print "HI"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment