{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
module Cardano.Ledger.Val (
Val (..),
inject,
scale,
invert,
sumVal,
)
where
import Cardano.Ledger.BaseTypes (Inject (..))
import Cardano.Ledger.Binary (DecCBOR, EncCBOR)
import Cardano.Ledger.Coin (Coin (..), CompactForm (..), DeltaCoin (..))
import Cardano.Ledger.Compactible (Compactible (..))
import Control.DeepSeq (NFData)
import Data.Aeson (ToJSON)
import Data.Coerce
import Data.Foldable as F (foldl')
import Data.Group (Abelian)
import NoThunks.Class (NoThunks)
class
( Compactible t
, Inject Coin t
, EncCBOR (CompactForm t)
, DecCBOR (CompactForm t)
, Abelian t
, NoThunks t
, EncCBOR t
, DecCBOR t
, ToJSON t
, NFData t
, Show t
, Eq t
) =>
Val t
where
zero :: t
zero = forall a. Monoid a => a
mempty
(<+>) :: t -> t -> t
t
x <+> t
y = t
x forall a. Semigroup a => a -> a -> a
<> t
y
(<×>) :: Integral i => i -> t -> t
(<->) :: t -> t -> t
t
x <-> t
y = t
x forall t. Val t => t -> t -> t
<+> ((-Integer
1 :: Integer) forall t i. (Val t, Integral i) => i -> t -> t
<×> t
y)
isZero :: t -> Bool
isZero t
t = t
t forall a. Eq a => a -> a -> Bool
== forall a. Monoid a => a
mempty
coin :: t -> Coin
modifyCoin :: (Coin -> Coin) -> t -> t
size :: t -> Integer
pointwise :: (Integer -> Integer -> Bool) -> t -> t -> Bool
isAdaOnly :: t -> Bool
isAdaOnlyCompact :: CompactForm t -> Bool
coinCompact :: CompactForm t -> CompactForm Coin
injectCompact :: CompactForm Coin -> CompactForm t
modifyCompactCoin :: (CompactForm Coin -> CompactForm Coin) -> CompactForm t -> CompactForm t
infixl 6 <+>
infixl 6 <->
infixl 7 <×>
scale :: (Val t, Integral i) => i -> t -> t
scale :: forall t i. (Val t, Integral i) => i -> t -> t
scale i
i t
v = i
i forall t i. (Val t, Integral i) => i -> t -> t
<×> t
v
sumVal :: (Foldable t, Val v) => t v -> v
sumVal :: forall (t :: * -> *) v. (Foldable t, Val v) => t v -> v
sumVal = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' forall t. Val t => t -> t -> t
(<+>) forall a. Monoid a => a
mempty
invert :: Val t => t -> t
invert :: forall t. Val t => t -> t
invert t
x = (-Integer
1 :: Integer) forall t i. (Val t, Integral i) => i -> t -> t
<×> t
x
instance Val Coin where
i
n <×> :: forall i. Integral i => i -> Coin -> Coin
<×> (Coin Integer
x) = Integer -> Coin
Coin forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral i
n forall a. Num a => a -> a -> a
* Integer
x
coin :: Coin -> Coin
coin = forall a. a -> a
id
size :: Coin -> Integer
size Coin
_ = Integer
1
modifyCoin :: (Coin -> Coin) -> Coin -> Coin
modifyCoin Coin -> Coin
f Coin
v = Coin -> Coin
f Coin
v
pointwise :: (Integer -> Integer -> Bool) -> Coin -> Coin -> Bool
pointwise Integer -> Integer -> Bool
p (Coin Integer
x) (Coin Integer
y) = Integer -> Integer -> Bool
p Integer
x Integer
y
isAdaOnly :: Coin -> Bool
isAdaOnly Coin
_ = Bool
True
isAdaOnlyCompact :: CompactForm Coin -> Bool
isAdaOnlyCompact CompactForm Coin
_ = Bool
True
coinCompact :: CompactForm Coin -> CompactForm Coin
coinCompact = forall a. a -> a
id
injectCompact :: CompactForm Coin -> CompactForm Coin
injectCompact = forall a. a -> a
id
modifyCompactCoin :: (CompactForm Coin -> CompactForm Coin)
-> CompactForm Coin -> CompactForm Coin
modifyCompactCoin = forall a b. (a -> b) -> a -> b
($)
instance Val DeltaCoin where
i
n <×> :: forall i. Integral i => i -> DeltaCoin -> DeltaCoin
<×> (DeltaCoin Integer
x) = Integer -> DeltaCoin
DeltaCoin forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral i
n forall a. Num a => a -> a -> a
* Integer
x
coin :: DeltaCoin -> Coin
coin = coerce :: forall a b. Coercible a b => a -> b
coerce
size :: DeltaCoin -> Integer
size DeltaCoin
_ = Integer
1
modifyCoin :: (Coin -> Coin) -> DeltaCoin -> DeltaCoin
modifyCoin Coin -> Coin
f DeltaCoin
v = coerce :: forall a b. Coercible a b => a -> b
coerce Coin -> Coin
f DeltaCoin
v
pointwise :: (Integer -> Integer -> Bool) -> DeltaCoin -> DeltaCoin -> Bool
pointwise Integer -> Integer -> Bool
p (DeltaCoin Integer
x) (DeltaCoin Integer
y) = Integer -> Integer -> Bool
p Integer
x Integer
y
isAdaOnly :: DeltaCoin -> Bool
isAdaOnly DeltaCoin
_ = Bool
True
isAdaOnlyCompact :: CompactForm DeltaCoin -> Bool
isAdaOnlyCompact CompactForm DeltaCoin
_ = Bool
True
coinCompact :: CompactForm DeltaCoin -> CompactForm Coin
coinCompact (CompactDeltaCoin Word64
cc) = Word64 -> CompactForm Coin
CompactCoin Word64
cc
injectCompact :: CompactForm Coin -> CompactForm DeltaCoin
injectCompact (CompactCoin Word64
cc) = Word64 -> CompactForm DeltaCoin
CompactDeltaCoin Word64
cc
modifyCompactCoin :: (CompactForm Coin -> CompactForm Coin)
-> CompactForm DeltaCoin -> CompactForm DeltaCoin
modifyCompactCoin CompactForm Coin -> CompactForm Coin
f (CompactDeltaCoin Word64
cc) =
case CompactForm Coin -> CompactForm Coin
f (Word64 -> CompactForm Coin
CompactCoin Word64
cc) of
CompactCoin Word64
cc' -> Word64 -> CompactForm DeltaCoin
CompactDeltaCoin Word64
cc'