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

Is it possible to derive a Traversable instance via another type constructor?

Suppose we have some class Foo such that an instance of Foo f gives us everything necessary to implement Functor f, Foldable f and Traversable f. To avoid overlapping instances, can witness this relationship between Foo and Functor, Foldable,…
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
6
votes
0 answers

How to implementing a fork function that combines two consumer into one

I'm trying to build a streaming library using the abstractions described in the paper "Faster coroutine pipelines". I've modified the code so that it correctly handles pipeline exiting (instead of throwing out errors when that happens): -- | r:…
Poscat
  • 565
  • 3
  • 15
6
votes
2 answers

Haskell Monoid Instance Question for a newtype

I am trying to define an instance: newtype Join a = Join { getJoin :: a -> Bool } deriving Generic instance Monoid (Join a) where f <> g = ??? mempty = ??? The goal is that the function foldMap Join should return True if all functions in…
6
votes
1 answer

Haskell: deriving Show for Fix types

I'm trying to implement a recursive datatype using recursion-schemes. I would like to be able to print it. import Data.Functor.Foldable data T1F a = Foo deriving Show type T1 = Fix T1F data T2 = Bar T1 deriving Show -- error here Error message: No…
Carl Patenaude Poulin
  • 6,238
  • 5
  • 24
  • 46
6
votes
1 answer

Cannot make a derived instance of monad transformer

I have the following newtype: {-# LANGUAGE GeneralizedNewtypeDeriving #-} newtype Wrap m a = Wrap {runWrap :: m a} deriving (Functor, Applicative, Monad, MonadTrans) I'm trying to derive MonadTrans automatically, but I get the following error: •…
Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
6
votes
1 answer

Unable to derive Applicative when combining two monad transformer stacks

I've written two monads for a domain-specific language I'm developing. The first is Lang, which is supposed to include everything needed to parse the language line by line. I knew I would want reader, writer, and state, so I used the RWS monad: type…
jefdaj
  • 2,025
  • 2
  • 21
  • 33
6
votes
2 answers

Why does Either derives Show but Maybe does not?

Documentation of both Either and Maybe indicate that they have instances of Show. Either is defined as deriving Show, simply : data Either a b = Left a | Right b deriving (Eq, Ord, Read, Show, Typeable) Yet, Maybe does not : data Maybe a = …
gxtaillon
  • 1,016
  • 1
  • 19
  • 33
6
votes
1 answer

js_of_ocaml and Deriving_Json

I need some help to get js_of_ocaml working. There's not much information about it on the net, and the manual is very sparse (no snippets or usage examples, no comment sections). I have a Card module on the server with a card record. I'm sending a…
Olle Härstedt
  • 3,799
  • 1
  • 24
  • 57
6
votes
1 answer

Error: Invalid base class C++

Could anyone, please, explain what can cause this error? Error: Invalid base class I've got two classes where one of them is derived from second: #if !defined(_CGROUND_H) #define _CGROUND_H #include "stdafx.h" #include "CGameObject.h" class…
dziwna
  • 1,212
  • 2
  • 14
  • 25
6
votes
1 answer

How can I derive a Data instance for a GADT in Haskell?

I have a GADT which is only ever used with two different parameters, ForwardPossible and (): -- | Used when a forward definition is possible. data ForwardPossible = ForwardPossible deriving (Eq, Ord, Typeable, Data, Show) -- | GADT which accepts…
a1kmm
  • 1,354
  • 7
  • 13
5
votes
2 answers

Deriving from classes generated by Entity Framework in C#

I have created an entity data model and generated a database from it. One of the entities is called Template. Created partial classes to extend the functionality of Template works fine. If I create a new class and try to derive from Template, I get…
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
5
votes
1 answer

How can I coerce constraints?

Is there any mechanism to coerce constraints in Haskell (beside unsafeCoerce which I hope works)? {-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE DerivingVia #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE RankNTypes…
nicolas
  • 9,549
  • 3
  • 39
  • 83
5
votes
3 answers

Are there laws for the Foldable typeclass that constrain how Foldable instances can be derived?

I'm experimenting with the Foldable typeclass in Haskell, using the following data type as an example: data Tree a = Empty | Node (Tree a) a (Tree a) If I use the DeriveFoldable GHC extension, it seems to derive a Foldable instance…
DylanSp
  • 1,379
  • 13
  • 27
5
votes
1 answer

Derive instance from field in record

Example code: {-# LANGUAGE NamedFieldPuns #-} module Sample where class Sample a where isA :: a -> Bool isB :: a -> Bool isC :: a -> Bool data X = X instance Sample X where isA = undefined isB = undefined isC = undefined data…
Allan W
  • 2,791
  • 4
  • 23
  • 41
5
votes
3 answers

Avoiding repeated instance declarations in Haskell

My question seems to be closely related to this one. My code parses a yaml file, rearanges the objects and writes a new yaml file. It works perfectly well, but there is a particularly ugly part in it. I have to declare my data structures as…
fata
  • 87
  • 1
  • 6
1 2
3
9 10