Questions tagged [derived-instances]

17 questions
106
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
  • 22,313
  • 10
  • 78
  • 95
14
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
  • 355
  • 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,809
  • 3
  • 25
  • 42
9
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
  • 100,511
  • 79
  • 236
  • 337
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,500
  • 19
  • 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,387
  • 1
  • 16
  • 28
4
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,244
  • 11
  • 27
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,527
  • 20
  • 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,809
  • 3
  • 25
  • 42
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,467
  • 10
  • 44
  • 93
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
  • 8,522
  • 4
  • 25
  • 44
1
vote
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,432
  • 2
  • 22
  • 35
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
  • 2,854
  • 6
  • 29
  • 69
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
  • 133
  • 1
  • 3
  • 11
1
2