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
Split 'Has*' into 'From*' and 'To*' again
  • Loading branch information
effectfully committed May 10, 2024
commit 94ff0ff38e4a5f65a82525c52ce788992da0badd
14 changes: 0 additions & 14 deletions plutus-tx/src/PlutusTx/Builtins/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import PlutusCore.Crypto.Ed25519 qualified
import PlutusCore.Crypto.Hash qualified as Hash
import PlutusCore.Crypto.Secp256k1 qualified
import PlutusCore.Data qualified as PLC
import PlutusCore.Default qualified as PLC
import PlutusCore.Pretty (Pretty (..), display)
import Prettyprinter (viaShow)

Expand Down Expand Up @@ -366,15 +365,6 @@ fst (BuiltinPair (a, _)) = a
snd :: BuiltinPair a b -> b
snd (BuiltinPair (_, b)) = b

{-# NOINLINE mkPair #-}
mkPair
:: PLC.DefaultUni (PLC.Esc a)
-> PLC.DefaultUni (PLC.Esc b)
-> aAsBuiltin
-> bAsBuiltin
-> BuiltinPair aAsBuiltin bAsBuiltin
mkPair _ _ x y = BuiltinPair (x, y)

{-# NOINLINE mkPairData #-}
mkPairData :: BuiltinData -> BuiltinData -> BuiltinPair BuiltinData BuiltinData
mkPairData d1 d2 = BuiltinPair (d1, d2)
Expand Down Expand Up @@ -413,10 +403,6 @@ chooseList :: BuiltinList a -> b -> b -> b
chooseList (BuiltinList []) b1 _ = b1
chooseList (BuiltinList (_:_)) _ b2 = b2

{-# NOINLINE mkNil #-}
mkNil :: PLC.DefaultUni (PLC.Esc a) -> BuiltinList aAsBuiltin
mkNil _ = BuiltinList []

{-# NOINLINE mkNilData #-}
mkNilData :: BuiltinUnit -> BuiltinList BuiltinData
mkNilData _ = BuiltinList []
Expand Down
64 changes: 39 additions & 25 deletions plutus-tx/src/PlutusTx/Builtins/IsBuiltin.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE StandaloneKindSignatures #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
Expand All @@ -11,7 +12,6 @@

module PlutusTx.Builtins.IsBuiltin where

import PlutusCore qualified as PLC
import PlutusCore.Crypto.BLS12_381.G1 qualified as BLS12_381.G1 (Element)
import PlutusCore.Crypto.BLS12_381.G2 qualified as BLS12_381.G2 (Element)
import PlutusCore.Crypto.BLS12_381.Pairing qualified as BLS12_381.Pairing (MlResult)
Expand Down Expand Up @@ -59,48 +59,56 @@ instance IsString BuiltinString where
-- See Note [noinline hack]
fromString = Magic.noinline stringToBuiltinString

type IsBuiltin :: GHC.Type -> GHC.Constraint
class PLC.DefaultUni `PLC.Contains` (FromBuiltin a) => IsBuiltin a where
type HasFromBuiltin :: GHC.Type -> GHC.Constraint
class HasFromBuiltin a where
type FromBuiltin a
fromBuiltin :: a -> FromBuiltin a

type HasToBuiltin :: GHC.Type -> GHC.Constraint
class HasToBuiltin a where
toBuiltin :: FromBuiltin a -> a

instance IsBuiltin BuiltinInteger where
instance HasFromBuiltin BuiltinInteger where
type FromBuiltin BuiltinInteger = Integer
{-# INLINABLE fromBuiltin #-}
fromBuiltin = id
instance HasToBuiltin BuiltinInteger where
{-# INLINABLE toBuiltin #-}
toBuiltin = id

instance IsBuiltin BuiltinByteString where
instance HasFromBuiltin BuiltinByteString where
type FromBuiltin BuiltinByteString = ByteString
{-# INLINABLE fromBuiltin #-}
fromBuiltin (BuiltinByteString b) = b
instance HasToBuiltin BuiltinByteString where
{-# INLINABLE toBuiltin #-}
toBuiltin = BuiltinByteString

instance IsBuiltin BuiltinString where
instance HasFromBuiltin BuiltinString where
type FromBuiltin BuiltinString = Text
{-# INLINABLE fromBuiltin #-}
fromBuiltin (BuiltinString t) = t
instance HasToBuiltin BuiltinString where
{-# INLINABLE toBuiltin #-}
toBuiltin = BuiltinString

instance IsBuiltin BuiltinUnit where
instance HasFromBuiltin BuiltinUnit where
type FromBuiltin BuiltinUnit = ()
{-# INLINABLE fromBuiltin #-}
fromBuiltin u = chooseUnit u ()
instance HasToBuiltin BuiltinUnit where
{-# INLINABLE toBuiltin #-}
toBuiltin x = case x of () -> unitval

instance IsBuiltin BuiltinBool where
instance HasFromBuiltin BuiltinBool where
type FromBuiltin BuiltinBool = Bool
{-# INLINABLE fromBuiltin #-}
fromBuiltin b = ifThenElse b True False
instance HasToBuiltin BuiltinBool where
{-# INLINABLE toBuiltin #-}
toBuiltin b = if b then true else false

instance IsBuiltin a => IsBuiltin (BuiltinList a) where
instance HasFromBuiltin a => HasFromBuiltin (BuiltinList a) where
type FromBuiltin (BuiltinList a) = [FromBuiltin a]

{-# INLINABLE fromBuiltin #-}
Expand All @@ -115,50 +123,56 @@ instance IsBuiltin a => IsBuiltin (BuiltinList a) where
-- need to do the manual laziness ourselves.
go l = chooseList l (\_ -> []) (\_ -> fromBuiltin (head l) : go (tail l)) unitval

instance HasToBuiltin (BuiltinList BuiltinData) where
{-# INLINE toBuiltin #-}
toBuiltin = goList where
goList :: [Data] -> BuiltinList BuiltinData
goList [] = mkNilData unitval
goList (d:ds) = mkCons (toBuiltin d) (goList ds)

instance HasToBuiltin (BuiltinList (BuiltinPair BuiltinData BuiltinData)) where
{-# INLINE toBuiltin #-}
toBuiltin = goList where
goList :: [FromBuiltin a] -> BuiltinList a
goList [] = mkNil @(FromBuiltin a) (Magic.inline PLC.knownUni)
goList :: [(Data, Data)] -> BuiltinList (BuiltinPair BuiltinData BuiltinData)
goList [] = mkNilPairData unitval
goList (d:ds) = mkCons (toBuiltin d) (goList ds)

instance (IsBuiltin a, IsBuiltin b) => IsBuiltin (BuiltinPair a b) where
instance (HasFromBuiltin a, HasFromBuiltin b) => HasFromBuiltin (BuiltinPair a b) where
type FromBuiltin (BuiltinPair a b) = (FromBuiltin a, FromBuiltin b)
{-# INLINABLE fromBuiltin #-}
fromBuiltin p = (fromBuiltin $ fst p, fromBuiltin $ snd p)
instance HasToBuiltin (BuiltinPair BuiltinData BuiltinData) where
{-# INLINABLE toBuiltin #-}
toBuiltin (d1, d2) =
mkPair
@(FromBuiltin a)
@(FromBuiltin b)
(Magic.inline PLC.knownUni)
(Magic.inline PLC.knownUni)
(toBuiltin d1)
(toBuiltin d2)

instance IsBuiltin BuiltinData where
toBuiltin (d1, d2) = mkPairData (toBuiltin d1) (toBuiltin d2)

instance HasFromBuiltin BuiltinData where
type FromBuiltin BuiltinData = Data
{-# INLINABLE fromBuiltin #-}
fromBuiltin (BuiltinData t) = t
instance HasToBuiltin BuiltinData where
{-# INLINABLE toBuiltin #-}
toBuiltin = BuiltinData

instance IsBuiltin BuiltinBLS12_381_G1_Element where
instance HasFromBuiltin BuiltinBLS12_381_G1_Element where
type FromBuiltin BuiltinBLS12_381_G1_Element = BLS12_381.G1.Element
{-# INLINABLE fromBuiltin #-}
fromBuiltin (BuiltinBLS12_381_G1_Element a) = a
instance HasToBuiltin BuiltinBLS12_381_G1_Element where
{-# INLINABLE toBuiltin #-}
toBuiltin = BuiltinBLS12_381_G1_Element

instance IsBuiltin BuiltinBLS12_381_G2_Element where
instance HasFromBuiltin BuiltinBLS12_381_G2_Element where
type FromBuiltin BuiltinBLS12_381_G2_Element = BLS12_381.G2.Element
{-# INLINABLE fromBuiltin #-}
fromBuiltin (BuiltinBLS12_381_G2_Element a) = a
instance HasToBuiltin BuiltinBLS12_381_G2_Element where
{-# INLINABLE toBuiltin #-}
toBuiltin = BuiltinBLS12_381_G2_Element

instance IsBuiltin BuiltinBLS12_381_MlResult where
instance HasFromBuiltin BuiltinBLS12_381_MlResult where
type FromBuiltin BuiltinBLS12_381_MlResult = BLS12_381.Pairing.MlResult
{-# INLINABLE fromBuiltin #-}
fromBuiltin (BuiltinBLS12_381_MlResult a) = a
instance HasToBuiltin BuiltinBLS12_381_MlResult where
{-# INLINABLE toBuiltin #-}
toBuiltin = BuiltinBLS12_381_MlResult
64 changes: 37 additions & 27 deletions plutus-tx/src/PlutusTx/Builtins/IsOpaque.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE StandaloneKindSignatures #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
Expand All @@ -11,60 +12,63 @@

module PlutusTx.Builtins.IsOpaque where

import PlutusCore qualified as PLC
import PlutusCore.Crypto.BLS12_381.G1 qualified as BLS12_381.G1 (Element)
import PlutusCore.Crypto.BLS12_381.G2 qualified as BLS12_381.G2 (Element)
import PlutusCore.Crypto.BLS12_381.Pairing qualified as BLS12_381.Pairing (MlResult)
import PlutusTx.Base (id, ($))
import PlutusTx.Bool (Bool (..))
import PlutusTx.Builtins.Internal
import PlutusTx.Builtins.IsBuiltin (FromBuiltin)

import Data.Kind qualified as GHC
import GHC.Magic qualified as Magic

type IsOpaque :: GHC.Type -> GHC.Constraint
class PLC.DefaultUni `PLC.Contains` FromBuiltin a => IsOpaque a where
type HasFromOpaque :: GHC.Type -> GHC.Constraint
class HasFromOpaque a where
type FromOpaque a
fromOpaque :: a -> FromOpaque a
class HasToOpaque a where
toOpaque :: FromOpaque a -> a

instance IsOpaque BuiltinInteger where
instance HasFromOpaque BuiltinInteger where
type FromOpaque BuiltinInteger = BuiltinInteger
{-# INLINABLE fromOpaque #-}
fromOpaque = id
instance HasToOpaque BuiltinInteger where
{-# INLINABLE toOpaque #-}
toOpaque = id

instance IsOpaque BuiltinByteString where
instance HasFromOpaque BuiltinByteString where
type FromOpaque BuiltinByteString = BuiltinByteString
{-# INLINABLE fromOpaque #-}
fromOpaque = id
instance HasToOpaque BuiltinByteString where
{-# INLINABLE toOpaque #-}
toOpaque = id

instance IsOpaque BuiltinString where
instance HasFromOpaque BuiltinString where
type FromOpaque BuiltinString = BuiltinString
{-# INLINABLE fromOpaque #-}
fromOpaque = id
instance HasToOpaque BuiltinString where
{-# INLINABLE toOpaque #-}
toOpaque = id

instance IsOpaque BuiltinUnit where
instance HasFromOpaque BuiltinUnit where
type FromOpaque BuiltinUnit = ()
{-# INLINABLE fromOpaque #-}
fromOpaque u = chooseUnit u ()
instance HasToOpaque BuiltinUnit where
{-# INLINABLE toOpaque #-}
toOpaque x = case x of () -> unitval

instance IsOpaque BuiltinBool where
instance HasFromOpaque BuiltinBool where
type FromOpaque BuiltinBool = Bool
{-# INLINABLE fromOpaque #-}
fromOpaque b = ifThenElse b True False
instance HasToOpaque BuiltinBool where
{-# INLINABLE toOpaque #-}
toOpaque b = if b then true else false

instance IsOpaque a => IsOpaque (BuiltinList a) where
instance HasFromOpaque a => HasFromOpaque (BuiltinList a) where
type FromOpaque (BuiltinList a) = [FromOpaque a]

{-# INLINABLE fromOpaque #-}
Expand All @@ -79,50 +83,56 @@ instance IsOpaque a => IsOpaque (BuiltinList a) where
-- need to do the manual laziness ourselves.
go l = chooseList l (\_ -> []) (\_ -> fromOpaque (head l) : go (tail l)) unitval

instance HasToOpaque (BuiltinList BuiltinData) where
{-# INLINE toOpaque #-}
toOpaque = goList where
goList :: [FromOpaque a] -> BuiltinList a
goList [] = mkNil @(FromBuiltin a) (Magic.inline PLC.knownUni)
goList :: [BuiltinData] -> BuiltinList BuiltinData
goList [] = mkNilData unitval
goList (d:ds) = mkCons (toOpaque d) (goList ds)

instance (IsOpaque a, IsOpaque b) => IsOpaque (BuiltinPair a b) where
instance HasToOpaque (BuiltinList (BuiltinPair BuiltinData BuiltinData)) where
{-# INLINE toOpaque #-}
toOpaque = goList where
goList :: [(BuiltinData, BuiltinData)] -> BuiltinList (BuiltinPair BuiltinData BuiltinData)
goList [] = mkNilPairData unitval
goList (d:ds) = mkCons (toOpaque d) (goList ds)

instance (HasFromOpaque a, HasFromOpaque b) => HasFromOpaque (BuiltinPair a b) where
type FromOpaque (BuiltinPair a b) = (FromOpaque a, FromOpaque b)
{-# INLINABLE fromOpaque #-}
fromOpaque p = (fromOpaque $ fst p, fromOpaque $ snd p)
instance HasToOpaque (BuiltinPair BuiltinData BuiltinData) where
{-# INLINABLE toOpaque #-}
toOpaque (d1, d2) =
mkPair
@(FromBuiltin a)
@(FromBuiltin b)
(Magic.inline PLC.knownUni)
(Magic.inline PLC.knownUni)
(toOpaque d1)
(toOpaque d2)

instance IsOpaque BuiltinData where
toOpaque (d1, d2) = mkPairData d1 d2

instance HasFromOpaque BuiltinData where
type FromOpaque BuiltinData = BuiltinData
{-# INLINABLE fromOpaque #-}
fromOpaque = id
instance HasToOpaque BuiltinData where
{-# INLINABLE toOpaque #-}
toOpaque = id

instance IsOpaque BuiltinBLS12_381_G1_Element where
instance HasFromOpaque BuiltinBLS12_381_G1_Element where
type FromOpaque BuiltinBLS12_381_G1_Element = BLS12_381.G1.Element
{-# INLINABLE fromOpaque #-}
fromOpaque (BuiltinBLS12_381_G1_Element a) = a
instance HasToOpaque BuiltinBLS12_381_G1_Element where
{-# INLINABLE toOpaque #-}
toOpaque = BuiltinBLS12_381_G1_Element

instance IsOpaque BuiltinBLS12_381_G2_Element where
instance HasFromOpaque BuiltinBLS12_381_G2_Element where
type FromOpaque BuiltinBLS12_381_G2_Element = BLS12_381.G2.Element
{-# INLINABLE fromOpaque #-}
fromOpaque (BuiltinBLS12_381_G2_Element a) = a
instance HasToOpaque BuiltinBLS12_381_G2_Element where
{-# INLINABLE toOpaque #-}
toOpaque = BuiltinBLS12_381_G2_Element

instance IsOpaque BuiltinBLS12_381_MlResult where
instance HasFromOpaque BuiltinBLS12_381_MlResult where
type FromOpaque BuiltinBLS12_381_MlResult = BLS12_381.Pairing.MlResult
{-# INLINABLE fromOpaque #-}
fromOpaque (BuiltinBLS12_381_MlResult a) = a
instance HasToOpaque BuiltinBLS12_381_MlResult where
{-# INLINABLE toOpaque #-}
toOpaque = BuiltinBLS12_381_MlResult
11 changes: 7 additions & 4 deletions plutus-tx/src/PlutusTx/Lift/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import PlutusCore.Quote
import PlutusIR.MkPir
import PlutusTx.Builtins
import PlutusTx.Builtins.Internal (BuiltinBool, BuiltinList, BuiltinPair, BuiltinUnit)
import PlutusTx.Builtins.IsBuiltin (FromBuiltin, IsBuiltin)
import PlutusTx.Builtins.IsBuiltin (FromBuiltin, HasFromBuiltin)

import Language.Haskell.TH qualified as TH hiding (newName)

Expand Down Expand Up @@ -188,14 +188,17 @@ instance uni `PLC.HasTypeLevel` [] => Typeable uni BuiltinList where
typeRep _proxyBuiltinList = typeRepBuiltin (Proxy @[])

-- See Note [Lift and Typeable instances for builtins]
instance (IsBuiltin a, uni `PLC.HasTermLevel` [FromBuiltin a]) => Lift uni (BuiltinList a) where
instance (HasFromBuiltin a, uni `PLC.HasTermLevel` [FromBuiltin a]) =>
Lift uni (BuiltinList a) where
lift = liftBuiltin . fromBuiltin

instance uni `PLC.HasTypeLevel` (,) => Typeable uni BuiltinPair where
typeRep _proxyBuiltinPair = typeRepBuiltin (Proxy @(,))

instance (IsBuiltin a, IsBuiltin b, uni `PLC.HasTermLevel` (FromBuiltin a, FromBuiltin b)) =>
Lift uni (BuiltinPair a b) where
instance
( HasFromBuiltin a, HasFromBuiltin b
, uni `PLC.HasTermLevel` (FromBuiltin a, FromBuiltin b)
) => Lift uni (BuiltinPair a b) where
lift = liftBuiltin . fromBuiltin

-- See Note [Lift and Typeable instances for builtins]
Expand Down