ghc-generics

Extracting an Id with GHC.Generics

↘锁芯ラ 提交于 2019-12-06 13:58:12
问题 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 [SortId] SortId data SortId = Name Id -- ... and 20 more of these things On top of that there are other types that wrap the types above, for instance, identifiers: data Identifier = IdVar VarId | IdFunc FuncId | IdSort SortId -- ... and 20 more of

How do I create a ListIsomorphic instance for generic vectors?

强颜欢笑 提交于 2019-12-05 14:26:26
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 where toList = V.toList fromList = V.fromList Giving me: test.hs:31:10: Variable ‘a’ occurs more often than in the instance head in the constraint: V.Vector v a (Use UndecidableInstances to permit this) In the instance declaration for ‘ListIsomorphic v’ Don't . Adding an instance for all v to your Listable class will become cumbersome to use due to overlapping

Using type families and Generics to find an Id value

不羁的心 提交于 2019-12-05 12:02:21
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 = Id { _id :: Int } And you want to define a function getId that extracts this Id from any structure that contains at least one Id value: class Identifiable e where getId :: e -> Id Now the problem is how to define such a class in a type-safe manner, while at the same time avoiding the boilerplate using Generics. In my previous question I was

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

岁酱吖の 提交于 2019-12-05 03:43:26
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 parametrized data types with Generics, all the examples I've seen were of kind * . Is this possible, and if yes, how? (I'm also interested in other similar frameworks, such as SYB.) The best place to look for lots of example functions using GHC Generics is the generic-deriving package . There's a generic definition of the Functor class in there. Copying

Extracting an Id with GHC.Generics

烂漫一生 提交于 2019-12-04 20:02:15
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 [SortId] SortId data SortId = Name Id -- ... and 20 more of these things On top of that there are other types that wrap the types above, for instance, identifiers: data Identifier = IdVar VarId | IdFunc FuncId | IdSort SortId -- ... and 20 more of these things Now I need to extract the _id field from any of these types containing an Id value. Currently