11
11
{-# LANGUAGE InstanceSigs #-}
12
12
{-# LANGUAGE LambdaCase #-}
13
13
{-# LANGUAGE MultiParamTypeClasses #-}
14
+ {-# LANGUAGE PartialTypeSignatures #-}
14
15
{-# LANGUAGE RecordWildCards #-}
15
16
{-# LANGUAGE ScopedTypeVariables #-}
16
17
{-# LANGUAGE StandaloneDeriving #-}
@@ -71,6 +72,7 @@ import Cardano.Chain.Common (
71
72
NetworkMagic (.. ),
72
73
UnparsedFields (.. ),
73
74
)
75
+ import Cardano.Crypto.Hash hiding (Blake2b_224 )
74
76
import Cardano.Crypto.Hashing (AbstractHash , abstractHashFromBytes )
75
77
import Cardano.Ledger.Address
76
78
import Cardano.Ledger.Allegra.Scripts
@@ -125,16 +127,11 @@ import Constrained.API
125
127
import Constrained.Base
126
128
import Constrained.GenT (pureGen , vectorOfT )
127
129
import Constrained.Generic
128
- import Constrained.List (List (.. ))
129
- import Constrained.Spec.ListFoldy (genListWithSize )
130
+ import Constrained.NumSpec
130
131
import Constrained.Spec.Map
131
- import Constrained.Spec.Size qualified as C
132
132
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
138
135
import Control.DeepSeq (NFData )
139
136
import Crypto.Hash (Blake2b_224 )
140
137
import Data.ByteString qualified as BS
@@ -166,6 +163,8 @@ import GHC.Generics (Generic)
166
163
import PlutusLedgerApi.V1 qualified as PV1
167
164
import Test.Cardano.Ledger.Allegra.Arbitrary ()
168
165
import Test.Cardano.Ledger.Alonzo.Arbitrary ()
166
+ import Test.Cardano.Ledger.Constrained.Conway.Instances.Basic
167
+ import Test.Cardano.Ledger.Constrained.Conway.Instances.PParams ()
169
168
import Test.Cardano.Ledger.Conway.Arbitrary ()
170
169
import Test.Cardano.Ledger.Core.Utils
171
170
import Test.Cardano.Ledger.Shelley.Utils
@@ -730,35 +729,34 @@ instance StringLike ShortByteString where
730
729
getLengthSpec (StringSpec len) = len
731
730
getLength = SBS. length
732
731
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
735
734
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 )
738
737
739
- strLen_ ::
740
- (StringLike s , HasSpec s ) =>
741
- Term s ->
742
- Term Int
738
+ strLen_ :: (HasSpec s , StringLike s ) => Term s -> Term Int
743
739
strLen_ = appTerm StrLenW
744
740
745
741
instance Syntax StringW
746
742
747
743
instance Semantics StringW where
748
744
semantics StrLenW = getLength
749
745
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)
760
757
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
762
760
763
761
class StringLike s where
764
762
lengthSpec :: Specification Int -> TypeSpec s
@@ -1714,28 +1712,23 @@ instance CoercibleLike (CompactForm Coin) Word64 where
1714
1712
Specification Word64
1715
1713
getCoerceSpec (NumSpecInterval a b) = TypeSpec (NumSpecInterval a b) mempty
1716
1714
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
1719
1717
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 )
1722
1720
1723
1721
instance Syntax CoercibleW
1724
1722
instance Semantics CoercibleW where
1725
1723
semantics = \ case
1726
1724
CoerceW -> coerce
1727
1725
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
1738
1729
1730
+ mapTypeSpec ::
1731
+ forall a b . (HasSpec a , HasSpec b ) => CoercibleW '[a ] b -> TypeSpec a -> Specification b
1739
1732
mapTypeSpec CoerceW ss = getCoerceSpec @ a ss
1740
1733
1741
1734
coerce_ ::
@@ -1750,11 +1743,11 @@ coerce_ = appTerm CoerceW
1750
1743
1751
1744
-- ==============================================================
1752
1745
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
1755
1748
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 )
1758
1751
1759
1752
instance Syntax CoinW
1760
1753
@@ -1767,20 +1760,13 @@ toDelta_ ::
1767
1760
Term DeltaCoin
1768
1761
toDelta_ = appTerm ToDeltaW
1769
1762
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)
1784
1770
1785
1771
mapTypeSpec ToDeltaW (NumSpecInterval l h) = typeSpec (NumSpecInterval (fromIntegral <$> l) (fromIntegral <$> h))
1786
1772
0 commit comments