deriving

How can I derive a Data instance for a GADT in Haskell?

怎甘沉沦 提交于 2020-01-01 10:16:34
问题 I have a GADT which is only ever used with two different parameters, ForwardPossible and (): -- | Used when a forward definition is possible. data ForwardPossible = ForwardPossible deriving (Eq, Ord, Typeable, Data, Show) -- | GADT which accepts forward definitions if parameter is ForwardPossible. data OrForward t forward where OFKnown :: t -> OrForward t forward OFForward :: NamespaceID -> SrcSpan -> BS.ByteString -> OrForward t ForwardPossible deriving instance Eq t => Eq (OrForward t

How can I derive a Data instance for a GADT in Haskell?

℡╲_俬逩灬. 提交于 2020-01-01 10:16:27
问题 I have a GADT which is only ever used with two different parameters, ForwardPossible and (): -- | Used when a forward definition is possible. data ForwardPossible = ForwardPossible deriving (Eq, Ord, Typeable, Data, Show) -- | GADT which accepts forward definitions if parameter is ForwardPossible. data OrForward t forward where OFKnown :: t -> OrForward t forward OFForward :: NamespaceID -> SrcSpan -> BS.ByteString -> OrForward t ForwardPossible deriving instance Eq t => Eq (OrForward t

How to define MonadUnliftIO instance for a newtype with a phantom type-variable?

十年热恋 提交于 2019-12-22 07:11:21
问题 Related question - Is it safe to derive MonadThrow, MonadCatch, MonadBaseControl, MonadUnliftIO, etc? - where I had enabled, both - DeriveAnyClass and GeneralizedNewtypeDeriving to get the code to compile, but didn't bother looking at the ominous warnings. Now, that I am running my refactored code, it's throwing a runtime error: No instance nor default method for class operation >>= So, I removed DeriveAnyClass and kept ONLY GeneralizedNewtypeDeriving and have the following compile error: {-#

Is it safe to derive MonadThrow, MonadCatch, MonadBaseControl, MonadUnliftIO, etc?

和自甴很熟 提交于 2019-12-20 04:31:07
问题 I'm refactoring some old code, which is in a polymorphic, but type-class constrained, monad: class ( MonadIO m , MonadLogger m , MonadLoggerIO m , MonadThrow m , MonadCatch m , MonadMask m , MonadBaseControl IO m , MonadUnliftIO) => HasLogging m where In the older code the application's main monad was... type AppM = ReaderT Env IO ...which will now change to... newtype AppM (features :: [FeatureFlag]) a = AppM (ReaderT Env IO a) deriving (Functor, Applicative, Monad, MonadReader Env, MonadIO)

Why isn’t this newtype being given the right Read instance?

不打扰是莪最后的温柔 提交于 2019-12-08 01:04:10
问题 I created a newtype alias of the IP type from Data.IP : {-# LANGUAGE GeneralizedNewtypeDeriving #-} module IPAddress (IPAddress) where import Data.IP (IP) import Database.PostgreSQL.Simple.ToField newtype IPAddress = IPAddress IP deriving (Read, Show) instance ToField IPAddress where toField ip = toField $ show ip (I wanted to make it an instance of ToField without creating an orphan instance.) The new type doesn’t seem to support Read in the way it should, though. In this GHCi transcript,

Automatic derivation of Data.Vector.Unbox with associated type synonyms

怎甘沉沦 提交于 2019-12-07 03:46:16
问题 I have a datatype newtype Zq q = Zq (IntType q) where 'q' will be an instance of the class class Foo a where type IntType a and 'IntType' is just the underlying representation (i.e. Int, Integral, etc) associated with 'q'. I want to make Zq an instance of Data.Vector.Unbox. We are currently manually deriving Unbox using about 50 lines of trivial code as suggested in the link above. We will be making several different types 'Unbox' in our code, so writing 50 lines for each type is not

Why isn’t this newtype being given the right Read instance?

末鹿安然 提交于 2019-12-06 09:23:46
I created a newtype alias of the IP type from Data.IP : {-# LANGUAGE GeneralizedNewtypeDeriving #-} module IPAddress (IPAddress) where import Data.IP (IP) import Database.PostgreSQL.Simple.ToField newtype IPAddress = IPAddress IP deriving (Read, Show) instance ToField IPAddress where toField ip = toField $ show ip (I wanted to make it an instance of ToField without creating an orphan instance.) The new type doesn’t seem to support Read in the way it should, though. In this GHCi transcript, you can see that the given string can be interpreted as an IP but not as an IPAddress : *Main IPAddress>

What is the difference between `DeriveAnyClass` and an empty instance?

萝らか妹 提交于 2019-12-05 12:05:36
问题 Using the cassava package, the following compiles: {-# LANGUAGE DeriveGeneric #-} import Data.Csv import GHC.Generics data Foo = Foo { foo :: Int } deriving (Generic) instance ToNamedRecord Foo However, the following does not: {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DeriveAnyClass #-} import Data.Csv import GHC.Generics data Foo = Foo { foo :: Int } deriving (Generic, ToNamedRecord) The compiler reports: test.hs:7:50: No instance for (ToNamedRecord Int) arising from the first field of

How to define MonadUnliftIO instance for a newtype with a phantom type-variable?

落花浮王杯 提交于 2019-12-05 09:44:05
Related question - Is it safe to derive MonadThrow, MonadCatch, MonadBaseControl, MonadUnliftIO, etc? - where I had enabled, both - DeriveAnyClass and GeneralizedNewtypeDeriving to get the code to compile, but didn't bother looking at the ominous warnings. Now, that I am running my refactored code, it's throwing a runtime error: No instance nor default method for class operation >>= So, I removed DeriveAnyClass and kept ONLY GeneralizedNewtypeDeriving and have the following compile error: {-# LANGUAGE DataKinds, GADTs, ScopedTypeVariables, TypeFamilies, AllowAmbiguousTypes, RankNTypes,

Automatic derivation of Data.Vector.Unbox with associated type synonyms

蓝咒 提交于 2019-12-05 06:44:29
I have a datatype newtype Zq q = Zq (IntType q) where 'q' will be an instance of the class class Foo a where type IntType a and 'IntType' is just the underlying representation (i.e. Int, Integral, etc) associated with 'q'. I want to make Zq an instance of Data.Vector.Unbox . We are currently manually deriving Unbox using about 50 lines of trivial code as suggested in the link above. We will be making several different types 'Unbox' in our code, so writing 50 lines for each type is not appealing. I found two alternatives here . One alternative is to use this package which uses Template Haskell