3 ms·
I was bothered by him equating closures and thunks. While they are kind of similar (a thunk is almost like a closure with no arguments), they aren't really the
by Xichekolas 17y ago
I was bothered by him equating closures and thunks. While they are kind of similar (a thunk is almost like a closure with no arguments), they aren't really the same thing.
- eru 17y agoCareful lying may aid in explaining. [1] The author knows the difference. [1] Can anybody find the source of this quote?
- jsyedidia 17y agoThe author feels that this technique of deliberate lying will actually make it easier for you to learn the ideas. --- Donald E Knuth, The TeXbook
- eru 17y agoThanks! That's what I was searching for.
- nandemo 17y ago"A little inaccuracy sometimes saves a ton of explanation." I thought it was anonymous but Google tells me it is by Saki, who happens to be a terrific writer.
- eru 17y agoThanks. That's close, but not what I had in mind.
- kscaldef 17y agoI read the article as saying almost exactly that: thunks are sort of like closures, but not entirely. I have the advantage of having a good understanding of both thunks and closures already, but I don't think most reasonable readers would think he's drawing an exact equivalence. Rather he's pointing out a similar concept (code that is run sometime later) that readers may have encountered elsewhere.
- Xichekolas 17y agoBut he does draw direct equivalence: "Local color so you can sound like you know what you’re talking about: in Haskell-land, a closure is called a “thunk”." Which just isn't true. A closure is called a closure and a thunk is called a thunk (as it is in Scheme and any number of other languages; there is nothing special about Haskell). For most of that article he should just replace the word 'closure' with 'thunk' and the issue would disappear.
- carterschonwald 17y agothey really are the same thing semantically, a thunk is a just closure that will be evaluated once (being applied to a unit/trivial value), and then GHC will have its runtime mark a bit that indicated that its been evaluated and redirect the thunk pointer to a value. This is simply a difference of efficiency between normal order evaluation vs lazy evaluation, with a special sort of common subexpression elimination built into the language
- nominolo 17y agoA closure is a function + environment, i.e., the values of its free variables at the time of creation. A thunk is a suspended computation together with an environment. That is, a thunk is a special type of closure, where the function takes no arguments. Additionally, a thunk is updated with its result. In GHC's execution model, the Spineless Tagless G-machine (STG), every heap object is actually a closure. That is, every heap object has a code pointer that can be called to return the object's value. For evaluated objects (including functions) that is essentially the identity function, for thunks this will evaluate as much as necessary, return, and possibly update it's heap object with its new value. Of course, there are several optimisations going on, but that's the basic idea. So, I'd say a thunk is a special kind of closure.
- dfranke 17y agoSee comment #3 on the blog :-)