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

Add PlutusTx Map backed by Data #5927

Merged
merged 44 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
c03986f
Add AssocList backed by Data
ana-pantilie Apr 29, 2024
0fcaac8
Fix build
ana-pantilie Apr 29, 2024
360474d
Fix AssocList union
ana-pantilie Apr 29, 2024
a730d6b
Add unionWith property test
ana-pantilie Apr 29, 2024
0d75e65
Clean-up
ana-pantilie Apr 29, 2024
ec6bccd
Fix performance bug
ana-pantilie Apr 29, 2024
634bea6
Add golden files for new tests
ana-pantilie Apr 29, 2024
857f418
Add documentation to AssocList
ana-pantilie Apr 29, 2024
8e620e5
Add docs to tests
ana-pantilie Apr 29, 2024
721d82d
Add data encoding test
ana-pantilie Apr 29, 2024
0f1d945
Address some review comments
ana-pantilie Apr 30, 2024
7dbd045
Rename AssocList to AssocMap
ana-pantilie May 7, 2024
0b51d91
Make Map newtype over BuiltinList Pair
ana-pantilie May 7, 2024
75edbf9
Fix union implementation
ana-pantilie May 8, 2024
cd2d843
Use BuiltinList internal functions
ana-pantilie May 8, 2024
4071a8a
Create internal top-level delete
ana-pantilie May 8, 2024
7d96dc4
Add union test
ana-pantilie May 8, 2024
2862dec
Add docs to integration tests
ana-pantilie May 8, 2024
1f7f634
Try naive type families
effectfully May 9, 2024
94ff0ff
Split 'Has*' into 'From*' and 'To*' again
effectfully May 10, 2024
1f0d147
Remove type families from '*Opaque'
effectfully May 10, 2024
8a02b66
Add 'ToBuiltin'
effectfully May 11, 2024
669e25f
Add 'TestInstances'
effectfully May 11, 2024
bb18d98
Make it work for GHC-8.10
effectfully May 11, 2024
0ec5c85
Polishing
effectfully May 12, 2024
173ee90
Improve docs
effectfully May 13, 2024
6e329d6
Polishing
effectfully May 13, 2024
ee05a90
Merge remote-tracking branch 'origin/master' into ana/data-assoclist
ana-pantilie May 13, 2024
29f0b1a
Address comments
effectfully May 14, 2024
f98841e
Fix compilation errors in AssocMap
ana-pantilie May 14, 2024
38270be
Add utils from bench package to plutus-tx-plugin tests
ana-pantilie May 14, 2024
a033dc0
Run first PlutusTx property test
ana-pantilie May 14, 2024
bc462b7
WIP: add makeLift to new Map type
ana-pantilie May 14, 2024
105d51c
Merge remote-tracking branch 'origin/effectfully/builtins/split-FromB…
ana-pantilie May 14, 2024
b2e34e4
Add first fully working plutus tx property test
ana-pantilie May 14, 2024
2ae4bf1
Merge remote-tracking branch 'origin/master' into ana/data-assoclist
ana-pantilie May 16, 2024
6f77819
Fix issue with insert propety test
ana-pantilie May 16, 2024
c57c9b7
Run all tests with PlutusTx
ana-pantilie May 16, 2024
678992d
Add changelog
ana-pantilie May 16, 2024
8cda32b
Fix test module warning
ana-pantilie May 21, 2024
393162c
Fix isData instance for These
ana-pantilie May 21, 2024
361cba3
Fix delete implementation
ana-pantilie May 21, 2024
72334a5
Fix redundancy
ana-pantilie May 21, 2024
c5c85ed
Address other review comments
ana-pantilie May 21, 2024
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
Prev Previous commit
Next Next commit
Try naive type families
  • Loading branch information
