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

Reusing MArray instances for a newtype

I have a dozen of newtypes like this: newtype MyBool = MyBool Bool newtype MyInt = MyInt Int I want to reuse existing instances: instance MArray IOUArray Int IO where ... instance MArray (STUArray s) Int (ST s) where ... Implementing…
oshyshko
  • 2,008
  • 2
  • 21
  • 31
8
votes
2 answers

How to define MonadUnliftIO instance for a newtype with a phantom type-variable?

Related question - Is it safe to derive MonadThrow, MonadCatch, MonadBaseControl, MonadUnliftIO, etc? - where I had enabled, both - DeriveAnyClass and GeneralizedNewtypeDeriving to get the code to compile, but didn't bother looking at the ominous…
Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60
8
votes
1 answer

Is there a way to shorten this deriving clause?

Is there a way to write the following: {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE DeriveAnyClass #-} data X = A | B | C deriving (Eq, Ord, Show, Read, Data, SymWord, HasKind, SMTValue) So that the deriving clause can be shortened…
alias
  • 28,120
  • 2
  • 23
  • 40
8
votes
1 answer

Automatically derive Add, Mul, Ord, etc. for a numeric type

What's the simplest way to create a custom type that behaves like a number? I want type-checking that prevents mixing different units in my program, but I still want to be able to easily perform calculations on the type without casting back and…
Kornel
  • 97,764
  • 37
  • 219
  • 309
8
votes
1 answer

Using Generic Deriving with a Record Haskell

I am basically attempting to see if I can emulate an ORM framework within Haskell, so that if a user wants to make a database model, they would do something like this data Car = Car { company :: String, model :: String, …
mdedetrich
  • 1,899
  • 1
  • 18
  • 29
7
votes
3 answers

Is there a Template Haskell / deriving mechanism for Data.Binary (or friends?)

The Data.Binary documentation shows writing an instance by hand. Is there a way around this? I saw here there is another library, SerTH, which has a (Template Haskell based) deriving mechanism, but the link to it seems broken. Also, if you know…
gatoatigrado
  • 16,580
  • 18
  • 81
  • 143
7
votes
1 answer

Haskell - Automatic Monad instance

I'm trying to create my own data type, which will be part of Monad class, but newtype Container a = Container a deriving Monad gives me this error: * Can't make a derived instance of `Monad Container' (even with cunning…
Edward Grey
  • 101
  • 3
7
votes
1 answer

How to use `deriving` in Idris?

I'm trying to deriving Show, Eq, Ord etc in Idris, but none of the following trials works: trail #1: data Expr = Lit Int | Neg Expr | Add Expr Expr deriving (Show) got: deriving.idr:5:15-18: | 5 | deriving (Show) | …
luochen1990
  • 3,689
  • 1
  • 22
  • 37
7
votes
1 answer

Haskell Labeled AST: No instance for (Show1 (Label a)), How to construct an instance?

I want to have an annotated AST, so I defined those recursive data structures using Fix: data Term a = Abstraction Name a | Application a a | Variable Name deriving (Read,Show,Eq,Functor,Foldable,Traversable) data Label a b = Label a…
7
votes
1 answer

Derived instance in Haskell

I would like to use derived instance like this: data Test3D = forall a. (Show a, Eq a, Typeable a, Generic a) => Test3D { testDt :: String , testPrm :: a } deriving (Show, Eq,…
QSpider
  • 537
  • 2
  • 10
7
votes
3 answers

What does deriving do/mean in Haskell?

The definition of deriving on stack overflow is: "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…
August Jelemson
  • 962
  • 1
  • 10
  • 29
7
votes
1 answer

Deriving Typeable and Data for GADTs?

Using: {-# LANGUAGE GADTs #-} {-#…
fho
  • 6,787
  • 26
  • 71
7
votes
1 answer

Standalone deriving declaration in Template Haskell quotation

Why Template Haskell ignores standalone deriving declaration in quotation? {-# LANGUAGE TemplateHaskell, StandaloneDeriving #-} data Test a = Test a $([d| deriving instance Show a => Show (Test a); f x = x |]) ghci> :l Test.hs [1 of 1] Compiling…
leventov
  • 14,760
  • 11
  • 69
  • 98
7
votes
2 answers

Automatic derivation of Data.Vector.Unbox with associated type synonyms

I have a datatype newtype Zq q = Zq (IntType q) where 'q' will be an instance of the class class Foo a where type IntType a and 'IntType' is just the underlying representation (i.e. Int, Integral, etc) associated with 'q'. I want to make Zq an…
crockeea
  • 21,651
  • 10
  • 48
  • 101
6
votes
1 answer

Haskell -- any way to turn off rebindable syntax for the case of `deriving` instances?

There's an annoying "feature" that deriving instances are also affected by the RebindableSyntax extension. Example of what I want to write: {-# LANGUAGE RebindableSyntax #-} import qualified Prelude data Color = Red | Green | Blue | Periwinkle |…
gatoatigrado
  • 16,580
  • 18
  • 81
  • 143
1
2
3
9 10