Questions tagged [ghc-generics]

Generic programming support in GHC allows defining classes with methods that do not need a user specification when instantiating: the method body is automatically derived by GHC. This is similar to what happens for standard classes such as Read and Show, for instance, but now for user-defined classes.

See also:

33 questions
9
votes
1 answer

Infinite recursion when enumerating all values of a Generic instance

For another answer of mine, I wrote the following code, providing diagonally traversed Universe instances for enumerable Generics (it's slightly updated from the version there, but uses the same logic): {-# LANGUAGE DeriveGeneric, TypeOperators,…
phipsgabler
  • 20,535
  • 4
  • 40
  • 60
9
votes
2 answers

How to construct generic Functor instances using GHC.Generics (or other similar frameworks)?

I'm trying to learn GHC Generics. After reviewing several examples, I wanted to try to create a generic Functor instances (disregarding that GHC can derive them automatically for me). However, I realized I have no idea how to work with a…
Petr
  • 62,528
  • 13
  • 153
  • 317
8
votes
1 answer

Advantages of SYB (scrap your boilerplate) over GHC Generics

Are any tasks that are possible only with SYB, or are much easier with it, when compared to GHC Generics?
Petr
  • 62,528
  • 13
  • 153
  • 317
7
votes
2 answers

Implement Applicative builder style with Generics

Context If we have data Foo = Foo { x :: Maybe Int, y :: Maybe Text } we can already build it up applicative-style in an Applicative context (here IO) as myfoo :: IO Foo myfoo = Foo <$> getEnvInt "someX" <*> getEnvText "someY" Problem What if one…
ron
  • 9,262
  • 4
  • 40
  • 73
6
votes
2 answers

Deriving projection functions using `generics-sop`

How would I go about deriving the function getField :: (Generic a, HasDatatypeInfo a) => Proxy (name :: Symbol) -> a -> b to project a field from an arbitrary record using a type-level string (Symbol), using the generics-sop library? This is…
4
votes
1 answer

Would it be possible to derive Data.Vector.Unbox via GHC's generic deriving?

It's possible to derive Storable via GHC's generic deriving mechanism: http://hackage.haskell.org/package/derive-storable (and https://hackage.haskell.org/package/derive-storable-plugin for performance). The only library I can find for deriving…
LogicChains
  • 4,332
  • 2
  • 18
  • 27
4
votes
1 answer

How to derive generic traversals that involve a type family

When configuring our applications, often the way that field is defined is the same as the way the field is used: data CfgMyHostName = CfgMyHostName Text Other times, they differ. Let's make this formal in a typeclass: data UsagePhase = ConfigTime |…
ImAlsoGreg
  • 655
  • 6
  • 17
4
votes
1 answer

Using type families and Generics to find an Id value

This question is related to this one, where I wanted to avoid the boilerplate of extracting an Id value from a data structure, but in a type-safe manner. I'll repeat the relevant details of the problem here: suppose you have a type Id: newtype Id =…
Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
4
votes
1 answer

Retrieving record function in generic SOP

In Sum of Products approach, how would one retrieve the record function? An example code below with record datatype (ghc 7.10.3): {-# LANGUAGE DeriveGeneric #-} import qualified GHC.Generics as GHC import Generics.SOP data Rec = Rec { frec :: Int,…
Sal
  • 4,312
  • 1
  • 17
  • 26
4
votes
2 answers

How do I create a ListIsomorphic instance for generic vectors?

Given the following class: class ListIsomorphic l where toList :: l a -> [a] fromList :: [a] -> l a How can I write a instance for vector types using Data.Vector.Generic? This doesn't work: instance (V.Vector v a) => ListIsomorphic v…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
4
votes
2 answers

Deriving functor instance, not on last type argument

Related to this question I asked earlier today. I have an AST data type with a large number of cases, which is parameterized by an "annotation" type data Expr ann def var = Plus a Int Int | ... | Times a Int Int deriving (Data, Typeable,…
jmite
  • 8,171
  • 6
  • 40
  • 81
4
votes
0 answers

Recover type definitions using GHC.Generics

Yesterday I took a swing at trying to answer this question about a representation for a datatype. using GHC.Generics. I could recover type definitions for the example problem given, for example, for: data Artist = Artist Text Genre data Genre = Jazz…
Cirdec
  • 24,019
  • 2
  • 50
  • 100
3
votes
2 answers

How do I replace Data.Generics with GHC.Generics?

So I've used syb for a long time, and often have functions like friendlyNames :: Data a => a -> a friendlyNames = everywhere (mkT (\(Name x _) -> Name x NameS)) What is the equivalent of this using GHC.Generics, assuming Generic a?
David Fox
  • 654
  • 4
  • 10
3
votes
1 answer

No instance for (Generic (f a)) arising from a use of `from'

I'm having issues with finding suitable type constraints for the following code {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DefaultSignatures #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE FlexibleContexts #-} import GHC.Generics data Value = One…
sgillis
  • 242
  • 2
  • 6
3
votes
1 answer

"packageName" with GHC.Generics

I have a class that provides a globally unique identifier for types: class Named a where nameOf :: a -> (String,String,String) -- (Package, Module, Identifier) default nameOf :: (Generic a, Named' (Rep a)) => a -> (String,String,String) nameOf =…
sam boosalis
  • 1,997
  • 4
  • 20
  • 32
1
2 3