{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}

module Shelley.Spec.Ledger.Coin
  ( Coin (..),
    CompactForm (..),
    DeltaCoin (..),
    word64ToCoin,
    coinToRational,
    rationalToCoinViaFloor,
    addDeltaCoin,
    toDeltaCoin,
  )
where

import Cardano.Binary (FromCBOR (..), ToCBOR (..))
import Cardano.Ledger.Compactible
import qualified Cardano.Ledger.Torsor as Torsor
import Control.DeepSeq (NFData)
import Data.Aeson (FromJSON, ToJSON)
import Data.Group (Abelian, Group (..))
import Data.Monoid (Sum (..))
import Data.PartialOrd (PartialOrd)
import Data.Word (Word64)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks (..))
import Quiet

-- | The amount of value held by a transaction output.
newtype Coin = Coin {Coin -> Integer
unCoin :: Integer}
  deriving
    ( Coin -> Coin -> Bool
(Coin -> Coin -> Bool) -> (Coin -> Coin -> Bool) -> Eq Coin
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Coin -> Coin -> Bool
$c/= :: Coin -> Coin -> Bool
== :: Coin -> Coin -> Bool
$c== :: Coin -> Coin -> Bool
Eq,
      Eq Coin
Eq Coin
-> (Coin -> Coin -> Ordering)
-> (Coin -> Coin -> Bool)
-> (Coin -> Coin -> Bool)
-> (Coin -> Coin -> Bool)
-> (Coin -> Coin -> Bool)
-> (Coin -> Coin -> Coin)
-> (Coin -> Coin -> Coin)
-> Ord Coin
Coin -> Coin -> Bool
Coin -> Coin -> Ordering
Coin -> Coin -> Coin
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Coin -> Coin -> Coin
$cmin :: Coin -> Coin -> Coin
max :: Coin -> Coin -> Coin
$cmax :: Coin -> Coin -> Coin
>= :: Coin -> Coin -> Bool
$c>= :: Coin -> Coin -> Bool
> :: Coin -> Coin -> Bool
$c> :: Coin -> Coin -> Bool
<= :: Coin -> Coin -> Bool
$c<= :: Coin -> Coin -> Bool
< :: Coin -> Coin -> Bool
$c< :: Coin -> Coin -> Bool
compare :: Coin -> Coin -> Ordering
$ccompare :: Coin -> Coin -> Ordering
$cp1Ord :: Eq Coin
Ord,
      Int -> Coin
Coin -> Int
Coin -> [Coin]
Coin -> Coin
Coin -> Coin -> [Coin]
Coin -> Coin -> Coin -> [Coin]
(Coin -> Coin)
-> (Coin -> Coin)
-> (Int -> Coin)
-> (Coin -> Int)
-> (Coin -> [Coin])
-> (Coin -> Coin -> [Coin])
-> (Coin -> Coin -> [Coin])
-> (Coin -> Coin -> Coin -> [Coin])
-> Enum Coin
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: Coin -> Coin -> Coin -> [Coin]
$cenumFromThenTo :: Coin -> Coin -> Coin -> [Coin]
enumFromTo :: Coin -> Coin -> [Coin]
$cenumFromTo :: Coin -> Coin -> [Coin]
enumFromThen :: Coin -> Coin -> [Coin]
$cenumFromThen :: Coin -> Coin -> [Coin]
enumFrom :: Coin -> [Coin]
$cenumFrom :: Coin -> [Coin]
fromEnum :: Coin -> Int
$cfromEnum :: Coin -> Int
toEnum :: Int -> Coin
$ctoEnum :: Int -> Coin
pred :: Coin -> Coin
$cpred :: Coin -> Coin
succ :: Coin -> Coin
$csucc :: Coin -> Coin
Enum,
      Context -> Coin -> IO (Maybe ThunkInfo)
Proxy Coin -> String
(Context -> Coin -> IO (Maybe ThunkInfo))
-> (Context -> Coin -> IO (Maybe ThunkInfo))
-> (Proxy Coin -> String)
-> NoThunks Coin
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy Coin -> String
$cshowTypeOf :: Proxy Coin -> String
wNoThunks :: Context -> Coin -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> Coin -> IO (Maybe ThunkInfo)
noThunks :: Context -> Coin -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> Coin -> IO (Maybe ThunkInfo)
NoThunks,
      (forall x. Coin -> Rep Coin x)
-> (forall x. Rep Coin x -> Coin) -> Generic Coin
forall x. Rep Coin x -> Coin
forall x. Coin -> Rep Coin x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Coin x -> Coin
$cfrom :: forall x. Coin -> Rep Coin x
Generic,
      [Coin] -> Encoding
[Coin] -> Value
Coin -> Encoding
Coin -> Value
(Coin -> Value)
-> (Coin -> Encoding)
-> ([Coin] -> Value)
-> ([Coin] -> Encoding)
-> ToJSON Coin
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [Coin] -> Encoding
$ctoEncodingList :: [Coin] -> Encoding
toJSONList :: [Coin] -> Value
$ctoJSONList :: [Coin] -> Value
toEncoding :: Coin -> Encoding
$ctoEncoding :: Coin -> Encoding
toJSON :: Coin -> Value
$ctoJSON :: Coin -> Value
ToJSON,
      Value -> Parser [Coin]
Value -> Parser Coin
(Value -> Parser Coin) -> (Value -> Parser [Coin]) -> FromJSON Coin
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [Coin]
$cparseJSONList :: Value -> Parser [Coin]
parseJSON :: Value -> Parser Coin
$cparseJSON :: Value -> Parser Coin
FromJSON,
      Coin -> ()
(Coin -> ()) -> NFData Coin
forall a. (a -> ()) -> NFData a
rnf :: Coin -> ()
$crnf :: Coin -> ()
NFData
    )
  deriving (Int -> Coin -> ShowS
[Coin] -> ShowS
Coin -> String
(Int -> Coin -> ShowS)
-> (Coin -> String) -> ([Coin] -> ShowS) -> Show Coin
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Coin] -> ShowS
$cshowList :: [Coin] -> ShowS
show :: Coin -> String
$cshow :: Coin -> String
showsPrec :: Int -> Coin -> ShowS
$cshowsPrec :: Int -> Coin -> ShowS
Show) via Quiet Coin
  deriving (Typeable Coin
Typeable Coin
-> (Coin -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy Coin -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size)
    -> Proxy [Coin] -> Size)
