| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
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:
- Pure syntactic expressions (such as arithmetic operations
SAdd,SSub,SMul, comparisonsSEq, and sequencingSSeq) require zero manual frame management: they are evaluated left-to-right automatically by GHC's derivedGeneric1dissection (FT_PureDissect). - Mutable variables (
set!) manipulate memory cells allocated in the unboxedSoAStore. - First-class continuations (
call/cc) capture the entire 32-frame unrolledKontStackin 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
- newtype Fix (f :: Type -> Type) = In {}
- data SchemeF r
- type SchemeTerm = Fix SchemeF
- data SchemeVal s
- newtype SchemeEnv = SchemeEnv (SmallArray Int32)
- data SchemeError
- = ErrOutOfFuel
- | ErrTypeError !String
- | ErrArityMismatch !Int !Int
- | ErrUnboundVar !Int
- pattern FT_SchemeIf :: FrameTag
- pattern FT_SchemeSet :: FrameTag
- pattern FT_SchemeCallCC :: FrameTag
- pattern FT_SchemeAppFn :: FrameTag
- pattern FT_SchemeAppArgs :: FrameTag
- evalScheme :: Int -> SchemeTerm -> Either SchemeError (Either Bool Int32)
Syntax Functors and Trees
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 ( |
| SVar !Int | Lexical variable reference indexed by De Bruijn offset into |
| SLam !Int !r | Lambda abstraction with arity and body term. |
| SLet !r !r | Let binding |
| SSet !Int !r | Mutable lexical assignment |
| SIf !r !r !r | Short-circuiting conditional expression |
| SCallCC !r | First-class continuation reification |
| SApp !r ![r] | Multi-argument function or continuation application |
| SAdd !r !r | Pure primitive addition |
| SSub !r !r | Pure primitive subtraction |
| SMul !r !r | Pure primitive multiplication |
| SEq !r !r | Pure primitive equality comparison |
| SSeq !r !r | Sequencing expression |
Instances
| Dissectable SchemeF Source # | |||||
| Functor SchemeF Source # | |||||
| Generic1 SchemeF Source # | |||||
Defined in Cek.Scheme Associated Types
| |||||
| Show r => Show (SchemeF r) Source # | |||||
| type Dissect SchemeF Source # | |||||
Defined in Cek.Scheme | |||||
| type Rep1 SchemeF Source # | |||||
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
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 |
| 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. |
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
| Eq SchemeError Source # | |
Defined in Cek.Scheme | |
| Show SchemeError Source # | |
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).