News for hackers. Stuff that matters.
Re:On Learning Haskell by Xichekolas on Wednesday March 10, 2010 @11:43AM (#1181286)
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.
Re:On Learning Haskell by nominolo on Wednesday March 10, 2010 @01:35PM (#1181585)
A 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.

Re:On Learning Haskell by carterschonwald on Wednesday March 10, 2010 @12:58PM (#1181501)
they 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
Re:On Learning Haskell by kscaldef on Wednesday March 10, 2010 @12:55PM (#1181496)
I 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.
Re:On Learning Haskell by Xichekolas on Wednesday March 10, 2010 @01:36PM (#1181588)
But 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.

Re:On Learning Haskell by eru on Wednesday March 10, 2010 @12:53PM (#1181487)
Careful lying may aid in explaining. [1] The author knows the difference.

[1] Can anybody find the source of this quote?

Re:On Learning Haskell by jsyedidia on Wednesday March 10, 2010 @06:43PM (#1182361)
The author feels that this technique of deliberate lying will actually make it easier for you to learn the ideas. --- Donald E Knuth, The TeXbook
Re:On Learning Haskell by eru on Thursday March 11, 2010 @09:19AM (#1183744)
Thanks! That's what I was searching for.
Re:On Learning Haskell by nandemo on Wednesday March 10, 2010 @11:01PM (#1182900)
"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.

Re:On Learning Haskell by eru on Thursday March 11, 2010 @09:18AM (#1183743)
Thanks. That's close, but not what I had in mind.
Re:On Learning Haskell by dfranke on Wednesday March 10, 2010 @02:13PM (#1181688)
See comment #3 on the blog :-)