Added These data types

This commit is contained in:
Jack Hodgkinson 2022-03-04 09:27:41 +00:00
parent 86182ced25
commit 43cd0c4507
6 changed files with 114 additions and 139 deletions

View file

@ -0,0 +1,62 @@
{-# OPTIONS_GHC -Wno-orphans #-}
module Plutarch.Api.V1.These (PTheseData (..)) where
import GHC.Generics qualified as GHC
import Generics.SOP
import Plutarch.DataRepr (PIsDataReprInstances (PIsDataReprInstances))
import Plutarch.Lift (
PConstantRepr,
PConstanted,
PLifted,
PUnsafeLiftDecl,
pconstantFromRepr,
pconstantToRepr,
)
import Plutus.V1.Ledger.Api qualified as Plutus
import PlutusTx.These qualified as PlutusThese
data PTheseData (a :: PType) (b :: PType) (s :: S)
= PDThis (Term s (PDataRecord '["_0" ':= a]))
| PDThat (Term s (PDataRecord '["_0" ':= b]))
| PDThese (Term s (PDataRecord '["_0" ':= a, "_1" ':= b]))
deriving stock (GHC.Generic)
deriving anyclass (Generic, PIsDataRepr)
deriving
(PlutusType, PIsData)
via PIsDataReprInstances (PTheseData a b)
instance
( Plutus.ToData (PLifted a)
, Plutus.ToData (PLifted b)
, Plutus.FromData (PLifted a)
, Plutus.FromData (PLifted b)
, PLift a
, PLift b
) =>
PUnsafeLiftDecl (PTheseData a b)
where
type PLifted (PTheseData a b) = PlutusThese.These (PLifted a) (PLifted b)
{- TODO: Make PTheseData an instance of PConstant:
https://github.com/Plutonomicon/plutarch/pull/355
-}
instance
( PLifted (PConstanted a) ~ a
, Plutus.ToData b
, Plutus.FromData b
, Plutus.ToData a
, Plutus.FromData a
, PConstant a
, PLifted (PConstanted b) ~ b
, Plutus.FromData b
, Plutus.ToData b
, PConstant b
) =>
PConstant (PlutusThese.These a b)
where
type PConstantRepr (PlutusThese.These a b) = [(Plutus.Data, Plutus.Data)]
type PConstanted (PlutusThese.These a b) = PTheseData (PConstanted a) (PConstanted b)
pconstantToRepr _t = undefined
pconstantFromRepr _t = undefined

12
src/Plutarch/These.hs Normal file
View file

@ -0,0 +1,12 @@
module Plutarch.These (PThese (..)) where
import GHC.Generics qualified as GHC
import Generics.SOP
-- | Plutus These type with Scott-encoded representation.
data PThese (a :: PType) (b :: PType) (s :: S)
= PThis (Term s a)
| PThat (Term s b)
| PThese (Term s a) (Term s b)
deriving stock (GHC.Generic)
deriving anyclass (Generic, PlutusType)