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…
![](../../users/profiles/126916.webp)
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…
![](../../users/profiles/3996781.webp)
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
,…
![](../../users/profiles/1417244.webp)
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?
![](../../users/profiles/165495.webp)
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…
![](../../users/profiles/57879.webp)
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…
![](../../users/profiles/1324720.webp)
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"<
![](../../users/profiles/974766.webp)
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…
![](../../users/profiles/1396134.webp)
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…
![](../../users/profiles/1417244.webp)
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…
![](../../users/profiles/954886.webp)
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…
![](../../users/profiles/925978.webp)
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…
![](../../users/profiles/621427.webp)
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
|…
![](../../users/profiles/1276441.webp)
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…
![](../../users/profiles/2105640.webp)
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…
![](../../users/profiles/7036574.webp)
kalle konsida
- 133
- 1
- 3
- 11