-> ToCBOR Coin
Coin -> Encoding
(forall t. ToCBOR t => Proxy t -> Size) -> Proxy [Coin] -> Size
(forall t. ToCBOR t => Proxy t -> Size) -> Proxy Coin -> Size
forall a.
Typeable a
-> (a -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy a -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy [a] -> Size)
-> ToCBOR a
encodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy [Coin] -> Size
$cencodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy [Coin] -> Size
encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy Coin -> Size
$cencodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy Coin -> Size
toCBOR :: Coin -> Encoding
$ctoCBOR :: Coin -> Encoding
$cp1ToCBOR :: Typeable Coin
ToCBOR, Typeable Coin
Decoder s Coin
Typeable Coin
-> (forall s. Decoder s Coin)
-> (Proxy Coin -> Text)
-> FromCBOR Coin
Proxy Coin -> Text
forall s. Decoder s Coin
forall a.
Typeable a
-> (forall s. Decoder s a) -> (Proxy a -> Text) -> FromCBOR a
label :: Proxy Coin -> Text
$clabel :: Proxy Coin -> Text
fromCBOR :: Decoder s Coin
$cfromCBOR :: forall s. Decoder s Coin
$cp1FromCBOR :: Typeable Coin
FromCBOR) via Compact Coin
  deriving (b -> Coin -> Coin
NonEmpty Coin -> Coin
Coin -> Coin -> Coin
(Coin -> Coin -> Coin)
-> (NonEmpty Coin -> Coin)
-> (forall b. Integral b => b -> Coin -> Coin)
-> Semigroup Coin
forall b. Integral b => b -> Coin -> Coin
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
stimes :: b -> Coin -> Coin
$cstimes :: forall b. Integral b => b -> Coin -> Coin
sconcat :: NonEmpty Coin -> Coin
$csconcat :: NonEmpty Coin -> Coin
<> :: Coin -> Coin -> Coin
$c<> :: Coin -> Coin -> Coin
Semigroup, Semigroup Coin
Coin
Semigroup Coin
-> Coin
-> (Coin -> Coin -> Coin)
-> ([Coin] -> Coin)
-> Monoid Coin
[Coin] -> Coin
Coin -> Coin -> Coin
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
mconcat :: [Coin] -> Coin
$cmconcat :: [Coin] -> Coin
mappend :: Coin -> Coin -> Coin
$cmappend :: Coin -> Coin -> Coin
mempty :: Coin
$cmempty :: Coin
$cp1Monoid :: Semigroup Coin
Monoid, Monoid Coin
Monoid Coin
-> (Coin -> Coin)
-> (Coin -> Coin -> Coin)
-> (forall x. Integral x => Coin -> x -> Coin)
-> Group Coin
Coin -> Coin
Coin -> x -> Coin
Coin -> Coin -> Coin
forall x. Integral x => Coin -> x -> Coin
forall m.
Monoid m
-> (m -> m)
-> (m -> m -> m)
-> (forall x. Integral x => m -> x -> m)
-> Group m
pow :: Coin -> x -> Coin
$cpow :: forall x. Integral x => Coin -> x -> Coin
~~ :: Coin -> Coin -> Coin
$c~~ :: Coin -> Coin -> Coin
invert :: Coin -> Coin
$cinvert :: Coin -> Coin
$cp1Group :: Monoid Coin
Group, Group Coin
Group Coin -> Abelian Coin
forall g. Group g -> Abelian g
Abelian) via Sum Integer
  deriving newtype (Coin -> Coin -> Bool
Coin -> Coin -> Maybe Ordering
(Coin -> Coin -> Bool)
-> (Coin -> Coin -> Bool)
-> (Coin -> Coin -> Bool)
-> (Coin -> Coin -> Bool)
-> (Coin -> Coin -> Bool)
-> (Coin -> Coin -> Bool)
-> (Coin -> Coin -> Maybe Ordering)
-> PartialOrd Coin
forall a.
(a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Maybe Ordering)
-> PartialOrd a
compare :: Coin -> Coin -> Maybe Ordering
$ccompare :: Coin -> Coin -> Maybe Ordering
> :: Coin -> Coin -> Bool
$c> :: Coin -> Coin -> Bool
< :: Coin -> Coin -> Bool
$c< :: Coin -> Coin -> Bool
/= :: Coin -> Coin -> Bool
$c/= :: Coin -> Coin -> Bool
== :: Coin -> Coin -> Bool
$c== :: Coin -> Coin -> Bool
>= :: Coin -> Coin -> Bool
$c>= :: Coin -> Coin -> Bool
<= :: Coin -> Coin -> Bool
$c<= :: Coin -> Coin -> Bool
PartialOrd)

