haskellderivingvia# Coercing when Rep only equal after deeply evaluating type representations

I want to learn to use Deriving Via for more types. Currently a problem I'm often struggling with is when the generic representations are different, but would be equal if the conversion to type representations went deeper into nested types.

**Simple example:**

```
coerce @(Either () ()) @Bool
```

doesn't work, as `Rec0 () :+: Rec0 ()`

doesn't coerce with `U1 :+: U1`

, even though `:kind! Rep ()`

gives U1, too.

**More complex example**

```
data LeafTree a = Leaf a | Branch [LeafTree a]
```

is isomorphic to `Free [] a`

and it doesn't coerce for similar reasons. How I can I coerce between these types? I also know how to use DerivingVia with coercing between types with equal Rep1 if that helps here.

Solution

Now with the iso-deriving package a monad instance could also be derived like this:

```
{-# LANGUAGE TypeOperators, FlexibleInstances, MultiParamTypeClasses, DeriveTraversable, DerivingVia #-}
import Control.Monad.Free
import Iso.Deriving
data LeafTree a = Leaf a | Branch [LeafTree a]
deriving (Functor)
deriving (Applicative, Monad) via Free [] `As1` LeafTree
instance Inject (Free [] a) (LeafTree a) where
inj (Pure x ) = Leaf x
inj (Free xs) = Branch (fmap inj xs)
instance Project (Free [] a) (LeafTree a) where
prj (Leaf x ) = Pure x
prj (Branch xs) = Free (fmap prj xs)
instance Isomorphic (Free [] a) (LeafTree a)
```

- What is the most practical way to express a dependency on a library for which we have a local git repository with some changes?
- Haskell fails to infer the return type of a monad after using the sequence operator
- Does extracting values from a multiple Value return in Haskell invoke the function more than once?
- How to specify c/c++ compiler on stack install command
- Why do I get "Unexpected reply type" from notify-send when using this Haskell notification server?
- Don't understand notation of morphisms in Monoid definition
- Foldln in haskell
- Is this property of a functor stronger than a monad?
- How to Instantiate a Custom Data Type with Record Syntax and Multiple Constructors
- How do I make a minimal working example for the a DBus server?
- Is it safe to downgrade Haskell stack version?
- Haskell, list of natural number
- unfamiliar syntax in Haskell / Clash function type signature
- foldM with monad State does not type check
- Why does my Runge-Kutta implementation oscillate to 0?
- How do I get the desired behavior in my TCP server?
- Why does the Haskell PVP describe new functions as non-breaking?
- How do I correctly use toLower in Haskell?
- How can I write a notification server in Haskell?
- Every Lens' is a Traversal'... how?
- How do I crate a value of type a{sv} for a call to org.freedesktop.Notifications.Notify via DBus?
- Web Scraping With Haskell
- Double exclamation marks in Haskell
- Haskell Servant POST FormUrlEncoded for (Vector String) field
- Confusion about list types in Haskell
- Idiomatic way to define new vs persisted types in Haskell
- Why does Cabal, unlike GHC, not automatically enable GeneralizedNewtypeDeriving if I explicitly enabled DerivingStrategies?
- What is the proper way of wrapping an Int (not a general type) in another type if type safety is the only motive?
- Parsing inside `between` with Megaparsec
- takeWhile implementation in JavaScript - Looking for better ideas