Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell Strict fields

Tags:

haskell

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
like image 871
Leo Zhang Avatar asked Jan 28 '23 14:01

Leo Zhang


1 Answers

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
like image 169
luqui Avatar answered Feb 08 '23 08:02

luqui