Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
Synopsis
- data ((a :: PType) :--> (b :: PType)) (s :: S)
- type ClosedTerm (a :: PType) = forall (s :: S). Term s a
- compile :: Config -> ClosedTerm a -> Either Text Script
- newtype Script = Script (Program DeBruijn DefaultUni DefaultFun ())
- type Dig = Digest Blake2b_160
- hashTerm :: Config -> ClosedTerm a -> Either Text Dig
- papp :: HasCallStack => Term s (a :--> b) -> Term s a -> Term s b
- pdelay :: Term s a -> Term s (PDelayed a)
- data PDelayed (a :: PType) (s :: S)
- perror :: Term s a
- pforce :: Term s (PDelayed a) -> Term s a
- phoistAcyclic :: HasCallStack => ClosedTerm a -> Term s a
- plet :: Term s a -> (Term s a -> Term s b) -> Term s b
- pthrow :: HasCallStack => Text -> Term s a
- data Term (s :: S) (a :: PType)
- data S
- type PType = S -> Type
- (#$) :: HasCallStack => Term s (a :--> b) -> Term s a -> Term s b
- (#) :: HasCallStack => Term s (a :--> b) -> Term s a -> Term s b
- class PlutusType (a :: PType)
- type family PInner a :: PType
- pcon :: PlutusType a => a s -> Term s a
- pmatch :: PlutusType a => Term s a -> (a s -> Term s b) -> Term s b
- type PCon = PlutusType
- type PMatch = PlutusType
- prettyTerm :: Config -> ClosedTerm a -> Doc ()
- prettyScript :: Script -> Doc ()
- printTerm :: HasCallStack => Config -> ClosedTerm a -> String
- printScript :: Script -> String
- pinl :: Term s a -> (Term s a -> Term s b) -> Term s b
- pto :: Term s a -> Term s (PInner a)
- pfix :: Term s (((a :--> b) :--> (a :--> b)) :--> (a :--> b))
- newtype POpaque s = POpaque (Term s POpaque)
- popaque :: Term s a -> Term s POpaque
- plam :: forall c. (PLamN a b s, HasCallStack) => (Term s c -> a) -> Term s (c :--> b)
- newtype TermCont :: forall (r :: PType). S -> Type -> Type where
- hashOpenTerm :: Term s a -> TermCont s Dig
- $sel:runTermCont:TermCont :: TermCont @r s a -> (a -> Term s r) -> Term s r
- unTermCont :: TermCont @a s (Term s a) -> Term s a
- newtype Config = Config {}
- data TracingMode
- pgetConfig :: (Config -> Term s a) -> Term s a
- newtype PForall (b :: a -> PType) s = PForall (forall (x :: a). Term s (b x))
- data PSome (b :: a -> PType) s = forall (x :: a). PSome (Term s (b x))
- newtype PScottEncoded a r s = PScottEncoded (Term s (ScottFn (ScottList a r) r))
- data PlutusTypeScott
- data PlutusTypeNewtype
- class (PInner a ~ DerivedPInner (DPTStrat a) a, PlutusTypeStrat (DPTStrat a), PlutusTypeStratConstraint (DPTStrat a) a, PlutusType a) => DerivePlutusType (a :: PType)
- type family DPTStrat a :: Type
- class (forall t. PCovariant'' t => PCovariant'' (a t)) => PCovariant a
- type family PCovariant' a :: Constraint
- class (forall t. PCovariant'' t => PContravariant'' (a t)) => PContravariant a
- type family PContravariant' a :: Constraint
- class (forall t. PVariant'' t => PVariant'' (a t)) => PVariant a
- type family PVariant' a :: Constraint
Documentation
data ((a :: PType) :--> (b :: PType)) (s :: S) infixr 0 Source #
Instances
PlutusType (a :--> b) Source # | |
Defined in Plutarch.Internal.PlutusType | |
type PContravariant' (a :--> b) Source # | |
Defined in Plutarch.Internal.PlutusType | |
type PCovariant' (a :--> b) Source # | |
Defined in Plutarch.Internal.PlutusType | |
type PInner (a :--> b) Source # | |
Defined in Plutarch.Internal.PlutusType | |
type PVariant' (a :--> b) Source # | |
Defined in Plutarch.Internal.PlutusType |
type ClosedTerm (a :: PType) = forall (s :: S). Term s a Source #
- Closed* terms with no free variables.
compile :: Config -> ClosedTerm a -> Either Text Script Source #
Compile a (closed) Plutus Term to a usable script
Instances
Generic Script Source # | |
Show Script Source # | |
Eq Script Source # | |
type Rep Script Source # | |
Defined in Plutarch.Script type Rep Script = D1 ('MetaData "Script" "Plutarch.Script" "plutarch-1.3.0-287WdR4V4qr3NTh5i3Je4I" 'True) (C1 ('MetaCons "Script" 'PrefixI 'True) (S1 ('MetaSel ('Just "unScript") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Program DeBruijn DefaultUni DefaultFun ())))) |
type Dig = Digest Blake2b_160 Source #
Plutus 'error'.
When using this explicitly, it should be ensured that the containing term is delayed, avoiding premature evaluation.
pforce :: Term s (PDelayed a) -> Term s a Source #
Plutus 'force',
used to force evaluation of PDelayed
terms.
phoistAcyclic :: HasCallStack => ClosedTerm a -> Term s a Source #
plet :: Term s a -> (Term s a -> Term s b) -> Term s b Source #
Let bindings.
This is appoximately a shorthand for a lambda and application:
plet v f
== papp (plam f) v
But sufficiently small terms in WHNF may be inlined for efficiency.
data Term (s :: S) (a :: PType) Source #
Instances
Instances
PEq (PUnit :: S -> Type) Source # | |
POrd (PUnit :: S -> Type) Source # | |
Defined in Plutarch.Unit | |
PPartialOrd (PUnit :: S -> Type) Source # | |
PIsData (PUnit :: S -> Type) Source # | |
PlutusType (PUnit :: S -> Type) Source # | |
Defined in Plutarch.Unit | |
PUnsafeLiftDecl (PUnit :: S -> Type) Source # | |
PShow (PUnit :: S -> Type) Source # | |
Monoid (Term s (PUnit :: S -> Type)) Source # | |
Semigroup (Term s (PUnit :: S -> Type)) Source # | |
type PContravariant' (PUnit :: S -> Type) Source # | |
Defined in Plutarch.Unit | |
type PCovariant' (PUnit :: S -> Type) Source # | |
Defined in Plutarch.Unit | |
type PInner (PUnit :: S -> Type) Source # | |
type PVariant' (PUnit :: S -> Type) Source # | |
Defined in Plutarch.Unit | |
type PLifted (PUnit :: S -> Type) Source # | |
Defined in Plutarch.Unit |
(#) :: HasCallStack => Term s (a :--> b) -> Term s a -> Term s b infixl 8 Source #
High precedence infixl synonym of papp
, to be used like
function juxtaposition. e.g.:
>>>
f # x # y
f x y
class PlutusType (a :: PType) Source #
Instances
type family PInner a :: PType Source #
Instances
pcon :: PlutusType a => a s -> Term s a Source #
Construct a Plutarch Term via a Haskell datatype
pmatch :: PlutusType a => Term s a -> (a s -> Term s b) -> Term s b Source #
Pattern match over Plutarch Terms via a Haskell datatype
type PCon = PlutusType Source #
Deprecated: Use PlutusType
type PMatch = PlutusType Source #
Deprecated: Use PlutusType
prettyTerm :: Config -> ClosedTerm a -> Doc () Source #
Prettify a Plutarch term.
This will call error
if there's a compilation failure. Use prettyTerm'
for a non-partial version.
Example ==
import Plutarch.Prelude
import Plutarch.Api.V1
import Plutarch.Extra.TermCont
checkSignatory :: Term s (PPubKeyHash :--> PScriptContext :--> PUnit)
checkSignatory = plam $ ph ctx' -> unTermCont $ do
ctx <- pletFieldsC ["txInfo", "purpose"] ctx'
purph <- pmatchC ctx.purpose
pure $ case purph of
PSpending _ ->
let signatories = pfield
"signatories" # ctx.txInfo
in pif
(pelem # pdata ph # pfromData signatories)
-- Success!
(pconstant ())
-- Signature not present.
perror
_ -> ptraceError "checkSignatoryCont: not a spending tx"
Prettification result:
let frSndPair = !!sndPair unDataSum = (xF -> frSndPair (unConstrData xF)) frTailList = !tailList frHeadList = !headList frIfThenElse = !ifThenElse in (oP4ECBT qsrxlF0Y7 -> let cjlB6yrGk = unDataSum qsrxlF0Y7 cRFO = unConstrData (frHeadList (frTailList cjlB6yrGk)) cs9iR = !!fstPair cRFO w4 = frSndPair cRFO in if equalsInteger 1 cs9iR then if (vModHwqYB -> let blM6d67 = (x5sad ePDSInSEC -> !(!!chooseList ePDSInSEC ~False ~(if equalsData (frHeadList ePDSInSEC) vModHwqYB then True else x5sad (frTailList ePDSInSEC)))) mC = (jfZs -> blM6d67 (itzT -> jfZs jfZs itzT)) in blM6d67 (ispwp_oeT -> mC mC ispwp_oeT)) (bData oP4ECBT) (unListData let q6X3 = frHeadList cjlB6yrGk in frHeadList let olbZ = unDataSum q6X3 in frTailList (frTailList (frTailList (frTailList (frTailList (frTailList (frTailList olbZ))))))) then () else ERROR else !(!trace "checkSignatoryCont: not a spending tx" ~ERROR))
Semantics ==
Constants ===
- Builtin integers are printed as regular integers. [0-9]+
- Builtin bytestrings are printed in hex notation, prefixed by `0x`. 0x[0-9a-f]+/i
- Builtin strings are printed as is.
- Builtin unit is printed as the unit literal. ()
- Builtin booleans are printed as the literal
True
orFalse
. - Builtin lists are prettified as list literals, i.e delimited with `[` and `]`.
- Builtin pairs are prettified as 2-ary tuple literals, e.g. `(a, b)`.
I
data (i.e data encoded integers) are prettified like builtin integers with a#
prefix. #[0-9]+B
data (i.e data encoded bytestrings) are prettified like builtin bytestrings with a#
prefix. #0x[0-9a-f]+/iList
data (i.e data encoded lists) are prettified like builtin lists with a#
prefix.Map
data is printed like record literals. Delimited by `{` and `}`.
Each key value pair is prettified like key = value and multiple pairs are joined with `,`.
For example, `Map [(I 42, I 0), (I 100, I 1)]` is prettified as `{ #42 = #0, #100 = #1 }` - Constr data has two core elements in its prettified form:
- The constructor index, prettified as an integer prefixed with
Σ
(sigma). - Its fields, prettified as a list.
These two elements are then joined with a .
(period).
For example, `Constr 1 [I 42]` is prettified as "Σ1.[#42]".
Builtin functions ===
Builtin functions are prettified into their name, in title case.
Forced term ===
Forced terms are prefixed with a !
. The unary operator !
has higher fixity than function application.
Delayed term ===
Delayed terms are prefixed with a ~
. The unary operator ~
has higher fixity than function application.
Var ===
Random names are generated for all variable bindings, and these names are used to refer to them.
Names are always unique, between 1 and 8 characters in length, and begin with a lowercase letter.
Names may consist of alphanumeric characters, underscore, or single quotes.
LamAbs ===
Lambdas are prettified similar to haskell lambdas, i.e `x -> ...`.
Lambdas with multiple arguments are detected and simplified: `x y z -> ...`.
Apply ===
Application is, simply, a space - just like haskell. `f x`.
Multi arg applications to the same function are detected and simplified: `f x y`.
Error term ===
perror
is represented by the literal ERROR
.
Special handling ===
To achieve better prettification, certain AST structures are given special handling logic.
- The AST structure produced by
plet
(SingleApply
+LamAbs
pair) is prettified into Haskell-like let bindings. - Lazy ifthenelse (
pif
in particular, notpif
`) is detected and prettified into Haskell-like syntax: `if cond then expr1 else expr2`.
Chains of ifthenelse are nested:
if cond then expr1 else if cond then expr2 else expr3
- When generating names for bindings, well known structures are identified and given special names.
This machinery is made to be extensible in the future.
For example, the structure of the pfix
function is well known and constant - so it is simply called fix
in the output.
Bindings to forced builtin functions inherit the builtin function name, prefixed with a fr
.
prettyScript :: Script -> Doc () Source #
prettyTerm
for pre-compiled Script
s.
printTerm :: HasCallStack => Config -> ClosedTerm a -> String Source #
Prettyprint a Term via the PLC pretty printer
TODO: Heavily improve. It's unreadable right now.
We could convert the de Bruijn indices into names with:
show . prettyPlcReadableDef . (\(Right p) -> p) . Scripts.mkTermToEvaluate . compile $ term
printScript :: Script -> String Source #
Prettyprint a compiled Script via the PLC pretty printer
pto :: Term s a -> Term s (PInner a) Source #
Safely coerce from a Term to it's PInner
representation.
pfix :: Term s (((a :--> b) :--> (a :--> b)) :--> (a :--> b)) Source #
Fixpoint recursion. Used to encode recursive functions.
Example:
iterateN' :: Term s (PInteger :--> (a :--> a) :--> a :--> a) -> Term s PInteger -> Term s (a :--> a) -> Term s a iterateN' self n f x = pif (n #== 0) x (self # n - 1 #$ f x) iterateN :: Term s (PInteger :--> (a :--> a) :--> a :--> a) iterateN = pfix #$ plam iterateN'
Further examples can be found in examples/Recursion.hs
An Arbitrary Term with an unknown type
Instances
PlutusType POpaque Source # | |
Defined in Plutarch.Internal.Other | |
type PContravariant' POpaque Source # | |
Defined in Plutarch.Internal.Other | |
type PCovariant' POpaque Source # | |
Defined in Plutarch.Internal.Other | |
type PInner POpaque Source # | |
Defined in Plutarch.Internal.Other | |
type PVariant' POpaque Source # | |
Defined in Plutarch.Internal.Other |
newtype TermCont :: forall (r :: PType). S -> Type -> Type where Source #
Instances
MonadFail (TermCont s) Source # | |
Applicative (TermCont s) Source # | |
Defined in Plutarch.TermCont pure :: a -> TermCont s a Source # (<*>) :: TermCont s (a -> b) -> TermCont s a -> TermCont s b Source # liftA2 :: (a -> b -> c) -> TermCont s a -> TermCont s b -> TermCont s c Source # (*>) :: TermCont s a -> TermCont s b -> TermCont s b Source # (<*) :: TermCont s a -> TermCont s b -> TermCont s a Source # | |
Functor (TermCont s) Source # | |
Monad (TermCont s) Source # | |
newtype PForall (b :: a -> PType) s Source #
Instances
PlutusType (PForall f) Source # | |
Defined in Plutarch.Internal.PlutusType | |
type PContravariant' (PForall f) Source # | |
Defined in Plutarch.Internal.PlutusType | |
type PCovariant' (PForall f) Source # | |
Defined in Plutarch.Internal.PlutusType | |
type PInner (PForall f) Source # | |
Defined in Plutarch.Internal.PlutusType | |
type PVariant' (PForall f) Source # | |
Defined in Plutarch.Internal.PlutusType |
data PSome (b :: a -> PType) s Source #
Instances
PlutusType (PSome f) Source # | |
Defined in Plutarch.Internal.PlutusType | |
type PContravariant' (PSome f) Source # | |
Defined in Plutarch.Internal.PlutusType | |
type PCovariant' (PSome f) Source # | |
Defined in Plutarch.Internal.PlutusType | |
type PInner (PSome f) Source # | |
Defined in Plutarch.Internal.PlutusType | |
type PVariant' (PSome f) Source # | |
Defined in Plutarch.Internal.PlutusType |
newtype PScottEncoded a r s Source #
PScottEncoded (Term s (ScottFn (ScottList a r) r)) |
Instances
PlutusType (PScottEncoded a r) Source # | |
Defined in Plutarch.Internal.ScottEncoding type PInner (PScottEncoded a r) :: PType Source # type PCovariant' (PScottEncoded a r) Source # type PContravariant' (PScottEncoded a r) Source # type PVariant' (PScottEncoded a r) Source # pcon' :: forall (s :: S). PScottEncoded a r s -> Term s (PInner (PScottEncoded a r)) Source # pmatch' :: forall (s :: S) (b :: PType). Term s (PInner (PScottEncoded a r)) -> (PScottEncoded a r s -> Term s b) -> Term s b Source # | |
type PContravariant' (PScottEncoded a r) Source # | |
Defined in Plutarch.Internal.ScottEncoding | |
type PCovariant' (PScottEncoded a r) Source # | |
Defined in Plutarch.Internal.ScottEncoding | |
type PInner (PScottEncoded a r) Source # | |
Defined in Plutarch.Internal.ScottEncoding | |
type PVariant' (PScottEncoded a r) Source # | |
Defined in Plutarch.Internal.ScottEncoding |
data PlutusTypeScott Source #
Instances
PlutusTypeStrat PlutusTypeScott Source # | |
Defined in Plutarch.Internal.ScottEncoding type PlutusTypeStratConstraint PlutusTypeScott :: PType -> Constraint Source # type DerivedPInner PlutusTypeScott a :: PType Source # derivedPCon :: forall a (s :: S). (DerivePlutusType a, DPTStrat a ~ PlutusTypeScott) => a s -> Term s (DerivedPInner PlutusTypeScott a) Source # derivedPMatch :: forall a (s :: S) (b :: PType). (DerivePlutusType a, DPTStrat a ~ PlutusTypeScott) => Term s (DerivedPInner PlutusTypeScott a) -> (a s -> Term s b) -> Term s b Source # | |
type PlutusTypeStratConstraint PlutusTypeScott Source # | |
Defined in Plutarch.Internal.ScottEncoding | |
type DerivedPInner PlutusTypeScott a Source # | |
Defined in Plutarch.Internal.ScottEncoding |
data PlutusTypeNewtype Source #
Instances
PlutusTypeStrat PlutusTypeNewtype Source # | |
Defined in Plutarch.Internal.Newtype type PlutusTypeStratConstraint PlutusTypeNewtype :: PType -> Constraint Source # type DerivedPInner PlutusTypeNewtype a :: PType Source # derivedPCon :: forall a (s :: S). (DerivePlutusType a, DPTStrat a ~ PlutusTypeNewtype) => a s -> Term s (DerivedPInner PlutusTypeNewtype a) Source # derivedPMatch :: forall a (s :: S) (b :: PType). (DerivePlutusType a, DPTStrat a ~ PlutusTypeNewtype) => Term s (DerivedPInner PlutusTypeNewtype a) -> (a s -> Term s b) -> Term s b Source # | |
type PlutusTypeStratConstraint PlutusTypeNewtype Source # | |
Defined in Plutarch.Internal.Newtype | |
type DerivedPInner PlutusTypeNewtype a Source # | |
Defined in Plutarch.Internal.Newtype |
class (PInner a ~ DerivedPInner (DPTStrat a) a, PlutusTypeStrat (DPTStrat a), PlutusTypeStratConstraint (DPTStrat a) a, PlutusType a) => DerivePlutusType (a :: PType) Source #
Instances
type family DPTStrat a :: Type Source #
Instances
class (forall t. PCovariant'' t => PCovariant'' (a t)) => PCovariant a Source #
Instances
(forall (t :: PType). PCovariant'' t => PCovariant'' (a t)) => PCovariant a Source # | |
Defined in Plutarch.Internal.PlutusType |
type family PCovariant' a :: Constraint Source #
Instances
class (forall t. PCovariant'' t => PContravariant'' (a t)) => PContravariant a Source #
Instances
(forall (t :: PType). PCovariant'' t => PContravariant'' (a t)) => PContravariant a Source # | |
Defined in Plutarch.Internal.PlutusType |
type family PContravariant' a :: Constraint Source #
Instances
class (forall t. PVariant'' t => PVariant'' (a t)) => PVariant a Source #
Instances
(forall (t :: PType). PVariant'' t => PVariant'' (a t)) => PVariant a Source # | |
Defined in Plutarch.Internal.PlutusType |
type family PVariant' a :: Constraint Source #