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 )
7476import Cardano.Crypto.Hashing (AbstractHash , abstractHashFromBytes )
7577import Cardano.Ledger.Address
7678import Cardano.Ledger.Allegra.Scripts
@@ -125,16 +127,11 @@ import Constrained.API
125127import Constrained.Base
126128import Constrained.GenT (pureGen , vectorOfT )
127129import Constrained.Generic
128- import Constrained.List (List (.. ))
129- import Constrained.Spec.ListFoldy (genListWithSize )
130+ import Constrained.NumSpec
130131import Constrained.Spec.Map
131- import Constrained.Spec.Size qualified as C
132132import 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
138135import Control.DeepSeq (NFData )
139136import Crypto.Hash (Blake2b_224 )
140137import Data.ByteString qualified as BS
@@ -166,6 +163,8 @@ import GHC.Generics (Generic)
166163import PlutusLedgerApi.V1 qualified as PV1
167164import Test.Cardano.Ledger.Allegra.Arbitrary ()
168165import Test.Cardano.Ledger.Alonzo.Arbitrary ()
166+ import Test.Cardano.Ledger.Constrained.Conway.Instances.Basic
167+ import Test.Cardano.Ledger.Constrained.Conway.Instances.PParams ()
169168import Test.Cardano.Ledger.Conway.Arbitrary ()
170169import Test.Cardano.Ledger.Core.Utils
171170import 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
743739strLen_ = appTerm StrLenW
744740
745741instance Syntax StringW
746742
747743instance 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
763761class 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
17231721instance Syntax CoercibleW
17241722instance 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
17411734coerce_ ::
@@ -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
17591752instance Syntax CoinW
17601753
@@ -1767,20 +1760,13 @@ toDelta_ ::
17671760 Term DeltaCoin
17681761toDelta_ = 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
0 commit comments