| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Cek.Polytropos.Dissect
Description
Replaces O(N) nested Generics sum/product continuation frames with a
single flat cursor over Rep.
The Problem of Generic Dissection Overhead
Standard polynomial functor dissection via GHC's Generic1 introduces deeply
nested tree structures of binary sums (:+:) and binary products (:*:).
For a constructor with \(k\) arguments, the generic representation requires
\(k\) nested Prod2 frame allocations. Each frame allocation costs multiple
heap words and pointer traversals.
Flat Representation Dissection
In Core, every algebraic data type or record constructor is represented
as a single contiguous array (Rep):
data Rep (f :: Type -> Type) a = Rep {-# UNPACK #-} !Word !(SmallArray Any)
PolyDissect exploits this contiguous representation by turning functor dissection
into a single integer cursor (pdCursor) sliding across a SmallArray:
- Slots
[0 .. pdCursor - 1]: Contain evaluated Clowns (c). - Slot
pdCursor: The active evaluation hole currently being reduced. - Slots
[pdCursor + 1 .. n - 1]: Contain unevaluated Jokers (j).
This reduces frame allocation to an unboxed cursor increment, achieving $O(1)$ stepping with optimal cache locality and zero tree-pointer chasing.
Synopsis
- data PolyDissect c j = PolyDissect {
- pdTag :: !Word
- pdCursor :: !Int
- pdSlots :: !(SmallArray (Any :: Type))
- polyStepIn :: Rep f j -> Either (Rep g c) (f j, PolyDissect (g c) (f j))
- polyStepNext :: g c -> PolyDissect (g c) (f j) -> Either (Rep g c) (f j, PolyDissect (g c) (f j))
Flat Dissection Representation
data PolyDissect c j Source #
Universal dissection state for any Rep syntax node.
Invariant: in pdSlots, indices [0 .. pdCursor - 1] hold evaluated
Clowns c, index pdCursor is the active hole, and indices
[pdCursor + 1 .. n - 1] hold unevaluated Jokers j.
Constructors
| PolyDissect | |
Fields
| |
Instances
| Bifunctor PolyDissect Source # | Bifunctor mapping over both evaluated Clowns and unevaluated Jokers. |
Defined in Cek.Polytropos.Dissect Methods bimap :: (a -> b) -> (c -> d) -> PolyDissect a c -> PolyDissect b d first :: (a -> b) -> PolyDissect a c -> PolyDissect b c second :: (b -> c) -> PolyDissect a b -> PolyDissect a c | |
| Functor (PolyDissect c) Source # | Functor mapping over the unevaluated Jokers to the right of |
Defined in Cek.Polytropos.Dissect Methods fmap :: (a -> b) -> PolyDissect c a -> PolyDissect c b (<$) :: a -> PolyDissect c b -> PolyDissect c a | |
Dissection Stepping Operations
polyStepIn :: Rep f j -> Either (Rep g c) (f j, PolyDissect (g c) (f j)) Source #
Step into slot 0 of a Rep syntax node in strictly O(1) time.
If the constructor has zero payload slots (e.g. nullary constructor or empty record),
returns Left immediately with zero heap allocations. Otherwise, initializes
pdCursor at 0 and returns Right with the first child term paired with PolyDissect.
polyStepNext :: g c -> PolyDissect (g c) (f j) -> Either (Rep g c) (f j, PolyDissect (g c) (f j)) Source #
Plug an evaluated Clown g c into pdCursor and advance to pdCursor + 1.
When pdCursor + 1 == sizeofSmallArray pdSlots, all child terms have been evaluated
and the complete reconstructed Rep is returned in Left.
Otherwise, returns Right with the next Joker subterm and updated PolyDissect.