As the GHC garbage / build-time collector knows that it can create an `inplace 'array

for instance

main = do
  let ls = [0..10000000]
  print ls

This will create an inplace array using O (1) memory.

The following editing causes the program to exit the memory at run time.

main = do
  let ls = [0..10000000]
  print ls
  print ls

lsin this case, you must save it in memory for printing again. In fact, it would be more efficient to use memory to recalculate the inplace array than to try to keep it in place. Nonetheless. My real question is: "How and when does the GHC communicate with a runtime system that lscan be destroyed at creation time O (1) times?" I understand that a viability analysis can find this information, I'm just wondering where this information is used. Is it the garbage collector that passed this information? Is this compiled somehow? (If I look at the compiled kernel from GHC, then in both examples it is used eftInt, therefore, if it is an artifact of the compiler, then this should happen at a deeper level).

edit: , . , , , GC, - . - , , . , , , , cmm?

edit2: , GC , ls . GC, , .. GC . , , () , () , 100%

+4
2

, , , , , , .

, , , , , , . . , , , .

  • , main.
  • 2.

, :

  • , .
  • , .

, main. " " GC , main, , .

foo = "Hi!"
main = print "Bye!"

foo main, , , main, .

, :

foo = "Hi!"
bar = "Bye!"
main = print foo >> print bar

foo main, . main , , ,

(primitive operation that prints out "Hi!") >> print bar

, foo , !

, "!", " "

print bar

WHNF , ,

(primitive operation to print "Bye!")

bar . " !" .


:

main = do
  let ls = [0..10000000]
  print ls

main =
  let ls = [0..10000000]
  in print ls

. " " - , in . ls print . , print, [Integer], ( -, ).

print xs = case xs of
  [] -> return ()
  (y:ys) = printInteger y >> print ys

, main ( ? ?), print ls. ls, ls WHNF. y:ys, , print ls print Integer y >> print ys, y ls ys thunk, ls. , ls ! ! print , , , .

,

main =
  let ls = ...
  in print ls >> print ls

, (print ls).

(printInteger y >> print ys) >> print ls

, , ls. , , , , .

- , IO. , [Int], - .

countup m n = if m == n then [] else m : countup (m+1)
main = countup 0 1000

, , . " " whatPrint. :

  • whatPrint main.
  • whatPrint ? , . , , printNow : whatPrint'.
  • printNow .
  • whatPrint whatPrint'
  • 1.

" " - whatPrint.

; IO. (). >>=, return IO IO.

data IO :: * -> * where
  Return :: a -> IO a
  Bind :: IO a -> (a -> IO b) -> IO b
  PrintInt :: Int -> IO ()
  ReadInt :: IO Int
  ...

whatShouldIDo main, . , , . .

+3

ls , . .

, , , . ls , print. print , ls, .

GHC , , . - , , , .

+9

All Articles