import Debug.Trace
main :: IO ()
main = do
let b = (fff 2 10)
print b
let c = (fff 3 10)
print c
print "---"
ff :: (->) Int Int
ff = do
x <- traceShow "x is: " . traceShowId
pure $ (x)
fff :: Int -> Int -> Int
fff = do
(-) . ff
The trace functions seem to be lazily evaluated or something which means the output can vary from:
"x is: "
2
-8
"x is: "
-7
3
"---"
And in another run:
"x is: "
2
"x is: "
3
-8
-7
"---"
I've tried the suggestions from here: How do I force evaluation of an IO action within `unsafePerformIO`? (BangPatterns Pragma + $!
) as well as adding the Strict
and StrictData
pragmas, however I still get the inconsistent behavior.
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE Strict #-}
{-# LANGUAGE StrictData #-}
import Debug.Trace
main :: IO ()
main = do
let !b = (fff 2 10)
print b
let !c = (fff 3 10)
print c
print "---"
ff :: (->) Int Int
ff = do
x <- id $! (traceShow "x is: " . traceShowId)
pure $ (x)
fff :: Int -> Int -> Int
fff = do
(-) . ff
I've also tried using unsafePerformIo
but this has similar behavior:
hmm :: a -> a
hmm x = unsafePerformIO $! do
pure x
ff :: Int -> Int
ff z = do
hmm (trace "x is: " . traceShowId) $ z
Is there a solution without having to have knowledge about strictness / Haskell's evaluation?