Questions tagged [thunk]

A parameterless closure (functional programming) or a function generated by a compiler to aid runtime linking with a dynamic library function.

  • In functional programming, a thunk is an anonymous and parameterless function (closure), used to pass lazily evaluated expressions.
  • In C++, a thunk is a compiler generated function which optimizes virtual function calls.
  • On systems which support dynamic linking, a thunk is often automatically inserted to provide a place for the runtime linker to insert code for invoking the actual implementation found in a library.
184 questions
163
votes
12 answers

What is a 'thunk'?

I've seen it used in programming (specifically in the C++ domain) and have no idea what it is. Presumably it is a design pattern, but I could be wrong. Can anyone give a good example of a thunk?
fbrereto
  • 35,429
  • 19
  • 126
  • 178
50
votes
3 answers

How atomic are GHC's thunks?

How does GHC handle thunks that are accessed by multiple threads (either explicit threads, or the internal ones that evaluate sparks)? Can it happen that multiple threads evaluate the same thunk, duplicating work? Or, if thunks are synchronized,…
Petr
  • 62,528
  • 13
  • 153
  • 317
46
votes
5 answers

Is everything in Haskell stored in thunks, even simple values?

What do the thunks for the following value/expression/function look like in the Haskell heap? val = 5 -- is `val` a pointer to a box containing 5? add x y = x + y result = add 2 val main = print $ result Would be nice to…
vis
  • 2,279
  • 1
  • 19
  • 27
42
votes
1 answer

Understanding the different behavior of thunks when GHCi let bindings are involved

I've been playing with some examples from Simon Marlow's book about parallel and concurrent programming in Haskell and stumbled across an interesting behavior that I don't really understand. This is really about me trying to understand some of the…
raichoo
  • 2,557
  • 21
  • 28
32
votes
2 answers

How much memory does a thunk use?

Let's say I have a very large number (millions/billions+) of these simple Foo data structures: data Foo = Foo { a :: {-# UNPACK #-}!Int , b :: Int } With so many of these floating around, it becomes necessary to think about how much…
Mike Izbicki
  • 6,286
  • 1
  • 23
  • 53
31
votes
4 answers

what is the difference between thunk, futures, and promises?

There are wiki articles about them: (http://en.wikipedia.org/wiki/Futures_and_promises, http://en.wikipedia.org/wiki/Thunk_(delayed_computation)). But I am not sure what are the exact differences between the three as a programming language concept?…
JRR
  • 6,014
  • 6
  • 39
  • 59
18
votes
4 answers

Test if a value has been evaluated to weak head normal form

In Haskell, is it possible to test if a value has been evaluated to weak head normal form? If a function already exists, I would expect it to have a signature like evaluated :: a -> IO Bool There are a few places that similar functionality lives. A…
Cirdec
  • 24,019
  • 2
  • 50
  • 100
16
votes
1 answer

If a thunk results in an exception, is the exception kept as the result of the thunk?

I created this small program that creates a long-running thunk that eventually fails with an exception. Then, multiple threads try to evaluate it. import Control.Monad import Control.Concurrent import Control.Concurrent.MVar main = do let thunk…
Petr
  • 62,528
  • 13
  • 153
  • 317
14
votes
1 answer

:sprint for polymorphic values?

I am wondering why :sprint reports xs = _ in this case: Prelude> xs = map (+1) [1..10] Prelude> length xs 10 Prelude> :sprint xs xs = _ but not in this case: Prelude> xs = map (+1) [1..10] :: [Int] Prelude> length xs 10 Prelude> :sprint xs xs =…
ErikR
  • 51,541
  • 9
  • 73
  • 124
11
votes
2 answers

In simple terms, what's the difference between a thunk and a Higher Order Function?

I understand that both are functions that return functions. My experience so far with thunks have been using them to return functions as opposed to just action objects so that I can work with async requests in Redux. A closure is an implementation…
8
votes
2 answers

"Missing non-virtual thunks" and inheritance order

We have a large code base in C++ and after a smallish refactor (one class added and some related methods rewritten), we started getting linker errors on GCC 3 & 4. The linker errors were specifically "missing references to non-virtual thunks" in…
Adrian
  • 1,842
  • 13
  • 25
8
votes
2 answers

Haskell Fibonacci Explanation

I am quite new to Haskell and I'm trying to wrap my head around how the lazy expression of Fibonacci sequences work. I know this has been asked before, but none of the answers have addressed an issue I'm having with visualising the result. The code…
7
votes
1 answer

What are the benefits of using the co library with promises instead of with thunks?

So I've been reading about the usage of the co library, and the general design pattern I've seen in most blog posts is wrapping functions that have callbacks in thunks. Then using an es6 generator to yield those thunks to the co object. Like…
m0meni
  • 16,006
  • 16
  • 82
  • 141
7
votes
1 answer

Thunk table in import address table?

What is a thunk table in relation to the import address table that's used in EXE files to import functions used in external DLLs? Is this thunk table just a table containing 'Thunks' to other functions?
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
7
votes
2 answers

Clojure thunks: stack overflow with [0] but not '(0)?

Here's a snippet of code that gives me a StackOverflowError (boiled down from an actual example in my codebase): ( ->> (range 3000) (mapcat #(concat [0] (take 100 (repeat %)))) (reduce (constantly nil)) (count)) (Note: this code…
Bosh
  • 8,138
  • 11
  • 51
  • 77
1
2 3
12 13