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

Monad for random generation of clash-core types.
-}

{-# LANGUAGE CPP #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE UndecidableInstances #-}

module Clash.Hedgehog.Core.Monad
  ( CoreGenT
  , runCoreGenT
  , CoreGenConfig(..)
  , defaultConfig
  , canGenDataKinds
  , canGenPolyKinds
  , canGenRankNTypes
  , canGenTypeFamilies
  , canGenUndecidableInstances

    -- * Re-exports
  , Alternative(..)
  , MonadGen(..)
  , MonadReader(..)
  ) where

import Control.Applicative (Alternative(..))
import Control.Monad.IO.Class (MonadIO)
import Control.Monad.Reader (MonadReader(..), ReaderT, runReaderT)
import Control.Monad.Trans (MonadTrans)
import Hedgehog (MonadGen(..))

-- | The CoreGenT monad keeps track of features like language extensions which
-- have an impact on what can be generated. This allows more meaningful random
-- generation, as the output of generators can be constrained to the same
-- variant of Haskell / Clash used by the caller.
--
newtype CoreGenT m a
  = CoreGenT (ReaderT CoreGenConfig m a)
  deriving newtype
    ( Applicative (CoreGenT m)
CoreGenT m a
Applicative (CoreGenT m)
-> (forall a. CoreGenT m a)
-> (forall a. CoreGenT m a -> CoreGenT m a -> CoreGenT m a)
-> (forall a. CoreGenT m a -> CoreGenT m [a])
-> (forall a. CoreGenT m a -> CoreGenT m [a])
-> Alternative (CoreGenT m)
CoreGenT m a -> CoreGenT m a -> CoreGenT m a
CoreGenT m a -> CoreGenT m [a]
CoreGenT m a -> CoreGenT m [a]
forall a. CoreGenT m a
forall a. CoreGenT m a -> CoreGenT m [a]
forall a. CoreGenT m a -> CoreGenT m a -> CoreGenT m a
forall (f :: Type -> Type).
Applicative f
-> (forall a. f a)
-> (forall a. f a -> f a -> f a)
-> (forall a. f a -> f [a])
-> (forall a. f a -> f [a])
-> Alternative f
forall (m :: Type -> Type).
Alternative m =>
Applicative (CoreGenT m)
forall (m :: Type -> Type) a. Alternative m => CoreGenT m a
forall (m :: Type -> Type) a.
Alternative m =>
CoreGenT m a -> CoreGenT m [a]
forall (m :: Type -> Type) a.
Alternative m =>
CoreGenT m a -> CoreGenT m a -> CoreGenT m a
many :: CoreGenT m a -> CoreGenT m [a]
$cmany :: forall (m :: Type -> Type) a.
Alternative m =>
CoreGenT m a -> CoreGenT m [a]
some :: CoreGenT m a -> CoreGenT m [a]
$csome :: forall (m :: Type -> Type) a.
Alternative m =>
CoreGenT m a -> CoreGenT m [a]
<|> :: CoreGenT m a -> CoreGenT m a -> CoreGenT m a
$c<|> :: forall (m :: Type -> Type) a.
Alternative m =>
CoreGenT m a -> CoreGenT m a -> CoreGenT m a
empty :: CoreGenT m a
$cempty :: forall (m :: Type -> Type) a. Alternative m => CoreGenT m a
$cp1Alternative :: forall (m :: Type -> Type).
Alternative m =>
Applicative (CoreGenT m)
Alternative
    , Functor (CoreGenT m)
a -> CoreGenT m a
Functor (CoreGenT m)
-> (forall a. a -> CoreGenT m a)
-> (forall a b.
    CoreGenT m (a -> b) -> CoreGenT m a -> CoreGenT m b)
-> (forall a b c.
    (a -> b -> c) -> CoreGenT m a -> CoreGenT m b -> CoreGenT m c)
-> (forall a b. CoreGenT m a -> CoreGenT m b -> CoreGenT m b)
-> (forall a b. CoreGenT m a -> CoreGenT m b -> CoreGenT m a)
-> Applicative (CoreGenT m)
CoreGenT m a -> CoreGenT m b -> CoreGenT m b
CoreGenT m a -> CoreGenT m b -> CoreGenT m a
CoreGenT m (a -> b) -> CoreGenT m a -> CoreGenT m b
(a -> b -> c) -> CoreGenT m a -> CoreGenT m b -> CoreGenT m c
forall a. a -> CoreGenT m a
forall a b. CoreGenT m a -> CoreGenT m b -> CoreGenT m a
forall a b. CoreGenT m a -> CoreGenT m b -> CoreGenT m b
forall a b. CoreGenT m (a -> b) -> CoreGenT m a -> CoreGenT m b
forall a b c.
(a -> b -> c) -> CoreGenT m a -> CoreGenT m b -> CoreGenT m c
forall (f :: Type -> Type).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
forall (m :: Type -> Type). Applicative m => Functor (CoreGenT m)
forall (m :: Type -> Type) a. Applicative m => a -> CoreGenT m a
forall (m :: Type -> Type) a b.
Applicative m =>
CoreGenT m a -> CoreGenT m b -> CoreGenT m a
forall (m :: Type -> Type) a b.
Applicative m =>
CoreGenT m a -> CoreGenT m b -> CoreGenT m b
forall (m :: Type -> Type) a b.
Applicative m =>
CoreGenT m (a -> b) -> CoreGenT m a -> CoreGenT m b
forall (m :: Type -> Type) a b c.
Applicative m =>
(a -> b -> c) -> CoreGenT m a -> CoreGenT m b -> CoreGenT m c
<* :: CoreGenT m a -> CoreGenT m b -> CoreGenT m a
$c<* :: forall (m :: Type -> Type) a b.
Applicative m =>
CoreGenT m a -> CoreGenT m b -> CoreGenT m a
*> :: CoreGenT m a -> CoreGenT m b -> CoreGenT m b
$c*> :: forall (m :: Type -> Type) a b.
Applicative m =>
CoreGenT m a -> CoreGenT m b -> CoreGenT m b
liftA2 :: (a -> b -> c) -> CoreGenT m a -> CoreGenT m b -> CoreGenT m c
$cliftA2 :: forall (m :: Type -> Type) a b c.
Applicative m =>
(a -> b -> c) -> CoreGenT m a -> CoreGenT m b -> CoreGenT m c
<*> :: CoreGenT m (a -> b) -> CoreGenT m a -> CoreGenT m b
$c<*> :: forall (m :: Type -> Type) a b.
Applicative m =>
CoreGenT m (a -> b) -> CoreGenT m a -> CoreGenT m b
pure :: a -> CoreGenT m a
$cpure :: forall (m :: Type -> Type) a. Applicative m => a -> CoreGenT m a
$cp1Applicative :: forall (m :: Type -> Type). Applicative m => Functor (CoreGenT m)
Applicative
    , a -> CoreGenT m b -> CoreGenT m a
(a -> b) -> CoreGenT m a -> CoreGenT m b
(forall a b. (a -> b) -> CoreGenT m a -> CoreGenT m b)
-> (forall a b. a -> CoreGenT m b -> CoreGenT m a)
-> Functor (CoreGenT m)
forall a b. a -> CoreGenT m b -> CoreGenT m a
forall a b. (a -> b) -> CoreGenT m a -> CoreGenT m b
forall (m :: Type -> Type) a b.
Functor m =>
a -> CoreGenT m b -> CoreGenT m a
forall (m :: Type -> Type) a b.
Functor m =>
(a -> b) -> CoreGenT m a -> CoreGenT m b
forall (f :: Type -> Type).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> CoreGenT m b -> CoreGenT m a
$c<$ :: forall (m :: Type -> Type) a b.
Functor m =>
a -> CoreGenT m b -> CoreGenT m a
fmap :: (a -> b) -> CoreGenT m a -> CoreGenT m b
$cfmap :: forall (m :: Type -> Type) a b.
Functor m =>
(a -> b) -> CoreGenT m a -> CoreGenT m b
Functor
    , Applicative (CoreGenT m)
a -> CoreGenT m a
Applicative (CoreGenT m)
-> (forall a b.
    CoreGenT m a -> (a -> CoreGenT m b) -> CoreGenT m b)
-> (forall a b. CoreGenT m a -> CoreGenT m b -> CoreGenT m b)
-> (forall a. a -> CoreGenT m a)
-> Monad (CoreGenT m)
CoreGenT m a -> (a -> CoreGenT m b) -> CoreGenT m b
CoreGenT m a -> CoreGenT m b -> CoreGenT m b
forall a. a -> CoreGenT m a
forall a b. CoreGenT m a -> CoreGenT m b -> CoreGenT m b
forall a b. CoreGenT m a -> (a -> CoreGenT m b) -> CoreGenT m b
forall (m :: Type -> Type). Monad m => Applicative (CoreGenT m)
forall (m :: Type -> Type) a. Monad m => a -> CoreGenT m a
forall (m :: Type -> Type) a b.
Monad m =>
CoreGenT m a -> CoreGenT m b -> CoreGenT m b
forall (m :: Type -> Type) a b.
Monad m =>
CoreGenT m a -> (a -> CoreGenT m b) -> CoreGenT m b
forall (m :: Type -> Type).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> CoreGenT m a
$creturn :: forall (m :: Type -> Type) a. Monad m => a -> CoreGenT m a
>> :: CoreGenT m a -> CoreGenT m b -> CoreGenT m b
$c>> :: forall (m :: Type -> Type) a b.
Monad m =>
CoreGenT m a -> CoreGenT m b -> CoreGenT m b
>>= :: CoreGenT m a -> (a -> CoreGenT m b) -> CoreGenT m b
$c>>= :: forall (m :: Type -> Type) a b.
Monad m =>
CoreGenT m a -> (a -> CoreGenT m b) -> CoreGenT m b
$cp1Monad :: forall (m :: Type -> Type). Monad m => Applicative (CoreGenT m)
Monad
    , Monad (CoreGenT m)
Monad (CoreGenT m)
-> (forall a. String -> CoreGenT m a) -> MonadFail (CoreGenT m)
String -> CoreGenT m a
forall a. String -> CoreGenT m a
forall (m :: Type -> Type).
Monad m -> (forall a. String -> m a) -> MonadFail m
forall (m :: Type -> Type). MonadFail m => Monad (CoreGenT m)
forall (m :: Type -> Type) a. MonadFail m => String -> CoreGenT m a
fail :: String -> CoreGenT m a
$cfail :: forall (m :: Type -> Type) a. MonadFail m => String -> CoreGenT m a
$cp1MonadFail :: forall (m :: Type -> Type). MonadFail m => Monad (CoreGenT m)
MonadFail
    , Monad (GenBase (CoreGenT m))
Monad (CoreGenT m)
Monad (CoreGenT m)
-> Monad (GenBase (CoreGenT m))
-> (forall a. CoreGenT m a -> GenT (GenBase (CoreGenT m)) a)
-> (forall a. GenT (GenBase (CoreGenT m)) a -> CoreGenT m a)
-> MonadGen (CoreGenT m)
GenT (GenBase (CoreGenT m)) a -> CoreGenT m a
CoreGenT m a -> GenT (GenBase (CoreGenT m)) a
forall a. GenT (GenBase (CoreGenT m)) a -> CoreGenT m a
forall a. CoreGenT m a -> GenT (GenBase (CoreGenT m)) a
forall (m :: Type -> Type).
Monad m
-> Monad (GenBase m)
-> (forall a. m a -> GenT (GenBase m) a)
-> (forall a. GenT (GenBase m) a -> m a)
-> MonadGen m
forall (m :: Type -> Type).
MonadGen m =>
Monad (GenBase (CoreGenT m))
forall (m :: Type -> Type). MonadGen m => Monad (CoreGenT m)
forall (m :: Type -> Type) a.
MonadGen m =>
GenT (GenBase (CoreGenT m)) a -> CoreGenT m a
forall (m :: Type -> Type) a.
MonadGen m =>
CoreGenT m a -> GenT (GenBase (CoreGenT m)) a
fromGenT :: GenT (GenBase (CoreGenT m)) a -> CoreGenT m a
$cfromGenT :: forall (m :: Type -> Type) a.
MonadGen m =>
GenT (GenBase (CoreGenT m)) a -> CoreGenT m a
toGenT :: CoreGenT m a -> GenT (GenBase (CoreGenT m)) a
$ctoGenT :: forall (m :: Type -> Type) a.
MonadGen m =>
CoreGenT m a -> GenT (GenBase (CoreGenT m)) a
$cp2MonadGen :: forall (m :: Type -> Type).
MonadGen m =>
Monad (GenBase (CoreGenT m))
$cp1MonadGen :: forall (m :: Type -> Type). MonadGen m => Monad (CoreGenT m)
MonadGen
    , Monad (CoreGenT m)
Monad (CoreGenT m)
-> (forall a. IO a -> CoreGenT m a) -> MonadIO (CoreGenT m)
IO a -> CoreGenT m a
forall a. IO a -> CoreGenT m a
forall (m :: Type -> Type).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
forall (m :: Type -> Type). MonadIO m => Monad (CoreGenT m)
forall (m :: Type -> Type) a. MonadIO m => IO a -> CoreGenT m a
liftIO :: IO a -> CoreGenT m a
$cliftIO :: forall (m :: Type -> Type) a. MonadIO m => IO a -> CoreGenT m a
$cp1MonadIO :: forall (m :: Type -> Type). MonadIO m => Monad (CoreGenT m)
MonadIO
    , MonadReader CoreGenConfig
    , m a -> CoreGenT m a
(forall (m :: Type -> Type) a. Monad m => m a -> CoreGenT m a)
-> MonadTrans CoreGenT
forall (m :: Type -> Type) a. Monad m => m a -> CoreGenT m a
forall (t :: (Type -> Type) -> Type -> Type).
(forall (m :: Type -> Type) a. Monad m => m a -> t m a)
-> MonadTrans t
lift :: m a -> CoreGenT m a
$clift :: forall (m :: Type -> Type) a. Monad m => m a -> CoreGenT m a
MonadTrans
    )

-- | Run a generator that generates types from @clash-lib@. This is intended
-- to transform another monad which implements 'MonadGen'.
--
runCoreGenT :: CoreGenT m a -> CoreGenConfig -> m a
runCoreGenT :: CoreGenT m a -> CoreGenConfig -> m a
runCoreGenT (CoreGenT ReaderT CoreGenConfig m a
act) = ReaderT CoreGenConfig m a -> CoreGenConfig -> m a
forall r (m :: Type -> Type) a. ReaderT r m a -> r -> m a
runReaderT ReaderT CoreGenConfig m a
act

-- | The configuration of Haskell / Clash which the generated source adheres
-- to. These are typically things which change what a user could potentially
-- have written in a source file, such as language extensions.
--
data CoreGenConfig = CoreGenConfig
  { CoreGenConfig -> Bool
allowDataKinds :: Bool
  , CoreGenConfig -> Bool
allowPolyKinds :: Bool
  , CoreGenConfig -> Bool
allowRankNTypes :: Bool
  , CoreGenConfig -> Bool
allowTypeFamilies :: Bool
  , CoreGenConfig -> Bool
allowUndecidableInstances :: Bool
  } deriving stock (Int -> CoreGenConfig -> ShowS
[CoreGenConfig] -> ShowS
CoreGenConfig -> String
(Int -> CoreGenConfig -> ShowS)
-> (CoreGenConfig -> String)
-> ([CoreGenConfig] -> ShowS)
-> Show CoreGenConfig
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CoreGenConfig] -> ShowS
$cshowList :: [CoreGenConfig] -> ShowS
show :: CoreGenConfig -> String
$cshow :: CoreGenConfig -> String
showsPrec :: Int -> CoreGenConfig -> ShowS
$cshowsPrec :: Int -> CoreGenConfig -> ShowS
Show)