newtype DeltaCoin = DeltaCoin Integer
  deriving (DeltaCoin -> DeltaCoin -> Bool
(DeltaCoin -> DeltaCoin -> Bool)
-> (DeltaCoin -> DeltaCoin -> Bool) -> Eq DeltaCoin
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DeltaCoin -> DeltaCoin -> Bool
$c/= :: DeltaCoin -> DeltaCoin -> Bool
== :: DeltaCoin -> DeltaCoin -> Bool
$c== :: DeltaCoin -> DeltaCoin -> Bool
Eq, Eq DeltaCoin
Eq DeltaCoin
-> (DeltaCoin -> DeltaCoin -> Ordering)
-> (DeltaCoin -> DeltaCoin -> Bool)
-> (DeltaCoin -> DeltaCoin -> Bool)
-> (DeltaCoin -> DeltaCoin -> Bool)
-> (DeltaCoin -> DeltaCoin -> Bool)
-> (DeltaCoin -> DeltaCoin -> DeltaCoin)
-> (DeltaCoin -> DeltaCoin -> DeltaCoin)
-> Ord DeltaCoin
DeltaCoin -> DeltaCoin -> Bool
DeltaCoin -> DeltaCoin -> Ordering
DeltaCoin -> DeltaCoin -> DeltaCoin
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: DeltaCoin -> DeltaCoin -> DeltaCoin
$cmin :: DeltaCoin -> DeltaCoin -> DeltaCoin
max :: DeltaCoin -> DeltaCoin -> DeltaCoin
$cmax :: DeltaCoin -> DeltaCoin -> DeltaCoin
>= :: DeltaCoin -> DeltaCoin -> Bool
$c>= :: DeltaCoin -> DeltaCoin -> Bool
> :: DeltaCoin -> DeltaCoin -> Bool
$c> :: DeltaCoin -> DeltaCoin -> Bool
<= :: DeltaCoin -> DeltaCoin -> Bool
$c<= :: DeltaCoin -> DeltaCoin -> Bool
< :: DeltaCoin -> DeltaCoin -> Bool
$c< :: DeltaCoin -> DeltaCoin -> Bool
compare :: DeltaCoin -> DeltaCoin -> Ordering
$ccompare :: DeltaCoin -> DeltaCoin -> Ordering
$cp1Ord :: Eq DeltaCoin
Ord, (forall x. DeltaCoin -> Rep DeltaCoin x)
-> (forall x. Rep DeltaCoin x -> DeltaCoin) -> Generic DeltaCoin
forall x. Rep DeltaCoin x -> DeltaCoin
forall x. DeltaCoin -> Rep DeltaCoin x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep DeltaCoin x -> DeltaCoin
$cfrom :: forall x. DeltaCoin -> Rep DeltaCoin x
Generic, Int -> DeltaCoin
DeltaCoin -> Int
DeltaCoin -> [DeltaCoin]
DeltaCoin -> DeltaCoin
DeltaCoin -> DeltaCoin -> [DeltaCoin]
DeltaCoin -> DeltaCoin -> DeltaCoin -> [DeltaCoin]
(DeltaCoin -> DeltaCoin)
-> (DeltaCoin -> DeltaCoin)
-> (Int -> DeltaCoin)
-> (DeltaCoin -> Int)
-> (DeltaCoin -> [DeltaCoin])
-> (DeltaCoin -> DeltaCoin -> [DeltaCoin])
-> (DeltaCoin -> DeltaCoin -> [DeltaCoin])
-> (DeltaCoin -> DeltaCoin -> DeltaCoin -> [DeltaCoin])
-> Enum DeltaCoin
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: DeltaCoin -> DeltaCoin -> DeltaCoin -> [DeltaCoin]
$cenumFromThenTo :: DeltaCoin -> DeltaCoin -> DeltaCoin -> [DeltaCoin]
enumFromTo :: DeltaCoin -> DeltaCoin -> [DeltaCoin]
$cenumFromTo :: DeltaCoin -> DeltaCoin -> [DeltaCoin]
enumFromThen :: DeltaCoin -> DeltaCoin -> [DeltaCoin]
$cenumFromThen :: DeltaCoin -> DeltaCoin -> [DeltaCoin]
enumFrom :: DeltaCoin -> [DeltaCoin]
$cenumFrom :: DeltaCoin -> [DeltaCoin]
fromEnum :: DeltaCoin -> Int
$cfromEnum :: DeltaCoin -> Int
toEnum :: Int -> DeltaCoin
$ctoEnum :: Int -> DeltaCoin
pred :: DeltaCoin -> DeltaCoin
$cpred :: DeltaCoin -> DeltaCoin
succ :: DeltaCoin -> DeltaCoin
$csucc :: DeltaCoin -> DeltaCoin
Enum, Context -> DeltaCoin -> IO (Maybe ThunkInfo)
Proxy DeltaCoin -> String
(Context -> DeltaCoin -> IO (Maybe ThunkInfo))
-> (Context -> DeltaCoin -> IO (Maybe ThunkInfo))
-> (Proxy DeltaCoin -> String)
-> NoThunks DeltaCoin
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy DeltaCoin -> String
$cshowTypeOf :: Proxy DeltaCoin -> String
wNoThunks :: Context -> DeltaCoin -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> DeltaCoin -> IO (Maybe ThunkInfo)
noThunks :: Context -> DeltaCoin -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> DeltaCoin -> IO (Maybe ThunkInfo)
NoThunks, DeltaCoin -> ()
(DeltaCoin -> ()) -> NFData DeltaCoin
forall a. (a -> ()) -> NFData a
rnf :: DeltaCoin -> ()
$crnf :: DeltaCoin -> ()
NFData, Typeable DeltaCoin
Decoder s DeltaCoin
Typeable DeltaCoin
-> (forall s. Decoder s DeltaCoin)
-> (Proxy DeltaCoin -> Text)
-> FromCBOR DeltaCoin
Proxy DeltaCoin -> Text
forall s. Decoder s DeltaCoin
forall a.
Typeable a
-> (forall s. Decoder s a) -> (Proxy a -> Text) -> FromCBOR a
label :: Proxy DeltaCoin -> Text
$clabel :: Proxy DeltaCoin -> Text
fromCBOR :: Decoder s DeltaCoin
$cfromCBOR :: forall s. Decoder s DeltaCoin
$cp1FromCBOR :: Typeable DeltaCoin
FromCBOR, Typeable DeltaCoin
Typeable DeltaCoin
-> (DeltaCoin -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size)
    -> Proxy DeltaCoin -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size)
    -> Proxy [DeltaCoin] -> Size)
