abduce |
Forks the computation. The rest of the do block is executed in a new thread, while for the current thread, abduce acts as stop (fails). It is the basis for building other concurrency primitives. |
main = keep $ do (do liftIO $ putStrLn "Main Thread: Before abduce" abduce liftIO $ putStrLn "Child Thread: After abduce" ) <|> (do liftIO $ putStrLn "Main Thread: Alternative path" ) liftIO $ putStrLn "Both threads: End of block"
|
async |
Executes an IO action in a new background thread and returns its result to the TransIO flow when it finishes. Built on abduce. |
main = keep $ do liftIO $ putStrLn "Starting asynchronous task..." result <- async $ do liftIO $ threadDelay 2000000 -- Simulate work return "Task completed" liftIO $ putStrLn $ "Result received: " ++ result
|
for |
Processes a list of elements sequentially in a single thread. Although the loop body can launch asynchronous tasks, for schedules each iteration in order. Equivalent to threads 0 . choose. |
main = keep $ do n <- for [1..3 :: Int] liftIO $ print n
|
choose |
Processes a list of elements in parallel, attempting to execute each element in a separate thread. The order of execution is not guaranteed. |
main = keep $ threads 2 $ do -- Only 2 threads will be used for printing n <- choose [1..5 :: Int] liftIO $ print n
|
threads |
Limits the maximum number of threads that can be created for a computation. threads 0 forces sequential execution in a single thread. |
main = keep $ threads 1 $ do -- Only one print will execute at a time n <- choose [1..5 :: Int] liftIO $ print n
|
await |
Waits for a TransIO computation (and all child threads it has generated) to complete fully, collecting all results into a list. |
main = keep $ do results <- await $ (async $ return "A") <|> (async $ return "B") liftIO $ print results -- Prints ["A", "B"] or ["B", "A"]
|
<*> |
Applicative operator. Executes the left and right computations in parallel and combines their results. |
main = keep $ do res <- (,) <$> (async $ return "Hello") <*> (async $ return "World") liftIO $ print res -- Prints ("Hello", "World")
|
<> |
Semigroup operator. Executes the left and right computations in parallel and concatenates their results (if the type allows it). |
main = keep $ do res <- (async $ return "Part1") <> (async $ return "Part2") liftIO $ putStrLn res -- Prints "Part1Part2"
|
+ (Num) |
Overloaded arithmetic operator for TransIO. Executes the computations in parallel and sums their results. |
main = keep $ do res <- (async $ return 5) + (async $ return 3) liftIO $ print res -- Prints 8
|