DerivingVia
An instance from a third type
deriving newtype takes the instance of the representation type: a newtype over Int can have the Num Int instance. deriving via goes one step further and takes the instance of any newtype with the same representation that has the instance you want:
newtype Distance = Distance Double
deriving (Size) via Meters
Distance and Meters are both a Double at run time, so the compiler can use the Size Meters instance as the Size Distance instance with a coercion and no wrapper. The class, the method and the representation all stay; only the choice of instance is borrowed.
Distance via Meters
{-# LANGUAGE DerivingVia #-}
module Main (main) where
class Size a where
size :: a -> Double
newtype Meters = Meters Double
instance Size Meters where
size (Meters m) = m
newtype Distance = Distance Double
deriving stock (Show)
deriving (Size) via Meters
mkDistance :: Double -> Distance
mkDistance = Distance
main :: IO ()
main = do
print (mkDistance 2)
print (size (mkDistance 2))
ghc -Wall -O1 -o via via.hs
./via
Distance 2.0
2.0
size (mkDistance 2) returned 2.0 through the Meters instance, which is the same as the identity here. The interesting part is the type: Distance is not Meters, so a function that takes a Meters still rejects it, and the instance is nonetheless available without a line of code.
Why the representation has to match
The instance is applied through a coercion, so the two types have to have the same representation: a newtype over the same type, or a type that is Coercible to it. If it does not, the compiler refuses and the error names the two representations:
- Distance over Double via Meters over Double is fine.
- A type over Int via a type over Double is not, even with the same class.
- A data type with several fields can be coerced only if the via type has the same fields in the same order, which is a rare and deliberate case.
The check is at compile time and the conversion is free at run time, which is why the strategy is popular in libraries that define a dozen wrappers over the same representation.
Where DerivingVia pays
- A family of wrappers over one representation that should share one instance: a unit of measure, a currency, an identifier.
- A wrapper that should get an instance written for a different newtype, such as a monoid of Sum or Product for a numeric newtype.
- A type whose instance is defined once, in a newtype, and reused by deriving via in several places.
The lesson on phantom types uses the same idea with a parameter instead of a wrapper: a type that records a state and gets its instances from the shape it wraps. The two extensions are a pair, and most modern libraries use both.
Exercises
- Derive Size for a Kilometers newtype via Meters and print a value.
- Try the same via a newtype over Int and read the error.
- Derive Semigroup for a wrapper via Sum.
- Write the same instance by hand and compare the two versions.
- Explain what Coercible means in one sentence.
- Decide whether deriving via or a hand-written instance is clearer for one of the cases above.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
deriving (Size) via Meterstake?Answer
The Size Meters instance, applied through a coercion
The via type supplies the instance and the representation must match for the coercion.
-
What has to match for DerivingVia to work?
Answer
The representation of the two types
The instance is coerced, so the two types must have the same representation.
-
Why does the instance cost nothing at run time?
Answer
The conversion is a coercion erased by the compiler
Coercions have no run-time representation, so the same code runs.
-
What does this program print?
{-# LANGUAGE DerivingVia #-} module Main (main) where class Size a where size :: a -> Double newtype Meters = Meters Double instance Size Meters where size (Meters m) = m newtype Distance = Distance Double deriving (Size) via Meters main :: IO () main = print (size (Distance 3))Answer
3.0
The Meters instance returns the Double, which print shows as 3.0.
-
Which extension is needed for the syntax of this lesson?
Answer
DerivingViaDerivingVia enables deriving via; it implies DerivingStrategies.
-
What does this program print?
{-# LANGUAGE DerivingVia #-} module Main (main) where newtype Sum' = Sum' Int instance Semigroup Sum' where Sum' a <> Sum' b = Sum' (a + b) instance Monoid Sum' where mempty = Sum' 0 newtype Total = Total Int deriving stock (Show) deriving (Semigroup, Monoid) via Sum' main :: IO () main = print (mconcat (map Total [1, 2, 3 :: Int]))Answer
Total 6
The monoid of Sum adds the representations and Show prints the wrapper.
-
Why is
deriving viabetter than a hand-written instance here?Answer
It reuses one tested instance and cannot drift from it
One definition of the instance is reused, so the two types cannot disagree.
-
What does this program print?
module Main (main) where class Size a where size :: a -> Double newtype Meters = Meters Double instance Size Meters where size (Meters m) = m * 100 main :: IO () main = print (size (Meters 2))Answer
200.0
The instance multiplies by 100, so the method returns 200.0.
-
What happens when the representations differ?
Answer
A compile error about the coercion
Coercible fails for different representations, and the instance cannot be derived.
-
Which pair of types can share an instance with
deriving via?Answer
Two newtypes over the same representation
The shared representation is the condition for the coercion.
The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev