derive NFData in a way compatible with GHC < 7.10

The DeriveAnyClass extension requires GHC 7.10, so instead do this in a
more verbose backwards-compatible way. Adds a dependency on deepseq.
This commit is contained in:
Simon Michael 2015-07-01 16:08:39 -07:00
parent 790d42bfa4
commit 632a000f08
2 changed files with 68 additions and 23 deletions

View File

@ -1,4 +1,4 @@
{-# LANGUAGE CPP, DeriveDataTypeable, StandaloneDeriving, DeriveAnyClass, DeriveGeneric, TypeSynonymInstances, FlexibleInstances #-} {-# LANGUAGE CPP, DeriveDataTypeable, StandaloneDeriving, DeriveGeneric, TypeSynonymInstances, FlexibleInstances #-}
{-| {-|
Most data types are defined here to avoid import cycles. Most data types are defined here to avoid import cycles.
@ -39,21 +39,29 @@ type SmartDate = (String,String,String)
data WhichDate = PrimaryDate | SecondaryDate deriving (Eq,Show) data WhichDate = PrimaryDate | SecondaryDate deriving (Eq,Show)
data DateSpan = DateSpan (Maybe Day) (Maybe Day) deriving (Eq,Ord,Data,NFData,Generic,Typeable) data DateSpan = DateSpan (Maybe Day) (Maybe Day) deriving (Eq,Ord,Data,Generic,Typeable)
instance NFData DateSpan
data Interval = NoInterval data Interval = NoInterval
| Days Int | Weeks Int | Months Int | Quarters Int | Years Int | Days Int | Weeks Int | Months Int | Quarters Int | Years Int
| DayOfMonth Int | DayOfWeek Int | DayOfMonth Int | DayOfWeek Int
-- WeekOfYear Int | MonthOfYear Int | QuarterOfYear Int -- WeekOfYear Int | MonthOfYear Int | QuarterOfYear Int
deriving (Eq,Show,Ord,Data,NFData,Generic,Typeable) deriving (Eq,Show,Ord,Data,Generic,Typeable)
instance NFData Interval
type AccountName = String type AccountName = String
data AccountAlias = BasicAlias AccountName AccountName data AccountAlias = BasicAlias AccountName AccountName
| RegexAlias Regexp Replacement | RegexAlias Regexp Replacement
deriving (Eq, Read, Show, Ord, Data, NFData,Generic, Typeable) deriving (Eq, Read, Show, Ord, Data, Generic, Typeable)
data Side = L | R deriving (Eq,Show,Read,Ord,Typeable,Data,NFData,Generic) instance NFData AccountAlias
data Side = L | R deriving (Eq,Show,Read,Ord,Typeable,Data,Generic)
instance NFData Side
type Commodity = String type Commodity = String
@ -76,7 +84,9 @@ numberRepresentation = "Decimal"
-- | An amount's price (none, per unit, or total) in another commodity. -- | An amount's price (none, per unit, or total) in another commodity.
-- Note the price should be a positive number, although this is not enforced. -- Note the price should be a positive number, although this is not enforced.
data Price = NoPrice | UnitPrice Amount | TotalPrice Amount deriving (Eq,Ord,Typeable,Data,NFData,Generic) data Price = NoPrice | UnitPrice Amount | TotalPrice Amount deriving (Eq,Ord,Typeable,Data,Generic)
instance NFData Price
-- | Display style for an amount. -- | Display style for an amount.
data AmountStyle = AmountStyle { data AmountStyle = AmountStyle {
@ -85,7 +95,9 @@ data AmountStyle = AmountStyle {
asprecision :: Int, -- ^ number of digits displayed after the decimal point asprecision :: Int, -- ^ number of digits displayed after the decimal point
asdecimalpoint :: Maybe Char, -- ^ character used as decimal point: period or comma. Nothing means "unspecified, use default" asdecimalpoint :: Maybe Char, -- ^ character used as decimal point: period or comma. Nothing means "unspecified, use default"
asdigitgroups :: Maybe DigitGroupStyle -- ^ style for displaying digit groups, if any asdigitgroups :: Maybe DigitGroupStyle -- ^ style for displaying digit groups, if any
} deriving (Eq,Ord,Read,Show,Typeable,Data,NFData,Generic) } deriving (Eq,Ord,Read,Show,Typeable,Data,Generic)
instance NFData AmountStyle
-- | A style for displaying digit groups in the integer part of a -- | A style for displaying digit groups in the integer part of a
-- floating point number. It consists of the character used to -- floating point number. It consists of the character used to
@ -94,24 +106,34 @@ data AmountStyle = AmountStyle {
-- the decimal point. The last group size is assumed to repeat. Eg, -- the decimal point. The last group size is assumed to repeat. Eg,
-- comma between thousands is DigitGroups ',' [3]. -- comma between thousands is DigitGroups ',' [3].
data DigitGroupStyle = DigitGroups Char [Int] data DigitGroupStyle = DigitGroups Char [Int]
deriving (Eq,Ord,Read,Show,Typeable,Data,NFData,Generic) deriving (Eq,Ord,Read,Show,Typeable,Data,Generic)
instance NFData DigitGroupStyle
data Amount = Amount { data Amount = Amount {
acommodity :: Commodity, acommodity :: Commodity,
aquantity :: Quantity, aquantity :: Quantity,
aprice :: Price, -- ^ the (fixed) price for this amount, if any aprice :: Price, -- ^ the (fixed) price for this amount, if any
astyle :: AmountStyle astyle :: AmountStyle
} deriving (Eq,Ord,Typeable,Data,NFData,Generic) } deriving (Eq,Ord,Typeable,Data,Generic)
newtype MixedAmount = Mixed [Amount] deriving (Eq,Ord,Typeable,Data,NFData,Generic) instance NFData Amount
newtype MixedAmount = Mixed [Amount] deriving (Eq,Ord,Typeable,Data,Generic)
instance NFData MixedAmount
data PostingType = RegularPosting | VirtualPosting | BalancedVirtualPosting data PostingType = RegularPosting | VirtualPosting | BalancedVirtualPosting
deriving (Eq,Show,Typeable,Data,NFData,Generic) deriving (Eq,Show,Typeable,Data,Generic)
instance NFData PostingType
type Tag = (String, String) -- ^ A tag name and (possibly empty) value. type Tag = (String, String) -- ^ A tag name and (possibly empty) value.
data ClearedStatus = Uncleared | Pending | Cleared data ClearedStatus = Uncleared | Pending | Cleared
deriving (Eq,Ord,Typeable,Data,NFData,Generic) deriving (Eq,Ord,Typeable,Data,Generic)
instance NFData ClearedStatus
instance Show ClearedStatus where -- custom show instance Show ClearedStatus where -- custom show
show Uncleared = "" -- a bad idea show Uncleared = "" -- a bad idea
@ -130,7 +152,9 @@ data Posting = Posting {
pbalanceassertion :: Maybe MixedAmount, -- ^ optional: the expected balance in the account after this posting pbalanceassertion :: Maybe MixedAmount, -- ^ optional: the expected balance in the account after this posting
ptransaction :: Maybe Transaction -- ^ this posting's parent transaction (co-recursive types). ptransaction :: Maybe Transaction -- ^ this posting's parent transaction (co-recursive types).
-- Tying this knot gets tedious, Maybe makes it easier/optional. -- Tying this knot gets tedious, Maybe makes it easier/optional.
} deriving (Typeable,Data,NFData,Generic) } deriving (Typeable,Data,Generic)
instance NFData Posting
-- The equality test for postings ignores the parent transaction's -- The equality test for postings ignores the parent transaction's
-- identity, to avoid infinite loops. -- identity, to avoid infinite loops.
@ -140,7 +164,9 @@ instance Eq Posting where
-- | The position of parse errors (eg), like parsec's SourcePos but generic. -- | The position of parse errors (eg), like parsec's SourcePos but generic.
-- File name, 1-based line number and 1-based column number. -- File name, 1-based line number and 1-based column number.
data GenericSourcePos = GenericSourcePos FilePath Int Int data GenericSourcePos = GenericSourcePos FilePath Int Int
deriving (Eq, Read, Show, Ord, Data, NFData, Generic, Typeable) deriving (Eq, Read, Show, Ord, Data, Generic, Typeable)
instance NFData GenericSourcePos
data Transaction = Transaction { data Transaction = Transaction {
tsourcepos :: GenericSourcePos, tsourcepos :: GenericSourcePos,
@ -153,19 +179,27 @@ data Transaction = Transaction {
ttags :: [Tag], -- ^ tag names and values, extracted from the comment ttags :: [Tag], -- ^ tag names and values, extracted from the comment
tpostings :: [Posting], -- ^ this transaction's postings tpostings :: [Posting], -- ^ this transaction's postings
tpreceding_comment_lines :: String -- ^ any comment lines immediately preceding this transaction tpreceding_comment_lines :: String -- ^ any comment lines immediately preceding this transaction
} deriving (Eq,Typeable,Data,NFData,Generic) } deriving (Eq,Typeable,Data,Generic)
instance NFData Transaction
data ModifierTransaction = ModifierTransaction { data ModifierTransaction = ModifierTransaction {
mtvalueexpr :: String, mtvalueexpr :: String,
mtpostings :: [Posting] mtpostings :: [Posting]
} deriving (Eq,Typeable,Data,NFData,Generic) } deriving (Eq,Typeable,Data,Generic)
instance NFData ModifierTransaction
data PeriodicTransaction = PeriodicTransaction { data PeriodicTransaction = PeriodicTransaction {
ptperiodicexpr :: String, ptperiodicexpr :: String,
ptpostings :: [Posting] ptpostings :: [Posting]
} deriving (Eq,Typeable,Data,NFData,Generic) } deriving (Eq,Typeable,Data,Generic)
data TimeLogCode = SetBalance | SetRequiredHours | In | Out | FinalOut deriving (Eq,Ord,Typeable,Data,NFData,Generic) instance NFData PeriodicTransaction
data TimeLogCode = SetBalance | SetRequiredHours | In | Out | FinalOut deriving (Eq,Ord,Typeable,Data,Generic)
instance NFData TimeLogCode
data TimeLogEntry = TimeLogEntry { data TimeLogEntry = TimeLogEntry {
tlsourcepos :: GenericSourcePos, tlsourcepos :: GenericSourcePos,
@ -173,13 +207,17 @@ data TimeLogEntry = TimeLogEntry {
tldatetime :: LocalTime, tldatetime :: LocalTime,
tlaccount :: String, tlaccount :: String,
tldescription :: String tldescription :: String
} deriving (Eq,Ord,Typeable,Data,NFData,Generic) } deriving (Eq,Ord,Typeable,Data,Generic)
instance NFData TimeLogEntry
data HistoricalPrice = HistoricalPrice { data HistoricalPrice = HistoricalPrice {
hdate :: Day, hdate :: Day,
hcommodity :: Commodity, hcommodity :: Commodity,
hamount :: Amount hamount :: Amount
} deriving (Eq,Typeable,Data, NFData,Generic) -- & Show (in Amount.hs) } deriving (Eq,Typeable,Data,Generic) -- & Show (in Amount.hs)
instance NFData HistoricalPrice
type Year = Integer type Year = Integer
@ -194,13 +232,16 @@ data JournalContext = Ctx {
-- specified with "account" directive(s). Concatenated, these -- specified with "account" directive(s). Concatenated, these
-- are the account prefix prepended to parsed account names. -- are the account prefix prepended to parsed account names.
, ctxAliases :: ![AccountAlias] -- ^ the current list of account name aliases in effect , ctxAliases :: ![AccountAlias] -- ^ the current list of account name aliases in effect
} deriving (Read, Show, Eq, Data, Typeable, NFData,Generic) } deriving (Read, Show, Eq, Data, Typeable, Generic)
instance NFData JournalContext
deriving instance Data (ClockTime) deriving instance Data (ClockTime)
deriving instance Typeable (ClockTime) deriving instance Typeable (ClockTime)
deriving instance NFData (ClockTime)
deriving instance Generic (ClockTime) deriving instance Generic (ClockTime)
instance NFData ClockTime
data Journal = Journal { data Journal = Journal {
jmodifiertxns :: [ModifierTransaction], jmodifiertxns :: [ModifierTransaction],
jperiodictxns :: [PeriodicTransaction], jperiodictxns :: [PeriodicTransaction],
@ -215,7 +256,9 @@ data Journal = Journal {
-- order encountered. -- order encountered.
filereadtime :: ClockTime, -- ^ when this journal was last read from its file(s) filereadtime :: ClockTime, -- ^ when this journal was last read from its file(s)
jcommoditystyles :: M.Map Commodity AmountStyle -- ^ how to display amounts in each commodity jcommoditystyles :: M.Map Commodity AmountStyle -- ^ how to display amounts in each commodity
} deriving (Eq, Typeable, Data, NFData,Generic) } deriving (Eq, Typeable, Data, Generic)
instance NFData Journal
-- | A JournalUpdate is some transformation of a Journal. It can do I/O or -- | A JournalUpdate is some transformation of a Journal. It can do I/O or
-- raise an error. -- raise an error.

View File

@ -98,6 +98,7 @@ library
,csv ,csv
-- ,data-pprint >= 0.2.3 && < 0.3 -- ,data-pprint >= 0.2.3 && < 0.3
,Decimal ,Decimal
,deepseq
,directory ,directory
,filepath ,filepath
,mtl ,mtl
@ -135,6 +136,7 @@ test-suite tests
, csv , csv
-- , data-pprint >= 0.2.3 && < 0.3 -- , data-pprint >= 0.2.3 && < 0.3
, Decimal , Decimal
, deepseq
, directory , directory
, filepath , filepath
, HUnit , HUnit