Leo Zhang
Leo Zhang

Reputation: 3230

Haskell Strict fields

When defining a lazy field, there is no exception, until you print it.

> data T = T Int deriving (Show)
> let t = T undefined
> t
T *** Exception: Prelude.undefined
CallStack (from HasCallStack):
  error, called at libraries/base/GHC/Err.hs:79:14 in base:GHC.Err
  undefined, called at <interactive>:3:7 in interactive:Ghci3

With strict field (!Int), I thought undefined would be evaluated right away, which will cause exception, but actually, it's still no evaluated until you print it. Why is that?

> data X = X !Int deriving (Show)
> let x = X undefined
> x
*** Exception: Prelude.undefined
CallStack (from HasCallStack):
  error, called at libraries/base/GHC/Err.hs:79:14 in base:GHC.Err
  undefined, called at <interactive>:6:11 in interactive:Ghci5

Upvotes: 4

Views: 199

Answers (1)

luqui
luqui

Reputation: 60463

Because let itself defines a lazy binding -- let never evaluates anything by itself (unless BangPatterns are used).

ghci> let x = undefined
ghci> x
*** Exception: Prelude.undefined

You can tell the difference between the strict and lazy constructors like so:

ghci> T undefined `seq` ()
()
ghci> X undefined `seq` ()
*** Exception: Prelude.undefined

Upvotes: 8

Related Questions