1 {-# LANGUAGE MagicHash, BangPatterns #-}
7 import System.Environment
13 args <- map length `fmap` getArgs
15 (l ++ l2 ++ args) `deepseq` return ()
17 let x = l ++ l2 ++ args
19 putStrLn "ghc-heap-view-demo"
21 putStrLn "Here are a four different lists, where the first three are already evaluated."
22 putStrLn "The first one, l, was defined as a top level constant as "
23 putStrLn "> l = [1,2,3]"
24 putStrLn $ "and is now found at " ++ show (asBox l) ++ " (where the /2 is the pointer tag information) and fully evaluated:"
25 getClosureData l >>= printInd
26 putStrLn $ "The second one, l2, is locally defined"
27 putStrLn "> let l2 = 4:l"
28 putStrLn $ "and now found at " ++ show (asBox l2) ++ ". See how the cons-cell references l!"
29 getClosureData l2 >>= printInd
30 putStrLn "And the binding"
31 putStrLn "> args <- map length `fmap` getArgs"
32 putStrLn $ "gives us at " ++ show (asBox args) ++ " a static, but at compile time unknown list:"
33 getClosureData args >>= printInd
34 putStrLn $ "And now we have, at " ++ show (asBox x) ++ ", the concatenation of them, but unevaluated:"
35 putStrLn "> let x = l ++ l2 ++ args"
36 putStrLn "The thunk keeps a reference to l2 and args, but not l, as that is at a static address, unless you are running this in GHCi:"
37 getClosureData x >>= printInd
40 putStrLn "Now to some more closure types. m and m' locally bound of type the unboxed type Int#, with values 42 resp. 23."
41 putStrLn "> let f = \\x n -> take (I# m + I# x) n ++ args"
42 putStrLn " t = f m' l2"
43 let !(I# m) = length args + 42
44 let !(I# m') = length args + 23
45 let f = \x n -> take (I# m + I# x) n ++ args
47 putStrLn $ "So here is (" ++ show (asBox f) ++ "), referencing its free variables args and 42:"
48 getClosureData f >>= printInd
49 putStrLn "And t is a thunk that applies f (also referenced here) to an unboxed value (23) and l2:"
50 getClosureData t >>= printInd
53 putStrLn "Lastly, here is the standard example for self reference:"
54 putStrLn "> let x = id (:) () x"
56 putStrLn $ "This is what x (" ++ show (asBox x) ++ ") looks like, at least without -O:"
57 getClosureData x >>= printInd
59 putStrLn $ "So it is unevaluated. Let us evaluate it using seq. Now we have, still at " ++ show (asBox x) ++ ":"
60 getClosureData x >>= printInd
61 target <- indirectee `fmap` getClosureData x
62 putStrLn $ "The thunk was replaced by an indirection. If we look at the target, " ++ show target ++ ", we see that it is a newly created cons-cell referencing the original location of x:"
63 getBoxedClosureData target >>= printInd
65 putStrLn $ "After running the garbage collector (performGC), we find that the address of x is now " ++ show (asBox x) ++ " and that the self-reference is without indirections:"
66 getClosureData x >>= printInd
68 printInd :: Show a => a -> IO ()
69 printInd x = putStrLn $ " " ++ show x
71 recurse :: Int -> Box -> IO ()
73 where go i b = if i >= m then return () else do
74 putStrLn $ ind ++ show b
75 c <- getBoxedClosureData b
76 putStrLn $ ind ++ show c
77 mapM_ (go (succ i)) (allPtrs c)
79 ind = concat $ replicate i " "