All Questions
15 questions
0
votes
1
answer
98
views
Witnessing how frequently a value has been evaluated in Haskell
With a little bit of unsafe, you can see how much of a lazy value has been evaluated in Haskell
import Data.IORef
import System.IO.Unsafe
data Nat = Z | S Nat
deriving (Eq, Show, Read, Ord)
...
1
vote
1
answer
160
views
Memory usage oddity (memory leak?)
I have the following type and two relevant functions that I intend to measure as a part of a large list fold:
Type and access functions:
data Aggregate a = Aggregate (Maybe a) (a -> Aggregate a)
...
6
votes
1
answer
589
views
why foldl is not short circuiting with andFn function?
My understanding is that foldl and foldr executes like :
foldl f a [1..30] => (f (f (f ... (f a 1) 2) 3) ... 30)
and
foldr f a [1..30] => (f 1 (f 2 (f 3 (f ....(f 30 a)))))..)
so.. foldr (&&...
0
votes
1
answer
147
views
Understanding 'sprint' after evaluating a polymorphic expression [duplicate]
Given:
λ: let x = 1 + 2
I run sprint to print its value:
λ: :sprint x
x = _
As expected, it's unevaluated.
But, after evaluating x:
λ: x
3
sprint still outputs _, i.e. unevaluated:
λ: :sprint ...
51
votes
3
answers
3k
views
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, ...
18
votes
4
answers
2k
views
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 ...
8
votes
2
answers
594
views
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 ...
42
votes
1
answer
543
views
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 ...
0
votes
2
answers
128
views
About strictness in haskell
I've created the following Haskell prime function (within ghci):
let pi :: Int -> Int -> Int;
pi 1 _ = 2;
pi x y = if all (/=0) (map (rem y) [pi z 2| z <- [1..(x-1)]]) then y else pi x (y+1);...
3
votes
1
answer
182
views
Have Haskell expand certain thunks at compile time? [duplicate]
Is there a way to have Haskell expand certain thunks at run time. For example, say I have
--Purposely inefficient code for demonstration
fib 0=0
fib 1=1
fib n=fib n=fib (n-1) + fib (n-2)
goldRatio=...
6
votes
4
answers
466
views
How do I serialize or save to a file a Thunk?
In Haskell, you can have infinite lists, because it doesn't completely compute them, it uses thunks. I am wondering if there is a way to serialize or otherwise save to a file a piece of data's thunk. ...
14
votes
1
answer
577
views
: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.....
16
votes
1
answer
516
views
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
...
32
votes
2
answers
2k
views
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,...
46
votes
5
answers
5k
views
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 ...