| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Cek.Core.Dissect
Description
Automatically derives small-step continuation frames for any syntax functor
with a Generic1 instance, separating evaluated values (Clowns, left of the hole)
from unevaluated terms (Jokers, right of the hole).
Theoretical Foundation: Functor Dissection
Conor McBride's classic functional pearl "Clowns to the left of me, Jokers to the right" (POPL 2008) showed that the continuation frames of small-step abstract machines (such as the CEK, SECD, and Krivine machines) are precisely the dissections of syntax functors.
For any polynomial functor \(F(X)\), its dissection is a bifunctor \(F'(C, J)\) where:
- \(C\) represents Clowns: values in subterms to the left of the active evaluation hole that have already been evaluated to weak-head or full normal forms.
- \(J\) represents Jokers: terms in subterms to the right of the active evaluation hole that remain unevaluated in their initial syntactic state.
By instantiating dissection generically over GHC's Generic1 representation, any AST
functor equipped with DeriveGeneric1 automatically inherits correct, type-safe
small-step evaluation context frames without manual AST-traversal boilerplate.
Machine Strategy Taxonomy
To support diverse evaluation semantics across the language spectrum (e.g. Scheme's strict Call-by-Value, Dhall's Strong Call-by-Need, and Mercury's backtracking search), the abstract machine is parameterized along three orthogonal axes:
BindPolicy: How variable bindings are captured and evaluated.NormDepth: How deeply subterms are evaluated (WHNF vs full normal form).ControlMode: How continuations and branching are managed.
Synopsis
- class (Functor f, Bifunctor (Dissect f)) => Dissectable (f :: Type -> Type) where
- class Bifunctor (GDissectRep rep) => GDissect (rep :: Type -> Type) where
- type GDissectRep (rep :: Type -> Type) :: Type -> Type -> Type
- gStepIn :: rep j -> Either (rep c) (j, GDissectRep rep c j)
- gStepNext :: c -> GDissectRep rep c j -> Either (rep c) (j, GDissectRep rep c j)
- newtype GenericDissect (rep :: Type -> Type) c j = GenericDissect (GDissectRep rep c j)
- data Void2 c j
- data Unit2 c j = Unit2
- data Sum2 (d1 :: Type -> Type -> Type) (d2 :: Type -> Type -> Type) c j
- data Prod2 (f :: Type -> Type) (g :: Type -> Type) (df :: Type -> Type -> Type) (dg :: Type -> Type -> Type) c j
- data ListDissect c j = ListDissect ![c] ![j]
- data BindPolicy
- data NormDepth
- data ControlMode
- data MachineProfile = Profile {
- profBind :: !BindPolicy
- profDepth :: !NormDepth
- profControl :: !ControlMode
Dissection Type Classes
class (Functor f, Bifunctor (Dissect f)) => Dissectable (f :: Type -> Type) where Source #
Class of syntax functors admitting small-step left-to-right dissection.
Any syntax functor f that can be dissected yields an associated bifunctor
Dissect f c j representing the one-hole evaluation context where:
cis the type of evaluated Clowns (values to the left of the hole).jis the type of unevaluated Jokers (terms to the right of the hole).
Minimal complete definition
Nothing
Associated Types
type Dissect (f :: Type -> Type) :: Type -> Type -> Type Source #
Associated dissection bifunctor representing continuation frames.
type Dissect (f :: Type -> Type) = GenericDissect (Rep1 f)
Methods
stepIn :: f j -> Either (f c) (j, Dissect f c j) Source #
Enter the leftmost child hole of a syntax node.
Returns Left with the node intact if it contains no child holes (i.e. is a leaf value),
or Right with the first child term j paired with the continuation frame.
default stepIn :: (Generic1 f, GDissect (Rep1 f), Dissect f ~ GenericDissect (Rep1 f)) => f j -> Either (f c) (j, Dissect f c j) Source #
stepNext :: c -> Dissect f c j -> Either (f c) (j, Dissect f c j) Source #
Plug an evaluated value c into the active hole and advance to the next hole.
If all child holes have been filled with evaluated values, returns Left with the
completed node. Otherwise, returns Right with the next unevaluated child term j
and the updated continuation frame.
Instances
| Dissectable [] Source # | Dissectable instance for standard lists '[]'. Traverses elements from head to tail, evaluating each element in sequence. | ||||
Defined in Cek.Core.Dissect Associated Types
| |||||
class Bifunctor (GDissectRep rep) => GDissect (rep :: Type -> Type) where Source #
Generic dissection algebra over GHC's Rep1 representation types.
Defines inductive rules for computing the derivative of sums, products, and constants.
Associated Types
type GDissectRep (rep :: Type -> Type) :: Type -> Type -> Type Source #
Representation of the one-hole context for this structural representation.
Methods
gStepIn :: rep j -> Either (rep c) (j, GDissectRep rep c j) Source #
Step into the leftmost subterm of this representation.
gStepNext :: c -> GDissectRep rep c j -> Either (rep c) (j, GDissectRep rep c j) Source #
Supply a value to the current hole and step to the next subterm.
Instances
| GDissect Par1 Source # | Dissection of Stepping in immediately finds the single hole, yielding | ||||
Defined in Cek.Core.Dissect Associated Types
Methods gStepIn :: Par1 j -> Either (Par1 c) (j, GDissectRep Par1 c j) Source # gStepNext :: c -> GDissectRep Par1 c j -> Either (Par1 c) (j, GDissectRep Par1 c j) Source # | |||||
| GDissect (U1 :: Type -> Type) Source # | Dissection of Since there are no fields, stepping in immediately returns 'Left U1'. | ||||
Defined in Cek.Core.Dissect Associated Types
Methods gStepIn :: U1 j -> Either (U1 c) (j, GDissectRep (U1 :: Type -> Type) c j) Source # gStepNext :: c -> GDissectRep (U1 :: Type -> Type) c j -> Either (U1 c) (j, GDissectRep (U1 :: Type -> Type) c j) Source # | |||||
| Dissectable f => GDissect (Rec1 f) Source # | Dissection through container functor 'Rec1 f'. Delegates dissection directly to the inner | ||||
Defined in Cek.Core.Dissect Associated Types
Methods gStepIn :: Rec1 f j -> Either (Rec1 f c) (j, GDissectRep (Rec1 f) c j) Source # gStepNext :: c -> GDissectRep (Rec1 f) c j -> Either (Rec1 f c) (j, GDissectRep (Rec1 f) c j) Source # | |||||
| (Functor f, Functor g, GDissect f, GDissect g) => GDissect (f :*: g) Source # | Dissection of pair product 'f :*: g'. Implements standard left-to-right applicative order evaluation across pairs of fields. | ||||
Defined in Cek.Core.Dissect Associated Types
Methods gStepIn :: (f :*: g) j -> Either ((f :*: g) c) (j, GDissectRep (f :*: g) c j) Source # gStepNext :: c -> GDissectRep (f :*: g) c j -> Either ((f :*: g) c) (j, GDissectRep (f :*: g) c j) Source # | |||||
| (GDissect f, GDissect g) => GDissect (f :+: g) Source # | Dissection of constructor sum 'f :+: g'. Distributes over sum alternatives: evaluates within the chosen constructor branch. | ||||
Defined in Cek.Core.Dissect Associated Types
Methods gStepIn :: (f :+: g) j -> Either ((f :+: g) c) (j, GDissectRep (f :+: g) c j) Source # gStepNext :: c -> GDissectRep (f :+: g) c j -> Either ((f :+: g) c) (j, GDissectRep (f :+: g) c j) Source # | |||||
| GDissect (K1 i a :: Type -> Type) Source # | Dissection of Constant fields (such as variable names, primitive integers, strings) cannot be stepped into. | ||||
Defined in Cek.Core.Dissect Associated Types
Methods gStepIn :: K1 i a j -> Either (K1 i a c) (j, GDissectRep (K1 i a :: Type -> Type) c j) Source # gStepNext :: c -> GDissectRep (K1 i a :: Type -> Type) c j -> Either (K1 i a c) (j, GDissectRep (K1 i a :: Type -> Type) c j) Source # | |||||
| GDissect f => GDissect (M1 i meta f) Source # | Dissection through metadata wrappers Passes through directly to the underlying representation. | ||||
Defined in Cek.Core.Dissect Associated Types
Methods gStepIn :: M1 i meta f j -> Either (M1 i meta f c) (j, GDissectRep (M1 i meta f) c j) Source # gStepNext :: c -> GDissectRep (M1 i meta f) c j -> Either (M1 i meta f c) (j, GDissectRep (M1 i meta f) c j) Source # | |||||
newtype GenericDissect (rep :: Type -> Type) c j Source #
Wrapper providing a Bifunctor instance for Generic1-derived dissections.
Constructors
| GenericDissect (GDissectRep rep c j) |
Instances
| Bifunctor (GDissectRep rep) => Bifunctor (GenericDissect rep) Source # | |
Defined in Cek.Core.Dissect Methods bimap :: (a -> b) -> (c -> d) -> GenericDissect rep a c -> GenericDissect rep b d first :: (a -> b) -> GenericDissect rep a c -> GenericDissect rep b c second :: (b -> c) -> GenericDissect rep a b -> GenericDissect rep a c | |
| Bifunctor (GDissectRep rep) => Functor (GenericDissect rep c) Source # | |
Defined in Cek.Core.Dissect Methods fmap :: (a -> b) -> GenericDissect rep c a -> GenericDissect rep c b (<$) :: a -> GenericDissect rep c b -> GenericDissect rep c a | |
Dissection Representation Bifunctors
Empty dissection bifunctor for nullary constructors and constant fields.
Singleton dissection frame for a single recursive child term.
Constructors
| Unit2 |
data Sum2 (d1 :: Type -> Type -> Type) (d2 :: Type -> Type -> Type) c j Source #
Sum dissection: disjoint union of two alternative continuation frames.
data Prod2 (f :: Type -> Type) (g :: Type -> Type) (df :: Type -> Type -> Type) (dg :: Type -> Type -> Type) c j Source #
Leibniz product dissection:
By the product rule \(\partial(F \times G) = (\partial F \times G) + (F \times \partial G)\), evaluation proceeds in two phases:
Constructors
| StepL !(df c j) !(g j) | The left factor is currently being stepped into; right factor is unevaluated. |
| StepR !(f c) !(dg c j) | The left factor is complete (all Clowns); the right factor is being stepped into. |
data ListDissect c j Source #
Dissection of a variable-arity list '[r]' into reversed Clowns and remaining Jokers.
Accumulates evaluated elements in reverse order for O(1) consing, reversing on completion.
Constructors
| ListDissect ![c] ![j] |
Instances
| Bifunctor ListDissect Source # | |
Defined in Cek.Core.Dissect Methods bimap :: (a -> b) -> (c -> d) -> ListDissect a c -> ListDissect b d first :: (a -> b) -> ListDissect a c -> ListDissect b c second :: (b -> c) -> ListDissect a b -> ListDissect a c | |
| Functor (ListDissect c) Source # | |
Defined in Cek.Core.Dissect Methods fmap :: (a -> b) -> ListDissect c a -> ListDissect c b (<$) :: a -> ListDissect c b -> ListDissect c a | |
| (Eq c, Eq j) => Eq (ListDissect c j) Source # | |
Defined in Cek.Core.Dissect Methods (==) :: ListDissect c j -> ListDissect c j -> Bool (/=) :: ListDissect c j -> ListDissect c j -> Bool | |
| (Show c, Show j) => Show (ListDissect c j) Source # | |
Defined in Cek.Core.Dissect Methods showsPrec :: Int -> ListDissect c j -> ShowS show :: ListDissect c j -> String showList :: [ListDissect c j] -> ShowS | |
Abstract Machine Classification Axes
data BindPolicy Source #
Axis 1: Variable binding evaluation strategy.
Determines when and how sub-expressions assigned to variables are evaluated:
Constructors
| BindCBV | Strict Call-by-Value: arguments are evaluated to values before function application (e.g. standard Scheme, ML, OCaml, and Strict Haskell). |
| BindCBN | Pure Call-by-Name: arguments are passed unevaluated as closures and re-evaluated on every variable reference (e.g. Algol 60, pure lambda-calculus). |
| BindNeed | Strong Call-by-Need / Lazy: arguments are passed as shared thunks and evaluated at most once on first reference, updating the cell in-place (e.g. Haskell, Dhall). |
Instances
| Eq BindPolicy Source # | |
Defined in Cek.Core.Dissect | |
| Ord BindPolicy Source # | |
Defined in Cek.Core.Dissect Methods compare :: BindPolicy -> BindPolicy -> Ordering (<) :: BindPolicy -> BindPolicy -> Bool (<=) :: BindPolicy -> BindPolicy -> Bool (>) :: BindPolicy -> BindPolicy -> Bool (>=) :: BindPolicy -> BindPolicy -> Bool max :: BindPolicy -> BindPolicy -> BindPolicy min :: BindPolicy -> BindPolicy -> BindPolicy | |
| Show BindPolicy Source # | |
Defined in Cek.Core.Dissect Methods showsPrec :: Int -> BindPolicy -> ShowS show :: BindPolicy -> String showList :: [BindPolicy] -> ShowS | |
Axis 2: Normalization depth.
Determines whether reduction stops at top-level constructors or proceeds recursively:
Constructors
| DepthWHNF | Weak-Head Normal Form: evaluation stops at the outermost constructor, lambda, or primitive literal. Subterms under lambdas or constructor arguments are not forced. |
| DepthNF | Full Normal Form: evaluation reduces terms completely under binders and constructors. Essential for type-level languages, proof assistants, and Dhall's semantic hashing. |
data ControlMode Source #
Axis 3: Continuation and state-space branching discipline.
Governs the execution topology and multi-shot continuation capabilities:
Constructors
| DetOneShot | Deterministic single-shot execution: the continuation stack is strictly linear and discarded upon return or unhandled exception. |
| MultiShotCtl | First-class multi-shot continuations: allows capturing continuations as values
(e.g. Scheme's |
| NondetAAM | Abstract Accounting Machine / Relational Search: continuation frames branch across choice-points, supporting backtracking (e.g. Prolog, Mercury, Kanren). |
Instances
| Eq ControlMode Source # | |
Defined in Cek.Core.Dissect | |
| Ord ControlMode Source # | |
Defined in Cek.Core.Dissect Methods compare :: ControlMode -> ControlMode -> Ordering (<) :: ControlMode -> ControlMode -> Bool (<=) :: ControlMode -> ControlMode -> Bool (>) :: ControlMode -> ControlMode -> Bool (>=) :: ControlMode -> ControlMode -> Bool max :: ControlMode -> ControlMode -> ControlMode min :: ControlMode -> ControlMode -> ControlMode | |
| Show ControlMode Source # | |
Defined in Cek.Core.Dissect Methods showsPrec :: Int -> ControlMode -> ShowS show :: ControlMode -> String showList :: [ControlMode] -> ShowS | |
data MachineProfile Source #
Compile-time profile specializing the CEK/CESK abstract machine.
Encapsulates the complete behavioral profile of an interpreter engine.
Constructors
| Profile | |
Fields
| |
Instances
| Eq MachineProfile Source # | |
Defined in Cek.Core.Dissect Methods (==) :: MachineProfile -> MachineProfile -> Bool (/=) :: MachineProfile -> MachineProfile -> Bool | |
| Ord MachineProfile Source # | |
Defined in Cek.Core.Dissect Methods compare :: MachineProfile -> MachineProfile -> Ordering (<) :: MachineProfile -> MachineProfile -> Bool (<=) :: MachineProfile -> MachineProfile -> Bool (>) :: MachineProfile -> MachineProfile -> Bool (>=) :: MachineProfile -> MachineProfile -> Bool max :: MachineProfile -> MachineProfile -> MachineProfile min :: MachineProfile -> MachineProfile -> MachineProfile | |
| Show MachineProfile Source # | |
Defined in Cek.Core.Dissect Methods showsPrec :: Int -> MachineProfile -> ShowS show :: MachineProfile -> String showList :: [MachineProfile] -> ShowS | |