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
3
votes
2 answers

Convert from type `T a` to `T b` without boilerplate

So, I have an AST data type with a large number of cases, which is parameterized by an "annotation" type data Expr a = Plus a Int Int | ... | Times a Int Int I have annotation types S and T, and some function f :: S -> T. I want to take an…
jmite
  • 8,171
  • 6
  • 40
  • 81
3
votes
2 answers

Use GHC.Generics to restrict generic function domain

Say I have a typeclass: data Proxy a = Proxy class Fixed a where fixed :: Proxy a -> Int The definition for fixed is quite trivial so I derive it using GHC.Generics: class GFixed f where gfixed :: Proxy (f a) -> Int instance (GFixed f,…
user2407038
  • 14,400
  • 3
  • 29
  • 42
3
votes
0 answers

Derive a record datatype without template haskell

So, I've been toying around a little bit with GHC.Generics, which are great, but seem limited/focused mainly on generating instances. What I would like to do, if possible, is to derive a new data type from another. Let's say you've got the…
jcristovao
  • 554
  • 2
  • 12
2
votes
1 answer

Statically analyze type with Generics

For the purpose of generically deriving instances for FromRow-kind-of-class on simple products I would like to statically analyze a type without actually providing any concrete terms. Example: class FromRow a where rowrep :: Proxy a -> [Maybe…
user2847643
  • 2,893
  • 14
  • 22
2
votes
1 answer

Record of maps with compositional lookups and updates?

Some pseudocode: data A = A data B = B data C = C data D = D data E = E data F = F data G = G data A1 = A1 A B C data A2 = A2 A data A3 = A3 B C D data A4 = A4 D E F data A5 = A5 A1 A4 G data Foo k = Foo { a1s :: Map.Map k A1, …
PrettyPrincessKitty FS
  • 6,117
  • 5
  • 36
  • 51
2
votes
1 answer

How to "reuse" instance definitions from another typeclass while introduding minor differences?

I want to output my application's logs in JSON, but there are some ubiquitous data-types for which ToJSON instances are not defined - most notably SomeException and the entire Exception hierarchy of types. I have two choices: Define instances of…
Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60
2
votes
1 answer

Defining an "mempty"-like function with GHC Generics?

I am writing a client library for the Zoho REST API and have a bunch of different record types that have all Maybe a fields, i.e: data Approval = Approval { apDelegate :: Maybe Bool , apApprove :: Maybe Bool , apReject :: Maybe Bool ,…
Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60
2
votes
2 answers

Constructing a n-ary product with all the values of a simple sum type

I'm working with the generics-sop library. I want to write a value with the following type: values :: forall r. IsEnumType r => NP (K r) (Code r) That is, for sum types whose constructors don't have any arguments (IsEnumType) I want to produce an…
danidiaz
  • 26,936
  • 4
  • 45
  • 95
2
votes
1 answer

deriving Generic and ToJSON at the same time?

I have a module Foo.hs which contains a definition which does not derive Generic: -- Foo.hs data Blather = Blather ... -- Generic not derived here And in another module I want to derive ToJSON: -- Bar.hs {-# LANGUAGE DeriveGeneric, DeriveAnyClass…
ErikR
  • 51,541
  • 9
  • 73
  • 124
2
votes
1 answer

Understanding how to construct GHC.Generics Rep's and convert back to values

I'm trying to learn about how to use GHC.Generics. A fascinating topic but daunting. While reading through the blog entry 24 Days of GHC Extensions: DeriveGeneric, I learned how to take a value and navigate its Rep. Okay. However, reading the blog…
user1002430
2
votes
0 answers

Deriving Typeable for Text.PrettyPrint.Doc

I have an AST type that I want to derive as Typeable, so that I can do Scrap-your-boilerplate generic traversals of it. However, the tree is annotated with messages in the Doc type of the Text.PrettyPrint library from the pretty package. To derive…
jmite
  • 8,171
  • 6
  • 40
  • 81
1
vote
1 answer

Using `generics-sop` to get type metadata info at compile time

I've been using generics-sop quite successfully so far, but for one use case I'd like to get the names of record fields. The types I'm working with are product types, and I can use the constraint IsProductType a xs to enforce this. Doing so I end up…
Clinton
  • 22,361
  • 15
  • 67
  • 163
1
vote
1 answer

generics-sop: lifting a polymorphic action into a product

Using the generics-sop library, I have the following function: f :: (Applicative m) => (forall b. m (ref b)) -> m (NP I '[ref x1, ref x2]) f act = sequence_NP (act :* act :* Nil) How would I generalize this to a n-product i.e. with act in every…
Ari Fordsham
  • 2,437
  • 7
  • 28
1
vote
0 answers

Extracting an Id with GHC.Generics

How to extract an identifier (in this case an integer) from a structure using GHC.Generics? I have an Id type: newtype Id = Id { _id :: Int } and a lot of types that use that type: data VarId = VarId Name Id SortId data FuncId = FuncId Name Id…
Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
1
vote
1 answer

How to create a Mk instance for GHC.Generics.U1?

I'm working through content at the blog posting Building data constructors with GHC Generics. My previous question is here. The posting has the following code to create a Rep: {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-} {-#…
user1002430