effectfully committed May 9, 2024
commit 1f7f6340f32061e71ad6106ad05cb91d4466f196
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(\xxx -> 1) ((\s -> s s) (\s -> force trace "hello" (\z -> s s z)))
2 changes: 2 additions & 0 deletions plutus-tx/plutus-tx.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ library
PlutusTx.Builtins
PlutusTx.Builtins.Class
PlutusTx.Builtins.Internal
PlutusTx.Builtins.IsBuiltin
PlutusTx.Builtins.IsOpaque
PlutusTx.Code
PlutusTx.Coverage
PlutusTx.Either
Expand Down
74 changes: 39 additions & 35 deletions plutus-tx/src/PlutusTx/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ module PlutusTx.Builtins (
, bls12_381_mulMlResult
, bls12_381_finalVerify
-- * Conversions
, fromOpaque
, toOpaque
, fromBuiltin
, toBuiltin
, integerToByteString
Expand All @@ -111,11 +113,12 @@ module PlutusTx.Builtins (
import Data.Maybe
import PlutusTx.Base (const, uncurry)
import PlutusTx.Bool (Bool (..))
import PlutusTx.Builtins.Class
import PlutusTx.Builtins.Internal (BuiltinBLS12_381_G1_Element (..),
BuiltinBLS12_381_G2_Element (..), BuiltinBLS12_381_MlResult (..),
BuiltinByteString (..), BuiltinData, BuiltinString)
import PlutusTx.Builtins.Internal qualified as BI
import PlutusTx.Builtins.IsBuiltin
import PlutusTx.Builtins.IsOpaque
import PlutusTx.Integer (Integer)

import GHC.ByteOrder (ByteOrder (BigEndian, LittleEndian))
Expand All @@ -128,12 +131,12 @@ appendByteString = BI.appendByteString
{-# INLINABLE consByteString #-}
-- | Adds a byte to the front of a 'ByteString'.
consByteString :: Integer -> BuiltinByteString -> BuiltinByteString
consByteString n bs = BI.consByteString (toBuiltin n) bs
consByteString n bs = BI.consByteString (toOpaque n) bs

{-# INLINABLE sliceByteString #-}
-- | Returns the substring of a 'ByteString' from index 'start' of length 'n'.
sliceByteString :: Integer -> Integer -> BuiltinByteString -> BuiltinByteString
sliceByteString start n bs = BI.sliceByteString (toBuiltin start) (toBuiltin n) bs
sliceByteString start n bs = BI.sliceByteString (toOpaque start) (toOpaque n) bs

{-# INLINABLE lengthOfByteString #-}
-- | Returns the length of a 'ByteString'.
Expand All @@ -143,7 +146,7 @@ lengthOfByteString = BI.lengthOfByteString
{-# INLINABLE indexByteString #-}
-- | Returns the byte of a 'ByteString' at index.
indexByteString :: BuiltinByteString -> Integer -> Integer
indexByteString b n = BI.indexByteString b (toBuiltin n)
indexByteString b n = BI.indexByteString b (toOpaque n)

{-# INLINABLE emptyByteString #-}
-- | An empty 'ByteString'.
Expand Down Expand Up @@ -184,22 +187,23 @@ verifyEd25519Signature
-> BuiltinByteString -- ^ Message (arbirtary length)
-> BuiltinByteString -- ^ Signature (64 bytes)
-> Bool
verifyEd25519Signature pubKey message signature = fromBuiltin (BI.verifyEd25519Signature pubKey message signature)
verifyEd25519Signature pubKey message signature =
fromOpaque (BI.verifyEd25519Signature pubKey message signature)

{-# INLINABLE equalsByteString #-}
-- | Check if two 'ByteString's are equal.
equalsByteString :: BuiltinByteString -> BuiltinByteString -> Bool
equalsByteString x y = fromBuiltin (BI.equalsByteString x y)
equalsByteString x y = fromOpaque (BI.equalsByteString x y)

{-# INLINABLE lessThanByteString #-}
-- | Check if one 'ByteString' is less than another.
lessThanByteString :: BuiltinByteString -> BuiltinByteString -> Bool
lessThanByteString x y = fromBuiltin (BI.lessThanByteString x y)
lessThanByteString x y = fromOpaque (BI.lessThanByteString x y)

{-# INLINABLE lessThanEqualsByteString #-}
-- | Check if one 'ByteString' is less than or equal to another.
lessThanEqualsByteString :: BuiltinByteString -> BuiltinByteString -> Bool
lessThanEqualsByteString x y = fromBuiltin (BI.lessThanEqualsByteString x y)
lessThanEqualsByteString x y = fromOpaque (BI.lessThanEqualsByteString x y)

{-# INLINABLE greaterThanByteString #-}
-- | Check if one 'ByteString' is greater than another.
Expand Down Expand Up @@ -257,7 +261,7 @@ verifyEcdsaSecp256k1Signature
-> BuiltinByteString -- ^ Signature (64 bytes)
-> Bool
verifyEcdsaSecp256k1Signature vk msg sig =
fromBuiltin (BI.verifyEcdsaSecp256k1Signature vk msg sig)
fromOpaque (BI.verifyEcdsaSecp256k1Signature vk msg sig)

{-# INLINEABLE verifySchnorrSecp256k1Signature #-}
-- | Given a Schnorr SECP256k1 verification key, a Schnorr SECP256k1 signature,
Expand Down Expand Up @@ -291,42 +295,42 @@ verifySchnorrSecp256k1Signature
-> BuiltinByteString -- ^ Signature (64 bytes)
-> Bool
verifySchnorrSecp256k1Signature vk msg sig =
fromBuiltin (BI.verifySchnorrSecp256k1Signature vk msg sig)
fromOpaque (BI.verifySchnorrSecp256k1Signature vk msg sig)

{-# INLINABLE addInteger #-}
-- | Add two 'Integer's.
addInteger :: Integer -> Integer -> Integer
addInteger x y = fromBuiltin (BI.addInteger (toBuiltin x) (toBuiltin y))
addInteger x y = fromOpaque (BI.addInteger (toOpaque x) (toOpaque y))

{-# INLINABLE subtractInteger #-}
-- | Subtract two 'Integer's.
subtractInteger :: Integer -> Integer -> Integer
subtractInteger x y = fromBuiltin (BI.subtractInteger (toBuiltin x) (toBuiltin y))
subtractInteger x y = fromOpaque (BI.subtractInteger (toOpaque x) (toOpaque y))

{-# INLINABLE multiplyInteger #-}
-- | Multiply two 'Integer's.
multiplyInteger :: Integer -> Integer -> Integer
multiplyInteger x y = fromBuiltin (BI.multiplyInteger (toBuiltin x) (toBuiltin y))
multiplyInteger x y = fromOpaque (BI.multiplyInteger (toOpaque x) (toOpaque y))

{-# INLINABLE divideInteger #-}
-- | Divide two integers.
divideInteger :: Integer -> Integer -> Integer
divideInteger x y = fromBuiltin (BI.divideInteger (toBuiltin x) (toBuiltin y))
divideInteger x y = fromOpaque (BI.divideInteger (toOpaque x) (toOpaque y))

{-# INLINABLE modInteger #-}
-- | Integer modulo operation.
modInteger :: Integer -> Integer -> Integer
modInteger x y = fromBuiltin (BI.modInteger (toBuiltin x) (toBuiltin y))
modInteger x y = fromOpaque (BI.modInteger (toOpaque x) (toOpaque y))

{-# INLINABLE quotientInteger #-}
-- | Quotient of two integers.
quotientInteger :: Integer -> Integer -> Integer
quotientInteger x y = fromBuiltin (BI.quotientInteger (toBuiltin x) (toBuiltin y))
quotientInteger x y = fromOpaque (BI.quotientInteger (toOpaque x) (toOpaque y))

{-# INLINABLE remainderInteger #-}
-- | Take the remainder of dividing two 'Integer's.
remainderInteger :: Integer -> Integer -> Integer
remainderInteger x y = fromBuiltin (BI.remainderInteger (toBuiltin x) (toBuiltin y))
remainderInteger x y = fromOpaque (BI.remainderInteger (toOpaque x) (toOpaque y))

{-# INLINABLE greaterThanInteger #-}
-- | Check whether one 'Integer' is greater than another.
Expand All @@ -341,22 +345,22 @@ greaterThanEqualsInteger x y = BI.ifThenElse (BI.lessThanInteger x y) False True
{-# INLINABLE lessThanInteger #-}
-- | Check whether one 'Integer' is less than another.
lessThanInteger :: Integer -> Integer -> Bool
lessThanInteger x y = fromBuiltin (BI.lessThanInteger (toBuiltin x) (toBuiltin y))
lessThanInteger x y = fromOpaque (BI.lessThanInteger (toOpaque x) (toOpaque y))

{-# INLINABLE lessThanEqualsInteger #-}
-- | Check whether one 'Integer' is less than or equal to another.
lessThanEqualsInteger :: Integer -> Integer -> Bool
lessThanEqualsInteger x y = fromBuiltin (BI.lessThanEqualsInteger (toBuiltin x) (toBuiltin y))
lessThanEqualsInteger x y = fromOpaque (BI.lessThanEqualsInteger (toOpaque x) (toOpaque y))

{-# INLINABLE equalsInteger #-}
-- | Check if two 'Integer's are equal.
equalsInteger :: Integer -> Integer -> Bool
equalsInteger x y = fromBuiltin (BI.equalsInteger (toBuiltin x) (toBuiltin y))
equalsInteger x y = fromOpaque (BI.equalsInteger (toOpaque x) (toOpaque y))

{-# INLINABLE error #-}
-- | Aborts evaluation with an error.
error :: () -> a
error x = BI.error (toBuiltin x)
error x = BI.error (toOpaque x)

{-# INLINABLE appendString #-}
-- | Append two 'String's.
Expand All @@ -371,7 +375,7 @@ emptyString = BI.emptyString
{-# INLINABLE equalsString #-}
-- | Check if two strings are equal
equalsString :: BuiltinString -> BuiltinString -> Bool
equalsString x y = fromBuiltin (BI.equalsString x y)
equalsString x y = fromOpaque (BI.equalsString x y)

{-# INLINABLE trace #-}
-- | Emit the given string as a trace message before evaluating the argument.
Expand Down Expand Up @@ -420,17 +424,17 @@ serialiseData = BI.serialiseData
{-# INLINABLE mkConstr #-}
-- | Constructs a 'BuiltinData' value with the @Constr@ constructor.
mkConstr :: Integer -> [BuiltinData] -> BuiltinData
mkConstr i args = BI.mkConstr (toBuiltin i) (toBuiltin args)
mkConstr i args = BI.mkConstr (toOpaque i) (toOpaque args)

{-# INLINABLE mkMap #-}
-- | Constructs a 'BuiltinData' value with the @Map@ constructor.
mkMap :: [(BuiltinData, BuiltinData)] -> BuiltinData
mkMap es = BI.mkMap (toBuiltin es)
mkMap es = BI.mkMap (toOpaque es)

{-# INLINABLE mkList #-}
-- | Constructs a 'BuiltinData' value with the @List@ constructor.
mkList :: [BuiltinData] -> BuiltinData
mkList l = BI.mkList (toBuiltin l)
mkList l = BI.mkList (toOpaque l)

{-# INLINABLE mkI #-}
-- | Constructs a 'BuiltinData' value with the @I@ constructor.
Expand All @@ -445,22 +449,22 @@ mkB = BI.mkB
{-# INLINABLE unsafeDataAsConstr #-}
-- | Deconstructs a 'BuiltinData' as a @Constr@, or fails if it is not one.
unsafeDataAsConstr :: BuiltinData -> (Integer, [BuiltinData])
unsafeDataAsConstr d = fromBuiltin (BI.unsafeDataAsConstr d)
unsafeDataAsConstr d = fromOpaque (BI.unsafeDataAsConstr d)

{-# INLINABLE unsafeDataAsMap #-}
-- | Deconstructs a 'BuiltinData' as a @Map@, or fails if it is not one.
unsafeDataAsMap :: BuiltinData -> [(BuiltinData, BuiltinData)]
unsafeDataAsMap d = fromBuiltin (BI.unsafeDataAsMap d)
unsafeDataAsMap d = fromOpaque (BI.unsafeDataAsMap d)

{-# INLINABLE unsafeDataAsList #-}
-- | Deconstructs a 'BuiltinData' as a @List@, or fails if it is not one.
unsafeDataAsList :: BuiltinData -> [BuiltinData]
unsafeDataAsList d = fromBuiltin (BI.unsafeDataAsList d)
unsafeDataAsList d = fromOpaque (BI.unsafeDataAsList d)

{-# INLINABLE unsafeDataAsI #-}
-- | Deconstructs a 'BuiltinData' as an @I@, or fails if it is not one.
unsafeDataAsI :: BuiltinData -> Integer
unsafeDataAsI d = fromBuiltin (BI.unsafeDataAsI d)
unsafeDataAsI d = fromOpaque (BI.unsafeDataAsI d)

{-# INLINABLE unsafeDataAsB #-}
-- | Deconstructs a 'BuiltinData' as a @B@, or fails if it is not one.
Expand All @@ -470,7 +474,7 @@ unsafeDataAsB = BI.unsafeDataAsB
{-# INLINABLE equalsData #-}
-- | Check if two 'BuiltinData's are equal.
equalsData :: BuiltinData -> BuiltinData -> Bool
equalsData d1 d2 = fromBuiltin (BI.equalsData d1 d2)
equalsData d1 d2 = fromOpaque (BI.equalsData d1 d2)

{-# INLINABLE matchData #-}
-- | Given a 'BuiltinData' value and matching functions for the five constructors,
Expand Down Expand Up @@ -517,7 +521,7 @@ matchData' d constrCase mapCase listCase iCase bCase =
-- G1 --
{-# INLINABLE bls12_381_G1_equals #-}
bls12_381_G1_equals :: BuiltinBLS12_381_G1_Element -> BuiltinBLS12_381_G1_Element -> Bool
bls12_381_G1_equals a b = fromBuiltin (BI.bls12_381_G1_equals a b)
bls12_381_G1_equals a b = fromOpaque (BI.bls12_381_G1_equals a b)

{-# INLINABLE bls12_381_G1_add #-}
bls12_381_G1_add :: BuiltinBLS12_381_G1_Element -> BuiltinBLS12_381_G1_Element -> BuiltinBLS12_381_G1_Element
Expand Down Expand Up @@ -554,7 +558,7 @@ bls12_381_G1_compressed_generator = BI.bls12_381_G1_compressed_generator
-- G2 --
{-# INLINABLE bls12_381_G2_equals #-}
bls12_381_G2_equals :: BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_G2_Element -> Bool
bls12_381_G2_equals a b = fromBuiltin (BI.bls12_381_G2_equals a b)
bls12_381_G2_equals a b = fromOpaque (BI.bls12_381_G2_equals a b)

{-# INLINABLE bls12_381_G2_add #-}
bls12_381_G2_add :: BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_G2_Element
Expand Down Expand Up @@ -599,7 +603,7 @@ bls12_381_mulMlResult = BI.bls12_381_mulMlResult

{-# INLINABLE bls12_381_finalVerify #-}
bls12_381_finalVerify :: BuiltinBLS12_381_MlResult -> BuiltinBLS12_381_MlResult -> Bool
bls12_381_finalVerify a b = fromBuiltin (BI.bls12_381_finalVerify a b)
bls12_381_finalVerify a b = fromOpaque (BI.bls12_381_finalVerify a b)

-- Bitwise conversions

Expand All @@ -626,7 +630,7 @@ byteOrderToBool LittleEndian = False
-- fit into a bytestring of length 8192.
{-# INLINABLE integerToByteString #-}
integerToByteString :: ByteOrder -> Integer -> Integer -> BuiltinByteString
integerToByteString endianness = BI.integerToByteString (toBuiltin (byteOrderToBool endianness))
integerToByteString endianness = BI.integerToByteString (toOpaque (byteOrderToBool endianness))

-- | Convert a 'BuiltinByteString' to a 'BuiltinInteger', as described in
-- [CIP-0087](https://github.com/mlabs-haskell/CIPs/tree/koz/to-from-bytestring/CIP-XXXX).
Expand All @@ -636,4 +640,4 @@ integerToByteString endianness = BI.integerToByteString (toBuiltin (byteOrderToB
{-# INLINABLE byteStringToInteger #-}
byteStringToInteger :: ByteOrder -> BuiltinByteString -> Integer
byteStringToInteger endianness =
BI.byteStringToInteger (toBuiltin (byteOrderToBool endianness))
BI.byteStringToInteger (toOpaque (byteOrderToBool endianness))
Loading