Skip to content

Commit

Permalink
Merge pull request #4450 from Quviq/PR-data-instances-release-node-1.34
Browse files Browse the repository at this point in the history
Instances of Data for some datatypes on release/node-1.34
  • Loading branch information
michaelpj authored Mar 7, 2022
2 parents fc5fe94 + dbcbc74 commit 4127e9c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
4 changes: 3 additions & 1 deletion plutus-core/plutus-core/src/PlutusCore/Data.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
Expand All @@ -19,6 +20,7 @@ import Control.Monad.Except
import Data.Bits (shiftR)
import Data.ByteString qualified as BS
import Data.ByteString.Lazy qualified as BSL
import Data.Data qualified
import Data.Word (Word64, Word8)
import GHC.Generics
import Prelude
Expand All @@ -36,7 +38,7 @@ data Data =
| List [Data]
| I Integer
| B BS.ByteString
deriving stock (Show, Eq, Ord, Generic)
deriving stock (Show, Eq, Ord, Generic, Data.Data.Data)
deriving anyclass (NFData)

instance Pretty Data where
Expand Down
4 changes: 3 additions & 1 deletion plutus-ledger-api/src/Plutus/V1/Ledger/Slot.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
Expand All @@ -23,6 +24,7 @@ module Plutus.V1.Ledger.Slot(
import Codec.Serialise.Class (Serialise)
import Control.DeepSeq (NFData)
import Data.Aeson (FromJSON, FromJSONKey, ToJSON, ToJSONKey)
import Data.Data
import Data.Hashable (Hashable)
import GHC.Generics (Generic)
import Prelude qualified as Haskell
Expand All @@ -40,7 +42,7 @@ import Plutus.V1.Ledger.Interval
-- | The slot number. This is a good proxy for time, since on the Cardano blockchain
-- slots pass at a constant rate.
newtype Slot = Slot { getSlot :: Integer }
deriving stock (Haskell.Eq, Haskell.Ord, Haskell.Show, Generic)
deriving stock (Haskell.Eq, Haskell.Ord, Haskell.Show, Generic, Data)
deriving anyclass (FromJSON, FromJSONKey, ToJSON, ToJSONKey, NFData)
deriving newtype (AdditiveSemigroup, AdditiveMonoid, AdditiveGroup, Eq, Ord, Enum, PlutusTx.ToData, PlutusTx.FromData, PlutusTx.UnsafeFromData)
deriving newtype (Haskell.Num, Haskell.Enum, Haskell.Real, Haskell.Integral, Serialise, Hashable)
Expand Down
10 changes: 6 additions & 4 deletions plutus-ledger-api/src/Plutus/V1/Ledger/Value.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE LambdaCase #-}
Expand Down Expand Up @@ -57,6 +58,7 @@ import Data.Aeson (FromJSON, FromJSONKey, ToJSON, ToJSONKey, (.:))
import Data.Aeson qualified as JSON
import Data.Aeson.Extras qualified as JSON
import Data.ByteString qualified as BS
import Data.Data
import Data.Hashable (Hashable)
import Data.List qualified (sortBy)
import Data.String (IsString (fromString))
Expand All @@ -79,7 +81,7 @@ import Prettyprinter.Extras

newtype CurrencySymbol = CurrencySymbol { unCurrencySymbol :: PlutusTx.BuiltinByteString }
deriving (IsString, Haskell.Show, Serialise, Pretty) via LedgerBytes
deriving stock (Generic)
deriving stock (Generic, Data)
deriving newtype (Haskell.Eq, Haskell.Ord, Eq, Ord, PlutusTx.ToData, PlutusTx.FromData, PlutusTx.UnsafeFromData)
deriving anyclass (Hashable, ToJSONKey, FromJSONKey, NFData)

Expand Down Expand Up @@ -119,7 +121,7 @@ currencySymbol = CurrencySymbol . PlutusTx.toBuiltin
-- | ByteString of a name of a token, shown as UTF-8 string when possible
newtype TokenName = TokenName { unTokenName :: PlutusTx.BuiltinByteString }
deriving (Serialise) via LedgerBytes
deriving stock (Generic)
deriving stock (Generic, Data)
deriving newtype (Haskell.Eq, Haskell.Ord, Eq, Ord, PlutusTx.ToData, PlutusTx.FromData, PlutusTx.UnsafeFromData)
deriving anyclass (Hashable, NFData)
deriving Pretty via (PrettyShow TokenName)
Expand Down Expand Up @@ -178,7 +180,7 @@ instance FromJSON TokenName where

-- | An asset class, identified by currency symbol and token name.
newtype AssetClass = AssetClass { unAssetClass :: (CurrencySymbol, TokenName) }
deriving stock (Generic)
deriving stock (Generic, Data)
deriving newtype (Haskell.Eq, Haskell.Ord, Haskell.Show, Eq, Ord, Serialise, PlutusTx.ToData, PlutusTx.FromData, PlutusTx.UnsafeFromData)
deriving anyclass (Hashable, NFData, ToJSON, FromJSON)
deriving Pretty via (PrettyShow (CurrencySymbol, TokenName))
Expand All @@ -203,7 +205,7 @@ assetClass s t = AssetClass (s, t)
--
-- See note [Currencies] for more details.
newtype Value = Value { getValue :: Map.Map CurrencySymbol (Map.Map TokenName Integer) }
deriving stock (Generic)
deriving stock (Generic, Data)
deriving anyclass (ToJSON, FromJSON, Hashable, NFData)
deriving newtype (Serialise, PlutusTx.ToData, PlutusTx.FromData, PlutusTx.UnsafeFromData)
deriving Pretty via (PrettyShow Value)
Expand Down
4 changes: 3 additions & 1 deletion plutus-tx/src/PlutusTx/AssocMap.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
Expand Down Expand Up @@ -38,6 +39,7 @@ module PlutusTx.AssocMap (
) where

import Control.DeepSeq (NFData)
import Data.Data
import GHC.Generics (Generic)
import PlutusTx.Builtins qualified as P
import PlutusTx.Builtins.Internal qualified as BI
Expand All @@ -53,7 +55,7 @@ import Prettyprinter (Pretty (..))

-- | A 'Map' of key-value pairs.
newtype Map k v = Map { unMap :: [(k, v)] }
deriving stock (Generic, Haskell.Eq, Haskell.Show)
deriving stock (Generic, Haskell.Eq, Haskell.Show, Data)
deriving newtype (Eq, Ord, NFData)

-- Hand-written instances to use the underlying 'Map' type in 'Data', and to be reasonably efficient.
Expand Down
14 changes: 10 additions & 4 deletions plutus-tx/src/PlutusTx/Builtins/Internal.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
Expand All @@ -19,7 +20,8 @@ import Data.ByteArray qualified as BA
import Data.ByteString as BS
import Data.ByteString.Hash qualified as Hash
import Data.Coerce (coerce)
import Data.Hashable (Hashable)
import Data.Data
import Data.Hashable (Hashable (..))
import Data.Maybe (fromMaybe)
import Data.Text as Text (Text, empty)
import Data.Text.Encoding as Text (decodeUtf8, encodeUtf8)
Expand Down Expand Up @@ -69,7 +71,7 @@ error = mustBeReplaced "error"
BOOL
-}

