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 PIsData and PlutusType instances for PMap and PValue #161

Merged
merged 3 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 10 additions & 6 deletions Plutarch/Api/V1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,11 @@ deriving via
newtype PValue (s :: S) = PValue (Term s (PMap PCurrencySymbol (PMap PTokenName PInteger)))
deriving
( PlutusType
-- FIXME: This requires `PIsData` instance for `PMap`.
-- , PIsData
, PIsData
)
via (DerivePNewtype PValue (PMap PCurrencySymbol (PMap PTokenName PInteger)))

-- FIXME: This fails typecheck.
-- FIXME: This fails typecheck. Representations do not match.
-- instance PUnsafeLiftDecl PValue where type PLifted PValue = Plutus.Value
-- deriving via
-- (DerivePConstantViaNewtype Plutus.Value PValue (PMap PCurrencySymbol (PMap PTokenName PInteger)))
Expand Down Expand Up @@ -572,10 +571,15 @@ instance PIsDataRepr PDCert where

---------- AssocMap

newtype PMap (a :: PType) (b :: PType) (s :: S)
= PMap (Term s (PBuiltinMap a b))
newtype PMap (k :: PType) (v :: PType) (s :: S) = PMap (Term s (PBuiltinMap k v))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@t1lde why did you make a newtype for this BTW?

deriving (PlutusType, PIsData) via (DerivePNewtype (PMap k v) (PBuiltinMap k v))

-- TODO: PMap needs instances for PConstant/PLift, PlutusType, and PIsData.
-- FIXME: This fails typecheck. "Illegal type synonym family application ‘PLifted k’ in instance"
-- instance PUnsafeLiftDecl (PMap k v) where type PLifted (PMap k v) = PlutusMap.Map (PLifted k) (PLifted v)
-- deriving via
-- (DerivePConstantViaNewtype (PlutusMap.Map (PLifted k) (PLifted v)) (PMap k v) (PBuiltinMap k v))
-- instance
-- (PConstant (PlutusMap.Map (PLifted k) (PLifted v)))

---------- Others

Expand Down
24 changes: 11 additions & 13 deletions examples/Examples/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Plutarch
import Plutarch.Api.V1 (
PAddress (PAddress),
PCredential (PScriptCredential),
PCurrencySymbol,
PPubKeyHash,
PScriptContext (PScriptContext),
PScriptPurpose (PSpending),
Expand All @@ -17,7 +18,7 @@ import Plutarch.Api.V1 (
PValue,
)
import Plutarch.Bool (pif)
import Plutarch.Builtin (PAsData, PBuiltinList, pdata, pfromData)
import Plutarch.Builtin (PAsData, PBuiltinList, pdata, pfromData, pfstBuiltin)
import Plutarch.DataRepr (pindexDataList)
import Plutarch.Lift (pconstant, plift)
import Plutarch.List (pelem, phead)
Expand Down Expand Up @@ -117,8 +118,8 @@ getTxInfo = plam $ \x -> P.do
PScriptContext c <- pmatch x
pindexDataList (Proxy @0) # c

_getMint :: Term s (PTxInfo :--> PAsData PValue)
_getMint = plam $ \x -> P.do
getMint :: Term s (PTxInfo :--> PAsData PValue)
getMint = plam $ \x -> P.do
PTxInfo i <- pmatch x
pindexDataList (Proxy @3) # i

Expand All @@ -139,11 +140,10 @@ getValidator =
PScriptCredential v <- pmatch (pfromData $ pindexDataList (Proxy @0) # a)
pindexDataList (Proxy @0) # v

-- FIXME: 'PMap' needs 'PlutusType' instance.
---- | Get first CurrencySymbol from Value
-- getSym :: Term s (PValue :--> PAsData PCurrencySymbol)
-- getSym =
-- plam $ \v -> pfstBuiltin #$ phead # pinner (pinner v)
-- | Get first CurrencySymbol from Value
getSym :: Term s (PValue :--> PAsData PCurrencySymbol)
getSym =
plam $ \v -> pfstBuiltin #$ phead # pto (pto v)

checkSignatory :: Term s (PPubKeyHash :--> PScriptContext :--> PUnit)
checkSignatory = plam $ \ph ctx -> P.do
Expand Down Expand Up @@ -172,11 +172,9 @@ tests =
testCase "getting validator" $ do
plift (pfromData $ getValidator #$ pfromData $ getInputs #$ pfromData $ getTxInfo # ctx)
@?= validator
, -- FIXME: Need 'PlutusType' etc. instance for 'PMap'
-- , testCase "getting sym" $ do
-- plift (pfromData $ getSym #$ pfromData $ getMint #$ pfromData $ getTxInfo # ctx)
-- @?= sym
testCase "signatory validator" $ do
, testCase "getting sym" $ do
plift (pfromData $ getSym #$ pfromData $ getMint #$ pfromData $ getTxInfo # ctx) @?= sym
, testCase "signatory validator" $ do
() <$ traverse (\x -> succeeds $ checkSignatory # pconstant x # ctx) signatories
fails $ checkSignatory # pconstant "41" # ctx
]
Expand Down