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?
You can define a newtype wrapper like this:
If we also want to say "I want
Foo
to have the same instances forFunctor
,Monad
,Applicative
... thatm
has, the only difference being that the methods useFoo m a
instead ofm a
" we can use the-XDerivingVia
extension:Any time the compiler complains about a missing instance for
Foo
, add it to thederiving
clause.A slight refinement. Suppose we also wanted to "inherit"
Semigroup
andMonoid
instances. For example:IO ()
is aMonoid
, so we might wantFoo IO ()
to be aMonoid
as well. We need a separatederiving
clause:Why a separate clause? Because the typeclasses have different kinds.
Functor
has kind(Type -> Type) -> Constraint
and we are declaring the instance forFoo m
, which has kindType -> Type
.Meanwhile,
Semigroup
has kindType -> Constraint
and we are declaring the instance forFoo m a
, the "fully applied" type constructor with kindType
.