| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Plutarch.Internal.Parse
Synopsis
- class PValidateData (a :: S -> Type) where
- pparseData :: forall (a :: S -> Type) (s :: S). (PIsData a, PValidateData a) => Term s PData -> Term s (PAsData a)
- newtype Don'tValidate (a :: S -> Type) (s :: S) = Don'tValidate {
- unDon'tValidate :: a s
- newtype DeriveNewtypePValidateData (a :: S -> Type) (b :: S -> Type) (s :: S) = DeriveNewtypePValidateData (a s)
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
| PValidateData PBool Source # | Checks that we have a Since: 1.12.0 |
| PValidateData PByteString Source # | Checks that we have a Since: 1.12.0 |
| PValidateData PData Source # | Checks (and does) nothing. Since: 1.12.0 |
| PValidateData PInteger Source # | Checks that we have an Since: 1.12.0 |
| PValidateData PPositive Source # | Checks that we have a positive Since: 1.13.0 |
| PValidateData a => PValidateData (PAsData a) Source # | Since: 1.12.0 |
| (PValidateData a, PValidateData b) => PValidateData (PBuiltinList (PBuiltinPair (PAsData a) (PAsData b))) Source # | Checks that we have a Since: 1.13.0 |
| PValidateData (PBuiltinList PData) Source # | Checks that we have a Since: 1.12.0 |
| PValidateData a => PValidateData (PBuiltinList a) Source # | Checks that we have a Since: 1.12.0 |
| PValidateData (Don'tValidate a) Source # | Since: 1.12.0 |
| (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 Since: 1.12.0 |
| (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 Since: 1.12.0 |
| Generic (a (Any :: S)) => PValidateData (DeriveAsTag a) Source # | Checks that we have an Since: 1.12.0 |
| (PValidateData a, PValidateData b) => PValidateData (PBuiltinPair (PAsData a) (PAsData b)) Source # | Checks that we have a Since: 1.12.0 |
| PValidateData (PBuiltinPair PData PData) Source # | Checks that we have a Since: 1.12.0 |
| PValidateData b => PValidateData (DeriveNewtypePValidateData a b) Source # | Since: 1.12.0 |
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
| PValidateData (Don'tValidate a) Source # | Since: 1.12.0 | ||||
| PlutusType a => PlutusType (Don'tValidate a) Source # | Since: 1.12.0 | ||||
Defined in Plutarch.Internal.Parse Associated Types
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 # | |||||
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
| PValidateData b => PValidateData (DeriveNewtypePValidateData a b) Source # | Since: 1.12.0 |