### Haskell: Use MonadTime with Eff and Time Effect Source: https://context7.com/haskell-effectful/monad-time-effectful/llms.txt Demonstrates how to use the MonadTime instance for Eff with the Time effect. This allows generic functions constrained by MonadTime to work seamlessly within an effectful context. It shows scheduling a task based on current time and running it within an effectful computation. ```haskell {-# LANGUAGE DataKinds #} {-# LANGUAGE TypeOperators #} {-# LANGUAGE FlexibleContexts #} import Effectful import Effectful.Time import Control.Monad.Time (MonadTime, currentTime, monotonicTime) import Data.Time (UTCTime, addUTCTime, NominalDiffTime) -- Generic function using MonadTime constraint -- Works with any MonadTime instance, including Eff with Time effect scheduleTask :: MonadTime m => NominalDiffTime -> m UTCTime scheduleTask delay = do now <- currentTime pure $ addUTCTime delay now -- Use in effectful context example :: IO UTCTime example = runEff . runTime $ do -- Schedule task 3600 seconds (1 hour) from now scheduledTime <- scheduleTask 3600 liftIO $ putStrLn $ "Task scheduled for: " ++ show scheduledTime pure scheduledTime ``` -------------------------------- ### runFrozenTime Handler for Testing in Haskell Source: https://context7.com/haskell-effectful/monad-time-effectful/llms.txt The runFrozenTime handler fixes the currentTime operation to a specific timestamp, useful for deterministic testing. Monotonic time remains functional, allowing for real elapsed time measurements. ```haskell #!/usr/bin/env runghc {-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeOperators #-} import Effectful import Effectful.Time import Control.Monad.Time (currentTime, monotonicTime) import Data.Time (UTCTime) import Data.Time.Clock (getCurrentTime) -- Test with frozen time testFrozenTime :: UTCTime -> IO () testFrozenTime frozenTimestamp = runEff . runFrozenTime frozenTimestamp $ do -- This will always return the frozen timestamp time1 <- currentTime liftIO $ putStrLn $ "First call: " ++ show time1 -- Even after delay, currentTime returns frozen value start <- monotonicTime liftIO $ putStrLn "Simulating work..." end <- monotonicTime time2 <- currentTime liftIO $ putStrLn $ "Second call: " ++ show time2 liftIO $ putStrLn $ "Times equal: " ++ show (time1 == time2) -- True liftIO $ putStrLn $ "Monotonic elapsed: " ++ show (end - start) -- Real elapsed time -- Usage in tests: -- frozenTime <- getCurrentTime -- testFrozenTime frozenTime ``` -------------------------------- ### Time Effect Operations in Haskell Source: https://context7.com/haskell-effectful/monad-time-effectful/llms.txt Demonstrates how to use the Time effect for measuring durations and logging with timestamps. It relies on the MonadTime typeclass and the Effectful library. ```haskell #!/usr/bin/env runghc {-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeOperators #-} import Effectful import Effectful.Time import Control.Monad.Time (currentTime, monotonicTime) import Data.Time (UTCTime, diffUTCTime) -- Define a computation that uses the Time effect measureDuration :: Time :> es => Eff es a -> Eff es (a, Double) measureDuration action = do start <- monotonicTime result <- action end <- monotonicTime pure (result, end - start) -- Get current timestamp logWithTimestamp :: Time :> es => String -> Eff es (UTCTime, String) logWithTimestamp msg = do now <- currentTime pure (now, msg) ``` -------------------------------- ### runTime Handler for Live Time Operations in Haskell Source: https://context7.com/haskell-effectful/monad-time-effectful/llms.txt The runTime handler executes Time effect operations using actual IO. It uses Data.Time.getCurrentTime for UTC time and GHC.Clock.getMonotonicTime for monotonic readings, suitable for production environments. ```haskell #!/usr/bin/env runghc {-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeOperators #-} import Effectful import Effectful.Time import Control.Monad.Time (currentTime, monotonicTime) -- Run a program with real time operations main :: IO () main = runEff . runTime $ do now <- currentTime liftIO $ putStrLn $ "Current time: " ++ show now start <- monotonicTime -- Perform some work liftIO $ putStrLn "Working..." end <- monotonicTime liftIO $ putStrLn $ "Elapsed: " ++ show (end - start) ++ " seconds" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.