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
1
vote
1 answer

Deriving a class instance for a type based on another class it's an instance of

I have a situation where I have some similar newtypes that all need to be instances of Random, Arbitrary, and lots of other stuff. They all need the same custom implementation of the functions randomR, random, arbitrary, etc. So I put all of those…
mhwombat
  • 8,026
  • 28
  • 53
1
vote
1 answer

How to make `co-log`'s `withLog` work with `Scotty`?

I already asked on Reddit but wanted to ask a wider circle for help. Here's a repository with code that you can run for a minimal test case: https://github.com/cideM/co_log_issue If you run stack build you'll get: • Could not deduce (HasLog …
Vey
  • 435
  • 5
  • 15
1
vote
1 answer

How to override/provide custom instances using bs-deriving

Using bs-deriving, I can derive e.g. show instances using [@deriving show]. However, it's not clear how I would use the same derivation but providing a custom show instance for a specific datatype. Example: [@deriving show] type bar =…
Felix
  • 8,385
  • 10
  • 40
  • 59
1
vote
1 answer

Is it safe to derive MonadThrow, MonadCatch, MonadBaseControl, MonadUnliftIO, etc?

I'm refactoring some old code, which is in a polymorphic, but type-class constrained, monad: class ( MonadIO m , MonadLogger m , MonadLoggerIO m , MonadThrow m , MonadCatch m , MonadMask m , MonadBaseControl IO m …
Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60
1
vote
2 answers

How to write "deriving instance" declaration?

I'm trying to store a crypto hash as a field in a record type, but the compiler complains: HashTest.hs:13:1: error: • No instance for (Data (Digest MD5)) arising from the second field of ‘MyRecord’ (type ‘Digest MD5’) Possible fix: use a…
Gareth Stockwell
  • 3,112
  • 18
  • 23
1
vote
1 answer

Derive positional Show

Notice how T 5 shows in > newtype T = T { getT :: Int } deriving Show > T 5 T {getT = 5} Is there some way to derive the positional, non-record-syntax variant of Show for a type that was declared with record syntax? (btw T is only a simple example…
yairchu
  • 23,680
  • 7
  • 69
  • 109
1
vote
1 answer

Using Deriving Via with Phantom Types

Apologies for the long repro but I haven't been able to make it any shorter. The following code compiles fine until the last line: {-# LANGUAGE RankNTypes #-} {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, UndecidableInstances #-} {-#…
Julian Birch
  • 2,605
  • 1
  • 21
  • 36
1
vote
1 answer

Deriving MonadFree from a newtype with a transformer stack

I’m trying to derive MonadFree from a newtype and I just can’t work it out. My current code is: newtype ApplicationStack s r p m = ApplicationStack { runApplication :: StateT s (ReaderT r p) m } deriving (Functor, Applicative, Monad, MonadState…
tomphp
  • 307
  • 1
  • 4
  • 10
1
vote
1 answer

How to go from a value of a finite discrete type to a (Finite n) and back, using the type's derived Generic instance, in Haskell?

I have a library that currently demands of users that they provide a helper function with type: tEnum :: (KnownNat n) => MyType -> Finite n so that the library implementation can use a very efficient sized vector representation of a function with…
dbanas
  • 1,707
  • 14
  • 24
1
vote
1 answer

When should we put parenthesis after deriving?

I see two expressions data BinTree a = BTNil | BTNode a (BinTree a) (BinTree a) deriving Show data Day = Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday deriving (Enum) I am confusing when I should use…
user8314628
  • 1,952
  • 2
  • 22
  • 46
1
vote
1 answer

Haskell deriving Show error

I did some research about Colored Petri Nets for a university assessment, and I need to implement them in Haskell. I used this document as a point of start. When I am trying to import this module in Haskell: module SimpleHCPN where import…
1
vote
2 answers

Writing a proper custom read instance

Hello fellow Haskellers, I am learning Haskell from one month now, and I am struggling in creating a custom read instance for a personnal data type. I followed this and the relevant chapter in Learn Yourself a Haskell, here is my code snippet. …
1
vote
1 answer

Fail to read a self defined data type

data MyNum = One | Two | Three deriving (Show, Eq) I just define MyNum with constructor One, Two and Three. *Main> :t One One :: MyNum But ghci produces errors when I add x = read("One")::MyNum to my program: No…
Dong
  • 13
  • 3
1
vote
0 answers

Automatically derive Unbox for newtype without TH

So I've got a pretty basic newtype: newtype SomeID = SomeID Word64 deriving (Show,Eq) And I would like to use this type in an unboxed vector, but on first inspection this seems to be more complicated than deriving...say...Storable. And when I say…
carpemb
  • 691
  • 1
  • 8
  • 19
1
vote
1 answer

Automatic haskell deriving declaration that lifts

Is there a way to have the compiler derive the functionality that I would write manually as: instance Class c => Class (Trans c) where foo1 = lift foo1 foo2 = lift foo2 ... foo999 = lift foo999 bar1 = \a b c -> lift $ bar1 a b c …
jakubdaniel
  • 2,233
  • 1
  • 13
  • 20