Questions tagged [deriving]

In Haskell, a derived instance is an instance declaration that is generated automatically in conjunction with a data or newtype declaration. The body of a derived instance declaration is derived syntactically from the definition of the associated type.

In Haskell, a derived instance is an instance declaration that is generated automatically in conjunction with a data or newtype declaration. The body of a derived instance declaration is derived syntactically from the definition of the associated type.

139 questions
4
votes
3 answers

Custom deriving(Read,Show) for enum type

Let's say I have this enumeration type: data TVShow = BobsBurgers | MrRobot | BatmanTAS and I want to define instances for Read and Show with the following behavior: show BobsBurgers = "Bob's Burgers" show MrRobot = "Mr. Robot" show BatmanTAS =…
Pubby
  • 51,882
  • 13
  • 139
  • 180
4
votes
1 answer

PartialEq on Array

Question about rust arrays (the constant size ones, [T, ..Size]). I am trying to make the following work: #[deriving(PartialEq)] struct Test { dats : [f32, ..16] } I know I could not use deriving and simply write my own PartialEq, but that is…
luke
  • 1,024
  • 3
  • 11
  • 21
4
votes
1 answer

Enabling "-fno-warn-" for Inaccessible code in GADTs

Given a GADT indexed by a phantom variable I can use standalone deriving to create some simple instances data Client data Temporary data Permanent data Token ty where ClientToken :: Token Client TemporaryToken :: ByteString -> ByteString ->…
J. Abrahamson
  • 72,246
  • 9
  • 135
  • 180
3
votes
0 answers

Why automatical deriving for newtype does not work

I use library msgpack-types-0.0.4 and I have a type: newtype A = A Text and I tried deriving MessagePack, deriving newtype MessagePack, standalone derivings, it seems nothing works (or I do something wrong). There is MessagePack instances for Text,…
RandomB
  • 3,367
  • 19
  • 30
3
votes
2 answers

Problem when deriving Eq from data type with function as field in Haskell

I'm trying to deriving Eq from data type with a function as a field but doesn't work as expected. I also try to write te instance but still doesn't work data Conf = Conf { rule :: ([Char] -> Char), start :: Int, numLines :: Double, …
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
1 answer

How to write custom ppx decorator to rescript?

I need to generate a value with a different type from my passed type. This is the first time I write on ocaml-like, and for example, in a familiar me haskell I would use Data.Generics. How I have understood I need to use decorator and ppx. I wrote…
Gleb Patcia
  • 365
  • 2
  • 15
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

Eq instance has some strange comparisons

I have made an image processing module that defines a Pixel type as a Color and Location. Pixel, Color, and Location derive Eq, as I may want to compare pixels between multiple images. Eq suits my needs for comparing pixels to see if they're exactly…
adorablepuppy
  • 1,077
  • 1
  • 7
  • 13
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
0 answers

Newtype deriving IsSequence

I have a newtype X, which is basically a list of Ints. I use ClassyPrelude instead of the standard Prelude and want to derive the IsSequence class. This makes it necessary to also derive lots of other classes. The language extension…
3
votes
1 answer

Deriving extensions with multiparameter types

I have an Ast type constructor, parameterized by the identifier type. Using the DeriveFunctor, DeriveFoldable and DeriveTraversable extensions it is possible to automatically create the appropriate instances. Now I find it useful to introduce more…
3
votes
1 answer

Automatically deriving show instances for GADTs

Assume I have a complex GADT which with many hidden type parameters as constructors: data T where A :: Num n => n -> T B :: (Num n, Integral m) => n -> m -> T C :: Floating a => [a] -> T -- and so on Z :: Num n => n -> n -> T I want to…
ThreeFx
  • 7,250
  • 1
  • 27
  • 51