cek-core
Safe HaskellSafe-Inferred
LanguageHaskell2010

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:

  1. BindPolicy: How variable bindings are captured and evaluated.
  2. NormDepth: How deeply subterms are evaluated (WHNF vs full normal form).
  3. ControlMode: How continuations and branching are managed.
Synopsis

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:

  • c is the type of evaluated Clowns (values to the left of the hole).
  • j is 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.

default stepNext :: (Generic1 f, GDissect (Rep1 f), Dissect f ~ GenericDissect (Rep1 f)) => c -> Dissect f c j -> Either (f c) (j, Dissect f c j) Source #

Instances

Instances details
Dissectable [] Source #

Dissectable instance for standard lists '[]'.

Traverses elements from head to tail, evaluating each element in sequence.

Instance details

Defined in Cek.Core.Dissect

Associated Types

type Dissect [] 
Instance details

Defined in Cek.Core.Dissect

Methods

stepIn :: [j] -> Either [c] (j, Dissect [] c j) Source #

stepNext :: c -> Dissect [] c j -> Either [c] (j, Dissect [] c j) Source #

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

Instances details
GDissect Par1 Source #

Dissection of Par1 (the recursive parameter hole).

Stepping in immediately finds the single hole, yielding Unit2.

Instance details

Defined in Cek.Core.Dissect

Associated Types

type GDissectRep Par1 
Instance details

Defined in Cek.Core.Dissect

type GDissectRep Par1 = Unit2

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 U1 (empty constructor without fields).

Since there are no fields, stepping in immediately returns 'Left U1'.

Instance details

Defined in Cek.Core.Dissect

Associated Types

type GDissectRep (U1 :: Type -> Type) 
Instance details

Defined in Cek.Core.Dissect

type GDissectRep (U1 :: Type -> Type) = Void2

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 Dissectable container functor f.

Instance details

Defined in Cek.Core.Dissect

Associated Types

type GDissectRep (Rec1 f) 
Instance details

Defined in Cek.Core.Dissect

type GDissectRep (Rec1 f) = Dissect f

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.

Instance details

Defined in Cek.Core.Dissect

Associated Types

type GDissectRep (f :*: g) 
Instance details

Defined in Cek.Core.Dissect

type GDissectRep (f :*: g) = Prod2 f g (GDissectRep f) (GDissectRep g)

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.

Instance details

Defined in Cek.Core.Dissect

Associated Types

type GDissectRep (f :+: g) 
Instance details

Defined in Cek.Core.Dissect

type GDissectRep (f :+: g) = Sum2 (GDissectRep f) (GDissectRep g)

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 K1 (constant data field not parameterized by the recursive term).

Constant fields (such as variable names, primitive integers, strings) cannot be stepped into.

Instance details

Defined in Cek.Core.Dissect

Associated Types

type GDissectRep (K1 i a :: Type -> Type) 
Instance details

Defined in Cek.Core.Dissect

type GDissectRep (K1 i a :: Type -> Type) = Void2

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 M1.

Passes through directly to the underlying representation.

Instance details

Defined in Cek.Core.Dissect

Associated Types

type GDissectRep (M1 i meta f) 
Instance details

Defined in Cek.Core.Dissect

type GDissectRep (M1 i meta f) = GDissectRep f

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

Instances details
Bifunctor (GDissectRep rep) => Bifunctor (GenericDissect rep) Source # 
Instance details

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 # 
Instance details

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

data Void2 c j Source #

Empty dissection bifunctor for nullary constructors and constant fields.

Instances

Instances details
Bifunctor Void2 Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

bimap :: (a -> b) -> (c -> d) -> Void2 a c -> Void2 b d

first :: (a -> b) -> Void2 a c -> Void2 b c

second :: (b -> c) -> Void2 a b -> Void2 a c

Functor (Void2 c) Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

fmap :: (a -> b) -> Void2 c a -> Void2 c b

(<$) :: a -> Void2 c b -> Void2 c a

data Unit2 c j Source #

Singleton dissection frame for a single recursive child term.

Constructors

Unit2 

Instances

Instances details
Bifunctor Unit2 Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

bimap :: (a -> b) -> (c -> d) -> Unit2 a c -> Unit2 b d

first :: (a -> b) -> Unit2 a c -> Unit2 b c

second :: (b -> c) -> Unit2 a b -> Unit2 a c

Functor (Unit2 c) Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

fmap :: (a -> b) -> Unit2 c a -> Unit2 c b

(<$) :: a -> Unit2 c b -> Unit2 c a

Eq (Unit2 c j) Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

(==) :: Unit2 c j -> Unit2 c j -> Bool

(/=) :: Unit2 c j -> Unit2 c j -> Bool

Show (Unit2 c j) Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

showsPrec :: Int -> Unit2 c j -> ShowS

show :: Unit2 c j -> String

showList :: [Unit2 c j] -> ShowS

