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
5
votes
1 answer

Haskell DerivingVia on multi param type classes with fun deps

I'm trying to use DerivingVia to cut the boilerplate on instance definitions for a multi parameter type class with functional dependencies. I have these types and class: {-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE StandaloneDeriving #-} {-#…
Guillaum
  • 170
  • 6
5
votes
2 answers

How to derive instances for records with type-families

Here's what I'm trying but it doesn't compile: {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE FlexibleInstances #-} import Data.Text as T import Data.Int (Int64) type family Incoming validationResult baseType type…
Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60
5
votes
3 answers

Haskell Deriving Show Instance

I am playing with a Red-Black tree: -- Taken from Okasaki 1999 module RedBlackTree where --node coloring data --a node is R (red) or B (black) data Color = R | B --tree constructor --a RBT can be E (empty) or be T (a non empty tree) data RBT e = E…
Joe
  • 320
  • 1
  • 4
  • 15
5
votes
2 answers

How to derive Eq for a GADT with a non-* kinded phantom type parameter

For example, trying to compile the following code {-# LANGUAGE StandaloneDeriving, KindSignatures, DataKinds, GADTs#-} data ExprTag = Tag1 | Tag2 data Expr (tag :: ExprTag) where Con1 :: Int -> Expr tag Con2 :: Expr tag -> Expr tag Con3 ::…
Alex R
  • 2,201
  • 1
  • 19
  • 32
4
votes
1 answer

Any way to customize one or two methods of GeneralizedNewtypeDeriving instances?

Haskell's GeneralizedNewtypeDeriving mechanism is great; for those who haven't seen it, writing something like newtype SkewOptT α = SkewOptT (StateT Bool α) deriving (Applicative, Functor, Monad, MonadTrans) will automatically generate…
gatoatigrado
  • 16,580
  • 18
  • 81
  • 143
4
votes
1 answer

Derive typeclass instances for opaque types in Scala 3

Is there a way in Scala 3 to use derives keyword in combination with opaque type aliases? It would be nice to have a boilerplate-free way to provide a typeclass instance to a given opaque type alias by automatically rely on the instance of the same…
4
votes
2 answers

Can I use DerivingVia to derive instances for data types isomorphic to tuples

Given the following data type data Both a b = Both { left :: a, right :: b } I can write instances for Applicative etc. like so (omitting Functor here as we can use DeriveFunctor): instance Monoid a => Applicative (Both a) where pure x = Both…
l7r7
  • 1,134
  • 1
  • 7
  • 23
4
votes
3 answers

Alternative of Show that only uses name

Is there something like Show (deriving Show) that only uses an algebraic datatype's constructors? (please don't mind that I'm using the word constructor, I don't know the right name...) The reason for this question is that with many of my algebraic…
John Smith
  • 2,282
  • 1
  • 14
  • 22
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

Why can you create a value with "Just (+)"?

Currently I'm learning Haskell and are stuck with the instantiation of types to typeclasses. I actually don't understand, why it's possible to create a value of the Maybe a type with Just (+). The problem why this behaves strange to me is, that the…
Daniel
  • 111
  • 7
4
votes
1 answer

Can't derive Generic for this type?

Compiling this short snippet on GHC 8.6.2: {-# LANGUAGE DeriveGeneric, PolyKinds #-} import GHC.Generics data Foo f = FA | FB (f (Foo f)) deriving (Generic, Generic1) Results in this error: Can't make a derived instance of ‘Generic1…
yairchu
  • 23,680
  • 7
  • 69
  • 109
4
votes
1 answer

Why can't we have Random class instances derived for enumerations in Haskell?

I wrote this today: data Door = A | B | C deriving (Eq,Bounded,Enum) instance Random Door where randomR (lo,hi) g = (toEnum i, g') where (i,g') = randomR (fromEnum lo, fromEnum hi) g random = randomR (minBound,maxBound) And I figured this is…
hipogumafe
  • 51
  • 2
4
votes
2 answers

Haskell instance read for newtype that is just an int

I am new to Haskell and I want to be able to a newtype so I can tell what is what, but I also have to read it from a string. I have newtype SpecialId Int deriving (Eq, Ord, Show) I want to be able to read "5" :: SpecialId if I derive Read in…
Ross MacArthur
  • 4,735
  • 1
  • 24
  • 36
4
votes
1 answer

Type class instance not being used when deriving containing data structure

I've been exploring using more newtype wrappers in my code to make more distinct types. I also do a lot of cheap serialization using Read/Show, particularly as a simple form of strongly-typed config file. I ran into this today: The example starts…
dino
  • 1,123
  • 9
  • 13
4
votes
1 answer

Deriving Read in Haskell: Why do I have to use the contructor's parameter names?

I've been experimenting with deriving and had some hard time to understand how deriving (Read) works. Let's have a look at the following data definition: data Data = D Int deriving (Eq, Read, Show) data DataWithName = DWN { val :: Int }…
Markus Weninger
  • 11,931
  • 7
  • 64
  • 137