Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
This module defines functions, associated type families, and newtypes for use with
[DerivingVia
](https:/ryanglscott.github.iopapers/deriving-via.pdf) to allow
Plutarch to convert to and from PTypes and Haskell types.
Synopsis
- pconstant :: forall p s. PLift p => PLifted p -> Term s p
- plift :: forall p. (HasCallStack, PLift p) => ClosedTerm p -> PLifted p
- plift' :: forall p. PUnsafeLiftDecl p => Config -> ClosedTerm p -> Either LiftError (PLifted p)
- data LiftError
- class (PUnsafeLiftDecl (PConstanted h), DefaultUni `Includes` PConstantRepr h) => PConstantDecl (h :: Type) where
- type PConstantRepr h :: Type
- type PConstanted h :: PType
- pconstantToRepr :: h -> PConstantRepr h
- pconstantFromRepr :: PConstantRepr h -> Maybe h
- type PLift = PUnsafeLiftDecl
- type PConstant a = (a ~ PLifted (PConstanted a), PConstantDecl a)
- newtype DerivePConstantDirect (h :: Type) (p :: PType) = DerivePConstantDirect h
- newtype DerivePConstantViaNewtype (h :: Type) (p :: PType) (p' :: PType) = DerivePConstantViaNewtype h
- newtype DerivePConstantViaBuiltin (h :: Type) (p :: PType) (p' :: PType) = DerivePConstantViaBuiltin h
- class (PConstantDecl (PLifted p), PConstanted (PLifted p) ~ p) => PUnsafeLiftDecl (p :: PType) where
Converstion between Plutarch terms and Haskell types
pconstant :: forall p s. PLift p => PLifted p -> Term s p Source #
Create a Plutarch-level constant, from a Haskell value. Example: > pconstant @PInteger 42
plift :: forall p. (HasCallStack, PLift p) => ClosedTerm p -> PLifted p Source #
Like plift
` but throws on failure.
plift' :: forall p. PUnsafeLiftDecl p => Config -> ClosedTerm p -> Either LiftError (PLifted p) Source #
Convert a Plutarch term to the associated Haskell value. Fail otherwise. This will fully evaluate the arbitrary closed expression, and convert the resulting value.
Error during script evaluation.
Since: 1.2.1
Define your own conversion
class (PUnsafeLiftDecl (PConstanted h), DefaultUni `Includes` PConstantRepr h) => PConstantDecl (h :: Type) where Source #
Class of Haskell types h
that can be represented as a Plutus core builtin
and converted to a Plutarch type.
The Plutarch type is determined by `PConstanted h`. Its Plutus Core representation is given by `PConstantRepr h`.
This typeclass is closely tied with PLift
.
Laws:
- pconstantFromRepr . pconstantToRepr ≡ Just
- (pconstantToRepr $) . pconstantFromRepr ≡ Just
- plift . pfromData . flip ptryFrom fst . pconstant . PlutusTx.toData ≡ id
- PlutusTx.fromData . plift . pforgetData . pdata . pconstant ≡ Just
These laws must be upheld for the sake of soundness of the type system.
type PConstantRepr h :: Type Source #
type PConstanted h :: PType Source #
pconstantToRepr :: h -> PConstantRepr h Source #
pconstantFromRepr :: PConstantRepr h -> Maybe h Source #
Instances
type PLift = PUnsafeLiftDecl Source #
Class of Plutarch types p
that can be converted to/from a Haskell type.
The Haskell type is determined by `PLifted p`.
This typeclass is closely tied with PConstant
.
type PConstant a = (a ~ PLifted (PConstanted a), PConstantDecl a) Source #
Type synonym to simplify deriving of PConstant
via DerivePConstantViaNewtype
.
A newtype Foo a
is considered Constantable if:
- The wrapped type
a
has aPConstant
instance. - The lifted type of
a
has aPUnsafeLiftDecl
instance. - There is type equality between
a
andPLifted (PConstanted a)
.
These constraints are sufficient to derive a PConstant
instance for the newtype.
For deriving PConstant
for a wrapped type represented in UPLC as Data
, see
DerivePConstantViaData
.
newtype DerivePConstantDirect (h :: Type) (p :: PType) Source #
Newtype wrapper for deriving PConstant
when the wrapped type is directly
represented by a builtin UPLC type that is not Data
.
Ex: PInteger
is directly represented as a builtin integer.
Instances
(PLift p, Includes DefaultUni h) => PConstantDecl (DerivePConstantDirect h p) Source # | |
Defined in Plutarch.Lift type PConstantRepr (DerivePConstantDirect h p) Source # type PConstanted (DerivePConstantDirect h p) :: PType Source # pconstantToRepr :: DerivePConstantDirect h p -> PConstantRepr (DerivePConstantDirect h p) Source # pconstantFromRepr :: PConstantRepr (DerivePConstantDirect h p) -> Maybe (DerivePConstantDirect h p) Source # | |
type PConstantRepr (DerivePConstantDirect h p) Source # | |
Defined in Plutarch.Lift | |
type PConstanted (DerivePConstantDirect h p) Source # | |
Defined in Plutarch.Lift |
newtype DerivePConstantViaNewtype (h :: Type) (p :: PType) (p' :: PType) Source #
Newtype wrapper for deriving PConstant
when the wrapped type is represented
indirectly by a builtin UPLC type that is not Data
.
Ex: PPubKeyHash
is a newtype to a PByteString
and PByteString
is directly
represented as a builtin bytestring.
Polymorphic types can be derived as follows:
newtype Foo a = Foo a newtype PFoo a s = PFoo (Term s a) instance forall a. PLift a => PUnsafeLiftDecl (PFoo a) where type PLifted (PFoo a) = Foo (PLifted a) deriving via ( DerivePConstantViaNewtype (Foo a) (PFoo (PConstanted a)) (PConstanted a) ) instance PConstant a => PConstantDecl (Foo a)
DerivePConstantViaNewtype h | The Haskell newtype we are deriving a |
Instances
(PLift p, PLift p', Coercible h (PLifted p')) => PConstantDecl (DerivePConstantViaNewtype h p p') Source # | |
Defined in Plutarch.Lift type PConstantRepr (DerivePConstantViaNewtype h p p') Source # type PConstanted (DerivePConstantViaNewtype h p p') :: PType Source # pconstantToRepr :: DerivePConstantViaNewtype h p p' -> PConstantRepr (DerivePConstantViaNewtype h p p') Source # pconstantFromRepr :: PConstantRepr (DerivePConstantViaNewtype h p p') -> Maybe (DerivePConstantViaNewtype h p p') Source # | |
type PConstantRepr (DerivePConstantViaNewtype h p p') Source # | |
Defined in Plutarch.Lift | |
type PConstanted (DerivePConstantViaNewtype h p p') Source # | |
Defined in Plutarch.Lift |
newtype DerivePConstantViaBuiltin (h :: Type) (p :: PType) (p' :: PType) Source #
Instances
(PLift p, PLift p', Coercible h h', ToBuiltin' (PLifted p') h', FromBuiltin' h' (PLifted p')) => PConstantDecl (DerivePConstantViaBuiltin h p p') Source # | |
Defined in Plutarch.Lift type PConstantRepr (DerivePConstantViaBuiltin h p p') Source # type PConstanted (DerivePConstantViaBuiltin h p p') :: PType Source # pconstantToRepr :: DerivePConstantViaBuiltin h p p' -> PConstantRepr (DerivePConstantViaBuiltin h p p') Source # pconstantFromRepr :: PConstantRepr (DerivePConstantViaBuiltin h p p') -> Maybe (DerivePConstantViaBuiltin h p p') Source # | |
type PConstantRepr (DerivePConstantViaBuiltin h p p') Source # | |
Defined in Plutarch.Lift | |
type PConstanted (DerivePConstantViaBuiltin h p p') Source # | |
Defined in Plutarch.Lift |
Internal use
class (PConstantDecl (PLifted p), PConstanted (PLifted p) ~ p) => PUnsafeLiftDecl (p :: PType) Source #
Laws:
- It must be that PConstantRepr (PLifted p)
when encoded as a constant
in UPLC (via the Constant
constructor) is a valid p
.