{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeApplications #-}

-- | Share Hedgehog inputs and terms between assertions and conformance generation.
module Evaluation.Builtins.Bitwise.Conformance
  ( Check (..)
  , checkSame
  , checkConstant
  , checkProperty
  , genByteString
  , genByteStringThat
  , generateBitwisePropertyFiles
  ) where

import Evaluation.Builtins.Common
  ( PlcTerm
  , TypeErrorOrCekResult (..)
  , cekSuccessTrue
  , evalTerm
  )
import Evaluation.Builtins.Conformance
  ( Check (..)
  , CheckMode
  , Generated
  , OutputFormat
  , PropertyId
  , caseTerms
  , generateTermFiles
  , isCekSuccess
  , propertySeed
  , samplingSize
  )
import Evaluation.Helpers (evaluateTheSame)
import PlutusCore qualified as PLC
import PlutusCore.MkPlc (mkConstant)

import Data.ByteString (ByteString)
import Data.List (nub)
import Data.Maybe (mapMaybe)
import Hedgehog
  ( Gen
  , Property
  , annotate
  , annotateShow
  , assert
  , failure
  , forAll
  , property
  , success
  , (===)
  )
import Hedgehog.Gen qualified as Gen
import Hedgehog.Internal.Gen (evalGen)
import Hedgehog.Internal.Seed qualified as Seed
import Hedgehog.Internal.Tree (treeValue)
import Hedgehog.Range qualified as Range
import Test.QuickCheck qualified as QC
import Test.QuickCheck.Gen (unGen)
import Test.QuickCheck.Random (QCGen)

checkSame :: PlcTerm -> PlcTerm -> Gen Check
checkSame :: PlcTerm -> PlcTerm -> Gen Check
checkSame PlcTerm
lhs PlcTerm
rhs = Check -> Gen Check
forall a. a -> GenT Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Check -> Gen Check) -> Check -> Gen Check
forall a b. (a -> b) -> a -> b
$ PlcTerm -> PlcTerm -> Check
CheckEq PlcTerm
lhs PlcTerm
rhs

{-| Compare a term's result with a constant. The constant is only used by the
normal tests; the generated tests export the term alone. -}
checkConstant :: forall a. PLC.Contains PLC.DefaultUni a => a -> PlcTerm -> Gen Check
checkConstant :: forall a. Contains DefaultUni a => a -> PlcTerm -> Gen Check
checkConstant a
expected PlcTerm
term =
  Check -> Gen Check
forall a. a -> GenT Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Check -> Gen Check) -> Check -> Gen Check
forall a b. (a -> b) -> a -> b
$ PlcTerm -> TypeErrorOrCekResult -> Check
CheckEvaluatesTo PlcTerm
term (UplcTerm -> TypeErrorOrCekResult
CekSuccess (() -> a -> UplcTerm
forall a (uni :: * -> *) fun (term :: * -> *) tyname name ann.
(TermLike term tyname name uni fun, HasTermLevel uni a) =>
ann -> a -> term ann
mkConstant () a
expected))

-- | Keep Hedgehog's input shrinking and normal assertion reporting.
checkProperty :: Gen Check -> Property
checkProperty :: Gen Check -> Property
checkProperty Gen Check
gen = HasCallStack => PropertyT IO () -> Property
PropertyT IO () -> Property
property (PropertyT IO () -> Property) -> PropertyT IO () -> Property
forall a b. (a -> b) -> a -> b
$ Gen Check -> PropertyT IO Check
forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen Check
gen PropertyT IO Check -> (Check -> PropertyT IO ()) -> PropertyT IO ()
forall a b.
PropertyT IO a -> (a -> PropertyT IO b) -> PropertyT IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Check -> PropertyT IO ()
check
  where
    check :: Check -> PropertyT IO ()
