| Safe Haskell | Safe-Inferred |
|---|
Lessons.Lesson06
Description
Notes taken by Emilija Rimšelytė
Synopsis
- main :: IO ()
- main' :: IO String
- m :: IO ()
- queryName :: IO String
- aGame :: IO ()
- pureF :: String -> IO String
- pureF' :: IO String
- action :: IO ()
- threading :: IO ()
- actionC :: Chan String -> IO ()
- threading' :: IO ()
- actionA :: String -> IO String
- threading'' :: IO String
- transfer :: TVar Integer -> TVar Integer -> Integer -> STM ()
- runTx :: IO ()
Documentation
Entry point of a Haskell program.
putStrLn performs an effect: it prints to the terminal.
In Haskell, effects live in IO — they’re not just values, they’re actions.
Reads a line from the terminal. This is an IO action — it doesn’t give you a String directly, it gives you a computation that will produce a String when run.
Reads and prints a line. Demonstrates how to extract a value from IO using '<-' inside a 'do' block. You can't escape IO in pure code — but inside 'do', you can unwrap and use.
queryName :: IO String Source #
Asks the user for their name. This is interactive, so it lives in IO. Side effects like printing and reading are sequenced here.
Combines IO (input/output) with pure logic. The greeting is pure, but getting the name is not.
pureF :: String -> IO String Source #
This won’t compile, as you can see, IO and String can’t mix so free. pureF :: String -> String pureF a = queryName ++ a
Mixes IO with pure string manipulation. You extract the name, then glue it tight, But wrap it in IO to make it right.
Waits 10 seconds, then prints Hi. Demonstrates time delay — an effect, so it’s in IO.
Starts two concurrent threads using forkIO.
Each runs action independently.
Threads in Haskell are lightweight — managed by the runtime, not the OS.
actionC :: Chan String -> IO () Source #
Channels let threads communicate safely. Writes to the channel, after a pause, Communication between threads — that’s the cause.
threading' :: IO () Source #
Demonstrates inter-thread communication. Two threads write to the same channel. Main thread reads both messages and prints them.
actionA :: String -> IO String Source #
Delayed computation that returns a string.
Used with async to run in parallel.
threading'' :: IO String Source #
Runs two async actions concurrently. Waits for both to finish, then combines results.