| Copyright | (c) Nadia Yvette Chambers 2026 |
|---|---|
| License | EUPL-1.2 |
| Maintainer | Nadia Chambers <nadia.yvette.chambers@ik.me> |
| Stability | experimental |
| Portability | non-portable (GHC extensions) |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
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
- data Term
- data Literal
- data Value
- newtype Env = Env {}
- emptyEnv :: Env
- extendEnv :: Text -> Value -> Env -> Env
- lookupEnv :: Text -> Env -> Maybe Value
- data Frame
- type Cont = [Frame]
- emptyCont :: Cont
- data CEKState
- stepCEK :: CEKState -> Either Text (Maybe CEKState)
- evalCEK :: Term -> Either Text Value
- runCEKWithFuel :: Word64 -> Term -> (Either Text Value, Word64)
Abstract Syntax
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 ( |
Instances
| Eq Term Source # | |||||
| Generic Term Source # | |||||
Defined in Cek.Core Associated Types
| |||||
| Show Term Source # | |||||
| type Rep Term Source # | |||||
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)))))) | |||||
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
| Eq Literal Source # | |||||
| Generic Literal Source # | |||||
Defined in Cek.Core Associated Types
| |||||
| Show Literal Source # | |||||
| type Rep Literal Source # | |||||
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
Evaluated runtime values.
Constructors
| VClosure !Text !Term !Env | Lexical closure \(\langle x, e, E \rangle\) |
| VLit !Literal | Primitive literal value |
Instances
| Eq Value Source # | |||||
| Generic Value Source # | |||||
Defined in Cek.Core Associated Types
| |||||
| Show Value Source # | |||||
| type Rep Value Source # | |||||
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))) | |||||
Lexical variable environment mapping identifiers to values.
extendEnv :: Text -> Value -> Env -> Env Source #
Extend an environment with a new variable-to-value binding.
Continuation Frames
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
| Eq Frame Source # | |||||
| Generic Frame Source # | |||||
Defined in Cek.Core Associated Types
| |||||
| Show Frame Source # | |||||
| type Rep Frame Source # | |||||
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))))) | |||||
Machine State & Execution
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
| Eq CEKState Source # | |||||
| Generic CEKState Source # | |||||
Defined in Cek.Core Associated Types
| |||||
| Show CEKState Source # | |||||
| type Rep CEKState Source # | |||||
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.