-- | The default configuration matches the set of language extensions which
-- are enabled by default when running @clash@ / @clashi@. For most projects,
-- this will likely be the most representative set of options.
--
defaultConfig :: CoreGenConfig
defaultConfig :: CoreGenConfig
defaultConfig = CoreGenConfig :: Bool -> Bool -> Bool -> Bool -> Bool -> CoreGenConfig
CoreGenConfig
  { allowDataKinds :: Bool
allowDataKinds = Bool
True
  , allowPolyKinds :: Bool
allowPolyKinds = Bool
False
  , allowRankNTypes :: Bool
allowRankNTypes = Bool
False
  , allowTypeFamilies :: Bool
allowTypeFamilies = Bool
True
  , allowUndecidableInstances :: Bool
allowUndecidableInstances = Bool
False
  }

canGenDataKinds :: forall m. Monad m => CoreGenT m Bool
canGenDataKinds :: CoreGenT m Bool
canGenDataKinds = (CoreGenConfig -> Bool) -> CoreGenT m Bool
forall r (m :: Type -> Type) a. MonadReader r m => (r -> a) -> m a
reader CoreGenConfig -> Bool
allowDataKinds

canGenPolyKinds :: forall m. Monad m => CoreGenT m Bool
canGenPolyKinds :: CoreGenT m Bool
canGenPolyKinds = (CoreGenConfig -> Bool) -> CoreGenT m Bool
forall r (m :: Type -> Type) a. MonadReader r m => (r -> a) -> m a
reader CoreGenConfig -> Bool
allowPolyKinds

canGenRankNTypes :: forall m. Monad m => CoreGenT m Bool
canGenRankNTypes :: CoreGenT m Bool
canGenRankNTypes = (CoreGenConfig -> Bool) -> CoreGenT m Bool
forall r (m :: Type -> Type) a. MonadReader r m => (r -> a) -> m a
reader CoreGenConfig -> Bool
allowRankNTypes

canGenTypeFamilies :: forall m. Monad m => CoreGenT m Bool
canGenTypeFamilies :: CoreGenT m Bool
canGenTypeFamilies = (CoreGenConfig -> Bool) -> CoreGenT m Bool
forall r (m :: Type -> Type) a. MonadReader r m => (r -> a) -> m a
reader CoreGenConfig -> Bool
allowTypeFamilies

canGenUndecidableInstances :: forall m. Monad m => CoreGenT m Bool
canGenUndecidableInstances :: CoreGenT m Bool
canGenUndecidableInstances = (CoreGenConfig -> Bool) -> CoreGenT m Bool
forall r (m :: Type -> Type) a. MonadReader r m => (r -> a) -> m a
reader CoreGenConfig -> Bool
allowUndecidableInstances