Why does Haskell's “do nothing” function, id, consume tons of memory?

前端 未结 1 1401
南方客
南方客 2021-02-01 11:43

Haskell has an identity function which returns the input unchanged. The definition is simple:

id :: a -> a
id x = x

So for fun, this should

相关标签:
1条回答
  • 2021-02-01 12:24

    We know the type of id,

    id :: a -> a
    

    And when we specialize this for id id, the left copy of id has type:

    id :: (a -> a) -> (a -> a)
    

    And then when you specialize this again for the leftmost id in id id id, you get:

    id :: ((a -> a) -> (a -> a)) -> ((a -> a) -> (a -> a))
    

    So you see each id you add, the type signature of the leftmost id is twice as large.

    Note that types are deleted during compilation, so this will only take up memory in GHC. It won't take up memory in your program.

    0 讨论(0)
提交回复
热议问题