From 565236d0577a55af9dfd546e444d7bc5424827d1 Mon Sep 17 00:00:00 2001 From: Ryan Scott Date: Tue, 28 Nov 2023 07:59:03 -0500 Subject: [PATCH] Add Data instances for BigFloat, Sign, BFRep, BFNum Fixes #28. --- CHANGELOG.md | 4 ++++ src/LibBF.hs | 10 ++++++++++ src/LibBF/Mutable.hsc | 8 +++++--- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b53fffb..0ff92fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Revision history for libBF-hs +## next -- TBA + +* Add `Data` instances for `BigFloat`, `Sign`, `BFRep`, and `BFNum`. + ## 0.6.6 -- 2023.07.17 * Update the vendored version of `libbf` to include the changes from the diff --git a/src/LibBF.hs b/src/LibBF.hs index b2a1022..3ace56e 100644 --- a/src/LibBF.hs +++ b/src/LibBF.hs @@ -61,6 +61,7 @@ module LibBF import Data.Bits +import Data.Data (Data(..)) import Data.Hashable import Data.Word import Data.Int @@ -73,6 +74,15 @@ import Control.DeepSeq -- | Arbitrary precision floating point numbers. newtype BigFloat = BigFloat BF +instance Data BigFloat where + -- BigFloat is exported as an abstract data type, so we intentionally define + -- the Data instance in a simplistic way so as to avoid leaking the BF + -- internals. + gfoldl _ z = z + gunfold _ _ = error "Data.Data.gunfold(BigFloat)" + toConstr _ = error "Data.Data.toConstr(BigFloat)" + dataTypeOf _ = error "Data.Data.dataTypeOf(BigFloat)" + instance NFData BigFloat where rnf x = x `seq` () diff --git a/src/LibBF/Mutable.hsc b/src/LibBF/Mutable.hsc index 50e6cb9..f9810d8 100644 --- a/src/LibBF/Mutable.hsc +++ b/src/LibBF/Mutable.hsc @@ -2,6 +2,7 @@ {-# Language PatternSynonyms #-} {-# Language MultiWayIf #-} {-# Language BlockArguments #-} +{-# Language DeriveDataTypeable #-} -- | Mutable big-float computation. module LibBF.Mutable ( -- * Allocation @@ -69,6 +70,7 @@ import Foreign.Ptr(Ptr,FunPtr,minusPtr) import Foreign.ForeignPtr import Foreign.C.Types import Foreign.C.String +import Data.Data (Data) import Data.Word import Data.Int import Data.Bits @@ -175,7 +177,7 @@ bf3 f (BF fin1) (BF fin2) (BF fout) = -- | Indicates if a number is positive or negative. data Sign = Neg {-^ Negative -} | Pos {-^ Positive -} - deriving (Eq,Ord,Show) + deriving (Data,Eq,Ord,Show) foreign import ccall "bf_set_nan" @@ -553,7 +555,7 @@ toString radix (ShowFmt ds flags) = -- | An explicit representation for big nums. data BFRep = BFRep !Sign !BFNum -- ^ A signed number | BFNaN -- ^ Not a number - deriving (Eq,Ord,Show) + deriving (Data,Eq,Ord,Show) instance Hashable BFRep where hashWithSalt s BFNaN = s `hashWithSalt` (0::Int) @@ -564,7 +566,7 @@ instance Hashable BFRep where data BFNum = Zero -- ^ zero | Num Integer !Int64 -- ^ @x * 2 ^ y@ | Inf -- ^ infinity - deriving (Eq,Ord,Show) + deriving (Data,Eq,Ord,Show) instance Hashable BFNum where hashWithSalt s Zero = s `hashWithSalt` (0::Int)