Export `Monoid(..)`/`Foldable(..)`/`Traversable(..)` from Prelude
[ghc.git] / testsuite / tests / ghci.debugger / mdo.hs
1 import Control.Monad.Fix
2 import Data.IORef
3 import Prelude hiding (traverse)
4
5 data N a = N (IORef Bool, N a, a, N a)
6
7 newNode :: N a -> a -> N a -> IO (N a)
8 newNode b c f = do v <- newIORef False
9 return (N (v, b, c, f))
10
11 ll = mdo n0 <- newNode n3 0 n1
12 n1 <- newNode n0 1 n2
13 n2 <- newNode n1 2 n3
14 n3 <- newNode n2 3 n0
15 return n0
16
17 data Dir = F | B deriving Eq
18
19 traverse :: Dir -> N a -> IO [a]
20 traverse d (N (v, b, i, f)) =
21 do visited <- readIORef v
22 if visited
23 then return []
24 else do writeIORef v True
25 let next = if d == F then f else b
26 is <- traverse d next
27 return (i:is)
28
29 l2dll :: [a] -> IO (N a)
30 l2dll (x:xs) = mdo c <- newNode l x f
31 (f, l) <- l2dll' c xs
32 return c
33
34 l2dll' :: N a -> [a] -> IO (N a, N a)
35 l2dll' p [] = return (p, p)
36 l2dll' p (x:xs) = mdo c <- newNode p x f
37 (f, l) <- l2dll' c xs
38 return (c, l)