cek-oneml
Safe HaskellNone
LanguageHaskell2010

Cek.OneML

Description

Models 1ML structures and signatures as flat SchemaId-indexed arrays of lazy SoAStore addresses, using MergeMorph3 for module include (OpPreferR) and existential sealing Mod :> Sig (OpRestrict).

Background: Rossberg's 1ML

Andreas Rossberg's "1ML — Core and the Modules in the Same Language" (ICFP 2015) showed that the traditional stratified distinction between ML core expressions and ML modules (structures, signatures, functors) can be completely dissolved. Modules become first-class records, functors become ordinary functions, and signatures become first-class record types with existential components.

Efficient Interpretation with MergeMorph3

Traditional implementations of 1ML require heavy elaborators that translate module constructs into System \(F_\omega\) terms with dynamic dictionary passing. This CEK machine implementation executes 1ML directly on flat unboxed structures:

  1. Structures and signatures are flat arrays of lazy cells indexed by FieldId and mapped to structural SchemaId identifiers in HiddenClass.
  2. Module extension (include M) compiles into a single $O(N_1 + N_2)$ linear merge plan (OpPreferR) via synthesizeMergeMorph3.
  3. Existential sealing (Mod :> Sig) computes a domain restriction (OpRestrict) in $O(|Sig|)$ time. For every abstract type field (SpecAbstract), the evaluator mints a fresh, globally unique skolem constant (CT_WNeu) in SoAStore, hiding the concrete implementation without dynamic wrapper closures.
**Demonstrative Reference Implementation**: This package is an educational
and demonstrative reference implementation showcasing Andreas Rossberg's 1ML
first-class module system over the CEK abstract machine with MergeMorph3
existential sealing and Pykinos hidden classes. It is provided primarily as an
executable architectural example within the @cek@ ecosystem documentation.
Synopsis

Type Specifications

data TypeSpec Source #

Type specifications inside a 1ML signature structure.

Constructors

SpecAbstract

Opaque abstract type field (type t): generates a fresh skolem CT_WNeu.

SpecManifest !OneMLTerm

Transparent type alias (type t = tau): preserves the underlying type cell.

SpecVal !OneMLTerm

Value field specification (val x : tau): preserves the lazy value thunk.

Instances

Instances details
Eq TypeSpec Source # 
Instance details

Defined in Cek.OneML

Methods

(==) :: TypeSpec -> TypeSpec -> Bool

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

Show TypeSpec Source # 
Instance details

Defined in Cek.OneML

Methods

showsPrec :: Int -> TypeSpec -> ShowS

show :: TypeSpec -> String

showList :: [TypeSpec] -> ShowS

1ML Syntax

data OneMLTerm Source #

Unified 1ML Core and Module AST.

Constructors

MVar !Int

Variable reference resolved via De Bruijn indexing into the environment.

MLit !Int32

32-bit signed integer literal value.

MReified !TypeSpec

Reified first-class type specification treated as a value.

MFunctor !OneMLTerm !OneMLTerm

First-class functor / function abstraction taking a domain signature and body.

MApp !OneMLTerm !OneMLTerm

Application of a functor to a module argument.

MLet !OneMLTerm !OneMLTerm

Lexical let binding introducing a local module or value definition.

MStruct !SchemaId !(SmallArray OneMLTerm)

First-class module structure pairing an interned SchemaId with component terms.

MProj !OneMLTerm !FieldId

Structure projection M.x retrieving the field labeled by FieldId.

MInclude !OneMLTerm !OneMLTerm

Asymmetric module inclusion / union include M1 in M2 using OpPreferR.

MSeal !OneMLTerm !OneMLTerm

Existential signature sealing M :> S using OpRestrict and skolem generation.

Instances

Instances details
Eq OneMLTerm Source # 
Instance details

Defined in Cek.OneML

Methods

(==) :: OneMLTerm -> OneMLTerm -> Bool

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

Show OneMLTerm Source # 
Instance details

Defined in Cek.OneML

Methods

showsPrec :: Int -> OneMLTerm -> ShowS

show :: OneMLTerm -> String

showList :: [OneMLTerm] -> ShowS

Values and Weak-Head Normal Forms

data OneMLWHNF Source #

Weak-Head Normal Form representation for 1ML values and modules.

Constructors

V1Lit !Int32

Unboxed integer literal value.

V1Spec !TypeSpec ![Int32]

Evaluated type specification closure.

V1Functor !Int32 !OneMLTerm ![Int32]

Evaluated functor closure capturing parameter domain address, body, and env.

V1Struct !SchemaId !(SmallArray Int32)

Evaluated structure pairing a SchemaId with an array of lazy store addresses.

V1AbstractType !Int32

Opaque existential skolem type constant generated during signature sealing.

Instances

Instances details
Eq OneMLWHNF Source # 
Instance details

Defined in Cek.OneML

Methods

(==) :: OneMLWHNF -> OneMLWHNF -> Bool

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

Show OneMLWHNF Source # 
Instance details

Defined in Cek.OneML

Methods

showsPrec :: Int -> OneMLWHNF -> ShowS

show :: OneMLWHNF -> String

showList :: [OneMLWHNF] -> ShowS

Continuation Frame Tags

pattern FT_1ML_App :: FrameTag Source #

Frame tag for evaluating a functor argument cell.

pattern FT_1ML_Proj :: FrameTag Source #

Frame tag for projecting a field from an evaluated module structure.

pattern FT_1ML_IncludeL :: FrameTag Source #

Frame tag for evaluating the left-hand module in an include (//) expression.

pattern FT_1ML_IncludeR :: FrameTag Source #

Frame tag for evaluating the right-hand module in an include (//) expression.

pattern FT_1ML_SealSig :: FrameTag Source #

Frame tag for evaluating the signature expression in existential sealing.

pattern FT_1ML_SealMod :: FrameTag Source #

Frame tag for applying signature restriction and skolemization to the module structure.

Sealing and Evaluation

stepOneMLSealing :: SoAStore s -> MutableByteArray s -> MergeMorph3 -> SchemaId -> SmallArray Int32 -> SmallArray Int32 -> ST s OneMLWHNF Source #

Execute 1ML existential sealing (Mod :> Sig) in O(|Sig|) using MergeMorph3 with OpRestrict. Private fields omitted from Sig are never forced, while SpecAbstract fields mint fresh skolem symbols in SoAStore.

evalOneML :: (SchemaId -> PrimArray Word32) -> (PrimArray Word32 -> SchemaId) -> OneMLTerm -> OneMLWHNF Source #

Evaluate a closed 1ML expression given a schema lookup table mapping SchemaId to its sorted 'PrimArray Word32' field descriptor.