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
2
votes
1 answer

Wrap a type in newtype

In the following code I got warning Orphan instance: instance (MonadIO m, Monad m) => GenerateUUID m instance (MonadIO m, Monad m) => GenerateUUID m where generateUUID = liftIO nextRandom According to it the solution is either move the…
majkrzak
  • 1,332
  • 3
  • 14
  • 30
2
votes
1 answer

Can I avoid explicitly deriving built-in Haskell typeclasses over and over again?

I have a large type hierarchy in Haskell. Counting family instances, which (can) have separate class membership after all, there are hundreds of data types. Since the top-most type needs to implement built-in classes like Generic,Eq,Ord,Show, every…
Leif Willerts
  • 205
  • 1
  • 9
2
votes
1 answer

Encoding a choice of monad transformers

> {-# LANGUAGE FlexibleInstances, UndecidableInstances #-} > {-# LANGUAGE ConstraintKinds, DerivingVia, DerivingStrategies, GeneralizedNewtypeDeriving, KindSignatures, NoMonomorphismRestriction, RecordWildCards #-} > {-# LANGUAGE GADTs,…
2
votes
1 answer

How can I find out the classes which list types belong to?

How can I find out the classes which list types belong to? In https://www.haskell.org/onlinereport/haskell2010/haskellch6.html says data [a] = [] | a : [a] deriving (Eq, Ord) and Lists are an instance of classes Read, Show, Eq, Ord, Monad,…
Tim
  • 1
  • 141
  • 372
  • 590
2
votes
1 answer

DeriveAnyClass vs Empty Instance

Suppose that I have this type family that throws a custom type error during compile time if the type passed to it is not a record: type family IsRecord (a :: Type) where ... Now I have this type class that has methods with default…
Nick Tchayka
  • 563
  • 3
  • 14
2
votes
1 answer

(Data.Monoid) - Sum and Product deriving Bounded and Num at the same time?

in Data.Monoid : newtype Sum a = Sum { getSum :: a } deriving ( Eq -- ^ @since 2.01 , Ord -- ^ @since 2.01 , Read -- ^ @since 2.01 , Show -- ^ @since 2.01 , Bounded -- ^…
Nicolas Henin
  • 3,244
  • 2
  • 21
  • 42
2
votes
0 answers

Easiest way to extend GHC with new deriving capabilities?

I would like to teach GHC to derive new typeclasses. Is it possible to do this externally without using Generic or TemplateHaskell? Like, with source plugins or something similar. So I can write: data MyType = ... deriving MyTypeClass UPDATE:…
Shersh
  • 9,019
  • 3
  • 33
  • 61
2
votes
1 answer

Why isn’t this newtype being given the right Read instance?

I created a newtype alias of the IP type from Data.IP: {-# LANGUAGE GeneralizedNewtypeDeriving #-} module IPAddress (IPAddress) where import Data.IP (IP) import Database.PostgreSQL.Simple.ToField newtype IPAddress = IPAddress IP deriving…
bdesham
  • 15,430
  • 13
  • 79
  • 123
2
votes
1 answer

Make a newtype instance of Eq

I'm learning Haskell and I've been given following assignment - I have a newtype consisting of two mixed data types, and I have to make it an instance of Eq without using deriving. Here's what I have: data Number = One | Two | Three deriving Eq data…
skulpt
  • 527
  • 2
  • 6
  • 25
2
votes
1 answer

Opam install deriving, syntax error

So basically I am trying to use Opam to install deriving lib on my Mac. opam install deriving I use ocamlfind query and it seems the installation is successful. #ocamlfind query deriving …
lllllllllllll
  • 8,519
  • 9
  • 45
  • 80
2
votes
1 answer

Deriving a type and its dependencies

I've been playing with newtype wrappers for my indexes to avoid bugs, and I have some code like this: {-# LANGUAGE GeneralizedNewtypeDeriving #-} newtype PersonIdx = PersonIdx Int deriving (Enum, Eq, Integral, Num, Ord, Real, Show) To derive…
yairchu
  • 23,680
  • 7
  • 69
  • 109
2
votes
5 answers

Base class object as argument for derived class

(Simplified) Scenario: public class BaseClass { public int BaseClassInt {get; set;} public BaseClass(int pBaseClassInt) { this.BaseClassInt = pBaseClassInt; } } public class DerivedClass : BaseClass { public int DerivedClassInt…
tafkab76
  • 465
  • 1
  • 6
  • 18
1
vote
0 answers

How to create a new Data.Derive instance generator?

Does anyone know of a step-by-step tutorial of how to create a Data.Derive instance generator? There seems to be an amount of documentation for how to use an existing deriving instance generators. For those not familiar with Data.Derive (or if I'm…
gatoatigrado
  • 16,580
  • 18
  • 81
  • 143
1
vote
0 answers

Which kinds of lawfull `Functor`s are possible in Haskell?

I know following types of Functors: Polynomial functor == simple not-nested parameterized datatype Generalization of 1, by using "parameter removing" newtypes: Fix, Some (existentials), possibly some GADTs Exponential functor == X -> a for fixed…
uhbif19
  • 3,139
  • 3
  • 26
  • 48
1
vote
1 answer

Trying to add deriving(Show, Read) to an expression tree

I am fairly new to Haskell so it is probably something simple that I am missing but I have an expression tree that looks like this: data Expression = Lit Float | Add Expression Expression | Mul Expression…