Haskell Plutus : Lift, UnsafeLift, and safeLift
π Haskell Plutus : Lift, UnsafeLift, and safeLift
Lift, UnsafeLift, and safeLiftTable of Contents
π Introduction to Lifting
π§±
LiftTypeclass: The Bridge from Haskell to Plutus Coreβ οΈ
UnsafeLift: Risky but Flexibleπ‘οΈ
safeLift: Your Safety Netπ§ͺ Example: Custom Type Lifting
π§ Quiz Yourself
π Glossary of Terms
π§© Summary and Best Practices
1. π Introduction to Lifting
In Plutus, lifting refers to the process of converting Haskell values into Plutus Core constants so they can be used inside on-chain scripts. This is crucial because:
Plutus Core (on-chain) has a limited set of primitive types.
Haskell (off-chain) has a much richer type system.
β‘οΈ Lifting makes Haskell data available inside Plutus scripts.
2. π§± Lift Typeclass: The Bridge from Haskell to Plutus Core
Lift Typeclass: The Bridge from Haskell to Plutus CoreThe Lift typeclass in Plutus defines how a Haskell value is converted (lifted) into a Plutus Core term.
π¦ Module
π§Ύ Class Definition
β
Usage
You derive Lift for your types so that they can be compiled into Plutus scripts:
This allows Color to be used in on-chain logic.
3. β οΈ UnsafeLift: Risky but Flexible
UnsafeLift: Risky but FlexibleThe UnsafeLift typeclass is a more permissive lifting mechanism introduced in newer Plutus versions.
π¬ Why βUnsafeβ?
It doesnβt guarantee that the lifted representation will preserve semantics safely.
Itβs used internally by
LiftandsafeLift, but shouldnβt be relied on directly unless you know what you're doing.
π§Ύ Class
π‘ Usually, you donβt use this directly.
4. π‘οΈ safeLift: Your Safety Net
safeLift: Your Safety NetThe safeLift function uses the UnsafeLift instance but adds compile-time guarantees using Template Haskell.
β
Safer Interface
This is the preferred way to lift Haskell values inside Template Haskell expressions.
π‘ Example
5. π§ͺ Example: Custom Type Lifting
Let's walk through lifting a custom record type:
π§Ύ Type Definition
β
Derive Lift
π Lift a Value
This allows User to be safely passed into on-chain logic.
6. π§ Quiz Yourself
β Which of the following is the safest way to lift a value?
A.
lift valueB.makeUnsafeLift valueC.safeLift valueD.compile value
Answer: β
C. safeLift value
7. π Glossary of Terms
Lift
A typeclass allowing Haskell values to be embedded in Plutus Core
PlutusTx
Module containing TH-based tools for Plutus
Template Haskell (TH)
Meta-programming in Haskell to generate code at compile-time
CompiledCode
A wrapper type representing compiled Plutus Core
BuiltinByteString
Optimized byte strings for on-chain Plutus logic
Q
The Template Haskell monad used for generating code
TExp
A typed Template Haskell expression
UnsafeLift
An internal or low-level way to lift values without checks
safeLift
A safer way to lift using Template Haskell and UnsafeLift
8. π§© Summary and Best Practices
Use safeLift for all Template Haskell-based lifting
Use UnsafeLift unless you really know what youβre doing
Derive Lift with makeLift or makeLiftData
Manually implement Lift unless needed
Keep lifted types small and serializable
Lift large Haskell types with complex dependencies
Use BuiltinByteString, Integer, etc. for compatibility
Use String, Int, or complex types directly
Last updated