Dorai Sitaram <ds26@goldshoe.gte.com> wrote:
+---------------
| Alfred J. Reich <reich@io.com> wrote:
| >SICP (2nd Ed.) has a footnote on the origin of the word (p. 401).
| >Basically, it's slang for a past tense form of "thought". A thought (noun)
| >that has already been thought (verb) is a "thunk".
|
| SICP seems to have changed its etymology for the word
| _thunk_ when it went into its second edition.
| The first edition advanced an onomatopoeic origin: the
| sound made by an activation record as it landed on the
| control stack.
+---------------
Well, the ALGOL-10 compiler for the DEC PDP-10 (~1970) used thunks
(their term) for the call-by-name argument accessor code fragments
they passed around, and IIRC (though it's been ~30yr, so I could
have it slightly wrong), those thunks weren't "on the stack", nor
were they "activation records" -- they were fixed code fragments
whose addresses were passed as arguments.
That is, for each argument (all of which were call-by-name, of course,
this being Algol), a subroutine was passed the addresses of two thunks,
one for fetching that argument and one for storing it back again. [O.k.,
so by Scheme's modern definition of "thunk" == "arity-0 proc" the
second one was actually a 1-arg closure.] A subroutine call like
"foo(a, (a**2)*(b+c))" actually generated code that in Scheme might
look somewhat like this:
(foo (vector (lambda () a)
(lambda (x) (set! a x)))
(vector (lambda () (* (expt a 2) (+ b c)))
(lambda (x) (error "Can't mutate argument expression!"))))
allowing weird call-by-name stuff like Jensen's Device to work properly.
So "thunk" as "pre-thought" is more consistent with the ALGOL-10 usage.
-Rob
p.s. Yes, I know that one way to do the above would be for the caller to
JIT-compile the thunks onto the stack at call time, but IIRC that's not
what the ALGOL-10 compiler did. The thunk code was read-only (rather,
reentrant, since on the KA-10 it did live in the low seg), and it walked
back up the display link to get at whatever lexical variables it needed.
p.p.s. The only reason I remember as much of the above as I do is that I
happened to have spent some time reverse-engineering the interface between
the compiled code and the run-time system -- using only the executables
of the compiler and the RTS (and the binary output from test compiles) --
because we were missing the library that connected the two!! [And couldn't
ask DEC for it, but that's another story...]
-----
Rob Warnock, 31-2-510 rpw3@sgi.com
SGI Network Engineering http://reality.sgi.com/rpw3/
1600 Amphitheatre Pkwy. Phone: 650-933-1673
Mountain View, CA 94043 PP-ASEL-IA