### Register Resource with IO Actions (Haskell) Source: https://context7.com/haskell-effectful/resourcet-effectful/llms.txt Shows how to use the `allocate` function to register a resource acquired and released via standard IO actions. The cleanup action is guaranteed to execute when the resource scope ends or if manually released. ```haskell import Effectful import Effectful.Resource import Control.Monad.Trans.Resource (allocate, ReleaseKey) import qualified System.IO as IO processFile :: (IOE :> es, Resource :> es) => FilePath -> Eff es String processFile path = do -- allocate takes IO actions for both acquire and release (key, handle) <- allocate (IO.openFile path IO.ReadMode) -- acquire: IO action IO.hClose -- release: IO action contents <- liftIO $ IO.hGetContents handle -- Optional: manually release early -- release key pure contents ``` -------------------------------- ### Allocate Resource Without Return Value (Haskell) Source: https://context7.com/haskell-effectful/resourcet-effectful/llms.txt Presents `allocate_` and `allocateEff_`, simplified functions for registering resources when only the release key is needed, not the acquired resource value itself. Useful for managing side effects during acquisition and release. ```haskell import Effectful import Effectful.Resource import Control.Monad.Trans.Resource (allocate_) example :: IO () example = runEff . runResource $ do -- allocate_ when you only care about the side effect key <- allocate_ (putStrLn "Acquiring resource...") (putStrLn "Releasing resource...") liftIO $ putStrLn "Doing work..." -- Release action runs automatically ``` -------------------------------- ### Register Cleanup Actions (Haskell) Source: https://context7.com/haskell-effectful/resourcet-effectful/llms.txt Demonstrates `register` and `registerEff` for adding cleanup actions without an explicit acquisition step. This is useful for managing externally acquired resources, ensuring they are properly cleaned up when the scope ends. ```haskell import Effectful import Effectful.Resource import Control.Monad.Trans.Resource (register, ReleaseKey) -- registerEff for Eff monad cleanup actions logAndCleanup :: (IOE :> es, Resource :> es) => String -> Eff es ReleaseKey logAndCleanup resource = registerEff $ do liftIO $ putStrLn $ "Cleaning up: " ++ resource example :: IO () example = runEff . runResource $ do -- Register cleanup for externally managed resources key1 <- register $ putStrLn "IO cleanup action" key2 <- registerEff $ liftIO $ putStrLn "Eff cleanup action" liftIO $ putStrLn "Working..." -- Both cleanup actions run when scope ends (in reverse order) ``` -------------------------------- ### Run Resource Effect with Automatic Cleanup (Haskell) Source: https://context7.com/haskell-effectful/resourcet-effectful/llms.txt Demonstrates the primary handler `runResource` for executing computations with resource management. It ensures all registered resources are cleaned up upon completion or exception, using standard IO actions for acquisition and release. ```haskell import Effectful import Effectful.Resource import qualified System.IO as IO -- Basic usage: open a file handle with automatic cleanup main :: IO () main = runEff . runResource $ do -- Resources are automatically freed when runResource completes (releaseKey, handle) <- allocate (IO.openFile "data.txt" IO.ReadMode) IO.hClose contents <- liftIO $ IO.hGetContents handle liftIO $ putStrLn contents -- handle is automatically closed here, even if an exception occurs ``` -------------------------------- ### Register Resource with Effectful Actions (Haskell) Source: https://context7.com/haskell-effectful/resourcet-effectful/llms.txt Illustrates `allocateEff`, a variant of `allocate` that accepts `Eff` monad actions for acquisition and release. This allows cleanup actions to leverage the full effect stack, with the release action running in a cloned environment. ```haskell import Effectful import Effectful.Resource data Connection = Connection { connId :: Int } openConnection :: (IOE :> es) => Eff es Connection openConnection = liftIO $ do putStrLn "Opening connection..." pure $ Connection 1 closeConnection :: (IOE :> es) => Connection -> Eff es () closeConnection conn = liftIO $ putStrLn $ "Closing connection " ++ show (connId conn) example :: IO () example = runEff . runResource $ do -- Use allocateEff when acquire/release need Eff capabilities (key, conn) <- allocateEff openConnection -- acquire: Eff es a closeConnection -- release: a -> Eff es () liftIO $ putStrLn $ "Using connection " ++ show (connId conn) -- closeConnection is automatically called when scope ends ``` -------------------------------- ### MonadResource Instance for resourcet Compatibility Source: https://context7.com/haskell-effectful/resourcet-effectful/llms.txt Provides a `MonadResource` instance for `Eff`, enabling seamless use of existing resourcet-based code and libraries. This allows direct interoperability with libraries expecting the `MonadResource` typeclass. ```haskell import Effectful import Effectful.Resource import Control.Monad.Trans.Resource (MonadResource, liftResourceT, resourceForkIO) import qualified Control.Monad.Trans.Resource as R -- Use any MonadResource function with Eff useExistingResourcetCode :: (IOE :> es, Resource :> es) => Eff es () useExistingResourcetCode = do -- Works with any library expecting MonadResource (key, handle) <- R.allocate (putStrLn "Acquiring" >> pure ()) (\ _ -> putStrLn "Releasing") -- liftResourceT works for ResourceT actions liftResourceT $ R.ResourceT $ \ _ -> putStrLn "Inside ResourceT" ``` -------------------------------- ### Manually Release Resource with release and releaseEff Source: https://context7.com/haskell-effectful/resourcet-effectful/llms.txt Allows manual triggering of resource cleanup actions before the scope ends. The release action is removed from the cleanup queue after execution. This is useful for freeing resources early when they are no longer needed. ```haskell import Effectful import Effectful.Resource import Control.Monad.Trans.Resource (release, allocate) import qualified System.IO as IO example :: IO () example = runEff . runResource $ do (key1, h1) <- allocate (IO.openFile "a.txt" IO.ReadMode) IO.hClose (key2, h2) <- allocate (IO.openFile "b.txt" IO.ReadMode) IO.hClose -- Process first file content1 <- liftIO $ IO.hGetContents h1 liftIO $ putStrLn content1 -- Release first handle early to free resources release key1 -- Continue with second file content2 <- liftIO $ IO.hGetContents h2 liftIO $ putStrLn content2 -- h2 is released automatically at scope end ``` -------------------------------- ### Remove Resource from Cleanup with unprotect and unprotectEff Source: https://context7.com/haskell-effectful/resourcet-effectful/llms.txt Removes a resource from automatic cleanup management, returning the release action for manual control. Returns `Nothing` if the resource was already released. This enables transferring ownership of resource management. ```haskell import Effectful import Effectful.Resource import Control.Monad.Trans.Resource (allocate, unprotect) transferOwnership :: IO () transferOwnership = runEff . runResource $ do (key, value) <- allocate (pure "managed resource") (\v -> putStrLn $ "Would clean: " ++ v) -- Remove from automatic cleanup mAction <- unprotect key case mAction of Nothing -> liftIO $ putStrLn "Already released" Just action -> do liftIO $ putStrLn "Resource transferred, manual cleanup needed" -- action can be called later manually: action ``` -------------------------------- ### Run with Existing Internal State using runInternalState Source: https://context7.com/haskell-effectful/resourcet-effectful/llms.txt Executes a Resource effect computation using a pre-existing internal state. The state is not closed at the end, allowing for resource state sharing across multiple computations. This is useful for managing resources across different parts of an application. ```haskell import Effectful import Effectful.Resource import Control.Monad.Trans.Resource (createInternalState, closeInternalState) example :: IO () example = runEff $ do -- Create state manually istate <- liftIO createInternalState -- Run multiple computations sharing the same resource state runInternalState istate $ do liftIO $ putStrLn "First computation" runInternalState istate $ do liftIO $ putStrLn "Second computation" -- Manually close when done liftIO $ closeInternalState istate ``` -------------------------------- ### Access Internal Resource State with getInternalState Source: https://context7.com/haskell-effectful/resourcet-effectful/llms.txt Retrieves the internal state of the Resource effect for advanced use cases, such as passing it to libraries that require direct access to this state. This is typically used for deep integration or debugging. ```haskell import Effectful import Effectful.Resource import Control.Monad.Trans.Resource (InternalState) getState :: (Resource :> es) => Eff es InternalState getState = getInternalState example :: IO () example = runEff . runResource $ do istate <- getInternalState -- Use istate with libraries that require InternalState directly liftIO $ putStrLn "Got internal state" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.