haskellderivingderivingvia# Deriving Via With Standalone Deriving

I'm not really sure what I'm doing wrong here:

```
data Vector2D u = Vector2D {
_x :: u,
_y :: u
} deriving stock (Show, Eq, Functor, Foldable, Traversable)
{-# INLINE addVector2 #-}
addVector2 :: (Additive a) => Vector2D a -> Vector2D a -> Vector2D a
addVector2 (Vector2D { _x = x1, _y = y1 }) (Vector2D { _x = x2, _y = y2 }) =
Vector2D { _x = x1 + x2, _y = y1 + y2 }
instance (Additive a) => Additive (Vector2D a) where
(+) = addVector2
newtype Square a = Square {
unpackSquare :: Vector2D a
} deriving stock (Show, Eq)
```

So far, so normal (Additive is defined in the algebra package but is pretty self-explanatory).

However, now I want to be clever using DerivingVia and StandaloneDeriving and I can't even get the next line to compile

```
deriving instance (Additive a) => Additive (Square a) via (Vector2D a)
```

But that gets me

* Expected kind `k0 -> * -> Constraint', but `Additive (Square a)' has kind `Constraint' * In the stand-alone deriving instance for `(Additive a) => Additive (Square a) via (Vector2D a)'

Can anyone tell me what I'm doing wrong? I'm running GHC 8.6.2

Solution

It's

```
deriving via (Vector2D a) instance (Additive a) => Additive (Square a)
```

The way you wrote it, `via`

looks like a type variable

```
deriving instance (Additive a) => Additive (Square a) via (Vector2D a)
-- <==>
deriving instance forall a via. (Additive a) => Additive (Square a) (via) (Vector2D a)
```

and this produces a kind-mismatch error, as `Additive (Square a) :: Constraint`

is already saturated, but you have applied it to two more arguments.

This is backwards from the shorthand form:

```
data T' = ...
deriving Class via T
```

- Comparing lists in Haskell
- Is there a non-identity monad morphism M ~> M that is monadically natural in M?
- Problem with loading module ‘Distribution.Simple’
- Improving efficiency in Stirling numbers calculation
- Does sequencing an infinite list of IO actions by definition result in a never-ending action? Or is there a way to bail out?
- How to call pgQuery from postgresql-query?
- How to avoid whitespace after a tag (link) in Hamlet templates?
- Understanding type-directed resolution in Haskell with existential types
- Why is seq bad?
- Understanding bind function in Haskell
- How to create route that will trigger on any path in Servant?
- How do I use a global state in WAI middleware?
- nixos 23.11 cabal install mysql-simple problem - "Missing (or bad) C libraries"
- Is there a way to kill all forked threads in a GHCi session without restarting it?
- Why can an invalid list expression such as 2:1 be assigned to a variable, but not printed?
- Iterate over a type level list and call a function based on each type in the list
- How does this solution of Project Euler Problem 27 in the Haskell Wiki work?
- Why `Monad` is required to use `pure`?
- Can't do partial function definitions in GHCi
- recommended way to convert Double -> Float in Haskell
- Haskell profiling understanding cost centre summary for anonymous lambda
- Why is Haskell fully declarative?
- GHC Generating Redundant Core Operations
- Question about Event firing in reflex-frp
- Using Haskell's "Maybe", type declarations
- How can I elegantly invert a Map's keys and values?
- Why there is no output for wrapped IO in Haskell?
- What are the definitions of Weather and Memory in xmobar repo?
- Serializing a Data.Text value to a ByteString without unnecessary \NUL bytes
- Using Haskell with VS Code