data Sum2 (d1 :: Type -> Type -> Type) (d2 :: Type -> Type -> Type) c j Source #

Sum dissection: disjoint union of two alternative continuation frames.

Constructors

InL2 !(d1 c j) 
InR2 !(d2 c j) 

Instances

Instances details
(Bifunctor d1, Bifunctor d2) => Bifunctor (Sum2 d1 d2) Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

bimap :: (a -> b) -> (c -> d) -> Sum2 d1 d2 a c -> Sum2 d1 d2 b d

first :: (a -> b) -> Sum2 d1 d2 a c -> Sum2 d1 d2 b c

second :: (b -> c) -> Sum2 d1 d2 a b -> Sum2 d1 d2 a c

(Bifunctor d1, Bifunctor d2) => Functor (Sum2 d1 d2 c) Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

fmap :: (a -> b) -> Sum2 d1 d2 c a -> Sum2 d1 d2 c b

(<$) :: a -> Sum2 d1 d2 c b -> Sum2 d1 d2 c a

(Eq (d1 c j), Eq (d2 c j)) => Eq (Sum2 d1 d2 c j) Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

(==) :: Sum2 d1 d2 c j -> Sum2 d1 d2 c j -> Bool

(/=) :: Sum2 d1 d2 c j -> Sum2 d1 d2 c j -> Bool

(Show (d1 c j), Show (d2 c j)) => Show (Sum2 d1 d2 c j) Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

showsPrec :: Int -> Sum2 d1 d2 c j -> ShowS

show :: Sum2 d1 d2 c j -> String

showList :: [Sum2 d1 d2 c j] -> ShowS

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:

  1. StepL: stepping inside the left factor df while the right factor g j waits as a Joker.
  2. StepR: the left factor f c is fully evaluated to Clowns, and stepping proceeds in dg.

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.

Instances

Instances details
(Functor f, Functor g, Bifunctor df, Bifunctor dg) => Bifunctor (Prod2 f g df dg) Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

bimap :: (a -> b) -> (c -> d) -> Prod2 f g df dg a c -> Prod2 f g df dg b d

first :: (a -> b) -> Prod2 f g df dg a c -> Prod2 f g df dg b c

second :: (b -> c) -> Prod2 f g df dg a b -> Prod2 f g df dg a c

(Functor g, Bifunctor df, Bifunctor dg) => Functor (Prod2 f g df dg c) Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

fmap :: (a -> b) -> Prod2 f g df dg c a -> Prod2 f g df dg c b

(<$) :: a -> Prod2 f g df dg c b -> Prod2 f g df dg c a

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

Instances details
Bifunctor ListDissect Source # 
Instance details

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 # 
Instance details

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 # 
Instance details

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 # 
Instance details

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

Instances details
Eq BindPolicy Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

(==) :: BindPolicy -> BindPolicy -> Bool

(/=) :: BindPolicy -> BindPolicy -> Bool

Ord BindPolicy Source # 
Instance details

Defined in Cek.Core.Dissect

Show BindPolicy Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

showsPrec :: Int -> BindPolicy -> ShowS

show :: BindPolicy -> String

showList :: [BindPolicy] -> ShowS

data NormDepth Source #

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.

Instances

Instances details
Eq NormDepth Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

(==) :: NormDepth -> NormDepth -> Bool

(/=) :: NormDepth -> NormDepth -> Bool

Ord NormDepth Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

compare :: NormDepth -> NormDepth -> Ordering

(<) :: NormDepth -> NormDepth -> Bool

(<=) :: NormDepth -> NormDepth -> Bool

(>) :: NormDepth -> NormDepth -> Bool

(>=) :: NormDepth -> NormDepth -> Bool

max :: NormDepth -> NormDepth -> NormDepth

min :: NormDepth -> NormDepth -> NormDepth

Show NormDepth Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

showsPrec :: Int -> NormDepth -> ShowS

show :: NormDepth -> String

showList :: [NormDepth] -> ShowS

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 call/cc, delimited control operators shift/reset).

NondetAAM

Abstract Accounting Machine / Relational Search: continuation frames branch across choice-points, supporting backtracking (e.g. Prolog, Mercury, Kanren).

Instances

Instances details
Eq ControlMode Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

(==) :: ControlMode -> ControlMode -> Bool

(/=) :: ControlMode -> ControlMode -> Bool

Ord ControlMode Source # 
Instance details

Defined in Cek.Core.Dissect

Show ControlMode Source # 
Instance details

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

Instances details
Eq MachineProfile Source # 
Instance details

Defined in Cek.Core.Dissect

Ord MachineProfile Source # 
Instance details

Defined in Cek.Core.Dissect

Show MachineProfile Source # 
Instance details

Defined in Cek.Core.Dissect

Methods

showsPrec :: Int -> MachineProfile -> ShowS

show :: MachineProfile -> String

showList :: [MachineProfile] -> ShowS