ghc-generics

Defining an “mempty”-like function with GHC Generics?

爱⌒轻易说出口 提交于 2021-01-28 04:44:45
问题 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 , apResubmit :: Maybe Bool } deriving (Eq, Show, Generic) data ContactSpecialFields = ContactSpecialFields { csfCurrencySymbol :: Maybe Text -- $currency_symbol , csfState :: Maybe Text -- $state , csfProcessFlow :: Maybe Bool -- $process_flow , csfApproved ::

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

六月ゝ 毕业季﹏ 提交于 2021-01-27 21:14:10
问题 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 n-ary product (NP) which contains the corresponding constructor value at each point. For example, for the type {-# LANGUAGE DeriveGeneric #-} import qualified GHC.Generics as GHC import Generics.SOP data Foo = Bar | Baz deriving (GHC.Generic)

“packageName” with GHC.Generics

不想你离开。 提交于 2020-01-04 04:02:11
问题 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 = nameOf' . from which almost works: >>> data D = C >>> instance Named D >>> nameOf C ("","Main","D") but I can't get the datatype's package with GHC.Generics : class Named' f where nameOf' :: f a -> (String,String,String) instance (Datatype t) => Named

Using type families and Generics to find an Id value

北慕城南 提交于 2019-12-22 08:35:07
问题 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

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

半世苍凉 提交于 2019-12-10 23:23:16
问题 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 entry Building data constructors with GHC Generics which describes the analog of constructing the Rep and converting it back to a value, I got stumped. I've read through a number of other resources, but to no great help. In the blog entry is the

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

こ雲淡風輕ζ 提交于 2019-12-10 19:46:07
问题 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 Expr S and convert it to an Expr T using my conversion f on each S which occurs within an Expr value. Is there a way to do this using SYB or generics and avoid having to pattern match on every case? It seems like the type of thing that this is suited for.

Recover type definitions using GHC.Generics

℡╲_俬逩灬. 提交于 2019-12-10 18:02:55
问题 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 | Metal with the derived Generic instances, default instances for Modelable (the class of things that can recover their type definitions), and a Modelable instance for Text deriving instance Generic Artist instance Modelable Artist deriving

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

二次信任 提交于 2019-12-10 02:50:05
问题 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.) 回答1: The best place to look for lots of example functions using GHC Generics

Infinite recursion when enumerating all values of a Generic instance

[亡魂溺海] 提交于 2019-12-08 16:10:31
问题 For another answer of mine, I wrote the following code, providing diagonally traversed Universe instances for enumerable Generic s (it's slightly updated from the version there, but uses the same logic): {-# LANGUAGE DeriveGeneric, TypeOperators, ScopedTypeVariables #-} {-# LANGUAGE FlexibleInstances, FlexibleContexts, DefaultSignatures #-} {-# LANGUAGE UndecidableInstances, OverlappingInstances #-} import Data.Universe import Control.Monad.Omega import GHC.Generics import Control.Monad

How do I create a ListIsomorphic instance for generic vectors?

匆匆过客 提交于 2019-12-07 09:36:38
问题 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’ 回答1: Don