The Plutarch guide is your one-stop shop for getting up to speed on Plutarch!
Note: If you spot any mistakes/have any related questions that this guide lacks the answer to, please don't hesitate to raise an issue. The goal is to have high quality documentation for Plutarch users!
Table of Contents
Aside: Not interested in the details? Skip straight to examples!
- Common Extensions and GHC options
- Evaluation
- Nix binary cache is recommended. Otherwise nix will build from GHC itself.
Use Plutonomicon binary cache via cachix: https://plutonomicon.cachix.org
DO NOT add your user to trusted-users
as cachix use
suggests because this is
insecure.
The Introduction section serves as a introduction to Plutarch's basic concepts and core syntactic constructs. It will help build a mental model of Plutarch, but is insufficient to write production-ready code.
- Overview
- Untyped Plutus Core (UPLC)
- Plutarch Types
- Plutarch
Term
s - Pattern matching constant
Term
s withpmatch
. - Strictness and Laziness; Delayed Terms and Forcing
- References
The Usage section fills in the gaps left by the previous. It illustrates techniques that make Plutarch easier to work with.
- Conditionals
- Recursion
- Using the Plutarch Prelude
- Do syntax with
TermCont
- Do syntax with
QualifiedDo
andPlutarch.Monadic
- Deriving typeclasses for
newtype
s - Deriving typeclasses with generics
plet
to avoid work duplication- Tracing
- Raising errors
- Unsafe functions
- Interoperability with PlutusTx
The Concepts section details additional concepts.
- Hoisting, metaprogramming, and fundamentals
- What is the
s
? - Data encoding and Scott encoding
- Haskell synonym of Plutarch types
The Typeclasses section discusses the primary typeclasses related to Plutarch.
PEq
&POrd
PIntegral
PIsData
PlutusType
,PCon
, andPMatch
PConstant
&PLift
PListLike
PIsDataRepr
&PDataFields
PTryFrom
The Types section discusses the core types of Plutarch.
PInteger
PBool
PString
PByteString
PUnit
PBuiltinList
PList
PBuiltinPair
PTuple
PAsData
PDataSum
&PDataRecord
PData
Also see: examples.
Outside of the fundamental user guide, there are rules of thumb and general guidelines you can follow to improve your Plutarch experience. The Tricks section discusses ways of writing efficient and high quality Plutarch code, as well as rules that can help auditing Plutarch easier.
- Plutarch functions are strict
- Don't duplicate work
- Prefer Plutarch level functions
- When to use Haskell level functions?
- The difference between
PlutusType
/PCon
andPLift
'spconstant
- Let Haskell level functions take responsibility of evaluation
- The isomorphism between
makeIsDataIndexed
, Haskell ADTs, andPIsDataRepr
- Prefer statically building constants whenever possible
- Figuring out the representation of a Plutarch type
- Prefer pattern matching on the result of
pmatch
immediately - Working with bound fields yielded by
pletFields
Due to the highly abstracted nature of Plutarch and its utilization of advanced type level concepts, you might face unfamiliar errors. Don't worry, the guide is here to help!
- No instance for
PUnsafeLiftDecl a
- Couldn't match representation of type: ... arising from the 'deriving' clause
- Infinite loop / Infinite AST
- Couldn't match type
Plutarch.DataRepr.Internal.PUnLabel ...
arising from a use ofpfield
(orgetField
, orpletFields
) - Expected a type, but "fieldName" has kind
GHC.Types.Symbol
- Lifting
PAsData
- Couldn't match type
PLifted (PConstanted Foo)
withFoo
- Type match errors when using
pfield
/getField
(orOverloadedRecordDot
to access field)