Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate arbitrary constant for SomeConstant #4573

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Generate arbitrary constant for SomeConstant
Previously only integer constants were generated.
  • Loading branch information
zliu41 committed May 2, 2022
commit 90a57593e2a0114006e19fbd5343c21430ace777
4 changes: 2 additions & 2 deletions plutus-core/plutus-core/test/Evaluation/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Evaluation.Spec (test_evaluation) where
import PlutusCore hiding (Term)
import PlutusCore qualified as PLC
import PlutusCore.Builtin
import PlutusCore.Generators (forAllNoShow, genConstant)
import PlutusCore.Generators (forAllNoShow, genTypeable)
import PlutusCore.Generators.AST hiding (genConstant)
import PlutusCore.Pretty

Expand Down Expand Up @@ -90,7 +90,7 @@ prop_builtinsDon'tThrow bn = property $ do
TODO: currently it only generates constant terms.
-}
genArgsWellTyped :: DefaultFun -> Gen [Term]
genArgsWellTyped = genArgs (fmap mkTerm . genConstant)
genArgsWellTyped = genArgs (fmap mkTerm . genTypeable)
where
mkTerm :: forall (a :: GHC.Type). MakeKnown Term a => a -> Term
mkTerm a = case runEmitter . runExceptT $ makeKnown a of
Expand Down
119 changes: 69 additions & 50 deletions plutus-core/testlib/PlutusCore/Generators/Internal/Builtin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}

module PlutusCore.Generators.Internal.Builtin (
genTypeable,
genConstant,
genInteger,
genByteString,
genText,
genData,
genI,
genB,
genList,
genMap,
genConstr,
genDataI,
genDataB,
genDataList,
genDataMap,
genDataConstr,
matchTyCon,
) where

import PlutusCore
import PlutusCore.Builtin
import PlutusCore.Data (Data (..))
import PlutusCore.Generators.AST (genTerm, runAstGen)
import PlutusCore.Generators.AST qualified as AST

import Data.ByteString qualified as BS
import Data.Int (Int64)
Expand All @@ -32,35 +34,65 @@ import Hedgehog.Gen qualified as Gen
import Hedgehog.Range qualified as Range
import Type.Reflection

genConstant :: forall a. TypeRep a -> Gen a
genConstant tr
genTypeable :: forall a. TypeRep a -> Gen a
genTypeable tr
| Just HRefl <- eqTypeRep tr (typeRep @()) = pure ()
| Just HRefl <- eqTypeRep tr (typeRep @Integer) = genInteger
| Just HRefl <- eqTypeRep tr (typeRep @Int) = fromIntegral <$> genInteger
| Just HRefl <- eqTypeRep tr (typeRep @Bool) = Gen.bool
| Just HRefl <- eqTypeRep tr (typeRep @BS.ByteString) = genByteString
| Just HRefl <- eqTypeRep tr (typeRep @Text) = genText
| Just HRefl <- eqTypeRep tr (typeRep @Data) = genData 5
| Just HRefl <- eqTypeRep tr (typeRep @Data) = genData
| Just HRefl <- eqTypeRep tr (typeRep @(Term TyName Name DefaultUni DefaultFun ())) =
runAstGen genTerm
AST.runAstGen AST.genTerm
| trPair `App` tr1 `App` tr2 <- tr
, Just HRefl <- eqTypeRep trPair (typeRep @(,)) =
(,) <$> genConstant tr1 <*> genConstant tr2
(,) <$> genTypeable tr1 <*> genTypeable tr2
| trList `App` trElem <- tr
, Just HRefl <- eqTypeRep trList (typeRep @[]) =
Gen.list (Range.linear 0 10) $ genConstant trElem
Gen.list (Range.linear 0 10) $ genTypeable trElem
| trOpaque `App` trVal `App` _ <- tr
, Just HRefl <- eqTypeRep trOpaque (typeRep @Opaque) =
Opaque <$> genConstant trVal
Opaque <$> genTypeable trVal
| trSomeConstant `App` trUni `App` _ <- tr
, Just HRefl <- eqTypeRep trUni (typeRep @DefaultUni)
, Just HRefl <- eqTypeRep trSomeConstant (typeRep @SomeConstant) =
-- In the current implementation, all type variables are instantiated
-- to `Integer` (TODO: change this).
SomeConstant . someValue <$> genInteger
SomeConstant <$> genConstant
| otherwise =
error $
"genConstant: I don't know how to generate constant of this type: " <> show tr
"genTypeable: I don't know how to generate values of this type: " <> show tr

genConstant :: Gen (Some (ValueOf DefaultUni))
genConstant = AST.simpleRecursive nonRecursive recursive
where
nonRecursive =
[ pure $ someValue ()
, someValue <$> genInteger
, someValue <$> Gen.bool
, someValue <$> genByteString
, someValue <$> genText
]
recursive = [pairGen, listGen]
pairGen = do
Some (ValueOf uni1 val1) <- genConstant
Some (ValueOf uni2 val2) <- genConstant
pure $
someValueOf
(DefaultUniApply (DefaultUniApply DefaultUniProtoPair uni1) uni2)
(val1, val2)
listGen =
let genList ::
DefaultUni `Contains` a =>
Gen a ->
Gen (Some (ValueOf DefaultUni))
genList = fmap someValue . Gen.list (Range.linear 0 10)
in Gen.choice
[ genList genInteger
, genList Gen.bool
, genList genByteString
, genList genText
, genList genData
]

-- | If the given `TypeRep`'s `TyCon` is @con@, return its type arguments.
matchTyCon :: forall con a. (Typeable con) => TypeRep a -> Maybe [SomeTypeRep]
Expand All @@ -81,36 +113,23 @@ genByteString = Gen.utf8 (Range.linear 0 100) Gen.enumBounded
genText :: Gen Text
genText = Gen.text (Range.linear 0 100) Gen.enumBounded

genData :: Int -> Gen Data
genData depth =
Gen.choice $
[genI, genB]
<> [ genRec | depth > 1, genRec <-
[ genList (depth - 1)
, genMap (depth - 1)
, genConstr (depth - 1)
]
]

genI :: Gen Data
genI = I <$> genInteger

genB :: Gen Data
genB = B <$> genByteString

genList :: Int -> Gen Data
genList depth = List <$> Gen.list (Range.linear 0 5) (genData (depth - 1))

genMap :: Int -> Gen Data
genMap depth =
Map
<$> Gen.list
(Range.linear 0 5)
((,) <$> genData (depth - 1) <*> genData (depth - 1))

genConstr :: Int -> Gen Data
genConstr depth =
Constr <$> genInteger
<*> Gen.list
(Range.linear 0 5)
(genData (depth - 1))
genData :: Gen Data
genData = AST.simpleRecursive nonRecursive recursive
where
nonRecursive = [genDataI, genDataB]
recursive = [genDataList, genDataMap, genDataConstr]

genDataI :: Gen Data
genDataI = I <$> genInteger

genDataB :: Gen Data
genDataB = B <$> genByteString

genDataList :: Gen Data
genDataList = List <$> Gen.list (Range.linear 0 5) genData

genDataMap :: Gen Data
genDataMap = Map <$> Gen.list (Range.linear 0 5) ((,) <$> genData <*> genData)

genDataConstr :: Gen Data
genDataConstr = Constr <$> genInteger <*> Gen.list (Range.linear 0 5) genData