stock: Stock-style deriving via coercion, with no Generic
A GHC type-checker plugin that derives class instances for
Stock T and higher-kinded variants at compile time, straight
from the structure of T without a Generic representation or
runtime cost. Synthesized instances emulate GHC's stock deriving.
Supported classes:
Stock:Eq,Ord,Show,Read,Semigroup,Monoid,Enum,Bounded,Ix,GenericStock1:Functor,Foldable,Traversable,†Contravariant,Applicative,Eq1,Ord1,Show1,Read1,Generic1,TestEquality,TestCoercionStock2:Bifunctor,Bifoldable,Bitraversable,†Eq2,Ord2,Show2,Read2,Category
† Traversable,
Bitraversable
are synthesisable but cannot be used with DerivingVia due to role
issues.
Companion packages add more classes through DeriveStock instances
(see Stock.Derive), discovered automatically without an extra
-fplugin flag:
stock-aeson:ToJSON,ToJSON1,ToJSON2;FromJSON,FromJSON1,FromJSON2stock-quickcheck:Arbitrary,Arbitrary1,Arbitrary2;CoArbitrary
Ordinary DerivingVia modifiers compose with
Stock:
Down (Stock T)reverses ordering, enumeration and boundsBackwards (Stock1 F)reversesApplicativeeffectsReverse (Stock1 F)reversesFoldable/Traversable
{-# options_ghc -fplugin Stock #-}
{-# language DerivingVia #-}
import Stock
import Data.Ord (Down(..))
-- >>> sort [Bronze,Silver,Gold]
-- [Gold,Silver,Bronze]
data Place = Bronze | Silver | Gold
deriving (Eq, Show) via Stock Place
deriving (Ord, Bounded, Enum) via Down (Stock Place)Per-field modifiers (Override, Stock.Override, re-exported by
Stock) customise individual fields by name, type, or position; _
leaves a field unchanged. A modifier is any newtype with the relevant
instance.
This game example shows hit points and coins accumulate with
addition, poisoning contaminates by disjunction (or), items, and
weapons union with addition to produce a multiset.
import Data.Map.Monoidal (MonoidalMap(..))
type MultiSet key = MonoidalMap key (Sum Int)
data Inventory = Inventory
{ hp :: Int -- (+)
, coins :: Int -- (+)
, poisoned :: Bool -- (||)
, items :: Map Item Int -- unionWith (+)
, weapons :: Map Weapon Int -- unionWith (+)
}
deriving (Eq, Ord, Show, Read) via
Stock Inventory
deriving (Semigroup, Monoid) via
Overriding Inventory
'[ hp via Sum
, coins via Sum
, poisoned via Any
, items via MultiSet Item
, weapons via MultiSet Weapon
]Synthesis runs once per instance (not per use): deriving Cls via Stock
T produces a single shared instance Cls T that every call reuses.
[Skip to Readme]
Modules
[Index] [Quick Jump]
Downloads
- stock-0.1.0.0.tar.gz [browse] (Cabal source package)
- Package description (revised from the package)
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
For package maintainers and hackage trustees
Candidates
- No Candidates
| Versions [RSS] | 0.1.0.0, 0.1.0.1, 0.1.0.2 (info) |
|---|---|
| Change log | CHANGELOG.md |
| Dependencies | base (>=4.18 && <5), ghc (>=9.6 && <9.16) [details] |
| Tested with | ghc >=9.6 && <9.15, ghc ==9.6.7, ghc ==9.8.1, ghc ==9.8.2, ghc ==9.8.4, ghc ==9.10.1, ghc ==9.10.2, ghc ==9.10.3, ghc ==9.12.1, ghc ==9.12.2, 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-23T14:40:23Z |
| Revised | Revision 14 made by BaldurBlondal at 2026-06-23T23:59:44Z |
| Category | Type System |
| Source repo | head: git clone https://github.com/Icelandjack/stock.git |
| Distributions | |
| Reverse Dependencies | 5 direct, 0 indirect [details] |
| Downloads | 6 total (6 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] |