{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE StandaloneKindSignatures #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeData #-}
{-# LANGUAGE TypeFamilies #-}

-- | Model implementing committee selection based on the Fait Accompli scheme.
--
-- This is based on the paper:
--
-- Peter Gaži, Aggelos Kiayias, and Alexander Russell. 2023. Fait Accompli
-- Committee Selection: Improving the Size-Security Tradeoff of Stake-Based
-- Committees. In Proceedings of the 2023 ACM SIGSAC Conference on Computer and
-- Communications Security (CCS '23). Association for Computing Machinery, New
-- York, NY, USA, 845–858. https://doi.org/10.1145/3576915.3623194
--
-- PDF: https://eprint.iacr.org/2023/1273.pdf
module Test.Consensus.Committee.WFALS.Model
  ( -- * Auxiliary types
    StakeRole (..)
  , StakeType (..)
  , Stake (..)
  , IsStake (..)
  , coerceStake
  , adjustStake
  , SeatIndex
  , VoterId
  , NumSeats

    -- * Stake Distribution
  , StakeDistr
  , stakeDistrToDecreasingStakes
  , stakeDistrTotalStake

    -- * Fait Accompli Committee Selection
  , weightedFaitAccompliWith
  , weightedFaitAccompliPersistentSeats
  , weightedFaitAccompliThreshold
  , localSortition
  , makeStakeDistrVotingGlobal
  ) where

import Data.Bifunctor (Bifunctor (..))
import Data.Kind (Type)
import Data.List (sortBy)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Ord (Down (..), comparing)
import Data.Traversable (mapAccumR)
import GHC.Generics (Generic)
import Numeric.Natural (Natural, minusNaturalMaybe)
import System.Random (RandomGen)
import qualified System.Random.MWC.Distributions as MWC
import System.Random.Stateful (runStateGen)

-------------------------------------------------------------------------------

-- * Auxiliary types

-- | The role of a given stake.
--
-- NOTE: used mostly to better document type signatures in this module.
type data StakeRole
  = -- | Stake as recorded on the ledger.
    -- Used for inputs to the committee selection algorithm.
    Ledger
  | -- | Stake used when casting a new vote.
    -- This may be derived from the ledger stake via some transformation.
    -- Used for outputs of the committee selection algorithm.
    Weight

-- | Extra type information about a given stake.
--
-- NOTE: used mostly to better document type signatures in this module.
type data StakeType
  = Global
  | Persistent
  | Residual
  | Cumulative

-- | Number of seats in the committee
type NumSeats :: StakeType -> Type
type family NumSeats st

-- | Seat index (0-based)
type SeatIndex = Natural

-- | Voter identifier
type VoterId = String

-- ** Ledger and voting stakes

-- | Stake types annotated with their role and type information
data family Stake :: StakeRole -> StakeType -> Type

-- | Type class with operations needed over stakes
class (Num stake, Ord stake) => IsStake stake where
  stakeToRational :: stake -> Rational
  rationalToStake :: Rational -> stake

-- | DerivingVia helper to implement 'IsStake' instances for different stake
-- types via a common underlying representation (Rational).
type RationalStake :: StakeRole -> StakeType -> Type
newtype RationalStake sr st
  = RationalStake Rational
  deriving stock (RationalStake sr st -> RationalStake sr st -> Bool
(RationalStake sr st -> RationalStake sr st -> Bool)
-> (RationalStake sr st -> RationalStake sr st -> Bool)
-> Eq (RationalStake sr st)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st -> Bool
$c== :: forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st -> Bool
== :: RationalStake sr st -> RationalStake sr st -> Bool
$c/= :: forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st -> Bool
/= :: RationalStake sr st -> RationalStake sr st -> Bool
Eq, Eq (RationalStake sr st)
Eq (RationalStake sr st) =>
(RationalStake sr st -> RationalStake sr st -> Ordering)
-> (RationalStake sr st -> RationalStake sr st -> Bool)
-> (RationalStake sr st -> RationalStake sr st -> Bool)
-> (RationalStake sr st -> RationalStake sr st -> Bool)
-> (RationalStake sr st -> RationalStake sr st -> Bool)
-> (RationalStake sr st
    -> RationalStake sr st -> RationalStake sr st)
-> (RationalStake sr st
    -> RationalStake sr st -> RationalStake sr st)
-> Ord (RationalStake sr st)
RationalStake sr st -> RationalStake sr st -> Bool
RationalStake sr st -> RationalStake sr st -> Ordering
RationalStake sr st -> RationalStake sr st -> RationalStake sr st
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
forall (sr :: StakeRole) (st :: StakeType).
Eq (RationalStake sr st)
forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st -> Bool
forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st -> Ordering
forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st -> RationalStake sr st
$ccompare :: forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st -> Ordering
compare :: RationalStake sr st -> RationalStake sr st -> Ordering
$c< :: forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st -> Bool
< :: RationalStake sr st -> RationalStake sr st -> Bool
$c<= :: forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st -> Bool
<= :: RationalStake sr st -> RationalStake sr st -> Bool
$c> :: forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st -> Bool
> :: RationalStake sr st -> RationalStake sr st -> Bool
$c>= :: forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st -> Bool
>= :: RationalStake sr st -> RationalStake sr st -> Bool
$cmax :: forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st -> RationalStake sr st
max :: RationalStake sr st -> RationalStake sr st -> RationalStake sr st
$cmin :: forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st -> RationalStake sr st
min :: RationalStake sr st -> RationalStake sr st -> RationalStake sr st
Ord, Int -> RationalStake sr st -> ShowS
[RationalStake sr st] -> ShowS
RationalStake sr st -> VoterId
(Int -> RationalStake sr st -> ShowS)
-> (RationalStake sr st -> VoterId)
-> ([RationalStake sr st] -> ShowS)
-> Show (RationalStake sr st)
forall a.
(Int -> a -> ShowS) -> (a -> VoterId) -> ([a] -> ShowS) -> Show a
forall (sr :: StakeRole) (st :: StakeType).
Int -> RationalStake sr st -> ShowS
forall (sr :: StakeRole) (st :: StakeType).
[RationalStake sr st] -> ShowS
forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> VoterId
$cshowsPrec :: forall (sr :: StakeRole) (st :: StakeType).
Int -> RationalStake sr st -> ShowS
showsPrec :: Int -> RationalStake sr st -> ShowS
$cshow :: forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> VoterId
show :: RationalStake sr st -> VoterId
$cshowList :: forall (sr :: StakeRole) (st :: StakeType).
[RationalStake sr st] -> ShowS
showList :: [RationalStake sr st] -> ShowS
Show, (forall x. RationalStake sr st -> Rep (RationalStake sr st) x)
-> (forall x. Rep (RationalStake sr st) x -> RationalStake sr st)
-> Generic (RationalStake sr st)
forall x. Rep (RationalStake sr st) x -> RationalStake sr st
forall x. RationalStake sr st -> Rep (RationalStake sr st) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (sr :: StakeRole) (st :: StakeType) x.
Rep (RationalStake sr st) x -> RationalStake sr st
forall (sr :: StakeRole) (st :: StakeType) x.
RationalStake sr st -> Rep (RationalStake sr st) x
$cfrom :: forall (sr :: StakeRole) (st :: StakeType) x.
RationalStake sr st -> Rep (RationalStake sr st) x
from :: forall x. RationalStake sr st -> Rep (RationalStake sr st) x
$cto :: forall (sr :: StakeRole) (st :: StakeType) x.
Rep (RationalStake sr st) x -> RationalStake sr st
to :: forall x. Rep (RationalStake sr st) x -> RationalStake sr st
Generic)
  deriving newtype Integer -> RationalStake sr st
RationalStake sr st -> RationalStake sr st
RationalStake sr st -> RationalStake sr st -> RationalStake sr st
(RationalStake sr st -> RationalStake sr st -> RationalStake sr st)
-> (RationalStake sr st
    -> RationalStake sr st -> RationalStake sr st)
-> (RationalStake sr st
    -> RationalStake sr st -> RationalStake sr st)
-> (RationalStake sr st -> RationalStake sr st)
-> (RationalStake sr st -> RationalStake sr st)
-> (RationalStake sr st -> RationalStake sr st)
-> (Integer -> RationalStake sr st)
-> Num (RationalStake sr st)
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
forall (sr :: StakeRole) (st :: StakeType).
Integer -> RationalStake sr st
forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st
forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st -> RationalStake sr st
$c+ :: forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st -> RationalStake sr st
+ :: RationalStake sr st -> RationalStake sr st -> RationalStake sr st
$c- :: forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st -> RationalStake sr st
- :: RationalStake sr st -> RationalStake sr st -> RationalStake sr st
$c* :: forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st -> RationalStake sr st
* :: RationalStake sr st -> RationalStake sr st -> RationalStake sr st
$cnegate :: forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st
negate :: RationalStake sr st -> RationalStake sr st
$cabs :: forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st
abs :: RationalStake sr st -> RationalStake sr st
$csignum :: forall (sr :: StakeRole) (st :: StakeType).
RationalStake sr st -> RationalStake sr st
signum :: RationalStake sr st -> RationalStake sr st
$cfromInteger :: forall (sr :: StakeRole) (st :: StakeType).
Integer -> RationalStake sr st
fromInteger :: Integer -> RationalStake sr st
Num

instance IsStake (RationalStake sr st) where
  stakeToRational :: RationalStake sr st -> Rational
stakeToRational (RationalStake Rational
r) = Rational
r
  rationalToStake :: Rational -> RationalStake sr st
rationalToStake = Rational -> RationalStake sr st
forall (sr :: StakeRole) (st :: StakeType).
Rational -> RationalStake sr st
RationalStake

-- | Adjust a stake via a function on rationals
adjustStake ::
  forall stake' stake.
  ( IsStake stake
  , IsStake stake'
  ) =>
  (Rational -> Rational) ->
  stake ->
  stake'
adjustStake :: forall stake' stake.
(IsStake stake, IsStake stake') =>
(Rational -> Rational) -> stake -> stake'
adjustStake Rational -> Rational
f stake
stake =
  Rational -> stake'
forall stake. IsStake stake => Rational -> stake
rationalToStake (Rational -> Rational
f (stake -> Rational
forall stake. IsStake stake => stake -> Rational
stakeToRational stake
stake))

-- | Retag a stake with a different origin and type.
--
-- NOTE: type parameters are arranged so that one can use type applications
-- to specify the parameters of the resulting type first.
coerceStake ::
  forall stake' stake.
  ( IsStake stake
  , IsStake stake'
  ) =>
  stake ->
  stake'
coerceStake :: forall stake' stake.
(IsStake stake, IsStake stake') =>
stake -> stake'
coerceStake =
  (Rational -> Rational) -> stake -> stake'
forall stake' stake.
(IsStake stake, IsStake stake') =>
(Rational -> Rational) -> stake -> stake'
adjustStake Rational -> Rational
forall a. a -> a
id

-- ** Stake distributions

-- | Stake distribution mapping voters to their stakes
type StakeDistr (sr :: StakeRole) (st :: StakeType) = Map VoterId (Stake sr st)

-- | Get the total stake in a stake distribution
stakeDistrTotalStake ::
  forall sr st.
  IsStake (Stake sr st) =>
  StakeDistr sr st ->
  Stake sr st
stakeDistrTotalStake :: forall (sr :: StakeRole) (st :: StakeType).
IsStake (Stake sr st) =>
StakeDistr sr st -> Stake sr st
stakeDistrTotalStake StakeDistr sr st
distr =
  [Stake sr st] -> Stake sr st
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (StakeDistr sr st -> [Stake sr st]
forall k a. Map k a -> [a]
Map.elems StakeDistr sr st
distr)

-- | View a stake distribution as a non-increasing list of stakes
stakeDistrToDecreasingStakes ::
  IsStake (Stake sr st) =>
  StakeDistr sr st ->
  [(VoterId, Stake sr st)]
stakeDistrToDecreasingStakes :: forall (sr :: StakeRole) (st :: StakeType).
IsStake (Stake sr st) =>
StakeDistr sr st -> [(VoterId, Stake sr st)]
stakeDistrToDecreasingStakes StakeDistr sr st
distr =
  ((VoterId, Stake sr st) -> (VoterId, Stake sr st) -> Ordering)
-> [(VoterId, Stake sr st)] -> [(VoterId, Stake sr st)]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (((VoterId, Stake sr st) -> Down (Stake sr st))
-> (VoterId, Stake sr st) -> (VoterId, Stake sr st) -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing (Stake sr st -> Down (Stake sr st)
forall a. a -> Down a
Down (Stake sr st -> Down (Stake sr st))
-> ((VoterId, Stake sr st) -> Stake sr st)
-> (VoterId, Stake sr st)
-> Down (Stake sr st)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (VoterId, Stake sr st) -> Stake sr st
forall a b. (a, b) -> b
snd)) (StakeDistr sr st -> [(VoterId, Stake sr st)]
forall k a. Map k a -> [(k, a)]
Map.toList StakeDistr sr st
distr)

-------------------------------------------------------------------------------

-- * Fait Accompli Committee Selection

-- | Weighted Fait Accompli committee selection with a fallback mechanism.
--
-- This is mostly just plumbing to connect the result of
-- 'weightedFaitAccompliPersistentSeats' with a given fallback scheme.
weightedFaitAccompliWith ::
  -- | Fallback function for non-persistent seats
  ( NumSeats Residual ->
    StakeDistr Ledger Residual ->
    StakeDistr Weight Residual
  ) ->
  -- | Total number of seats
  NumSeats Global ->
  -- | Stake distribution
  StakeDistr Ledger Global ->
  -- | Selected persistent and non-persistent seats with their corresponding
  -- voting stakes
  ( StakeDistr Weight Persistent
  , StakeDistr Weight Residual
  )
weightedFaitAccompliWith :: (NumSeats Residual
 -> StakeDistr Ledger Residual -> StakeDistr Weight Residual)
-> NumSeats Global
-> StakeDistr Ledger Global
-> (StakeDistr Weight Persistent, StakeDistr Weight Residual)
weightedFaitAccompliWith NumSeats Residual
-> StakeDistr Ledger Residual -> StakeDistr Weight Residual
fallback NumSeats Global
globalNumSeats StakeDistr Ledger Global
stakeDistr =
  (StakeDistr Weight Persistent
persistentSeats, StakeDistr Weight Residual
nonPersistentSeats)
 where
  (StakeDistr Weight Persistent
persistentSeats, NumSeats Residual
numNonPersistentSeats, StakeDistr Ledger Residual
residualStakeDistr) =
    NumSeats Global
-> StakeDistr Ledger Global
-> (StakeDistr Weight Persistent, NumSeats Residual,
    StakeDistr Ledger Residual)
weightedFaitAccompliPersistentSeats
      NumSeats Global
globalNumSeats
      StakeDistr Ledger Global
stakeDistr

  nonPersistentSeats :: StakeDistr Weight Residual
nonPersistentSeats =
    NumSeats Residual
-> StakeDistr Ledger Residual -> StakeDistr Weight Residual
fallback
      NumSeats Residual
numNonPersistentSeats
      StakeDistr Ledger Residual
residualStakeDistr

-- | First step of the weighted Fait Accompli committee selection.
--
-- This step selects the persistent seats deterministically via the weighted
-- Fait Accompli threshold, and returns the remaining stake distribution for the
-- non-persistent seats selection.
weightedFaitAccompliPersistentSeats ::
  NumSeats Global ->
  StakeDistr Ledger Global ->
  ( StakeDistr Weight Persistent
  , NumSeats Residual
  , StakeDistr Ledger Residual
  )
weightedFaitAccompliPersistentSeats :: NumSeats Global
-> StakeDistr Ledger Global
-> (StakeDistr Weight Persistent, NumSeats Residual,
    StakeDistr Ledger Residual)
weightedFaitAccompliPersistentSeats NumSeats Global
globalNumSeats StakeDistr Ledger Global
stakeDistr
  | SeatIndex
NumSeats Global
globalNumSeats SeatIndex -> SeatIndex -> Bool
forall a. Ord a => a -> a -> Bool
<= SeatIndex
0 =
      VoterId
-> (StakeDistr Weight Persistent, SeatIndex,
    StakeDistr Ledger Residual)
forall a. HasCallStack => VoterId -> a
error VoterId
"Number of seats must be positive"
  | StakeDistr Ledger Global -> Int
forall k a. Map k a -> Int
Map.size StakeDistr Ledger Global
stakeDistr Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 =
      VoterId
-> (StakeDistr Weight Persistent, SeatIndex,
    StakeDistr Ledger Residual)
forall a. HasCallStack => VoterId -> a
error VoterId
"Stake distribution cannot be empty"
  | StakeDistr Ledger Global -> Stake Ledger Global
forall a. Num a => Map VoterId a -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum StakeDistr Ledger Global
stakeDistr Stake Ledger Global -> Stake Ledger Global -> Bool
forall a. Eq a => a -> a -> Bool
== Stake Ledger Global
0 =
      VoterId
-> (StakeDistr Weight Persistent, SeatIndex,
    StakeDistr Ledger Residual)
forall a. HasCallStack => VoterId -> a
error VoterId
"Total stake must be positive"
  | Bool
otherwise =
      (StakeDistr Weight Persistent
persistentSeats, SeatIndex
NumSeats Residual
numNonPersistentSeats, StakeDistr Ledger Residual
residualStakeDistr)
 where
  -- Persistent seats selected deterministically based on their ledger stake.
  -- NOTE: their voting stake is *exactly* their ledger stake.
  persistentSeats :: StakeDistr Weight Persistent
persistentSeats =
    [(VoterId, Stake Weight Persistent, SeatIndex,
  Stake Ledger Cumulative)]
-> StakeDistr Weight Persistent
forall {a} {c} {d}. [(VoterId, a, c, d)] -> Map VoterId a
voterListToStakeDistr [(VoterId, Stake Weight Persistent, SeatIndex,
  Stake Ledger Cumulative)]
persistentVotersWithStake

  -- Residual stake distribution for the non-persistent seats selection. This
  -- includes all the voters that did not qualify for a persistent seat, with
  -- their original ledger stake.
  residualStakeDistr :: StakeDistr Ledger Residual
residualStakeDistr =
    [(VoterId, Stake Ledger Residual, SeatIndex,
  Stake Ledger Cumulative)]
-> StakeDistr Ledger Residual
forall {a} {c} {d}. [(VoterId, a, c, d)] -> Map VoterId a
voterListToStakeDistr [(VoterId, Stake Ledger Residual, SeatIndex,
  Stake Ledger Cumulative)]
residualVotersWithStake

  -- Number of non-persistent seats to be selected via a fallback scheme
  numNonPersistentSeats :: SeatIndex
numNonPersistentSeats =
    case SeatIndex
NumSeats Global
globalNumSeats SeatIndex -> SeatIndex -> Maybe SeatIndex
`minusNaturalMaybe` SeatIndex
numPersistentSeats of
      Just SeatIndex
n -> SeatIndex
n
      Maybe SeatIndex
Nothing -> VoterId -> SeatIndex
forall a. HasCallStack => VoterId -> a
error VoterId
"Number of persistent seats exceeds total seats"
   where
    numPersistentSeats :: SeatIndex
numPersistentSeats = Int -> SeatIndex
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([(VoterId, Stake Weight Persistent, SeatIndex,
  Stake Ledger Cumulative)]
-> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(VoterId, Stake Weight Persistent, SeatIndex,
  Stake Ledger Cumulative)]
persistentVotersWithStake)

  -- Split the cumulative stakes into persistent and non-persistent seats
  ([(VoterId, Stake Weight Persistent, SeatIndex,
  Stake Ledger Cumulative)]
persistentVotersWithStake, [(VoterId, Stake Ledger Residual, SeatIndex,
  Stake Ledger Cumulative)]
residualVotersWithStake) =
    ([(VoterId, Stake Ledger Global, SeatIndex,
   Stake Ledger Cumulative)]
 -> [(VoterId, Stake Weight Persistent, SeatIndex,
      Stake Ledger Cumulative)])
-> ([(VoterId, Stake Ledger Global, SeatIndex,
      Stake Ledger Cumulative)]
    -> [(VoterId, Stake Ledger Residual, SeatIndex,
         Stake Ledger Cumulative)])
-> ([(VoterId, Stake Ledger Global, SeatIndex,
      Stake Ledger Cumulative)],
    [(VoterId, Stake Ledger Global, SeatIndex,
      Stake Ledger Cumulative)])
-> ([(VoterId, Stake Weight Persistent, SeatIndex,
      Stake Ledger Cumulative)],
    [(VoterId, Stake Ledger Residual, SeatIndex,
      Stake Ledger Cumulative)])
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap
      ((Stake Ledger Global -> Stake Weight Persistent)
-> [(VoterId, Stake Ledger Global, SeatIndex,
     Stake Ledger Cumulative)]
-> [(VoterId, Stake Weight Persistent, SeatIndex,
     Stake Ledger Cumulative)]
forall {f :: * -> *} {t} {b} {a} {c} {d}.
Functor f =>
(t -> b) -> f (a, t, c, d) -> f (a, b, c, d)
mapStakes (forall stake' stake.
(IsStake stake, IsStake stake') =>
stake -> stake'
coerceStake @(Stake Weight Persistent)))
      ((Stake Ledger Global -> Stake Ledger Residual)
-> [(VoterId, Stake Ledger Global, SeatIndex,
     Stake Ledger Cumulative)]
-> [(VoterId, Stake Ledger Residual, SeatIndex,
     Stake Ledger Cumulative)]
forall {f :: * -> *} {t} {b} {a} {c} {d}.
Functor f =>
(t -> b) -> f (a, t, c, d) -> f (a, b, c, d)
mapStakes (forall stake' stake.
(IsStake stake, IsStake stake') =>
stake -> stake'
coerceStake @(Stake Ledger Residual)))
      (([(VoterId, Stake Ledger Global, SeatIndex,
    Stake Ledger Cumulative)],
  [(VoterId, Stake Ledger Global, SeatIndex,
    Stake Ledger Cumulative)])
 -> ([(VoterId, Stake Weight Persistent, SeatIndex,
       Stake Ledger Cumulative)],
     [(VoterId, Stake Ledger Residual, SeatIndex,
       Stake Ledger Cumulative)]))
-> ([(VoterId, Stake Ledger Global, SeatIndex,
      Stake Ledger Cumulative)],
    [(VoterId, Stake Ledger Global, SeatIndex,
      Stake Ledger Cumulative)])
-> ([(VoterId, Stake Weight Persistent, SeatIndex,
      Stake Ledger Cumulative)],
    [(VoterId, Stake Ledger Residual, SeatIndex,
      Stake Ledger Cumulative)])
forall a b. (a -> b) -> a -> b
$ ((VoterId, Stake Ledger Global, SeatIndex, Stake Ledger Cumulative)
 -> Bool)
-> [(VoterId, Stake Ledger Global, SeatIndex,
     Stake Ledger Cumulative)]
-> ([(VoterId, Stake Ledger Global, SeatIndex,
      Stake Ledger Cumulative)],
    [(VoterId, Stake Ledger Global, SeatIndex,
      Stake Ledger Cumulative)])
forall a. (a -> Bool) -> [a] -> ([a], [a])
span
        ( \(VoterId
_, Stake Ledger Global
voterStake, SeatIndex
seatIndex, Stake Ledger Cumulative
stakeAccum) ->
            NumSeats Global
-> SeatIndex
-> Stake Ledger Global
-> Stake Ledger Cumulative
-> Bool
weightedFaitAccompliThreshold
              NumSeats Global
globalNumSeats
              SeatIndex
seatIndex
              Stake Ledger Global
voterStake
              Stake Ledger Cumulative
stakeAccum
        )
        [(VoterId, Stake Ledger Global, SeatIndex,
  Stake Ledger Cumulative)]
rightCumulativeDecreasingStakes
   where
    mapStakes :: (t -> b) -> f (a, t, c, d) -> f (a, b, c, d)
mapStakes t -> b
f = ((a, t, c, d) -> (a, b, c, d)) -> f (a, t, c, d) -> f (a, b, c, d)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(a
x, t
s, c
y, d
z) -> (a
x, t -> b
f t
s, c
y, d
z))

  -- Stake distribution in non-increasing order, including voter seat index and
  -- accumulated stakes from the right, i.e., from all the voters with smaller
  -- stake than the current one.
  rightCumulativeDecreasingStakes :: [(VoterId, Stake Ledger Global, SeatIndex,
  Stake Ledger Cumulative)]
rightCumulativeDecreasingStakes =
    [(VoterId, Stake Ledger Global, SeatIndex)]
-> [(VoterId, Stake Ledger Global, SeatIndex,
     Stake Ledger Cumulative)]
forall {t :: * -> *} {b} {a} {c}.
(Traversable t, IsStake b) =>
t (a, b, c) -> t (a, b, c, Stake Ledger Cumulative)
accumStakeR ([(VoterId, Stake Ledger Global, SeatIndex)]
 -> [(VoterId, Stake Ledger Global, SeatIndex,
      Stake Ledger Cumulative)])
-> [(VoterId, Stake Ledger Global, SeatIndex)]
-> [(VoterId, Stake Ledger Global, SeatIndex,
     Stake Ledger Cumulative)]
forall a b. (a -> b) -> a -> b
$
      [(VoterId, Stake Ledger Global)]
-> [(VoterId, Stake Ledger Global, SeatIndex)]
forall {a} {b}. [(a, b)] -> [(a, b, SeatIndex)]
addSeatIndex ([(VoterId, Stake Ledger Global)]
 -> [(VoterId, Stake Ledger Global, SeatIndex)])
-> [(VoterId, Stake Ledger Global)]
-> [(VoterId, Stake Ledger Global, SeatIndex)]
forall a b. (a -> b) -> a -> b
$
        StakeDistr Ledger Global -> [(VoterId, Stake Ledger Global)]
forall (sr :: StakeRole) (st :: StakeType).
IsStake (Stake sr st) =>
StakeDistr sr st -> [(VoterId, Stake sr st)]
stakeDistrToDecreasingStakes (StakeDistr Ledger Global -> [(VoterId, Stake Ledger Global)])
-> StakeDistr Ledger Global -> [(VoterId, Stake Ledger Global)]
forall a b. (a -> b) -> a -> b
$
          StakeDistr Ledger Global
stakeDistr
   where
    -- Add an increasing seat index to each candidate
    addSeatIndex :: [(a, b)] -> [(a, b, SeatIndex)]
addSeatIndex =
      (SeatIndex -> (a, b) -> (a, b, SeatIndex))
-> [SeatIndex] -> [(a, b)] -> [(a, b, SeatIndex)]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith
        (\SeatIndex
seatIndex (a
vId, b
vStake) -> (a
vId, b
vStake, SeatIndex
seatIndex))
        [SeatIndex
0 ..]
    -- Accumulated stake of every candidate beyond (and including) this one
    accumStakeR :: t (a, b, c) -> t (a, b, c, Stake Ledger Cumulative)
accumStakeR t (a, b, c)
xs =
      (Stake Ledger Cumulative, t (a, b, c, Stake Ledger Cumulative))
-> t (a, b, c, Stake Ledger Cumulative)
forall a b. (a, b) -> b
snd ((Stake Ledger Cumulative, t (a, b, c, Stake Ledger Cumulative))
 -> t (a, b, c, Stake Ledger Cumulative))
-> (Stake Ledger Cumulative, t (a, b, c, Stake Ledger Cumulative))
-> t (a, b, c, Stake Ledger Cumulative)
forall a b. (a -> b) -> a -> b
$
        (Stake Ledger Cumulative
 -> (a, b, c)
 -> (Stake Ledger Cumulative, (a, b, c, Stake Ledger Cumulative)))
-> Stake Ledger Cumulative
-> t (a, b, c)
-> (Stake Ledger Cumulative, t (a, b, c, Stake Ledger Cumulative))
forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
mapAccumR
          ( \Stake Ledger Cumulative
stakeAccR (a
voterId, b
voterStake, c
seatIndex) ->
              let stakeAccR' :: Stake Ledger Cumulative
stakeAccR' =
                    Stake Ledger Cumulative
stakeAccR
                      Stake Ledger Cumulative
-> Stake Ledger Cumulative -> Stake Ledger Cumulative
forall a. Num a => a -> a -> a
+ forall stake' stake.
(IsStake stake, IsStake stake') =>
stake -> stake'
coerceStake @(Stake Ledger Cumulative) b
voterStake
               in ( Stake Ledger Cumulative
stakeAccR'
                  , (a
voterId, b
voterStake, c
seatIndex, Stake Ledger Cumulative
stakeAccR')
                  )
          )
          Stake Ledger Cumulative
0
          t (a, b, c)
xs

  -- Drop the accumulated information to get back to a stake distribution
  voterListToStakeDistr :: [(VoterId, a, c, d)] -> Map VoterId a
voterListToStakeDistr =
    [(VoterId, a)] -> Map VoterId a
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(VoterId, a)] -> Map VoterId a)
-> ([(VoterId, a, c, d)] -> [(VoterId, a)])
-> [(VoterId, a, c, d)]
-> Map VoterId a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((VoterId, a, c, d) -> (VoterId, a))
-> [(VoterId, a, c, d)] -> [(VoterId, a)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(VoterId
voterId, a
stake, c
_, d
_) -> (VoterId
voterId, a
stake))

-- | Weighted Fait Accompli threshold for persistent seats
--
-- For more details, see (Section 4, Figure 7) of the paper:
-- https://eprint.iacr.org/2023/1273.pdf
weightedFaitAccompliThreshold ::
  -- | Total number of seats
  NumSeats Global ->
  -- | Voter seat index (0-based)
  SeatIndex ->
  -- | Voter ledger stake
  Stake Ledger Global ->
  -- | Cumulative ledger stake from the right (including the current voter stake)
  Stake Ledger Cumulative ->
  -- | Whether the voter qualifies for a persistent seat
  Bool
weightedFaitAccompliThreshold :: NumSeats Global
-> SeatIndex
-> Stake Ledger Global
-> Stake Ledger Cumulative
-> Bool
weightedFaitAccompliThreshold NumSeats Global
numSeats SeatIndex
voterSeat Stake Ledger Global
voterStake Stake Ledger Cumulative
stakeAccR
  | Stake Ledger Cumulative -> Rational
forall stake. IsStake stake => stake -> Rational
stakeToRational Stake Ledger Cumulative
stakeAccR Rational -> Rational -> Bool
forall a. Ord a => a -> a -> Bool
<= Rational
0 =
      Bool
False -- Avoid division by zero in the right-hand side of the inequality
  | SeatIndex
voterSeat SeatIndex -> SeatIndex -> Bool
forall a. Ord a => a -> a -> Bool
>= SeatIndex
NumSeats Global
numSeats =
      Bool
False -- Avoid underflow in the right-hand side of the inequality
  | Bool
otherwise =
      ( (Rational
1 Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
- (Stake Ledger Global -> Rational
forall stake. IsStake stake => stake -> Rational
stakeToRational Stake Ledger Global
voterStake Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ Stake Ledger Cumulative -> Rational
forall stake. IsStake stake => stake -> Rational
stakeToRational Stake Ledger Cumulative
stakeAccR))
          Rational -> Integer -> Rational
forall a b. (Num a, Integral b) => a -> b -> a
^ (Integer
2 :: Integer)
      )
        Rational -> Rational -> Bool
forall a. Ord a => a -> a -> Bool
< SeatIndex -> Rational
forall a. Real a => a -> Rational
toRational (SeatIndex
NumSeats Global
numSeats SeatIndex -> SeatIndex -> SeatIndex
forall a. Num a => a -> a -> a
- SeatIndex
voterSeat SeatIndex -> SeatIndex -> SeatIndex
forall a. Num a => a -> a -> a
- SeatIndex
1)
          Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ SeatIndex -> Rational
forall a. Real a => a -> Rational
toRational (SeatIndex
NumSeats Global
numSeats SeatIndex -> SeatIndex -> SeatIndex
forall a. Num a => a -> a -> a
- SeatIndex
voterSeat)

-- | Local sortition fallback
localSortition ::
  ( IsStake (Stake Ledger st)
  , IsStake (Stake Weight st)
  , Integral (NumSeats st)
  ) =>
  RandomGen rng =>
  -- | Random number generator
  rng ->
  -- | Expected number of non-persistent seats
  NumSeats st ->
  -- | Non-persistent stake distribution candidate (not necessarily normalized)
  StakeDistr Ledger st ->
  -- | Selected non-persistent seats with their correspoinding voting stakes
  StakeDistr Weight st
localSortition :: forall (st :: StakeType) rng.
(IsStake (Stake Ledger st), IsStake (Stake Weight st),
 Integral (NumSeats st), RandomGen rng) =>
rng -> NumSeats st -> StakeDistr Ledger st -> StakeDistr Weight st
localSortition rng
rng0 NumSeats st
numSeats StakeDistr Ledger st
stakeDistr =
  [(VoterId, Stake Weight st)] -> Map VoterId (Stake Weight st)
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(VoterId, Stake Weight st)] -> Map VoterId (Stake Weight st))
-> [(VoterId, Stake Weight st)] -> Map VoterId (Stake Weight st)
forall a b. (a -> b) -> a -> b
$
    [(VoterId, Stake Ledger st)] -> [(VoterId, Stake Weight st)]
sampleNonPersistentVoters ([(VoterId, Stake Ledger st)] -> [(VoterId, Stake Weight st)])
-> [(VoterId, Stake Ledger st)] -> [(VoterId, Stake Weight st)]
forall a b. (a -> b) -> a -> b
$
      StakeDistr Ledger st -> [(VoterId, Stake Ledger st)]
forall k a. Map k a -> [(k, a)]
Map.toList (StakeDistr Ledger st -> [(VoterId, Stake Ledger st)])
-> StakeDistr Ledger st -> [(VoterId, Stake Ledger st)]
forall a b. (a -> b) -> a -> b
$
        StakeDistr Ledger st
stakeDistr
 where
  -- The total stake of the non-persistent candidate stake distribution
  totalStake :: Stake Ledger st
totalStake = StakeDistr Ledger st -> Stake Ledger st
forall (sr :: StakeRole) (st :: StakeType).
IsStake (Stake sr st) =>
StakeDistr sr st -> Stake sr st
stakeDistrTotalStake StakeDistr Ledger st
stakeDistr
  -- All non-persistent voters share the same voting "weight"
  seatStake :: Stake Weight st
seatStake = Rational -> Stake Weight st
forall stake. IsStake stake => Rational -> stake
rationalToStake (Stake Ledger st -> Rational
forall stake. IsStake stake => stake -> Rational
stakeToRational Stake Ledger st
totalStake Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ NumSeats st -> Rational
forall a b. (Integral a, Num b) => a -> b
fromIntegral NumSeats st
numSeats)
  -- Sample non-persistent voter seats independently to emulate local sortition
  sampleNonPersistentVoters :: [(VoterId, Stake Ledger st)] -> [(VoterId, Stake Weight st)]
sampleNonPersistentVoters = (rng, [(VoterId, Stake Weight st)]) -> [(VoterId, Stake Weight st)]
forall a b. (a, b) -> b
snd ((rng, [(VoterId, Stake Weight st)])
 -> [(VoterId, Stake Weight st)])
-> ([(VoterId, Stake Ledger st)]
    -> (rng, [(VoterId, Stake Weight st)]))
-> [(VoterId, Stake Ledger st)]
-> [(VoterId, Stake Weight st)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((VoterId, Stake Ledger st)
 -> (rng, [(VoterId, Stake Weight st)])
 -> (rng, [(VoterId, Stake Weight st)]))
-> (rng, [(VoterId, Stake Weight st)])
-> [(VoterId, Stake Ledger st)]
-> (rng, [(VoterId, Stake Weight st)])
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (VoterId, Stake Ledger st)
-> (rng, [(VoterId, Stake Weight st)])
-> (rng, [(VoterId, Stake Weight st)])
sampleVoterStake (rng
rng0, [])
  -- Sample voter seats for each voter independently to emulate local sortition
  sampleVoterStake :: (VoterId, Stake Ledger st)
-> (rng, [(VoterId, Stake Weight st)])
-> (rng, [(VoterId, Stake Weight st)])
sampleVoterStake (VoterId
voterId, Stake Ledger st
stake) (rng
rng, [(VoterId, Stake Weight st)]
acc) =
    let
      normalizedStake :: Rational
normalizedStake = Stake Ledger st -> Rational
forall stake. IsStake stake => stake -> Rational
stakeToRational Stake Ledger st
stake Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ Stake Ledger st -> Rational
forall stake. IsStake stake => stake -> Rational
stakeToRational Stake Ledger st
totalStake
      -- Expected number of seats for this voter
      lambda :: Double
lambda = NumSeats st -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral NumSeats st
numSeats Double -> Double -> Double
forall a. Num a => a -> a -> a
* Rational -> Double
forall a. Fractional a => Rational -> a
fromRational Rational
normalizedStake
      -- Sample number of seats using Poisson distribution
      (SeatIndex
numSeatsForVoter, rng
rng') = Double -> rng -> (SeatIndex, rng)
forall rng. RandomGen rng => Double -> rng -> (SeatIndex, rng)
poisson Double
lambda rng
rng
      -- Voting stake of this voter equally divided
      --
      -- NOTE: if desired we could also scale this by the number of seats this
      -- voter was granted via the Poisson sampling.
      votingStake :: Stake Weight st
votingStake = SeatIndex -> Stake Weight st
forall a b. (Integral a, Num b) => a -> b
fromIntegral SeatIndex
numSeatsForVoter Stake Weight st -> Stake Weight st -> Stake Weight st
forall a. Num a => a -> a -> a
* Stake Weight st
seatStake
     in
      ( -- Thread the updated random generator
        rng
rng'
      , -- Accumulate voter if they won any seats
        if SeatIndex
numSeatsForVoter SeatIndex -> SeatIndex -> Bool
forall a. Ord a => a -> a -> Bool
> SeatIndex
0 then (VoterId
voterId, Stake Weight st
votingStake) (VoterId, Stake Weight st)
-> [(VoterId, Stake Weight st)] -> [(VoterId, Stake Weight st)]
forall a. a -> [a] -> [a]
: [(VoterId, Stake Weight st)]
acc else [(VoterId, Stake Weight st)]
acc
      )

-- | Combine the resulting persistent and non-persistent stake distributions
-- into a single global one
makeStakeDistrVotingGlobal ::
  StakeDistr Weight Persistent ->
  StakeDistr Weight Residual ->
  StakeDistr Weight Global
makeStakeDistrVotingGlobal :: StakeDistr Weight Persistent
-> StakeDistr Weight Residual -> StakeDistr Weight Global
makeStakeDistrVotingGlobal StakeDistr Weight Persistent
persistent StakeDistr Weight Residual
residual =
  (Stake Weight Global -> Stake Weight Global -> Stake Weight Global)
-> StakeDistr Weight Global
-> StakeDistr Weight Global
-> StakeDistr Weight Global
forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith
    Stake Weight Global -> Stake Weight Global -> Stake Weight Global
forall a. Num a => a -> a -> a
(+)
    (forall stake' stake.
(IsStake stake, IsStake stake') =>
stake -> stake'
coerceStake @(Stake Weight Global) (Stake Weight Persistent -> Stake Weight Global)
-> StakeDistr Weight Persistent -> StakeDistr Weight Global
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> StakeDistr Weight Persistent
persistent)
    (forall stake' stake.
(IsStake stake, IsStake stake') =>
stake -> stake'
coerceStake @(Stake Weight Global) (Stake Weight Residual -> Stake Weight Global)
-> StakeDistr Weight Residual -> StakeDistr Weight Global
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> StakeDistr Weight Residual
residual)

-- ** Random sampling helpers

-- | Wrapper over 'mwc-random' to sample from a Poisson distribution using an
-- explicit seed.
poisson :: RandomGen rng => Double -> rng -> (Natural, rng)
poisson :: forall rng. RandomGen rng => Double -> rng -> (SeatIndex, rng)
poisson Double
lambda rng
rng0
  | Double
lambda Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
0 =
      VoterId -> (SeatIndex, rng)
forall a. HasCallStack => VoterId -> a
error VoterId
"Poisson's lambda parameter must be non-negative"
  | Bool
otherwise =
      (Int -> SeatIndex) -> (Int, rng) -> (SeatIndex, rng)
forall a b c. (a -> b) -> (a, c) -> (b, c)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Int -> SeatIndex
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Int, rng) -> (SeatIndex, rng)) -> (Int, rng) -> (SeatIndex, rng)
forall a b. (a -> b) -> a -> b
$
        rng -> (StateGenM rng -> State rng Int) -> (Int, rng)
forall g a.
RandomGen g =>
g -> (StateGenM g -> State g a) -> (a, g)
runStateGen rng
rng0 ((StateGenM rng -> State rng Int) -> (Int, rng))
-> (StateGenM rng -> State rng Int) -> (Int, rng)
forall a b. (a -> b) -> a -> b
$
          Double -> StateGenM rng -> State rng Int
forall g (m :: * -> *). StatefulGen g m => Double -> g -> m Int
MWC.poisson Double
lambda

-------------------------------------------------------------------------------

-- * Boilerplate instances

type instance NumSeats Global = Natural

type instance NumSeats Persistent = Natural

type instance NumSeats Residual = Natural

newtype instance Stake Ledger Global = StakeLedgerGlobal Rational
  deriving stock (Stake Ledger Global -> Stake Ledger Global -> Bool
(Stake Ledger Global -> Stake Ledger Global -> Bool)
-> (Stake Ledger Global -> Stake Ledger Global -> Bool)
-> Eq (Stake Ledger Global)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Stake Ledger Global -> Stake Ledger Global -> Bool
== :: Stake Ledger Global -> Stake Ledger Global -> Bool
$c/= :: Stake Ledger Global -> Stake Ledger Global -> Bool
/= :: Stake Ledger Global -> Stake Ledger Global -> Bool
Eq, Eq (Stake Ledger Global)
Eq (Stake Ledger Global) =>
(Stake Ledger Global -> Stake Ledger Global -> Ordering)
-> (Stake Ledger Global -> Stake Ledger Global -> Bool)
-> (Stake Ledger Global -> Stake Ledger Global -> Bool)
-> (Stake Ledger Global -> Stake Ledger Global -> Bool)
-> (Stake Ledger Global -> Stake Ledger Global -> Bool)
-> (Stake Ledger Global
    -> Stake Ledger Global -> Stake Ledger Global)
-> (Stake Ledger Global
    -> Stake Ledger Global -> Stake Ledger Global)
-> Ord (Stake Ledger Global)
Stake Ledger Global -> Stake Ledger Global -> Bool
Stake Ledger Global -> Stake Ledger Global -> Ordering
Stake Ledger Global -> Stake Ledger Global -> Stake Ledger Global
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
$ccompare :: Stake Ledger Global -> Stake Ledger Global -> Ordering
compare :: Stake Ledger Global -> Stake Ledger Global -> Ordering
$c< :: Stake Ledger Global -> Stake Ledger Global -> Bool
< :: Stake Ledger Global -> Stake Ledger Global -> Bool
$c<= :: Stake Ledger Global -> Stake Ledger Global -> Bool
<= :: Stake Ledger Global -> Stake Ledger Global -> Bool
$c> :: Stake Ledger Global -> Stake Ledger Global -> Bool
> :: Stake Ledger Global -> Stake Ledger Global -> Bool
$c>= :: Stake Ledger Global -> Stake Ledger Global -> Bool
>= :: Stake Ledger Global -> Stake Ledger Global -> Bool
$cmax :: Stake Ledger Global -> Stake Ledger Global -> Stake Ledger Global
max :: Stake Ledger Global -> Stake Ledger Global -> Stake Ledger Global
$cmin :: Stake Ledger Global -> Stake Ledger Global -> Stake Ledger Global
min :: Stake Ledger Global -> Stake Ledger Global -> Stake Ledger Global
Ord, Int -> Stake Ledger Global -> ShowS
[Stake Ledger Global] -> ShowS
Stake Ledger Global -> VoterId
(Int -> Stake Ledger Global -> ShowS)
-> (Stake Ledger Global -> VoterId)
-> ([Stake Ledger Global] -> ShowS)
-> Show (Stake Ledger Global)
forall a.
(Int -> a -> ShowS) -> (a -> VoterId) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Stake Ledger Global -> ShowS
showsPrec :: Int -> Stake Ledger Global -> ShowS
$cshow :: Stake Ledger Global -> VoterId
show :: Stake Ledger Global -> VoterId
$cshowList :: [Stake Ledger Global] -> ShowS
showList :: [Stake Ledger Global] -> ShowS
Show, (forall x. Stake Ledger Global -> Rep (Stake Ledger Global) x)
-> (forall x. Rep (Stake Ledger Global) x -> Stake Ledger Global)
-> Generic (Stake Ledger Global)
forall x. Rep (Stake Ledger Global) x -> Stake Ledger Global
forall x. Stake Ledger Global -> Rep (Stake Ledger Global) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Stake Ledger Global -> Rep (Stake Ledger Global) x
from :: forall x. Stake Ledger Global -> Rep (Stake Ledger Global) x
$cto :: forall x. Rep (Stake Ledger Global) x -> Stake Ledger Global
to :: forall x. Rep (Stake Ledger Global) x -> Stake Ledger Global
Generic)
  deriving newtype Integer -> Stake Ledger Global
Stake Ledger Global -> Stake Ledger Global
Stake Ledger Global -> Stake Ledger Global -> Stake Ledger Global
(Stake Ledger Global -> Stake Ledger Global -> Stake Ledger Global)
-> (Stake Ledger Global
    -> Stake Ledger Global -> Stake Ledger Global)
-> (Stake Ledger Global
    -> Stake Ledger Global -> Stake Ledger Global)
-> (Stake Ledger Global -> Stake Ledger Global)
-> (Stake Ledger Global -> Stake Ledger Global)
-> (Stake Ledger Global -> Stake Ledger Global)
-> (Integer -> Stake Ledger Global)
-> Num (Stake Ledger Global)
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: Stake Ledger Global -> Stake Ledger Global -> Stake Ledger Global
+ :: Stake Ledger Global -> Stake Ledger Global -> Stake Ledger Global
$c- :: Stake Ledger Global -> Stake Ledger Global -> Stake Ledger Global
- :: Stake Ledger Global -> Stake Ledger Global -> Stake Ledger Global
$c* :: Stake Ledger Global -> Stake Ledger Global -> Stake Ledger Global
* :: Stake Ledger Global -> Stake Ledger Global -> Stake Ledger Global
$cnegate :: Stake Ledger Global -> Stake Ledger Global
negate :: Stake Ledger Global -> Stake Ledger Global
$cabs :: Stake Ledger Global -> Stake Ledger Global
abs :: Stake Ledger Global -> Stake Ledger Global
$csignum :: Stake Ledger Global -> Stake Ledger Global
signum :: Stake Ledger Global -> Stake Ledger Global
$cfromInteger :: Integer -> Stake Ledger Global
fromInteger :: Integer -> Stake Ledger Global
Num

newtype instance Stake Ledger Persistent = StakeLedgerPersistent Rational
  deriving stock (Stake Ledger Persistent -> Stake Ledger Persistent -> Bool
(Stake Ledger Persistent -> Stake Ledger Persistent -> Bool)
-> (Stake Ledger Persistent -> Stake Ledger Persistent -> Bool)
-> Eq (Stake Ledger Persistent)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Stake Ledger Persistent -> Stake Ledger Persistent -> Bool
== :: Stake Ledger Persistent -> Stake Ledger Persistent -> Bool
$c/= :: Stake Ledger Persistent -> Stake Ledger Persistent -> Bool
/= :: Stake Ledger Persistent -> Stake Ledger Persistent -> Bool
Eq, Eq (Stake Ledger Persistent)
Eq (Stake Ledger Persistent) =>
(Stake Ledger Persistent -> Stake Ledger Persistent -> Ordering)
-> (Stake Ledger Persistent -> Stake Ledger Persistent -> Bool)
-> (Stake Ledger Persistent -> Stake Ledger Persistent -> Bool)
-> (Stake Ledger Persistent -> Stake Ledger Persistent -> Bool)
-> (Stake Ledger Persistent -> Stake Ledger Persistent -> Bool)
-> (Stake Ledger Persistent
    -> Stake Ledger Persistent -> Stake Ledger Persistent)
-> (Stake Ledger Persistent
    -> Stake Ledger Persistent -> Stake Ledger Persistent)
-> Ord (Stake Ledger Persistent)
Stake Ledger Persistent -> Stake Ledger Persistent -> Bool
Stake Ledger Persistent -> Stake Ledger Persistent -> Ordering
Stake Ledger Persistent
-> Stake Ledger Persistent -> Stake Ledger Persistent
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
$ccompare :: Stake Ledger Persistent -> Stake Ledger Persistent -> Ordering
compare :: Stake Ledger Persistent -> Stake Ledger Persistent -> Ordering
$c< :: Stake Ledger Persistent -> Stake Ledger Persistent -> Bool
< :: Stake Ledger Persistent -> Stake Ledger Persistent -> Bool
$c<= :: Stake Ledger Persistent -> Stake Ledger Persistent -> Bool
<= :: Stake Ledger Persistent -> Stake Ledger Persistent -> Bool
$c> :: Stake Ledger Persistent -> Stake Ledger Persistent -> Bool
> :: Stake Ledger Persistent -> Stake Ledger Persistent -> Bool
$c>= :: Stake Ledger Persistent -> Stake Ledger Persistent -> Bool
>= :: Stake Ledger Persistent -> Stake Ledger Persistent -> Bool
$cmax :: Stake Ledger Persistent
-> Stake Ledger Persistent -> Stake Ledger Persistent
max :: Stake Ledger Persistent
-> Stake Ledger Persistent -> Stake Ledger Persistent
$cmin :: Stake Ledger Persistent
-> Stake Ledger Persistent -> Stake Ledger Persistent
min :: Stake Ledger Persistent
-> Stake Ledger Persistent -> Stake Ledger Persistent
Ord, Int -> Stake Ledger Persistent -> ShowS
[Stake Ledger Persistent] -> ShowS
Stake Ledger Persistent -> VoterId
(Int -> Stake Ledger Persistent -> ShowS)
-> (Stake Ledger Persistent -> VoterId)
-> ([Stake Ledger Persistent] -> ShowS)
-> Show (Stake Ledger Persistent)
forall a.
(Int -> a -> ShowS) -> (a -> VoterId) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Stake Ledger Persistent -> ShowS
showsPrec :: Int -> Stake Ledger Persistent -> ShowS
$cshow :: Stake Ledger Persistent -> VoterId
show :: Stake Ledger Persistent -> VoterId
$cshowList :: [Stake Ledger Persistent] -> ShowS
showList :: [Stake Ledger Persistent] -> ShowS
Show, (forall x.
 Stake Ledger Persistent -> Rep (Stake Ledger Persistent) x)
-> (forall x.
    Rep (Stake Ledger Persistent) x -> Stake Ledger Persistent)
-> Generic (Stake Ledger Persistent)
forall x.
Rep (Stake Ledger Persistent) x -> Stake Ledger Persistent
forall x.
Stake Ledger Persistent -> Rep (Stake Ledger Persistent) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x.
Stake Ledger Persistent -> Rep (Stake Ledger Persistent) x
from :: forall x.
Stake Ledger Persistent -> Rep (Stake Ledger Persistent) x
$cto :: forall x.
Rep (Stake Ledger Persistent) x -> Stake Ledger Persistent
to :: forall x.
Rep (Stake Ledger Persistent) x -> Stake Ledger Persistent
Generic)
  deriving newtype Integer -> Stake Ledger Persistent
Stake Ledger Persistent -> Stake Ledger Persistent
Stake Ledger Persistent
-> Stake Ledger Persistent -> Stake Ledger Persistent
(Stake Ledger Persistent
 -> Stake Ledger Persistent -> Stake Ledger Persistent)
-> (Stake Ledger Persistent
    -> Stake Ledger Persistent -> Stake Ledger Persistent)
-> (Stake Ledger Persistent
    -> Stake Ledger Persistent -> Stake Ledger Persistent)
-> (Stake Ledger Persistent -> Stake Ledger Persistent)
-> (Stake Ledger Persistent -> Stake Ledger Persistent)
-> (Stake Ledger Persistent -> Stake Ledger Persistent)
-> (Integer -> Stake Ledger Persistent)
-> Num (Stake Ledger Persistent)
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: Stake Ledger Persistent
-> Stake Ledger Persistent -> Stake Ledger Persistent
+ :: Stake Ledger Persistent
-> Stake Ledger Persistent -> Stake Ledger Persistent
$c- :: Stake Ledger Persistent
-> Stake Ledger Persistent -> Stake Ledger Persistent
- :: Stake Ledger Persistent
-> Stake Ledger Persistent -> Stake Ledger Persistent
$c* :: Stake Ledger Persistent
-> Stake Ledger Persistent -> Stake Ledger Persistent
* :: Stake Ledger Persistent
-> Stake Ledger Persistent -> Stake Ledger Persistent
$cnegate :: Stake Ledger Persistent -> Stake Ledger Persistent
negate :: Stake Ledger Persistent -> Stake Ledger Persistent
$cabs :: Stake Ledger Persistent -> Stake Ledger Persistent
abs :: Stake Ledger Persistent -> Stake Ledger Persistent
$csignum :: Stake Ledger Persistent -> Stake Ledger Persistent
signum :: Stake Ledger Persistent -> Stake Ledger Persistent
$cfromInteger :: Integer -> Stake Ledger Persistent
fromInteger :: Integer -> Stake Ledger Persistent
Num

newtype instance Stake Ledger Residual = StakeLedgerResidual Rational
  deriving stock (Stake Ledger Residual -> Stake Ledger Residual -> Bool
(Stake Ledger Residual -> Stake Ledger Residual -> Bool)
-> (Stake Ledger Residual -> Stake Ledger Residual -> Bool)
-> Eq (Stake Ledger Residual)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Stake Ledger Residual -> Stake Ledger Residual -> Bool
== :: Stake Ledger Residual -> Stake Ledger Residual -> Bool
$c/= :: Stake Ledger Residual -> Stake Ledger Residual -> Bool
/= :: Stake Ledger Residual -> Stake Ledger Residual -> Bool
Eq, Eq (Stake Ledger Residual)
Eq (Stake Ledger Residual) =>
(Stake Ledger Residual -> Stake Ledger Residual -> Ordering)
-> (Stake Ledger Residual -> Stake Ledger Residual -> Bool)
-> (Stake Ledger Residual -> Stake Ledger Residual -> Bool)
-> (Stake Ledger Residual -> Stake Ledger Residual -> Bool)
-> (Stake Ledger Residual -> Stake Ledger Residual -> Bool)
-> (Stake Ledger Residual
    -> Stake Ledger Residual -> Stake Ledger Residual)
-> (Stake Ledger Residual
    -> Stake Ledger Residual -> Stake Ledger Residual)
-> Ord (Stake Ledger Residual)
Stake Ledger Residual -> Stake Ledger Residual -> Bool
Stake Ledger Residual -> Stake Ledger Residual -> Ordering
Stake Ledger Residual
-> Stake Ledger Residual -> Stake Ledger Residual
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
$ccompare :: Stake Ledger Residual -> Stake Ledger Residual -> Ordering
compare :: Stake Ledger Residual -> Stake Ledger Residual -> Ordering
$c< :: Stake Ledger Residual -> Stake Ledger Residual -> Bool
< :: Stake Ledger Residual -> Stake Ledger Residual -> Bool
$c<= :: Stake Ledger Residual -> Stake Ledger Residual -> Bool
<= :: Stake Ledger Residual -> Stake Ledger Residual -> Bool
$c> :: Stake Ledger Residual -> Stake Ledger Residual -> Bool
> :: Stake Ledger Residual -> Stake Ledger Residual -> Bool
$c>= :: Stake Ledger Residual -> Stake Ledger Residual -> Bool
>= :: Stake Ledger Residual -> Stake Ledger Residual -> Bool
$cmax :: Stake Ledger Residual
-> Stake Ledger Residual -> Stake Ledger Residual
max :: Stake Ledger Residual
-> Stake Ledger Residual -> Stake Ledger Residual
$cmin :: Stake Ledger Residual
-> Stake Ledger Residual -> Stake Ledger Residual
min :: Stake Ledger Residual
-> Stake Ledger Residual -> Stake Ledger Residual
Ord, Int -> Stake Ledger Residual -> ShowS
[Stake Ledger Residual] -> ShowS
Stake Ledger Residual -> VoterId
(Int -> Stake Ledger Residual -> ShowS)
-> (Stake Ledger Residual -> VoterId)
-> ([Stake Ledger Residual] -> ShowS)
-> Show (Stake Ledger Residual)
forall a.
(Int -> a -> ShowS) -> (a -> VoterId) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Stake Ledger Residual -> ShowS
showsPrec :: Int -> Stake Ledger Residual -> ShowS
$cshow :: Stake Ledger Residual -> VoterId
show :: Stake Ledger Residual -> VoterId
$cshowList :: [Stake Ledger Residual] -> ShowS
showList :: [Stake Ledger Residual] -> ShowS
Show, (forall x. Stake Ledger Residual -> Rep (Stake Ledger Residual) x)
-> (forall x.
    Rep (Stake Ledger Residual) x -> Stake Ledger Residual)
-> Generic (Stake Ledger Residual)
forall x. Rep (Stake Ledger Residual) x -> Stake Ledger Residual
forall x. Stake Ledger Residual -> Rep (Stake Ledger Residual) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Stake Ledger Residual -> Rep (Stake Ledger Residual) x
from :: forall x. Stake Ledger Residual -> Rep (Stake Ledger Residual) x
$cto :: forall x. Rep (Stake Ledger Residual) x -> Stake Ledger Residual
to :: forall x. Rep (Stake Ledger Residual) x -> Stake Ledger Residual
Generic)
  deriving newtype Integer -> Stake Ledger Residual
Stake Ledger Residual -> Stake Ledger Residual
Stake Ledger Residual
-> Stake Ledger Residual -> Stake Ledger Residual
(Stake Ledger Residual
 -> Stake Ledger Residual -> Stake Ledger Residual)
-> (Stake Ledger Residual
    -> Stake Ledger Residual -> Stake Ledger Residual)
-> (Stake Ledger Residual
    -> Stake Ledger Residual -> Stake Ledger Residual)
-> (Stake Ledger Residual -> Stake Ledger Residual)
-> (Stake Ledger Residual -> Stake Ledger Residual)
-> (Stake Ledger Residual -> Stake Ledger Residual)
-> (Integer -> Stake Ledger Residual)
-> Num (Stake Ledger Residual)
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: Stake Ledger Residual
-> Stake Ledger Residual -> Stake Ledger Residual
+ :: Stake Ledger Residual
-> Stake Ledger Residual -> Stake Ledger Residual
$c- :: Stake Ledger Residual
-> Stake Ledger Residual -> Stake Ledger Residual
- :: Stake Ledger Residual
-> Stake Ledger Residual -> Stake Ledger Residual
$c* :: Stake Ledger Residual
-> Stake Ledger Residual -> Stake Ledger Residual
* :: Stake Ledger Residual
-> Stake Ledger Residual -> Stake Ledger Residual
$cnegate :: Stake Ledger Residual -> Stake Ledger Residual
negate :: Stake Ledger Residual -> Stake Ledger Residual
$cabs :: Stake Ledger Residual -> Stake Ledger Residual
abs :: Stake Ledger Residual -> Stake Ledger Residual
$csignum :: Stake Ledger Residual -> Stake Ledger Residual
signum :: Stake Ledger Residual -> Stake Ledger Residual
$cfromInteger :: Integer -> Stake Ledger Residual
fromInteger :: Integer -> Stake Ledger Residual
Num

newtype instance Stake Ledger Cumulative = StakeLedgerCumulative Rational
  deriving stock (Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool
(Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool)
-> (Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool)
-> Eq (Stake Ledger Cumulative)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool
== :: Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool
$c/= :: Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool
/= :: Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool
Eq, Eq (Stake Ledger Cumulative)
Eq (Stake Ledger Cumulative) =>
(Stake Ledger Cumulative -> Stake Ledger Cumulative -> Ordering)
-> (Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool)
-> (Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool)
-> (Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool)
-> (Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool)
-> (Stake Ledger Cumulative
    -> Stake Ledger Cumulative -> Stake Ledger Cumulative)
-> (Stake Ledger Cumulative
    -> Stake Ledger Cumulative -> Stake Ledger Cumulative)
-> Ord (Stake Ledger Cumulative)
Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool
Stake Ledger Cumulative -> Stake Ledger Cumulative -> Ordering
Stake Ledger Cumulative
-> Stake Ledger Cumulative -> Stake Ledger Cumulative
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
$ccompare :: Stake Ledger Cumulative -> Stake Ledger Cumulative -> Ordering
compare :: Stake Ledger Cumulative -> Stake Ledger Cumulative -> Ordering
$c< :: Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool
< :: Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool
$c<= :: Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool
<= :: Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool
$c> :: Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool
> :: Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool
$c>= :: Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool
>= :: Stake Ledger Cumulative -> Stake Ledger Cumulative -> Bool
$cmax :: Stake Ledger Cumulative
-> Stake Ledger Cumulative -> Stake Ledger Cumulative
max :: Stake Ledger Cumulative
-> Stake Ledger Cumulative -> Stake Ledger Cumulative
$cmin :: Stake Ledger Cumulative
-> Stake Ledger Cumulative -> Stake Ledger Cumulative
min :: Stake Ledger Cumulative
-> Stake Ledger Cumulative -> Stake Ledger Cumulative
Ord, Int -> Stake Ledger Cumulative -> ShowS
[Stake Ledger Cumulative] -> ShowS
Stake Ledger Cumulative -> VoterId
(Int -> Stake Ledger Cumulative -> ShowS)
-> (Stake Ledger Cumulative -> VoterId)
-> ([Stake Ledger Cumulative] -> ShowS)
-> Show (Stake Ledger Cumulative)
forall a.
(Int -> a -> ShowS) -> (a -> VoterId) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Stake Ledger Cumulative -> ShowS
showsPrec :: Int -> Stake Ledger Cumulative -> ShowS
$cshow :: Stake Ledger Cumulative -> VoterId
show :: Stake Ledger Cumulative -> VoterId
$cshowList :: [Stake Ledger Cumulative] -> ShowS
showList :: [Stake Ledger Cumulative] -> ShowS
Show, (forall x.
 Stake Ledger Cumulative -> Rep (Stake Ledger Cumulative) x)
-> (forall x.
    Rep (Stake Ledger Cumulative) x -> Stake Ledger Cumulative)
-> Generic (Stake Ledger Cumulative)
forall x.
Rep (Stake Ledger Cumulative) x -> Stake Ledger Cumulative
forall x.
Stake Ledger Cumulative -> Rep (Stake Ledger Cumulative) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x.
Stake Ledger Cumulative -> Rep (Stake Ledger Cumulative) x
from :: forall x.
Stake Ledger Cumulative -> Rep (Stake Ledger Cumulative) x
$cto :: forall x.
Rep (Stake Ledger Cumulative) x -> Stake Ledger Cumulative
to :: forall x.
Rep (Stake Ledger Cumulative) x -> Stake Ledger Cumulative
Generic)
  deriving newtype Integer -> Stake Ledger Cumulative
Stake Ledger Cumulative -> Stake Ledger Cumulative
Stake Ledger Cumulative
-> Stake Ledger Cumulative -> Stake Ledger Cumulative
(Stake Ledger Cumulative
 -> Stake Ledger Cumulative -> Stake Ledger Cumulative)
-> (Stake Ledger Cumulative
    -> Stake Ledger Cumulative -> Stake Ledger Cumulative)
-> (Stake Ledger Cumulative
    -> Stake Ledger Cumulative -> Stake Ledger Cumulative)
-> (Stake Ledger Cumulative -> Stake Ledger Cumulative)
-> (Stake Ledger Cumulative -> Stake Ledger Cumulative)
-> (Stake Ledger Cumulative -> Stake Ledger Cumulative)
-> (Integer -> Stake Ledger Cumulative)
-> Num (Stake Ledger Cumulative)
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: Stake Ledger Cumulative
-> Stake Ledger Cumulative -> Stake Ledger Cumulative
+ :: Stake Ledger Cumulative
-> Stake Ledger Cumulative -> Stake Ledger Cumulative
$c- :: Stake Ledger Cumulative
-> Stake Ledger Cumulative -> Stake Ledger Cumulative
- :: Stake Ledger Cumulative
-> Stake Ledger Cumulative -> Stake Ledger Cumulative
$c* :: Stake Ledger Cumulative
-> Stake Ledger Cumulative -> Stake Ledger Cumulative
* :: Stake Ledger Cumulative
-> Stake Ledger Cumulative -> Stake Ledger Cumulative
$cnegate :: Stake Ledger Cumulative -> Stake Ledger Cumulative
negate :: Stake Ledger Cumulative -> Stake Ledger Cumulative
$cabs :: Stake Ledger Cumulative -> Stake Ledger Cumulative
abs :: Stake Ledger Cumulative -> Stake Ledger Cumulative
$csignum :: Stake Ledger Cumulative -> Stake Ledger Cumulative
signum :: Stake Ledger Cumulative -> Stake Ledger Cumulative
$cfromInteger :: Integer -> Stake Ledger Cumulative
fromInteger :: Integer -> Stake Ledger Cumulative
Num

newtype instance Stake Weight Global = StakeWeightGlobal Rational
  deriving stock (Stake Weight Global -> Stake Weight Global -> Bool
(Stake Weight Global -> Stake Weight Global -> Bool)
-> (Stake Weight Global -> Stake Weight Global -> Bool)
-> Eq (Stake Weight Global)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Stake Weight Global -> Stake Weight Global -> Bool
== :: Stake Weight Global -> Stake Weight Global -> Bool
$c/= :: Stake Weight Global -> Stake Weight Global -> Bool
/= :: Stake Weight Global -> Stake Weight Global -> Bool
Eq, Eq (Stake Weight Global)
Eq (Stake Weight Global) =>
(Stake Weight Global -> Stake Weight Global -> Ordering)
-> (Stake Weight Global -> Stake Weight Global -> Bool)
-> (Stake Weight Global -> Stake Weight Global -> Bool)
-> (Stake Weight Global -> Stake Weight Global -> Bool)
-> (Stake Weight Global -> Stake Weight Global -> Bool)
-> (Stake Weight Global
    -> Stake Weight Global -> Stake Weight Global)
-> (Stake Weight Global
    -> Stake Weight Global -> Stake Weight Global)
-> Ord (Stake Weight Global)
Stake Weight Global -> Stake Weight Global -> Bool
Stake Weight Global -> Stake Weight Global -> Ordering
Stake Weight Global -> Stake Weight Global -> Stake Weight Global
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
$ccompare :: Stake Weight Global -> Stake Weight Global -> Ordering
compare :: Stake Weight Global -> Stake Weight Global -> Ordering
$c< :: Stake Weight Global -> Stake Weight Global -> Bool
< :: Stake Weight Global -> Stake Weight Global -> Bool
$c<= :: Stake Weight Global -> Stake Weight Global -> Bool
<= :: Stake Weight Global -> Stake Weight Global -> Bool
$c> :: Stake Weight Global -> Stake Weight Global -> Bool
> :: Stake Weight Global -> Stake Weight Global -> Bool
$c>= :: Stake Weight Global -> Stake Weight Global -> Bool
>= :: Stake Weight Global -> Stake Weight Global -> Bool
$cmax :: Stake Weight Global -> Stake Weight Global -> Stake Weight Global
max :: Stake Weight Global -> Stake Weight Global -> Stake Weight Global
$cmin :: Stake Weight Global -> Stake Weight Global -> Stake Weight Global
min :: Stake Weight Global -> Stake Weight Global -> Stake Weight Global
Ord, Int -> Stake Weight Global -> ShowS
[Stake Weight Global] -> ShowS
Stake Weight Global -> VoterId
(Int -> Stake Weight Global -> ShowS)
-> (Stake Weight Global -> VoterId)
-> ([Stake Weight Global] -> ShowS)
-> Show (Stake Weight Global)
forall a.
(Int -> a -> ShowS) -> (a -> VoterId) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Stake Weight Global -> ShowS
showsPrec :: Int -> Stake Weight Global -> ShowS
$cshow :: Stake Weight Global -> VoterId
show :: Stake Weight Global -> VoterId
$cshowList :: [Stake Weight Global] -> ShowS
showList :: [Stake Weight Global] -> ShowS
Show, (forall x. Stake Weight Global -> Rep (Stake Weight Global) x)
-> (forall x. Rep (Stake Weight Global) x -> Stake Weight Global)
-> Generic (Stake Weight Global)
forall x. Rep (Stake Weight Global) x -> Stake Weight Global
forall x. Stake Weight Global -> Rep (Stake Weight Global) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Stake Weight Global -> Rep (Stake Weight Global) x
from :: forall x. Stake Weight Global -> Rep (Stake Weight Global) x
$cto :: forall x. Rep (Stake Weight Global) x -> Stake Weight Global
to :: forall x. Rep (Stake Weight Global) x -> Stake Weight Global
Generic)
  deriving newtype Integer -> Stake Weight Global
Stake Weight Global -> Stake Weight Global
Stake Weight Global -> Stake Weight Global -> Stake Weight Global
(Stake Weight Global -> Stake Weight Global -> Stake Weight Global)
-> (Stake Weight Global
    -> Stake Weight Global -> Stake Weight Global)
-> (Stake Weight Global
    -> Stake Weight Global -> Stake Weight Global)
-> (Stake Weight Global -> Stake Weight Global)
-> (Stake Weight Global -> Stake Weight Global)
-> (Stake Weight Global -> Stake Weight Global)
-> (Integer -> Stake Weight Global)
-> Num (Stake Weight Global)
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: Stake Weight Global -> Stake Weight Global -> Stake Weight Global
+ :: Stake Weight Global -> Stake Weight Global -> Stake Weight Global
$c- :: Stake Weight Global -> Stake Weight Global -> Stake Weight Global
- :: Stake Weight Global -> Stake Weight Global -> Stake Weight Global
$c* :: Stake Weight Global -> Stake Weight Global -> Stake Weight Global
* :: Stake Weight Global -> Stake Weight Global -> Stake Weight Global
$cnegate :: Stake Weight Global -> Stake Weight Global
negate :: Stake Weight Global -> Stake Weight Global
$cabs :: Stake Weight Global -> Stake Weight Global
abs :: Stake Weight Global -> Stake Weight Global
$csignum :: Stake Weight Global -> Stake Weight Global
signum :: Stake Weight Global -> Stake Weight Global
$cfromInteger :: Integer -> Stake Weight Global
fromInteger :: Integer -> Stake Weight Global
Num

newtype instance Stake Weight Persistent = StakeWeightPersistent Rational
  deriving stock (Stake Weight Persistent -> Stake Weight Persistent -> Bool
(Stake Weight Persistent -> Stake Weight Persistent -> Bool)
-> (Stake Weight Persistent -> Stake Weight Persistent -> Bool)
-> Eq (Stake Weight Persistent)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Stake Weight Persistent -> Stake Weight Persistent -> Bool
== :: Stake Weight Persistent -> Stake Weight Persistent -> Bool
$c/= :: Stake Weight Persistent -> Stake Weight Persistent -> Bool
/= :: Stake Weight Persistent -> Stake Weight Persistent -> Bool
Eq, Eq (Stake Weight Persistent)
Eq (Stake Weight Persistent) =>
(Stake Weight Persistent -> Stake Weight Persistent -> Ordering)
-> (Stake Weight Persistent -> Stake Weight Persistent -> Bool)
-> (Stake Weight Persistent -> Stake Weight Persistent -> Bool)
-> (Stake Weight Persistent -> Stake Weight Persistent -> Bool)
-> (Stake Weight Persistent -> Stake Weight Persistent -> Bool)
-> (Stake Weight Persistent
    -> Stake Weight Persistent -> Stake Weight Persistent)
-> (Stake Weight Persistent
    -> Stake Weight Persistent -> Stake Weight Persistent)
-> Ord (Stake Weight Persistent)
Stake Weight Persistent -> Stake Weight Persistent -> Bool
Stake Weight Persistent -> Stake Weight Persistent -> Ordering
Stake Weight Persistent
-> Stake Weight Persistent -> Stake Weight Persistent
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
$ccompare :: Stake Weight Persistent -> Stake Weight Persistent -> Ordering
compare :: Stake Weight Persistent -> Stake Weight Persistent -> Ordering
$c< :: Stake Weight Persistent -> Stake Weight Persistent -> Bool
< :: Stake Weight Persistent -> Stake Weight Persistent -> Bool
$c<= :: Stake Weight Persistent -> Stake Weight Persistent -> Bool
<= :: Stake Weight Persistent -> Stake Weight Persistent -> Bool
$c> :: Stake Weight Persistent -> Stake Weight Persistent -> Bool
> :: Stake Weight Persistent -> Stake Weight Persistent -> Bool
$c>= :: Stake Weight Persistent -> Stake Weight Persistent -> Bool
>= :: Stake Weight Persistent -> Stake Weight Persistent -> Bool
$cmax :: Stake Weight Persistent
-> Stake Weight Persistent -> Stake Weight Persistent
max :: Stake Weight Persistent
-> Stake Weight Persistent -> Stake Weight Persistent
$cmin :: Stake Weight Persistent
-> Stake Weight Persistent -> Stake Weight Persistent
min :: Stake Weight Persistent
-> Stake Weight Persistent -> Stake Weight Persistent
Ord, Int -> Stake Weight Persistent -> ShowS
[Stake Weight Persistent] -> ShowS
Stake Weight Persistent -> VoterId
(Int -> Stake Weight Persistent -> ShowS)
-> (Stake Weight Persistent -> VoterId)
-> ([Stake Weight Persistent] -> ShowS)
-> Show (Stake Weight Persistent)
forall a.
(Int -> a -> ShowS) -> (a -> VoterId) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Stake Weight Persistent -> ShowS
showsPrec :: Int -> Stake Weight Persistent -> ShowS
$cshow :: Stake Weight Persistent -> VoterId
show :: Stake Weight Persistent -> VoterId
$cshowList :: [Stake Weight Persistent] -> ShowS
showList :: [Stake Weight Persistent] -> ShowS
Show, (forall x.
 Stake Weight Persistent -> Rep (Stake Weight Persistent) x)
-> (forall x.
    Rep (Stake Weight Persistent) x -> Stake Weight Persistent)
-> Generic (Stake Weight Persistent)
forall x.
Rep (Stake Weight Persistent) x -> Stake Weight Persistent
forall x.
Stake Weight Persistent -> Rep (Stake Weight Persistent) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x.
Stake Weight Persistent -> Rep (Stake Weight Persistent) x
from :: forall x.
Stake Weight Persistent -> Rep (Stake Weight Persistent) x
$cto :: forall x.
Rep (Stake Weight Persistent) x -> Stake Weight Persistent
to :: forall x.
Rep (Stake Weight Persistent) x -> Stake Weight Persistent
Generic)
  deriving newtype Integer -> Stake Weight Persistent
Stake Weight Persistent -> Stake Weight Persistent
Stake Weight Persistent
-> Stake Weight Persistent -> Stake Weight Persistent
(Stake Weight Persistent
 -> Stake Weight Persistent -> Stake Weight Persistent)
-> (Stake Weight Persistent
    -> Stake Weight Persistent -> Stake Weight Persistent)
-> (Stake Weight Persistent
    -> Stake Weight Persistent -> Stake Weight Persistent)
-> (Stake Weight Persistent -> Stake Weight Persistent)
-> (Stake Weight Persistent -> Stake Weight Persistent)
-> (Stake Weight Persistent -> Stake Weight Persistent)
-> (Integer -> Stake Weight Persistent)
-> Num (Stake Weight Persistent)
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: Stake Weight Persistent
-> Stake Weight Persistent -> Stake Weight Persistent
+ :: Stake Weight Persistent
-> Stake Weight Persistent -> Stake Weight Persistent
$c- :: Stake Weight Persistent
-> Stake Weight Persistent -> Stake Weight Persistent
- :: Stake Weight Persistent
-> Stake Weight Persistent -> Stake Weight Persistent
$c* :: Stake Weight Persistent
-> Stake Weight Persistent -> Stake Weight Persistent
* :: Stake Weight Persistent
-> Stake Weight Persistent -> Stake Weight Persistent
$cnegate :: Stake Weight Persistent -> Stake Weight Persistent
negate :: Stake Weight Persistent -> Stake Weight Persistent
$cabs :: Stake Weight Persistent -> Stake Weight Persistent
abs :: Stake Weight Persistent -> Stake Weight Persistent
$csignum :: Stake Weight Persistent -> Stake Weight Persistent
signum :: Stake Weight Persistent -> Stake Weight Persistent
$cfromInteger :: Integer -> Stake Weight Persistent
fromInteger :: Integer -> Stake Weight Persistent
Num

newtype instance Stake Weight Residual = StakeWeightResidual Rational
  deriving stock (Stake Weight Residual -> Stake Weight Residual -> Bool
(Stake Weight Residual -> Stake Weight Residual -> Bool)
-> (Stake Weight Residual -> Stake Weight Residual -> Bool)
-> Eq (Stake Weight Residual)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Stake Weight Residual -> Stake Weight Residual -> Bool
== :: Stake Weight Residual -> Stake Weight Residual -> Bool
$c/= :: Stake Weight Residual -> Stake Weight Residual -> Bool
/= :: Stake Weight Residual -> Stake Weight Residual -> Bool
Eq, Eq (Stake Weight Residual)
Eq (Stake Weight Residual) =>
(Stake Weight Residual -> Stake Weight Residual -> Ordering)
-> (Stake Weight Residual -> Stake Weight Residual -> Bool)
-> (Stake Weight Residual -> Stake Weight Residual -> Bool)
-> (Stake Weight Residual -> Stake Weight Residual -> Bool)
-> (Stake Weight Residual -> Stake Weight Residual -> Bool)
-> (Stake Weight Residual
    -> Stake Weight Residual -> Stake Weight Residual)
-> (Stake Weight Residual
    -> Stake Weight Residual -> Stake Weight Residual)
-> Ord (Stake Weight Residual)
Stake Weight Residual -> Stake Weight Residual -> Bool
Stake Weight Residual -> Stake Weight Residual -> Ordering
Stake Weight Residual
-> Stake Weight Residual -> Stake Weight Residual
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
$ccompare :: Stake Weight Residual -> Stake Weight Residual -> Ordering
compare :: Stake Weight Residual -> Stake Weight Residual -> Ordering
$c< :: Stake Weight Residual -> Stake Weight Residual -> Bool
< :: Stake Weight Residual -> Stake Weight Residual -> Bool
$c<= :: Stake Weight Residual -> Stake Weight Residual -> Bool
<= :: Stake Weight Residual -> Stake Weight Residual -> Bool
$c> :: Stake Weight Residual -> Stake Weight Residual -> Bool
> :: Stake Weight Residual -> Stake Weight Residual -> Bool
$c>= :: Stake Weight Residual -> Stake Weight Residual -> Bool
>= :: Stake Weight Residual -> Stake Weight Residual -> Bool
$cmax :: Stake Weight Residual
-> Stake Weight Residual -> Stake Weight Residual
max :: Stake Weight Residual
-> Stake Weight Residual -> Stake Weight Residual
$cmin :: Stake Weight Residual
-> Stake Weight Residual -> Stake Weight Residual
min :: Stake Weight Residual
-> Stake Weight Residual -> Stake Weight Residual
Ord, Int -> Stake Weight Residual -> ShowS
[Stake Weight Residual] -> ShowS
Stake Weight Residual -> VoterId
(Int -> Stake Weight Residual -> ShowS)
-> (Stake Weight Residual -> VoterId)
-> ([Stake Weight Residual] -> ShowS)
-> Show (Stake Weight Residual)
forall a.
(Int -> a -> ShowS) -> (a -> VoterId) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Stake Weight Residual -> ShowS
showsPrec :: Int -> Stake Weight Residual -> ShowS
$cshow :: Stake Weight Residual -> VoterId
show :: Stake Weight Residual -> VoterId
$cshowList :: [Stake Weight Residual] -> ShowS
showList :: [Stake Weight Residual] -> ShowS
Show, (forall x. Stake Weight Residual -> Rep (Stake Weight Residual) x)
-> (forall x.
    Rep (Stake Weight Residual) x -> Stake Weight Residual)
-> Generic (Stake Weight Residual)
forall x. Rep (Stake Weight Residual) x -> Stake Weight Residual
forall x. Stake Weight Residual -> Rep (Stake Weight Residual) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Stake Weight Residual -> Rep (Stake Weight Residual) x
from :: forall x. Stake Weight Residual -> Rep (Stake Weight Residual) x
$cto :: forall x. Rep (Stake Weight Residual) x -> Stake Weight Residual
to :: forall x. Rep (Stake Weight Residual) x -> Stake Weight Residual
Generic)
  deriving newtype Integer -> Stake Weight Residual
Stake Weight Residual -> Stake Weight Residual
Stake Weight Residual
-> Stake Weight Residual -> Stake Weight Residual
(Stake Weight Residual
 -> Stake Weight Residual -> Stake Weight Residual)
-> (Stake Weight Residual
    -> Stake Weight Residual -> Stake Weight Residual)
-> (Stake Weight Residual
    -> Stake Weight Residual -> Stake Weight Residual)
-> (Stake Weight Residual -> Stake Weight Residual)
-> (Stake Weight Residual -> Stake Weight Residual)
-> (Stake Weight Residual -> Stake Weight Residual)
-> (Integer -> Stake Weight Residual)
-> Num (Stake Weight Residual)
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: Stake Weight Residual
-> Stake Weight Residual -> Stake Weight Residual
+ :: Stake Weight Residual
-> Stake Weight Residual -> Stake Weight Residual
$c- :: Stake Weight Residual
-> Stake Weight Residual -> Stake Weight Residual
- :: Stake Weight Residual
-> Stake Weight Residual -> Stake Weight Residual
$c* :: Stake Weight Residual
-> Stake Weight Residual -> Stake Weight Residual
* :: Stake Weight Residual
-> Stake Weight Residual -> Stake Weight Residual
$cnegate :: Stake Weight Residual -> Stake Weight Residual
negate :: Stake Weight Residual -> Stake Weight Residual
$cabs :: Stake Weight Residual -> Stake Weight Residual
abs :: Stake Weight Residual -> Stake Weight Residual
$csignum :: Stake Weight Residual -> Stake Weight Residual
signum :: Stake Weight Residual -> Stake Weight Residual
$cfromInteger :: Integer -> Stake Weight Residual
fromInteger :: Integer -> Stake Weight Residual
Num

deriving via (RationalStake Ledger Global) instance IsStake (Stake Ledger Global)
deriving via (RationalStake Ledger Persistent) instance IsStake (Stake Ledger Persistent)
deriving via (RationalStake Ledger Residual) instance IsStake (Stake Ledger Residual)
deriving via (RationalStake Ledger Cumulative) instance IsStake (Stake Ledger Cumulative)
deriving via (RationalStake Weight Global) instance IsStake (Stake Weight Global)
deriving via (RationalStake Weight Persistent) instance IsStake (Stake Weight Persistent)
deriving via (RationalStake Weight Residual) instance IsStake (Stake Weight Residual)