check (CheckEq PlcTerm
lhs PlcTerm
rhs) = HasCallStack => PlcTerm -> PlcTerm -> PropertyT IO ()
PlcTerm -> PlcTerm -> PropertyT IO ()
evaluateTheSame PlcTerm
lhs PlcTerm
rhs
    check (CheckEvaluatesTo PlcTerm
term TypeErrorOrCekResult
expected) = PlcTerm -> TypeErrorOrCekResult
evalTerm PlcTerm
term TypeErrorOrCekResult -> TypeErrorOrCekResult -> PropertyT IO ()
forall (m :: * -> *) a.
(MonadTest m, Eq a, Show a, HasCallStack) =>
a -> a -> m ()
=== TypeErrorOrCekResult
expected
    check (CheckTrue PlcTerm
term) = PlcTerm -> TypeErrorOrCekResult
evalTerm PlcTerm
term TypeErrorOrCekResult -> TypeErrorOrCekResult -> PropertyT IO ()
forall (m :: * -> *) a.
(MonadTest m, Eq a, Show a, HasCallStack) =>
a -> a -> m ()
=== TypeErrorOrCekResult
cekSuccessTrue
    check (CheckOk PlcTerm
term) = case PlcTerm -> TypeErrorOrCekResult
evalTerm PlcTerm
term of
      CekSuccess UplcTerm
_ -> PropertyT IO ()
forall (m :: * -> *). MonadTest m => m ()
success
      TypeErrorOrCekResult
result -> TypeErrorOrCekResult -> PropertyT IO ()
forall (m :: * -> *) a.
(MonadTest m, Show a, HasCallStack) =>
a -> m ()
annotateShow TypeErrorOrCekResult
result PropertyT IO () -> PropertyT IO () -> PropertyT IO ()
forall a b. PropertyT IO a -> PropertyT IO b -> PropertyT IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PropertyT IO ()
forall (m :: * -> *) a. (MonadTest m, HasCallStack) => m a
failure
    check (CheckFails PlcTerm
term) = PlcTerm -> TypeErrorOrCekResult
evalTerm PlcTerm
term TypeErrorOrCekResult -> TypeErrorOrCekResult -> PropertyT IO ()
forall (m :: * -> *) a.
(MonadTest m, Eq a, Show a, HasCallStack) =>
a -> a -> m ()
=== TypeErrorOrCekResult
CekError
    check (CheckBoth Check
lhs Check
rhs) = Check -> PropertyT IO ()
check Check
lhs PropertyT IO () -> PropertyT IO () -> PropertyT IO ()
forall a b. PropertyT IO a -> PropertyT IO b -> PropertyT IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Check -> PropertyT IO ()
check Check
rhs
    check (CheckImplies Bool
condition Check
inner) = if Bool
condition then Check -> PropertyT IO ()
check Check
inner else PropertyT IO ()
forall (m :: * -> *). MonadTest m => m ()
success
    check (CheckDistinct [PlcTerm]
terms) =
      let results :: [TypeErrorOrCekResult]
results = (PlcTerm -> TypeErrorOrCekResult)
-> [PlcTerm] -> [TypeErrorOrCekResult]
forall a b. (a -> b) -> [a] -> [b]
map PlcTerm -> TypeErrorOrCekResult
evalTerm [PlcTerm]
terms
       in Bool -> PropertyT IO ()
forall (m :: * -> *). (MonadTest m, HasCallStack) => Bool -> m ()
assert ((TypeErrorOrCekResult -> Bool) -> [TypeErrorOrCekResult] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all TypeErrorOrCekResult -> Bool
isCekSuccess [TypeErrorOrCekResult]
results Bool -> Bool -> Bool
&& [TypeErrorOrCekResult] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TypeErrorOrCekResult]
results Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [TypeErrorOrCekResult] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([TypeErrorOrCekResult] -> [TypeErrorOrCekResult]
forall a. Eq a => [a] -> [a]
nub [TypeErrorOrCekResult]
results))
    check (CheckSatisfies String
label PlcTerm
term TypeErrorOrCekResult -> Bool
predicate) =
      String -> PropertyT IO ()
