Skip to content

Commit

Permalink
Merge branch 'staging' of github.com:Plutonomicon/plutarch into tilde…
Browse files Browse the repository at this point in the history
…/boolfix
  • Loading branch information
t1lde committed Jan 17, 2022
2 parents 4abd538 + 231267e commit e6994e9
Show file tree
Hide file tree
Showing 30 changed files with 1,077 additions and 320 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
/result*
/dist-newstyle
.direnv
bench.csv
16 changes: 6 additions & 10 deletions Plutarch.hs
Expand Up @@ -22,9 +22,8 @@ module Plutarch (
PI.punsafeConstant,
PI.Term,
PI.TermCont (..),
PL.plift,
PL.pconstant,
PL.plift',
PI.S,
PI.PType,
PlutusType (..),
printTerm,
printScript,
Expand All @@ -40,13 +39,10 @@ module Plutarch (
popaque,
punsafeFromOpaque,
plam,
-- $plam
) where

import Data.Kind (Type)
import Plutarch.Internal (ClosedTerm, Term, compile, papp, phoistAcyclic, plam', punsafeCoerce, (:-->))
import Plutarch.Internal (ClosedTerm, PType, Term, compile, papp, phoistAcyclic, plam', punsafeCoerce, (:-->))
import qualified Plutarch.Internal as PI
import qualified Plutarch.Lift as PL
import Plutus.V1.Ledger.Scripts (Script (Script))
import PlutusCore.Pretty (prettyPlcReadableDebug)

Expand Down Expand Up @@ -139,7 +135,7 @@ pinl v f = f v
A simple example, encoding a Sum type as an Enum via PInteger:
> data AB (s :: k) = A | B
> data AB (s :: S) = A | B
>
> instance PlutusType AB where
> type PInner AB _ = PInteger
Expand All @@ -161,10 +157,10 @@ pinl v f = f v
Further examples can be found in examples/PlutusType.hs
-}
class (PCon a, PMatch a) => PlutusType (a :: k -> Type) where
class (PCon a, PMatch a) => PlutusType (a :: PType) where
-- `b' :: k'` causes GHC to fail type checking at various places
-- due to not being able to expand the type family.
type PInner a (b' :: k -> Type) :: k -> Type
type PInner a (b' :: PType) :: PType
pcon' :: forall s. a s -> forall b. Term s (PInner a b)
pmatch' :: forall s c. (forall b. Term s (PInner a b)) -> (a s -> Term s c) -> Term s c

Expand Down
62 changes: 31 additions & 31 deletions Plutarch/Api/V1.hs
Expand Up @@ -86,7 +86,7 @@ type PTuple = PDataList

---------- V1 Specific types, Incompatible with V2

newtype PTxInfo (s :: k)
newtype PTxInfo (s :: S)
= PTxInfo
( Term
s
Expand Down Expand Up @@ -126,7 +126,7 @@ instance PIsDataRepr PTxInfo where
pmatchRepr dat f =
(pmatchDataRepr dat) ((DRHCons (f . PTxInfo)) $ DRHNil)

newtype PScriptContext (s :: k)
newtype PScriptContext (s :: S)
= PScriptContext (Term s (PDataList '[PTxInfo, PScriptPurpose]))
deriving
(PMatch, PIsData, PUnsafeLiftDecl Plutus.ScriptContext)
Expand All @@ -144,7 +144,7 @@ instance PIsDataRepr PScriptContext where

-- General types, used by V1 and V2

data PScriptPurpose (s :: k)
data PScriptPurpose (s :: S)
= PMinting (Term s (PDataList '[PCurrencySymbol]))
| PSpending (Term s (PDataList '[PTxOutRef]))
| PRewarding (Term s (PDataList '[PStakingCredential]))
Expand Down Expand Up @@ -173,60 +173,60 @@ instance PIsDataRepr PScriptPurpose where

---------- Scripts

newtype PDatum (s :: k) = PDatum (Term s PData)
newtype PDatum (s :: S) = PDatum (Term s PData)
deriving (PIsData, PEq) via PData

newtype PRedeemer (s :: k) = PRedeemer (Term s PData)
newtype PRedeemer (s :: S) = PRedeemer (Term s PData)
deriving (PIsData, PEq) via PData

newtype PDatumHash (s :: k)
newtype PDatumHash (s :: S)
= PDatumHash (Term s PByteString)
deriving (PEq, POrd, PIsData) via PByteString

newtype PStakeValidatorHash (s :: k)
newtype PStakeValidatorHash (s :: S)
= PStakeValidatorHash (Term s PByteString)
deriving (PEq, POrd, PIsData) via PByteString

newtype PRedeemerHash (s :: k)
newtype PRedeemerHash (s :: S)
= PRedeemerHash (Term s PByteString)
deriving (PEq, POrd, PIsData) via PByteString

newtype PValidatorHash (s :: k)
newtype PValidatorHash (s :: S)
= PValidatorHash (Term s PByteString)
deriving (PEq, POrd, PIsData) via PByteString

---------- Value

newtype PTokenName (s :: k)
newtype PTokenName (s :: S)
= PTokenName (Term s PByteString)
deriving (PEq, POrd, PIsData) via (PByteString)
deriving newtype (Semigroup, Monoid)

newtype PValue (s :: k)
newtype PValue (s :: S)
= PValue (Term s (PMap PCurrencySymbol (PMap PTokenName PInteger)))
deriving (PIsData) via (PMap PCurrencySymbol (PMap PTokenName PInteger))

newtype PCurrencySymbol (s :: k)
newtype PCurrencySymbol (s :: S)
= PCurrencySymbol (Term s PByteString)
deriving (PEq, POrd, PIsData) via PByteString

---------- Crypto

newtype PPubKeyHash (s :: k)
newtype PPubKeyHash (s :: S)
= PPubKeyHash (Term s PByteString)
deriving (PEq, POrd, PIsData) via PByteString

newtype PPubKey (s :: k)
newtype PPubKey (s :: S)
= PPubKey (Term s PByteString)
deriving (PEq, POrd, PIsData) via PByteString

newtype PSignature (s :: k)
newtype PSignature (s :: S)
= PSignature (Term s PByteString)
deriving (PEq, POrd, PIsData) via PByteString

---------- Time

newtype PPOSIXTime (s :: k)
newtype PPOSIXTime (s :: S)
= PPOSIXTime (Term s PInteger)
deriving (POrd, PEq, PIntegral, PIsData) via (PInteger)
deriving newtype (Num)
Expand All @@ -237,7 +237,7 @@ type PPOSIXTimeRange = PInterval PPOSIXTime

type PClosure = PBool

newtype PInterval a (s :: k)
newtype PInterval a (s :: S)
= PInterval
( Term
s
Expand Down Expand Up @@ -267,7 +267,7 @@ instance PIsDataRepr (PInterval a) where
pmatchDataRepr dat $
DRHCons (f . PInterval) DRHNil

newtype PLowerBound a (s :: k)
newtype PLowerBound a (s :: S)
= PLowerBound (Term s (PDataList '[PExtended a, PClosure]))
deriving
( PMatch
Expand All @@ -290,7 +290,7 @@ instance PIsDataRepr (PLowerBound a) where
pmatchDataRepr dat $
DRHCons (f . PLowerBound) DRHNil

newtype PUpperBound a (s :: k)
newtype PUpperBound a (s :: S)
= PUpperBound (Term s (PDataList '[PExtended a, PClosure]))
deriving
( PMatch
Expand All @@ -312,7 +312,7 @@ instance PIsDataRepr (PUpperBound a) where
pmatchDataRepr dat $
DRHCons (f . PUpperBound) DRHNil

data PExtended a (s :: k)
data PExtended a (s :: S)
= PNegInf (Term s (PDataList '[]))
| PFinite (Term s (PDataList '[a]))
| PPosInf (Term s (PDataList '[]))
Expand All @@ -335,7 +335,7 @@ instance PIsDataRepr (PExtended a) where

---------- Tx/Address

data PCredential (s :: k)
data PCredential (s :: S)
= PPubKeyCredential (Term s (PDataList '[PPubKeyHash]))
| PScriptCredential (Term s (PDataList '[PValidatorHash]))
deriving
Expand All @@ -356,7 +356,7 @@ instance PIsDataRepr PCredential where
(f . PScriptCredential)
DRHNil

data PStakingCredential (s :: k)
data PStakingCredential (s :: S)
= PStakingHash (Term s (PDataList '[PCredential]))
| PStakingPtr (Term s (PDataList '[PInteger, PInteger, PInteger]))
deriving
Expand All @@ -377,7 +377,7 @@ instance PIsDataRepr PStakingCredential where
DRHCons (f . PStakingHash) $
DRHCons (f . PStakingPtr) DRHNil

newtype PAddress (s :: k)
newtype PAddress (s :: S)
= PAddress
( Term
s
Expand Down Expand Up @@ -405,7 +405,7 @@ instance PIsDataRepr PAddress where

---------- Tx

newtype PTxId (s :: k)
newtype PTxId (s :: S)
= PTxId (Term s (PDataList '[PByteString]))
deriving
(PMatch, PIsData, PUnsafeLiftDecl Plutus.TxId)
Expand All @@ -418,7 +418,7 @@ instance PIsDataRepr PTxId where
pmatchDataRepr dat $
DRHCons (f . PTxId) DRHNil

newtype PTxOutRef (s :: k)
newtype PTxOutRef (s :: S)
= PTxOutRef (Term s (PDataList '[PTxId, PInteger]))
deriving
(PMatch, PIsData, PUnsafeLiftDecl Plutus.TxOutRef)
Expand All @@ -431,7 +431,7 @@ instance PIsDataRepr PTxOutRef where
pmatchDataRepr dat $
DRHCons (f . PTxOutRef) DRHNil

newtype PTxInInfo (s :: k)
newtype PTxInInfo (s :: S)
= PTxInInfo (Term s (PDataList '[PTxOutRef, PTxOut]))
deriving
(PMatch, PIsData, PUnsafeLiftDecl Plutus.TxInfo)
Expand All @@ -444,7 +444,7 @@ instance PIsDataRepr PTxInInfo where
pmatchDataRepr dat $
DRHCons (f . PTxInInfo) DRHNil

newtype PTxOut (s :: k)
newtype PTxOut (s :: S)
= PTxOut
( Term
s
Expand Down Expand Up @@ -472,7 +472,7 @@ instance PIsDataRepr PTxOut where
pmatchDataRepr dat $
DRHCons (f . PTxOut) DRHNil

data PDCert (s :: k)
data PDCert (s :: S)
= PDCertDelegRegKey (Term s (PDataList '[PStakingCredential]))
| PDCertDelegDeRegKey (Term s (PDataList '[PStakingCredential]))
| PDCertDelegDelegate (Term s (PDataList '[PStakingCredential, PPubKeyHash]))
Expand Down Expand Up @@ -508,13 +508,13 @@ instance PIsDataRepr PDCert where

---------- AssocMap

newtype PMap (a :: k -> Type) (b :: k -> Type) (s :: k)
newtype PMap (a :: PType) (b :: PType) (s :: S)
= PMap (Term s (PBuiltinMap a b))
deriving (PIsData) via (PBuiltinMap a b)

---------- Others

data PMaybe a (s :: k)
data PMaybe a (s :: S)
= PNothing (Term s (PDataList '[]))
| PJust (Term s (PDataList '[a]))
deriving
Expand All @@ -531,7 +531,7 @@ instance PIsDataRepr (PMaybe a) where
DRHCons (f . PNothing) $
DRHCons (f . PJust) DRHNil

data PEither a b (s :: k)
data PEither a b (s :: S)
= PLeft (Term s (PDataList '[a]))
| PRight (Term s (PDataList '[b]))
deriving
Expand Down
24 changes: 16 additions & 8 deletions Plutarch/Builtin.hs
Expand Up @@ -33,16 +33,16 @@ import qualified PlutusCore as PLC
import PlutusTx (Data)

-- | Plutus 'BuiltinPair'
data PBuiltinPair (a :: k -> Type) (b :: k -> Type) (s :: k)
data PBuiltinPair (a :: PType) (b :: PType) (s :: S)

-- FIXME: figure out good way of deriving this
instance (PUnsafeLiftDecl ah a, PUnsafeLiftDecl bh b) => PUnsafeLiftDecl (ah, bh) (PBuiltinPair a b) where
type PLiftedRepr (PBuiltinPair a b) = (PLiftedRepr a, PLiftedRepr b)
type PLifted (PBuiltinPair a b) = (PLifted a, PLifted b)
pliftToRepr (x, y) = (pliftToRepr @_ @_ @a x, pliftToRepr @_ @_ @b y)
pliftToRepr (x, y) = (pliftToRepr @_ @a x, pliftToRepr @_ @b y)
pliftFromRepr (x, y) = do
x' <- pliftFromRepr @_ @_ @a x
y' <- pliftFromRepr @_ @_ @b y
x' <- pliftFromRepr @_ @a x
y' <- pliftFromRepr @_ @b y
Just (x', y')

pfstBuiltin :: Term s (PBuiltinPair a b :--> a)
Expand All @@ -59,7 +59,7 @@ ppairDataBuiltin :: Term s (PAsData a :--> PAsData b :--> PBuiltinPair (PAsData
ppairDataBuiltin = punsafeBuiltin PLC.MkPairData

-- | Plutus 'BuiltinList'
data PBuiltinList (a :: k -> Type) (s :: k)
data PBuiltinList (a :: PType) (s :: S)
= PCons (Term s a) (Term s (PBuiltinList a))
| PNil

Expand All @@ -81,8 +81,8 @@ pconsBuiltin = phoistAcyclic $ pforce $ punsafeBuiltin PLC.MkCons
instance PUnsafeLiftDecl ah a => PUnsafeLiftDecl [ah] (PBuiltinList a) where
type PLifted (PBuiltinList a) = [PLifted a]
type PLiftedRepr (PBuiltinList a) = [PLiftedRepr a]
pliftToRepr x = pliftToRepr @_ @_ @a <$> x
pliftFromRepr x = traverse (pliftFromRepr @_ @_ @a) x
pliftToRepr x = pliftToRepr @_ @a <$> x
pliftFromRepr x = traverse (pliftFromRepr @_ @a) x

instance PLift a => PlutusType (PBuiltinList a) where
type PInner (PBuiltinList a) _ = PBuiltinList a
Expand Down Expand Up @@ -150,7 +150,15 @@ pasByteStr = punsafeBuiltin PLC.UnBData
pdataLiteral :: Data -> Term s PData
pdataLiteral = pconstant

data PAsData (a :: k -> Type) (s :: k)
data PAsData (a :: PType) (s :: S)

data PAsDataLifted (a :: PType)

instance PUnsafeLiftDecl (PAsDataLifted a) (PAsData a) where
type PLifted (PAsData a) = PAsDataLifted a
type PLiftedRepr (PAsData a) = Data
pliftToRepr = \case
pliftFromRepr _ = Nothing

pforgetData :: Term s (PAsData a) -> Term s PData
pforgetData = punsafeCoerce
Expand Down
14 changes: 7 additions & 7 deletions Plutarch/DataRepr.hs
Expand Up @@ -28,16 +28,16 @@ import Plutarch.Prelude
import qualified Plutus.V1.Ledger.Api as Ledger
import qualified PlutusCore as PLC

data PDataList (as :: [k -> Type]) (s :: k)
data PDataList (as :: [PType]) (s :: S)

pdhead :: Term s (PDataList (a : as) :--> PAsData a)
pdhead = phoistAcyclic $ pforce $ punsafeBuiltin PLC.HeadList

pdtail :: Term s (PDataList (a : as) :--> PDataList as)
pdtail = phoistAcyclic $ pforce $ punsafeBuiltin PLC.TailList

type PDataRepr :: [[k -> Type]] -> k -> Type
data PDataRepr (defs :: [[k -> Type]]) (s :: k)
type PDataRepr :: [[PType]] -> PType
data PDataRepr (defs :: [[PType]]) (s :: S)

pasData :: Term s (PDataRepr _) -> Term s PData
pasData = punsafeCoerce
Expand Down Expand Up @@ -72,7 +72,7 @@ pindexDataList n =
ind :: Term s PInteger
ind = fromInteger $ toInteger $ natVal n

data DataReprHandlers (out :: k -> Type) (def :: [[k -> Type]]) (s :: k) where
data DataReprHandlers (out :: PType) (def :: [[PType]]) (s :: S) where
DRHNil :: DataReprHandlers out '[] s
DRHCons :: (Term s (PDataList def) -> Term s out) -> DataReprHandlers out defs s -> DataReprHandlers out (def : defs) s

Expand Down Expand Up @@ -122,10 +122,10 @@ pmatchDataRepr d handlers =
handler
$ go common (idx + 1) rest constr

newtype PIsDataReprInstances (a :: k -> Type) (h :: Type) (s :: k) = PIsDataReprInstances (a s)
newtype PIsDataReprInstances (a :: PType) (h :: Type) (s :: S) = PIsDataReprInstances (a s)

class (PMatch a, PIsData a) => PIsDataRepr (a :: k -> Type) where
type PIsDataReprRepr a :: [[k -> Type]]
class (PMatch a, PIsData a) => PIsDataRepr (a :: PType) where
type PIsDataReprRepr a :: [[PType]]
pmatchRepr :: forall s b. Term s (PDataRepr (PIsDataReprRepr a)) -> (a s -> Term s b) -> Term s b

instance PIsDataRepr a => PIsData (PIsDataReprInstances a h) where
Expand Down
2 changes: 1 addition & 1 deletion Plutarch/Either.hs
Expand Up @@ -3,7 +3,7 @@ module Plutarch.Either (PEither (..)) where
import Plutarch (PlutusType (PInner, pcon', pmatch'))
import Plutarch.Prelude

data PEither (a :: k -> Type) (b :: k -> Type) (s :: k) = PLeft (Term s a) | PRight (Term s b)
data PEither (a :: PType) (b :: PType) (s :: S) = PLeft (Term s a) | PRight (Term s b)

instance PlutusType (PEither a b) where
type PInner (PEither a b) c = (a :--> c) :--> (b :--> c) :--> c
Expand Down

0 comments on commit e6994e9

Please sign in to comment.