cek-scheme
Safe HaskellNone
LanguageHaskell2010

Cek.Scheme

Description

Demonstrates how a language can combine automatic polynomial functor dissection (Dissectable derived via Generic1) for uniform left-to-right expression spines with explicit control and state effects (SCallCC, SSet) backed by KontStack and SoAStore.

Architecture of the Scheme Showcase

The Scheme interpreter models R5RS/R7RS Call-by-Value semantics:

  1. Pure syntactic expressions (such as arithmetic operations SAdd, SSub, SMul, comparisons SEq, and sequencing SSeq) require zero manual frame management: they are evaluated left-to-right automatically by GHC's derived Generic1 dissection (FT_PureDissect).
  2. Mutable variables (set!) manipulate memory cells allocated in the unboxed SoAStore.
  3. First-class continuations (call/cc) capture the entire 32-frame unrolled KontStack in strictly $O(1)$ time via Copy-on-Write (captureKontSoA). Invoking a captured continuation reinstates the execution context immediately (restoreKontSoA) with zero stack unwinding overhead.
**Demonstrative Reference Implementation**: This package is an educational
and demonstrative reference implementation showcasing CEK machine execution with
first-class continuations (@call/cc@), mutable lexical state, and automatic
GHC 'Generic1' polynomial functor dissection. It is provided primarily as an
executable architectural example within the @cek@ ecosystem documentation.
Synopsis

Syntax Functors and Trees

newtype Fix (f :: Type -> Type) Source #

Fixed-point wrapper for recursive syntax functors.

Constructors

In 

Fields

data SchemeF r Source #

Base pattern functor for the Call-by-Value Scheme showcase calculus.

Uniform expression nodes (SAdd, SSub, SMul, SEq, SSeq) are handled automatically by the derived Generic1 Dissectable instance. Special binding/control forms (SVar, SLam, SLet, SSet, SIf, SCallCC, SApp) are intercepted in the evaluator before entering generic dissection.

Constructors

SLit !Int32

32-bit signed integer literal constant.

SBool !Bool

Boolean literal constant (#t or #f).

SVar !Int

Lexical variable reference indexed by De Bruijn offset into SchemeEnv.

SLam !Int !r

Lambda abstraction with arity and body term.

SLet !r !r

Let binding (let ((x rhs)) body) desugared to immediate 1-argument application.

SSet !Int !r

Mutable lexical assignment (set! var rhs) mutating the SoAStore cell in place.

SIf !r !r !r

Short-circuiting conditional expression (if cond thenBranch elseBranch).

SCallCC !r

First-class continuation reification (call/cc (lambda (k) ...)).

SApp !r ![r]

Multi-argument function or continuation application (fn arg0 arg1 ...).

SAdd !r !r

Pure primitive addition (+ a b).

SSub !r !r

Pure primitive subtraction (- a b).

SMul !r !r

Pure primitive multiplication (* a b).

SEq !r !r

Pure primitive equality comparison (= a b).

SSeq !r !r

Sequencing expression (begin e1 e2).

Instances

Instances details
Dissectable SchemeF Source # 
Instance details

Defined in Cek.Scheme

Associated Types

type Dissect SchemeF 
Instance details

Defined in Cek.Scheme

type Dissect SchemeF = GenericDissect (Rep1 SchemeF)

Methods

stepIn :: SchemeF j -> Either (SchemeF c) (j, Dissect SchemeF c j)

stepNext :: c -> Dissect SchemeF c j -> Either (SchemeF c) (j, Dissect SchemeF c j)

Functor SchemeF Source # 
Instance details

Defined in Cek.Scheme

Methods

fmap :: (a -> b) -> SchemeF a -> SchemeF b

(<$) :: a -> SchemeF b -> SchemeF a

Generic1 SchemeF Source # 
Instance details

Defined in Cek.Scheme

Associated Types

type Rep1 SchemeF 
Instance details

Defined in Cek.Scheme

type Rep1 SchemeF = D1 ('MetaData "SchemeF" "Cek.Scheme" "cek-scheme-0.1.0.0-inplace" 'False) (((C1 ('MetaCons "SLit" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int32)) :+: (C1 ('MetaCons "SBool" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Bool)) :+: C1 ('MetaCons "SVar" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int)))) :+: ((C1 ('MetaCons "SLam" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1) :+: C1 ('MetaCons "SLet" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1)) :+: (C1 ('MetaCons "SSet" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1) :+: C1 ('MetaCons "SIf" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1))))) :+: ((C1 ('MetaCons "SCallCC" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1) :+: (C1 ('MetaCons "SApp" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec1 [])) :+: C1 ('MetaCons "SAdd" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1))) :+: ((C1 ('MetaCons "SSub" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1) :+: C1 ('MetaCons "SMul" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1)) :+: (C1 ('MetaCons "SEq" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1) :+: C1 ('MetaCons "SSeq" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1)))))

