Skip to content

Commit 855f74e

Browse files
Futher refactor ConstrainedGenerators. Update contexts and get rid of symbol
1 parent c1772ef commit 855f74e

File tree

28 files changed

+3090
-3784
lines changed

28 files changed

+3090
-3784
lines changed

libs/cardano-ledger-test/src/Test/Cardano/Ledger/Constrained/Conway/Instances/Basic.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import Constrained.API
5454
import Constrained.Base
5555
import Constrained.GenT
5656
import Constrained.NumSpec
57-
import Constrained.Spec.ListFoldy (genListWithSize)
57+
import Constrained.SumList (genListWithSize)
5858
import Constrained.TheKnot
5959
import Control.Monad.Identity (Identity (..))
6060
import Control.Monad.Trans.Fail.String

libs/cardano-ledger-test/src/Test/Cardano/Ledger/Constrained/Conway/Instances/Ledger.hs

Lines changed: 45 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
{-# LANGUAGE InstanceSigs #-}
1212
{-# LANGUAGE LambdaCase #-}
1313
{-# LANGUAGE MultiParamTypeClasses #-}
14+
{-# LANGUAGE PartialTypeSignatures #-}
1415
{-# LANGUAGE RecordWildCards #-}
1516
{-# LANGUAGE ScopedTypeVariables #-}
1617
{-# LANGUAGE StandaloneDeriving #-}
@@ -71,6 +72,7 @@ import Cardano.Chain.Common (
7172
NetworkMagic (..),
7273
UnparsedFields (..),
7374
)
75+
import Cardano.Crypto.Hash hiding (Blake2b_224)
7476
import Cardano.Crypto.Hashing (AbstractHash, abstractHashFromBytes)
7577
import Cardano.Ledger.Address
7678
import Cardano.Ledger.Allegra.Scripts
@@ -125,16 +127,11 @@ import Constrained.API
125127
import Constrained.Base
126128
import Constrained.GenT (pureGen, vectorOfT)
127129
import Constrained.Generic
128-
import Constrained.List (List (..))
129-
import Constrained.Spec.ListFoldy (genListWithSize)
130+
import Constrained.NumSpec
130131
import Constrained.Spec.Map
131-
import Constrained.Spec.Size qualified as C
132132
import Constrained.Spec.Tree ()
133-
import GHC.TypeLits hiding (Text)
134-
import Test.Cardano.Ledger.Constrained.Conway.Instances.Basic
135-
import Test.Cardano.Ledger.Constrained.Conway.Instances.PParams ()
136-
137-
import Cardano.Crypto.Hash hiding (Blake2b_224)
133+
import Constrained.SumList (genListWithSize)
134+
import Constrained.TheKnot qualified as C
138135
import Control.DeepSeq (NFData)
139136
import Crypto.Hash (Blake2b_224)
140137
import Data.ByteString qualified as BS
@@ -166,6 +163,8 @@ import GHC.Generics (Generic)
166163
import PlutusLedgerApi.V1 qualified as PV1
167164
import Test.Cardano.Ledger.Allegra.Arbitrary ()
168165
import Test.Cardano.Ledger.Alonzo.Arbitrary ()
166+
import Test.Cardano.Ledger.Constrained.Conway.Instances.Basic
167+
import Test.Cardano.Ledger.Constrained.Conway.Instances.PParams ()
169168
import Test.Cardano.Ledger.Conway.Arbitrary ()
170169
import Test.Cardano.Ledger.Core.Utils
171170
import Test.Cardano.Ledger.Shelley.Utils
@@ -730,35 +729,34 @@ instance StringLike ShortByteString where
730729
getLengthSpec (StringSpec len) = len
731730
getLength = SBS.length
732731

733-
data StringW (sym :: Symbol) (as :: [Type]) (b :: Type) where
734-
StrLenW :: StringLike s => StringW "strLen_" '[s] Int
732+
data StringW :: [Type] -> Type -> Type where
733+
StrLenW :: StringLike s => StringW '[s] Int
735734

736-
deriving instance Show (StringW s as b)
737-
deriving instance Eq (StringW s as b)
735+
deriving instance Show (StringW as b)
736+
deriving instance Eq (StringW as b)
738737

739-
strLen_ ::
740-
(StringLike s, HasSpec s) =>
741-
Term s ->
742-
Term Int
738+
strLen_ :: (HasSpec s, StringLike s) => Term s -> Term Int
743739
strLen_ = appTerm StrLenW
744740

745741
instance Syntax StringW
746742

747743
instance Semantics StringW where
748744
semantics StrLenW = getLength
749745

750-
instance (Typeable s, StringLike s) => Logic "strLen_" StringW '[s] Int where
751-
propagate ctxt (ExplainSpec [] s) = propagate ctxt s
752-
propagate ctxt (ExplainSpec es s) = ExplainSpec es $ propagate ctxt s
753-
propagate _ TrueSpec = TrueSpec
754-
propagate _ (ErrorSpec msgs) = ErrorSpec msgs
755-
propagate (Context StrLenW (HOLE :<> End)) (SuspendedSpec v ps) =
756-
constrained $ \v' -> Let (App StrLenW (v' :> Nil)) (v :-> ps)
757-
propagate (Context StrLenW (HOLE :<> End)) spec = typeSpec $ lengthSpec @s spec
758-
propagate ctx _ =
759-
ErrorSpec $ pure ("Logic instance for StrLenW with wrong number of arguments. " ++ show ctx)
746+
-- | In this instance there is no way to bring the type variable `s` into scope
747+
-- so we introduce some local functions that have a signature that bring it into scope.
748+
instance Logic StringW where
749+
propagateTypeSpec StrLenW (Unary HOLE) ts cant = foo ts cant
750+
where
751+
foo :: forall s. (HasSpec s, StringLike s) => NumSpec Int -> [Int] -> Specification s
752+
foo t c = typeSpec $ lengthSpec @s (TypeSpec t c)
753+
propagateMemberSpec StrLenW (Unary HOLE) xs = bar xs
754+
where
755+
bar :: forall s. (HasSpec s, StringLike s) => NonEmpty Int -> Specification s
756+
bar ys = typeSpec $ lengthSpec @s (MemberSpec ys)
760757

761-
mapTypeSpec StrLenW ss = getLengthSpec @s ss
758+
mapTypeSpec :: forall a b. (HasSpec a, HasSpec b) => StringW '[a] b -> TypeSpec a -> Specification b
759+
mapTypeSpec StrLenW ss = getLengthSpec @a ss
762760

763761
class StringLike s where
764762
lengthSpec :: Specification Int -> TypeSpec s
@@ -1714,28 +1712,23 @@ instance CoercibleLike (CompactForm Coin) Word64 where
17141712
Specification Word64
17151713
getCoerceSpec (NumSpecInterval a b) = TypeSpec (NumSpecInterval a b) mempty
17161714

1717-
data CoercibleW (s :: Symbol) (args :: [Type]) (res :: Type) where
1718-
CoerceW :: (CoercibleLike a b, Coercible a b) => CoercibleW "coerce_" '[a] b
1715+
data CoercibleW (args :: [Type]) (res :: Type) where
1716+
CoerceW :: (CoercibleLike a b, Coercible a b) => CoercibleW '[a] b
17191717

1720-
deriving instance Show (CoercibleW sym args res)
1721-
deriving instance Eq (CoercibleW sym args res)
1718+
deriving instance Show (CoercibleW args res)
1719+
deriving instance Eq (CoercibleW args res)
17221720

17231721
instance Syntax CoercibleW
17241722
instance Semantics CoercibleW where
17251723
semantics = \case
17261724
CoerceW -> coerce
17271725

1728-
instance (Typeable a, Typeable b, CoercibleLike a b) => Logic "coerce_" CoercibleW '[a] b where
1729-
propagate ctxt (ExplainSpec [] s) = propagate ctxt s
1730-
propagate ctxt (ExplainSpec es s) = ExplainSpec es $ propagate ctxt s
1731-
propagate _ TrueSpec = TrueSpec
1732-
propagate _ (ErrorSpec msgs) = ErrorSpec msgs
1733-
propagate (Context CoerceW (HOLE :<> End)) (SuspendedSpec v ps) =
1734-
constrained $ \v' -> Let (App CoerceW (v' :> Nil)) (v :-> ps)
1735-
propagate (Context CoerceW (HOLE :<> End)) spec = coerceSpec @a @b spec
1736-
propagate ctx _ =
1737-
ErrorSpec $ pure ("Logic instance for CoerceW with wrong number of arguments. " ++ show ctx)
1726+
instance Logic CoercibleW where
1727+
propagateMemberSpec CoerceW (Unary HOLE) xs = coerceSpec $ MemberSpec xs
1728+
propagateTypeSpec CoerceW (Unary HOLE) ts cant = coerceSpec $ TypeSpec ts cant
17381729

1730+
mapTypeSpec ::
1731+
forall a b. (HasSpec a, HasSpec b) => CoercibleW '[a] b -> TypeSpec a -> Specification b
17391732
mapTypeSpec CoerceW ss = getCoerceSpec @a ss
17401733

17411734
coerce_ ::
@@ -1750,11 +1743,11 @@ coerce_ = appTerm CoerceW
17501743

17511744
-- ==============================================================
17521745

1753-
data CoinW (s :: Symbol) (ds :: [Type]) (res :: Type) where
1754-
ToDeltaW :: CoinW "toDelta_" '[Coin] DeltaCoin
1746+
data CoinW (ds :: [Type]) (res :: Type) where
1747+
ToDeltaW :: CoinW '[Coin] DeltaCoin
17551748

1756-
deriving instance Show (CoinW s args res)
1757-
deriving instance Eq (CoinW s args res)
1749+
deriving instance Show (CoinW args res)
1750+
deriving instance Eq (CoinW args res)
17581751

17591752
instance Syntax CoinW
17601753

@@ -1767,20 +1760,13 @@ toDelta_ ::
17671760
Term DeltaCoin
17681761
toDelta_ = appTerm ToDeltaW
17691762

1770-
instance Logic "toDelta_" CoinW '[Coin] DeltaCoin where
1771-
propagate ctxt (ExplainSpec es s) = ExplainSpec es $ propagate ctxt s
1772-
propagate _ TrueSpec = TrueSpec
1773-
propagate _ (ErrorSpec msgs) = ErrorSpec msgs
1774-
propagate (Context ToDeltaW (HOLE :<> End)) (SuspendedSpec v ps) =
1775-
constrained $ \v' -> Let (App ToDeltaW (v' :> Nil)) (v :-> ps)
1776-
propagate (Context ToDeltaW (HOLE :<> End)) (MemberSpec xs) = MemberSpec (NE.map deltaToCoin xs)
1777-
propagate (Context ToDeltaW (HOLE :<> End)) (TypeSpec (NumSpecInterval l h) cant) =
1778-
( TypeSpec
1779-
(NumSpecInterval (fromIntegral <$> l) (fromIntegral <$> h))
1780-
(map deltaToCoin cant)
1781-
)
1782-
propagate ctx _ =
1783-
ErrorSpec $ pure ("Logic instance for ToDeltaW with wrong number of arguments. " ++ show ctx)
1763+
instance Logic CoinW where
1764+
propagateMemberSpec ToDeltaW (Unary HOLE) xs = MemberSpec (NE.map deltaToCoin xs)
1765+
1766+
propagateTypeSpec ToDeltaW (Unary HOLE) (NumSpecInterval l h) cant =
1767+
TypeSpec
1768+
(NumSpecInterval (fromIntegral <$> l) (fromIntegral <$> h))
1769+
(map deltaToCoin cant)
17841770

17851771
mapTypeSpec ToDeltaW (NumSpecInterval l h) = typeSpec (NumSpecInterval (fromIntegral <$> l) (fromIntegral <$> h))
17861772

libs/cardano-ledger-test/src/Test/Cardano/Ledger/Constrained/Conway/WitnessUniverse.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import Cardano.Ledger.Shelley.TxCert
7171
import Constrained.API
7272
import Constrained.Base (toPred)
7373
import Constrained.GenT (pureGen)
74-
import Constrained.Spec.Size (hasSize, rangeSize)
74+
import Constrained.TheKnot (hasSize, rangeSize)
7575
import Control.DeepSeq (NFData (..), deepseq)
7676
import Control.Monad (replicateM)
7777
import Data.ByteString (ByteString)

libs/cardano-ledger-test/src/Test/Cardano/Ledger/Constrained/TypeRep.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ import Formatting (formatToString)
121121
import Lens.Micro
122122
import Numeric.Natural (Natural)
123123
import Prettyprinter (hsep)
124+
import Test.Cardano.Ledger.Allegra.Arbitrary ()
124125
import Test.Cardano.Ledger.Alonzo.Arbitrary (genAlonzoPlutusPurposePointer)
125126
import Test.Cardano.Ledger.Binary.Arbitrary (genByteString)
126127
import Test.Cardano.Ledger.Constrained.Classes (

libs/constrained-generators/constrained-generators.cabal

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,8 @@ library
3838
Constrained.List
3939
Constrained.NumSpec
4040
Constrained.Properties
41-
Constrained.Spec.ListFoldy
4241
Constrained.Spec.Map
43-
Constrained.Spec.Num
4442
Constrained.Spec.Set
45-
Constrained.Spec.Size
4643
Constrained.Spec.SumProd
4744
Constrained.Spec.Tree
4845
Constrained.SumList

0 commit comments

Comments
 (0)