Questions tagged [derivingvia]

In Haskell, DerivingVia is a deriving strategy generalizing GeneralizedNewtypeDeriving. DerivingVia allows deriving instances of a type using a via type that is representationally equal to it which determines the instance behavior. Unlike GND, DerivingVia works for both 'data' and 'newtype' declarations and is not limited to a single behavior.

For details, consult the GHC manual section on Deriving via.

40 questions
4
votes
1 answer

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…
Johannes Riecken
  • 2,301
  • 16
  • 17
4
votes
3 answers

Is there syntax for creating [Maybe a] analog, but of kind (* -> *)

I was trying to create an fmap that works on [Maybe a]. However, Maybe a has kind *, and fmap demands kind * -> *. This leads to the following unfortunate solution: newtype Unfortunate a = Unfortunate ([Maybe a]) deriving Show instance Functor…
josinalvo
  • 1,366
  • 13
  • 26
4
votes
1 answer

Would it be possible to derive Data.Vector.Unbox via GHC's generic deriving?

It's possible to derive Storable via GHC's generic deriving mechanism: http://hackage.haskell.org/package/derive-storable (and https://hackage.haskell.org/package/derive-storable-plugin for performance). The only library I can find for deriving…
LogicChains
  • 4,332
  • 2
  • 18
  • 27
3
votes
1 answer

Deriving via ReaderT

I'm writing the Monad instance for this type using ReaderT and deriving via newtype HIO a = HIO {runHIO :: Set HiPermission -> IO a} I've tried to do it this way newtype HIO a = HIO {runHIO :: Set HiPermission -> IO a} deriving (Functor,…
Maria Z
  • 31
  • 2
3
votes
0 answers

How to use DerivingVia with Arbitrary

I need to write a heck of Arbitrary instances for newtypes. I am trying to cheap on typing and use deriving via GHC extension. My use case complies, but blows up in runtime! GHCi is 8.8.4 import Test.QuickCheck (Gen, elements, listOf, Arbitrary,…
Daniil Iaitskov
  • 5,525
  • 8
  • 39
  • 49
3
votes
4 answers

Deriving Eq and Show for an ADT that contains fields that can't have Eq or Show

I'd like to be able to derive Eq and Show for an ADT that contains multiple fields. One of them is a function field. When doing Show, I'd like it to display something bogus, like e.g. ""; when doing Eq, I'd like it to ignore that field.…
cheater
  • 347
  • 2
  • 8
3
votes
1 answer

What is this `deriving newtype` syntax?

From a blog post I read -- | Newtype for disabling logging newtype NoLoggingT m a = NoLoggingT { runNoLoggingT :: m a } deriving newtype (Functor, Applicative, Monad) deriving (MonadTrans) via IdentityT instance Monad m => MonadLog…
typetetris
  • 4,586
  • 16
  • 31
3
votes
2 answers

Is it possible to provide default instances of some type class X for my type class Y?

To elaborate, it is often possible to provide default implementations for type class instance functions, but I wonder if it is also possible to provide default implementations for type class instances of other type classes. For instance, say I'm…
bbarker
  • 11,636
  • 9
  • 38
  • 62
3
votes
3 answers

Why does this expression have a valid type?

Banging around in ghci, I happened to notice that the expression (*) 1 [1..5] apparently has a valid type. :t (*) 1 [1..5] (*) 1 [1..5] :: (Enum t, Num [t], Num t) => [t] Apparently it is a list with several type constraints, including Num [t]…
mherzl
  • 5,624
  • 6
  • 34
  • 75
3
votes
2 answers

Using custom instance when deriving an instance via GeneralizedNewtypeDeriving

Suppose that we have a typeclass class (A a, B a) => C a where. Using newtype will allow us to clone a data type and then automatically derive the instances via the GeneralizedNewtypeDeriving language extension (See how to write a derivable class?…
artella
  • 5,068
  • 4
  • 27
  • 35
2
votes
1 answer

Unable to coerce during `DerivingVia` when using optics types like `Prism'`

I'm unable to automatically derive instances using DerivingVia on any type that uses types from optics-core like Prism'; the error I get from compiler is: src/Main.hs:24:13-19: error: • Couldn't match type ‘Foo’ with ‘Stringable Foo’ …
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
2
votes
1 answer

Reify arbitrary data-kind to a (compile-time known) value

I want to be able to write something like: reify (Proxy @True)) == True; reify (Proxy @(Just 5)) == Just 5; Is it possible by a blanket implementation? I got as far as class Reify (a :: k) where reify :: Proxy a -> k And I have no idea how to…
MorJ
  • 566
  • 4
  • 14
2
votes
1 answer

deriving implementation in OCaml

The best code is code that does not exist, and in that regard, Haskell has great support for deriving implementation (that became even better with deriving via). {-# LANGUAGE DeriveTraversable #-} {-# LANGUAGE KindSignatures, PolyKinds#-} import…
nicolas
  • 9,549
  • 3
  • 39
  • 83
2
votes
1 answer

Deriving Via: Cannot derive well-kinded instance

I am encountering this error when I try to derive an instance. Cannot derive well-kinded instance of form ‘HFunctor (ControlFlowCMD ...)’ Class ‘HFunctor’ expects an argument of kind ‘(* -> *, *) …
Robert
  • 165
  • 10
2
votes
1 answer

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…
majkrzak
  • 1,332
  • 3
  • 14
  • 30