Questions tagged [derived-instances]

Questions relating to the automated process of deriving typeclass instances for particular classes or types, such as using the "deriving" keyword in Haskell or the "derive" macro in Rust.

Typeclass resolution is a process available in several languages, most notably Haskell and Rust, by which polymorphic functionality can be provided in an ad-hoc way to several types.

Writing instances for these types can become tedious in some cases where large amounts of predictable boilerplate can be possible. For instance, the Eq typeclass in Haskell compares two values to determine if they're equal, and it is very common to implement this typeclass by simply comparing each field of the two values for equality recursively.

For classes like this, languages often provide a "deriving" mechanism by which the intuitive, default behavior can be "opted into".

data MyCustomType = MyCustomType Int String
    deriving (Eq)

In this Haskell example, we define a type called MyCustomType which contains an integer and a string. Then we "derive" the Eq instance in the default way. This is equivalent to the longer, more verbose

data MyCustomType = MyCustomType Int String

instance Eq MyCustomType where
    MyCustomType n s == MyCustomType n' s' = n == n' && s == s'

In cases where something unusual needs to be done, the full instance can still be written out.

This tag should be used for questions about the mechanism by which instances are derived in languages that provide this ability.

17 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
15
votes
1 answer

Can't make a derived instance of Num

I am using ghci, this code section newtype Gold = Gold Int deriving (Eq, Ord, Show, Num) is showing the error as Can't make a derived instance of 'Num Gold': 'Num' is not a derivable class Try GeneralizedNewTypeDeriving for GHC's…
Ammlan Ghosh
  • 365
  • 3
  • 12
11
votes
1 answer

invisible / hidden field in the constructor

I am ploughing through Learn You a Haskell for Great Good, and I have reached up to section 8.4, "Derived Instances". In this section, there's the following data type declaration: data Person = Person { firstName :: String ,…
Optimight
  • 2,989
  • 6
  • 30
  • 48
10
votes
4 answers

Deriving arbitrary functions in Haskell

When working with derived instances in Haskell, is it possible to derive functions for arbitrary types, or are we restricted to particular functions?
Casebash
  • 114,675
  • 90
  • 247
  • 350
7
votes
1 answer

How can I get GHC to generate instances of Data.Typeable for GADTs with Typeable in the context?

Suppose I have the following code: {-# LANGUAGE GADTs, DeriveDataTypeable, StandaloneDeriving #-} import Data.Typeable class Eq t => OnlyEq t class (Eq t, Typeable t) => BothEqAndTypeable t data Wrapper a where Wrap :: BothEqAndTypeable a => a…
yatima2975
  • 6,580
  • 21
  • 42
7
votes
1 answer

Is there a way of deriving Binary instances for Vinyl record types using Derive and Template Haskell or otherwise

I have been trying out the Vinyl package, which uses type level kinds to create record structures with field level polymorphism and automatically provided lenses. Both of these features would be very handy to my project, as the former allows for…
Vic Smith
  • 3,477
  • 1
  • 18
  • 29
5
votes
3 answers

In C++, why does casting to reference of derived type work?

Precisely, why does B b = (B&) a compile and work whereas B b = (B) a does not in the below program? #include using namespace std; class A {public: void f(){ cout<<"A"<
Rajan Prasad
  • 1,582
  • 1
  • 16
  • 33
3
votes
1 answer

Cannot derive Typeable for associated data family

I'm trying to derive Typeable for an associated data family like so: {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE TypeFamilies #-} module Test where import Data.Typeable class Test a where data DTest a :: * instance Test () where data…
raichoo
  • 2,557
  • 21
  • 28
3
votes
2 answers

haskell enum - what to do in case value constructors require value instead of nullary? Requirement scenario is given

LYAH says at Derived Instances that [...] all the value constructors are nullary (take no parameters, i.e. fields), we can make it part of the Enum typeclass. data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday deriving…
Optimight
  • 2,989
  • 6
  • 30
  • 48
2
votes
2 answers

Handling Specified Member Classes In C#

In building a class structure, I would like to have derived classes potentially posses derived member classes. For example: class GamePiece { } class Checker : GamePiece {} class ChessMan : GamePiece {} class Game { protected GamePiece…
user954886
  • 23
  • 2
2
votes
1 answer

Using -XGeneralizedNewtypeDeriving with -XMultiParamTypeClasses

The following code results in an error: {-# LANGUAGE GeneralizedNewtypeDeriving, MultiParamTypeClasses, StandaloneDeriving #-} class Module a b where (*>) :: a -> b -> b data D newtype DWrapper = DW D instance Module D D deriving…
crockeea
  • 21,651
  • 10
  • 48
  • 101
2
votes
3 answers

Use parts of constructor for deriving instance in Haskell data

I need to derive Eq for a data, but for some constructors I want to ignore some fields. The data is for representing DataTypes (we are developing a compiler): data DataType = Int | Float | Bool | Char | Range | Type | String Width |…
chamini2
  • 2,820
  • 2
  • 24
  • 37
1
vote
1 answer

How to create StandaloneDeriving instances

Given the following code {-# LANGUAGE StandaloneDeriving #-} type Date = Int data Decision a = Decision a deriving (Show) data InProgress a = InProgress {progress :: a, decision :: Decision a } deriving (Show) data Finished a = Finished…
robkuz
  • 9,488
  • 5
  • 29
  • 50
1
vote
1 answer

inherit methods declared in .m file

I now know there is no protected method in Objective-C and here is my problem. I have two viewControllers with many functions and properties that are shared. My vision was to have a BaseViewController holding the shared methods and properties, and…
liv a
  • 3,232
  • 6
  • 35
  • 76
0
votes
1 answer

Is it possible to write a generall derived instance for a data type in haskell?

I require comparison between two Htrees and to do so i implemented my own comparison function which i use together with sortBy, however i want to implement a derived instance of the Eq and Ord classes but the amount of cases needed to cover all…
kalle konsida
  • 323
  • 2
  • 5
  • 12
1
2