stock-deepseq: Derive NFData via the stock plugin

[ bsd3, library, type-system ] [ Propose Tags ] [ Report a vulnerability ]

The stock plugin provides a newtype Stock for deriving and synthesising instances at compile time. stock-deepseq extends it to support NFData and higher-kinded variants.

{-# options_ghc -fplugin Stock #-}

{-# language DerivingVia #-}
{-# language DataKinds   #-}

import Stock
import Stock.NFData

import Control.Exception

data Person = P { name :: String, age :: Int, secret :: Person }
  deriving (Eq, Ord, Show, NFData) via 
    Stock Person

-- >> try @SomeException (evaluate (rnf geir))
-- Left stack overflow
-- >> geir == geir
-- <loop>
-- >> geir
-- P {name = "Geir", age = 10, secret = P {name = "Geir", .. } }
geir :: Person
geir = P { name = "Geir", age = 10, secret = geir }

The rnf function triggers the loop, but by pinning secret as an ignored field, we can happily force the other fields.

newtype Ignore a = Ignore a
instance NFData (Ignore a) where rnf _ = ()
instance Eq     (Ignore a) where _ == _ = True
instance Ord    (Ignore a) where compare _ _ = EQ
instance Show   (Ignore a) where show _ = "<ignored>"

data Person = P { name :: String, age :: Int, secret :: Person }
  deriving (Eq, Ord, Show, NFData) via 
    Overriding Person '[ secret via Ignore ]

-- >> try @SomeException (evaluate (rnf geir))
-- Right ()
-- >> geir == geir
-- True
-- >> geir
-- P {name = "Geir", age = 10, secret = <ignored>}

stock-deepseq provides three instances, that signal to the plugin how to derive NFData, NFData1, NFData2.

instance DeriveStock  NFData  ..
instance DeriveStock1 NFData1 ..
instance DeriveStock2 NFData2 ..

stock companion packages include:

Modules

[Index] [Quick Jump]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0
Dependencies base (>=4.18 && <5), deepseq (>=1.4 && <1.6), ghc (>=9.8 && <9.16), stock (>=0.1 && <0.2) [details]
Tested with ghc ==9.8.1, ghc ==9.10.3, ghc ==9.12.4, ghc ==9.14.1, ghc ==9.10.3, ghc ==9.12.4, ghc ==9.14.1
License BSD-3-Clause
Author Baldur Blöndal
Maintainer baldur.blondal@iohk.io
Uploaded by BaldurBlondal at 2026-06-23T15:43:48Z
Revised Revision 10 made by BaldurBlondal at 2026-06-24T01:45:55Z
Category Type System
Source repo head: git clone https://github.com/Icelandjack/stock.git(deepseq)
Distributions
Downloads 3 total (3 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2026-06-23 [all 1 reports]