cek-core
Copyright(c) Nadia Yvette Chambers 2026
LicenseEUPL-1.2
MaintainerNadia Chambers <nadia.yvette.chambers@ik.me>
Stabilityexperimental
Portabilitynon-portable (GHC extensions)
Safe HaskellSafe-Inferred
LanguageHaskell2010

Cek.Core

Description

Implements the classical Felleisen-Friedman (1986/1987) Control-Environment-Continuation (CEK) abstract machine for call-by-value evaluation of functional expressions.

Architectural Context

Per Note [Felleisen-Friedman CEK Abstract Machine] [ref:note-core-architecture], this module models evaluation state transitions as an explicit 3-tuple \(\langle C, E, K \rangle\): 1. Control (\(C\)): The active term undergoing reduction (Term). 2. Environment (\(E\)): Lexical scope mapping free variables to values (Env). 3. Continuation (\(K\)): Explicit evaluation context frames eliminating host stack recursion (Cont).

Transitions

  • Variable: \(\langle x, E, K \rangle \Longrightarrow \text{applyCont}(K, E(x))\)
  • Abstraction: \(\langle \lambda x. e, E, K \rangle \Longrightarrow \text{applyCont}(K, \text{Closure}(x, e, E))\)
  • Application: \(\langle e_1 \; e_2, E, K \rangle \Longrightarrow \langle e_1, E, \text{FrameArg}(e_2, E) : K \rangle\)
  • Argument Evaluated: \(\text{applyCont}(\text{FrameArg}(e_2, E) : K, v_1) \Longrightarrow \langle e_2, E, \text{FrameFun}(v_1) : K \rangle\)
  • Function Applied: \(\text{applyCont}(\text{FrameFun}(\text{Closure}(x, e, E')) : K, v_2) \Longrightarrow \langle e, E'[x \mapsto v_2], K \rangle\)
Synopsis

Abstract Syntax

data Term Source #

Core functional term syntax.

Constructors

Var !Text

Variable identifier

Lam !Text !Term

Anonymous lambda abstraction (\(\lambda x. e\))

App !Term !Term

Function application (\(e_1 \; e_2\))

Lit !Literal

Embedded literal constant

Let !Text !Term !Term

Let-binding (let x = e1 in e2)

Instances

Instances details
Eq Term Source # 
Instance details

Defined in Cek.Core

Methods

(==) :: Term -> Term -> Bool

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

Generic Term Source # 
Instance details

Defined in Cek.Core

Associated Types

type Rep Term 
Instance details

Defined in Cek.Core

type Rep Term = D1 ('MetaData "Term" "Cek.Core" "cek-core-0.1.0.0-inplace" 'False) ((C1 ('MetaCons "Var" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)) :+: C1 ('MetaCons "Lam" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term))) :+: (C1 ('MetaCons "App" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term)) :+: (C1 ('MetaCons "Lit" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Literal)) :+: C1 ('MetaCons "Let" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term))))))

Methods

from :: Term -> Rep Term x

to :: Rep Term x -> Term

Show Term Source # 
Instance details

Defined in Cek.Core

Methods

showsPrec :: Int -> Term -> ShowS

show :: Term -> String

showList :: [Term] -> ShowS

type Rep Term Source # 
Instance details

Defined in Cek.Core

type Rep Term = D1 ('MetaData "Term" "Cek.Core" "cek-core-0.1.0.0-inplace" 'False) ((C1 ('MetaCons "Var" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)) :+: C1 ('MetaCons "Lam" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term))) :+: (C1 ('MetaCons "App" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term)) :+: (C1 ('MetaCons "Lit" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Literal)) :+: C1 ('MetaCons "Let" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term))))))

data Literal Source #

Primitive literal values supported by the base CEK machine.

Constructors

LitNat !Word64

Natural numbers

LitInt !Integer

Arbitrary-precision integers

LitBool !Bool

Boolean values

LitText !Text

Text literals

Instances

Instances details
Eq Literal Source # 
Instance details

Defined in Cek.Core

Methods

(==) :: Literal -> Literal -> Bool

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

Generic Literal Source # 
Instance details

Defined in Cek.Core

Associated Types

type Rep Literal 
Instance details

Defined in Cek.Core

type Rep Literal = D1 ('MetaData "Literal" "Cek.Core" "cek-core-0.1.0.0-inplace" 'False) ((C1 ('MetaCons "LitNat" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Word64)) :+: C1 ('MetaCons "LitInt" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Integer))) :+: (C1 ('MetaCons "LitBool" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Bool)) :+: C1 ('MetaCons "LitText" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text))))

Methods

from :: Literal -> Rep Literal x

to :: Rep Literal x -> Literal

Show Literal Source # 
Instance details

Defined in Cek.Core

Methods

showsPrec :: Int -> Literal -> ShowS

show :: Literal -> String

showList :: [Literal] -> ShowS

type Rep Literal Source # 
Instance details

Defined in Cek.Core

type Rep Literal = D1 ('MetaData "Literal" "Cek.Core" "cek-core-0.1.0.0-inplace" 'False) ((C1 ('MetaCons "LitNat" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Word64)) :+: C1 ('MetaCons "LitInt" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Integer))) :+: (C1 ('MetaCons "LitBool" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Bool)) :+: C1 ('MetaCons "LitText" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text))))

Value Domain & Environment

data Value Source #

Evaluated runtime values.

Constructors

VClosure !Text !Term !Env

Lexical closure \(\langle x, e, E \rangle\)

VLit !Literal

Primitive literal value

Instances

Instances details
Eq Value Source # 
Instance details

Defined in Cek.Core

Methods

(==) :: Value -> Value -> Bool

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

Generic Value Source # 
Instance details

Defined in Cek.Core

Associated Types

type Rep Value 
Instance details

Defined in Cek.Core

type Rep Value = D1 ('MetaData "Value" "Cek.Core" "cek-core-0.1.0.0-inplace" 'False) (C1 ('MetaCons "VClosure" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Env))) :+: C1 ('MetaCons "VLit" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Literal)))

