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
120
votes
3 answers

How does deriving work in Haskell?

Algebraic Data Types (ADTs) in Haskell can automatically become instances of some typeclasses (like Show, Eq) by deriving from them. data Maybe a = Nothing | Just a deriving (Eq, Ord) My question is, how does this deriving work, i.e. how does…
Abhinav Sarkar
  • 23,534
  • 11
  • 81
  • 97
83
votes
3 answers

Zipper Comonads, Generically

Given any container type we can form the (element-focused) Zipper and know that this structure is a Comonad. This was recently explored in wonderful detail in another Stack Overflow question for the following type: data Bin a = Branch (Bin a) a (Bin…
J. Abrahamson
  • 72,246
  • 9
  • 135
  • 180
29
votes
2 answers

How to view the generated code for derived instances / deriving in Haskell

So, in Haskell, it's really easy to do this: data Foo = Bar | Baz deriving (Read, Show) This is great, but I'd like to be able to pass some data as a string from Haskell to the Elm language. The languages are similar enough that, if I had a…
jmite
  • 8,171
  • 6
  • 40
  • 81
25
votes
3 answers

Haskell: Get data constructor name as string

Let us say we have data D = X Int | Y Int Int | Z String I wish to have a function getDConst getDConst :: D -> String that returns either "X", "Y", or "Z", according to the data constructor used for its input. Is there a generic way to write this…
tohava
  • 5,344
  • 1
  • 25
  • 47
24
votes
1 answer

What is the difference between `DeriveAnyClass` and an empty instance?

Using the cassava package, the following compiles: {-# LANGUAGE DeriveGeneric #-} import Data.Csv import GHC.Generics data Foo = Foo { foo :: Int } deriving (Generic) instance ToNamedRecord Foo However, the following does not: {-# LANGUAGE…
Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380
19
votes
2 answers

Are all differentiable types Monads

Given a differentiable type, we know that its Zipper is a Comonad. In response to this, Dan Burton asked, "If derivation makes a comonad, does that mean that integration makes a monad? Or is that nonsense?". I'd like to give this question a specific…
Cirdec
  • 24,019
  • 2
  • 50
  • 100
18
votes
3 answers

Why is there no `-XDeriveApplicative` extension?

GHC has several useful language extensions for mechanically deriving various common Haskell typeclasses (-XDeriveFunctor, -XDeriveFoldable, -XDeriveTraversable). It seems that Applicative is another class which is often needed and frequently easily…
bgamari
  • 5,913
  • 1
  • 18
  • 21
17
votes
2 answers

Why there is no way to derive Applicative Functors in Haskell?

In Haskell, you can derive Functor, Foldable and Traversable automatically using deriving. There is no way to derive Applicative, though. Considering there is one obvious way to define an Applicative instance (which would amount to a zipped…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
13
votes
1 answer

Can I make haskell GADT data constructor infix in derived Show?

Consider two data declarations: {-# LANGUAGE GADTs #-} data X = Int `Y` Int deriving Show data Z where W :: Int -> Int -> Z deriving Show main = do print (1 `Y` 2) print (3 `W` 4) Running the above program produces: 1…
Alexander Gorshenev
  • 2,769
  • 18
  • 33
12
votes
1 answer

Deriving Via With Standalone Deriving

I'm not really sure what I'm doing wrong here: data Vector2D u = Vector2D { _x :: u, _y :: u } deriving stock (Show, Eq, Functor, Foldable, Traversable) {-# INLINE addVector2 #-} addVector2 :: (Additive a) => Vector2D a -> Vector2D a ->…
Julian Birch
  • 2,605
  • 1
  • 21
  • 36
11
votes
2 answers

Quantified constraints vs. (closed) type families

I am trying to use this blogpost's approach to higher-kinded data without dangling Identity functors for the trival case together with quantified-constraint deriving: {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE QuantifiedConstraints,…
Cactus
  • 27,075
  • 9
  • 69
  • 149
9
votes
1 answer

Generalized Newtype Deriving

Haskell can derive the instance for MonadState s in T1 below but not in T2 which is however a very similar type. In which way should I modify the code for T2 so that the instance for MonadState s can be automatically derived? {-# LANGUAGE…
Bob
  • 1,713
  • 10
  • 23
9
votes
1 answer

Deriving default instances using GHC.Generics

I have a typeclass Cyclic for which I would like to be able to provide generic instances. class Cyclic g where gen :: g rot :: g -> g ord :: g -> Int Given a sum type of nullary constructors, data T3 = A | B | C deriving (Generic,…
cdk
  • 6,698
  • 24
  • 51
8
votes
1 answer

haskell -- any way to generate "deriving" instances for roughly-tuple-isomorphic data types?

Suppose I have a data type like data D a = D a a a and a typeclass class C c ... instance (C c1, C c2) => C (c1, c2) Then, I want to be able to write data D a = D a a a deriving C and have that generate an instance, instance C ((a, a), a) => C (D…
gatoatigrado
  • 16,580
  • 18
  • 81
  • 143
8
votes
3 answers

Is it possible to establish Coercible instances between custom types and standard library ones?

For a simple example, say I want a type to represent tic-tac-toe marks: data Mark = Nought | Cross Which is the same as Bool Prelude> :info Bool data Bool = False | True -- Defined in ‘GHC.Types’ But there's no Coercible Bool Mark between them,…
Javran
  • 3,394
  • 2
  • 23
  • 40
1
2 3
9 10