newtype BuiltinBool = BuiltinBool Bool
newtype BuiltinBool = BuiltinBool Bool deriving stock Data

{-# NOINLINE true #-}
true :: BuiltinBool
Expand All @@ -87,7 +89,7 @@ ifThenElse (BuiltinBool b) x y = if b then x else y
UNIT
-}

newtype BuiltinUnit = BuiltinUnit ()
newtype BuiltinUnit = BuiltinUnit () deriving stock Data

{-# NOINLINE unitval #-}
unitval :: BuiltinUnit
Expand Down Expand Up @@ -149,7 +151,7 @@ BYTESTRING

-- | An opaque type representing Plutus Core ByteStrings.
newtype BuiltinByteString = BuiltinByteString ByteString
deriving stock (Generic)
deriving stock (Generic, Data)
deriving newtype (Haskell.Show, Haskell.Eq, Haskell.Ord, Haskell.Semigroup, Haskell.Monoid)
deriving newtype (Hashable, Serialise, NFData, BA.ByteArrayAccess, BA.ByteArray)

Expand Down Expand Up @@ -218,6 +220,7 @@ STRING
-}

newtype BuiltinString = BuiltinString Text
deriving stock Data
deriving newtype (Haskell.Show, Haskell.Eq, Haskell.Ord)

{-# NOINLINE appendString #-}
Expand Down Expand Up @@ -245,6 +248,7 @@ PAIR
-}

newtype BuiltinPair a b = BuiltinPair (a, b)
deriving stock Data
deriving newtype (Haskell.Show, Haskell.Eq, Haskell.Ord)

{-# NOINLINE fst #-}
Expand All @@ -264,6 +268,7 @@ LIST
-}

newtype BuiltinList a = BuiltinList [a]
deriving stock Data
deriving newtype (Haskell.Show, Haskell.Eq, Haskell.Ord)

{-# NOINLINE null #-}
Expand Down Expand Up @@ -315,6 +320,7 @@ For off-chain usage, there are conversion functions 'builtinDataToData' and
'dataToBuiltinData', but note that these will not work on-chain.
-}
newtype BuiltinData = BuiltinData PLC.Data
deriving stock Data
deriving newtype (Haskell.Show, Haskell.Eq, Haskell.Ord)

-- NOT a builtin, only safe off-chain, hence the NOINLINE
Expand Down

0 comments on commit 4127e9c

Please sign in to comment.