-> ToCBOR DeltaCoin
DeltaCoin -> Encoding
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [DeltaCoin] -> Size
(forall t. ToCBOR t => Proxy t -> Size) -> Proxy DeltaCoin -> Size
forall a.
Typeable a
-> (a -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy a -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy [a] -> Size)
-> ToCBOR a
encodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [DeltaCoin] -> Size
$cencodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [DeltaCoin] -> Size
encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy DeltaCoin -> Size
$cencodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy DeltaCoin -> Size
toCBOR :: DeltaCoin -> Encoding
$ctoCBOR :: DeltaCoin -> Encoding
$cp1ToCBOR :: Typeable DeltaCoin
ToCBOR)
  deriving (Int -> DeltaCoin -> ShowS
[DeltaCoin] -> ShowS
DeltaCoin -> String
(Int -> DeltaCoin -> ShowS)
-> (DeltaCoin -> String)
-> ([DeltaCoin] -> ShowS)
-> Show DeltaCoin
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DeltaCoin] -> ShowS
$cshowList :: [DeltaCoin] -> ShowS
show :: DeltaCoin -> String
$cshow :: DeltaCoin -> String
showsPrec :: Int -> DeltaCoin -> ShowS
$cshowsPrec :: Int -> DeltaCoin -> ShowS
Show) via Quiet DeltaCoin
  deriving (b -> DeltaCoin -> DeltaCoin
NonEmpty DeltaCoin -> DeltaCoin
DeltaCoin -> DeltaCoin -> DeltaCoin
(DeltaCoin -> DeltaCoin -> DeltaCoin)
-> (NonEmpty DeltaCoin -> DeltaCoin)
-> (forall b. Integral b => b -> DeltaCoin -> DeltaCoin)
-> Semigroup DeltaCoin
forall b. Integral b => b -> DeltaCoin -> DeltaCoin
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
stimes :: b -> DeltaCoin -> DeltaCoin
$cstimes :: forall b. Integral b => b -> DeltaCoin -> DeltaCoin
sconcat :: NonEmpty DeltaCoin -> DeltaCoin
$csconcat :: NonEmpty DeltaCoin -> DeltaCoin
<> :: DeltaCoin -> DeltaCoin -> DeltaCoin
$c<> :: DeltaCoin -> DeltaCoin -> DeltaCoin
Semigroup, Semigroup DeltaCoin
DeltaCoin
Semigroup DeltaCoin
-> DeltaCoin
-> (DeltaCoin -> DeltaCoin -> DeltaCoin)
-> ([DeltaCoin] -> DeltaCoin)
-> Monoid DeltaCoin
[DeltaCoin] -> DeltaCoin
DeltaCoin -> DeltaCoin -> DeltaCoin
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
mconcat :: [DeltaCoin] -> DeltaCoin
$cmconcat :: [DeltaCoin] -> DeltaCoin
mappend :: DeltaCoin -> DeltaCoin -> DeltaCoin
$cmappend :: DeltaCoin -> DeltaCoin -> DeltaCoin
mempty :: DeltaCoin
$cmempty :: DeltaCoin
$cp1Monoid :: Semigroup DeltaCoin
Monoid, Monoid DeltaCoin
Monoid DeltaCoin
-> (DeltaCoin -> DeltaCoin)
-> (DeltaCoin -> DeltaCoin -> DeltaCoin)
-> (forall x. Integral x => DeltaCoin -> x -> DeltaCoin)
-> Group DeltaCoin
DeltaCoin -> DeltaCoin
DeltaCoin -> x -> DeltaCoin
DeltaCoin -> DeltaCoin -> DeltaCoin
forall x. Integral x => DeltaCoin -> x -> DeltaCoin
forall m.
Monoid m
-> (m -> m)
-> (m -> m -> m)
-> (forall x. Integral x => m -> x -> m)
-> Group m
pow :: DeltaCoin -> x -> DeltaCoin
$cpow :: forall x. Integral x => DeltaCoin -> x -> DeltaCoin
~~ :: DeltaCoin -> DeltaCoin -> DeltaCoin
$c~~ :: DeltaCoin -> DeltaCoin -> DeltaCoin
invert :: DeltaCoin -> DeltaCoin
$cinvert :: DeltaCoin -> DeltaCoin
$cp1Group :: Monoid DeltaCoin
Group, Group DeltaCoin
Group DeltaCoin -> Abelian DeltaCoin
forall g. Group g -> Abelian g
Abelian) via Sum Integer
  deriving newtype (DeltaCoin -> DeltaCoin -> Bool
DeltaCoin -> DeltaCoin -> Maybe Ordering
(DeltaCoin -> DeltaCoin -> Bool)
-> (DeltaCoin -> DeltaCoin -> Bool)
-> (DeltaCoin -> DeltaCoin -> Bool)
-> (DeltaCoin -> DeltaCoin -> Bool)
-> (DeltaCoin -> DeltaCoin -> Bool)
-> (DeltaCoin -> DeltaCoin -> Bool)
-> (DeltaCoin -> DeltaCoin -> Maybe Ordering)
-> PartialOrd DeltaCoin
forall a.
(a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Maybe Ordering)
-> PartialOrd a
compare :: DeltaCoin -> DeltaCoin -> Maybe Ordering
$ccompare :: DeltaCoin -> DeltaCoin -> Maybe Ordering
> :: DeltaCoin -> DeltaCoin -> Bool
$c> :: DeltaCoin -> DeltaCoin -> Bool
< :: DeltaCoin -> DeltaCoin -> Bool
$c< :: DeltaCoin -> DeltaCoin -> Bool
/= :: DeltaCoin -> DeltaCoin -> Bool
$c/= :: DeltaCoin -> DeltaCoin -> Bool
== :: DeltaCoin -> DeltaCoin -> Bool
$c== :: DeltaCoin -> DeltaCoin -> Bool
>= :: DeltaCoin -> DeltaCoin -> Bool
$c>= :: DeltaCoin -> DeltaCoin -> Bool
<= :: DeltaCoin -> DeltaCoin -> Bool
$c<= :: DeltaCoin -> DeltaCoin -> Bool
PartialOrd)