Methods

from :: Value -> Rep Value x

to :: Rep Value x -> Value

Show Value Source # 
Instance details

Defined in Cek.Core

Methods

showsPrec :: Int -> Value -> ShowS

show :: Value -> String

showList :: [Value] -> ShowS

type Rep Value Source # 
Instance details

Defined in Cek.Core

type Rep Value = D1 ('MetaData "Value" "Cek.Core" "cek-core-0.1.0.0-inplace" 'False) (C1 ('MetaCons "VClosure" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Env))) :+: C1 ('MetaCons "VLit" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Literal)))

newtype Env Source #

Lexical variable environment mapping identifiers to values.

Constructors

Env 

Fields

Instances

Instances details
Eq Env Source # 
Instance details

Defined in Cek.Core

Methods

(==) :: Env -> Env -> Bool

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

Generic Env Source # 
Instance details

Defined in Cek.Core

Associated Types

type Rep Env 
Instance details

Defined in Cek.Core

type Rep Env = D1 ('MetaData "Env" "Cek.Core" "cek-core-0.1.0.0-inplace" 'True) (C1 ('MetaCons "Env" 'PrefixI 'True) (S1 ('MetaSel ('Just "unEnv") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [(Text, Value)])))

Methods

from :: Env -> Rep Env x

to :: Rep Env x -> Env

Show Env Source # 
Instance details

Defined in Cek.Core

Methods

showsPrec :: Int -> Env -> ShowS

show :: Env -> String

showList :: [Env] -> ShowS

type Rep Env Source # 
Instance details

Defined in Cek.Core

type Rep Env = D1 ('MetaData "Env" "Cek.Core" "cek-core-0.1.0.0-inplace" 'True) (C1 ('MetaCons "Env" 'PrefixI 'True) (S1 ('MetaSel ('Just "unEnv") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [(Text, Value)])))

emptyEnv :: Env Source #

Construct an empty lexical environment.

extendEnv :: Text -> Value -> Env -> Env Source #

Extend an environment with a new variable-to-value binding.

lookupEnv :: Text -> Env -> Maybe Value Source #

Look up a variable in the environment.

Continuation Frames

data Frame Source #

Individual continuation frame representing pending reduction context.

Constructors

FrameArg !Term !Env

Waiting for function argument to evaluate

FrameFun !Value

Waiting for function body to apply after argument value is ready

FrameLet !Text !Term !Env

Waiting for let-bound variable computation

Instances

Instances details
Eq Frame Source # 
Instance details

Defined in Cek.Core

Methods

(==) :: Frame -> Frame -> Bool

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

Generic Frame Source # 
Instance details

Defined in Cek.Core

Associated Types

type Rep Frame 
Instance details

Defined in Cek.Core

type Rep Frame = D1 ('MetaData "Frame" "Cek.Core" "cek-core-0.1.0.0-inplace" 'False) (C1 ('MetaCons "FrameArg" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Env)) :+: (C1 ('MetaCons "FrameFun" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Value)) :+: C1 ('MetaCons "FrameLet" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Env)))))

Methods

from :: Frame -> Rep Frame x

to :: Rep Frame x -> Frame

Show Frame Source # 
Instance details

Defined in Cek.Core

Methods

showsPrec :: Int -> Frame -> ShowS

show :: Frame -> String

showList :: [Frame] -> ShowS

type Rep Frame Source # 
Instance details

Defined in Cek.Core

type Rep Frame = D1 ('MetaData "Frame" "Cek.Core" "cek-core-0.1.0.0-inplace" 'False) (C1 ('MetaCons "FrameArg" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Env)) :+: (C1 ('MetaCons "FrameFun" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Value)) :+: C1 ('MetaCons "FrameLet" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Env)))))

type Cont = [Frame] Source #

Evaluation continuation stack.

emptyCont :: Cont Source #

Construct an initial empty continuation stack.

Machine State & Execution

data CEKState Source #

Active state of the CEK abstract machine.

Constructors

StateEval !Term !Env !Cont

Evaluation state: active control term \(C\), environment \(E\), continuation \(K\)

StateReturn !Value !Cont

Return state: value \(V\) being passed into continuation stack \(K\)

Instances

Instances details
Eq CEKState Source # 
Instance details

Defined in Cek.Core

Methods

(==) :: CEKState -> CEKState -> Bool

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

Generic CEKState Source # 
Instance details

Defined in Cek.Core

Associated Types

type Rep CEKState 
Instance details

Defined in Cek.Core

type Rep CEKState = D1 ('MetaData "CEKState" "Cek.Core" "cek-core-0.1.0.0-inplace" 'False) (C1 ('MetaCons "StateEval" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Env) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Cont))) :+: C1 ('MetaCons "StateReturn" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Value) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Cont)))

Methods

from :: CEKState -> Rep CEKState x

to :: Rep CEKState x -> CEKState

Show CEKState Source # 
Instance details

Defined in Cek.Core

Methods

showsPrec :: Int -> CEKState -> ShowS

show :: CEKState -> String

showList :: [CEKState] -> ShowS

type Rep CEKState Source # 
Instance details

Defined in Cek.Core

type Rep CEKState = D1 ('MetaData "CEKState" "Cek.Core" "cek-core-0.1.0.0-inplace" 'False) (C1 ('MetaCons "StateEval" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Term) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Env) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Cont))) :+: C1 ('MetaCons "StateReturn" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Value) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Cont)))

stepCEK :: CEKState -> Either Text (Maybe CEKState) Source #

Perform a single deterministic step of the CEK abstract machine.

Returns: * 'Right (Just nextState)' on successful transition. * 'Right Nothing' when execution terminates in a final value. * 'Left err' when a runtime error (e.g. unbound variable, type mismatch) occurs.

evalCEK :: Term -> Either Text Value Source #

Evaluate a Term to a final Value under an empty environment and continuation.

runCEKWithFuel :: Word64 -> Term -> (Either Text Value, Word64) Source #

Fuel-bounded execution of a Term, returning either the computed value or fuel exhaustion.