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

Deriving in newtype with more type variables

newtype MyNewtype1 f v = MyNewtype1 { getVal1 :: f v } deriving Eq -- OK newtype MyNewtype2 f maybe v = MyNewtype2 { getVal2 :: f (maybe v) } deriving Eq --OK newtype MyNewtype3 f v = MyNewtype3 { getVal3 :: f (Maybe v) } -- OK newtype MyNewtype4 f…
user1747134
  • 2,374
  • 1
  • 19
  • 26
3
votes
1 answer

Haskell Typeable instance

I'm using cmdargs to get some arguments from a command line program. I'm using some special type in my program data Function = Max | Min | Moy | Med deriving (Eq,Data,Typeable) I can pass…
JeanJouX
  • 2,555
  • 1
  • 25
  • 37
3
votes
1 answer

Derive Clone for array of something that is Clone?

This code (also on play) use std::sync::Arc; struct Foo { x: isize, // Something complex in actual code, implements Drop } #[derive(Clone)] struct Good { a: Option>, b: Option>, c:…
Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
3
votes
2 answers

How can I automatically derive Typeable instance for DataKinds derived types?

I had some types like this: data Currency = USD | EUR deriving (Show, Typeable) data Money :: Currency -> * where Money :: Int -> Money c deriving (Show, Typeable) And I wanted to use typeOf with them in this function: findRate…
Ramith Jayatilleka
  • 2,132
  • 16
  • 25
3
votes
0 answers

Modern haskell implementation of generically derived bifunctors

I'm looking for a way to derive fmapFirst and fmapSecond for bifunctors automatically. I would prefer a way to do it using the new Generic type class or using Data.Data, and without Template Haskell. (Note that I already know that…
tohava
  • 5,344
  • 1
  • 25
  • 47
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
3
votes
0 answers

Derive a record datatype without template haskell

So, I've been toying around a little bit with GHC.Generics, which are great, but seem limited/focused mainly on generating instances. What I would like to do, if possible, is to derive a new data type from another. Let's say you've got the…
jcristovao
  • 554
  • 2
  • 12
3
votes
2 answers

Haskell GHC-7.6.2 deriving Data and Typeable with HashMap

After upgrading to Ubuntu 13.10, one of my Haskell codes fails to compile. The following code compiles without problems under (at least) ghc-7.0.x, but fails under ghc-7.6.2 (with unordered-containers-0.2.2.1): {-# LANGUAGE OverloadedStrings,…
3
votes
1 answer

Haskell deriving instances of Eq for Existentials type classes

Is there a way to automatically derive instances for Eq (and show) for Power? I managed to find http://www.haskell.org/ghc/docs/7.4.2/html/users_guide/deriving.html but I could not locate any explanation relevant to the code below. In addition, if…
Ra is dead
  • 569
  • 2
  • 18
2
votes
0 answers

Deriving from std::vector - what am I doing wrong?

Trying to switch from some custom made classes I built to std classes. Just for a start, I am trying to derive from std::vector. #include template < class DATA_T > class CArray : public std::vector < DATA_T > { public: DATA_T* Create…
Razzupaltuff
  • 2,250
  • 2
  • 21
  • 37
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

Why is the key in Data.Map constrained to class Ord?

Suppose I define a simple type like this: data Object = House | Table | Book To be able to use Objects as keys for a map, the type has to be derived from Eq and Ord: data Object = House | Table | Book deriving (Eq, Ord) What is the reason for this…
2
votes
2 answers

Shapeless: what the difference between these two approaches of instance derivation?

Can someone explain me what the difference between these two approaches for typeclass instance derivation (specifically for Option[A])? 1. trait MyTrait[A] {...} object MyTrait extends LowPriority { // instances for primitives } trait LowPriority…
Nikita Ryanov
  • 1,520
  • 3
  • 17
  • 34
2
votes
0 answers

How to access default values for case class fields?

I want to derive instances of some type (Decoder[A]) for arbitrary case classes using shapeless. trait Decoder[A] extends Serializable { self => def decode(source: String): Either[Throwable, A] } If i don't consider the default values for case…
Nikita Ryanov
  • 1,520
  • 3
  • 17
  • 34
2
votes
0 answers

Is it possible to read the derived instances in GHC?

When I have a complicated type, I often use deriving clauses to let the compiler infer the instance implementation for me. I would like to see exactly what the implementations are it comes up with.
schuelermine
  • 1,958
  • 2
  • 17
  • 31