In Haskell, is there ever a situation where for a data type
{-# LANGUAGE BangPatterns #-}
import Control.DeepSeq
data D = D Int
the instance>
Indeed this is correct. We can see what is evaluated using :sprint
in GHCi
, which shows us what thunks have been evaluated.
With no bang patterns:
λ data D = D Int
λ d1 = D 1
λ :sprint d1
d1 = _
λ f1 (D _) = 0
λ f1 d1
0
λ :sprint d1
d1 = _ -- Only D evaluated
With an inner bang pattern:
λ d2 = D 2
λ :sprint d2
d2 = _
λ f2 (D !_) = 0
λ f2 d2
0
λ :sprint d2
d2 = 2 -- Everything evaluated
With an outer bang pattern:
λ d3 = D 3
λ :sprint d3
d3 = _
λ f3 !(D _) = 0
λ f3 d3
0
λ :sprint d3
d3 = _ -- Only D evaluated
With an inner and outer bang patterns:
λ d4 = D 4
λ :sprint d4
d4 = _
λ f4 !(D !_) = 0
λ f4 d4
0
λ :sprint d4
d4 = 4 -- Everything evaluated
From this we can easily see that the patterns !(D !_)
and (D !_)
are equivalent, and moreover that patterns of the form !(D ...)
are redundant.