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

Update PTryFrom instances for Plutarch 1.2 #520

Merged
merged 13 commits into from
Jul 30, 2022
Merged

Update PTryFrom instances for Plutarch 1.2 #520

merged 13 commits into from
Jul 30, 2022

Conversation

danielfarrelly
Copy link
Contributor

Updates based on #438

MangoIV and others added 6 commits April 15, 2022 22:02
- PMaybeData
- PCredential
- PPubKeyHash
- PValidatorHash
- recovering PValue
- recovering all the types that belong to PValue
- test all the recoveries succeed with the correct values with property tests
- new module Extra.ByteSTring that exports convenience functions
@L-as
Copy link
Member

L-as commented Jul 13, 2022

Thanks!

Comment on lines 50 to 62
instance PTryFrom PData (PAsData PTxId) where
type PTryFromExcess PData (PAsData PTxId) = Flip Term PTxId
ptryFrom' opq = runTermCont $ do
opq' <- tcont . plet $ pasConstr # opq
dataBs <- tcont $ \f ->
pif
(pfstBuiltin # opq' #== 0 #&& plength # (psndBuiltin # opq') #== 1)
(f $ phead #$ psndBuiltin # opq')
(ptraceError "ptryFrom(TxId): bad constructor")
unwrapped <- tcont . plet $ ptryFrom @(PAsData PByteString) dataBs snd
tcont $ \f ->
pif (plengthBS # unwrapped #== 28) (f ()) (ptraceError "ptryFrom(TxId): must be 28 bytes long")
pure (punsafeCoerce opq, pcon . PTxId $ pdcons # pdata unwrapped # pdnil)
Copy link
Collaborator

Choose a reason for hiding this comment

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

@L-as Is this the most ergonomic way to implement PTryFrom for data sum stuff currently? Surely users shouldn't be expected to perform structure validation when the target invariant is only for one of the fields within - I assume there is a better solution utilizing PTryFromExcess?

Copy link
Member

Choose a reason for hiding this comment

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

From my other comment:

Optimally you should be able to first verify it's a PDataSum, but because it doesn't return any excess, it's not efficient.

Copy link
Member

@L-as L-as left a comment

Choose a reason for hiding this comment

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

Only a bit remaining!

ptryFrom' opq cont = ptryFrom @(PAsData PTxId) opq (cont . first punsafeCoerce)

instance PTryFrom PData (PAsData PTxId) where
type PTryFromExcess PData (PAsData PTxId) = Flip Term PTxId
Copy link
Member

Choose a reason for hiding this comment

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

There's no point in having this excess, since it's the same as the output.

opq' <- tcont . plet $ pasConstr # opq
dataBs <- tcont $ \f ->
pif
(pfstBuiltin # opq' #== 0 #&& plength # (psndBuiltin # opq') #== 1)
Copy link
Member

Choose a reason for hiding this comment

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

This is inefficient. You're traversing the list twice.

Copy link
Member

Choose a reason for hiding this comment

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

Optimally you should be able to first verify it's a PDataSum, but because it doesn't return any excess, it's not efficient.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems like a big drawback, why doesn't PDataSum return an excess with fields?

Copy link
Member

Choose a reason for hiding this comment

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

(Human) laziness. I'm sure there's a way to do it, but I'd rather rethink the whole excess field bullshit.

@@ -3,6 +3,7 @@
{-# OPTIONS_GHC -Wno-orphans #-}

module Plutarch.Builtin (
Flip,
Copy link
Member

Choose a reason for hiding this comment

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

.…This should not be public API.

Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe we should add it to Extra.TryFrom, otherwise everybody declares their own Flip? Maybe it's not an issue though.

Copy link
Collaborator

Choose a reason for hiding this comment

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

it really does feel weird for everyone to have to define it inline - even if it's supposed to be a "type lambda".

+1 to having it exposed in plutarch-extra at this point

@L-as
Copy link
Member

L-as commented Jul 30, 2022 via email

@L-as L-as merged commit 2f6faa2 into Plutonomicon:staging Jul 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants