safe-money-0.2: Type-safe and lossless encoding and manipulation of money, world currencies and precious metals.

Data.Money

Description

Import this module qualified:

import qualified Data.Money as Money


Synopsis

# Dense monetary values

data Dense currency Source #

Dense represents a dense monetary value for currency (usually a ISO-4217 currency code, but not necessarily) as a rational number.

While monetary values associated with a particular currency are discrete, you can still treat monetary values as dense while operating on them. For example, the half of USD 3.41 is USD 1.705, which is not an amount that can't be represented as a number of USD cents (the smallest unit that can represent USD amounts). Nevertheless, if you eventually multiply USD 1.705 by 4, for example, you end up with USD 6.82, which is again a value representable as USD cents. In other words, Dense monetary values allow us to perform precise calculations deferring the conversion to a Discrete monetary values as much as posible. Once you are ready to aproximate a Dense value to a Discrete value you can use one of round, floor, ceiling or truncate. Otherwise, using toRational you can obtain a precise Rational representation.

Construct Dense monetary values using dense, or fromInteger / fromIntegral if that suffices.

WARNING if you want to treat a dense monetary value as a Real number (for example, to take the square root of that monetary value), then you are on your own. We can only guarantee lossless manipulation of rational values, so you will need to convert back and forth betwen the Rational representation for Dense and your (likely lossy) representation for Real numbers.

Instances

 Eq (Dense currency) Source # Methods(==) :: Dense currency -> Dense currency -> Bool #(/=) :: Dense currency -> Dense currency -> Bool # Fractional (Dense currency) Source # Methods(/) :: Dense currency -> Dense currency -> Dense currency #recip :: Dense currency -> Dense currency #fromRational :: Rational -> Dense currency # Num (Dense currency) Source # Methods(+) :: Dense currency -> Dense currency -> Dense currency #(-) :: Dense currency -> Dense currency -> Dense currency #(*) :: Dense currency -> Dense currency -> Dense currency #negate :: Dense currency -> Dense currency #abs :: Dense currency -> Dense currency #signum :: Dense currency -> Dense currency #fromInteger :: Integer -> Dense currency # Ord (Dense currency) Source # Methodscompare :: Dense currency -> Dense currency -> Ordering #(<) :: Dense currency -> Dense currency -> Bool #(<=) :: Dense currency -> Dense currency -> Bool #(>) :: Dense currency -> Dense currency -> Bool #(>=) :: Dense currency -> Dense currency -> Bool #max :: Dense currency -> Dense currency -> Dense currency #min :: Dense currency -> Dense currency -> Dense currency # KnownSymbol currency => Read (Dense currency) Source # MethodsreadsPrec :: Int -> ReadS (Dense currency) #readList :: ReadS [Dense currency] #readPrec :: ReadPrec (Dense currency) #readListPrec :: ReadPrec [Dense currency] # Real (Dense currency) Source # MethodstoRational :: Dense currency -> Rational # KnownSymbol currency => Show (Dense currency) Source # MethodsshowsPrec :: Int -> Dense currency -> ShowS #show :: Dense currency -> String #showList :: [Dense currency] -> ShowS # Generic (Dense currency) Source # Associated Typestype Rep (Dense currency) :: * -> * # Methodsfrom :: Dense currency -> Rep (Dense currency) x #to :: Rep (Dense currency) x -> Dense currency # Hashable (Dense currency) Source # MethodshashWithSalt :: Int -> Dense currency -> Int #hash :: Dense currency -> Int # KnownSymbol currency => ToJSON (Dense currency) Source # Compatible with DenseRep MethodstoJSON :: Dense currency -> Value #toEncoding :: Dense currency -> Encoding #toJSONList :: [Dense currency] -> Value #toEncodingList :: [Dense currency] -> Encoding # KnownSymbol currency => FromJSON (Dense currency) Source # Compatible with DenseRep MethodsparseJSON :: Value -> Parser (Dense currency) #parseJSONList :: Value -> Parser [Dense currency] # KnownSymbol currency => Binary (Dense currency) Source # Compatible with DenseRep. Methodsput :: Dense currency -> Put #get :: Get (Dense currency) #putList :: [Dense currency] -> Put # KnownSymbol currency => Serialize (Dense currency) Source # Compatible with DenseRep. Methodsput :: Putter (Dense currency) #get :: Get (Dense currency) # NFData (Dense currency) Source # Methodsrnf :: Dense currency -> () # KnownSymbol currency => Store (Dense currency) Source # Compatible with DenseRep. Methodssize :: Size (Dense currency) #poke :: Dense currency -> Poke () #peek :: Peek (Dense currency) # type Rep (Dense currency) Source # type Rep (Dense currency) = D1 (MetaData "Dense" "Data.Money.Internal" "safe-money-0.2-9KEKOJrropPa8pssJxMaR" True) (C1 (MetaCons "Dense" PrefixI False) (S1 (MetaSel (Nothing Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Rational)))

