pykinos-core
Safe HaskellNone
LanguageGHC2024

Pykinos.Schema.HiddenClass

Description

Eliminates dynamic Map Text Expr trees by interning field names into rank-ordered FieldIds and representing record shapes as interned SchemaIds with precomputed 3-way MergeMorph3 gather plans.

Architectural Rationale

Traditional AST record representations in interpreters (such as Dhall and 1ML) use unbalanced search trees or associative maps. When performing record operations (such as record merge //, deep meet /\, or signature projection :>), every operation traverses pointer-heavy tree structures, resulting in significant GC pressure and branch mispredictions.

By decoupling the field names into a frozen, rank-ordered Lexicon and mapping field sets into contiguous sorted PrimArrays of Word32s (SchemaId), record merge operations become pure array-to-array linear scans synthesized in $O(N_1 + N_2)$ time into unboxed 64-bit gather codes (MergeMorph3).

3-Way Gather Code Representation

Each gather code in MergeMorph3 is an unboxed Int64:

  1. Positive values (code >= 0): The field is copied directly from slot code of the left record.
  2. Negative values with bit 62 clear (code < 0, bit 62 == 0): The field is copied directly from slot complement code of the right record.
  3. Negative values with bit 62 set (code < 0, bit 62 == 1): Both records contain the field. A collision has occurred that must be resolved via recursive normalization (for OpCombineDeep). The two source slot indices (idx1, idx2) are packed into bits 0..59.
Synopsis

Field and Schema Identifiers

newtype FieldId Source #

Rank-ordered 32-bit field identifier.

Invariant: For all field identifiers fid1 and fid2, fid1 < fid2 holds if and only if text(fid1) < text(fid2) in standard lexicographical UTF-8 byte order. This invariant guarantees that sorting by FieldId is completely isomorphic to sorting by record label Text.

Constructors

FieldId 

Fields

Instances

Instances details
Eq FieldId Source # 
Instance details

Defined in Pykinos.Schema.HiddenClass

Methods

(==) :: FieldId -> FieldId -> Bool

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

Ord FieldId Source # 
Instance details

Defined in Pykinos.Schema.HiddenClass

Methods

compare :: FieldId -> FieldId -> Ordering

(<) :: FieldId -> FieldId -> Bool

(<=) :: FieldId -> FieldId -> Bool

(>) :: FieldId -> FieldId -> Bool

(>=) :: FieldId -> FieldId -> Bool

max :: FieldId -> FieldId -> FieldId

min :: FieldId -> FieldId -> FieldId

Show FieldId Source # 
Instance details

Defined in Pykinos.Schema.HiddenClass

Methods

showsPrec :: Int -> FieldId -> ShowS

show :: FieldId -> String

showList :: [FieldId] -> ShowS

newtype SchemaId Source #

Interned 32-bit identifier for a sorted PrimArray of FieldIds.

A SchemaId represents the immutable structural type of a record. Two records share the same SchemaId if and only if they possess identical field sets in identical order.

Constructors

SchemaId 

Fields

Instances

Instances details
Eq SchemaId Source # 
Instance details

Defined in Pykinos.Schema.HiddenClass

Methods

(==) :: SchemaId -> SchemaId -> Bool

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

Ord SchemaId Source # 
Instance details

Defined in Pykinos.Schema.HiddenClass

Methods

compare :: SchemaId -> SchemaId -> Ordering

(<) :: SchemaId -> SchemaId -> Bool

(<=) :: SchemaId -> SchemaId -> Bool

(>) :: SchemaId -> SchemaId -> Bool

(>=) :: SchemaId -> SchemaId -> Bool

max :: SchemaId -> SchemaId -> SchemaId

min :: SchemaId -> SchemaId -> SchemaId

Show SchemaId Source # 
Instance details

Defined in Pykinos.Schema.HiddenClass

Methods

showsPrec :: Int -> SchemaId -> ShowS

show :: SchemaId -> String

showList :: [SchemaId] -> ShowS

Dynamic Record Operations

data RecordOp Source #

Dynamic record algebra operations supported by MergeMorph3.

These correspond to the fundamental record combinators in languages with first-class records and modules:

Constructors

OpPreferR

Right-biased override union. When a label appears in both records, the value from the right-hand record overwrites the left-hand record. Corresponds to Dhall's // and 1ML structure include.

OpCombineDeep

Recursive deep meet. When a label appears in both records, the two values collide and must be recursively merged. Corresponds to Dhall's /\ and 1ML signature sharing.

OpRestrict

Domain restriction / projection. Restricts the fields of the left record to the schema defined by the right record. Corresponds to 1ML existential sealing (Mod :> Sig).

Instances

Instances details
Eq RecordOp Source # 
Instance details

Defined in Pykinos.Schema.HiddenClass

Methods

(==) :: RecordOp -> RecordOp -> Bool

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

Ord RecordOp Source # 
Instance details

Defined in Pykinos.Schema.HiddenClass

Methods

compare :: RecordOp -> RecordOp -> Ordering

(<) :: RecordOp -> RecordOp -> Bool

(<=) :: RecordOp -> RecordOp -> Bool

(>) :: RecordOp -> RecordOp -> Bool

(>=) :: RecordOp -> RecordOp -> Bool

max :: RecordOp -> RecordOp -> RecordOp

min :: RecordOp -> RecordOp -> RecordOp

Show RecordOp Source # 
Instance details

Defined in Pykinos.Schema.HiddenClass

Methods

showsPrec :: Int -> RecordOp -> ShowS

show :: RecordOp -> String

showList :: [RecordOp] -> ShowS

newtype MergeMorph3 Source #

3-way gather plan encoded in an unboxed PrimArray of Int64 codes.

The array length matches the number of fields in the resulting merged record. Each slot contains an instruction specifying how the target value is obtained:

  • code >= 0 : copy slot code from left record
  • code < 0, bit 62 == 0 : copy slot complement code from right record
  • code < 0, bit 62 == 1 : lazy deep-meet collision on (idx1, idx2)

Constructors

MergeMorph3 

Fields

Instances

Instances details
Eq MergeMorph3 Source # 
Instance details

Defined in Pykinos.Schema.HiddenClass

Methods

(==) :: MergeMorph3 -> MergeMorph3 -> Bool

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

Show MergeMorph3 Source # 
Instance details

Defined in Pykinos.Schema.HiddenClass

Methods

showsPrec :: Int -> MergeMorph3 -> ShowS

show :: MergeMorph3 -> String

showList :: [MergeMorph3] -> ShowS

Symbol Lexicon

data Lexicon Source #

Frozen rank-ordered lexicon mapping Text labels to FieldIds.

Provides bidirectional O(log L) lookup from Text to FieldId and O(1) indexed recovery from FieldId back to Text.

Constructors

Lexicon 

Fields

  • lexToId :: !(Map Text FieldId)

    Forward index mapping unique text labels to rank-ordered field IDs.

  • lexToText :: !(SmallArray Text)

    Reverse index enabling O(1) term reconstruction during pretty-printing.

Instances

Instances details
Show Lexicon Source # 
Instance details

Defined in Pykinos.Schema.HiddenClass

Methods

showsPrec :: Int -> Lexicon -> ShowS

show :: Lexicon -> String

showList :: [Lexicon] -> ShowS

buildLexicon :: [Text] -> Lexicon Source #

Construct a rank-ordered Lexicon from all field labels appearing in an AST.

Collects all unique labels, sorts them into strictly ascending order, and assigns zero-based sequential Word32 identifiers.

lookupFieldId :: Lexicon -> Text -> Maybe FieldId Source #

Resolve a Text label to its rank-ordered FieldId in O(log L) time.

lookupFieldText :: Lexicon -> FieldId -> Text Source #

Recover the original Text label from a FieldId in strictly O(1) time.

Slot Search and Gather Code Encoding

findFieldSlot :: PrimArray Word32 -> FieldId -> Maybe Int Source #

Branch-minimized binary search over a sorted PrimArray of Word32 field IDs.

Returns the slot index of the target FieldId if present, or Nothing. Operates in $O(log N)$ time with zero heap allocations.

collisionBit3 :: Int64 Source #

Bit 62 distinguishes a right-record slot from a two-record deep collision. Bit 63 is the sign bit (1 for negative numbers). Bit 62 serves as the discriminator flag.

encodeRightSlot3 :: Int -> Int64 Source #

Encode a right-record slot index idx2 as a negative Int64 with bit 62 clear.

Uses bitwise complement (complement) so that idx2 = 0 maps to -1, idx2 = 1 maps to -2, etc., ensuring strict negativity while keeping bit 62 zero.

encodeCollision3 :: Int -> Int -> Int64 Source #

Encode a colliding pair of slot indices (idx1, idx2) for OpCombineDeep.

Packs idx1 into bits 30..59 and idx2 into bits 0..29, sets bit 62, and negates.

decodeMergeCode3 :: Int64 -> Either (Either Int Int) (Int, Int) Source #

Decode a MergeMorph3 gather code into its constituent source slots:

  • Left (Left idx1) : Unambiguous value from slot idx1 of the left record.
  • Left (Right idx2) : Unambiguous value from slot idx2 of the right record.
  • Right (idx1, idx2) : Recursive collision between idx1 and idx2.

Linear Merge Synthesis

synthesizeMergeMorph3 :: RecordOp -> PrimArray Word32 -> PrimArray Word32 -> (PrimArray Word32, MergeMorph3) Source #

Two-finger linear synthesis of a 3-way MergeMorph3 over two sorted schemas.

Traverses the two sorted PrimArrays in parallel in $O(N_1 + N_2)$ time, emitting:

  1. The merged schema's sorted PrimArray of field IDs.
  2. The compiled MergeMorph3 gather codes for instantiating the merged record values.