plutarch
Safe HaskellNone
LanguageGHC2021

Plutarch.Internal.Parse

Synopsis

Type class

class PValidateData (a :: S -> Type) where Source #

Describes a Data encoded Plutarch type that requires some additional validation to ensure its structure is indeed what we expect. This is especially useful for datums or other user-supplied arguments, since these can be malformed.

Why the CPS

pwithValidated is written in continuation-passing style (or CPS) for reasons of efficiency. As pwithValidated is meant to check structure (and nothing more), our first instinct would be to write something like

pwithValidated :: Term s PData -> Term s PBool

or

pwithValidated :: Term s PData -> Term s PUnit

and rely on perror to sort things out. However, constructing either PUnit or PBool isn't free, and ultimately, this value ends up unused. At the same time, we want to ensure that the validation specified in pwithValidated is actually performed, which neither of the above signatures can promise anything about.

CPS solves both of these problems. Since the result of pwithValidated is technically a function that must behave the same no matter what type of r it operates over, we can't do anything except potentially mess with the argument PData or error out, which means we don't need to allocate any 'result value'. Furthermore, by working in CPS, we ensure that any validation defined in pwithValidated must happen, even if the PData (or whatever it's supposed to be) is never handled or forced.

Important note

It is essential practice to document what exactly any given instance of PValidateData checks. Each instance should specify this: all the instances provided by Plutarch and its related libraries follow this rule.

Since: 1.12.0

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

Instances

Instances details
PValidateData PBool Source #

Checks that we have a Constr with either 0 or 1 as its tag. The second field of Constr is not checked at all.

Since: 1.12.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

PValidateData PByteString Source #

Checks that we have a B.

Since: 1.12.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

PValidateData PData Source #

Checks (and does) nothing.

Since: 1.12.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

PValidateData PInteger Source #

Checks that we have an I.

Since: 1.12.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

PValidateData PPositive Source #

Checks that we have a positive I.

Since: 1.13.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

PValidateData a => PValidateData (PAsData a) Source #

Since: 1.12.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

(PValidateData a, PValidateData b) => PValidateData (PBuiltinList (PBuiltinPair (PAsData a) (PAsData b))) Source #

Checks that we have a Map. Furthermore, checks that every key-value pair validates as per a and b. Takes precedence over the overlapping PValidateData (PBuiltinList a) instance.

Since: 1.13.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

PValidateData (PBuiltinList PData) Source #

Checks that we have a List.

Since: 1.12.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

PValidateData a => PValidateData (PBuiltinList a) Source #

Checks that we have a List. Furthermore, checks that every element validates as per a.

Since: 1.12.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

PValidateData (Don'tValidate a) Source #

Since: 1.12.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

(Generic (a (Any :: S)), All PInnermostIsDataDataRepr struct, struct ~ UnTermRec struct', Generic (a (Any :: S)), '[struct'] ~ Code (a (Any :: S)), All PValidateData struct, SListI struct) => PValidateData (DeriveAsDataRec a) Source #

Checks that we have a List, that it has (at least) enough elements for each field of a, and that each of those elements, in order, validates as per its respective PValidateData instance.

Since: 1.12.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

(Generic (a (Any :: S)), struct ~ UnTermStruct (a (Any :: S)), All2 PInnermostIsDataDataRepr struct, All2 PValidateData struct, SListI2 struct) => PValidateData (DeriveAsDataStruct a) Source #

Checks that we have a Constr, that its tag is in the range [0, n - 1] (where n is the number of 'arms' in the encoded sum type), and that there are at least enough fields in the second Constr argument, each of which decodes as per that field's PValidateData instance.

Since: 1.12.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

Generic (a (Any :: S)) => PValidateData (DeriveAsTag a) Source #

Checks that we have an I, and that it is in the range [0, n - 1], where n is the number of 'arms' in the encoded sum type.

Since: 1.12.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

(PValidateData a, PValidateData b) => PValidateData (PBuiltinPair (PAsData a) (PAsData b)) Source #

Checks that we have a Constr with a second field of at least length 2. Furthermore, checks that the first element validates as per a, while the second element validates as per b. The Constr tag is not checked at all.

Since: 1.12.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

PValidateData (PBuiltinPair PData PData) Source #

Checks that we have a Constr with a second field of at least length 2. The Constr tag, or the elements, are not checked at all.

Since: 1.12.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

PValidateData b => PValidateData (DeriveNewtypePValidateData a b) Source #

Since: 1.12.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

Function

pparseData :: forall (a :: S -> Type) (s :: S). (PIsData a, PValidateData a) => Term s PData -> Term s (PAsData a) Source #

Given a PData, check that it is, indeed, structured as a expects. If it is, return that same PData 'rewrapped' into PAsData a.

This helper exists to avoid having to work in CPS when writing regular code. It is kept out of PValidateData for efficiency and safety reasons.

Since: 1.12.0

Helper deriving newtype

newtype Don'tValidate (a :: S -> Type) (s :: S) Source #

Helper to define a do-nothing instance of PValidateData. Useful when defining an instance for a complex type where we want to validate some parts, but not others.

Since: 1.12.0

Constructors

Don'tValidate 

Fields

Instances

Instances details
PValidateData (Don'tValidate a) Source #

Since: 1.12.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #

PlutusType a => PlutusType (Don'tValidate a) Source #

Since: 1.12.0

Instance details

Defined in Plutarch.Internal.Parse

Associated Types

type PInner (Don'tValidate a) 
Instance details

Defined in Plutarch.Internal.Parse

Methods

pcon' :: forall (s :: S). Don'tValidate a s -> Term s (PInner (Don'tValidate a)) Source #

pmatch' :: forall (s :: S) (b :: S -> Type). Term s (PInner (Don'tValidate a)) -> (Don'tValidate a s -> Term s b) -> Term s b Source #

type PInner (Don'tValidate a) Source # 
Instance details

Defined in Plutarch.Internal.Parse

newtype DeriveNewtypePValidateData (a :: S -> Type) (b :: S -> Type) (s :: S) Source #

Helper to define an instance of PValidateData for newtypes over Terms, which 'borrows' the PValidateData instance for whatever the newtype is wrapping.

Since: 1.12.0

Constructors

DeriveNewtypePValidateData (a s) 

Instances

Instances details
PValidateData b => PValidateData (DeriveNewtypePValidateData a b) Source #

Since: 1.12.0

Instance details

Defined in Plutarch.Internal.Parse

Methods

pwithValidated :: forall (s :: S). Term s PData -> forall (r :: S -> Type). Term s r -> Term s r Source #