Questions tagged [derivingvia]

In Haskell, DerivingVia is a deriving strategy generalizing GeneralizedNewtypeDeriving. DerivingVia allows deriving instances of a type using a via type that is representationally equal to it which determines the instance behavior. Unlike GND, DerivingVia works for both 'data' and 'newtype' declarations and is not limited to a single behavior.

For details, consult the GHC manual section on Deriving via.

40 questions
18
votes
3 answers

Why is there no `-XDeriveApplicative` extension?

GHC has several useful language extensions for mechanically deriving various common Haskell typeclasses (-XDeriveFunctor, -XDeriveFoldable, -XDeriveTraversable). It seems that Applicative is another class which is often needed and frequently easily…
bgamari
  • 5,913
  • 1
  • 18
  • 21
17
votes
2 answers

Why there is no way to derive Applicative Functors in Haskell?

In Haskell, you can derive Functor, Foldable and Traversable automatically using deriving. There is no way to derive Applicative, though. Considering there is one obvious way to define an Applicative instance (which would amount to a zipped…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
12
votes
1 answer

Deriving Via With Standalone Deriving

I'm not really sure what I'm doing wrong here: data Vector2D u = Vector2D { _x :: u, _y :: u } deriving stock (Show, Eq, Functor, Foldable, Traversable) {-# INLINE addVector2 #-} addVector2 :: (Additive a) => Vector2D a -> Vector2D a ->…
Julian Birch
  • 2,605
  • 1
  • 21
  • 36
11
votes
2 answers

Quantified constraints vs. (closed) type families

I am trying to use this blogpost's approach to higher-kinded data without dangling Identity functors for the trival case together with quantified-constraint deriving: {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE QuantifiedConstraints,…
Cactus
  • 27,075
  • 9
  • 69
  • 149
10
votes
3 answers

Haskell: Overlapping instances

Consider the following example program: next :: Int -> Int next i | 0 == m2 = d2 | otherwise = 3 * i + 1 where (d2, m2) = i `divMod` 2 loopIteration :: MaybeT (StateT Int IO) () loopIteration = do i <- get guard $ i > 1 liftIO $…
yairchu
  • 23,680
  • 7
  • 69
  • 109
8
votes
3 answers

Is it possible to establish Coercible instances between custom types and standard library ones?

For a simple example, say I want a type to represent tic-tac-toe marks: data Mark = Nought | Cross Which is the same as Bool Prelude> :info Bool data Bool = False | True -- Defined in ‘GHC.Types’ But there's no Coercible Bool Mark between them,…
Javran
  • 3,394
  • 2
  • 23
  • 40
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…
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
2 answers

How to convert a custom type to an Integer in Haskell?

I am trying to use my own data type in haskell for prime numbers, but i am currently running into a few issues. newtype Prime = Prime Integer deriving (Eq, Ord, Typeable, Show) As soon as i am doing any numeric operation on a prime number (e.g. the…
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
5
votes
1 answer

Haskell DerivingVia on multi param type classes with fun deps

I'm trying to use DerivingVia to cut the boilerplate on instance definitions for a multi parameter type class with functional dependencies. I have these types and class: {-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE StandaloneDeriving #-} {-#…
Guillaum
  • 170
  • 6
4
votes
1 answer

Derive typeclass instances for opaque types in Scala 3

Is there a way in Scala 3 to use derives keyword in combination with opaque type aliases? It would be nice to have a boilerplate-free way to provide a typeclass instance to a given opaque type alias by automatically rely on the instance of the same…
4
votes
2 answers

Can I use DerivingVia to derive instances for data types isomorphic to tuples

Given the following data type data Both a b = Both { left :: a, right :: b } I can write instances for Applicative etc. like so (omitting Functor here as we can use DeriveFunctor): instance Monoid a => Applicative (Both a) where pure x = Both…
l7r7
  • 1,134
  • 1
  • 7
  • 23
1
2 3