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!
Aside: Not interested in the details? Skip straight to examples!
Overview
Haddock
Haddock documentation of plutus-core
, plutus-ledger-api
, plutus-tx
, and few other upstream library with correctly matched version to latest Plutarch is available:
Compiling and Running
Introduction and Basic Syntax
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
Practical Usage
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
Concepts
The Concepts section details additional concepts.
- Hoisting, metaprogramming, and fundamentals
- What is the
s
? - Data encoding and Scott encoding
- Haskell synonym of Plutarch types
Typeclasses
The Typeclasses section discusses the primary typeclasses related to Plutarch.
PEq
&POrd
PIntegral
PIsData
PlutusType
,PCon
, andPMatch
PListLike
PIsDataRepr
&PDataFields
PTryFrom
Working with Types
The Types section discusses the core types of Plutarch.
PInteger
PBool
PString
PByteString
PUnit
PBuiltinList
PList
PBuiltinPair
PAsData
PDataSum
&PDataRecord
PData
Examples
Rules of thumb, Tips, and Tricks
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
Common Issues and Troubleshooting
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
- Type match errors when using
pfield
/getField
(orOverloadedRecordDot
to access field)