{-|
Copyright   : (C) 2021, QBayLogic B.V.
License     : BSD2 (see the file LICENSE)
Maintainer  : QBayLogic B.V. <devops@qbaylogic.com>

Random generation of Unsigned numbers.
-}

{-# OPTIONS_GHC -fplugin=GHC.TypeLits.KnownNat.Solver #-}

{-# LANGUAGE GADTs #-}

module Clash.Hedgehog.Sized.Unsigned
  ( genUnsigned
  , SomeUnsigned(..)
  , genSomeUnsigned
  ) where

import GHC.Natural (Natural)
import GHC.TypeNats
import Hedgehog (MonadGen, Range)
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range

import Clash.Promoted.Nat
import Clash.Sized.Internal.Unsigned

genUnsigned :: (MonadGen m, KnownNat n) => Range (Unsigned n) -> m (Unsigned n)
genUnsigned :: Range (Unsigned n) -> m (Unsigned n)
genUnsigned Range (Unsigned n)
range =
  [(Int, m (Unsigned n))] -> m (Unsigned n)
forall (m :: Type -> Type) a. MonadGen m => [(Int, m a)] -> m a
Gen.frequency
    [ (Int
60, Range (Unsigned n) -> m (Unsigned n)
forall (m :: Type -> Type) a.
(MonadGen m, Integral a) =>
Range a -> m a
Gen.integral Range (Unsigned n)
range)
    , (Int
20, Unsigned n -> m (Unsigned n)
forall (m :: Type -> Type) a. MonadGen m => a -> m a
Gen.constant Unsigned n
forall a. Bounded a => a
minBound)
    , (Int
20, Unsigned n -> m (Unsigned n)
forall (m :: Type -> Type) a. MonadGen m => a -> m a
Gen.constant Unsigned n
forall a. Bounded a => a
maxBound)
    ]

data SomeUnsigned atLeast where
  SomeUnsigned :: SNat n -> Unsigned (atLeast + n) -> SomeUnsigned atLeast

genSomeUnsigned
  :: (MonadGen m, KnownNat atLeast)
  => Range Natural
  -> m (SomeUnsigned atLeast)
genSomeUnsigned :: Range Natural -> m (SomeUnsigned atLeast)
genSomeUnsigned Range Natural
rangeUnsigned = do
  Natural
numExtra <- Range Natural -> m Natural
forall (m :: Type -> Type) a.
(MonadGen m, Integral a) =>
Range a -> m a
Gen.integral Range Natural
rangeUnsigned

  case Natural -> SomeNat
someNatVal Natural
numExtra of
    SomeNat Proxy n
proxy -> SNat n -> Unsigned (atLeast + n) -> SomeUnsigned atLeast
forall (n :: Nat) (atLeast :: Nat).
SNat n -> Unsigned (atLeast + n) -> SomeUnsigned atLeast
SomeUnsigned (Proxy n -> SNat n
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> SNat n
snatProxy Proxy n
proxy) (Unsigned (atLeast + n) -> SomeUnsigned atLeast)
-> m (Unsigned (atLeast + n)) -> m (SomeUnsigned atLeast)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Range (Unsigned (atLeast + n)) -> m (Unsigned (atLeast + n))
forall (m :: Type -> Type) (n :: Nat).
(MonadGen m, KnownNat n) =>
Range (Unsigned n) -> m (Unsigned n)
genUnsigned Range (Unsigned (atLeast + n))
forall a. (Bounded a, Integral a) => Range a
Range.linearBounded