-- taken from https://gitlab.haskell.org/ghc/ghc/-/blob/c94aaacd4c4e31a2fe2cb8dadcdd14c7621d27c5/libraries/base/tests/lazySTexamples.hsmodule LazyST whereimport Data.STRef.Lazyimport Control.Monad.ST.Lazy as Limport Control.Monad.ST as Simport qualified Data.STRef as Simport Data.Function (fix)--import System.IO (hPutStrLn, stderr)--import Debug.Trace (trace)import System.IO.Unsafe(unsafePerformIO)-- Make a trace that prints on stdout, otherwise example5-- mixes printing on stderr and stdout.trace :: String -> a -> atrace s a = unsafePerformIO $ do putStrLn s return a-- The following implements `fix` using lazy `ST`. It is based on code-- by Oleg Kiselyov (source: http://okmij.org/ftp/Haskell/Fix.hs) which is-- in the public domain according to the main page (http://okmij.org/ftp/).fact :: (Int -> Int) -> Int -> Intfact self 0 = 1fact self n = n * self (pred n)-- Test liftM style (Oleg's original style)fix1 :: (a -> a) -> afix1 f = L.runST $ do wrap <- newSTRef (error "black hole") let aux = readSTRef wrap >>= (\x -> x >>= pure . f) writeSTRef wrap aux aux-- Test fmap stylefix2 :: (a -> a) -> afix2 f = L.runST $ do wrap <- newSTRef (error "black hole") let aux = readSTRef wrap >>= \x -> f <$> x writeSTRef wrap aux aux-- The following examples are by Albert Y. C. Lai, and included (under the-- GHC license) with his permission:-- https://mail.haskell.org/pipermail/haskell-cafe/2017-January/126182.htmlexample1 :: [Int]example1 = L.runST $ do v <- strictToLazyST (S.newSTRef 0) fix (\loop -> do n <- strictToLazyST (do n <- S.readSTRef v S.writeSTRef v (n+1) return n ) ns <- loop return (n : ns))example2 :: [Int]example2 = L.runST $ do v <- strictToLazyST (S.newSTRef 0) sequence (repeat (strictToLazyST (do n <- S.readSTRef v S.writeSTRef v (n+1) return n )))example3 :: L.ST s [Integer]example3 = do r <- newSTRef 0 let loop = do x <- readSTRef r writeSTRef r $ x + 1 xs <- loop writeSTRef r $ x + 2 return $ x : xs loopexample4 :: L.ST s [Integer]example4 = do r <- newSTRef 0 let loop = do x <- readSTRef r writeSTRef r $ x + 1 xs <- loop error "this line is dead code" return $ x : xs loopstar n s = trace ("<" ++ s ++ show n ++ ">") (return ())-- Albert called this "Sprinkle sprinkle little stars, how-- I wonder when you are"example5 :: L.ST s [Integer]example5 = do star 0 "init begin" r <- newSTRef 0 star 0 "init end" let loop n = do star n "A" x <- readSTRef r star n "B" writeSTRef r $ x + 1 star n "C" xs <- loop (n+1) star n "D" writeSTRef r $ x + 2 star n "E" return $ x : xs loop 0main :: IO ()main = do print $ fix fact 5 print $ fix fact 6 print $ take 5 example1 print $ take 5 example2 print $ take 10 (L.runST example3) print $ take 10 (L.runST example4) print $ take 5 (L.runST example5) print $ S.runST $ do ref <- lazyToStrictST $ newSTRef 0 let action = lazyToStrictST $ modifySTRef ref (+ 1) action action lazyToStrictST $ readSTRef ref