Skip to content

Commit 2ee717f

Browse files
committed
Add Uniform instance for Ptr, SlotNo32, CertIx and TxIx
1 parent 13a7342 commit 2ee717f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

libs/cardano-ledger-core/src/Cardano/Ledger/BaseTypes.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ import Data.Aeson.Types (Pair)
160160
import qualified Data.Binary.Put as B
161161
import qualified Data.ByteString as BS
162162
import qualified Data.ByteString.Lazy as BSL
163+
import Data.Coerce (coerce)
163164
import Data.Default (Default (def))
164165
import qualified Data.Fixed as FP (Fixed, HasResolution, resolution)
165166
import Data.Functor.Identity (Identity)
@@ -186,6 +187,7 @@ import GHC.Stack (HasCallStack)
186187
import NoThunks.Class (NoThunks (..))
187188
import Numeric.Natural (Natural)
188189
import Quiet (Quiet (Quiet))
190+
import System.Random.Stateful (Random, Uniform (..), UniformRange (..))
189191

190192
maxDecimalsWord64 :: Int
191193
maxDecimalsWord64 = 19
@@ -832,6 +834,14 @@ newtype TxIx = TxIx {unTxIx :: Word16}
832834
deriving newtype
833835
(NFData, Enum, Bounded, NoThunks, FromCBOR, ToCBOR, EncCBOR, DecCBOR, ToJSON, MemPack)
834836

837+
instance Random TxIx
838+
839+
instance Uniform TxIx where
840+
uniformM g = TxIx <$> uniformM g
841+
842+
instance UniformRange TxIx where
843+
uniformRM r g = TxIx <$> uniformRM (coerce r) g
844+
835845
-- | Construct a `TxIx` from a 16 bit unsigned integer
836846
mkTxIx :: Word16 -> TxIx
837847
mkTxIx = TxIx . fromIntegral
@@ -857,6 +867,14 @@ newtype CertIx = CertIx {unCertIx :: Word16}
857867
deriving stock (Eq, Ord, Show)
858868
deriving newtype (NFData, Enum, Bounded, NoThunks, EncCBOR, DecCBOR, ToCBOR, FromCBOR, ToJSON)
859869

870+
instance Random CertIx
871+
872+
instance Uniform CertIx where
873+
uniformM g = CertIx <$> uniformM g
874+
875+
instance UniformRange CertIx where
876+
uniformRM r g = CertIx <$> uniformRM (coerce r) g
877+
860878
-- | Construct a `CertIx` from a 16 bit unsigned integer
861879
mkCertIx :: Word16 -> CertIx
862880
mkCertIx = CertIx . fromIntegral

libs/cardano-ledger-core/src/Cardano/Ledger/Credential.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import Data.Aeson (
6969
)
7070
import qualified Data.Aeson as Aeson
7171
import Data.Aeson.Types (toJSONKeyText)
72+
import Data.Coerce (coerce)
7273
import Data.Default (Default (..))
7374
import Data.Foldable (asum)
7475
import Data.Maybe (fromMaybe)
@@ -78,6 +79,7 @@ import Data.Typeable (Typeable)
7879
import Data.Word
7980
import GHC.Generics (Generic)
8081
import NoThunks.Class (NoThunks (..))
82+
import System.Random.Stateful (Uniform (..), UniformRange (..), Random)
8183

8284
-- | Script hash or key hash for a payment or a staking object.
8385
--
@@ -198,13 +200,24 @@ newtype SlotNo32 = SlotNo32 Word32
198200
deriving newtype
199201
(Eq, Ord, Num, Bounded, NFData, NoThunks, EncCBOR, DecCBOR, FromCBOR, ToCBOR, FromJSON, ToJSON)
200202

203+
instance Random SlotNo32
204+
205+
instance Uniform SlotNo32 where
206+
uniformM g = SlotNo32 <$> uniformM g
207+
208+
instance UniformRange SlotNo32 where
209+
uniformRM r g = SlotNo32 <$> uniformRM (coerce r) g
210+
201211
-- | Pointer to a slot number, transaction index and an index in certificate
202212
-- list.
203213
data Ptr = Ptr {-# UNPACK #-} !SlotNo32 {-# UNPACK #-} !TxIx {-# UNPACK #-} !CertIx
204214
deriving (Eq, Ord, Generic)
205215
deriving (EncCBOR, DecCBOR) via CBORGroup Ptr
206216
deriving (ToJSON) via KeyValuePairs Ptr
207217

218+
instance Uniform Ptr where
219+
uniformM g = Ptr <$> uniformM g <*> (TxIx <$> uniformM g) <*> (CertIx <$> uniformM g)
220+
208221
instance NFData Ptr
209222

210223
instance NoThunks Ptr

0 commit comments

Comments
 (0)