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

Random generation of Index.
-}

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

{-# LANGUAGE GADTs #-}

module Clash.Hedgehog.Sized.Index
  ( genIndex
  , SomeIndex(..)
  , genSomeIndex
  ) 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.Index

genIndex :: (MonadGen m, KnownNat n) => Range (Index n) -> m (Index n)
genIndex :: Range (Index n) -> m (Index n)
genIndex Range (Index n)
range =
  [(Int, m (Index n))] -> m (Index n)
forall (m :: Type -> Type) a. MonadGen m => [(Int, m a)] -> m a
Gen.frequency
    [ (Int
70, Range (Index n) -> m (Index n)
forall (m :: Type -> Type) a.
(MonadGen m, Integral a) =>
Range a -> m a
Gen.integral Range (Index n)
range)
    , (Int
30, Index n -> m (Index n)
forall (m :: Type -> Type) a. MonadGen m => a -> m a
Gen.constant (Size -> Range (Index n) -> Index n
forall a. Ord a => Size -> Range a -> a
Range.upperBound Size
99 Range (Index n)
range))
    ]

data SomeIndex atLeast where
  SomeIndex :: SNat n -> Index (atLeast + n) -> SomeIndex atLeast

instance KnownNat atLeast => Show (SomeIndex atLeast) where
  show :: SomeIndex atLeast -> String
show (SomeIndex SNat n
SNat Index (atLeast + n)
ix) = Index (atLeast + n) -> String
forall a. Show a => a -> String
show Index (atLeast + n)
ix

genSomeIndex
  :: (MonadGen m, KnownNat atLeast)
  => Range Natural
  -> m (SomeIndex atLeast)
genSomeIndex :: Range Natural -> m (SomeIndex atLeast)
genSomeIndex Range Natural
rangeIx = do
  Natural
numExtra <- Range Natural -> m Natural
forall (m :: Type -> Type) a.
(MonadGen m, Integral a) =>
Range a -> m a
Gen.integral Range Natural
rangeIx

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