| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
PlutusCore.Unroll
Description
Static loop unrolling utilities using type-level Peano numbers.
This module provides utilities for statically unrolling loops at compile time. GHC resolves the type class instances at compile time, resulting in unrolled code that avoids runtime overhead.
See Note [Static loop unrolling] for details on the technique.
Peano numbers
Peano representation of natural numbers for type-level recursion.
type family NatToPeano (n ∷ Nat) ∷ Peano where ... Source #
Equations
| NatToPeano 0 = 'Z | |
| NatToPeano n = 'S (NatToPeano (n - 1)) |
Statically unrolled list drop
dropN ∷ ∀ n a. Drop (NatToPeano n) ⇒ [a] → Maybe [a] Source #
Drop exactly n elements from a list, returning Nothing if too short.
The drop count is statically unrolled at compile time via instance resolution.
dropN @3 [1,2,3,4,5] == Just [4,5] dropN @3 [1,2] == Nothing
Statically unrolled effectful iteration
class Applicative f ⇒ UpwardsM f n where Source #
Statically unrolled effectful iteration over a range.
upwardsM i k means k i *> k (i + 1) *> ... *> k (i + n - 1).
We use this to statically unroll loops through instance resolution. This makes validation benchmarks a couple of percent faster.