haskellghcderivingderivingvia# Wrap a type in newtype

In the following code I got warning `Orphan instance: instance (MonadIO m, Monad m) => GenerateUUID m`

```
instance (MonadIO m, Monad m) => GenerateUUID m where
generateUUID = liftIO nextRandom
```

According to it the solution is either

```
move the instance declaration to the module of the class or of the type, or
wrap the type with a newtype and declare the instance on the new type.
```

(or disable the warning hat Internet also suggest)

My problem is that I'm not able to find **how to wrap the type with a newtype?**

Solution

You can define a newtype wrapper like this:

```
newtype Foo m a = Foo { unFoo :: m a }
```

If we also want to say "I want `Foo`

to have the same instances for `Functor`

, `Monad`

, `Applicative`

... that `m`

has, the only difference being that the methods use `Foo m a`

instead of `m a`

" we can use the `-XDerivingVia`

extension:

```
{-# LANGUAGE DerivingVia #-}
newtype Foo m a = Foo { unFoo :: m a }
deriving (Functor,Applicative,Monad,MonadIO,GenerateUUID) via m
```

Any time the compiler complains about a missing instance for `Foo`

, add it to the `deriving`

clause.

A slight refinement. Suppose we also wanted to "inherit" `Semigroup`

and `Monoid`

instances. For example: `IO ()`

is a `Monoid`

, so we might want `Foo IO ()`

to be a `Monoid`

as well. We need a separate `deriving`

clause:

```
newtype Foo m a = Foo { unFoo :: m a }
deriving (Functor,Applicative,Monad,MonadIO) via m
deriving (Semigroup,Monoid) via (m a)
```

Why a separate clause? Because the typeclasses have different kinds. `Functor`

has kind `(Type -> Type) -> Constraint`

and we are declaring the instance for `Foo m`

, which has kind `Type -> Type`

.

Meanwhile, `Semigroup`

has kind `Type -> Constraint`

and we are declaring the instance for `Foo m a`

, the "fully applied" type constructor with kind `Type`

.

- 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