Do inner bang patterns always force outer constructors in Haskell?

后端 未结 1 1375
余生分开走
余生分开走 2021-02-14 22:53

In Haskell, is there ever a situation where for a data type

{-# LANGUAGE BangPatterns #-}
import Control.DeepSeq

data D = D Int

the instance

1条回答
  •  一整个雨季
    2021-02-14 23:21

    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.

    0 讨论(0)
提交回复
热议问题