forall (m :: * -> *). (MonadTest m, HasCallStack) => String -> m ()
annotate String
label PropertyT IO () -> PropertyT IO () -> PropertyT IO ()
forall a b. PropertyT IO a -> PropertyT IO b -> PropertyT IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> PropertyT IO ()
forall (m :: * -> *). (MonadTest m, HasCallStack) => Bool -> m ()
assert (TypeErrorOrCekResult -> Bool
predicate (PlcTerm -> TypeErrorOrCekResult
evalTerm PlcTerm
term))

genByteString :: Int -> Int -> Gen ByteString
genByteString :: Int -> Int -> Gen ByteString
genByteString Int
lo Int
hi = Range Int -> Gen ByteString
forall (m :: * -> *). MonadGen m => Range Int -> m ByteString
Gen.bytes (Range Int -> Gen ByteString) -> Range Int -> Gen ByteString
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Range Int
forall a. Integral a => a -> a -> Range a
Range.linear Int
lo Int
hi

genByteStringThat :: (ByteString -> Bool) -> Int -> Int -> Gen ByteString
genByteStringThat :: (ByteString -> Bool) -> Int -> Int -> Gen ByteString
genByteStringThat ByteString -> Bool
predicate Int
lo Int
hi = (ByteString -> Bool) -> Gen ByteString -> Gen ByteString
forall (m :: * -> *) a.
(MonadGen m, GenBase m ~ Identity) =>
(a -> Bool) -> m a -> m a
Gen.filter ByteString -> Bool
predicate (Gen ByteString -> Gen ByteString)
-> Gen ByteString -> Gen ByteString
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Gen ByteString
genByteString Int
lo Int
hi

{-| Sample Hedgehog generators directly, with seeds derived from the executable's
QuickCheck seed. No property runner or assertions are involved. Discarded
inputs are retried, and the lazy stream stops at the requested case count.
Equality cases retain both terms, numbered together with -A/-B suffixes. -}
generateBitwisePropertyFiles
  :: OutputFormat
  -> CheckMode
  -> FilePath
  -> Int
  -> QCGen
  -> PropertyId
  -> String
  -> Gen Check
  -> IO Generated
generateBitwisePropertyFiles :: OutputFormat
-> CheckMode
-> String
-> Int
-> QCGen
-> PropertyId
-> String
-> Gen Check
-> IO Generated
generateBitwisePropertyFiles OutputFormat
format CheckMode
mode String
outputDir Int
numFiles QCGen
seed PropertyId
propertyId String
description Gen Check
gen =
  OutputFormat
-> String
-> Int
-> PropertyId
-> String
-> [TermCase]
-> IO Generated
generateTermFiles OutputFormat
format String
outputDir Int
numFiles PropertyId
propertyId String
description ([TermCase] -> IO Generated) -> [TermCase] -> IO Generated
forall a b. (a -> b) -> a -> b
$
    (Check -> [TermCase]) -> [Check] -> [TermCase]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (CheckMode -> Check -> [TermCase]
caseTerms CheckMode
mode) [Check]
cases
  where
    seeds :: [Word64]
seeds = Gen [Word64] -> QCGen -> Int -> [Word64]
forall a. Gen a -> QCGen -> Int -> a
unGen (Gen Word64 -> Gen [Word64]
forall a. Gen a -> Gen [a]
QC.infiniteListOf Gen Word64
forall a. Arbitrary a => Gen a
QC.arbitrary) (QCGen -> PropertyId -> QCGen
propertySeed QCGen
seed PropertyId
propertyId) Int
samplingSize
    cases :: [Check]
cases = (Word64 -> Maybe Check) -> [Word64] -> [Check]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (\Word64
s -> Tree Check -> Check
forall a. Tree a -> a
treeValue (Tree Check -> Check) -> Maybe (Tree Check) -> Maybe Check
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Size -> Seed -> Gen Check -> Maybe (Tree Check)
forall a. Size -> Seed -> Gen a -> Maybe (Tree a)
evalGen (Int -> Size
Range.Size Int
samplingSize) (Word64 -> Seed
Seed.from Word64
s) Gen Check
gen) [Word64]
seeds