Skip to content

Commit

Permalink
Add Data instances for BigFloat, Sign, BFRep, BFNum
Browse files Browse the repository at this point in the history
Fixes #28.
  • Loading branch information
RyanGlScott committed Nov 28, 2023
1 parent 3c7c963 commit 565236d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/LibBF.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module LibBF


import Data.Bits
import Data.Data (Data(..))
import Data.Hashable
import Data.Word
import Data.Int
Expand All @@ -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` ()

Expand Down
8 changes: 5 additions & 3 deletions src/LibBF/Mutable.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# Language PatternSynonyms #-}
{-# Language MultiWayIf #-}
{-# Language BlockArguments #-}
{-# Language DeriveDataTypeable #-}
-- | Mutable big-float computation.
module LibBF.Mutable
( -- * Allocation
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 565236d

Please sign in to comment.