addDeltaCoin :: Coin -> DeltaCoin -> Coin
addDeltaCoin :: Coin -> DeltaCoin -> Coin
addDeltaCoin (Coin Integer
x) (DeltaCoin Integer
y) = Integer -> Coin
Coin (Integer
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
y)

toDeltaCoin :: Coin -> DeltaCoin
toDeltaCoin :: Coin -> DeltaCoin
toDeltaCoin (Coin Integer
x) = Integer -> DeltaCoin
DeltaCoin Integer
x

instance Torsor.Torsor Coin where
  type Delta Coin = DeltaCoin
  addDelta :: Coin -> Delta Coin -> Coin
addDelta = Coin -> Delta Coin -> Coin
Coin -> DeltaCoin -> Coin
addDeltaCoin
  toDelta :: Coin -> Delta Coin
toDelta = Coin -> Delta Coin
Coin -> DeltaCoin
toDeltaCoin

word64ToCoin :: Word64 -> Coin
word64ToCoin :: Word64 -> Coin
word64ToCoin Word64
w = Integer -> Coin
Coin (Integer -> Coin) -> Integer -> Coin
forall a b. (a -> b) -> a -> b
$ Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
w

coinToRational :: Coin -> Rational
coinToRational :: Coin -> Rational
coinToRational (Coin Integer
c) = Integer -> Rational
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
c

