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:

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

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

  1. Derive Size for a Kilometers newtype via Meters and print a value.
  2. Try the same via a newtype over Int and read the error.
  3. Derive Semigroup for a wrapper via Sum.
  4. Write the same instance by hand and compare the two versions.
  5. Explain what Coercible means in one sentence.
  6. 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.

  1. What does deriving (Size) via Meters take?

    Answer

    The Size Meters instance, applied through a coercion

    The via type supplies the instance and the representation must match for the coercion.

  2. 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.

  3. 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.

  4. 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.

  5. Which extension is needed for the syntax of this lesson?

    Answer

    DerivingVia

    DerivingVia enables deriving via; it implies DerivingStrategies.

  6. 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.

  7. Why is deriving via better 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.

  8. 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.

  9. What happens when the representations differ?

    Answer

    A compile error about the coercion

    Coercible fails for different representations, and the instance cannot be derived.

  10. 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