Methods

from1 :: SchemeF a -> Rep1 SchemeF a

to1 :: Rep1 SchemeF a -> SchemeF a

Show r => Show (SchemeF r) Source # 
Instance details

Defined in Cek.Scheme

Methods

showsPrec :: Int -> SchemeF r -> ShowS

show :: SchemeF r -> String

showList :: [SchemeF r] -> ShowS

type Dissect SchemeF Source # 
Instance details

Defined in Cek.Scheme

type Dissect SchemeF = GenericDissect (Rep1 SchemeF)
type Rep1 SchemeF Source # 
Instance details

Defined in Cek.Scheme

type Rep1 SchemeF = D1 ('MetaData "SchemeF" "Cek.Scheme" "cek-scheme-0.1.0.0-inplace" 'False) (((C1 ('MetaCons "SLit" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int32)) :+: (C1 ('MetaCons "SBool" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Bool)) :+: C1 ('MetaCons "SVar" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int)))) :+: ((C1 ('MetaCons "SLam" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1) :+: C1 ('MetaCons "SLet" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1)) :+: (C1 ('MetaCons "SSet" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1) :+: C1 ('MetaCons "SIf" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1))))) :+: ((C1 ('MetaCons "SCallCC" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1) :+: (C1 ('MetaCons "SApp" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec1 [])) :+: C1 ('MetaCons "SAdd" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1))) :+: ((C1 ('MetaCons "SSub" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1) :+: C1 ('MetaCons "SMul" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1)) :+: (C1 ('MetaCons "SEq" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1) :+: C1 ('MetaCons "SSeq" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1)))))

type SchemeTerm = Fix SchemeF Source #

Closed syntax tree for the Scheme calculus.

Values and Environments

data SchemeVal s Source #

Runtime values of the Scheme abstract machine.

Note that VCont holds an O(1) copy-on-write snapshot of the 32-frame SoA KontStack.

Constructors

VInt !Int32

Evaluated integer value.

VBool !Bool

Evaluated boolean truth value.

VUnit

Evaluated unit value returned by side-effecting operations like set!.

VClosure !Int !SchemeTerm !SchemeEnv

Lexical closure capturing arity, AST body, and captured environment.

VCont !(CapturedKont s)

First-class captured continuation snapshot supporting multi-shot resumption.

newtype SchemeEnv Source #

Lexical environment mapping De Bruijn indices to mutable SoAStore cell addresses.

Constructors

SchemeEnv (SmallArray Int32) 

data SchemeError Source #

Terminal outcome or error condition for a fuel-bounded Scheme evaluation.

Constructors

ErrOutOfFuel

Evaluation step counter reached zero before termination.

ErrTypeError !String

Value type did not match expected operator signature.

ErrArityMismatch !Int !Int

Actual argument count did not match procedure arity.

ErrUnboundVar !Int

Variable De Bruijn index exceeded current lexical environment depth.

Instances

Instances details
Eq SchemeError Source # 
Instance details

Defined in Cek.Scheme

Methods

(==) :: SchemeError -> SchemeError -> Bool

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

Show SchemeError Source # 
Instance details

Defined in Cek.Scheme

Methods

showsPrec :: Int -> SchemeError -> ShowS

show :: SchemeError -> String

showList :: [SchemeError] -> ShowS

Continuation Frame Tags

pattern FT_SchemeIf :: FrameTag Source #

Custom continuation frame tags for Scheme special forms in KontStack.

pattern FT_SchemeSet :: FrameTag Source #

Frame tag for pending assignment to a variable cell address.

pattern FT_SchemeCallCC :: FrameTag Source #

Frame tag for evaluating the receiver of call/cc.

pattern FT_SchemeAppFn :: FrameTag Source #

Frame tag for evaluating a function term before its arguments.

pattern FT_SchemeAppArgs :: FrameTag Source #

Frame tag for evaluating an argument list spine left-to-right.

Evaluator Entry Point

evalScheme :: Int -> SchemeTerm -> Either SchemeError (Either Bool Int32) Source #

Run a fuel-bounded Call-by-Value Scheme evaluation using KontStack and SoAStore.

Returns the terminal integer or boolean result (or SchemeError on fuel exhaustion).