dense :: Rational -> Maybe (Dense currency) Source #

Build a Dense monetary value from a Rational value.

For example, if you want to represent USD 12.52316, then you can use:

dense (125316 % 10000)


This function returns Nothing in case the given Rational is infinity or notANumber.

# Discrete monetary values

type Discrete currency unit = Discrete' currency (Scale currency unit) Source #

Discrete represents a discrete monetary value for a currency expresed as an integer amount of a particular unit. For example, with currency ~ "USD" and unit ~ "cent" you can represent United States Dollars to their full extent.

currency is usually a ISO-4217 currency code, but not necessarily.

Construct Discrete values using fromInteger.

For example, if you want to represent GBP 21.05, where the smallest represetable unit for a GBP (United Kingdom Pound) is the penny, and 100 pennies equal 1 GBP (i.e., Scale "GBP" ~ '(100, 1)), then you can use:

fromInteger 2105 :: Discrete "GBP" "penny"


Because 2015 / 100 == 20.15.

data Discrete' currency scale Source #

Discrete' represents a discrete monetary value for a currency expresed as an amount of scale, which is a rational number expressed as (numerator, denominator).

You'll be using Discrete instead of Discrete' most of the time, which mentions the unit name (such as cent or centavo) instead of explicitely mentioning the unit scale.

Instances

 GoodScale scale => Enum (Discrete' currency scale) Source # Methodssucc :: Discrete' currency scale -> Discrete' currency scale #pred :: Discrete' currency scale -> Discrete' currency scale #toEnum :: Int -> Discrete' currency scale #fromEnum :: Discrete' currency scale -> Int #enumFrom :: Discrete' currency scale -> [Discrete' currency scale] #enumFromThen :: Discrete' currency scale -> Discrete' currency scale -> [Discrete' currency scale] #enumFromTo :: Discrete' currency scale -> Discrete' currency scale -> [Discrete' currency scale] #enumFromThenTo :: Discrete' currency scale -> Discrete' currency scale -> Discrete' currency scale -> [Discrete' currency scale] # GoodScale scale => Eq (Discrete' currency scale) Source # Methods(==) :: Discrete' currency scale -> Discrete' currency scale -> Bool #(/=) :: Discrete' currency scale -> Discrete' currency scale -> Bool # (TypeError Constraint ((:$$:) ((:$$:) ((:<>:) ((:<>:) ((:<>:) (Text "The ") (ShowType (Symbol -> (Nat, Nat) -> *) Discrete')) (Text " type is deliberately not a ")) (ShowType (* -> Constraint) Fractional)) ((:<>:) ((:<>:) ((:<>:) (Text "instance. Convert the ") (ShowType (Symbol -> (Nat, Nat) -> *) Discrete')) (Text " value to a ")) (ShowType (Symbol -> *) Dense))) ((:<>:) ((:<>:) (Text "value and use the ") (ShowType (* -> Constraint) Fractional)) (Text " features on it instead."))), GoodScale scale) => Fractional (Discrete' currency scale) Source # Methods(/) :: Discrete' currency scale -> Discrete' currency scale -> Discrete' currency scale #recip :: Discrete' currency scale -> Discrete' currency scale #fromRational :: Rational -> Discrete' currency scale # GoodScale scale => Integral (Discrete' currency scale) Source # Methodsquot :: Discrete' currency scale -> Discrete' currency scale -> Discrete' currency scale #rem :: Discrete' currency scale -> Discrete' currency scale -> Discrete' currency scale #div :: Discrete' currency scale -> Discrete' currency scale -> Discrete' currency scale #mod :: Discrete' currency scale -> Discrete' currency scale -> Discrete' currency scale #quotRem :: Discrete' currency scale -> Discrete' currency scale -> (Discrete' currency scale, Discrete' currency scale) #divMod :: Discrete' currency scale -> Discrete' currency scale -> (Discrete' currency scale, Discrete' currency scale) #toInteger :: Discrete' currency scale -> Integer # GoodScale scale => Num (Discrete' currency scale) Source # Methods(+) :: Discrete' currency scale -> Discrete' currency scale -> Discrete' currency scale #(-) :: Discrete' currency scale -> Discrete' currency scale -> Discrete' currency scale #(*) :: Discrete' currency scale -> Discrete' currency scale -> Discrete' currency scale #negate :: Discrete' currency scale -> Discrete' currency scale #abs :: Discrete' currency scale -> Discrete' currency scale #signum :: Discrete' currency scale -> Discrete' currency scale #fromInteger :: Integer -> Discrete' currency scale # GoodScale scale => Ord (Discrete' currency scale) Source # Methodscompare :: Discrete' currency scale -> Discrete' currency scale -> Ordering #(<) :: Discrete' currency scale -> Discrete' currency scale -> Bool #(<=) :: Discrete' currency scale -> Discrete' currency scale -> Bool #(>) :: Discrete' currency scale -> Discrete' currency scale -> Bool #(>=) :: Discrete' currency scale -> Discrete' currency scale -> Bool #max :: Discrete' currency scale -> Discrete' currency scale -> Discrete' currency scale #min :: Discrete' currency scale -> Discrete' currency scale -> Discrete' currency scale # (KnownSymbol currency, GoodScale scale) => Read (Discrete' currency scale) Source # MethodsreadsPrec :: Int -> ReadS (Discrete' currency scale) #readList :: ReadS [Discrete' currency scale] #readPrec :: ReadPrec (Discrete' currency scale) #readListPrec :: ReadPrec [Discrete' currency scale] # GoodScale scale => Real (Discrete' currency scale) Source # MethodstoRational :: Discrete' currency scale -> Rational # (KnownSymbol currency, GoodScale scale) => Show (Discrete' currency scale) Source # MethodsshowsPrec :: Int -> Discrete' currency scale -> ShowS #show :: Discrete' currency scale -> String #showList :: [Discrete' currency scale] -> ShowS # GoodScale scale => Generic (Discrete' currency scale) Source # Associated Typestype Rep (Discrete' currency scale) :: * -> * # Methodsfrom :: Discrete' currency scale -> Rep (Discrete' currency scale) x #to :: Rep (Discrete' currency scale) x -> Discrete' currency scale # GoodScale scale => Hashable (Discrete' currency scale) Source # MethodshashWithSalt :: Int -> Discrete' currency scale -> Int #hash :: Discrete' currency scale -> Int # (KnownSymbol currency, GoodScale scale) => ToJSON (Discrete' currency scale) Source # Compatible with DiscreteRep MethodstoJSON :: Discrete' currency scale -> Value #toEncoding :: Discrete' currency scale -> Encoding #toJSONList :: [Discrete' currency scale] -> Value #toEncodingList :: [Discrete' currency scale] -> Encoding # (KnownSymbol currency, GoodScale scale) => FromJSON (Discrete' currency scale) Source # Compatible with DiscreteRep MethodsparseJSON :: Value -> Parser (Discrete' currency scale) #parseJSONList :: Value -> Parser [Discrete' currency scale] # (KnownSymbol currency, GoodScale scale) => Binary (Discrete' currency scale) Source # Compatible with DiscreteRep. Methodsput :: Discrete' currency scale -> Put #get :: Get (Discrete' currency scale) #putList :: [Discrete' currency scale] -> Put # (KnownSymbol currency, GoodScale scale) => Serialize (Discrete' currency scale) Source # Compatible with DiscreteRep. Methodsput :: Putter (Discrete' currency scale) #get :: Get (Discrete' currency scale) # GoodScale scale => NFData (Discrete' currency scale) Source # Methodsrnf :: Discrete' currency scale -> () # (KnownSymbol currency, GoodScale scale) => Store (Discrete' currency scale) Source # Compatible with DiscreteRep. Methodssize :: Size (Discrete' currency scale) #poke :: Discrete' currency scale -> Poke () #peek :: Peek (Discrete' currency scale) # type Rep (Discrete' currency scale) Source # type Rep (Discrete' currency scale) = D1 (MetaData "Discrete'" "Data.Money.Internal" "safe-money-0.2-9KEKOJrropPa8pssJxMaR" True) (C1 (MetaCons "Discrete" PrefixI False) (S1 (MetaSel (Nothing Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Integer)))

Arguments

 :: GoodScale scale => Discrete' currency scale -> Dense currency

Convert currency Discrete monetary value into a Dense monetary value.

Arguments

 :: GoodScale scale => Dense currency -> (Discrete' currency scale, Maybe (Dense currency))

Round a Dense value x to the nearest value fully representable in its currency's unit Scale, which might be x itself.

If x is already fully representable in its currency's unit Scale, then the following holds:

round x == (x, Nothing)


Otherwise, if the nearest value to x that is fully representable in its currency's unit Scale is greater than x, then the following holds:

round == ceiling


Otherwise, the nearest value to x that is fully representable in its currency's unit Scale is smaller than x, and the following holds:

round == floor


Proof that round doesn't lose money:

x == case round x of
(y, Nothing) -> y
(y, Just z)  -> y + z


Arguments

 :: GoodScale scale => Dense currency -> (Discrete' currency scale, Maybe (Dense currency))

Round a Dense value x to the nearest value fully representable in its currency's unit Scale which is greater than x or equal to x.

If x is already fully representable in its currency's unit Scale, then the following holds:

ceiling x == (x, Nothing)


Otherwise, if x is not representable in its currency's unit Scale, then the following holds:

ceiling x == (y, Just z)

x /= y

z < zero


Proof that ceiling doesn't lose money:

x == case ceiling x of
(y, Nothing) -> y
(y, Just z)  -> y + z


Arguments

 :: GoodScale scale => Dense currency -> (Discrete' currency scale, Maybe (Dense currency))

Round a Dense value x to the nearest value fully representable in its currency's unit Scale which is smaller than x or equal to x.

If x is already fully representable in its currency's unit Scale, then the following holds:

floor x == (x, Nothing)


Otherwise, if x is not representable in its currency's unit Scale, then the following holds:

floor x == (y, Just z)

x /= y

z > zero


Proof that floor doesn't lose money:

x == case floor x of
(y, Nothing) -> y
(y, Just z)  -> y + z


Arguments

 :: GoodScale scale => Dense currency -> (Discrete' currency scale, Maybe (Dense currency))

Round a Dense value x to the nearest value between zero and x (inclusive) which is fully representable in its currency's unit Scale.

If x is already fully representable in its currency's unit Scale, then the following holds:

truncate x == (x, Nothing)


Otherwise, if x is positive, then the following holds:

truncate == floor


Otherwise, if x is negative, the following holds:

truncate == ceiling


Proof that truncate doesn't lose money:

x == case truncate x of
(y, Nothing) -> y
(y, Just z)  -> y + z


# Currency scales

type family Scale (currency :: Symbol) (unit :: Symbol) :: (Nat, Nat) Source #

Scale currency unit is a rational number (expressed as '(numerator, denominator)) indicating how many pieces of unit fit in currency.

currency is usually a ISO-4217 currency code, but not necessarily.

The Scale will determine how to convert a Dense value into a Discrete value and vice-versa.

For example, there are 100 USD cents in 1 USD, so the scale for this relationship is:

type instance Scale "USD" "cent" = '(100, 1)


As another example, there is 1 dollar in USD, so the scale for this relationship is:

type instance Scale "USD" "dollar" = '(1, 1)


When using Discrete values to represent money, it will be impossible to represent an amount of currency smaller than unit. So, if you decide to use Scale "USD" "dollar" as your scale, you will not be able to represent values such as USD 3.50 or USD 21.87, since they are not exact multiples of a dollar.

If there exists a canonical smallest unit that can fully represent the currency, then an instance Scale currency currency exists.

type instance Scale "USD" "USD" = Scale "USD" "cent"


For some monetary values, such as precious metals, the smallest representable unit is not obvious, since you can continue to split the precious metal many times before it stops being a precious metal. Still, for practical purposes we can make a sane arbitrary choice of smallest unit. For example, the base unit for XAU (Gold) is the troy ounce, which is too big to be considered the smallest unit, but we can arbitrarily choose the milligrain as our smallest unit, which is about as heavy as a single grain of table salt and should be sufficiently precise for all monetary practical purposes. A /troy ounce equals 480000 milligrains/.

type instance Scale "XAG" "milligrain" = '(480000, 1)


You can use other units such as milligrams for measuring XAU, for example. However, since the amount of milligrams in a troy ounce (31103.477) is not integral, we need to use rational number to express it.

type instance Scale "XAU" "milligram" = '(31103477, 1000)


If you try to obtain the 'Scale of a currency without an obvious smallest representable unit, like XAU, you will get a compile error.

Instances

type GoodScale scale = (CmpNat 0 (Fst scale) ~ LT, CmpNat 0 (Snd scale) ~ LT, KnownNat (Fst scale), KnownNat (Snd scale)) Source #

Constraints to a scale (like the one returned by Scale currency unit) expected to always be satisfied. In particular, the scale is always guaranteed to be a positive rational number (infinity and notANumber are forbidden by GoodScale).

Arguments

 :: GoodScale scale => proxy scale -> Rational

Term-level representation for the currency's unit Scale.

For example, the Scale for "USD" in "cent"s is 100/1.

The returned Rational is statically guaranteed to be a positive number, and to be different from both notANumber and infinity.

# Currency exchange

data ExchangeRate src dst Source #

Exchange rate for converting monetary values of currency src into monetary values of currency dst by multiplying for it.

For example, if in order to convert USD to GBP we have to multiply by 1.2345, then we can represent this situaion using:

exchangeRate (12345 % 10000) :: Maybe (ExchangeRate "USD" "GBP")


Instances

 Eq (ExchangeRate src dst) Source # Methods(==) :: ExchangeRate src dst -> ExchangeRate src dst -> Bool #(/=) :: ExchangeRate src dst -> ExchangeRate src dst -> Bool # Ord (ExchangeRate src dst) Source # Methodscompare :: ExchangeRate src dst -> ExchangeRate src dst -> Ordering #(<) :: ExchangeRate src dst -> ExchangeRate src dst -> Bool #(<=) :: ExchangeRate src dst -> ExchangeRate src dst -> Bool #(>) :: ExchangeRate src dst -> ExchangeRate src dst -> Bool #(>=) :: ExchangeRate src dst -> ExchangeRate src dst -> Bool #max :: ExchangeRate src dst -> ExchangeRate src dst -> ExchangeRate src dst #min :: ExchangeRate src dst -> ExchangeRate src dst -> ExchangeRate src dst # (KnownSymbol src, KnownSymbol dst) => Read (ExchangeRate src dst) Source # MethodsreadsPrec :: Int -> ReadS (ExchangeRate src dst) #readList :: ReadS [ExchangeRate src dst] #readPrec :: ReadPrec (ExchangeRate src dst) #readListPrec :: ReadPrec [ExchangeRate src dst] # (KnownSymbol src, KnownSymbol dst) => Show (ExchangeRate src dst) Source # MethodsshowsPrec :: Int -> ExchangeRate src dst -> ShowS #show :: ExchangeRate src dst -> String #showList :: [ExchangeRate src dst] -> ShowS # Generic (ExchangeRate src dst) Source # Associated Typestype Rep (ExchangeRate src dst) :: * -> * # Methodsfrom :: ExchangeRate src dst -> Rep (ExchangeRate src dst) x #to :: Rep (ExchangeRate src dst) x -> ExchangeRate src dst # Hashable (ExchangeRate src dst) Source # MethodshashWithSalt :: Int -> ExchangeRate src dst -> Int #hash :: ExchangeRate src dst -> Int # (KnownSymbol src, KnownSymbol dst) => ToJSON (ExchangeRate src dst) Source # Compatible with ExchangeRateRep MethodstoJSON :: ExchangeRate src dst -> Value #toEncoding :: ExchangeRate src dst -> Encoding #toJSONList :: [ExchangeRate src dst] -> Value #toEncodingList :: [ExchangeRate src dst] -> Encoding # (KnownSymbol src, KnownSymbol dst) => FromJSON (ExchangeRate src dst) Source # Compatible with ExchangeRateRep MethodsparseJSON :: Value -> Parser (ExchangeRate src dst) #parseJSONList :: Value -> Parser [ExchangeRate src dst] # (KnownSymbol src, KnownSymbol dst) => Binary (ExchangeRate src dst) Source # Compatible with ExchangeRateRep. Methodsput :: ExchangeRate src dst -> Put #get :: Get (ExchangeRate src dst) #putList :: [ExchangeRate src dst] -> Put # (KnownSymbol src, KnownSymbol dst) => Serialize (ExchangeRate src dst) Source # Compatible with ExchangeRateRep. Methodsput :: Putter (ExchangeRate src dst) #get :: Get (ExchangeRate src dst) # NFData (ExchangeRate src dst) Source # Methodsrnf :: ExchangeRate src dst -> () # (KnownSymbol src, KnownSymbol dst) => Store (ExchangeRate src dst) Source # Compatible with ExchangeRateRep. Methodssize :: Size (ExchangeRate src dst) #poke :: ExchangeRate src dst -> Poke () #peek :: Peek (ExchangeRate src dst) # type Rep (ExchangeRate src dst) Source # type Rep (ExchangeRate src dst) = D1 (MetaData "ExchangeRate" "Data.Money.Internal" "safe-money-0.2-9KEKOJrropPa8pssJxMaR" True) (C1 (MetaCons "ExchangeRate" PrefixI False) (S1 (MetaSel (Nothing Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Rational)))

exchangeRate :: Rational -> Maybe (ExchangeRate src dst) Source #

Safely construct an ExchangeRate from a Rational number.

For construction to succeed, this Rational must be greater than 0, different from infinity and different from notANumber.

fromExchangeRate :: ExchangeRate src dst -> Rational Source #

Obtain a Rational representation of the ExchangeRate.

This Rational is statically guaranteed to be greater than 0, different from infinity and different from notANumber.

Flip the direction of an ExchangeRate.

Identity law:

flipExchangeRate . flipExchangeRate   ==  id


exchange :: ExchangeRate src dst -> Dense src -> Dense dst Source #

Apply the ExchangeRate to the given Dense src monetary value.

Identity law:

exchange (flipExchangeRate x) . exchange x  ==  id


Use the Identity law for reasoning about going back and forth between src and dst in order to manage any leftovers that might not be representable as a Discrete monetary value of src.

# Serializable representations

data DenseRep Source #

A monomorphic representation of Dense that is easier to serialize and deserialize than Dense in case you don't know the type indexes involved.

Instances

 Source # Methods Source # Methods(<) :: DenseRep -> DenseRep -> Bool #(>) :: DenseRep -> DenseRep -> Bool # Source # MethodsshowList :: [DenseRep] -> ShowS # Source # Associated Typestype Rep DenseRep :: * -> * # Methodsto :: Rep DenseRep x -> DenseRep # Source # Methodshash :: DenseRep -> Int # Source # Compatible with Dense MethodstoJSONList :: [DenseRep] -> Value #toEncodingList :: [DenseRep] -> Encoding # Source # Compatible with Dense Methods Source # Compatible with Dense. Methodsput :: DenseRep -> Put #putList :: [DenseRep] -> Put # Source # Compatible with Dense. Methods Source # Methodsrnf :: DenseRep -> () # Source # Compatible with Dense. Methodspoke :: DenseRep -> Poke () # type Rep DenseRep Source # type Rep DenseRep = D1 (MetaData "DenseRep" "Data.Money.Internal" "safe-money-0.2-9KEKOJrropPa8pssJxMaR" False) (C1 (MetaCons "DenseRep" PrefixI True) ((:*:) (S1 (MetaSel (Just Symbol "_denseRepCurrency") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 String)) ((:*:) (S1 (MetaSel (Just Symbol "_denseRepAmountNumerator") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 Integer)) (S1 (MetaSel (Just Symbol "_denseRepAmountDenominator") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 Integer)))))

denseRep :: KnownSymbol currency => Dense currency -> DenseRep Source #

Convert a Dense to a DenseRep for ease of serialization.

Arguments

 :: KnownSymbol currency => DenseRep -> Maybe (Dense currency)

Attempt to convert a DenseRep to a Dense, provided you know the target currency.

Arguments

 :: DenseRep -> (forall currency. KnownSymbol currency => Dense currency -> r) -> r

Convert a DenseRep to a Dense without knowing the target currency.

Notice that currency here can't leave its intended scope unless you can prove equality with some other type at the outer scope, but in that case you would be better off using fromDenseRep directly.

A monomorphic representation of Discrete that is easier to serialize and deserialize than Discrete in case you don't know the type indexes involved.

Instances

 Source # Methods Source # Methods Source # MethodsshowList :: [DiscreteRep] -> ShowS # Source # Associated Typestype Rep DiscreteRep :: * -> * # Methods Source # Methods Source # Compatible with Discrete' MethodstoJSONList :: [DiscreteRep] -> Value # Source # Compatible with Discrete' Methods Source # Compatible with Discrete. MethodsputList :: [DiscreteRep] -> Put # Source # Compatible with Discrete. Methods Source # Methodsrnf :: DiscreteRep -> () # Source # Compatible with Discrete'. Methodspoke :: DiscreteRep -> Poke () # type Rep DiscreteRep Source # type Rep DiscreteRep = D1 (MetaData "DiscreteRep" "Data.Money.Internal" "safe-money-0.2-9KEKOJrropPa8pssJxMaR" False) (C1 (MetaCons "DiscreteRep" PrefixI True) ((:*:) ((:*:) (S1 (MetaSel (Just Symbol "_discreteRepCurrency") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 String)) (S1 (MetaSel (Just Symbol "_discreteRepScaleNumerator") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 Integer))) ((:*:) (S1 (MetaSel (Just Symbol "_discreteRepScaleDenominator") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 Integer)) (S1 (MetaSel (Just Symbol "_discreteRepAmount") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 Integer)))))

Arguments

 :: (KnownSymbol currency, GoodScale scale) => Discrete' currency scale -> DiscreteRep

Convert a Discrete to a DiscreteRep for ease of serialization.

Arguments

 :: (KnownSymbol currency, GoodScale scale) => DiscreteRep -> Maybe (Discrete' currency scale)

Attempt to convert a DiscreteRep to a Discrete, provided you know the target currency and unit.

Arguments

 :: DiscreteRep -> (forall currency scale. (KnownSymbol currency, GoodScale scale) => Discrete' currency scale -> r) -> r

Convert a DiscreteRep to a Discrete without knowing the target currency and unit.

Notice that currency and unit here can't leave its intended scope unless you can prove equality with some other type at the outer scope, but in that case you would be better off using fromDiscreteRep directly.

Notice that you may need to add an explicit type to the result of this function in order to keep the compiler happy.

A monomorphic representation of ExchangeRate that is easier to serialize and deserialize than ExchangeRate in case you don't know the type indexes involved.

Instances

 Source # Methods Source # Methods Source # MethodsshowList :: [ExchangeRateRep] -> ShowS # Source # Associated Typestype Rep ExchangeRateRep :: * -> * # Methods Source # Methods Source # Compatible with ExchangeRate MethodstoJSONList :: [ExchangeRateRep] -> Value # Source # Compatible with ExchangeRate Methods Source # Compatible with ExchangeRate. MethodsputList :: [ExchangeRateRep] -> Put # Source # Compatible with ExchangeRate. Methods Source # Methodsrnf :: ExchangeRateRep -> () # Source # Compatible with ExchangeRate. Methodspoke :: ExchangeRateRep -> Poke () # Source # type Rep ExchangeRateRep = D1 (MetaData "ExchangeRateRep" "Data.Money.Internal" "safe-money-0.2-9KEKOJrropPa8pssJxMaR" False) (C1 (MetaCons "ExchangeRateRep" PrefixI True) ((:*:) ((:*:) (S1 (MetaSel (Just Symbol "_exchangeRateRepSrcCurrency") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 String)) (S1 (MetaSel (Just Symbol "_exchangeRateRepDstCurrency") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 String))) ((:*:) (S1 (MetaSel (Just Symbol "_exchangeRateRepRateNumerator") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 Integer)) (S1 (MetaSel (Just Symbol "_exchangeRateRepRateDenominator") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 Integer)))))

Arguments

 :: (KnownSymbol src, KnownSymbol dst) => ExchangeRate src dst -> ExchangeRateRep

Convert a ExchangeRate to a DiscreteRep for ease of serialization.

Arguments

 :: (KnownSymbol src, KnownSymbol dst) => ExchangeRateRep -> Maybe (ExchangeRate src dst)

Attempt to convert a ExchangeRateRep to a ExchangeRate, provided you know the target src and dst types.

Arguments

 :: ExchangeRateRep -> (forall src dst. (KnownSymbol src, KnownSymbol dst) => ExchangeRate src dst -> r) -> r

Convert a ExchangeRateRep to a ExchangeRate without knowing the target currency and unit.

Notice that src and dst here can't leave its intended scope unless you can prove equality with some other type at the outer scope, but in that case you would be better off using fromExchangeRateRep directly.