rationalToCoinViaFloor :: Rational -> Coin
rationalToCoinViaFloor :: Rational -> Coin
rationalToCoinViaFloor Rational
r = Integer -> Coin
Coin (Integer -> Coin) -> (Rational -> Integer) -> Rational -> Coin
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> Integer
forall a b. (RealFrac a, Integral b) => a -> b
floor (Rational -> Coin) -> Rational -> Coin
forall a b. (a -> b) -> a -> b
$ Rational
r

-- FIXME:
-- if coin is less than 0 or greater than (maxBound :: Word64), then
-- fromIntegral constructs the incorrect value. for now this is handled
-- with an erroring bounds check here. where should this really live?
instance Compactible Coin where
  newtype CompactForm Coin = CompactCoin Word64
  toCompact :: Coin -> CompactForm Coin
toCompact (Coin Integer
c)
    | Integer
c Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 = String -> CompactForm Coin
forall a. HasCallStack => String -> a
error (String -> CompactForm Coin) -> String -> CompactForm Coin
forall a b. (a -> b) -> a -> b
$ String
"out of bounds : " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show Integer
c
    | Integer
c Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
forall a. Bounded a => a
maxBound :: Word64)) =
      String -> CompactForm Coin
forall a. HasCallStack => String -> a
error (String -> CompactForm Coin) -> String -> CompactForm Coin
forall a b. (a -> b) -> a -> b
$ String
"out of bounds : " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show Integer
c
    | Bool
otherwise = Word64 -> CompactForm Coin
CompactCoin (Integer -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
c)
  fromCompact :: CompactForm Coin -> Coin
fromCompact (CompactCoin c) = Word64 -> Coin
word64ToCoin Word64
c

instance ToCBOR (CompactForm Coin) where
  toCBOR :: CompactForm Coin -> Encoding
toCBOR (CompactCoin c) = Word64 -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR Word64
c

instance FromCBOR (CompactForm Coin) where
  fromCBOR :: Decoder s (CompactForm Coin)
fromCBOR = Word64 -> CompactForm Coin
CompactCoin (Word64 -> CompactForm Coin)
-> Decoder s Word64 -> Decoder s (CompactForm Coin)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word64
forall a s. FromCBOR a => Decoder s a
fromCBOR